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 374a658b32SHong Zhang . -ts_time_span <t0,...tf> - sets the time span, solutions are computed and stored for each indicated time 38ef85077eSLisandro Dalcin . -ts_max_steps <steps> - maximum number of time-steps to take 39ef85077eSLisandro Dalcin . -ts_init_time <time> - initial time to start computation 40195e9b02SBarry Smith . -ts_final_time <time> - final time to compute to (deprecated: use `-ts_max_time`) 413e4cdcaaSBarry Smith . -ts_dt <dt> - initial time step 421628793fSSatish 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 43a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 44a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 45a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 46a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 4767b8a455SSatish Balay . -ts_atol <atol> - Absolute tolerance for local truncation error 48f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function 49b43aa488SJacob Faibussowitsch . -ts_rhs_jacobian_test_mult_transpose - test the Jacobian at each iteration against finite difference with RHS function 50195e9b02SBarry Smith . -ts_adjoint_solve <yes,no> - After solving the ODE/DAE solve the adjoint problem (requires `-ts_save_trajectory`) 51847ff0e1SMatthew G. Knepley . -ts_fd_color - Use finite differences with coloring to compute IJacobian 52bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 53aee7a9fbSMatthew G. Knepley . -ts_monitor_cancel - Cancel all monitors 54de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 55de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 567cf37e64SBarry Smith . -ts_monitor_error - Monitors norm of error 576934998bSLisandro Dalcin . -ts_monitor_lg_timestep - Monitor timestep size graphically 588b668821SLisandro Dalcin . -ts_monitor_lg_timestep_log - Monitor log timestep size graphically 59de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 60de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 61de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 62de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 633e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 643e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 65fde5950dSBarry Smith . -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep 66b43aa488SJacob Faibussowitsch . -ts_monitor_solution_interval <interval> - output once every interval (default=1) time steps 6763a3b9bcSJacob 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) 689e336e28SPatrick Sanan - -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 6953ea634cSHong Zhang 70bcf0153eSBarry Smith Level: beginner 713d5a8a6aSBarry Smith 72bcf0153eSBarry Smith Notes: 73bcf0153eSBarry Smith See `SNESSetFromOptions()` and `KSPSetFromOptions()` for how to control the nonlinear and linear solves used by the time-stepper. 74bcf0153eSBarry Smith 75195e9b02SBarry Smith Certain `SNES` options get reset for each new nonlinear solver, for example `-snes_lag_jacobian its` and `-snes_lag_preconditioner its`, in order 76195e9b02SBarry Smith to retain them over the multiple nonlinear solves that `TS` uses you mush also provide `-snes_lag_jacobian_persists true` and 77195e9b02SBarry Smith `-snes_lag_preconditioner_persists true` 783d5a8a6aSBarry Smith 79b43aa488SJacob Faibussowitsch Developer Notes: 809e336e28SPatrick Sanan We should unify all the -ts_monitor options in the way that -xxx_view has been unified 81bdad233fSMatthew Knepley 821cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetType()` 83bdad233fSMatthew Knepley @*/ 84d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetFromOptions(TS ts) 85d71ae5a4SJacob Faibussowitsch { 86bc952696SBarry Smith PetscBool opt, flg, tflg; 87eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 884a658b32SHong Zhang PetscReal time_step, tspan[100]; 894a658b32SHong Zhang PetscInt nt = PETSC_STATIC_ARRAY_LENGTH(tspan); 9049354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 91d1212d36SBarry Smith char dir[16]; 928434afd1SBarry Smith TSIFunctionFn *ifun; 936991f827SBarry Smith const char *defaultType; 946991f827SBarry Smith char typeName[256]; 95bdad233fSMatthew Knepley 96bdad233fSMatthew Knepley PetscFunctionBegin; 970700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 986991f827SBarry Smith 999566063dSJacob Faibussowitsch PetscCall(TSRegisterAll()); 1009566063dSJacob Faibussowitsch PetscCall(TSGetIFunction(ts, NULL, &ifun, NULL)); 101cd11d68dSLisandro Dalcin 102d0609cedSBarry Smith PetscObjectOptionsBegin((PetscObject)ts); 1031ef27442SStefano Zampini if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 1041ef27442SStefano Zampini else defaultType = ifun ? TSBEULER : TSEULER; 1059566063dSJacob Faibussowitsch PetscCall(PetscOptionsFList("-ts_type", "TS method", "TSSetType", TSList, defaultType, typeName, 256, &opt)); 1061e66621cSBarry Smith if (opt) PetscCall(TSSetType(ts, typeName)); 1071e66621cSBarry Smith else PetscCall(TSSetType(ts, defaultType)); 108bdad233fSMatthew Knepley 109bdad233fSMatthew Knepley /* Handle generic TS options */ 1109566063dSJacob Faibussowitsch PetscCall(PetscOptionsDeprecated("-ts_final_time", "-ts_max_time", "3.10", NULL)); 1119566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_max_time", "Maximum time to run to", "TSSetMaxTime", ts->max_time, &ts->max_time, NULL)); 1124a658b32SHong Zhang PetscCall(PetscOptionsRealArray("-ts_time_span", "Time span", "TSSetTimeSpan", tspan, &nt, &flg)); 1134a658b32SHong Zhang if (flg) PetscCall(TSSetTimeSpan(ts, nt, tspan)); 1149566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_max_steps", "Maximum number of time steps", "TSSetMaxSteps", ts->max_steps, &ts->max_steps, NULL)); 1159566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_init_time", "Initial time", "TSSetTime", ts->ptime, &ts->ptime, NULL)); 1169566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_dt", "Initial time step", "TSSetTimeStep", ts->time_step, &time_step, &flg)); 1179566063dSJacob Faibussowitsch if (flg) PetscCall(TSSetTimeStep(ts, time_step)); 1189566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum("-ts_exact_final_time", "Option for handling of final time step", "TSSetExactFinalTime", TSExactFinalTimeOptions, (PetscEnum)ts->exact_final_time, (PetscEnum *)&eftopt, &flg)); 1199566063dSJacob Faibussowitsch if (flg) PetscCall(TSSetExactFinalTime(ts, eftopt)); 12009cb0f53SBarry Smith PetscCall(PetscOptionsInt("-ts_max_snes_failures", "Maximum number of nonlinear solve failures", "TSSetMaxSNESFailures", ts->max_snes_failures, &ts->max_snes_failures, &flg)); 12109cb0f53SBarry Smith if (flg) PetscCall(TSSetMaxSNESFailures(ts, ts->max_snes_failures)); 12209cb0f53SBarry Smith PetscCall(PetscOptionsInt("-ts_max_reject", "Maximum number of step rejections before step fails", "TSSetMaxStepRejections", ts->max_reject, &ts->max_reject, &flg)); 12309cb0f53SBarry Smith if (flg) PetscCall(TSSetMaxStepRejections(ts, ts->max_reject)); 1249566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_error_if_step_fails", "Error if no step succeeds", "TSSetErrorIfStepFails", ts->errorifstepfailed, &ts->errorifstepfailed, NULL)); 12509cb0f53SBarry Smith PetscCall(PetscOptionsBoundedReal("-ts_rtol", "Relative tolerance for local truncation error", "TSSetTolerances", ts->rtol, &ts->rtol, NULL, 0)); 12609cb0f53SBarry Smith PetscCall(PetscOptionsBoundedReal("-ts_atol", "Absolute tolerance for local truncation error", "TSSetTolerances", ts->atol, &ts->atol, NULL, 0)); 127bdad233fSMatthew Knepley 1289566063dSJacob 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)); 1299566063dSJacob 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)); 1309566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_use_splitrhsfunction", "Use the split RHS function for multirate solvers ", "TSSetUseSplitRHSFunction", ts->use_splitrhsfunction, &ts->use_splitrhsfunction, NULL)); 13156f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 13256f85f32SBarry Smith { 13356f85f32SBarry Smith PetscBool set; 13456f85f32SBarry Smith flg = PETSC_FALSE; 1359566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_saws_block", "Block for SAWs memory snooper at end of TSSolve", "PetscObjectSAWsBlock", ((PetscObject)ts)->amspublishblock, &flg, &set)); 1361baa6e33SBarry Smith if (set) PetscCall(PetscObjectSAWsSetBlock((PetscObject)ts, flg)); 13756f85f32SBarry Smith } 13856f85f32SBarry Smith #endif 13956f85f32SBarry Smith 140bdad233fSMatthew Knepley /* Monitor options */ 1419566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_frequency", "Number of time steps between monitor output", "TSMonitorSetFrequency", ts->monitorFrequency, &ts->monitorFrequency, NULL)); 1429566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor", "Monitor time and timestep size", "TSMonitorDefault", TSMonitorDefault, NULL)); 1439566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor_extreme", "Monitor extreme values of the solution", "TSMonitorExtreme", TSMonitorExtreme, NULL)); 1449566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor_solution", "View the solution at each timestep", "TSMonitorSolution", TSMonitorSolution, NULL)); 1459566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_dmswarm_monitor_moments", "Monitor moments of particle distribution", "TSDMSwarmMonitorMoments", TSDMSwarmMonitorMoments, NULL)); 146fde5950dSBarry Smith 1479566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_python", "Use Python function", "TSMonitorSet", NULL, monfilename, sizeof(monfilename), &flg)); 1489566063dSJacob Faibussowitsch if (flg) PetscCall(PetscPythonMonitorSet((PetscObject)ts, monfilename)); 1495180491cSLisandro Dalcin 1509566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_solution", "Monitor solution graphically", "TSMonitorLGSolution", &opt)); 151b3603a34SBarry Smith if (opt) { 1523923b477SBarry Smith PetscInt howoften = 1; 153e669de00SBarry Smith DM dm; 154e669de00SBarry Smith PetscBool net; 155b3603a34SBarry Smith 1569566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_solution", "Monitor solution graphically", "TSMonitorLGSolution", howoften, &howoften, NULL)); 1579566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 1589566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)dm, DMNETWORK, &net)); 159e669de00SBarry Smith if (net) { 160e669de00SBarry Smith TSMonitorLGCtxNetwork ctx; 1619566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxNetworkCreate(ts, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 600, 400, howoften, &ctx)); 1629566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGCtxNetworkSolution, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxNetworkDestroy)); 1639566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_lg_solution_semilogy", "Plot the solution with a semi-log axis", "", ctx->semilogy, &ctx->semilogy, NULL)); 164e669de00SBarry Smith } else { 165e669de00SBarry Smith TSMonitorLGCtx ctx; 1669566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 1679566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGSolution, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy)); 168bdad233fSMatthew Knepley } 169e669de00SBarry Smith } 1706ba87a44SLisandro Dalcin 1719566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_error", "Monitor error graphically", "TSMonitorLGError", &opt)); 172ef20d060SBarry Smith if (opt) { 1730b039ecaSBarry Smith TSMonitorLGCtx ctx; 1743923b477SBarry Smith PetscInt howoften = 1; 175ef20d060SBarry Smith 1769566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_error", "Monitor error graphically", "TSMonitorLGError", howoften, &howoften, NULL)); 1779566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 1789566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGError, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy)); 179ef20d060SBarry Smith } 1809566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor_error", "View the error at each timestep", "TSMonitorError", TSMonitorError, NULL)); 1817cf37e64SBarry Smith 1829566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_timestep", "Monitor timestep size graphically", "TSMonitorLGTimeStep", &opt)); 1836934998bSLisandro Dalcin if (opt) { 1846934998bSLisandro Dalcin TSMonitorLGCtx ctx; 1856934998bSLisandro Dalcin PetscInt howoften = 1; 1866934998bSLisandro Dalcin 1879566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_timestep", "Monitor timestep size graphically", "TSMonitorLGTimeStep", howoften, &howoften, NULL)); 1889566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 1899566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGTimeStep, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy)); 1906934998bSLisandro Dalcin } 1919566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_timestep_log", "Monitor log timestep size graphically", "TSMonitorLGTimeStep", &opt)); 1928b668821SLisandro Dalcin if (opt) { 1938b668821SLisandro Dalcin TSMonitorLGCtx ctx; 1948b668821SLisandro Dalcin PetscInt howoften = 1; 1958b668821SLisandro Dalcin 1969566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_timestep_log", "Monitor log timestep size graphically", "TSMonitorLGTimeStep", howoften, &howoften, NULL)); 1979566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 1989566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGTimeStep, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy)); 1998b668821SLisandro Dalcin ctx->semilogy = PETSC_TRUE; 2008b668821SLisandro Dalcin } 2018b668821SLisandro Dalcin 2029566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_snes_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGSNESIterations", &opt)); 203201da799SBarry Smith if (opt) { 204201da799SBarry Smith TSMonitorLGCtx ctx; 205201da799SBarry Smith PetscInt howoften = 1; 206201da799SBarry Smith 2079566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_snes_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGSNESIterations", howoften, &howoften, NULL)); 2089566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 2099566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGSNESIterations, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy)); 210201da799SBarry Smith } 2119566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_ksp_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGKSPIterations", &opt)); 212201da799SBarry Smith if (opt) { 213201da799SBarry Smith TSMonitorLGCtx ctx; 214201da799SBarry Smith PetscInt howoften = 1; 215201da799SBarry Smith 2169566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_ksp_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGKSPIterations", howoften, &howoften, NULL)); 2179566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 2189566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGKSPIterations, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy)); 219201da799SBarry Smith } 2209566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_sp_eig", "Monitor eigenvalues of linearized operator graphically", "TSMonitorSPEig", &opt)); 2218189c53fSBarry Smith if (opt) { 2228189c53fSBarry Smith TSMonitorSPEigCtx ctx; 2238189c53fSBarry Smith PetscInt howoften = 1; 2248189c53fSBarry Smith 2259566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_sp_eig", "Monitor eigenvalues of linearized operator graphically", "TSMonitorSPEig", howoften, &howoften, NULL)); 2269566063dSJacob Faibussowitsch PetscCall(TSMonitorSPEigCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx)); 2279566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorSPEig, ctx, (PetscErrorCode(*)(void **))TSMonitorSPEigCtxDestroy)); 2288189c53fSBarry Smith } 22960e16b1bSMatthew G. Knepley PetscCall(PetscOptionsName("-ts_monitor_sp_swarm", "Display particle phase space from the DMSwarm", "TSMonitorSPSwarm", &opt)); 2301b575b74SJoseph Pusztay if (opt) { 2311b575b74SJoseph Pusztay TSMonitorSPCtx ctx; 232d7462660SMatthew Knepley PetscInt howoften = 1, retain = 0; 23360e16b1bSMatthew G. Knepley PetscBool phase = PETSC_TRUE, create = PETSC_TRUE, multispecies = PETSC_FALSE; 234d7462660SMatthew Knepley 2359371c9d4SSatish Balay for (PetscInt i = 0; i < ts->numbermonitors; ++i) 2369371c9d4SSatish Balay if (ts->monitor[i] == TSMonitorSPSwarmSolution) { 2379371c9d4SSatish Balay create = PETSC_FALSE; 2389371c9d4SSatish Balay break; 2399371c9d4SSatish Balay } 2406a5217c0SMatthew G. Knepley if (create) { 24160e16b1bSMatthew G. Knepley PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm", "Display particles phase space from the DMSwarm", "TSMonitorSPSwarm", howoften, &howoften, NULL)); 2429566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm_retain", "Retain n points plotted to show trajectory, -1 for all points", "TSMonitorSPSwarm", retain, &retain, NULL)); 2439566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_sp_swarm_phase", "Plot in phase space rather than coordinate space", "TSMonitorSPSwarm", phase, &phase, NULL)); 24460e16b1bSMatthew G. Knepley PetscCall(PetscOptionsBool("-ts_monitor_sp_swarm_multi_species", "Color particles by particle species", "TSMonitorSPSwarm", multispecies, &multispecies, NULL)); 24560e16b1bSMatthew G. Knepley PetscCall(TSMonitorSPCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, retain, phase, multispecies, &ctx)); 2469566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode(*)(void **))TSMonitorSPCtxDestroy)); 2471b575b74SJoseph Pusztay } 2486a5217c0SMatthew G. Knepley } 24960e16b1bSMatthew G. Knepley PetscCall(PetscOptionsName("-ts_monitor_hg_swarm", "Display particle histogram from the DMSwarm", "TSMonitorHGSwarm", &opt)); 25060e16b1bSMatthew G. Knepley if (opt) { 25160e16b1bSMatthew G. Knepley TSMonitorHGCtx ctx; 25260e16b1bSMatthew G. Knepley PetscInt howoften = 1, Ns = 1; 25360e16b1bSMatthew G. Knepley PetscBool velocity = PETSC_FALSE, create = PETSC_TRUE; 25460e16b1bSMatthew G. Knepley 25560e16b1bSMatthew G. Knepley for (PetscInt i = 0; i < ts->numbermonitors; ++i) 25660e16b1bSMatthew G. Knepley if (ts->monitor[i] == TSMonitorHGSwarmSolution) { 25760e16b1bSMatthew G. Knepley create = PETSC_FALSE; 25860e16b1bSMatthew G. Knepley break; 25960e16b1bSMatthew G. Knepley } 26060e16b1bSMatthew G. Knepley if (create) { 26160e16b1bSMatthew G. Knepley DM sw, dm; 26260e16b1bSMatthew G. Knepley PetscInt Nc, Nb; 26360e16b1bSMatthew G. Knepley 26460e16b1bSMatthew G. Knepley PetscCall(TSGetDM(ts, &sw)); 26560e16b1bSMatthew G. Knepley PetscCall(DMSwarmGetCellDM(sw, &dm)); 26660e16b1bSMatthew G. Knepley PetscCall(DMPlexGetHeightStratum(dm, 0, NULL, &Nc)); 26760e16b1bSMatthew G. Knepley Nb = PetscMin(20, PetscMax(10, Nc)); 26860e16b1bSMatthew G. Knepley PetscCall(PetscOptionsInt("-ts_monitor_hg_swarm", "Display particles histogram from the DMSwarm", "TSMonitorHGSwarm", howoften, &howoften, NULL)); 26960e16b1bSMatthew G. Knepley PetscCall(PetscOptionsBool("-ts_monitor_hg_swarm_velocity", "Plot in velocity space rather than coordinate space", "TSMonitorHGSwarm", velocity, &velocity, NULL)); 27060e16b1bSMatthew G. Knepley PetscCall(PetscOptionsInt("-ts_monitor_hg_swarm_species", "Number of species to histogram", "TSMonitorHGSwarm", Ns, &Ns, NULL)); 27160e16b1bSMatthew G. Knepley PetscCall(PetscOptionsInt("-ts_monitor_hg_swarm_bins", "Number of histogram bins", "TSMonitorHGSwarm", Nb, &Nb, NULL)); 27260e16b1bSMatthew G. Knepley PetscCall(TSMonitorHGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, Ns, Nb, velocity, &ctx)); 27360e16b1bSMatthew G. Knepley PetscCall(TSMonitorSet(ts, TSMonitorHGSwarmSolution, ctx, (PetscErrorCode(*)(void **))TSMonitorHGCtxDestroy)); 27460e16b1bSMatthew G. Knepley } 27560e16b1bSMatthew G. Knepley } 276ef20d060SBarry Smith opt = PETSC_FALSE; 2779566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution", "Monitor solution graphically", "TSMonitorDrawSolution", &opt)); 278a7cc72afSBarry Smith if (opt) { 27983a4ac43SBarry Smith TSMonitorDrawCtx ctx; 28083a4ac43SBarry Smith PetscInt howoften = 1; 281a80ad3e0SBarry Smith 2829566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_solution", "Monitor solution graphically", "TSMonitorDrawSolution", howoften, &howoften, NULL)); 2839566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, "Computed Solution", PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx)); 2849566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorDrawSolution, ctx, (PetscErrorCode(*)(void **))TSMonitorDrawCtxDestroy)); 285bdad233fSMatthew Knepley } 286fb1732b5SBarry Smith opt = PETSC_FALSE; 2879566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution_phase", "Monitor solution graphically", "TSMonitorDrawSolutionPhase", &opt)); 2882d5ee99bSBarry Smith if (opt) { 2892d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2902d5ee99bSBarry Smith PetscReal bounds[4]; 2912d5ee99bSBarry Smith PetscInt n = 4; 2922d5ee99bSBarry Smith PetscDraw draw; 2936934998bSLisandro Dalcin PetscDrawAxis axis; 2942d5ee99bSBarry Smith 2959566063dSJacob Faibussowitsch PetscCall(PetscOptionsRealArray("-ts_monitor_draw_solution_phase", "Monitor solution graphically", "TSMonitorDrawSolutionPhase", bounds, &n, NULL)); 2963c633725SBarry Smith PetscCheck(n == 4, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Must provide bounding box of phase field"); 2979566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, 1, &ctx)); 2989566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(ctx->viewer, 0, &draw)); 2999566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDrawAxis(ctx->viewer, 0, &axis)); 3009566063dSJacob Faibussowitsch PetscCall(PetscDrawAxisSetLimits(axis, bounds[0], bounds[2], bounds[1], bounds[3])); 3019566063dSJacob Faibussowitsch PetscCall(PetscDrawAxisSetLabels(axis, "Phase Diagram", "Variable 1", "Variable 2")); 3029566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorDrawSolutionPhase, ctx, (PetscErrorCode(*)(void **))TSMonitorDrawCtxDestroy)); 3032d5ee99bSBarry Smith } 3042d5ee99bSBarry Smith opt = PETSC_FALSE; 3059566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_error", "Monitor error graphically", "TSMonitorDrawError", &opt)); 3063a471f94SBarry Smith if (opt) { 30783a4ac43SBarry Smith TSMonitorDrawCtx ctx; 30883a4ac43SBarry Smith PetscInt howoften = 1; 3093a471f94SBarry Smith 3109566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_error", "Monitor error graphically", "TSMonitorDrawError", howoften, &howoften, NULL)); 3119566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, "Error", PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx)); 3129566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorDrawError, ctx, (PetscErrorCode(*)(void **))TSMonitorDrawCtxDestroy)); 3133a471f94SBarry Smith } 3140ed3bfb6SBarry Smith opt = PETSC_FALSE; 3159566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution_function", "Monitor solution provided by TSMonitorSetSolutionFunction() graphically", "TSMonitorDrawSolutionFunction", &opt)); 3160ed3bfb6SBarry Smith if (opt) { 3170ed3bfb6SBarry Smith TSMonitorDrawCtx ctx; 3180ed3bfb6SBarry Smith PetscInt howoften = 1; 3190ed3bfb6SBarry Smith 3209566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_solution_function", "Monitor solution provided by TSMonitorSetSolutionFunction() graphically", "TSMonitorDrawSolutionFunction", howoften, &howoften, NULL)); 3219566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, "Solution provided by user function", PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx)); 3229566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorDrawSolutionFunction, ctx, (PetscErrorCode(*)(void **))TSMonitorDrawCtxDestroy)); 3230ed3bfb6SBarry Smith } 324fde5950dSBarry Smith 325ed81e22dSJed Brown opt = PETSC_FALSE; 32663a3b9bcSJacob 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)); 327ed81e22dSJed Brown if (flg) { 328bbcf679cSJacob Faibussowitsch const char *ptr = NULL, *ptr2 = NULL; 329ed81e22dSJed Brown char *filetemplate; 33063a3b9bcSJacob Faibussowitsch PetscCheck(monfilename[0], PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03" PetscInt_FMT ".vts"); 331ed81e22dSJed Brown /* Do some cursory validation of the input. */ 3329566063dSJacob Faibussowitsch PetscCall(PetscStrstr(monfilename, "%", (char **)&ptr)); 33363a3b9bcSJacob Faibussowitsch PetscCheck(ptr, PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03" PetscInt_FMT ".vts"); 334ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 3359566063dSJacob Faibussowitsch PetscCall(PetscStrchr("DdiouxX", *ptr, (char **)&ptr2)); 33663a3b9bcSJacob Faibussowitsch PetscCheck(ptr2 || (*ptr >= '0' && *ptr <= '9'), PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03" PetscInt_FMT ".vts"); 337ed81e22dSJed Brown if (ptr2) break; 338ed81e22dSJed Brown } 3399566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(monfilename, &filetemplate)); 3409566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorSolutionVTK, filetemplate, (PetscErrorCode(*)(void **))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 3579566063dSJacob Faibussowitsch PetscCall(PetscInfo(((PetscObject)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 3809566063dSJacob Faibussowitsch PetscCall(PetscInfo(((PetscObject)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)); 3939566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorEnvelope, ctx, (PetscErrorCode(*)(void **))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 5536b867d5aSJose E. Roman - B - optional preconditioning matrix 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 559a7a1495cSBarry Smith is used internally within the nonlinear solvers. 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 774bcf0153eSBarry Smith - imex - flag indicates if the method is `TSIMEX` 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 869bcf0153eSBarry Smith - imex - flag indicates if the method is `TSIMEX` 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 1307*bc7fdbb5SPierre 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 1572efe9872eSLisandro Dalcin - P - optional preconditioning matrix 1573efe9872eSLisandro Dalcin 1574bcf0153eSBarry Smith Level: developer 1575bcf0153eSBarry Smith 1576efe9872eSLisandro Dalcin Notes: 1577efe9872eSLisandro Dalcin If F(t,U,V,A)=0 is the DAE, the required Jacobian is 1578efe9872eSLisandro Dalcin 1579efe9872eSLisandro Dalcin dF/dU + shiftV*dF/dV + shiftA*dF/dA 1580efe9872eSLisandro Dalcin 1581efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1582efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1583efe9872eSLisandro Dalcin 15841cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetI2Jacobian()` 1585efe9872eSLisandro Dalcin @*/ 1586d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeI2Jacobian(TS ts, PetscReal t, Vec U, Vec V, Vec A, PetscReal shiftV, PetscReal shiftA, Mat J, Mat P) 1587d71ae5a4SJacob Faibussowitsch { 1588efe9872eSLisandro Dalcin DM dm; 15898434afd1SBarry Smith TSI2JacobianFn *I2Jacobian; 1590efe9872eSLisandro Dalcin void *ctx; 15918434afd1SBarry Smith TSRHSJacobianFn *rhsjacobian; 1592efe9872eSLisandro Dalcin 1593efe9872eSLisandro Dalcin PetscFunctionBegin; 1594efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1595efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 1596efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V, VEC_CLASSID, 4); 1597efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A, VEC_CLASSID, 5); 1598efe9872eSLisandro Dalcin PetscValidHeaderSpecific(J, MAT_CLASSID, 8); 1599efe9872eSLisandro Dalcin PetscValidHeaderSpecific(P, MAT_CLASSID, 9); 1600efe9872eSLisandro Dalcin 16019566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 16029566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm, &I2Jacobian, &ctx)); 16039566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, &rhsjacobian, NULL)); 1604efe9872eSLisandro Dalcin 1605efe9872eSLisandro Dalcin if (!I2Jacobian) { 16069566063dSJacob Faibussowitsch PetscCall(TSComputeIJacobian(ts, t, U, A, shiftA, J, P, PETSC_FALSE)); 16073ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1608efe9872eSLisandro Dalcin } 1609efe9872eSLisandro Dalcin 1610da023ba9SStefano Zampini PetscCall(PetscLogEventBegin(TS_JacobianEval, U, ts, J, P)); 1611792fecdfSBarry Smith PetscCallBack("TS callback implicit Jacobian", I2Jacobian(ts, t, U, V, A, shiftV, shiftA, J, P, ctx)); 1612efe9872eSLisandro Dalcin if (rhsjacobian) { 1613d60b7d5cSBarry Smith Mat Jrhs, Prhs; 16149566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts, &Jrhs, &Prhs)); 16159566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts, t, U, Jrhs, Prhs)); 16169566063dSJacob Faibussowitsch PetscCall(MatAXPY(J, -1, Jrhs, ts->axpy_pattern)); 16179566063dSJacob Faibussowitsch if (P != J) PetscCall(MatAXPY(P, -1, Prhs, ts->axpy_pattern)); 1618efe9872eSLisandro Dalcin } 1619efe9872eSLisandro Dalcin 1620da023ba9SStefano Zampini PetscCall(PetscLogEventEnd(TS_JacobianEval, U, ts, J, P)); 16213ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1622efe9872eSLisandro Dalcin } 1623efe9872eSLisandro Dalcin 1624438f35afSJed Brown /*@C 1625438f35afSJed Brown TSSetTransientVariable - sets function to transform from state to transient variables 1626438f35afSJed Brown 1627438f35afSJed Brown Logically Collective 1628438f35afSJed Brown 16294165533cSJose E. Roman Input Parameters: 1630438f35afSJed Brown + ts - time stepping context on which to change the transient variable 16318434afd1SBarry Smith . tvar - a function that transforms to transient variables, see `TSTransientVariableFn` for the calling sequence 1632438f35afSJed Brown - ctx - a context for tvar 1633438f35afSJed Brown 1634438f35afSJed Brown Level: advanced 1635438f35afSJed Brown 1636438f35afSJed Brown Notes: 1637bcf0153eSBarry Smith This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., `TSBDF`) 1638438f35afSJed Brown can be conservative. In this context, primitive variables P are used to model the state (e.g., because they lead to 1639438f35afSJed Brown well-conditioned formulations even in limiting cases such as low-Mach or zero porosity). The transient variable is 1640438f35afSJed Brown C(P), specified by calling this function. An IFunction thus receives arguments (P, Cdot) and the IJacobian must be 1641438f35afSJed Brown evaluated via the chain rule, as in 1642195e9b02SBarry Smith .vb 1643438f35afSJed Brown dF/dP + shift * dF/dCdot dC/dP. 1644195e9b02SBarry Smith .ve 1645438f35afSJed Brown 16468434afd1SBarry Smith .seealso: [](ch_ts), `TS`, `TSBDF`, `TSTransientVariableFn`, `DMTSSetTransientVariable()`, `DMTSGetTransientVariable()`, `TSSetIFunction()`, `TSSetIJacobian()` 1647438f35afSJed Brown @*/ 16488434afd1SBarry Smith PetscErrorCode TSSetTransientVariable(TS ts, TSTransientVariableFn *tvar, void *ctx) 1649d71ae5a4SJacob Faibussowitsch { 1650438f35afSJed Brown DM dm; 1651438f35afSJed Brown 1652438f35afSJed Brown PetscFunctionBegin; 1653438f35afSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 16549566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 16559566063dSJacob Faibussowitsch PetscCall(DMTSSetTransientVariable(dm, tvar, ctx)); 16563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1657438f35afSJed Brown } 1658438f35afSJed Brown 1659efe9872eSLisandro Dalcin /*@ 1660e3c11fc1SJed Brown TSComputeTransientVariable - transforms state (primitive) variables to transient (conservative) variables 1661e3c11fc1SJed Brown 1662e3c11fc1SJed Brown Logically Collective 1663e3c11fc1SJed Brown 1664e3c11fc1SJed Brown Input Parameters: 1665e3c11fc1SJed Brown + ts - TS on which to compute 1666e3c11fc1SJed Brown - U - state vector to be transformed to transient variables 1667e3c11fc1SJed Brown 16682fe279fdSBarry Smith Output Parameter: 1669e3c11fc1SJed Brown . C - transient (conservative) variable 1670e3c11fc1SJed Brown 1671e3c11fc1SJed Brown Level: developer 1672e3c11fc1SJed Brown 1673b43aa488SJacob Faibussowitsch Developer Notes: 1674195e9b02SBarry Smith If `DMTSSetTransientVariable()` has not been called, then C is not modified in this routine and C = `NULL` is allowed. 1675bcf0153eSBarry Smith This makes it safe to call without a guard. One can use `TSHasTransientVariable()` to check if transient variables are 1676bcf0153eSBarry Smith being used. 1677bcf0153eSBarry Smith 16781cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSBDF`, `DMTSSetTransientVariable()`, `TSComputeIFunction()`, `TSComputeIJacobian()` 1679e3c11fc1SJed Brown @*/ 1680d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeTransientVariable(TS ts, Vec U, Vec C) 1681d71ae5a4SJacob Faibussowitsch { 1682e3c11fc1SJed Brown DM dm; 1683e3c11fc1SJed Brown DMTS dmts; 1684e3c11fc1SJed Brown 1685e3c11fc1SJed Brown PetscFunctionBegin; 1686e3c11fc1SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1687e3c11fc1SJed Brown PetscValidHeaderSpecific(U, VEC_CLASSID, 2); 16889566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 16899566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm, &dmts)); 1690e3c11fc1SJed Brown if (dmts->ops->transientvar) { 1691e3c11fc1SJed Brown PetscValidHeaderSpecific(C, VEC_CLASSID, 3); 16929566063dSJacob Faibussowitsch PetscCall((*dmts->ops->transientvar)(ts, U, C, dmts->transientvarctx)); 1693e3c11fc1SJed Brown } 16943ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1695e3c11fc1SJed Brown } 1696e3c11fc1SJed Brown 1697e3c11fc1SJed Brown /*@ 1698e3c11fc1SJed Brown TSHasTransientVariable - determine whether transient variables have been set 1699e3c11fc1SJed Brown 1700e3c11fc1SJed Brown Logically Collective 1701e3c11fc1SJed Brown 17022fe279fdSBarry Smith Input Parameter: 1703195e9b02SBarry Smith . ts - `TS` on which to compute 1704e3c11fc1SJed Brown 17052fe279fdSBarry Smith Output Parameter: 1706bcf0153eSBarry Smith . has - `PETSC_TRUE` if transient variables have been set 1707e3c11fc1SJed Brown 1708e3c11fc1SJed Brown Level: developer 1709e3c11fc1SJed Brown 17101cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSBDF`, `DMTSSetTransientVariable()`, `TSComputeTransientVariable()` 1711e3c11fc1SJed Brown @*/ 1712d71ae5a4SJacob Faibussowitsch PetscErrorCode TSHasTransientVariable(TS ts, PetscBool *has) 1713d71ae5a4SJacob Faibussowitsch { 1714e3c11fc1SJed Brown DM dm; 1715e3c11fc1SJed Brown DMTS dmts; 1716e3c11fc1SJed Brown 1717e3c11fc1SJed Brown PetscFunctionBegin; 1718e3c11fc1SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 17199566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 17209566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm, &dmts)); 1721e3c11fc1SJed Brown *has = dmts->ops->transientvar ? PETSC_TRUE : PETSC_FALSE; 17223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1723e3c11fc1SJed Brown } 1724e3c11fc1SJed Brown 1725e3c11fc1SJed Brown /*@ 1726efe9872eSLisandro Dalcin TS2SetSolution - Sets the initial solution and time derivative vectors 1727bcf0153eSBarry Smith for use by the `TS` routines handling second order equations. 1728efe9872eSLisandro Dalcin 1729c3339decSBarry Smith Logically Collective 1730efe9872eSLisandro Dalcin 1731efe9872eSLisandro Dalcin Input Parameters: 1732bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1733efe9872eSLisandro Dalcin . u - the solution vector 1734efe9872eSLisandro Dalcin - v - the time derivative vector 1735efe9872eSLisandro Dalcin 1736efe9872eSLisandro Dalcin Level: beginner 1737efe9872eSLisandro Dalcin 17381cc06b55SBarry Smith .seealso: [](ch_ts), `TS` 1739efe9872eSLisandro Dalcin @*/ 1740d71ae5a4SJacob Faibussowitsch PetscErrorCode TS2SetSolution(TS ts, Vec u, Vec v) 1741d71ae5a4SJacob Faibussowitsch { 1742efe9872eSLisandro Dalcin PetscFunctionBegin; 1743efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1744efe9872eSLisandro Dalcin PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 1745efe9872eSLisandro Dalcin PetscValidHeaderSpecific(v, VEC_CLASSID, 3); 17469566063dSJacob Faibussowitsch PetscCall(TSSetSolution(ts, u)); 17479566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)v)); 17489566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_dot)); 1749efe9872eSLisandro Dalcin ts->vec_dot = v; 17503ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1751efe9872eSLisandro Dalcin } 1752efe9872eSLisandro Dalcin 1753efe9872eSLisandro Dalcin /*@ 1754efe9872eSLisandro Dalcin TS2GetSolution - Returns the solution and time derivative at the present timestep 175514d0ab18SJacob Faibussowitsch for second order equations. 1756efe9872eSLisandro Dalcin 175714d0ab18SJacob Faibussowitsch Not Collective 1758efe9872eSLisandro Dalcin 1759efe9872eSLisandro Dalcin Input Parameter: 1760bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 1761efe9872eSLisandro Dalcin 1762d8d19677SJose E. Roman Output Parameters: 1763efe9872eSLisandro Dalcin + u - the vector containing the solution 1764efe9872eSLisandro Dalcin - v - the vector containing the time derivative 1765efe9872eSLisandro Dalcin 1766efe9872eSLisandro Dalcin Level: intermediate 1767efe9872eSLisandro Dalcin 176814d0ab18SJacob Faibussowitsch Notes: 176914d0ab18SJacob Faibussowitsch It is valid to call this routine inside the function 177014d0ab18SJacob Faibussowitsch that you are evaluating in order to move to the new timestep. This vector not 177114d0ab18SJacob Faibussowitsch changed until the solution at the next timestep has been calculated. 177214d0ab18SJacob Faibussowitsch 17731cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TS2SetSolution()`, `TSGetTimeStep()`, `TSGetTime()` 1774efe9872eSLisandro Dalcin @*/ 1775d71ae5a4SJacob Faibussowitsch PetscErrorCode TS2GetSolution(TS ts, Vec *u, Vec *v) 1776d71ae5a4SJacob Faibussowitsch { 1777efe9872eSLisandro Dalcin PetscFunctionBegin; 1778efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 17794f572ea9SToby Isaac if (u) PetscAssertPointer(u, 2); 17804f572ea9SToby Isaac if (v) PetscAssertPointer(v, 3); 1781efe9872eSLisandro Dalcin if (u) *u = ts->vec_sol; 1782efe9872eSLisandro Dalcin if (v) *v = ts->vec_dot; 17833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1784efe9872eSLisandro Dalcin } 1785efe9872eSLisandro Dalcin 1786ffeef943SBarry Smith /*@ 1787bcf0153eSBarry Smith TSLoad - Loads a `TS` that has been stored in binary with `TSView()`. 178855849f57SBarry Smith 1789c3339decSBarry Smith Collective 179055849f57SBarry Smith 179155849f57SBarry Smith Input Parameters: 1792b43aa488SJacob Faibussowitsch + ts - the newly loaded `TS`, this needs to have been created with `TSCreate()` or 1793bcf0153eSBarry Smith some related function before a call to `TSLoad()`. 1794bcf0153eSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` 179555849f57SBarry Smith 179655849f57SBarry Smith Level: intermediate 179755849f57SBarry Smith 1798bcf0153eSBarry Smith Note: 1799bcf0153eSBarry Smith The type is determined by the data in the file, any type set into the `TS` before this call is ignored. 180055849f57SBarry Smith 18011cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `PetscViewer`, `PetscViewerBinaryOpen()`, `TSView()`, `MatLoad()`, `VecLoad()` 180255849f57SBarry Smith @*/ 1803d71ae5a4SJacob Faibussowitsch PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 1804d71ae5a4SJacob Faibussowitsch { 180555849f57SBarry Smith PetscBool isbinary; 180655849f57SBarry Smith PetscInt classid; 180755849f57SBarry Smith char type[256]; 18082d53ad75SBarry Smith DMTS sdm; 1809ad6bc421SBarry Smith DM dm; 181055849f57SBarry Smith 181155849f57SBarry Smith PetscFunctionBegin; 1812f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 181355849f57SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 18149566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary)); 18153c633725SBarry Smith PetscCheck(isbinary, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 181655849f57SBarry Smith 18179566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT)); 18183c633725SBarry Smith PetscCheck(classid == TS_FILE_CLASSID, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Not TS next in file"); 18199566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR)); 18209566063dSJacob Faibussowitsch PetscCall(TSSetType(ts, type)); 1821dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, load, viewer); 18229566063dSJacob Faibussowitsch PetscCall(DMCreate(PetscObjectComm((PetscObject)ts), &dm)); 18239566063dSJacob Faibussowitsch PetscCall(DMLoad(dm, viewer)); 18249566063dSJacob Faibussowitsch PetscCall(TSSetDM(ts, dm)); 18259566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(ts->dm, &ts->vec_sol)); 18269566063dSJacob Faibussowitsch PetscCall(VecLoad(ts->vec_sol, viewer)); 18279566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm, &sdm)); 18289566063dSJacob Faibussowitsch PetscCall(DMTSLoad(sdm, viewer)); 18293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 183055849f57SBarry Smith } 183155849f57SBarry Smith 18329804daf3SBarry Smith #include <petscdraw.h> 1833e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1834e04113cfSBarry Smith #include <petscviewersaws.h> 1835f05ece33SBarry Smith #endif 1836fe2efc57SMark 1837ffeef943SBarry Smith /*@ 1838bcf0153eSBarry Smith TSViewFromOptions - View a `TS` based on values in the options database 1839fe2efc57SMark 1840c3339decSBarry Smith Collective 1841fe2efc57SMark 1842fe2efc57SMark Input Parameters: 1843bcf0153eSBarry Smith + ts - the `TS` context 1844bcf0153eSBarry Smith . obj - Optional object that provides the prefix for the options database keys 1845bcf0153eSBarry Smith - name - command line option string to be passed by user 1846fe2efc57SMark 1847fe2efc57SMark Level: intermediate 1848bcf0153eSBarry Smith 18491cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSView`, `PetscObjectViewFromOptions()`, `TSCreate()` 1850fe2efc57SMark @*/ 1851bcf0153eSBarry Smith PetscErrorCode TSViewFromOptions(TS ts, PetscObject obj, const char name[]) 1852d71ae5a4SJacob Faibussowitsch { 1853fe2efc57SMark PetscFunctionBegin; 1854bcf0153eSBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1855bcf0153eSBarry Smith PetscCall(PetscObjectViewFromOptions((PetscObject)ts, obj, name)); 18563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1857fe2efc57SMark } 1858fe2efc57SMark 1859ffeef943SBarry Smith /*@ 1860bcf0153eSBarry Smith TSView - Prints the `TS` data structure. 1861d763cef2SBarry Smith 1862c3339decSBarry Smith Collective 1863d763cef2SBarry Smith 1864d763cef2SBarry Smith Input Parameters: 1865bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1866d763cef2SBarry Smith - viewer - visualization context 1867d763cef2SBarry Smith 1868d763cef2SBarry Smith Options Database Key: 1869bcf0153eSBarry Smith . -ts_view - calls `TSView()` at end of `TSStep()` 1870bcf0153eSBarry Smith 1871bcf0153eSBarry Smith Level: beginner 1872d763cef2SBarry Smith 1873d763cef2SBarry Smith Notes: 1874d763cef2SBarry Smith The available visualization contexts include 1875bcf0153eSBarry Smith + `PETSC_VIEWER_STDOUT_SELF` - standard output (default) 1876bcf0153eSBarry Smith - `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard 1877d763cef2SBarry Smith output where only the first processor opens 1878d763cef2SBarry Smith the file. All other processors send their 1879d763cef2SBarry Smith data to the first processor to print. 1880d763cef2SBarry Smith 1881d763cef2SBarry Smith The user can open an alternative visualization context with 1882bcf0153eSBarry Smith `PetscViewerASCIIOpen()` - output to a specified file. 1883d763cef2SBarry Smith 1884bcf0153eSBarry Smith In the debugger you can do call `TSView`(ts,0) to display the `TS` solver. (The same holds for any PETSc object viewer). 1885595c91d4SBarry Smith 18861cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `PetscViewer`, `PetscViewerASCIIOpen()` 1887d763cef2SBarry Smith @*/ 1888d71ae5a4SJacob Faibussowitsch PetscErrorCode TSView(TS ts, PetscViewer viewer) 1889d71ae5a4SJacob Faibussowitsch { 189019fd82e9SBarry Smith TSType type; 18912b0a91c0SBarry Smith PetscBool iascii, isstring, isundials, isbinary, isdraw; 18922d53ad75SBarry Smith DMTS sdm; 1893e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1894536b137fSBarry Smith PetscBool issaws; 1895f05ece33SBarry Smith #endif 1896d763cef2SBarry Smith 1897d763cef2SBarry Smith PetscFunctionBegin; 18980700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 18991e66621cSBarry Smith if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts), &viewer)); 19000700a824SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 1901c9780b6fSBarry Smith PetscCheckSameComm(ts, 1, viewer, 2); 1902fd16b177SBarry Smith 19039566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 19049566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring)); 19059566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary)); 19069566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw)); 1907e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 19089566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSAWS, &issaws)); 1909f05ece33SBarry Smith #endif 191032077d6dSBarry Smith if (iascii) { 19119566063dSJacob Faibussowitsch PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)ts, viewer)); 1912efd4aadfSBarry Smith if (ts->ops->view) { 19139566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 1914dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, view, viewer); 19159566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 1916efd4aadfSBarry Smith } 19171e66621cSBarry Smith if (ts->max_steps < PETSC_MAX_INT) PetscCall(PetscViewerASCIIPrintf(viewer, " maximum steps=%" PetscInt_FMT "\n", ts->max_steps)); 19181e66621cSBarry Smith if (ts->max_time < PETSC_MAX_REAL) PetscCall(PetscViewerASCIIPrintf(viewer, " maximum time=%g\n", (double)ts->max_time)); 19191e66621cSBarry Smith if (ts->ifuncs) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of I function evaluations=%" PetscInt_FMT "\n", ts->ifuncs)); 19201e66621cSBarry Smith if (ts->ijacs) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of I Jacobian evaluations=%" PetscInt_FMT "\n", ts->ijacs)); 19211e66621cSBarry Smith if (ts->rhsfuncs) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of RHS function evaluations=%" PetscInt_FMT "\n", ts->rhsfuncs)); 19221e66621cSBarry Smith if (ts->rhsjacs) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of RHS Jacobian evaluations=%" PetscInt_FMT "\n", ts->rhsjacs)); 1923efd4aadfSBarry Smith if (ts->usessnes) { 1924efd4aadfSBarry Smith PetscBool lin; 19251e66621cSBarry Smith if (ts->problem_type == TS_NONLINEAR) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of nonlinear solver iterations=%" PetscInt_FMT "\n", ts->snes_its)); 192663a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " total number of linear solver iterations=%" PetscInt_FMT "\n", ts->ksp_its)); 19279566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompareAny((PetscObject)ts->snes, &lin, SNESKSPONLY, SNESKSPTRANSPOSEONLY, "")); 192863a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " total number of %slinear solve failures=%" PetscInt_FMT "\n", lin ? "" : "non", ts->num_snes_failures)); 1929efd4aadfSBarry Smith } 193063a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " total number of rejected steps=%" PetscInt_FMT "\n", ts->reject)); 19311e66621cSBarry Smith if (ts->vrtol) PetscCall(PetscViewerASCIIPrintf(viewer, " using vector of relative error tolerances, ")); 19321e66621cSBarry Smith else PetscCall(PetscViewerASCIIPrintf(viewer, " using relative error tolerance of %g, ", (double)ts->rtol)); 19331e66621cSBarry Smith if (ts->vatol) PetscCall(PetscViewerASCIIPrintf(viewer, " using vector of absolute error tolerances\n")); 19341e66621cSBarry Smith else PetscCall(PetscViewerASCIIPrintf(viewer, " using absolute error tolerance of %g\n", (double)ts->atol)); 19359566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 19369566063dSJacob Faibussowitsch PetscCall(TSAdaptView(ts->adapt, viewer)); 19379566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 19380f5bd95cSBarry Smith } else if (isstring) { 19399566063dSJacob Faibussowitsch PetscCall(TSGetType(ts, &type)); 19409566063dSJacob Faibussowitsch PetscCall(PetscViewerStringSPrintf(viewer, " TSType: %-7.7s", type)); 1941dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, view, viewer); 194255849f57SBarry Smith } else if (isbinary) { 194355849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 194455849f57SBarry Smith MPI_Comm comm; 194555849f57SBarry Smith PetscMPIInt rank; 194655849f57SBarry Smith char type[256]; 194755849f57SBarry Smith 19489566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)ts, &comm)); 19499566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 1950dd400576SPatrick Sanan if (rank == 0) { 19519566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer, &classid, 1, PETSC_INT)); 19529566063dSJacob Faibussowitsch PetscCall(PetscStrncpy(type, ((PetscObject)ts)->type_name, 256)); 19539566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer, type, 256, PETSC_CHAR)); 195455849f57SBarry Smith } 1955dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, view, viewer); 19569566063dSJacob Faibussowitsch if (ts->adapt) PetscCall(TSAdaptView(ts->adapt, viewer)); 19579566063dSJacob Faibussowitsch PetscCall(DMView(ts->dm, viewer)); 19589566063dSJacob Faibussowitsch PetscCall(VecView(ts->vec_sol, viewer)); 19599566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm, &sdm)); 19609566063dSJacob Faibussowitsch PetscCall(DMTSView(sdm, viewer)); 19612b0a91c0SBarry Smith } else if (isdraw) { 19622b0a91c0SBarry Smith PetscDraw draw; 19632b0a91c0SBarry Smith char str[36]; 196489fd9fafSBarry Smith PetscReal x, y, bottom, h; 19652b0a91c0SBarry Smith 19669566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw)); 19679566063dSJacob Faibussowitsch PetscCall(PetscDrawGetCurrentPoint(draw, &x, &y)); 1968c6a7a370SJeremy L Thompson PetscCall(PetscStrncpy(str, "TS: ", sizeof(str))); 1969c6a7a370SJeremy L Thompson PetscCall(PetscStrlcat(str, ((PetscObject)ts)->type_name, sizeof(str))); 19709566063dSJacob Faibussowitsch PetscCall(PetscDrawStringBoxed(draw, x, y, PETSC_DRAW_BLACK, PETSC_DRAW_BLACK, str, NULL, &h)); 197189fd9fafSBarry Smith bottom = y - h; 19729566063dSJacob Faibussowitsch PetscCall(PetscDrawPushCurrentPoint(draw, x, bottom)); 1973dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, view, viewer); 19749566063dSJacob Faibussowitsch if (ts->adapt) PetscCall(TSAdaptView(ts->adapt, viewer)); 19759566063dSJacob Faibussowitsch if (ts->snes) PetscCall(SNESView(ts->snes, viewer)); 19769566063dSJacob Faibussowitsch PetscCall(PetscDrawPopCurrentPoint(draw)); 1977e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1978536b137fSBarry Smith } else if (issaws) { 1979d45a07a7SBarry Smith PetscMPIInt rank; 19802657e9d9SBarry Smith const char *name; 19812657e9d9SBarry Smith 19829566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)ts, &name)); 19839566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank)); 1984dd400576SPatrick Sanan if (!((PetscObject)ts)->amsmem && rank == 0) { 1985d45a07a7SBarry Smith char dir[1024]; 1986d45a07a7SBarry Smith 19879566063dSJacob Faibussowitsch PetscCall(PetscObjectViewSAWs((PetscObject)ts, viewer)); 19889566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(dir, 1024, "/PETSc/Objects/%s/time_step", name)); 1989792fecdfSBarry Smith PetscCallSAWs(SAWs_Register, (dir, &ts->steps, 1, SAWs_READ, SAWs_INT)); 19909566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(dir, 1024, "/PETSc/Objects/%s/time", name)); 1991792fecdfSBarry Smith PetscCallSAWs(SAWs_Register, (dir, &ts->ptime, 1, SAWs_READ, SAWs_DOUBLE)); 1992d763cef2SBarry Smith } 1993dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, view, viewer); 1994f05ece33SBarry Smith #endif 1995f05ece33SBarry Smith } 199636a9e3b9SBarry Smith if (ts->snes && ts->usessnes) { 19979566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 19989566063dSJacob Faibussowitsch PetscCall(SNESView(ts->snes, viewer)); 19999566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 200036a9e3b9SBarry Smith } 20019566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm, &sdm)); 20029566063dSJacob Faibussowitsch PetscCall(DMTSView(sdm, viewer)); 2003f05ece33SBarry Smith 20049566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 20059566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)ts, TSSUNDIALS, &isundials)); 20069566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 20073ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2008d763cef2SBarry Smith } 2009d763cef2SBarry Smith 2010b07ff414SBarry Smith /*@ 2011d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 2012d763cef2SBarry Smith the timesteppers. 2013d763cef2SBarry Smith 2014c3339decSBarry Smith Logically Collective 2015d763cef2SBarry Smith 2016d763cef2SBarry Smith Input Parameters: 2017bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 2018bcf0153eSBarry Smith - usrP - user context 2019daf670e6SBarry Smith 2020d763cef2SBarry Smith Level: intermediate 2021d763cef2SBarry Smith 2022b43aa488SJacob Faibussowitsch Fortran Notes: 2023195e9b02SBarry Smith You must write a Fortran interface definition for this 2024195e9b02SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the `ctx` argument. 2025bcf0153eSBarry Smith 20261cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetApplicationContext()` 2027d763cef2SBarry Smith @*/ 2028d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetApplicationContext(TS ts, void *usrP) 2029d71ae5a4SJacob Faibussowitsch { 2030d763cef2SBarry Smith PetscFunctionBegin; 20310700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2032d763cef2SBarry Smith ts->user = usrP; 20333ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2034d763cef2SBarry Smith } 2035d763cef2SBarry Smith 2036b07ff414SBarry Smith /*@ 2037d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 2038bcf0153eSBarry Smith timestepper that was set with `TSSetApplicationContext()` 2039d763cef2SBarry Smith 2040d763cef2SBarry Smith Not Collective 2041d763cef2SBarry Smith 2042d763cef2SBarry Smith Input Parameter: 2043bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2044d763cef2SBarry Smith 2045d763cef2SBarry Smith Output Parameter: 2046d763cef2SBarry Smith . usrP - user context 2047d763cef2SBarry Smith 2048bcf0153eSBarry Smith Level: intermediate 2049bcf0153eSBarry Smith 2050b43aa488SJacob Faibussowitsch Fortran Notes: 2051195e9b02SBarry Smith You must write a Fortran interface definition for this 2052195e9b02SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the `ctx` argument. 2053daf670e6SBarry Smith 20541cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetApplicationContext()` 2055d763cef2SBarry Smith @*/ 2056d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetApplicationContext(TS ts, void *usrP) 2057d71ae5a4SJacob Faibussowitsch { 2058d763cef2SBarry Smith PetscFunctionBegin; 20590700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2060e71120c6SJed Brown *(void **)usrP = ts->user; 20613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2062d763cef2SBarry Smith } 2063d763cef2SBarry Smith 2064d763cef2SBarry Smith /*@ 2065bcf0153eSBarry Smith TSGetStepNumber - Gets the number of time steps completed. 2066d763cef2SBarry Smith 2067d763cef2SBarry Smith Not Collective 2068d763cef2SBarry Smith 2069d763cef2SBarry Smith Input Parameter: 2070bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2071d763cef2SBarry Smith 2072d763cef2SBarry Smith Output Parameter: 207380275a0aSLisandro Dalcin . steps - number of steps completed so far 2074d763cef2SBarry Smith 2075d763cef2SBarry Smith Level: intermediate 2076d763cef2SBarry Smith 20771cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTime()`, `TSGetTimeStep()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostStage()`, `TSSetPostStep()` 2078d763cef2SBarry Smith @*/ 2079d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetStepNumber(TS ts, PetscInt *steps) 2080d71ae5a4SJacob Faibussowitsch { 2081d763cef2SBarry Smith PetscFunctionBegin; 20820700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 20834f572ea9SToby Isaac PetscAssertPointer(steps, 2); 208480275a0aSLisandro Dalcin *steps = ts->steps; 20853ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 208680275a0aSLisandro Dalcin } 208780275a0aSLisandro Dalcin 208880275a0aSLisandro Dalcin /*@ 208980275a0aSLisandro Dalcin TSSetStepNumber - Sets the number of steps completed. 209080275a0aSLisandro Dalcin 2091c3339decSBarry Smith Logically Collective 209280275a0aSLisandro Dalcin 209380275a0aSLisandro Dalcin Input Parameters: 2094bcf0153eSBarry Smith + ts - the `TS` context 209580275a0aSLisandro Dalcin - steps - number of steps completed so far 209680275a0aSLisandro Dalcin 2097bcf0153eSBarry Smith Level: developer 2098bcf0153eSBarry Smith 2099bcf0153eSBarry Smith Note: 2100bcf0153eSBarry Smith For most uses of the `TS` solvers the user need not explicitly call 2101bcf0153eSBarry Smith `TSSetStepNumber()`, as the step counter is appropriately updated in 2102bcf0153eSBarry Smith `TSSolve()`/`TSStep()`/`TSRollBack()`. Power users may call this routine to 210380275a0aSLisandro Dalcin reinitialize timestepping by setting the step counter to zero (and time 210480275a0aSLisandro Dalcin to the initial time) to solve a similar problem with different initial 210580275a0aSLisandro Dalcin conditions or parameters. Other possible use case is to continue 2106195e9b02SBarry Smith timestepping from a previously interrupted run in such a way that `TS` 210780275a0aSLisandro Dalcin monitors will be called with a initial nonzero step counter. 210880275a0aSLisandro Dalcin 21091cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetStepNumber()`, `TSSetTime()`, `TSSetTimeStep()`, `TSSetSolution()` 211080275a0aSLisandro Dalcin @*/ 2111d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetStepNumber(TS ts, PetscInt steps) 2112d71ae5a4SJacob Faibussowitsch { 211380275a0aSLisandro Dalcin PetscFunctionBegin; 211480275a0aSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 211580275a0aSLisandro Dalcin PetscValidLogicalCollectiveInt(ts, steps, 2); 21163c633725SBarry Smith PetscCheck(steps >= 0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Step number must be non-negative"); 211780275a0aSLisandro Dalcin ts->steps = steps; 21183ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2119d763cef2SBarry Smith } 2120d763cef2SBarry Smith 2121d763cef2SBarry Smith /*@ 2122d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 2123d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 2124d763cef2SBarry Smith 2125c3339decSBarry Smith Logically Collective 2126d763cef2SBarry Smith 2127d763cef2SBarry Smith Input Parameters: 2128bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 2129d763cef2SBarry Smith - time_step - the size of the timestep 2130d763cef2SBarry Smith 2131d763cef2SBarry Smith Level: intermediate 2132d763cef2SBarry Smith 21331cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSPSEUDO`, `TSGetTimeStep()`, `TSSetTime()` 2134d763cef2SBarry Smith @*/ 2135d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTimeStep(TS ts, PetscReal time_step) 2136d71ae5a4SJacob Faibussowitsch { 2137d763cef2SBarry Smith PetscFunctionBegin; 21380700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2139c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts, time_step, 2); 2140d763cef2SBarry Smith ts->time_step = time_step; 21413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2142d763cef2SBarry Smith } 2143d763cef2SBarry Smith 2144a43b19c4SJed Brown /*@ 214549354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 214649354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 2147bcf0153eSBarry Smith or just return at the final time `TS` computed. 2148a43b19c4SJed Brown 2149c3339decSBarry Smith Logically Collective 2150a43b19c4SJed Brown 2151d8d19677SJose E. Roman Input Parameters: 2152a43b19c4SJed Brown + ts - the time-step context 215349354f04SShri Abhyankar - eftopt - exact final time option 2154bcf0153eSBarry Smith .vb 2155bcf0153eSBarry Smith TS_EXACTFINALTIME_STEPOVER - Don't do anything if final time is exceeded 2156bcf0153eSBarry Smith TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time 2157bcf0153eSBarry Smith TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time 2158bcf0153eSBarry Smith .ve 2159a43b19c4SJed Brown 2160bcf0153eSBarry Smith Options Database Key: 2161feed9e9dSBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime 2162feed9e9dSBarry Smith 2163a43b19c4SJed Brown Level: beginner 2164a43b19c4SJed Brown 2165bcf0153eSBarry Smith Note: 2166bcf0153eSBarry Smith If you use the option `TS_EXACTFINALTIME_STEPOVER` the solution may be at a very different time 2167bcf0153eSBarry Smith then the final time you selected. 2168bcf0153eSBarry Smith 21691cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSExactFinalTimeOption`, `TSGetExactFinalTime()` 2170a43b19c4SJed Brown @*/ 2171d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetExactFinalTime(TS ts, TSExactFinalTimeOption eftopt) 2172d71ae5a4SJacob Faibussowitsch { 2173a43b19c4SJed Brown PetscFunctionBegin; 2174a43b19c4SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 217549354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts, eftopt, 2); 217649354f04SShri Abhyankar ts->exact_final_time = eftopt; 21773ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2178a43b19c4SJed Brown } 2179a43b19c4SJed Brown 2180d763cef2SBarry Smith /*@ 2181bcf0153eSBarry Smith TSGetExactFinalTime - Gets the exact final time option set with `TSSetExactFinalTime()` 2182f6953c82SLisandro Dalcin 2183f6953c82SLisandro Dalcin Not Collective 2184f6953c82SLisandro Dalcin 2185f6953c82SLisandro Dalcin Input Parameter: 2186bcf0153eSBarry Smith . ts - the `TS` context 2187f6953c82SLisandro Dalcin 2188f6953c82SLisandro Dalcin Output Parameter: 2189f6953c82SLisandro Dalcin . eftopt - exact final time option 2190f6953c82SLisandro Dalcin 2191f6953c82SLisandro Dalcin Level: beginner 2192f6953c82SLisandro Dalcin 21931cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSExactFinalTimeOption`, `TSSetExactFinalTime()` 2194f6953c82SLisandro Dalcin @*/ 2195d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetExactFinalTime(TS ts, TSExactFinalTimeOption *eftopt) 2196d71ae5a4SJacob Faibussowitsch { 2197f6953c82SLisandro Dalcin PetscFunctionBegin; 2198f6953c82SLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 21994f572ea9SToby Isaac PetscAssertPointer(eftopt, 2); 2200f6953c82SLisandro Dalcin *eftopt = ts->exact_final_time; 22013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2202f6953c82SLisandro Dalcin } 2203f6953c82SLisandro Dalcin 2204f6953c82SLisandro Dalcin /*@ 2205d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 2206d763cef2SBarry Smith 2207d763cef2SBarry Smith Not Collective 2208d763cef2SBarry Smith 2209d763cef2SBarry Smith Input Parameter: 2210bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2211d763cef2SBarry Smith 2212d763cef2SBarry Smith Output Parameter: 2213d763cef2SBarry Smith . dt - the current timestep size 2214d763cef2SBarry Smith 2215d763cef2SBarry Smith Level: intermediate 2216d763cef2SBarry Smith 22171cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetTimeStep()`, `TSGetTime()` 2218d763cef2SBarry Smith @*/ 2219d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeStep(TS ts, PetscReal *dt) 2220d71ae5a4SJacob Faibussowitsch { 2221d763cef2SBarry Smith PetscFunctionBegin; 22220700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 22234f572ea9SToby Isaac PetscAssertPointer(dt, 2); 2224d763cef2SBarry Smith *dt = ts->time_step; 22253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2226d763cef2SBarry Smith } 2227d763cef2SBarry Smith 2228d8e5e3e6SSatish Balay /*@ 2229d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 2230d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 2231d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 2232d763cef2SBarry Smith the solution at the next timestep has been calculated. 2233d763cef2SBarry Smith 2234bcf0153eSBarry Smith Not Collective, but v returned is parallel if ts is parallel 2235d763cef2SBarry Smith 2236d763cef2SBarry Smith Input Parameter: 2237bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2238d763cef2SBarry Smith 2239d763cef2SBarry Smith Output Parameter: 2240d763cef2SBarry Smith . v - the vector containing the solution 2241d763cef2SBarry Smith 2242d763cef2SBarry Smith Level: intermediate 2243d763cef2SBarry Smith 2244bcf0153eSBarry Smith Note: 2245bcf0153eSBarry Smith If you used `TSSetExactFinalTime`(ts,`TS_EXACTFINALTIME_MATCHSTEP`); this does not return the solution at the requested 2246bcf0153eSBarry Smith final time. It returns the solution at the next timestep. 2247d763cef2SBarry Smith 22481cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTimeStep()`, `TSGetTime()`, `TSGetSolveTime()`, `TSGetSolutionComponents()`, `TSSetSolutionFunction()` 2249d763cef2SBarry Smith @*/ 2250d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSolution(TS ts, Vec *v) 2251d71ae5a4SJacob Faibussowitsch { 2252d763cef2SBarry Smith PetscFunctionBegin; 22530700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 22544f572ea9SToby Isaac PetscAssertPointer(v, 2); 22558737fe31SLisandro Dalcin *v = ts->vec_sol; 22563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2257d763cef2SBarry Smith } 2258d763cef2SBarry Smith 225903fe5f5eSDebojyoti Ghosh /*@ 2260b2bf4f3aSDebojyoti Ghosh TSGetSolutionComponents - Returns any solution components at the present 226103fe5f5eSDebojyoti Ghosh timestep, if available for the time integration method being used. 2262b2bf4f3aSDebojyoti Ghosh Solution components are quantities that share the same size and 226303fe5f5eSDebojyoti Ghosh structure as the solution vector. 226403fe5f5eSDebojyoti Ghosh 2265bcf0153eSBarry Smith Not Collective, but v returned is parallel if ts is parallel 226603fe5f5eSDebojyoti Ghosh 2267b43aa488SJacob Faibussowitsch Input Parameters: 2268bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` (input parameter). 2269195e9b02SBarry Smith . n - If v is `NULL`, then the number of solution components is 2270b2bf4f3aSDebojyoti Ghosh returned through n, else the n-th solution component is 227103fe5f5eSDebojyoti Ghosh returned in v. 2272a2b725a8SWilliam Gropp - v - the vector containing the n-th solution component 2273195e9b02SBarry Smith (may be `NULL` to use this function to find out 2274b2bf4f3aSDebojyoti Ghosh the number of solutions components). 227503fe5f5eSDebojyoti Ghosh 22764cdd57e5SDebojyoti Ghosh Level: advanced 227703fe5f5eSDebojyoti Ghosh 22781cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetSolution()` 227903fe5f5eSDebojyoti Ghosh @*/ 2280d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSolutionComponents(TS ts, PetscInt *n, Vec *v) 2281d71ae5a4SJacob Faibussowitsch { 228203fe5f5eSDebojyoti Ghosh PetscFunctionBegin; 228303fe5f5eSDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2284b2bf4f3aSDebojyoti Ghosh if (!ts->ops->getsolutioncomponents) *n = 0; 2285dbbe0bcdSBarry Smith else PetscUseTypeMethod(ts, getsolutioncomponents, n, v); 22863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 228703fe5f5eSDebojyoti Ghosh } 228803fe5f5eSDebojyoti Ghosh 22894cdd57e5SDebojyoti Ghosh /*@ 22904cdd57e5SDebojyoti Ghosh TSGetAuxSolution - Returns an auxiliary solution at the present 22914cdd57e5SDebojyoti Ghosh timestep, if available for the time integration method being used. 22924cdd57e5SDebojyoti Ghosh 2293bcf0153eSBarry Smith Not Collective, but v returned is parallel if ts is parallel 22944cdd57e5SDebojyoti Ghosh 2295b43aa488SJacob Faibussowitsch Input Parameters: 2296bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` (input parameter). 2297a2b725a8SWilliam Gropp - v - the vector containing the auxiliary solution 22984cdd57e5SDebojyoti Ghosh 22994cdd57e5SDebojyoti Ghosh Level: intermediate 23004cdd57e5SDebojyoti Ghosh 23011cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetSolution()` 23024cdd57e5SDebojyoti Ghosh @*/ 2303d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetAuxSolution(TS ts, Vec *v) 2304d71ae5a4SJacob Faibussowitsch { 23054cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 23064cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2307dbbe0bcdSBarry Smith if (ts->ops->getauxsolution) PetscUseTypeMethod(ts, getauxsolution, v); 23081e66621cSBarry Smith else PetscCall(VecZeroEntries(*v)); 23093ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23104cdd57e5SDebojyoti Ghosh } 23114cdd57e5SDebojyoti Ghosh 23124cdd57e5SDebojyoti Ghosh /*@ 23134cdd57e5SDebojyoti Ghosh TSGetTimeError - Returns the estimated error vector, if the chosen 2314bcf0153eSBarry Smith `TSType` has an error estimation functionality and `TSSetTimeError()` was called 23154cdd57e5SDebojyoti Ghosh 2316bcf0153eSBarry Smith Not Collective, but v returned is parallel if ts is parallel 23179657682dSDebojyoti Ghosh 2318b43aa488SJacob Faibussowitsch Input Parameters: 2319bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` (input parameter). 2320657c1e31SEmil Constantinescu . n - current estimate (n=0) or previous one (n=-1) 2321a2b725a8SWilliam Gropp - v - the vector containing the error (same size as the solution). 23224cdd57e5SDebojyoti Ghosh 23234cdd57e5SDebojyoti Ghosh Level: intermediate 23244cdd57e5SDebojyoti Ghosh 2325bcf0153eSBarry Smith Note: 2326bcf0153eSBarry Smith MUST call after `TSSetUp()` 23274cdd57e5SDebojyoti Ghosh 23281cc06b55SBarry Smith .seealso: [](ch_ts), `TSGetSolution()`, `TSSetTimeError()` 23294cdd57e5SDebojyoti Ghosh @*/ 2330d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeError(TS ts, PetscInt n, Vec *v) 2331d71ae5a4SJacob Faibussowitsch { 23324cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 23334cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2334dbbe0bcdSBarry Smith if (ts->ops->gettimeerror) PetscUseTypeMethod(ts, gettimeerror, n, v); 23351e66621cSBarry Smith else PetscCall(VecZeroEntries(*v)); 23363ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23374cdd57e5SDebojyoti Ghosh } 23384cdd57e5SDebojyoti Ghosh 233957df6a1bSDebojyoti Ghosh /*@ 234057df6a1bSDebojyoti Ghosh TSSetTimeError - Sets the estimated error vector, if the chosen 2341bcf0153eSBarry Smith `TSType` has an error estimation functionality. This can be used 234257df6a1bSDebojyoti Ghosh to restart such a time integrator with a given error vector. 234357df6a1bSDebojyoti Ghosh 2344bcf0153eSBarry Smith Not Collective, but v returned is parallel if ts is parallel 234557df6a1bSDebojyoti Ghosh 2346b43aa488SJacob Faibussowitsch Input Parameters: 2347bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` (input parameter). 2348a2b725a8SWilliam Gropp - v - the vector containing the error (same size as the solution). 234957df6a1bSDebojyoti Ghosh 235057df6a1bSDebojyoti Ghosh Level: intermediate 235157df6a1bSDebojyoti Ghosh 2352b43aa488SJacob Faibussowitsch .seealso: [](ch_ts), `TS`, `TSSetSolution()`, `TSGetTimeError()` 235357df6a1bSDebojyoti Ghosh @*/ 2354d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTimeError(TS ts, Vec v) 2355d71ae5a4SJacob Faibussowitsch { 235657df6a1bSDebojyoti Ghosh PetscFunctionBegin; 235757df6a1bSDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 23583c633725SBarry Smith PetscCheck(ts->setupcalled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call TSSetUp() first"); 2359dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, settimeerror, v); 23603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 236157df6a1bSDebojyoti Ghosh } 236257df6a1bSDebojyoti Ghosh 2363bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 2364d8e5e3e6SSatish Balay /*@ 2365bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 2366d763cef2SBarry Smith 2367bdad233fSMatthew Knepley Not collective 2368d763cef2SBarry Smith 2369bdad233fSMatthew Knepley Input Parameters: 2370bcf0153eSBarry Smith + ts - The `TS` 2371bcf0153eSBarry Smith - type - One of `TS_LINEAR`, `TS_NONLINEAR` where these types refer to problems of the forms 2372d763cef2SBarry Smith .vb 23730910c330SBarry Smith U_t - A U = 0 (linear) 23740910c330SBarry Smith U_t - A(t) U = 0 (linear) 23750910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 2376d763cef2SBarry Smith .ve 2377d763cef2SBarry Smith 2378d763cef2SBarry Smith Level: beginner 2379d763cef2SBarry Smith 23801cc06b55SBarry Smith .seealso: [](ch_ts), `TSSetUp()`, `TSProblemType`, `TS` 2381d763cef2SBarry Smith @*/ 2382d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 2383d71ae5a4SJacob Faibussowitsch { 2384d763cef2SBarry Smith PetscFunctionBegin; 23850700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2386bdad233fSMatthew Knepley ts->problem_type = type; 23879e2a6581SJed Brown if (type == TS_LINEAR) { 23889e2a6581SJed Brown SNES snes; 23899566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 23909566063dSJacob Faibussowitsch PetscCall(SNESSetType(snes, SNESKSPONLY)); 23919e2a6581SJed Brown } 23923ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2393d763cef2SBarry Smith } 2394d763cef2SBarry Smith 2395cc4c1da9SBarry Smith /*@ 2396bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 2397bdad233fSMatthew Knepley 2398bdad233fSMatthew Knepley Not collective 2399bdad233fSMatthew Knepley 2400bdad233fSMatthew Knepley Input Parameter: 2401bcf0153eSBarry Smith . ts - The `TS` 2402bdad233fSMatthew Knepley 2403bdad233fSMatthew Knepley Output Parameter: 2404bcf0153eSBarry Smith . type - One of `TS_LINEAR`, `TS_NONLINEAR` where these types refer to problems of the forms 2405bdad233fSMatthew Knepley .vb 2406089b2837SJed Brown M U_t = A U 2407089b2837SJed Brown M(t) U_t = A(t) U 2408b5abc632SBarry Smith F(t,U,U_t) 2409bdad233fSMatthew Knepley .ve 2410bdad233fSMatthew Knepley 2411bdad233fSMatthew Knepley Level: beginner 2412bdad233fSMatthew Knepley 24131cc06b55SBarry Smith .seealso: [](ch_ts), `TSSetUp()`, `TSProblemType`, `TS` 2414bdad233fSMatthew Knepley @*/ 2415d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 2416d71ae5a4SJacob Faibussowitsch { 2417bdad233fSMatthew Knepley PetscFunctionBegin; 24180700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 24194f572ea9SToby Isaac PetscAssertPointer(type, 2); 2420bdad233fSMatthew Knepley *type = ts->problem_type; 24213ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2422bdad233fSMatthew Knepley } 2423d763cef2SBarry Smith 2424303a5415SBarry Smith /* 2425303a5415SBarry 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() 2426303a5415SBarry Smith */ 2427d71ae5a4SJacob Faibussowitsch static PetscErrorCode TSSetExactFinalTimeDefault(TS ts) 2428d71ae5a4SJacob Faibussowitsch { 2429303a5415SBarry Smith PetscBool isnone; 2430303a5415SBarry Smith 2431303a5415SBarry Smith PetscFunctionBegin; 24329566063dSJacob Faibussowitsch PetscCall(TSGetAdapt(ts, &ts->adapt)); 24339566063dSJacob Faibussowitsch PetscCall(TSAdaptSetDefaultType(ts->adapt, ts->default_adapt_type)); 2434303a5415SBarry Smith 24359566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)ts->adapt, TSADAPTNONE, &isnone)); 24361e66621cSBarry Smith if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP; 24371e66621cSBarry Smith else if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) ts->exact_final_time = TS_EXACTFINALTIME_INTERPOLATE; 24383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2439303a5415SBarry Smith } 2440303a5415SBarry Smith 2441d763cef2SBarry Smith /*@ 2442303a5415SBarry Smith TSSetUp - Sets up the internal data structures for the later use of a timestepper. 2443d763cef2SBarry Smith 2444c3339decSBarry Smith Collective 2445d763cef2SBarry Smith 2446d763cef2SBarry Smith Input Parameter: 2447bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2448d763cef2SBarry Smith 2449d763cef2SBarry Smith Level: advanced 2450d763cef2SBarry Smith 2451bcf0153eSBarry Smith Note: 2452bcf0153eSBarry Smith For basic use of the `TS` solvers the user need not explicitly call 2453bcf0153eSBarry Smith `TSSetUp()`, since these actions will automatically occur during 2454bcf0153eSBarry Smith the call to `TSStep()` or `TSSolve()`. However, if one wishes to control this 2455bcf0153eSBarry Smith phase separately, `TSSetUp()` should be called after `TSCreate()` 2456bcf0153eSBarry Smith and optional routines of the form TSSetXXX(), but before `TSStep()` and `TSSolve()`. 2457bcf0153eSBarry Smith 24581cc06b55SBarry Smith .seealso: [](ch_ts), `TSCreate()`, `TS`, `TSStep()`, `TSDestroy()`, `TSSolve()` 2459d763cef2SBarry Smith @*/ 2460d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetUp(TS ts) 2461d71ae5a4SJacob Faibussowitsch { 24626c6b9e74SPeter Brune DM dm; 24636c6b9e74SPeter Brune PetscErrorCode (*func)(SNES, Vec, Vec, void *); 2464d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES, Vec, Mat, Mat, void *); 24658434afd1SBarry Smith TSIFunctionFn *ifun; 24668434afd1SBarry Smith TSIJacobianFn *ijac; 24678434afd1SBarry Smith TSI2JacobianFn *i2jac; 24688434afd1SBarry Smith TSRHSJacobianFn *rhsjac; 2469d763cef2SBarry Smith 2470d763cef2SBarry Smith PetscFunctionBegin; 24710700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 24723ba16761SJacob Faibussowitsch if (ts->setupcalled) PetscFunctionReturn(PETSC_SUCCESS); 2473277b19d0SLisandro Dalcin 24747adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 24759566063dSJacob Faibussowitsch PetscCall(TSGetIFunction(ts, NULL, &ifun, NULL)); 24769566063dSJacob Faibussowitsch PetscCall(TSSetType(ts, ifun ? TSBEULER : TSEULER)); 2477d763cef2SBarry Smith } 2478277b19d0SLisandro Dalcin 24791a638600SStefano Zampini if (!ts->vec_sol) { 24801e66621cSBarry Smith PetscCheck(ts->dm, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call TSSetSolution() first"); 24819566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(ts->dm, &ts->vec_sol)); 24821a638600SStefano Zampini } 2483277b19d0SLisandro Dalcin 24844a658b32SHong Zhang if (ts->tspan) { 24851e66621cSBarry Smith if (!ts->tspan->vecs_sol) PetscCall(VecDuplicateVecs(ts->vec_sol, ts->tspan->num_span_times, &ts->tspan->vecs_sol)); 24864a658b32SHong Zhang } 2487298bade4SHong Zhang if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */ 24889566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)ts->Jacprhs)); 2489298bade4SHong Zhang ts->Jacp = ts->Jacprhs; 2490298bade4SHong Zhang } 2491298bade4SHong Zhang 2492cd4cee2dSHong Zhang if (ts->quadraturets) { 24939566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts->quadraturets)); 24949566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_costintegrand)); 24959566063dSJacob Faibussowitsch PetscCall(VecDuplicate(ts->quadraturets->vec_sol, &ts->vec_costintegrand)); 2496cd4cee2dSHong Zhang } 2497cd4cee2dSHong Zhang 24989566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts, NULL, NULL, &rhsjac, NULL)); 2499f23ba4b3SHong Zhang if (rhsjac == TSComputeRHSJacobianConstant) { 2500e1244c69SJed Brown Mat Amat, Pmat; 2501e1244c69SJed Brown SNES snes; 25029566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 25039566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes, &Amat, &Pmat, NULL, NULL)); 2504e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 2505e1244c69SJed Brown * have displaced the RHS matrix */ 2506971015bcSStefano Zampini if (Amat && Amat == ts->Arhs) { 2507abc0d4abSBarry 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 */ 25089566063dSJacob Faibussowitsch PetscCall(MatDuplicate(ts->Arhs, MAT_COPY_VALUES, &Amat)); 25099566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes, Amat, NULL, NULL, NULL)); 25109566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Amat)); 2511e1244c69SJed Brown } 2512971015bcSStefano Zampini if (Pmat && Pmat == ts->Brhs) { 25139566063dSJacob Faibussowitsch PetscCall(MatDuplicate(ts->Brhs, MAT_COPY_VALUES, &Pmat)); 25149566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes, NULL, Pmat, NULL, NULL)); 25159566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Pmat)); 2516e1244c69SJed Brown } 2517e1244c69SJed Brown } 25182ffb9264SLisandro Dalcin 25199566063dSJacob Faibussowitsch PetscCall(TSGetAdapt(ts, &ts->adapt)); 25209566063dSJacob Faibussowitsch PetscCall(TSAdaptSetDefaultType(ts->adapt, ts->default_adapt_type)); 25212ffb9264SLisandro Dalcin 2522dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, setup); 2523277b19d0SLisandro Dalcin 25249566063dSJacob Faibussowitsch PetscCall(TSSetExactFinalTimeDefault(ts)); 25252ffb9264SLisandro Dalcin 2526a6772fa2SLisandro Dalcin /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 25276c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 25286c6b9e74SPeter Brune */ 25299566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 25309566063dSJacob Faibussowitsch PetscCall(DMSNESGetFunction(dm, &func, NULL)); 25311e66621cSBarry Smith if (!func) PetscCall(DMSNESSetFunction(dm, SNESTSFormFunction, ts)); 25321e66621cSBarry Smith 2533a6772fa2SLisandro 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. 25346c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 25356c6b9e74SPeter Brune */ 25369566063dSJacob Faibussowitsch PetscCall(DMSNESGetJacobian(dm, &jac, NULL)); 25379566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm, &ijac, NULL)); 25389566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm, &i2jac, NULL)); 25399566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, &rhsjac, NULL)); 25401e66621cSBarry Smith if (!jac && (ijac || i2jac || rhsjac)) PetscCall(DMSNESSetJacobian(dm, SNESTSFormJacobian, ts)); 2541c0517034SDebojyoti Ghosh 2542c0517034SDebojyoti Ghosh /* if time integration scheme has a starting method, call it */ 2543dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, startingmethod); 2544c0517034SDebojyoti Ghosh 2545277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 25463ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2547277b19d0SLisandro Dalcin } 2548277b19d0SLisandro Dalcin 2549f6a906c0SBarry Smith /*@ 2550bcf0153eSBarry Smith TSReset - Resets a `TS` context and removes any allocated `Vec`s and `Mat`s. 2551277b19d0SLisandro Dalcin 2552c3339decSBarry Smith Collective 2553277b19d0SLisandro Dalcin 2554277b19d0SLisandro Dalcin Input Parameter: 2555bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2556277b19d0SLisandro Dalcin 2557277b19d0SLisandro Dalcin Level: beginner 2558277b19d0SLisandro Dalcin 25591cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSSetup()`, `TSDestroy()` 2560277b19d0SLisandro Dalcin @*/ 2561d71ae5a4SJacob Faibussowitsch PetscErrorCode TSReset(TS ts) 2562d71ae5a4SJacob Faibussowitsch { 25631d06f6b3SHong Zhang TS_RHSSplitLink ilink = ts->tsrhssplit, next; 2564277b19d0SLisandro Dalcin 2565277b19d0SLisandro Dalcin PetscFunctionBegin; 2566277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2567b18ea86cSHong Zhang 2568dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, reset); 25699566063dSJacob Faibussowitsch if (ts->snes) PetscCall(SNESReset(ts->snes)); 25709566063dSJacob Faibussowitsch if (ts->adapt) PetscCall(TSAdaptReset(ts->adapt)); 2571bbd56ea5SKarl Rupp 25729566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Arhs)); 25739566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Brhs)); 25749566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->Frhs)); 25759566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_sol)); 2576c61711c8SStefano Zampini PetscCall(VecDestroy(&ts->vec_sol0)); 25779566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_dot)); 25789566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vatol)); 25799566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vrtol)); 25809566063dSJacob Faibussowitsch PetscCall(VecDestroyVecs(ts->nwork, &ts->work)); 2581bbd56ea5SKarl Rupp 25829566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Jacprhs)); 25839566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Jacp)); 25841baa6e33SBarry Smith if (ts->forward_solve) PetscCall(TSForwardReset(ts)); 2585cd4cee2dSHong Zhang if (ts->quadraturets) { 25869566063dSJacob Faibussowitsch PetscCall(TSReset(ts->quadraturets)); 25879566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_costintegrand)); 2588cd4cee2dSHong Zhang } 25891d06f6b3SHong Zhang while (ilink) { 25901d06f6b3SHong Zhang next = ilink->next; 25919566063dSJacob Faibussowitsch PetscCall(TSDestroy(&ilink->ts)); 25929566063dSJacob Faibussowitsch PetscCall(PetscFree(ilink->splitname)); 25939566063dSJacob Faibussowitsch PetscCall(ISDestroy(&ilink->is)); 25949566063dSJacob Faibussowitsch PetscCall(PetscFree(ilink)); 25951d06f6b3SHong Zhang ilink = next; 259687f4e208SHong Zhang } 25976bf673ebSJoe Pusztay ts->tsrhssplit = NULL; 2598545aaa6fSHong Zhang ts->num_rhs_splits = 0; 25994a658b32SHong Zhang if (ts->tspan) { 26004a658b32SHong Zhang PetscCall(PetscFree(ts->tspan->span_times)); 26014a658b32SHong Zhang PetscCall(VecDestroyVecs(ts->tspan->num_span_times, &ts->tspan->vecs_sol)); 26024a658b32SHong Zhang PetscCall(PetscFree(ts->tspan)); 26034a658b32SHong Zhang } 2604b3a72457SStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 2605b3a72457SStefano Zampini ts->rhsjacobian.scale = 1.0; 2606b3a72457SStefano Zampini ts->ijacobian.shift = 1.0; 2607277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 26083ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2609d763cef2SBarry Smith } 2610d763cef2SBarry Smith 261114d0ab18SJacob Faibussowitsch static PetscErrorCode TSResizeReset(TS); 261214d0ab18SJacob Faibussowitsch 26130764c050SBarry Smith /*@ 2614d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2615bcf0153eSBarry Smith with `TSCreate()`. 2616d763cef2SBarry Smith 2617c3339decSBarry Smith Collective 2618d763cef2SBarry Smith 2619d763cef2SBarry Smith Input Parameter: 2620bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2621d763cef2SBarry Smith 2622d763cef2SBarry Smith Level: beginner 2623d763cef2SBarry Smith 26241cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSSetUp()`, `TSSolve()` 2625d763cef2SBarry Smith @*/ 2626d71ae5a4SJacob Faibussowitsch PetscErrorCode TSDestroy(TS *ts) 2627d71ae5a4SJacob Faibussowitsch { 2628d763cef2SBarry Smith PetscFunctionBegin; 26293ba16761SJacob Faibussowitsch if (!*ts) PetscFunctionReturn(PETSC_SUCCESS); 2630ecf68647SHong Zhang PetscValidHeaderSpecific(*ts, TS_CLASSID, 1); 2631f4f49eeaSPierre Jolivet if (--((PetscObject)*ts)->refct > 0) { 26329371c9d4SSatish Balay *ts = NULL; 26333ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 26349371c9d4SSatish Balay } 2635d763cef2SBarry Smith 26369566063dSJacob Faibussowitsch PetscCall(TSReset(*ts)); 26379566063dSJacob Faibussowitsch PetscCall(TSAdjointReset(*ts)); 26381e66621cSBarry Smith if ((*ts)->forward_solve) PetscCall(TSForwardReset(*ts)); 26396bd3a4fdSStefano Zampini PetscCall(TSResizeReset(*ts)); 26401e66621cSBarry Smith 2641e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 26429566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsViewOff((PetscObject)*ts)); 2643f4f49eeaSPierre Jolivet PetscTryTypeMethod(*ts, destroy); 26446d4c513bSLisandro Dalcin 26459566063dSJacob Faibussowitsch PetscCall(TSTrajectoryDestroy(&(*ts)->trajectory)); 2646bc952696SBarry Smith 26479566063dSJacob Faibussowitsch PetscCall(TSAdaptDestroy(&(*ts)->adapt)); 26489566063dSJacob Faibussowitsch PetscCall(TSEventDestroy(&(*ts)->event)); 26496427ac75SLisandro Dalcin 26509566063dSJacob Faibussowitsch PetscCall(SNESDestroy(&(*ts)->snes)); 26519566063dSJacob Faibussowitsch PetscCall(DMDestroy(&(*ts)->dm)); 2652f4f49eeaSPierre Jolivet PetscCall(TSMonitorCancel(*ts)); 2653f4f49eeaSPierre Jolivet PetscCall(TSAdjointMonitorCancel(*ts)); 26546d4c513bSLisandro Dalcin 26559566063dSJacob Faibussowitsch PetscCall(TSDestroy(&(*ts)->quadraturets)); 26569566063dSJacob Faibussowitsch PetscCall(PetscHeaderDestroy(ts)); 26573ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2658d763cef2SBarry Smith } 2659d763cef2SBarry Smith 2660d8e5e3e6SSatish Balay /*@ 2661bcf0153eSBarry Smith TSGetSNES - Returns the `SNES` (nonlinear solver) associated with 2662bcf0153eSBarry Smith a `TS` (timestepper) context. Valid only for nonlinear problems. 2663d763cef2SBarry Smith 2664bcf0153eSBarry Smith Not Collective, but snes is parallel if ts is parallel 2665d763cef2SBarry Smith 2666d763cef2SBarry Smith Input Parameter: 2667bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2668d763cef2SBarry Smith 2669d763cef2SBarry Smith Output Parameter: 2670d763cef2SBarry Smith . snes - the nonlinear solver context 2671d763cef2SBarry Smith 2672d763cef2SBarry Smith Level: beginner 2673d763cef2SBarry Smith 2674bcf0153eSBarry Smith Notes: 2675bcf0153eSBarry Smith The user can then directly manipulate the `SNES` context to set various 2676bcf0153eSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2677bcf0153eSBarry Smith `KSP`, and `PC` contexts as well. 2678bcf0153eSBarry Smith 2679bcf0153eSBarry Smith `TSGetSNES()` does not work for integrators that do not use `SNES`; in 2680195e9b02SBarry Smith this case `TSGetSNES()` returns `NULL` in `snes`. 2681bcf0153eSBarry Smith 26821cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `TSCreate()`, `TSSetUp()`, `TSSolve()` 2683d763cef2SBarry Smith @*/ 2684d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSNES(TS ts, SNES *snes) 2685d71ae5a4SJacob Faibussowitsch { 2686d763cef2SBarry Smith PetscFunctionBegin; 26870700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 26884f572ea9SToby Isaac PetscAssertPointer(snes, 2); 2689d372ba47SLisandro Dalcin if (!ts->snes) { 26909566063dSJacob Faibussowitsch PetscCall(SNESCreate(PetscObjectComm((PetscObject)ts), &ts->snes)); 26919566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptions((PetscObject)ts->snes, ((PetscObject)ts)->options)); 26929566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(ts->snes, NULL, SNESTSFormFunction, ts)); 26939566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)ts->snes, (PetscObject)ts, 1)); 26949566063dSJacob Faibussowitsch if (ts->dm) PetscCall(SNESSetDM(ts->snes, ts->dm)); 26951e66621cSBarry Smith if (ts->problem_type == TS_LINEAR) PetscCall(SNESSetType(ts->snes, SNESKSPONLY)); 2696d372ba47SLisandro Dalcin } 2697d763cef2SBarry Smith *snes = ts->snes; 26983ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2699d763cef2SBarry Smith } 2700d763cef2SBarry Smith 2701deb2cd25SJed Brown /*@ 2702bcf0153eSBarry Smith TSSetSNES - Set the `SNES` (nonlinear solver) to be used by the timestepping context 2703deb2cd25SJed Brown 2704deb2cd25SJed Brown Collective 2705deb2cd25SJed Brown 2706d8d19677SJose E. Roman Input Parameters: 2707bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 2708deb2cd25SJed Brown - snes - the nonlinear solver context 2709deb2cd25SJed Brown 2710deb2cd25SJed Brown Level: developer 2711deb2cd25SJed Brown 2712bcf0153eSBarry Smith Note: 2713bcf0153eSBarry Smith Most users should have the `TS` created by calling `TSGetSNES()` 2714bcf0153eSBarry Smith 27151cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `TSCreate()`, `TSSetUp()`, `TSSolve()`, `TSGetSNES()` 2716deb2cd25SJed Brown @*/ 2717d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetSNES(TS ts, SNES snes) 2718d71ae5a4SJacob Faibussowitsch { 2719d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES, Vec, Mat, Mat, void *); 2720deb2cd25SJed Brown 2721deb2cd25SJed Brown PetscFunctionBegin; 2722deb2cd25SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2723deb2cd25SJed Brown PetscValidHeaderSpecific(snes, SNES_CLASSID, 2); 27249566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)snes)); 27259566063dSJacob Faibussowitsch PetscCall(SNESDestroy(&ts->snes)); 2726bbd56ea5SKarl Rupp 2727deb2cd25SJed Brown ts->snes = snes; 2728bbd56ea5SKarl Rupp 27299566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(ts->snes, NULL, SNESTSFormFunction, ts)); 27309566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(ts->snes, NULL, NULL, &func, NULL)); 27311e66621cSBarry Smith if (func == SNESTSFormJacobian) PetscCall(SNESSetJacobian(ts->snes, NULL, NULL, SNESTSFormJacobian, ts)); 27323ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2733deb2cd25SJed Brown } 2734deb2cd25SJed Brown 2735d8e5e3e6SSatish Balay /*@ 2736bcf0153eSBarry Smith TSGetKSP - Returns the `KSP` (linear solver) associated with 2737bcf0153eSBarry Smith a `TS` (timestepper) context. 2738d763cef2SBarry Smith 2739195e9b02SBarry Smith Not Collective, but `ksp` is parallel if `ts` is parallel 2740d763cef2SBarry Smith 2741d763cef2SBarry Smith Input Parameter: 2742bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2743d763cef2SBarry Smith 2744d763cef2SBarry Smith Output Parameter: 274594b7f48cSBarry Smith . ksp - the nonlinear solver context 2746d763cef2SBarry Smith 2747d763cef2SBarry Smith Level: beginner 2748d763cef2SBarry Smith 2749bcf0153eSBarry Smith Notes: 2750bcf0153eSBarry Smith The user can then directly manipulate the `KSP` context to set various 2751bcf0153eSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2752bcf0153eSBarry Smith `PC` context as well. 2753bcf0153eSBarry Smith 2754bcf0153eSBarry Smith `TSGetKSP()` does not work for integrators that do not use `KSP`; 2755195e9b02SBarry Smith in this case `TSGetKSP()` returns `NULL` in `ksp`. 2756bcf0153eSBarry Smith 27571cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `KSP`, `TSCreate()`, `TSSetUp()`, `TSSolve()`, `TSGetSNES()` 2758d763cef2SBarry Smith @*/ 2759d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetKSP(TS ts, KSP *ksp) 2760d71ae5a4SJacob Faibussowitsch { 2761089b2837SJed Brown SNES snes; 2762d372ba47SLisandro Dalcin 2763d763cef2SBarry Smith PetscFunctionBegin; 27640700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 27654f572ea9SToby Isaac PetscAssertPointer(ksp, 2); 27663c633725SBarry Smith PetscCheck(((PetscObject)ts)->type_name, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "KSP is not created yet. Call TSSetType() first"); 27673c633725SBarry Smith PetscCheck(ts->problem_type == TS_LINEAR, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Linear only; use TSGetSNES()"); 27689566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 27699566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes, ksp)); 27703ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2771d763cef2SBarry Smith } 2772d763cef2SBarry Smith 2773d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2774d763cef2SBarry Smith 2775adb62b0dSMatthew Knepley /*@ 2776618ce8baSLisandro Dalcin TSSetMaxSteps - Sets the maximum number of steps to use. 2777618ce8baSLisandro Dalcin 2778c3339decSBarry Smith Logically Collective 2779618ce8baSLisandro Dalcin 2780618ce8baSLisandro Dalcin Input Parameters: 2781bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 2782618ce8baSLisandro Dalcin - maxsteps - maximum number of steps to use 2783618ce8baSLisandro Dalcin 2784bcf0153eSBarry Smith Options Database Key: 2785618ce8baSLisandro Dalcin . -ts_max_steps <maxsteps> - Sets maxsteps 2786618ce8baSLisandro Dalcin 2787618ce8baSLisandro Dalcin Level: intermediate 2788618ce8baSLisandro Dalcin 2789bcf0153eSBarry Smith Note: 279009cb0f53SBarry Smith Use `PETSC_DETERMINE` to reset the maximum number of steps to the default from when the object's type was set 279109cb0f53SBarry Smith 279209cb0f53SBarry Smith The default maximum number of steps is 5,000 279309cb0f53SBarry Smith 279409cb0f53SBarry Smith Fortran Note: 279509cb0f53SBarry Smith Use `PETSC_DETERMINE_INTEGER` 2796bcf0153eSBarry Smith 27971cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetMaxSteps()`, `TSSetMaxTime()`, `TSSetExactFinalTime()` 2798618ce8baSLisandro Dalcin @*/ 2799d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxSteps(TS ts, PetscInt maxsteps) 2800d71ae5a4SJacob Faibussowitsch { 2801618ce8baSLisandro Dalcin PetscFunctionBegin; 2802618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2803618ce8baSLisandro Dalcin PetscValidLogicalCollectiveInt(ts, maxsteps, 2); 280409cb0f53SBarry Smith if (maxsteps == PETSC_DETERMINE) { 280509cb0f53SBarry Smith ts->max_steps = ts->default_max_steps; 280609cb0f53SBarry Smith } else { 28073c633725SBarry Smith PetscCheck(maxsteps >= 0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Maximum number of steps must be non-negative"); 2808618ce8baSLisandro Dalcin ts->max_steps = maxsteps; 280909cb0f53SBarry Smith } 28103ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2811618ce8baSLisandro Dalcin } 2812618ce8baSLisandro Dalcin 2813618ce8baSLisandro Dalcin /*@ 2814618ce8baSLisandro Dalcin TSGetMaxSteps - Gets the maximum number of steps to use. 2815618ce8baSLisandro Dalcin 2816618ce8baSLisandro Dalcin Not Collective 2817618ce8baSLisandro Dalcin 28182fe279fdSBarry Smith Input Parameter: 2819bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2820618ce8baSLisandro Dalcin 2821618ce8baSLisandro Dalcin Output Parameter: 2822618ce8baSLisandro Dalcin . maxsteps - maximum number of steps to use 2823618ce8baSLisandro Dalcin 2824618ce8baSLisandro Dalcin Level: advanced 2825618ce8baSLisandro Dalcin 28261cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetMaxSteps()`, `TSGetMaxTime()`, `TSSetMaxTime()` 2827618ce8baSLisandro Dalcin @*/ 2828d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetMaxSteps(TS ts, PetscInt *maxsteps) 2829d71ae5a4SJacob Faibussowitsch { 2830618ce8baSLisandro Dalcin PetscFunctionBegin; 2831618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 28324f572ea9SToby Isaac PetscAssertPointer(maxsteps, 2); 2833618ce8baSLisandro Dalcin *maxsteps = ts->max_steps; 28343ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2835618ce8baSLisandro Dalcin } 2836618ce8baSLisandro Dalcin 2837618ce8baSLisandro Dalcin /*@ 2838618ce8baSLisandro Dalcin TSSetMaxTime - Sets the maximum (or final) time for timestepping. 2839618ce8baSLisandro Dalcin 2840c3339decSBarry Smith Logically Collective 2841618ce8baSLisandro Dalcin 2842618ce8baSLisandro Dalcin Input Parameters: 2843bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 2844618ce8baSLisandro Dalcin - maxtime - final time to step to 2845618ce8baSLisandro Dalcin 2846bcf0153eSBarry Smith Options Database Key: 2847ef85077eSLisandro Dalcin . -ts_max_time <maxtime> - Sets maxtime 2848618ce8baSLisandro Dalcin 2849bcf0153eSBarry Smith Level: intermediate 2850bcf0153eSBarry Smith 2851618ce8baSLisandro Dalcin Notes: 285209cb0f53SBarry Smith Use `PETSC_DETERMINE` to reset the maximum time to the default from when the object's type was set 285309cb0f53SBarry Smith 2854618ce8baSLisandro Dalcin The default maximum time is 5.0 2855618ce8baSLisandro Dalcin 285609cb0f53SBarry Smith Fortran Note: 285709cb0f53SBarry Smith Use `PETSC_DETERMINE_REAL` 285809cb0f53SBarry Smith 28591cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetMaxTime()`, `TSSetMaxSteps()`, `TSSetExactFinalTime()` 2860618ce8baSLisandro Dalcin @*/ 2861d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxTime(TS ts, PetscReal maxtime) 2862d71ae5a4SJacob Faibussowitsch { 2863618ce8baSLisandro Dalcin PetscFunctionBegin; 2864618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2865618ce8baSLisandro Dalcin PetscValidLogicalCollectiveReal(ts, maxtime, 2); 286609cb0f53SBarry Smith if (maxtime == PETSC_DETERMINE) { 286709cb0f53SBarry Smith ts->max_time = ts->default_max_time; 286809cb0f53SBarry Smith } else { 2869618ce8baSLisandro Dalcin ts->max_time = maxtime; 287009cb0f53SBarry Smith } 28713ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2872618ce8baSLisandro Dalcin } 2873618ce8baSLisandro Dalcin 2874618ce8baSLisandro Dalcin /*@ 2875618ce8baSLisandro Dalcin TSGetMaxTime - Gets the maximum (or final) time for timestepping. 2876618ce8baSLisandro Dalcin 2877618ce8baSLisandro Dalcin Not Collective 2878618ce8baSLisandro Dalcin 28792fe279fdSBarry Smith Input Parameter: 2880bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2881618ce8baSLisandro Dalcin 2882618ce8baSLisandro Dalcin Output Parameter: 2883618ce8baSLisandro Dalcin . maxtime - final time to step to 2884618ce8baSLisandro Dalcin 2885618ce8baSLisandro Dalcin Level: advanced 2886618ce8baSLisandro Dalcin 28871cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetMaxTime()`, `TSGetMaxSteps()`, `TSSetMaxSteps()` 2888618ce8baSLisandro Dalcin @*/ 2889d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetMaxTime(TS ts, PetscReal *maxtime) 2890d71ae5a4SJacob Faibussowitsch { 2891618ce8baSLisandro Dalcin PetscFunctionBegin; 2892618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 28934f572ea9SToby Isaac PetscAssertPointer(maxtime, 2); 2894618ce8baSLisandro Dalcin *maxtime = ts->max_time; 28953ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2896618ce8baSLisandro Dalcin } 2897618ce8baSLisandro Dalcin 289814d0ab18SJacob Faibussowitsch // PetscClangLinter pragma disable: -fdoc-* 2899618ce8baSLisandro Dalcin /*@ 2900bcf0153eSBarry Smith TSSetInitialTimeStep - Deprecated, use `TSSetTime()` and `TSSetTimeStep()`. 2901edc382c3SSatish Balay 2902edc382c3SSatish Balay Level: deprecated 2903edc382c3SSatish Balay 2904aaa6c58dSLisandro Dalcin @*/ 2905d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetInitialTimeStep(TS ts, PetscReal initial_time, PetscReal time_step) 2906d71ae5a4SJacob Faibussowitsch { 2907aaa6c58dSLisandro Dalcin PetscFunctionBegin; 2908aaa6c58dSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 29099566063dSJacob Faibussowitsch PetscCall(TSSetTime(ts, initial_time)); 29109566063dSJacob Faibussowitsch PetscCall(TSSetTimeStep(ts, time_step)); 29113ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2912aaa6c58dSLisandro Dalcin } 2913aaa6c58dSLisandro Dalcin 291414d0ab18SJacob Faibussowitsch // PetscClangLinter pragma disable: -fdoc-* 2915aaa6c58dSLisandro Dalcin /*@ 2916bcf0153eSBarry Smith TSGetDuration - Deprecated, use `TSGetMaxSteps()` and `TSGetMaxTime()`. 2917edc382c3SSatish Balay 2918edc382c3SSatish Balay Level: deprecated 2919edc382c3SSatish Balay 2920adb62b0dSMatthew Knepley @*/ 2921d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2922d71ae5a4SJacob Faibussowitsch { 2923adb62b0dSMatthew Knepley PetscFunctionBegin; 29240700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2925abc0a331SBarry Smith if (maxsteps) { 29264f572ea9SToby Isaac PetscAssertPointer(maxsteps, 2); 2927adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2928adb62b0dSMatthew Knepley } 2929abc0a331SBarry Smith if (maxtime) { 29304f572ea9SToby Isaac PetscAssertPointer(maxtime, 3); 2931adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2932adb62b0dSMatthew Knepley } 29333ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2934adb62b0dSMatthew Knepley } 2935adb62b0dSMatthew Knepley 293614d0ab18SJacob Faibussowitsch // PetscClangLinter pragma disable: -fdoc-* 2937d763cef2SBarry Smith /*@ 2938bcf0153eSBarry Smith TSSetDuration - Deprecated, use `TSSetMaxSteps()` and `TSSetMaxTime()`. 2939edc382c3SSatish Balay 2940edc382c3SSatish Balay Level: deprecated 2941edc382c3SSatish Balay 2942d763cef2SBarry Smith @*/ 2943d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetDuration(TS ts, PetscInt maxsteps, PetscReal maxtime) 2944d71ae5a4SJacob Faibussowitsch { 2945d763cef2SBarry Smith PetscFunctionBegin; 294609cb0f53SBarry Smith if (maxsteps != (PetscInt)PETSC_CURRENT) PetscCall(TSSetMaxSteps(ts, maxsteps)); 294709cb0f53SBarry Smith if (maxtime != (PetscReal)PETSC_CURRENT) PetscCall(TSSetMaxTime(ts, maxtime)); 29483ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2949d763cef2SBarry Smith } 2950d763cef2SBarry Smith 295114d0ab18SJacob Faibussowitsch // PetscClangLinter pragma disable: -fdoc-* 2952d763cef2SBarry Smith /*@ 2953bcf0153eSBarry Smith TSGetTimeStepNumber - Deprecated, use `TSGetStepNumber()`. 2954edc382c3SSatish Balay 2955edc382c3SSatish Balay Level: deprecated 2956edc382c3SSatish Balay 29575c5f5948SLisandro Dalcin @*/ 2958d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeStepNumber(TS ts, PetscInt *steps) 2959d71ae5a4SJacob Faibussowitsch { 29609371c9d4SSatish Balay return TSGetStepNumber(ts, steps); 29619371c9d4SSatish Balay } 29625c5f5948SLisandro Dalcin 296314d0ab18SJacob Faibussowitsch // PetscClangLinter pragma disable: -fdoc-* 296419eac22cSLisandro Dalcin /*@ 2965bcf0153eSBarry Smith TSGetTotalSteps - Deprecated, use `TSGetStepNumber()`. 2966edc382c3SSatish Balay 2967edc382c3SSatish Balay Level: deprecated 2968edc382c3SSatish Balay 29694f4e0956SLisandro Dalcin @*/ 2970d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTotalSteps(TS ts, PetscInt *steps) 2971d71ae5a4SJacob Faibussowitsch { 29729371c9d4SSatish Balay return TSGetStepNumber(ts, steps); 29739371c9d4SSatish Balay } 29744f4e0956SLisandro Dalcin 29754f4e0956SLisandro Dalcin /*@ 2976d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 2977bcf0153eSBarry Smith for use by the `TS` routines. 2978d763cef2SBarry Smith 2979c3339decSBarry Smith Logically Collective 2980d763cef2SBarry Smith 2981d763cef2SBarry Smith Input Parameters: 2982bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 29830910c330SBarry Smith - u - the solution vector 2984d763cef2SBarry Smith 2985d763cef2SBarry Smith Level: beginner 2986d763cef2SBarry Smith 29871cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetSolutionFunction()`, `TSGetSolution()`, `TSCreate()` 2988d763cef2SBarry Smith @*/ 2989d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetSolution(TS ts, Vec u) 2990d71ae5a4SJacob Faibussowitsch { 2991496e6a7aSJed Brown DM dm; 29928737fe31SLisandro Dalcin 2993d763cef2SBarry Smith PetscFunctionBegin; 29940700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 29950910c330SBarry Smith PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 29969566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)u)); 29979566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_sol)); 29980910c330SBarry Smith ts->vec_sol = u; 2999bbd56ea5SKarl Rupp 30009566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 30019566063dSJacob Faibussowitsch PetscCall(DMShellSetGlobalVector(dm, u)); 30023ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3003d763cef2SBarry Smith } 3004d763cef2SBarry Smith 3005ac226902SBarry Smith /*@C 3006000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 30073f2090d5SJed Brown called once at the beginning of each time step. 3008000e7ae3SMatthew Knepley 3009c3339decSBarry Smith Logically Collective 3010000e7ae3SMatthew Knepley 3011000e7ae3SMatthew Knepley Input Parameters: 3012bcf0153eSBarry Smith + ts - The `TS` context obtained from `TSCreate()` 3013000e7ae3SMatthew Knepley - func - The function 3014000e7ae3SMatthew Knepley 301520f4b53cSBarry Smith Calling sequence of `func`: 301614d0ab18SJacob Faibussowitsch . ts - the `TS` context 3017000e7ae3SMatthew Knepley 3018000e7ae3SMatthew Knepley Level: intermediate 3019000e7ae3SMatthew Knepley 30201cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStage()`, `TSSetPostStage()`, `TSSetPostStep()`, `TSStep()`, `TSRestartStep()` 3021000e7ae3SMatthew Knepley @*/ 302214d0ab18SJacob Faibussowitsch PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS ts)) 3023d71ae5a4SJacob Faibussowitsch { 3024000e7ae3SMatthew Knepley PetscFunctionBegin; 30250700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3026ae60f76fSBarry Smith ts->prestep = func; 30273ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3028000e7ae3SMatthew Knepley } 3029000e7ae3SMatthew Knepley 303009ee8438SJed Brown /*@ 3031bcf0153eSBarry Smith TSPreStep - Runs the user-defined pre-step function provided with `TSSetPreStep()` 30323f2090d5SJed Brown 3033c3339decSBarry Smith Collective 30343f2090d5SJed Brown 30352fe279fdSBarry Smith Input Parameter: 3036bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 30373f2090d5SJed Brown 30383f2090d5SJed Brown Level: developer 30393f2090d5SJed Brown 3040bcf0153eSBarry Smith Note: 3041bcf0153eSBarry Smith `TSPreStep()` is typically used within time stepping implementations, 3042bcf0153eSBarry Smith so most users would not generally call this routine themselves. 3043bcf0153eSBarry Smith 30441cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStep()`, `TSPreStage()`, `TSPostStage()`, `TSPostStep()` 30453f2090d5SJed Brown @*/ 3046d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPreStep(TS ts) 3047d71ae5a4SJacob Faibussowitsch { 30483f2090d5SJed Brown PetscFunctionBegin; 30490700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3050ae60f76fSBarry Smith if (ts->prestep) { 30515efd42a4SStefano Zampini Vec U; 3052ef8d1ce0SJohann Rudi PetscObjectId idprev; 3053ef8d1ce0SJohann Rudi PetscBool sameObject; 30545efd42a4SStefano Zampini PetscObjectState sprev, spost; 30555efd42a4SStefano Zampini 30569566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts, &U)); 30579566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U, &idprev)); 30589566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &sprev)); 305925e27a38SBarry Smith PetscCallBack("TS callback preset", (*ts->prestep)(ts)); 30609566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts, &U)); 30619566063dSJacob Faibussowitsch PetscCall(PetscObjectCompareId((PetscObject)U, idprev, &sameObject)); 30629566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &spost)); 30639566063dSJacob Faibussowitsch if (!sameObject || sprev != spost) PetscCall(TSRestartStep(ts)); 3064312ce896SJed Brown } 30653ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 30663f2090d5SJed Brown } 30673f2090d5SJed Brown 3068b8123daeSJed Brown /*@C 3069b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 3070b8123daeSJed Brown called once at the beginning of each stage. 3071b8123daeSJed Brown 3072c3339decSBarry Smith Logically Collective 3073b8123daeSJed Brown 3074b8123daeSJed Brown Input Parameters: 3075bcf0153eSBarry Smith + ts - The `TS` context obtained from `TSCreate()` 3076b8123daeSJed Brown - func - The function 3077b8123daeSJed Brown 307820f4b53cSBarry Smith Calling sequence of `func`: 307914d0ab18SJacob Faibussowitsch + ts - the `TS` context 308014d0ab18SJacob Faibussowitsch - stagetime - the stage time 3081b8123daeSJed Brown 3082b8123daeSJed Brown Level: intermediate 3083b8123daeSJed Brown 3084b8123daeSJed Brown Note: 3085b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 3086bcf0153eSBarry Smith The time step number being computed can be queried using `TSGetStepNumber()` and the total size of the step being 3087bcf0153eSBarry Smith attempted can be obtained using `TSGetTimeStep()`. The time at the start of the step is available via `TSGetTime()`. 3088b8123daeSJed Brown 30891cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPostStage()`, `TSSetPreStep()`, `TSSetPostStep()`, `TSGetApplicationContext()` 3090b8123daeSJed Brown @*/ 309114d0ab18SJacob Faibussowitsch PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS ts, PetscReal stagetime)) 3092d71ae5a4SJacob Faibussowitsch { 3093b8123daeSJed Brown PetscFunctionBegin; 3094b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3095ae60f76fSBarry Smith ts->prestage = func; 30963ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3097b8123daeSJed Brown } 3098b8123daeSJed Brown 30999be3e283SDebojyoti Ghosh /*@C 3100bcf0153eSBarry Smith TSSetPostStage - Sets the general-purpose function, provided with `TSSetPostStep()`, 31019be3e283SDebojyoti Ghosh called once at the end of each stage. 31029be3e283SDebojyoti Ghosh 3103c3339decSBarry Smith Logically Collective 31049be3e283SDebojyoti Ghosh 31059be3e283SDebojyoti Ghosh Input Parameters: 3106bcf0153eSBarry Smith + ts - The `TS` context obtained from `TSCreate()` 31079be3e283SDebojyoti Ghosh - func - The function 31089be3e283SDebojyoti Ghosh 310920f4b53cSBarry Smith Calling sequence of `func`: 311014d0ab18SJacob Faibussowitsch + ts - the `TS` context 311114d0ab18SJacob Faibussowitsch . stagetime - the stage time 311214d0ab18SJacob Faibussowitsch . stageindex - the stage index 311314d0ab18SJacob Faibussowitsch - Y - Array of vectors (of size = total number of stages) with the stage solutions 31149be3e283SDebojyoti Ghosh 31159be3e283SDebojyoti Ghosh Level: intermediate 31169be3e283SDebojyoti Ghosh 31179be3e283SDebojyoti Ghosh Note: 31189be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 3119bcf0153eSBarry Smith The time step number being computed can be queried using `TSGetStepNumber()` and the total size of the step being 3120bcf0153eSBarry Smith attempted can be obtained using `TSGetTimeStep()`. The time at the start of the step is available via `TSGetTime()`. 31219be3e283SDebojyoti Ghosh 31221cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStage()`, `TSSetPreStep()`, `TSSetPostStep()`, `TSGetApplicationContext()` 31239be3e283SDebojyoti Ghosh @*/ 312414d0ab18SJacob Faibussowitsch PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)) 3125d71ae5a4SJacob Faibussowitsch { 31269be3e283SDebojyoti Ghosh PetscFunctionBegin; 31279be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 31289be3e283SDebojyoti Ghosh ts->poststage = func; 31293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 31309be3e283SDebojyoti Ghosh } 31319be3e283SDebojyoti Ghosh 3132c688d042SShri Abhyankar /*@C 3133c688d042SShri Abhyankar TSSetPostEvaluate - Sets the general-purpose function 3134c688d042SShri Abhyankar called once at the end of each step evaluation. 3135c688d042SShri Abhyankar 3136c3339decSBarry Smith Logically Collective 3137c688d042SShri Abhyankar 3138c688d042SShri Abhyankar Input Parameters: 3139bcf0153eSBarry Smith + ts - The `TS` context obtained from `TSCreate()` 3140c688d042SShri Abhyankar - func - The function 3141c688d042SShri Abhyankar 314220f4b53cSBarry Smith Calling sequence of `func`: 314314d0ab18SJacob Faibussowitsch . ts - the `TS` context 3144c688d042SShri Abhyankar 3145c688d042SShri Abhyankar Level: intermediate 3146c688d042SShri Abhyankar 3147c688d042SShri Abhyankar Note: 3148bcf0153eSBarry Smith Semantically, `TSSetPostEvaluate()` differs from `TSSetPostStep()` since the function it sets is called before event-handling 3149bcf0153eSBarry Smith thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, `TSPostStep()` 3150bcf0153eSBarry Smith may be passed a different solution, possibly changed by the event handler. `TSPostEvaluate()` is called after the next step 3151bcf0153eSBarry Smith solution is evaluated allowing to modify it, if need be. The solution can be obtained with `TSGetSolution()`, the time step 3152bcf0153eSBarry Smith with `TSGetTimeStep()`, and the time at the start of the step is available via `TSGetTime()` 3153c688d042SShri Abhyankar 31541cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStage()`, `TSSetPreStep()`, `TSSetPostStep()`, `TSGetApplicationContext()` 3155c688d042SShri Abhyankar @*/ 315614d0ab18SJacob Faibussowitsch PetscErrorCode TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS ts)) 3157d71ae5a4SJacob Faibussowitsch { 3158c688d042SShri Abhyankar PetscFunctionBegin; 3159c688d042SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3160c688d042SShri Abhyankar ts->postevaluate = func; 31613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3162c688d042SShri Abhyankar } 3163c688d042SShri Abhyankar 3164b8123daeSJed Brown /*@ 3165bcf0153eSBarry Smith TSPreStage - Runs the user-defined pre-stage function set using `TSSetPreStage()` 3166b8123daeSJed Brown 3167c3339decSBarry Smith Collective 3168b8123daeSJed Brown 3169b8123daeSJed Brown Input Parameters: 317014d0ab18SJacob Faibussowitsch + ts - The `TS` context obtained from `TSCreate()` 317114d0ab18SJacob Faibussowitsch - stagetime - The absolute time of the current stage 3172b8123daeSJed Brown 3173b8123daeSJed Brown Level: developer 3174b8123daeSJed Brown 3175bcf0153eSBarry Smith Note: 3176bcf0153eSBarry Smith `TSPreStage()` is typically used within time stepping implementations, 3177bcf0153eSBarry Smith most users would not generally call this routine themselves. 3178bcf0153eSBarry Smith 31791cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSPostStage()`, `TSSetPreStep()`, `TSPreStep()`, `TSPostStep()` 3180b8123daeSJed Brown @*/ 3181d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 3182d71ae5a4SJacob Faibussowitsch { 3183b8123daeSJed Brown PetscFunctionBegin; 3184b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 31851e66621cSBarry Smith if (ts->prestage) PetscCallBack("TS callback prestage", (*ts->prestage)(ts, stagetime)); 31863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3187b8123daeSJed Brown } 3188b8123daeSJed Brown 31899be3e283SDebojyoti Ghosh /*@ 3190bcf0153eSBarry Smith TSPostStage - Runs the user-defined post-stage function set using `TSSetPostStage()` 31919be3e283SDebojyoti Ghosh 3192c3339decSBarry Smith Collective 31939be3e283SDebojyoti Ghosh 31949be3e283SDebojyoti Ghosh Input Parameters: 319514d0ab18SJacob Faibussowitsch + ts - The `TS` context obtained from `TSCreate()` 319614d0ab18SJacob Faibussowitsch . stagetime - The absolute time of the current stage 319714d0ab18SJacob Faibussowitsch . stageindex - Stage number 319814d0ab18SJacob Faibussowitsch - Y - Array of vectors (of size = total number of stages) with the stage solutions 31999be3e283SDebojyoti Ghosh 32009be3e283SDebojyoti Ghosh Level: developer 32019be3e283SDebojyoti Ghosh 3202bcf0153eSBarry Smith Note: 3203bcf0153eSBarry Smith `TSPostStage()` is typically used within time stepping implementations, 3204bcf0153eSBarry Smith most users would not generally call this routine themselves. 3205bcf0153eSBarry Smith 32061cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSPreStage()`, `TSSetPreStep()`, `TSPreStep()`, `TSPostStep()` 32079be3e283SDebojyoti Ghosh @*/ 3208d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 3209d71ae5a4SJacob Faibussowitsch { 32109be3e283SDebojyoti Ghosh PetscFunctionBegin; 32119be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 32121e66621cSBarry Smith if (ts->poststage) PetscCallBack("TS callback poststage", (*ts->poststage)(ts, stagetime, stageindex, Y)); 32133ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 32149be3e283SDebojyoti Ghosh } 32159be3e283SDebojyoti Ghosh 3216c688d042SShri Abhyankar /*@ 3217bcf0153eSBarry Smith TSPostEvaluate - Runs the user-defined post-evaluate function set using `TSSetPostEvaluate()` 3218c688d042SShri Abhyankar 3219c3339decSBarry Smith Collective 3220c688d042SShri Abhyankar 32212fe279fdSBarry Smith Input Parameter: 3222bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 3223c688d042SShri Abhyankar 3224c688d042SShri Abhyankar Level: developer 3225c688d042SShri Abhyankar 3226bcf0153eSBarry Smith Note: 3227bcf0153eSBarry Smith `TSPostEvaluate()` is typically used within time stepping implementations, 3228bcf0153eSBarry Smith most users would not generally call this routine themselves. 3229bcf0153eSBarry Smith 32301cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPostEvaluate()`, `TSSetPreStep()`, `TSPreStep()`, `TSPostStep()` 3231c688d042SShri Abhyankar @*/ 3232d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPostEvaluate(TS ts) 3233d71ae5a4SJacob Faibussowitsch { 3234c688d042SShri Abhyankar PetscFunctionBegin; 3235c688d042SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3236c688d042SShri Abhyankar if (ts->postevaluate) { 3237dcb233daSLisandro Dalcin Vec U; 3238dcb233daSLisandro Dalcin PetscObjectState sprev, spost; 3239dcb233daSLisandro Dalcin 32409566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts, &U)); 32419566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &sprev)); 324225e27a38SBarry Smith PetscCallBack("TS callback postevaluate", (*ts->postevaluate)(ts)); 32439566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &spost)); 32449566063dSJacob Faibussowitsch if (sprev != spost) PetscCall(TSRestartStep(ts)); 3245c688d042SShri Abhyankar } 32463ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3247c688d042SShri Abhyankar } 3248c688d042SShri Abhyankar 3249ac226902SBarry Smith /*@C 3250000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 32513f2090d5SJed Brown called once at the end of each time step. 3252000e7ae3SMatthew Knepley 3253c3339decSBarry Smith Logically Collective 3254000e7ae3SMatthew Knepley 3255000e7ae3SMatthew Knepley Input Parameters: 3256bcf0153eSBarry Smith + ts - The `TS` context obtained from `TSCreate()` 3257000e7ae3SMatthew Knepley - func - The function 3258000e7ae3SMatthew Knepley 325920f4b53cSBarry Smith Calling sequence of `func`: 326014d0ab18SJacob Faibussowitsch . ts - the `TS` context 3261000e7ae3SMatthew Knepley 3262000e7ae3SMatthew Knepley Level: intermediate 3263000e7ae3SMatthew Knepley 3264bcf0153eSBarry Smith Note: 3265195e9b02SBarry Smith The function set by `TSSetPostStep()` is called after each successful step. The solution vector 3266bcf0153eSBarry Smith obtained by `TSGetSolution()` may be different than that computed at the step end if the event handler 3267bcf0153eSBarry Smith locates an event and `TSPostEvent()` modifies it. Use `TSSetPostEvaluate()` if an unmodified solution is needed instead. 3268bcf0153eSBarry Smith 32691cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostEvaluate()`, `TSGetTimeStep()`, `TSGetStepNumber()`, `TSGetTime()`, `TSRestartStep()` 3270000e7ae3SMatthew Knepley @*/ 327114d0ab18SJacob Faibussowitsch PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS ts)) 3272d71ae5a4SJacob Faibussowitsch { 3273000e7ae3SMatthew Knepley PetscFunctionBegin; 32740700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3275ae60f76fSBarry Smith ts->poststep = func; 32763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3277000e7ae3SMatthew Knepley } 3278000e7ae3SMatthew Knepley 327909ee8438SJed Brown /*@ 3280195e9b02SBarry Smith TSPostStep - Runs the user-defined post-step function that was set with `TSSetPostStep()` 32813f2090d5SJed Brown 3282c3339decSBarry Smith Collective 32833f2090d5SJed Brown 32842fe279fdSBarry Smith Input Parameter: 3285bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 32863f2090d5SJed Brown 3287bcf0153eSBarry Smith Note: 3288bcf0153eSBarry Smith `TSPostStep()` is typically used within time stepping implementations, 32893f2090d5SJed Brown so most users would not generally call this routine themselves. 32903f2090d5SJed Brown 32913f2090d5SJed Brown Level: developer 32923f2090d5SJed Brown 32931cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostEvaluate()`, `TSGetTimeStep()`, `TSGetStepNumber()`, `TSGetTime()`, `TSSetPotsStep()` 32943f2090d5SJed Brown @*/ 3295d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPostStep(TS ts) 3296d71ae5a4SJacob Faibussowitsch { 32973f2090d5SJed Brown PetscFunctionBegin; 32980700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3299ae60f76fSBarry Smith if (ts->poststep) { 33005efd42a4SStefano Zampini Vec U; 3301ef8d1ce0SJohann Rudi PetscObjectId idprev; 3302ef8d1ce0SJohann Rudi PetscBool sameObject; 33035efd42a4SStefano Zampini PetscObjectState sprev, spost; 33045efd42a4SStefano Zampini 33059566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts, &U)); 33069566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U, &idprev)); 33079566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &sprev)); 330825e27a38SBarry Smith PetscCallBack("TS callback poststep", (*ts->poststep)(ts)); 33099566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts, &U)); 33109566063dSJacob Faibussowitsch PetscCall(PetscObjectCompareId((PetscObject)U, idprev, &sameObject)); 33119566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &spost)); 33129566063dSJacob Faibussowitsch if (!sameObject || sprev != spost) PetscCall(TSRestartStep(ts)); 331372ac3e02SJed Brown } 33143ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 33153f2090d5SJed Brown } 33163f2090d5SJed Brown 3317cd652676SJed Brown /*@ 3318cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3319cd652676SJed Brown 3320c3339decSBarry Smith Collective 3321cd652676SJed Brown 33224165533cSJose E. Roman Input Parameters: 3323cd652676SJed Brown + ts - time stepping context 3324cd652676SJed Brown - t - time to interpolate to 3325cd652676SJed Brown 33264165533cSJose E. Roman Output Parameter: 33270910c330SBarry Smith . U - state at given time 3328cd652676SJed Brown 3329cd652676SJed Brown Level: intermediate 3330cd652676SJed Brown 3331b43aa488SJacob Faibussowitsch Developer Notes: 3332bcf0153eSBarry Smith `TSInterpolate()` and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3333cd652676SJed Brown 33341cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetExactFinalTime()`, `TSSolve()` 3335cd652676SJed Brown @*/ 3336d71ae5a4SJacob Faibussowitsch PetscErrorCode TSInterpolate(TS ts, PetscReal t, Vec U) 3337d71ae5a4SJacob Faibussowitsch { 3338cd652676SJed Brown PetscFunctionBegin; 3339cd652676SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3340b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 334163a3b9bcSJacob 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); 3342dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, interpolate, t, U); 33433ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3344cd652676SJed Brown } 3345cd652676SJed Brown 3346d763cef2SBarry Smith /*@ 33476d9e5789SSean Farley TSStep - Steps one time step 3348d763cef2SBarry Smith 3349c3339decSBarry Smith Collective 3350d763cef2SBarry Smith 3351d763cef2SBarry Smith Input Parameter: 3352bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 3353d763cef2SBarry Smith 335427829d71SBarry Smith Level: developer 3355d763cef2SBarry Smith 3356b8123daeSJed Brown Notes: 3357bcf0153eSBarry Smith The public interface for the ODE/DAE solvers is `TSSolve()`, you should almost for sure be using that routine and not this routine. 335827829d71SBarry Smith 3359bcf0153eSBarry Smith The hook set using `TSSetPreStep()` is called before each attempt to take the step. In general, the time step size may 3360b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3361b8123daeSJed Brown 3362bcf0153eSBarry Smith This may over-step the final time provided in `TSSetMaxTime()` depending on the time-step used. `TSSolve()` interpolates to exactly the 3363bcf0153eSBarry Smith time provided in `TSSetMaxTime()`. One can use `TSInterpolate()` to determine an interpolated solution within the final timestep. 336425cb2221SBarry Smith 33651cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSSetUp()`, `TSDestroy()`, `TSSolve()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostStage()`, `TSInterpolate()` 3366d763cef2SBarry Smith @*/ 3367d71ae5a4SJacob Faibussowitsch PetscErrorCode TSStep(TS ts) 3368d71ae5a4SJacob Faibussowitsch { 3369fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3370be5899b3SLisandro Dalcin PetscReal ptime; 3371d763cef2SBarry Smith 3372d763cef2SBarry Smith PetscFunctionBegin; 33730700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3374d0609cedSBarry Smith PetscCall(PetscCitationsRegister("@article{tspaper,\n" 3375fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3376f1d62c27SHong Zhang " author = {Abhyankar, Shrirang and Brown, Jed and Constantinescu, Emil and Ghosh, Debojyoti and Smith, Barry F. and Zhang, Hong},\n" 3377f1d62c27SHong Zhang " journal = {arXiv e-preprints},\n" 3378f1d62c27SHong Zhang " eprint = {1806.01437},\n" 3379f1d62c27SHong Zhang " archivePrefix = {arXiv},\n" 33809371c9d4SSatish Balay " year = {2018}\n}\n", 33819371c9d4SSatish Balay &cite)); 33829566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts)); 33839566063dSJacob Faibussowitsch PetscCall(TSTrajectorySetUp(ts->trajectory, ts)); 3384ca4445c7SIlya Fursov if (ts->tspan) 3385ca4445c7SIlya Fursov ts->tspan->worktol = 0; /* In each step of TSSolve() 'tspan->worktol' will be meaningfully defined (later) only once: 3386ca4445c7SIlya Fursov in TSAdaptChoose() or TSEvent_dt_cap(), and then reused till the end of the step */ 3387d405a339SMatthew Knepley 33883c633725SBarry Smith PetscCheck(ts->max_time < PETSC_MAX_REAL || ts->max_steps != PETSC_MAX_INT, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "You must call TSSetMaxTime() or TSSetMaxSteps(), or use -ts_max_time <time> or -ts_max_steps <steps>"); 33893c633725SBarry 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()"); 33903c633725SBarry 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"); 3391a6772fa2SLisandro Dalcin 3392c61711c8SStefano Zampini if (!ts->vec_sol0) PetscCall(VecDuplicate(ts->vec_sol, &ts->vec_sol0)); 3393c61711c8SStefano Zampini PetscCall(VecCopy(ts->vec_sol, ts->vec_sol0)); 3394c61711c8SStefano Zampini ts->time_step0 = ts->time_step; 3395c61711c8SStefano Zampini 3396be5899b3SLisandro Dalcin if (!ts->steps) ts->ptime_prev = ts->ptime; 33979371c9d4SSatish Balay ptime = ts->ptime; 3398c61711c8SStefano Zampini 33999371c9d4SSatish Balay ts->ptime_prev_rollback = ts->ptime_prev; 34002808aa04SLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3401fc8dbba5SLisandro Dalcin 34029566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_Step, ts, 0, 0, 0)); 3403dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, step); 34049566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_Step, ts, 0, 0, 0)); 3405fc8dbba5SLisandro Dalcin 3406fc8dbba5SLisandro Dalcin if (ts->reason >= 0) { 3407be5899b3SLisandro Dalcin ts->ptime_prev = ptime; 34082808aa04SLisandro Dalcin ts->steps++; 3409be5899b3SLisandro Dalcin ts->steprollback = PETSC_FALSE; 341028d5b5d6SLisandro Dalcin ts->steprestart = PETSC_FALSE; 3411d2daff3dSHong Zhang } 3412fc8dbba5SLisandro Dalcin 34135c9bbc89SJed Brown if (ts->reason < 0 && ts->errorifstepfailed) { 34145c9bbc89SJed Brown PetscCall(TSMonitorCancel(ts)); 341509cb0f53SBarry 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]); 34165c9bbc89SJed Brown SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_NOT_CONVERGED, "TSStep has failed due to %s", TSConvergedReasons[ts->reason]); 34175c9bbc89SJed Brown } 34183ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 341908c7845fSBarry Smith } 342008c7845fSBarry Smith 342108c7845fSBarry Smith /*@ 34227cbde773SLisandro Dalcin TSEvaluateWLTE - Evaluate the weighted local truncation error norm 34237cbde773SLisandro Dalcin at the end of a time step with a given order of accuracy. 34247cbde773SLisandro Dalcin 3425c3339decSBarry Smith Collective 34267cbde773SLisandro Dalcin 34274165533cSJose E. Roman Input Parameters: 34287cbde773SLisandro Dalcin + ts - time stepping context 3429bcf0153eSBarry Smith - wnormtype - norm type, either `NORM_2` or `NORM_INFINITY` 34307cbde773SLisandro Dalcin 343197bb3fdcSJose E. Roman Input/Output Parameter: 3432bcf0153eSBarry Smith . order - optional, desired order for the error evaluation or `PETSC_DECIDE`; 343397bb3fdcSJose E. Roman on output, the actual order of the error evaluation 343497bb3fdcSJose E. Roman 343597bb3fdcSJose E. Roman Output Parameter: 343697bb3fdcSJose E. Roman . wlte - the weighted local truncation error norm 34377cbde773SLisandro Dalcin 34387cbde773SLisandro Dalcin Level: advanced 34397cbde773SLisandro Dalcin 3440bcf0153eSBarry Smith Note: 34417cbde773SLisandro Dalcin If the timestepper cannot evaluate the error in a particular step 34427cbde773SLisandro Dalcin (eg. in the first step or restart steps after event handling), 34437cbde773SLisandro Dalcin this routine returns wlte=-1.0 . 34447cbde773SLisandro Dalcin 34451cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSStep()`, `TSAdapt`, `TSErrorWeightedNorm()` 34467cbde773SLisandro Dalcin @*/ 3447d71ae5a4SJacob Faibussowitsch PetscErrorCode TSEvaluateWLTE(TS ts, NormType wnormtype, PetscInt *order, PetscReal *wlte) 3448d71ae5a4SJacob Faibussowitsch { 34497cbde773SLisandro Dalcin PetscFunctionBegin; 34507cbde773SLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 34517cbde773SLisandro Dalcin PetscValidType(ts, 1); 3452064a246eSJacob Faibussowitsch PetscValidLogicalCollectiveEnum(ts, wnormtype, 2); 34534f572ea9SToby Isaac if (order) PetscAssertPointer(order, 3); 34547cbde773SLisandro Dalcin if (order) PetscValidLogicalCollectiveInt(ts, *order, 3); 34554f572ea9SToby Isaac PetscAssertPointer(wlte, 4); 34563c633725SBarry Smith PetscCheck(wnormtype == NORM_2 || wnormtype == NORM_INFINITY, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "No support for norm type %s", NormTypes[wnormtype]); 3457dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, evaluatewlte, wnormtype, order, wlte); 34583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 34597cbde773SLisandro Dalcin } 34607cbde773SLisandro Dalcin 346105175c85SJed Brown /*@ 346205175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 346305175c85SJed Brown 3464c3339decSBarry Smith Collective 346505175c85SJed Brown 34664165533cSJose E. Roman Input Parameters: 34671c3436cfSJed Brown + ts - time stepping context 34681c3436cfSJed Brown . order - desired order of accuracy 3469195e9b02SBarry Smith - done - whether the step was evaluated at this order (pass `NULL` to generate an error if not available) 347005175c85SJed Brown 34714165533cSJose E. Roman Output Parameter: 34720910c330SBarry Smith . U - state at the end of the current step 347305175c85SJed Brown 347405175c85SJed Brown Level: advanced 347505175c85SJed Brown 3476108c343cSJed Brown Notes: 3477108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3478108c343cSJed Brown 3479bcf0153eSBarry 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. 3480bcf0153eSBarry Smith 34811cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSStep()`, `TSAdapt` 348205175c85SJed Brown @*/ 3483d71ae5a4SJacob Faibussowitsch PetscErrorCode TSEvaluateStep(TS ts, PetscInt order, Vec U, PetscBool *done) 3484d71ae5a4SJacob Faibussowitsch { 348505175c85SJed Brown PetscFunctionBegin; 348605175c85SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 348705175c85SJed Brown PetscValidType(ts, 1); 34880910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 3489dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, evaluatestep, order, U, done); 34903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 349105175c85SJed Brown } 349205175c85SJed Brown 3493aad739acSMatthew G. Knepley /*@C 34942e61be88SMatthew G. Knepley TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping. 3495aad739acSMatthew G. Knepley 3496aad739acSMatthew G. Knepley Not collective 3497aad739acSMatthew G. Knepley 34984165533cSJose E. Roman Input Parameter: 3499aad739acSMatthew G. Knepley . ts - time stepping context 3500aad739acSMatthew G. Knepley 35014165533cSJose E. Roman Output Parameter: 3502b43aa488SJacob Faibussowitsch . initCondition - The function which computes an initial condition 3503aad739acSMatthew G. Knepley 350420f4b53cSBarry Smith Calling sequence of `initCondition`: 350520f4b53cSBarry Smith + ts - The timestepping context 350620f4b53cSBarry Smith - u - The input vector in which the initial condition is stored 3507bcf0153eSBarry Smith 3508aad739acSMatthew G. Knepley Level: advanced 3509aad739acSMatthew G. Knepley 35101cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetComputeInitialCondition()`, `TSComputeInitialCondition()` 3511aad739acSMatthew G. Knepley @*/ 351214d0ab18SJacob Faibussowitsch PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS ts, Vec u)) 3513d71ae5a4SJacob Faibussowitsch { 3514aad739acSMatthew G. Knepley PetscFunctionBegin; 3515aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 35164f572ea9SToby Isaac PetscAssertPointer(initCondition, 2); 35172e61be88SMatthew G. Knepley *initCondition = ts->ops->initcondition; 35183ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3519aad739acSMatthew G. Knepley } 3520aad739acSMatthew G. Knepley 3521aad739acSMatthew G. Knepley /*@C 35222e61be88SMatthew G. Knepley TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping. 3523aad739acSMatthew G. Knepley 3524c3339decSBarry Smith Logically collective 3525aad739acSMatthew G. Knepley 35264165533cSJose E. Roman Input Parameters: 3527aad739acSMatthew G. Knepley + ts - time stepping context 35282e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition 3529aad739acSMatthew G. Knepley 353020f4b53cSBarry Smith Calling sequence of `initCondition`: 3531a96d6ef6SBarry Smith + ts - The timestepping context 353214d0ab18SJacob Faibussowitsch - e - The input vector in which the initial condition is to be stored 3533aad739acSMatthew G. Knepley 3534bcf0153eSBarry Smith Level: advanced 3535bcf0153eSBarry Smith 35361cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetComputeInitialCondition()`, `TSComputeInitialCondition()` 3537aad739acSMatthew G. Knepley @*/ 353814d0ab18SJacob Faibussowitsch PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS ts, Vec e)) 3539d71ae5a4SJacob Faibussowitsch { 3540aad739acSMatthew G. Knepley PetscFunctionBegin; 3541aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 35422e61be88SMatthew G. Knepley PetscValidFunction(initCondition, 2); 35432e61be88SMatthew G. Knepley ts->ops->initcondition = initCondition; 35443ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3545aad739acSMatthew G. Knepley } 3546aad739acSMatthew G. Knepley 3547aad739acSMatthew G. Knepley /*@ 3548bcf0153eSBarry Smith TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set with `TSSetComputeInitialCondition()` 3549aad739acSMatthew G. Knepley 3550c3339decSBarry Smith Collective 3551aad739acSMatthew G. Knepley 35524165533cSJose E. Roman Input Parameters: 3553aad739acSMatthew G. Knepley + ts - time stepping context 3554bcf0153eSBarry Smith - u - The `Vec` to store the condition in which will be used in `TSSolve()` 3555aad739acSMatthew G. Knepley 3556aad739acSMatthew G. Knepley Level: advanced 3557aad739acSMatthew G. Knepley 35581cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetComputeInitialCondition()`, `TSSetComputeInitialCondition()`, `TSSolve()` 3559aad739acSMatthew G. Knepley @*/ 3560d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeInitialCondition(TS ts, Vec u) 3561d71ae5a4SJacob Faibussowitsch { 3562aad739acSMatthew G. Knepley PetscFunctionBegin; 3563aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3564aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3565dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, initcondition, u); 35663ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3567aad739acSMatthew G. Knepley } 3568aad739acSMatthew G. Knepley 3569aad739acSMatthew G. Knepley /*@C 3570aad739acSMatthew G. Knepley TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping. 3571aad739acSMatthew G. Knepley 3572aad739acSMatthew G. Knepley Not collective 3573aad739acSMatthew G. Knepley 35744165533cSJose E. Roman Input Parameter: 3575aad739acSMatthew G. Knepley . ts - time stepping context 3576aad739acSMatthew G. Knepley 35774165533cSJose E. Roman Output Parameter: 3578aad739acSMatthew G. Knepley . exactError - The function which computes the solution error 3579aad739acSMatthew G. Knepley 358020f4b53cSBarry Smith Calling sequence of `exactError`: 3581a96d6ef6SBarry Smith + ts - The timestepping context 3582a96d6ef6SBarry Smith . u - The approximate solution vector 358320f4b53cSBarry Smith - e - The vector in which the error is stored 3584aad739acSMatthew G. Knepley 3585bcf0153eSBarry Smith Level: advanced 3586bcf0153eSBarry Smith 358742747ad1SJacob Faibussowitsch .seealso: [](ch_ts), `TS`, `TSComputeExactError()` 3588aad739acSMatthew G. Knepley @*/ 358914d0ab18SJacob Faibussowitsch PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS ts, Vec u, Vec e)) 3590d71ae5a4SJacob Faibussowitsch { 3591aad739acSMatthew G. Knepley PetscFunctionBegin; 3592aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 35934f572ea9SToby Isaac PetscAssertPointer(exactError, 2); 3594aad739acSMatthew G. Knepley *exactError = ts->ops->exacterror; 35953ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3596aad739acSMatthew G. Knepley } 3597aad739acSMatthew G. Knepley 3598aad739acSMatthew G. Knepley /*@C 3599aad739acSMatthew G. Knepley TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping. 3600aad739acSMatthew G. Knepley 3601c3339decSBarry Smith Logically collective 3602aad739acSMatthew G. Knepley 36034165533cSJose E. Roman Input Parameters: 3604aad739acSMatthew G. Knepley + ts - time stepping context 3605aad739acSMatthew G. Knepley - exactError - The function which computes the solution error 3606aad739acSMatthew G. Knepley 360720f4b53cSBarry Smith Calling sequence of `exactError`: 3608a96d6ef6SBarry Smith + ts - The timestepping context 3609a96d6ef6SBarry Smith . u - The approximate solution vector 361020f4b53cSBarry Smith - e - The vector in which the error is stored 3611aad739acSMatthew G. Knepley 3612bcf0153eSBarry Smith Level: advanced 3613bcf0153eSBarry Smith 36141cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetComputeExactError()`, `TSComputeExactError()` 3615aad739acSMatthew G. Knepley @*/ 361614d0ab18SJacob Faibussowitsch PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS ts, Vec u, Vec e)) 3617d71ae5a4SJacob Faibussowitsch { 3618aad739acSMatthew G. Knepley PetscFunctionBegin; 3619aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3620f907fdbfSMatthew G. Knepley PetscValidFunction(exactError, 2); 3621aad739acSMatthew G. Knepley ts->ops->exacterror = exactError; 36223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3623aad739acSMatthew G. Knepley } 3624aad739acSMatthew G. Knepley 3625aad739acSMatthew G. Knepley /*@ 3626bcf0153eSBarry Smith TSComputeExactError - Compute the solution error for the timestepping using the function previously set with `TSSetComputeExactError()` 3627aad739acSMatthew G. Knepley 3628c3339decSBarry Smith Collective 3629aad739acSMatthew G. Knepley 36304165533cSJose E. Roman Input Parameters: 3631aad739acSMatthew G. Knepley + ts - time stepping context 3632aad739acSMatthew G. Knepley . u - The approximate solution 3633bcf0153eSBarry Smith - e - The `Vec` used to store the error 3634aad739acSMatthew G. Knepley 3635aad739acSMatthew G. Knepley Level: advanced 3636aad739acSMatthew G. Knepley 36371cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetComputeInitialCondition()`, `TSSetComputeInitialCondition()`, `TSSolve()` 3638aad739acSMatthew G. Knepley @*/ 3639d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e) 3640d71ae5a4SJacob Faibussowitsch { 3641aad739acSMatthew G. Knepley PetscFunctionBegin; 3642aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3643aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3644aad739acSMatthew G. Knepley PetscValidHeaderSpecific(e, VEC_CLASSID, 3); 3645dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, exacterror, u, e); 36463ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3647aad739acSMatthew G. Knepley } 3648aad739acSMatthew G. Knepley 36496bd3a4fdSStefano Zampini /*@C 36506bd3a4fdSStefano Zampini TSSetResize - Sets the resize callbacks. 36516bd3a4fdSStefano Zampini 36526bd3a4fdSStefano Zampini Logically Collective 36536bd3a4fdSStefano Zampini 36546bd3a4fdSStefano Zampini Input Parameters: 36556bd3a4fdSStefano Zampini + ts - The `TS` context obtained from `TSCreate()` 3656ecc87898SStefano Zampini . rollback - Whether a resize will restart the step 36576bd3a4fdSStefano Zampini . setup - The setup function 365814d0ab18SJacob Faibussowitsch . transfer - The transfer function 365914d0ab18SJacob Faibussowitsch - ctx - [optional] The user-defined context 36606bd3a4fdSStefano Zampini 36616bd3a4fdSStefano Zampini Calling sequence of `setup`: 36628847d985SBarry Smith + ts - the `TS` context 36636bd3a4fdSStefano Zampini . step - the current step 36646bd3a4fdSStefano Zampini . time - the current time 36656bd3a4fdSStefano Zampini . state - the current vector of state 36666bd3a4fdSStefano Zampini . resize - (output parameter) `PETSC_TRUE` if need resizing, `PETSC_FALSE` otherwise 36676bd3a4fdSStefano Zampini - ctx - user defined context 36686bd3a4fdSStefano Zampini 36696bd3a4fdSStefano Zampini Calling sequence of `transfer`: 36708847d985SBarry Smith + ts - the `TS` context 36716bd3a4fdSStefano Zampini . nv - the number of vectors to be transferred 36726bd3a4fdSStefano Zampini . vecsin - array of vectors to be transferred 36736bd3a4fdSStefano Zampini . vecsout - array of transferred vectors 36746bd3a4fdSStefano Zampini - ctx - user defined context 36756bd3a4fdSStefano Zampini 36766bd3a4fdSStefano Zampini Notes: 3677ecc87898SStefano Zampini The `setup` function is called inside `TSSolve()` after `TSEventHandler()` or after `TSPostStep()` 3678ecc87898SStefano Zampini depending on the `rollback` value: if `rollback` is true, then these callbacks behave as error indicators 3679ecc87898SStefano Zampini and will flag the need to remesh and restart the current step. Otherwise, they will simply flag the solver 3680ecc87898SStefano Zampini that the size of the discrete problem has changed. 3681ecc87898SStefano Zampini In both cases, the solver will collect the needed vectors that will be 3682ecc87898SStefano Zampini transferred from the old to the new sizes using the `transfer` callback. These vectors will include the 3683ecc87898SStefano Zampini current solution vector, and other vectors needed by the specific solver used. 36846bd3a4fdSStefano Zampini For example, `TSBDF` uses previous solutions vectors to solve for the next time step. 36856bd3a4fdSStefano Zampini Other application specific objects associated with the solver, i.e. Jacobian matrices and `DM`, 36866bd3a4fdSStefano Zampini will be automatically reset if the sizes are changed and they must be specified again by the user 36876bd3a4fdSStefano Zampini inside the `transfer` function. 36886bd3a4fdSStefano Zampini The input and output arrays passed to `transfer` are allocated by PETSc. 36896bd3a4fdSStefano Zampini Vectors in `vecsout` must be created by the user. 36906bd3a4fdSStefano Zampini Ownership of vectors in `vecsout` is transferred to PETSc. 36916bd3a4fdSStefano Zampini 36926bd3a4fdSStefano Zampini Level: advanced 36936bd3a4fdSStefano Zampini 36946bd3a4fdSStefano Zampini .seealso: [](ch_ts), `TS`, `TSSetDM()`, `TSSetIJacobian()`, `TSSetRHSJacobian()` 36956bd3a4fdSStefano Zampini @*/ 3696ecc87898SStefano 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) 36976bd3a4fdSStefano Zampini { 36986bd3a4fdSStefano Zampini PetscFunctionBegin; 36996bd3a4fdSStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3700ecc87898SStefano Zampini ts->resizerollback = rollback; 37016bd3a4fdSStefano Zampini ts->resizesetup = setup; 37026bd3a4fdSStefano Zampini ts->resizetransfer = transfer; 37036bd3a4fdSStefano Zampini ts->resizectx = ctx; 37046bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 37056bd3a4fdSStefano Zampini } 37066bd3a4fdSStefano Zampini 370714d0ab18SJacob Faibussowitsch /* 37086bd3a4fdSStefano Zampini TSResizeRegisterOrRetrieve - Register or import vectors transferred with `TSResize()`. 37096bd3a4fdSStefano Zampini 37106bd3a4fdSStefano Zampini Collective 37116bd3a4fdSStefano Zampini 37126bd3a4fdSStefano Zampini Input Parameters: 37136bd3a4fdSStefano Zampini + ts - The `TS` context obtained from `TSCreate()` 37146bd3a4fdSStefano 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. 37156bd3a4fdSStefano Zampini 37166bd3a4fdSStefano Zampini Level: developer 37176bd3a4fdSStefano Zampini 37186bd3a4fdSStefano Zampini Note: 37196bd3a4fdSStefano Zampini `TSResizeRegisterOrRetrieve()` is declared PETSC_INTERN since it is 37206bd3a4fdSStefano Zampini used within time stepping implementations, 37216bd3a4fdSStefano Zampini so most users would not generally call this routine themselves. 37226bd3a4fdSStefano Zampini 37236bd3a4fdSStefano Zampini .seealso: [](ch_ts), `TS`, `TSSetResize()` 37246bd3a4fdSStefano Zampini @*/ 372514d0ab18SJacob Faibussowitsch static PetscErrorCode TSResizeRegisterOrRetrieve(TS ts, PetscBool flg) 37266bd3a4fdSStefano Zampini { 37276bd3a4fdSStefano Zampini PetscFunctionBegin; 37286bd3a4fdSStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 37296bd3a4fdSStefano Zampini PetscTryTypeMethod(ts, resizeregister, flg); 37306bd3a4fdSStefano Zampini /* PetscTryTypeMethod(adapt, resizeregister, flg); */ 37316bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 37326bd3a4fdSStefano Zampini } 37336bd3a4fdSStefano Zampini 373414d0ab18SJacob Faibussowitsch static PetscErrorCode TSResizeReset(TS ts) 37356bd3a4fdSStefano Zampini { 37366bd3a4fdSStefano Zampini PetscFunctionBegin; 37376bd3a4fdSStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 37386bd3a4fdSStefano Zampini PetscCall(PetscObjectListDestroy(&ts->resizetransferobjs)); 37396bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 37406bd3a4fdSStefano Zampini } 37416bd3a4fdSStefano Zampini 37426bd3a4fdSStefano Zampini static PetscErrorCode TSResizeTransferVecs(TS ts, PetscInt cnt, Vec vecsin[], Vec vecsout[]) 37436bd3a4fdSStefano Zampini { 37446bd3a4fdSStefano Zampini PetscFunctionBegin; 37456bd3a4fdSStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 37466bd3a4fdSStefano Zampini PetscValidLogicalCollectiveInt(ts, cnt, 2); 37476bd3a4fdSStefano Zampini for (PetscInt i = 0; i < cnt; i++) PetscCall(VecLockReadPush(vecsin[i])); 37486bd3a4fdSStefano Zampini if (ts->resizetransfer) { 37496bd3a4fdSStefano Zampini PetscCall(PetscInfo(ts, "Transferring %" PetscInt_FMT " vectors\n", cnt)); 37506bd3a4fdSStefano Zampini PetscCallBack("TS callback resize transfer", (*ts->resizetransfer)(ts, cnt, vecsin, vecsout, ts->resizectx)); 37516bd3a4fdSStefano Zampini } 37526bd3a4fdSStefano Zampini for (PetscInt i = 0; i < cnt; i++) PetscCall(VecLockReadPop(vecsin[i])); 37536bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 37546bd3a4fdSStefano Zampini } 37556bd3a4fdSStefano Zampini 37566bd3a4fdSStefano Zampini /*@C 37576bd3a4fdSStefano Zampini TSResizeRegisterVec - Register a vector to be transferred with `TSResize()`. 37586bd3a4fdSStefano Zampini 37596bd3a4fdSStefano Zampini Collective 37606bd3a4fdSStefano Zampini 37616bd3a4fdSStefano Zampini Input Parameters: 37626bd3a4fdSStefano Zampini + ts - The `TS` context obtained from `TSCreate()` 3763baca6076SPierre Jolivet . name - A string identifying the vector 37646bd3a4fdSStefano Zampini - vec - The vector 37656bd3a4fdSStefano Zampini 37666bd3a4fdSStefano Zampini Level: developer 37676bd3a4fdSStefano Zampini 37686bd3a4fdSStefano Zampini Note: 37696bd3a4fdSStefano Zampini `TSResizeRegisterVec()` is typically used within time stepping implementations, 37706bd3a4fdSStefano Zampini so most users would not generally call this routine themselves. 37716bd3a4fdSStefano Zampini 37726bd3a4fdSStefano Zampini .seealso: [](ch_ts), `TS`, `TSSetResize()`, `TSResize()`, `TSResizeRetrieveVec()` 37736bd3a4fdSStefano Zampini @*/ 3774cc4c1da9SBarry Smith PetscErrorCode TSResizeRegisterVec(TS ts, const char name[], Vec vec) 37756bd3a4fdSStefano Zampini { 37766bd3a4fdSStefano Zampini PetscFunctionBegin; 37776bd3a4fdSStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 37784f572ea9SToby Isaac PetscAssertPointer(name, 2); 37796bd3a4fdSStefano Zampini if (vec) PetscValidHeaderSpecific(vec, VEC_CLASSID, 3); 37806bd3a4fdSStefano Zampini PetscCall(PetscObjectListAdd(&ts->resizetransferobjs, name, (PetscObject)vec)); 37816bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 37826bd3a4fdSStefano Zampini } 37836bd3a4fdSStefano Zampini 37846bd3a4fdSStefano Zampini /*@C 37856bd3a4fdSStefano Zampini TSResizeRetrieveVec - Retrieve a vector registered with `TSResizeRegisterVec()`. 37866bd3a4fdSStefano Zampini 37876bd3a4fdSStefano Zampini Collective 37886bd3a4fdSStefano Zampini 37896bd3a4fdSStefano Zampini Input Parameters: 37906bd3a4fdSStefano Zampini + ts - The `TS` context obtained from `TSCreate()` 3791baca6076SPierre Jolivet . name - A string identifying the vector 37926bd3a4fdSStefano Zampini - vec - The vector 37936bd3a4fdSStefano Zampini 37946bd3a4fdSStefano Zampini Level: developer 37956bd3a4fdSStefano Zampini 37966bd3a4fdSStefano Zampini Note: 37976bd3a4fdSStefano Zampini `TSResizeRetrieveVec()` is typically used within time stepping implementations, 37986bd3a4fdSStefano Zampini so most users would not generally call this routine themselves. 37996bd3a4fdSStefano Zampini 38006bd3a4fdSStefano Zampini .seealso: [](ch_ts), `TS`, `TSSetResize()`, `TSResize()`, `TSResizeRegisterVec()` 38016bd3a4fdSStefano Zampini @*/ 3802cc4c1da9SBarry Smith PetscErrorCode TSResizeRetrieveVec(TS ts, const char name[], Vec *vec) 38036bd3a4fdSStefano Zampini { 38046bd3a4fdSStefano Zampini PetscFunctionBegin; 38056bd3a4fdSStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 38064f572ea9SToby Isaac PetscAssertPointer(name, 2); 38074f572ea9SToby Isaac PetscAssertPointer(vec, 3); 38086bd3a4fdSStefano Zampini PetscCall(PetscObjectListFind(ts->resizetransferobjs, name, (PetscObject *)vec)); 38096bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 38106bd3a4fdSStefano Zampini } 38116bd3a4fdSStefano Zampini 38126bd3a4fdSStefano Zampini static PetscErrorCode TSResizeGetVecArray(TS ts, PetscInt *nv, const char **names[], Vec *vecs[]) 38136bd3a4fdSStefano Zampini { 38146bd3a4fdSStefano Zampini PetscInt cnt; 38156bd3a4fdSStefano Zampini PetscObjectList tmp; 38166bd3a4fdSStefano Zampini Vec *vecsin = NULL; 38176bd3a4fdSStefano Zampini const char **namesin = NULL; 38186bd3a4fdSStefano Zampini 38196bd3a4fdSStefano Zampini PetscFunctionBegin; 38206bd3a4fdSStefano Zampini for (tmp = ts->resizetransferobjs, cnt = 0; tmp; tmp = tmp->next) 38216bd3a4fdSStefano Zampini if (tmp->obj && tmp->obj->classid == VEC_CLASSID) cnt++; 382203ba6ac9SBarry Smith if (names) PetscCall(PetscMalloc1(cnt, &namesin)); 382303ba6ac9SBarry Smith if (vecs) PetscCall(PetscMalloc1(cnt, &vecsin)); 38246bd3a4fdSStefano Zampini for (tmp = ts->resizetransferobjs, cnt = 0; tmp; tmp = tmp->next) { 38256bd3a4fdSStefano Zampini if (tmp->obj && tmp->obj->classid == VEC_CLASSID) { 38266bd3a4fdSStefano Zampini if (vecs) vecsin[cnt] = (Vec)tmp->obj; 38276bd3a4fdSStefano Zampini if (names) namesin[cnt] = tmp->name; 38286bd3a4fdSStefano Zampini cnt++; 38296bd3a4fdSStefano Zampini } 38306bd3a4fdSStefano Zampini } 38316bd3a4fdSStefano Zampini if (nv) *nv = cnt; 38326bd3a4fdSStefano Zampini if (names) *names = namesin; 38336bd3a4fdSStefano Zampini if (vecs) *vecs = vecsin; 38346bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 38356bd3a4fdSStefano Zampini } 38366bd3a4fdSStefano Zampini 38376bd3a4fdSStefano Zampini /*@ 38386bd3a4fdSStefano Zampini TSResize - Runs the user-defined transfer functions provided with `TSSetResize()` 38396bd3a4fdSStefano Zampini 38406bd3a4fdSStefano Zampini Collective 38416bd3a4fdSStefano Zampini 38426bd3a4fdSStefano Zampini Input Parameter: 38436bd3a4fdSStefano Zampini . ts - The `TS` context obtained from `TSCreate()` 38446bd3a4fdSStefano Zampini 38456bd3a4fdSStefano Zampini Level: developer 38466bd3a4fdSStefano Zampini 38476bd3a4fdSStefano Zampini Note: 38486bd3a4fdSStefano Zampini `TSResize()` is typically used within time stepping implementations, 38496bd3a4fdSStefano Zampini so most users would not generally call this routine themselves. 38506bd3a4fdSStefano Zampini 38516bd3a4fdSStefano Zampini .seealso: [](ch_ts), `TS`, `TSSetResize()` 38526bd3a4fdSStefano Zampini @*/ 38536bd3a4fdSStefano Zampini PetscErrorCode TSResize(TS ts) 38546bd3a4fdSStefano Zampini { 38556bd3a4fdSStefano Zampini PetscInt nv = 0; 38566bd3a4fdSStefano Zampini const char **names = NULL; 38576bd3a4fdSStefano Zampini Vec *vecsin = NULL; 38586bd3a4fdSStefano Zampini const char *solname = "ts:vec_sol"; 38596bd3a4fdSStefano Zampini 38606bd3a4fdSStefano Zampini PetscFunctionBegin; 38616bd3a4fdSStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3862ecc87898SStefano Zampini if (!ts->resizesetup) PetscFunctionReturn(PETSC_SUCCESS); 38636bd3a4fdSStefano Zampini if (ts->resizesetup) { 38646bd3a4fdSStefano Zampini PetscBool flg = PETSC_FALSE; 38656bd3a4fdSStefano Zampini 38666bd3a4fdSStefano Zampini PetscCall(VecLockReadPush(ts->vec_sol)); 38676bd3a4fdSStefano Zampini PetscCallBack("TS callback resize setup", (*ts->resizesetup)(ts, ts->steps, ts->ptime, ts->vec_sol, &flg, ts->resizectx)); 38686bd3a4fdSStefano Zampini PetscCall(VecLockReadPop(ts->vec_sol)); 38696bd3a4fdSStefano Zampini if (flg) { 3870ecc87898SStefano Zampini if (ts->resizerollback) { 3871ecc87898SStefano Zampini PetscCall(TSRollBack(ts)); 3872ecc87898SStefano Zampini ts->time_step = ts->time_step0; 3873ecc87898SStefano Zampini } 38746bd3a4fdSStefano Zampini PetscCall(TSResizeRegisterVec(ts, solname, ts->vec_sol)); 38756bd3a4fdSStefano Zampini PetscCall(TSResizeRegisterOrRetrieve(ts, PETSC_TRUE)); /* specific impls register their own objects */ 38766bd3a4fdSStefano Zampini } 38776bd3a4fdSStefano Zampini } 38786bd3a4fdSStefano Zampini 38796bd3a4fdSStefano Zampini PetscCall(TSResizeGetVecArray(ts, &nv, &names, &vecsin)); 38806bd3a4fdSStefano Zampini if (nv) { 38816bd3a4fdSStefano Zampini Vec *vecsout, vecsol; 38826bd3a4fdSStefano Zampini 38836bd3a4fdSStefano Zampini /* Reset internal objects */ 38846bd3a4fdSStefano Zampini PetscCall(TSReset(ts)); 38856bd3a4fdSStefano Zampini 3886ecc87898SStefano Zampini /* Transfer needed vectors (users can call SetJacobian, SetDM, etc. here) */ 38876bd3a4fdSStefano Zampini PetscCall(PetscCalloc1(nv, &vecsout)); 38886bd3a4fdSStefano Zampini PetscCall(TSResizeTransferVecs(ts, nv, vecsin, vecsout)); 38896bd3a4fdSStefano Zampini for (PetscInt i = 0; i < nv; i++) { 3890ecc87898SStefano Zampini const char *name; 3891ecc87898SStefano Zampini char *oname; 3892ecc87898SStefano Zampini 3893ecc87898SStefano Zampini PetscCall(PetscObjectGetName((PetscObject)vecsin[i], &name)); 3894ecc87898SStefano Zampini PetscCall(PetscStrallocpy(name, &oname)); 38956bd3a4fdSStefano Zampini PetscCall(TSResizeRegisterVec(ts, names[i], vecsout[i])); 3896ecc87898SStefano Zampini if (vecsout[i]) PetscCall(PetscObjectSetName((PetscObject)vecsout[i], oname)); 3897ecc87898SStefano Zampini PetscCall(PetscFree(oname)); 38986bd3a4fdSStefano Zampini PetscCall(VecDestroy(&vecsout[i])); 38996bd3a4fdSStefano Zampini } 39006bd3a4fdSStefano Zampini PetscCall(PetscFree(vecsout)); 39016bd3a4fdSStefano Zampini PetscCall(TSResizeRegisterOrRetrieve(ts, PETSC_FALSE)); /* specific impls import the transferred objects */ 39026bd3a4fdSStefano Zampini 39036bd3a4fdSStefano Zampini PetscCall(TSResizeRetrieveVec(ts, solname, &vecsol)); 39046bd3a4fdSStefano Zampini if (vecsol) PetscCall(TSSetSolution(ts, vecsol)); 39056bd3a4fdSStefano Zampini PetscAssert(ts->vec_sol, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_NULL, "Missing TS solution"); 39066bd3a4fdSStefano Zampini } 39076bd3a4fdSStefano Zampini 39086bd3a4fdSStefano Zampini PetscCall(PetscFree(names)); 39096bd3a4fdSStefano Zampini PetscCall(PetscFree(vecsin)); 39106bd3a4fdSStefano Zampini PetscCall(TSResizeReset(ts)); 39116bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 39126bd3a4fdSStefano Zampini } 39136bd3a4fdSStefano Zampini 3914b1cb36f3SHong Zhang /*@ 39156a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 39166a4d4014SLisandro Dalcin 3917c3339decSBarry Smith Collective 39186a4d4014SLisandro Dalcin 3919d8d19677SJose E. Roman Input Parameters: 3920bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 3921bcf0153eSBarry Smith - u - the solution vector (can be null if `TSSetSolution()` was used and `TSSetExactFinalTime`(ts,`TS_EXACTFINALTIME_MATCHSTEP`) was not used, 392263e21af5SBarry Smith otherwise must contain the initial conditions and will contain the solution at the final requested time 39235a3a76d0SJed Brown 39246a4d4014SLisandro Dalcin Level: beginner 39256a4d4014SLisandro Dalcin 39265a3a76d0SJed Brown Notes: 39275a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 3928bcf0153eSBarry Smith held state accessible by `TSGetSolution()` and `TSGetTime()` because the method may have 39295a3a76d0SJed Brown stepped over the final time. 39305a3a76d0SJed Brown 39311cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSSetSolution()`, `TSStep()`, `TSGetTime()`, `TSGetSolveTime()` 39326a4d4014SLisandro Dalcin @*/ 3933d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSolve(TS ts, Vec u) 3934d71ae5a4SJacob Faibussowitsch { 3935b06615a5SLisandro Dalcin Vec solution; 3936f22f69f0SBarry Smith 39376a4d4014SLisandro Dalcin PetscFunctionBegin; 39380700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3939f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3940303a5415SBarry Smith 39419566063dSJacob Faibussowitsch PetscCall(TSSetExactFinalTimeDefault(ts)); 3942ee41a567SStefano 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 */ 39430910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 39449566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u, &solution)); 39459566063dSJacob Faibussowitsch PetscCall(TSSetSolution(ts, solution)); 39469566063dSJacob Faibussowitsch PetscCall(VecDestroy(&solution)); /* grant ownership */ 39475a3a76d0SJed Brown } 39489566063dSJacob Faibussowitsch PetscCall(VecCopy(u, ts->vec_sol)); 39493c633725SBarry Smith PetscCheck(!ts->forward_solve, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE"); 39501baa6e33SBarry Smith } else if (u) PetscCall(TSSetSolution(ts, u)); 39519566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts)); 39529566063dSJacob Faibussowitsch PetscCall(TSTrajectorySetUp(ts->trajectory, ts)); 3953a6772fa2SLisandro Dalcin 39543c633725SBarry Smith PetscCheck(ts->max_time < PETSC_MAX_REAL || ts->max_steps != PETSC_MAX_INT, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "You must call TSSetMaxTime() or TSSetMaxSteps(), or use -ts_max_time <time> or -ts_max_steps <steps>"); 39553c633725SBarry 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()"); 39563c633725SBarry 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"); 39574a658b32SHong Zhang PetscCheck(!(ts->tspan && ts->exact_final_time != TS_EXACTFINALTIME_MATCHSTEP), PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "You must use TS_EXACTFINALTIME_MATCHSTEP when using time span"); 39584a658b32SHong Zhang 3959e1db57b0SHong Zhang if (ts->tspan && PetscIsCloseAtTol(ts->ptime, ts->tspan->span_times[0], ts->tspan->reltol * ts->time_step + ts->tspan->abstol, 0)) { /* starting point in time span */ 39604a658b32SHong Zhang PetscCall(VecCopy(ts->vec_sol, ts->tspan->vecs_sol[0])); 39614a658b32SHong Zhang ts->tspan->spanctr = 1; 39624a658b32SHong Zhang } 3963a6772fa2SLisandro Dalcin 39641baa6e33SBarry Smith if (ts->forward_solve) PetscCall(TSForwardSetUp(ts)); 3965715f1b00SHong Zhang 3966e7069c78SShri /* reset number of steps only when the step is not restarted. ARKIMEX 3967715f1b00SHong Zhang restarts the step after an event. Resetting these counters in such case causes 3968e7069c78SShri TSTrajectory to incorrectly save the output files 3969e7069c78SShri */ 3970715f1b00SHong Zhang /* reset time step and iteration counters */ 39712808aa04SLisandro Dalcin if (!ts->steps) { 39725ef26d82SJed Brown ts->ksp_its = 0; 39735ef26d82SJed Brown ts->snes_its = 0; 3974c610991cSLisandro Dalcin ts->num_snes_failures = 0; 3975c610991cSLisandro Dalcin ts->reject = 0; 39762808aa04SLisandro Dalcin ts->steprestart = PETSC_TRUE; 39772808aa04SLisandro Dalcin ts->steprollback = PETSC_FALSE; 39787d51462cSStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 39792808aa04SLisandro Dalcin } 3980e97c63d7SStefano Zampini 39814a658b32SHong Zhang /* make sure initial time step does not overshoot final time or the next point in tspan */ 3982e97c63d7SStefano Zampini if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) { 39834a658b32SHong Zhang PetscReal maxdt; 3984e97c63d7SStefano Zampini PetscReal dt = ts->time_step; 3985e97c63d7SStefano Zampini 39864a658b32SHong Zhang if (ts->tspan) maxdt = ts->tspan->span_times[ts->tspan->spanctr] - ts->ptime; 39874a658b32SHong Zhang else maxdt = ts->max_time - ts->ptime; 3988e97c63d7SStefano Zampini ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt, maxdt, 10 * PETSC_MACHINE_EPSILON, 0) ? maxdt : dt); 3989e97c63d7SStefano Zampini } 3990193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 3991193ac0bcSJed Brown 3992900f6b5bSMatthew G. Knepley { 3993900f6b5bSMatthew G. Knepley PetscViewer viewer; 3994900f6b5bSMatthew G. Knepley PetscViewerFormat format; 3995900f6b5bSMatthew G. Knepley PetscBool flg; 3996900f6b5bSMatthew G. Knepley static PetscBool incall = PETSC_FALSE; 3997900f6b5bSMatthew G. Knepley 3998900f6b5bSMatthew G. Knepley if (!incall) { 3999900f6b5bSMatthew G. Knepley /* Estimate the convergence rate of the time discretization */ 4000648c30bcSBarry Smith PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)ts), ((PetscObject)ts)->options, ((PetscObject)ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg)); 4001900f6b5bSMatthew G. Knepley if (flg) { 4002900f6b5bSMatthew G. Knepley PetscConvEst conv; 4003900f6b5bSMatthew G. Knepley DM dm; 4004900f6b5bSMatthew G. Knepley PetscReal *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */ 4005900f6b5bSMatthew G. Knepley PetscInt Nf; 4006f2ed2dc7SMatthew G. Knepley PetscBool checkTemporal = PETSC_TRUE; 4007900f6b5bSMatthew G. Knepley 4008900f6b5bSMatthew G. Knepley incall = PETSC_TRUE; 40099566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject)ts)->prefix, "-ts_convergence_temporal", &checkTemporal, &flg)); 40109566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 40119566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &Nf)); 40129566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(PetscMax(Nf, 1), &alpha)); 40139566063dSJacob Faibussowitsch PetscCall(PetscConvEstCreate(PetscObjectComm((PetscObject)ts), &conv)); 40149566063dSJacob Faibussowitsch PetscCall(PetscConvEstUseTS(conv, checkTemporal)); 40159566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetSolver(conv, (PetscObject)ts)); 40169566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetFromOptions(conv)); 40179566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetUp(conv)); 40189566063dSJacob Faibussowitsch PetscCall(PetscConvEstGetConvRate(conv, alpha)); 40199566063dSJacob Faibussowitsch PetscCall(PetscViewerPushFormat(viewer, format)); 40209566063dSJacob Faibussowitsch PetscCall(PetscConvEstRateView(conv, alpha, viewer)); 40219566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(viewer)); 4022648c30bcSBarry Smith PetscCall(PetscViewerDestroy(&viewer)); 40239566063dSJacob Faibussowitsch PetscCall(PetscConvEstDestroy(&conv)); 40249566063dSJacob Faibussowitsch PetscCall(PetscFree(alpha)); 4025900f6b5bSMatthew G. Knepley incall = PETSC_FALSE; 4026900f6b5bSMatthew G. Knepley } 4027900f6b5bSMatthew G. Knepley } 4028900f6b5bSMatthew G. Knepley } 4029900f6b5bSMatthew G. Knepley 40309566063dSJacob Faibussowitsch PetscCall(TSViewFromOptions(ts, NULL, "-ts_view_pre")); 4031f05ece33SBarry Smith 4032193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 4033dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, solve); 40349566063dSJacob Faibussowitsch if (u) PetscCall(VecCopy(ts->vec_sol, u)); 4035cc708dedSBarry Smith ts->solvetime = ts->ptime; 4036a6772fa2SLisandro Dalcin solution = ts->vec_sol; 4037be5899b3SLisandro Dalcin } else { /* Step the requested number of timesteps. */ 4038db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 4039db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 4040e7069c78SShri 40412808aa04SLisandro Dalcin if (!ts->steps) { 40429566063dSJacob Faibussowitsch PetscCall(TSTrajectorySet(ts->trajectory, ts, ts->steps, ts->ptime, ts->vec_sol)); 40439566063dSJacob Faibussowitsch PetscCall(TSEventInitialize(ts->event, ts, ts->ptime, ts->vec_sol)); 40442808aa04SLisandro Dalcin } 40456427ac75SLisandro Dalcin 4046e1a7a14fSJed Brown while (!ts->reason) { 40479566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts, ts->steps, ts->ptime, ts->vec_sol)); 40481e66621cSBarry Smith if (!ts->steprollback) PetscCall(TSPreStep(ts)); 40499566063dSJacob Faibussowitsch PetscCall(TSStep(ts)); 40501baa6e33SBarry Smith if (ts->testjacobian) PetscCall(TSRHSJacobianTest(ts, NULL)); 40511baa6e33SBarry Smith if (ts->testjacobiantranspose) PetscCall(TSRHSJacobianTestTranspose(ts, NULL)); 4052cd4cee2dSHong Zhang if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */ 40537b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */ 40549566063dSJacob Faibussowitsch PetscCall(TSForwardCostIntegral(ts)); 40557b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps++; 4056b1cb36f3SHong Zhang } 405758818c2dSLisandro Dalcin if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */ 40587b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */ 40599566063dSJacob Faibussowitsch PetscCall(TSForwardStep(ts)); 40607b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps++; 4061715f1b00SHong Zhang } 40629566063dSJacob Faibussowitsch PetscCall(TSPostEvaluate(ts)); 40639566063dSJacob 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. */ 40641baa6e33SBarry Smith if (ts->steprollback) PetscCall(TSPostEvaluate(ts)); 4065ecc87898SStefano Zampini if (!ts->steprollback && ts->resizerollback) PetscCall(TSResize(ts)); 406690d719a4SStefano Zampini /* check convergence */ 406790d719a4SStefano Zampini if (!ts->reason) { 406890d719a4SStefano Zampini if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 406990d719a4SStefano Zampini else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 407090d719a4SStefano Zampini } 4071e783b05fSHong Zhang if (!ts->steprollback) { 40729566063dSJacob Faibussowitsch PetscCall(TSTrajectorySet(ts->trajectory, ts, ts->steps, ts->ptime, ts->vec_sol)); 40739566063dSJacob Faibussowitsch PetscCall(TSPostStep(ts)); 4074ecc87898SStefano Zampini if (!ts->resizerollback) PetscCall(TSResize(ts)); 4075ca4445c7SIlya Fursov 4076ca4445c7SIlya Fursov if (ts->tspan && ts->tspan->spanctr < ts->tspan->num_span_times) { 4077ca4445c7SIlya Fursov PetscCheck(ts->tspan->worktol > 0, PetscObjectComm((PetscObject)ts), PETSC_ERR_PLIB, "Unexpected state !(tspan->worktol > 0) in TSSolve()"); 4078ca4445c7SIlya Fursov if (PetscIsCloseAtTol(ts->ptime, ts->tspan->span_times[ts->tspan->spanctr], ts->tspan->worktol, 0)) PetscCall(VecCopy(ts->vec_sol, ts->tspan->vecs_sol[ts->tspan->spanctr++])); 4079ca4445c7SIlya Fursov } 4080aeb4809dSShri Abhyankar } 4081193ac0bcSJed Brown } 40829566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts, ts->steps, ts->ptime, ts->vec_sol)); 40836427ac75SLisandro Dalcin 408449354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 40855dbbf88eSStefano Zampini if (!u) u = ts->vec_sol; 40869566063dSJacob Faibussowitsch PetscCall(TSInterpolate(ts, ts->max_time, u)); 4087cc708dedSBarry Smith ts->solvetime = ts->max_time; 4088b06615a5SLisandro Dalcin solution = u; 40899566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts, -1, ts->solvetime, solution)); 40900574a7fbSJed Brown } else { 40919566063dSJacob Faibussowitsch if (u) PetscCall(VecCopy(ts->vec_sol, u)); 4092cc708dedSBarry Smith ts->solvetime = ts->ptime; 4093b06615a5SLisandro Dalcin solution = ts->vec_sol; 40940574a7fbSJed Brown } 4095193ac0bcSJed Brown } 4096d2daff3dSHong Zhang 40979566063dSJacob Faibussowitsch PetscCall(TSViewFromOptions(ts, NULL, "-ts_view")); 40989566063dSJacob Faibussowitsch PetscCall(VecViewFromOptions(solution, (PetscObject)ts, "-ts_view_solution")); 40999566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsBlock((PetscObject)ts)); 41001baa6e33SBarry Smith if (ts->adjoint_solve) PetscCall(TSAdjointSolve(ts)); 41013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 41026a4d4014SLisandro Dalcin } 41036a4d4014SLisandro Dalcin 4104d763cef2SBarry Smith /*@ 4105b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 4106d763cef2SBarry Smith 4107d763cef2SBarry Smith Not Collective 4108d763cef2SBarry Smith 4109d763cef2SBarry Smith Input Parameter: 4110bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 4111d763cef2SBarry Smith 4112d763cef2SBarry Smith Output Parameter: 4113bcf0153eSBarry Smith . t - the current time. This time may not corresponds to the final time set with `TSSetMaxTime()`, use `TSGetSolveTime()`. 4114d763cef2SBarry Smith 4115d763cef2SBarry Smith Level: beginner 4116d763cef2SBarry Smith 4117b8123daeSJed Brown Note: 4118bcf0153eSBarry Smith When called during time step evaluation (e.g. during residual evaluation or via hooks set using `TSSetPreStep()`, 4119bcf0153eSBarry Smith `TSSetPreStage()`, `TSSetPostStage()`, or `TSSetPostStep()`), the time is the time at the start of the step being evaluated. 4120b8123daeSJed Brown 4121a94f484eSPierre Jolivet .seealso: [](ch_ts), `TS`, `TSGetSolveTime()`, `TSSetTime()`, `TSGetTimeStep()`, `TSGetStepNumber()` 4122d763cef2SBarry Smith @*/ 4123d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTime(TS ts, PetscReal *t) 4124d71ae5a4SJacob Faibussowitsch { 4125d763cef2SBarry Smith PetscFunctionBegin; 41260700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 41274f572ea9SToby Isaac PetscAssertPointer(t, 2); 4128d763cef2SBarry Smith *t = ts->ptime; 41293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4130d763cef2SBarry Smith } 4131d763cef2SBarry Smith 4132e5e524a1SHong Zhang /*@ 4133e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 4134e5e524a1SHong Zhang 4135e5e524a1SHong Zhang Not Collective 4136e5e524a1SHong Zhang 4137e5e524a1SHong Zhang Input Parameter: 4138bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 4139e5e524a1SHong Zhang 4140e5e524a1SHong Zhang Output Parameter: 4141e5e524a1SHong Zhang . t - the previous time 4142e5e524a1SHong Zhang 4143e5e524a1SHong Zhang Level: beginner 4144e5e524a1SHong Zhang 4145a94f484eSPierre Jolivet .seealso: [](ch_ts), `TS`, `TSGetTime()`, `TSGetSolveTime()`, `TSGetTimeStep()` 4146e5e524a1SHong Zhang @*/ 4147d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetPrevTime(TS ts, PetscReal *t) 4148d71ae5a4SJacob Faibussowitsch { 4149e5e524a1SHong Zhang PetscFunctionBegin; 4150e5e524a1SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 41514f572ea9SToby Isaac PetscAssertPointer(t, 2); 4152e5e524a1SHong Zhang *t = ts->ptime_prev; 41533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4154e5e524a1SHong Zhang } 4155e5e524a1SHong Zhang 41566a4d4014SLisandro Dalcin /*@ 41576a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 41586a4d4014SLisandro Dalcin 4159c3339decSBarry Smith Logically Collective 41606a4d4014SLisandro Dalcin 41616a4d4014SLisandro Dalcin Input Parameters: 4162bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 4163b43aa488SJacob Faibussowitsch - t - the time 41646a4d4014SLisandro Dalcin 41656a4d4014SLisandro Dalcin Level: intermediate 41666a4d4014SLisandro Dalcin 41671cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTime()`, `TSSetMaxSteps()` 41686a4d4014SLisandro Dalcin @*/ 4169d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTime(TS ts, PetscReal t) 4170d71ae5a4SJacob Faibussowitsch { 41716a4d4014SLisandro Dalcin PetscFunctionBegin; 41720700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4173c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts, t, 2); 41746a4d4014SLisandro Dalcin ts->ptime = t; 41753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 41766a4d4014SLisandro Dalcin } 41776a4d4014SLisandro Dalcin 4178cc4c1da9SBarry Smith /*@ 4179d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 4180d763cef2SBarry Smith TS options in the database. 4181d763cef2SBarry Smith 4182c3339decSBarry Smith Logically Collective 4183d763cef2SBarry Smith 4184d8d19677SJose E. Roman Input Parameters: 4185bcf0153eSBarry Smith + ts - The `TS` context 4186d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4187d763cef2SBarry Smith 4188bcf0153eSBarry Smith Level: advanced 4189bcf0153eSBarry Smith 4190bcf0153eSBarry Smith Note: 4191d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4192d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4193d763cef2SBarry Smith hyphen. 4194d763cef2SBarry Smith 41951cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetFromOptions()`, `TSAppendOptionsPrefix()` 4196d763cef2SBarry Smith @*/ 4197d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetOptionsPrefix(TS ts, const char prefix[]) 4198d71ae5a4SJacob Faibussowitsch { 4199089b2837SJed Brown SNES snes; 4200d763cef2SBarry Smith 4201d763cef2SBarry Smith PetscFunctionBegin; 42020700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 42039566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)ts, prefix)); 42049566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 42059566063dSJacob Faibussowitsch PetscCall(SNESSetOptionsPrefix(snes, prefix)); 42063ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4207d763cef2SBarry Smith } 4208d763cef2SBarry Smith 4209cc4c1da9SBarry Smith /*@ 4210d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 4211d763cef2SBarry Smith TS options in the database. 4212d763cef2SBarry Smith 4213c3339decSBarry Smith Logically Collective 4214d763cef2SBarry Smith 4215d8d19677SJose E. Roman Input Parameters: 4216bcf0153eSBarry Smith + ts - The `TS` context 4217d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4218d763cef2SBarry Smith 4219bcf0153eSBarry Smith Level: advanced 4220bcf0153eSBarry Smith 4221bcf0153eSBarry Smith Note: 4222d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4223d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4224d763cef2SBarry Smith hyphen. 4225d763cef2SBarry Smith 42261cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetOptionsPrefix()`, `TSSetOptionsPrefix()`, `TSSetFromOptions()` 4227d763cef2SBarry Smith @*/ 4228d71ae5a4SJacob Faibussowitsch PetscErrorCode TSAppendOptionsPrefix(TS ts, const char prefix[]) 4229d71ae5a4SJacob Faibussowitsch { 4230089b2837SJed Brown SNES snes; 4231d763cef2SBarry Smith 4232d763cef2SBarry Smith PetscFunctionBegin; 42330700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 42349566063dSJacob Faibussowitsch PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)ts, prefix)); 42359566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 42369566063dSJacob Faibussowitsch PetscCall(SNESAppendOptionsPrefix(snes, prefix)); 42373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4238d763cef2SBarry Smith } 4239d763cef2SBarry Smith 4240cc4c1da9SBarry Smith /*@ 4241d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 4242bcf0153eSBarry Smith `TS` options in the database. 4243d763cef2SBarry Smith 4244d763cef2SBarry Smith Not Collective 4245d763cef2SBarry Smith 4246d763cef2SBarry Smith Input Parameter: 4247bcf0153eSBarry Smith . ts - The `TS` context 4248d763cef2SBarry Smith 4249d763cef2SBarry Smith Output Parameter: 4250d763cef2SBarry Smith . prefix - A pointer to the prefix string used 4251d763cef2SBarry Smith 4252d763cef2SBarry Smith Level: intermediate 4253d763cef2SBarry Smith 4254b43aa488SJacob Faibussowitsch Fortran Notes: 4255195e9b02SBarry Smith The user should pass in a string 'prefix' of 4256bcf0153eSBarry Smith sufficient length to hold the prefix. 4257bcf0153eSBarry Smith 42581cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSAppendOptionsPrefix()`, `TSSetFromOptions()` 4259d763cef2SBarry Smith @*/ 4260d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetOptionsPrefix(TS ts, const char *prefix[]) 4261d71ae5a4SJacob Faibussowitsch { 4262d763cef2SBarry Smith PetscFunctionBegin; 42630700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 42644f572ea9SToby Isaac PetscAssertPointer(prefix, 2); 42659566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)ts, prefix)); 42663ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4267d763cef2SBarry Smith } 4268d763cef2SBarry Smith 4269d763cef2SBarry Smith /*@C 4270d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4271d763cef2SBarry Smith 4272bcf0153eSBarry Smith Not Collective, but parallel objects are returned if ts is parallel 4273d763cef2SBarry Smith 4274d763cef2SBarry Smith Input Parameter: 4275bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 4276d763cef2SBarry Smith 4277d763cef2SBarry Smith Output Parameters: 4278195e9b02SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or `NULL`) 4279195e9b02SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as `Amat` (or `NULL`) 4280195e9b02SBarry Smith . func - Function to compute the Jacobian of the RHS (or `NULL`) 4281195e9b02SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or `NULL`) 4282d763cef2SBarry Smith 4283d763cef2SBarry Smith Level: intermediate 4284d763cef2SBarry Smith 4285bcf0153eSBarry Smith Note: 4286195e9b02SBarry Smith You can pass in `NULL` for any return argument you do not need. 4287bcf0153eSBarry Smith 42881cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTimeStep()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()` 4289d763cef2SBarry Smith 4290d763cef2SBarry Smith @*/ 42918434afd1SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts, Mat *Amat, Mat *Pmat, TSRHSJacobianFn **func, void **ctx) 4292d71ae5a4SJacob Faibussowitsch { 429324989b8cSPeter Brune DM dm; 4294089b2837SJed Brown 4295d763cef2SBarry Smith PetscFunctionBegin; 429623a57915SBarry Smith if (Amat || Pmat) { 429723a57915SBarry Smith SNES snes; 42989566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 42999566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 43009566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes, Amat, Pmat, NULL, NULL)); 430123a57915SBarry Smith } 43029566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 43039566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, func, ctx)); 43043ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4305d763cef2SBarry Smith } 4306d763cef2SBarry Smith 43072eca1d9cSJed Brown /*@C 43082eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 43092eca1d9cSJed Brown 4310bcf0153eSBarry Smith Not Collective, but parallel objects are returned if ts is parallel 43112eca1d9cSJed Brown 43122eca1d9cSJed Brown Input Parameter: 4313bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 43142eca1d9cSJed Brown 43152eca1d9cSJed Brown Output Parameters: 4316e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4317195e9b02SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as `Amat` 43182eca1d9cSJed Brown . f - The function to compute the matrices 43192eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 43202eca1d9cSJed Brown 43212eca1d9cSJed Brown Level: advanced 43222eca1d9cSJed Brown 4323bcf0153eSBarry Smith Note: 4324195e9b02SBarry Smith You can pass in `NULL` for any return argument you do not need. 4325bcf0153eSBarry Smith 43261cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTimeStep()`, `TSGetRHSJacobian()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()` 43272eca1d9cSJed Brown @*/ 43288434afd1SBarry Smith PetscErrorCode TSGetIJacobian(TS ts, Mat *Amat, Mat *Pmat, TSIJacobianFn **f, void **ctx) 4329d71ae5a4SJacob Faibussowitsch { 433024989b8cSPeter Brune DM dm; 43310910c330SBarry Smith 43322eca1d9cSJed Brown PetscFunctionBegin; 4333c0aab802Sstefano_zampini if (Amat || Pmat) { 4334c0aab802Sstefano_zampini SNES snes; 43359566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 43369566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 43379566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes, Amat, Pmat, NULL, NULL)); 4338c0aab802Sstefano_zampini } 43399566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 43409566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm, f, ctx)); 43413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 43422eca1d9cSJed Brown } 43432eca1d9cSJed Brown 4344af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 43456c699258SBarry Smith /*@ 4346bcf0153eSBarry Smith TSSetDM - Sets the `DM` that may be used by some nonlinear solvers or preconditioners under the `TS` 43476c699258SBarry Smith 4348c3339decSBarry Smith Logically Collective 43496c699258SBarry Smith 43506c699258SBarry Smith Input Parameters: 4351bcf0153eSBarry Smith + ts - the `TS` integrator object 4352195e9b02SBarry Smith - dm - the dm, cannot be `NULL` 43536c699258SBarry Smith 43546c699258SBarry Smith Level: intermediate 43556c699258SBarry Smith 4356bcf0153eSBarry Smith Notes: 4357bcf0153eSBarry Smith A `DM` can only be used for solving one problem at a time because information about the problem is stored on the `DM`, 4358bcf0153eSBarry Smith even when not using interfaces like `DMTSSetIFunction()`. Use `DMClone()` to get a distinct `DM` when solving 4359bcf0153eSBarry Smith different problems using the same function space. 4360bcf0153eSBarry Smith 43611cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `DM`, `TSGetDM()`, `SNESSetDM()`, `SNESGetDM()` 43626c699258SBarry Smith @*/ 4363d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetDM(TS ts, DM dm) 4364d71ae5a4SJacob Faibussowitsch { 4365089b2837SJed Brown SNES snes; 4366942e3340SBarry Smith DMTS tsdm; 43676c699258SBarry Smith 43686c699258SBarry Smith PetscFunctionBegin; 43690700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 43702a808120SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 2); 43719566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)dm)); 4372942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 43732a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 43749566063dSJacob Faibussowitsch PetscCall(DMCopyDMTS(ts->dm, dm)); 43759566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm, &tsdm)); 43761e66621cSBarry Smith /* Grant write privileges to the replacement DM */ 43771e66621cSBarry Smith if (tsdm->originaldm == ts->dm) tsdm->originaldm = dm; 437824989b8cSPeter Brune } 43799566063dSJacob Faibussowitsch PetscCall(DMDestroy(&ts->dm)); 438024989b8cSPeter Brune } 43816c699258SBarry Smith ts->dm = dm; 4382bbd56ea5SKarl Rupp 43839566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 43849566063dSJacob Faibussowitsch PetscCall(SNESSetDM(snes, dm)); 43853ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 43866c699258SBarry Smith } 43876c699258SBarry Smith 43886c699258SBarry Smith /*@ 4389bcf0153eSBarry Smith TSGetDM - Gets the `DM` that may be used by some preconditioners 43906c699258SBarry Smith 43913f9fe445SBarry Smith Not Collective 43926c699258SBarry Smith 43936c699258SBarry Smith Input Parameter: 4394bcf0153eSBarry Smith . ts - the `TS` 43956c699258SBarry Smith 43966c699258SBarry Smith Output Parameter: 4397195e9b02SBarry Smith . dm - the `DM` 43986c699258SBarry Smith 43996c699258SBarry Smith Level: intermediate 44006c699258SBarry Smith 44011cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `DM`, `TSSetDM()`, `SNESSetDM()`, `SNESGetDM()` 44026c699258SBarry Smith @*/ 4403d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetDM(TS ts, DM *dm) 4404d71ae5a4SJacob Faibussowitsch { 44056c699258SBarry Smith PetscFunctionBegin; 44060700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4407496e6a7aSJed Brown if (!ts->dm) { 44089566063dSJacob Faibussowitsch PetscCall(DMShellCreate(PetscObjectComm((PetscObject)ts), &ts->dm)); 44099566063dSJacob Faibussowitsch if (ts->snes) PetscCall(SNESSetDM(ts->snes, ts->dm)); 4410496e6a7aSJed Brown } 44116c699258SBarry Smith *dm = ts->dm; 44123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 44136c699258SBarry Smith } 44141713a123SBarry Smith 44150f5c6efeSJed Brown /*@ 44160f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 44170f5c6efeSJed Brown 4418c3339decSBarry Smith Logically Collective 44190f5c6efeSJed Brown 4420d8d19677SJose E. Roman Input Parameters: 4421d42a1c89SJed Brown + snes - nonlinear solver 44220910c330SBarry Smith . U - the current state at which to evaluate the residual 4423d42a1c89SJed Brown - ctx - user context, must be a TS 44240f5c6efeSJed Brown 44250f5c6efeSJed Brown Output Parameter: 44260f5c6efeSJed Brown . F - the nonlinear residual 44270f5c6efeSJed Brown 44280f5c6efeSJed Brown Level: advanced 44290f5c6efeSJed Brown 4430bcf0153eSBarry Smith Note: 4431bcf0153eSBarry Smith This function is not normally called by users and is automatically registered with the `SNES` used by `TS`. 4432bcf0153eSBarry Smith It is most frequently passed to `MatFDColoringSetFunction()`. 4433bcf0153eSBarry Smith 44341cc06b55SBarry Smith .seealso: [](ch_ts), `SNESSetFunction()`, `MatFDColoringSetFunction()` 44350f5c6efeSJed Brown @*/ 4436d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESTSFormFunction(SNES snes, Vec U, Vec F, void *ctx) 4437d71ae5a4SJacob Faibussowitsch { 44380f5c6efeSJed Brown TS ts = (TS)ctx; 44390f5c6efeSJed Brown 44400f5c6efeSJed Brown PetscFunctionBegin; 44410f5c6efeSJed Brown PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 44420910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 2); 44430f5c6efeSJed Brown PetscValidHeaderSpecific(F, VEC_CLASSID, 3); 44440f5c6efeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 4); 4445346ce620SStefano Zampini PetscCheck(ts->ops->snesfunction, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "No method snesfunction for TS of type %s", ((PetscObject)ts)->type_name); 4446346ce620SStefano Zampini PetscCall((*ts->ops->snesfunction)(snes, U, F, ts)); 44473ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 44480f5c6efeSJed Brown } 44490f5c6efeSJed Brown 44500f5c6efeSJed Brown /*@ 44510f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 44520f5c6efeSJed Brown 4453c3339decSBarry Smith Collective 44540f5c6efeSJed Brown 4455d8d19677SJose E. Roman Input Parameters: 44560f5c6efeSJed Brown + snes - nonlinear solver 44570910c330SBarry Smith . U - the current state at which to evaluate the residual 4458bcf0153eSBarry Smith - ctx - user context, must be a `TS` 44590f5c6efeSJed Brown 4460d8d19677SJose E. Roman Output Parameters: 44610f5c6efeSJed Brown + A - the Jacobian 44626b867d5aSJose E. Roman - B - the preconditioning matrix (may be the same as A) 44630f5c6efeSJed Brown 44640f5c6efeSJed Brown Level: developer 44650f5c6efeSJed Brown 4466bcf0153eSBarry Smith Note: 4467bcf0153eSBarry Smith This function is not normally called by users and is automatically registered with the `SNES` used by `TS`. 4468bcf0153eSBarry Smith 44691cc06b55SBarry Smith .seealso: [](ch_ts), `SNESSetJacobian()` 44700f5c6efeSJed Brown @*/ 4471d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESTSFormJacobian(SNES snes, Vec U, Mat A, Mat B, void *ctx) 4472d71ae5a4SJacob Faibussowitsch { 44730f5c6efeSJed Brown TS ts = (TS)ctx; 44740f5c6efeSJed Brown 44750f5c6efeSJed Brown PetscFunctionBegin; 44760f5c6efeSJed Brown PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 44770910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 2); 447894ab13aaSBarry Smith PetscValidHeaderSpecific(A, MAT_CLASSID, 3); 447994ab13aaSBarry Smith PetscValidHeaderSpecific(B, MAT_CLASSID, 4); 4480064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(ts, TS_CLASSID, 5); 4481346ce620SStefano Zampini PetscCheck(ts->ops->snesjacobian, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "No method snesjacobian for TS of type %s", ((PetscObject)ts)->type_name); 4482346ce620SStefano Zampini PetscCall((*ts->ops->snesjacobian)(snes, U, A, B, ts)); 44833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 44840f5c6efeSJed Brown } 4485325fc9f4SBarry Smith 44860e4ef248SJed Brown /*@C 4487dd8e379bSPierre Jolivet TSComputeRHSFunctionLinear - Evaluate the right-hand side via the user-provided Jacobian, for linear problems Udot = A U only 44880e4ef248SJed Brown 4489c3339decSBarry Smith Collective 44900e4ef248SJed Brown 44914165533cSJose E. Roman Input Parameters: 44920e4ef248SJed Brown + ts - time stepping context 44930e4ef248SJed Brown . t - time at which to evaluate 44940910c330SBarry Smith . U - state at which to evaluate 44950e4ef248SJed Brown - ctx - context 44960e4ef248SJed Brown 44974165533cSJose E. Roman Output Parameter: 4498dd8e379bSPierre Jolivet . F - right-hand side 44990e4ef248SJed Brown 45000e4ef248SJed Brown Level: intermediate 45010e4ef248SJed Brown 4502bcf0153eSBarry Smith Note: 4503dd8e379bSPierre Jolivet This function is intended to be passed to `TSSetRHSFunction()` to evaluate the right-hand side for linear problems. 4504bcf0153eSBarry Smith The matrix (and optionally the evaluation context) should be passed to `TSSetRHSJacobian()`. 45050e4ef248SJed Brown 45061cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSFunction()`, `TSSetRHSJacobian()`, `TSComputeRHSJacobianConstant()` 45070e4ef248SJed Brown @*/ 4508d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSFunctionLinear(TS ts, PetscReal t, Vec U, Vec F, void *ctx) 4509d71ae5a4SJacob Faibussowitsch { 45100e4ef248SJed Brown Mat Arhs, Brhs; 45110e4ef248SJed Brown 45120e4ef248SJed Brown PetscFunctionBegin; 45139566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts, &Arhs, &Brhs)); 45142663174eSHong Zhang /* undo the damage caused by shifting */ 45159566063dSJacob Faibussowitsch PetscCall(TSRecoverRHSJacobian(ts, Arhs, Brhs)); 45169566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts, t, U, Arhs, Brhs)); 45179566063dSJacob Faibussowitsch PetscCall(MatMult(Arhs, U, F)); 45183ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 45190e4ef248SJed Brown } 45200e4ef248SJed Brown 45210e4ef248SJed Brown /*@C 45220e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 45230e4ef248SJed Brown 4524c3339decSBarry Smith Collective 45250e4ef248SJed Brown 45264165533cSJose E. Roman Input Parameters: 45270e4ef248SJed Brown + ts - time stepping context 45280e4ef248SJed Brown . t - time at which to evaluate 45290910c330SBarry Smith . U - state at which to evaluate 45300e4ef248SJed Brown - ctx - context 45310e4ef248SJed Brown 45324165533cSJose E. Roman Output Parameters: 45330e4ef248SJed Brown + A - pointer to operator 453497bb3fdcSJose E. Roman - B - pointer to preconditioning matrix 45350e4ef248SJed Brown 45360e4ef248SJed Brown Level: intermediate 45370e4ef248SJed Brown 4538bcf0153eSBarry Smith Note: 4539bcf0153eSBarry Smith This function is intended to be passed to `TSSetRHSJacobian()` to evaluate the Jacobian for linear time-independent problems. 45400e4ef248SJed Brown 45411cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSFunction()`, `TSSetRHSJacobian()`, `TSComputeRHSFunctionLinear()` 45420e4ef248SJed Brown @*/ 4543d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSJacobianConstant(TS ts, PetscReal t, Vec U, Mat A, Mat B, void *ctx) 4544d71ae5a4SJacob Faibussowitsch { 45450e4ef248SJed Brown PetscFunctionBegin; 45463ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 45470e4ef248SJed Brown } 45480e4ef248SJed Brown 45490026cea9SSean Farley /*@C 45500026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 45510026cea9SSean Farley 4552c3339decSBarry Smith Collective 45530026cea9SSean Farley 45544165533cSJose E. Roman Input Parameters: 45550026cea9SSean Farley + ts - time stepping context 45560026cea9SSean Farley . t - time at which to evaluate 45570910c330SBarry Smith . U - state at which to evaluate 45580910c330SBarry Smith . Udot - time derivative of state vector 45590026cea9SSean Farley - ctx - context 45600026cea9SSean Farley 45614165533cSJose E. Roman Output Parameter: 45620026cea9SSean Farley . F - left hand side 45630026cea9SSean Farley 45640026cea9SSean Farley Level: intermediate 45650026cea9SSean Farley 45660026cea9SSean Farley Notes: 45670910c330SBarry 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 4568bcf0153eSBarry Smith user is required to write their own `TSComputeIFunction()`. 4569bcf0153eSBarry Smith This function is intended to be passed to `TSSetIFunction()` to evaluate the left hand side for linear problems. 4570bcf0153eSBarry Smith The matrix (and optionally the evaluation context) should be passed to `TSSetIJacobian()`. 45710026cea9SSean Farley 4572bcf0153eSBarry Smith Note that using this function is NOT equivalent to using `TSComputeRHSFunctionLinear()` since that solves Udot = A U 45739ae8fd06SBarry Smith 45741cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetIFunction()`, `TSSetIJacobian()`, `TSComputeIJacobianConstant()`, `TSComputeRHSFunctionLinear()` 45750026cea9SSean Farley @*/ 4576d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIFunctionLinear(TS ts, PetscReal t, Vec U, Vec Udot, Vec F, void *ctx) 4577d71ae5a4SJacob Faibussowitsch { 45780026cea9SSean Farley Mat A, B; 45790026cea9SSean Farley 45800026cea9SSean Farley PetscFunctionBegin; 45819566063dSJacob Faibussowitsch PetscCall(TSGetIJacobian(ts, &A, &B, NULL, NULL)); 45829566063dSJacob Faibussowitsch PetscCall(TSComputeIJacobian(ts, t, U, Udot, 1.0, A, B, PETSC_TRUE)); 45839566063dSJacob Faibussowitsch PetscCall(MatMult(A, Udot, F)); 45843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 45850026cea9SSean Farley } 45860026cea9SSean Farley 45870026cea9SSean Farley /*@C 45888434afd1SBarry Smith TSComputeIJacobianConstant - Reuses the matrix previously computed with the provided `TSIJacobianFn` for a semi-implicit DAE or ODE 45890026cea9SSean Farley 4590c3339decSBarry Smith Collective 45910026cea9SSean Farley 45924165533cSJose E. Roman Input Parameters: 45930026cea9SSean Farley + ts - time stepping context 45940026cea9SSean Farley . t - time at which to evaluate 45950910c330SBarry Smith . U - state at which to evaluate 45960910c330SBarry Smith . Udot - time derivative of state vector 45970026cea9SSean Farley . shift - shift to apply 45980026cea9SSean Farley - ctx - context 45990026cea9SSean Farley 46004165533cSJose E. Roman Output Parameters: 46010026cea9SSean Farley + A - pointer to operator 4602d1c5d1fcSBarry Smith - B - pointer to matrix from which the preconditioner is built (often `A`) 46030026cea9SSean Farley 4604b41af12eSJed Brown Level: advanced 46050026cea9SSean Farley 46060026cea9SSean Farley Notes: 4607bcf0153eSBarry Smith This function is intended to be passed to `TSSetIJacobian()` to evaluate the Jacobian for linear time-independent problems. 46080026cea9SSean Farley 4609b41af12eSJed Brown It is only appropriate for problems of the form 4610b41af12eSJed Brown 4611d1c5d1fcSBarry Smith $$ 4612d1c5d1fcSBarry Smith M \dot{U} = F(U,t) 4613d1c5d1fcSBarry Smith $$ 4614b41af12eSJed Brown 4615bcf0153eSBarry Smith where M is constant and F is non-stiff. The user must pass M to `TSSetIJacobian()`. The current implementation only 4616bcf0153eSBarry Smith works with IMEX time integration methods such as `TSROSW` and `TSARKIMEX`, since there is no support for de-constructing 4617b41af12eSJed Brown an implicit operator of the form 4618b41af12eSJed Brown 4619d1c5d1fcSBarry Smith $$ 4620d1c5d1fcSBarry Smith shift*M + J 4621d1c5d1fcSBarry Smith $$ 4622b41af12eSJed Brown 4623b41af12eSJed 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 4624b41af12eSJed Brown a copy of M or reassemble it when requested. 4625b41af12eSJed Brown 46261cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSROSW`, `TSARKIMEX`, `TSSetIFunction()`, `TSSetIJacobian()`, `TSComputeIFunctionLinear()` 46270026cea9SSean Farley @*/ 4628d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIJacobianConstant(TS ts, PetscReal t, Vec U, Vec Udot, PetscReal shift, Mat A, Mat B, void *ctx) 4629d71ae5a4SJacob Faibussowitsch { 46300026cea9SSean Farley PetscFunctionBegin; 46319566063dSJacob Faibussowitsch PetscCall(MatScale(A, shift / ts->ijacobian.shift)); 4632b41af12eSJed Brown ts->ijacobian.shift = shift; 46333ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 46340026cea9SSean Farley } 4635b41af12eSJed Brown 4636e817cc15SEmil Constantinescu /*@ 4637bcf0153eSBarry Smith TSGetEquationType - Gets the type of the equation that `TS` is solving. 4638e817cc15SEmil Constantinescu 4639e817cc15SEmil Constantinescu Not Collective 4640e817cc15SEmil Constantinescu 4641e817cc15SEmil Constantinescu Input Parameter: 4642bcf0153eSBarry Smith . ts - the `TS` context 4643e817cc15SEmil Constantinescu 4644e817cc15SEmil Constantinescu Output Parameter: 4645bcf0153eSBarry Smith . equation_type - see `TSEquationType` 4646e817cc15SEmil Constantinescu 4647e817cc15SEmil Constantinescu Level: beginner 4648e817cc15SEmil Constantinescu 46491cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetEquationType()`, `TSEquationType` 4650e817cc15SEmil Constantinescu @*/ 4651d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetEquationType(TS ts, TSEquationType *equation_type) 4652d71ae5a4SJacob Faibussowitsch { 4653e817cc15SEmil Constantinescu PetscFunctionBegin; 4654e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 46554f572ea9SToby Isaac PetscAssertPointer(equation_type, 2); 4656e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 46573ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4658e817cc15SEmil Constantinescu } 4659e817cc15SEmil Constantinescu 4660e817cc15SEmil Constantinescu /*@ 4661bcf0153eSBarry Smith TSSetEquationType - Sets the type of the equation that `TS` is solving. 4662e817cc15SEmil Constantinescu 4663e817cc15SEmil Constantinescu Not Collective 4664e817cc15SEmil Constantinescu 4665d8d19677SJose E. Roman Input Parameters: 4666bcf0153eSBarry Smith + ts - the `TS` context 4667bcf0153eSBarry Smith - equation_type - see `TSEquationType` 4668e817cc15SEmil Constantinescu 4669e817cc15SEmil Constantinescu Level: advanced 4670e817cc15SEmil Constantinescu 46711cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetEquationType()`, `TSEquationType` 4672e817cc15SEmil Constantinescu @*/ 4673d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetEquationType(TS ts, TSEquationType equation_type) 4674d71ae5a4SJacob Faibussowitsch { 4675e817cc15SEmil Constantinescu PetscFunctionBegin; 4676e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4677e817cc15SEmil Constantinescu ts->equation_type = equation_type; 46783ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4679e817cc15SEmil Constantinescu } 46800026cea9SSean Farley 46814af1b03aSJed Brown /*@ 4682bcf0153eSBarry Smith TSGetConvergedReason - Gets the reason the `TS` iteration was stopped. 46834af1b03aSJed Brown 46844af1b03aSJed Brown Not Collective 46854af1b03aSJed Brown 46864af1b03aSJed Brown Input Parameter: 4687bcf0153eSBarry Smith . ts - the `TS` context 46884af1b03aSJed Brown 46894af1b03aSJed Brown Output Parameter: 4690bcf0153eSBarry Smith . reason - negative value indicates diverged, positive value converged, see `TSConvergedReason` or the 46914af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 46924af1b03aSJed Brown 4693487e0bb9SJed Brown Level: beginner 46944af1b03aSJed Brown 4695bcf0153eSBarry Smith Note: 4696bcf0153eSBarry Smith Can only be called after the call to `TSSolve()` is complete. 46974af1b03aSJed Brown 46987d66147cSPierre Jolivet .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSConvergedReason` 46994af1b03aSJed Brown @*/ 4700d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetConvergedReason(TS ts, TSConvergedReason *reason) 4701d71ae5a4SJacob Faibussowitsch { 47024af1b03aSJed Brown PetscFunctionBegin; 47034af1b03aSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 47044f572ea9SToby Isaac PetscAssertPointer(reason, 2); 47054af1b03aSJed Brown *reason = ts->reason; 47063ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 47074af1b03aSJed Brown } 47084af1b03aSJed Brown 4709d6ad946cSShri Abhyankar /*@ 4710bcf0153eSBarry Smith TSSetConvergedReason - Sets the reason for handling the convergence of `TSSolve()`. 4711d6ad946cSShri Abhyankar 47126b221cbeSPatrick Sanan Logically Collective; reason must contain common value 4713d6ad946cSShri Abhyankar 47146b221cbeSPatrick Sanan Input Parameters: 4715bcf0153eSBarry Smith + ts - the `TS` context 4716bcf0153eSBarry Smith - reason - negative value indicates diverged, positive value converged, see `TSConvergedReason` or the 4717d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 4718d6ad946cSShri Abhyankar 4719f5abba47SShri Abhyankar Level: advanced 4720d6ad946cSShri Abhyankar 4721bcf0153eSBarry Smith Note: 4722bcf0153eSBarry Smith Can only be called while `TSSolve()` is active. 4723d6ad946cSShri Abhyankar 47241cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSConvergedReason` 4725d6ad946cSShri Abhyankar @*/ 4726d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetConvergedReason(TS ts, TSConvergedReason reason) 4727d71ae5a4SJacob Faibussowitsch { 4728d6ad946cSShri Abhyankar PetscFunctionBegin; 4729d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4730d6ad946cSShri Abhyankar ts->reason = reason; 47313ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4732d6ad946cSShri Abhyankar } 4733d6ad946cSShri Abhyankar 4734cc708dedSBarry Smith /*@ 4735bcf0153eSBarry Smith TSGetSolveTime - Gets the time after a call to `TSSolve()` 4736cc708dedSBarry Smith 4737cc708dedSBarry Smith Not Collective 4738cc708dedSBarry Smith 4739cc708dedSBarry Smith Input Parameter: 4740bcf0153eSBarry Smith . ts - the `TS` context 4741cc708dedSBarry Smith 4742cc708dedSBarry Smith Output Parameter: 4743bcf0153eSBarry Smith . ftime - the final time. This time corresponds to the final time set with `TSSetMaxTime()` 4744cc708dedSBarry Smith 4745487e0bb9SJed Brown Level: beginner 4746cc708dedSBarry Smith 4747bcf0153eSBarry Smith Note: 4748bcf0153eSBarry Smith Can only be called after the call to `TSSolve()` is complete. 4749cc708dedSBarry Smith 47507d66147cSPierre Jolivet .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSConvergedReason` 4751cc708dedSBarry Smith @*/ 4752d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSolveTime(TS ts, PetscReal *ftime) 4753d71ae5a4SJacob Faibussowitsch { 4754cc708dedSBarry Smith PetscFunctionBegin; 4755cc708dedSBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 47564f572ea9SToby Isaac PetscAssertPointer(ftime, 2); 4757cc708dedSBarry Smith *ftime = ts->solvetime; 47583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4759cc708dedSBarry Smith } 4760cc708dedSBarry Smith 47612c18e0fdSBarry Smith /*@ 47625ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 47639f67acb7SJed Brown used by the time integrator. 47649f67acb7SJed Brown 47659f67acb7SJed Brown Not Collective 47669f67acb7SJed Brown 47679f67acb7SJed Brown Input Parameter: 4768bcf0153eSBarry Smith . ts - `TS` context 47699f67acb7SJed Brown 47709f67acb7SJed Brown Output Parameter: 47719f67acb7SJed Brown . nits - number of nonlinear iterations 47729f67acb7SJed Brown 47739f67acb7SJed Brown Level: intermediate 47749f67acb7SJed Brown 4775195e9b02SBarry Smith Note: 4776bcf0153eSBarry Smith This counter is reset to zero for each successive call to `TSSolve()`. 4777bcf0153eSBarry Smith 47781cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSGetKSPIterations()` 47799f67acb7SJed Brown @*/ 4780d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSNESIterations(TS ts, PetscInt *nits) 4781d71ae5a4SJacob Faibussowitsch { 47829f67acb7SJed Brown PetscFunctionBegin; 47839f67acb7SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 47844f572ea9SToby Isaac PetscAssertPointer(nits, 2); 47855ef26d82SJed Brown *nits = ts->snes_its; 47863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 47879f67acb7SJed Brown } 47889f67acb7SJed Brown 47899f67acb7SJed Brown /*@ 47905ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 47919f67acb7SJed Brown used by the time integrator. 47929f67acb7SJed Brown 47939f67acb7SJed Brown Not Collective 47949f67acb7SJed Brown 47959f67acb7SJed Brown Input Parameter: 4796bcf0153eSBarry Smith . ts - `TS` context 47979f67acb7SJed Brown 47989f67acb7SJed Brown Output Parameter: 47999f67acb7SJed Brown . lits - number of linear iterations 48009f67acb7SJed Brown 48019f67acb7SJed Brown Level: intermediate 48029f67acb7SJed Brown 4803bcf0153eSBarry Smith Note: 4804bcf0153eSBarry Smith This counter is reset to zero for each successive call to `TSSolve()`. 4805bcf0153eSBarry Smith 48061cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSGetSNESIterations()`, `SNESGetKSPIterations()` 48079f67acb7SJed Brown @*/ 4808d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetKSPIterations(TS ts, PetscInt *lits) 4809d71ae5a4SJacob Faibussowitsch { 48109f67acb7SJed Brown PetscFunctionBegin; 48119f67acb7SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 48124f572ea9SToby Isaac PetscAssertPointer(lits, 2); 48135ef26d82SJed Brown *lits = ts->ksp_its; 48143ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 48159f67acb7SJed Brown } 48169f67acb7SJed Brown 4817cef5090cSJed Brown /*@ 4818cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 4819cef5090cSJed Brown 4820cef5090cSJed Brown Not Collective 4821cef5090cSJed Brown 4822cef5090cSJed Brown Input Parameter: 4823bcf0153eSBarry Smith . ts - `TS` context 4824cef5090cSJed Brown 4825cef5090cSJed Brown Output Parameter: 4826cef5090cSJed Brown . rejects - number of steps rejected 4827cef5090cSJed Brown 4828cef5090cSJed Brown Level: intermediate 4829cef5090cSJed Brown 4830bcf0153eSBarry Smith Note: 4831bcf0153eSBarry Smith This counter is reset to zero for each successive call to `TSSolve()`. 4832bcf0153eSBarry Smith 48331cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetSNESFailures()`, `TSSetMaxSNESFailures()`, `TSSetErrorIfStepFails()` 4834cef5090cSJed Brown @*/ 4835d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetStepRejections(TS ts, PetscInt *rejects) 4836d71ae5a4SJacob Faibussowitsch { 4837cef5090cSJed Brown PetscFunctionBegin; 4838cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 48394f572ea9SToby Isaac PetscAssertPointer(rejects, 2); 4840cef5090cSJed Brown *rejects = ts->reject; 48413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4842cef5090cSJed Brown } 4843cef5090cSJed Brown 4844cef5090cSJed Brown /*@ 4845bcf0153eSBarry Smith TSGetSNESFailures - Gets the total number of failed `SNES` solves in a `TS` 4846cef5090cSJed Brown 4847cef5090cSJed Brown Not Collective 4848cef5090cSJed Brown 4849cef5090cSJed Brown Input Parameter: 4850bcf0153eSBarry Smith . ts - `TS` context 4851cef5090cSJed Brown 4852cef5090cSJed Brown Output Parameter: 4853cef5090cSJed Brown . fails - number of failed nonlinear solves 4854cef5090cSJed Brown 4855cef5090cSJed Brown Level: intermediate 4856cef5090cSJed Brown 4857bcf0153eSBarry Smith Note: 4858bcf0153eSBarry Smith This counter is reset to zero for each successive call to `TSSolve()`. 4859bcf0153eSBarry Smith 48601cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSSetMaxSNESFailures()` 4861cef5090cSJed Brown @*/ 4862d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSNESFailures(TS ts, PetscInt *fails) 4863d71ae5a4SJacob Faibussowitsch { 4864cef5090cSJed Brown PetscFunctionBegin; 4865cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 48664f572ea9SToby Isaac PetscAssertPointer(fails, 2); 4867cef5090cSJed Brown *fails = ts->num_snes_failures; 48683ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4869cef5090cSJed Brown } 4870cef5090cSJed Brown 4871cef5090cSJed Brown /*@ 4872bcf0153eSBarry Smith TSSetMaxStepRejections - Sets the maximum number of step rejections before a time step fails 4873cef5090cSJed Brown 4874cef5090cSJed Brown Not Collective 4875cef5090cSJed Brown 4876d8d19677SJose E. Roman Input Parameters: 4877bcf0153eSBarry Smith + ts - `TS` context 487809cb0f53SBarry Smith - rejects - maximum number of rejected steps, pass `PETSC_UNLIMITED` for unlimited 4879cef5090cSJed Brown 4880cef5090cSJed Brown Options Database Key: 4881cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 4882cef5090cSJed Brown 4883cef5090cSJed Brown Level: intermediate 4884cef5090cSJed Brown 488509cb0f53SBarry Smith Developer Note: 488609cb0f53SBarry Smith The options database name is incorrect. 488709cb0f53SBarry Smith 48881cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxSNESFailures()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `TSSetErrorIfStepFails()`, `TSGetConvergedReason()` 4889cef5090cSJed Brown @*/ 4890d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxStepRejections(TS ts, PetscInt rejects) 4891d71ae5a4SJacob Faibussowitsch { 4892cef5090cSJed Brown PetscFunctionBegin; 4893cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 489409cb0f53SBarry Smith if (rejects == PETSC_UNLIMITED || rejects == -1) { 489509cb0f53SBarry Smith ts->max_reject = PETSC_UNLIMITED; 489609cb0f53SBarry Smith } else { 489709cb0f53SBarry Smith PetscCheck(rejects >= 0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Cannot have a negative maximum number of rejections"); 4898cef5090cSJed Brown ts->max_reject = rejects; 489909cb0f53SBarry Smith } 49003ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4901cef5090cSJed Brown } 4902cef5090cSJed Brown 4903cef5090cSJed Brown /*@ 4904bcf0153eSBarry Smith TSSetMaxSNESFailures - Sets the maximum number of failed `SNES` solves 4905cef5090cSJed Brown 4906cef5090cSJed Brown Not Collective 4907cef5090cSJed Brown 4908d8d19677SJose E. Roman Input Parameters: 4909bcf0153eSBarry Smith + ts - `TS` context 491009cb0f53SBarry Smith - fails - maximum number of failed nonlinear solves, pass `PETSC_UNLIMITED` to allow any number of failures. 4911cef5090cSJed Brown 4912cef5090cSJed Brown Options Database Key: 4913cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 4914cef5090cSJed Brown 4915cef5090cSJed Brown Level: intermediate 4916cef5090cSJed Brown 49171cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `SNESGetConvergedReason()`, `TSGetConvergedReason()` 4918cef5090cSJed Brown @*/ 4919d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxSNESFailures(TS ts, PetscInt fails) 4920d71ae5a4SJacob Faibussowitsch { 4921cef5090cSJed Brown PetscFunctionBegin; 4922cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 492309cb0f53SBarry Smith if (fails == PETSC_UNLIMITED || fails == -1) { 492409cb0f53SBarry Smith ts->max_snes_failures = PETSC_UNLIMITED; 492509cb0f53SBarry Smith } else { 492609cb0f53SBarry Smith PetscCheck(fails >= 0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Cannot have a negative maximum number of failures"); 4927cef5090cSJed Brown ts->max_snes_failures = fails; 492809cb0f53SBarry Smith } 49293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4930cef5090cSJed Brown } 4931cef5090cSJed Brown 4932cef5090cSJed Brown /*@ 4933195e9b02SBarry Smith TSSetErrorIfStepFails - Immediately error if no step succeeds during `TSSolve()` 4934cef5090cSJed Brown 4935cef5090cSJed Brown Not Collective 4936cef5090cSJed Brown 4937d8d19677SJose E. Roman Input Parameters: 4938bcf0153eSBarry Smith + ts - `TS` context 4939bcf0153eSBarry Smith - err - `PETSC_TRUE` to error if no step succeeds, `PETSC_FALSE` to return without failure 4940cef5090cSJed Brown 4941cef5090cSJed Brown Options Database Key: 4942cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 4943cef5090cSJed Brown 4944cef5090cSJed Brown Level: intermediate 4945cef5090cSJed Brown 494642747ad1SJacob Faibussowitsch .seealso: [](ch_ts), `TS`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `TSGetConvergedReason()` 4947cef5090cSJed Brown @*/ 4948d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetErrorIfStepFails(TS ts, PetscBool err) 4949d71ae5a4SJacob Faibussowitsch { 4950cef5090cSJed Brown PetscFunctionBegin; 4951cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4952cef5090cSJed Brown ts->errorifstepfailed = err; 49533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4954cef5090cSJed Brown } 4955cef5090cSJed Brown 495684df9cb4SJed Brown /*@ 4957552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 495884df9cb4SJed Brown 49598f14a041SBarry Smith Collective if controller has not yet been created 496084df9cb4SJed Brown 49614165533cSJose E. Roman Input Parameter: 4962ed81e22dSJed Brown . ts - time stepping context 496384df9cb4SJed Brown 49644165533cSJose E. Roman Output Parameter: 4965ed81e22dSJed Brown . adapt - adaptive controller 496684df9cb4SJed Brown 496784df9cb4SJed Brown Level: intermediate 496884df9cb4SJed Brown 49691cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSAdapt`, `TSAdaptSetType()`, `TSAdaptChoose()` 497084df9cb4SJed Brown @*/ 4971d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetAdapt(TS ts, TSAdapt *adapt) 4972d71ae5a4SJacob Faibussowitsch { 497384df9cb4SJed Brown PetscFunctionBegin; 497484df9cb4SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 49754f572ea9SToby Isaac PetscAssertPointer(adapt, 2); 497684df9cb4SJed Brown if (!ts->adapt) { 49779566063dSJacob Faibussowitsch PetscCall(TSAdaptCreate(PetscObjectComm((PetscObject)ts), &ts->adapt)); 49789566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)ts->adapt, (PetscObject)ts, 1)); 497984df9cb4SJed Brown } 4980bec58848SLisandro Dalcin *adapt = ts->adapt; 49813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 498284df9cb4SJed Brown } 4983d6ebe24aSShri Abhyankar 49841c3436cfSJed Brown /*@ 4985195e9b02SBarry Smith TSSetTolerances - Set tolerances for local truncation error when using an adaptive controller 49861c3436cfSJed Brown 49871c3436cfSJed Brown Logically Collective 49881c3436cfSJed Brown 49894165533cSJose E. Roman Input Parameters: 49901c3436cfSJed Brown + ts - time integration context 499109cb0f53SBarry Smith . atol - scalar absolute tolerances 499209cb0f53SBarry Smith . vatol - vector of absolute tolerances or `NULL`, used in preference to `atol` if present 499309cb0f53SBarry Smith . rtol - scalar relative tolerances 499409cb0f53SBarry Smith - vrtol - vector of relative tolerances or `NULL`, used in preference to `rtol` if present 49951c3436cfSJed Brown 4996195e9b02SBarry Smith Options Database Keys: 4997a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 499867b8a455SSatish Balay - -ts_atol <atol> - Absolute tolerance for local truncation error 4999a3cdaa26SBarry Smith 5000bcf0153eSBarry Smith Level: beginner 5001bcf0153eSBarry Smith 50023ff766beSShri Abhyankar Notes: 500309cb0f53SBarry Smith `PETSC_CURRENT` or `PETSC_DETERMINE` may be used for `atol` or `rtol` to indicate the current value 500409cb0f53SBarry Smith or the default value from when the object's type was set. 500509cb0f53SBarry Smith 50063ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 50073ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 50083ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 50093ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 50103ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 50113ff766beSShri Abhyankar differential variables. 50123ff766beSShri Abhyankar 501309cb0f53SBarry Smith Fortran Note: 501409cb0f53SBarry Smith Use `PETSC_CURRENT_INTEGER` or `PETSC_DETERMINE_INTEGER`. 501509cb0f53SBarry Smith 50161cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSAdapt`, `TSErrorWeightedNorm()`, `TSGetTolerances()` 50171c3436cfSJed Brown @*/ 5018d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTolerances(TS ts, PetscReal atol, Vec vatol, PetscReal rtol, Vec vrtol) 5019d71ae5a4SJacob Faibussowitsch { 50201c3436cfSJed Brown PetscFunctionBegin; 502109cb0f53SBarry Smith if (atol == (PetscReal)PETSC_DETERMINE) { 502209cb0f53SBarry Smith ts->atol = atol = ts->default_atol; 502309cb0f53SBarry Smith } else if (atol != (PetscReal)PETSC_CURRENT) { 502409cb0f53SBarry Smith PetscCheck(atol >= 0.0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Absolute tolerance %g must be non-negative", (double)atol); 502509cb0f53SBarry Smith ts->atol = atol; 502609cb0f53SBarry Smith } 502709cb0f53SBarry Smith 50281c3436cfSJed Brown if (vatol) { 50299566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)vatol)); 50309566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vatol)); 50311c3436cfSJed Brown ts->vatol = vatol; 50321c3436cfSJed Brown } 503309cb0f53SBarry Smith 503409cb0f53SBarry Smith if (rtol == (PetscReal)PETSC_DETERMINE) { 503509cb0f53SBarry Smith ts->rtol = atol = ts->default_rtol; 503609cb0f53SBarry Smith } else if (rtol != (PetscReal)PETSC_CURRENT) { 503709cb0f53SBarry Smith PetscCheck(rtol >= 0.0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Relative tolerance %g must be non-negative", (double)rtol); 503809cb0f53SBarry Smith ts->rtol = rtol; 503909cb0f53SBarry Smith } 504009cb0f53SBarry Smith 50411c3436cfSJed Brown if (vrtol) { 50429566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)vrtol)); 50439566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vrtol)); 50441c3436cfSJed Brown ts->vrtol = vrtol; 50451c3436cfSJed Brown } 50463ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 50471c3436cfSJed Brown } 50481c3436cfSJed Brown 5049c5033834SJed Brown /*@ 5050c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 5051c5033834SJed Brown 5052c5033834SJed Brown Logically Collective 5053c5033834SJed Brown 50544165533cSJose E. Roman Input Parameter: 5055c5033834SJed Brown . ts - time integration context 5056c5033834SJed Brown 50574165533cSJose E. Roman Output Parameters: 5058195e9b02SBarry Smith + atol - scalar absolute tolerances, `NULL` to ignore 5059195e9b02SBarry Smith . vatol - vector of absolute tolerances, `NULL` to ignore 5060195e9b02SBarry Smith . rtol - scalar relative tolerances, `NULL` to ignore 5061195e9b02SBarry Smith - vrtol - vector of relative tolerances, `NULL` to ignore 5062c5033834SJed Brown 5063c5033834SJed Brown Level: beginner 5064c5033834SJed Brown 50651cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSAdapt`, `TSErrorWeightedNorm()`, `TSSetTolerances()` 5066c5033834SJed Brown @*/ 5067d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTolerances(TS ts, PetscReal *atol, Vec *vatol, PetscReal *rtol, Vec *vrtol) 5068d71ae5a4SJacob Faibussowitsch { 5069c5033834SJed Brown PetscFunctionBegin; 5070c5033834SJed Brown if (atol) *atol = ts->atol; 5071c5033834SJed Brown if (vatol) *vatol = ts->vatol; 5072c5033834SJed Brown if (rtol) *rtol = ts->rtol; 5073c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 50743ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5075c5033834SJed Brown } 5076c5033834SJed Brown 50779c6b16b5SShri Abhyankar /*@ 50788a175baeSEmil Constantinescu TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances 50791c3436cfSJed Brown 5080c3339decSBarry Smith Collective 50811c3436cfSJed Brown 50824165533cSJose E. Roman Input Parameters: 50831c3436cfSJed Brown + ts - time stepping context 5084a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5085a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 5086bcf0153eSBarry Smith - wnormtype - norm type, either `NORM_2` or `NORM_INFINITY` 50877619abb3SShri 50884165533cSJose E. Roman Output Parameters: 5089a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 50908a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 5091a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 5092a4868fbcSLisandro Dalcin 5093bcf0153eSBarry Smith Options Database Key: 5094a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 5095a4868fbcSLisandro Dalcin 50961c3436cfSJed Brown Level: developer 50971c3436cfSJed Brown 50988c38e02aSSatish Balay .seealso: [](ch_ts), `TS`, `VecErrorWeightedNorms()`, `TSErrorWeightedENorm()` 50991c3436cfSJed Brown @*/ 5100d71ae5a4SJacob Faibussowitsch PetscErrorCode TSErrorWeightedNorm(TS ts, Vec U, Vec Y, NormType wnormtype, PetscReal *norm, PetscReal *norma, PetscReal *normr) 5101d71ae5a4SJacob Faibussowitsch { 5102037d63e4SStefano Zampini PetscInt norma_loc, norm_loc, normr_loc; 51038a175baeSEmil Constantinescu 51048a175baeSEmil Constantinescu PetscFunctionBegin; 51058a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5106037d63e4SStefano 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)); 5107037d63e4SStefano Zampini if (wnormtype == NORM_2) { 5108037d63e4SStefano Zampini if (norm_loc) *norm = PetscSqrtReal(PetscSqr(*norm) / norm_loc); 5109037d63e4SStefano Zampini if (norma_loc) *norma = PetscSqrtReal(PetscSqr(*norma) / norma_loc); 5110037d63e4SStefano Zampini if (normr_loc) *normr = PetscSqrtReal(PetscSqr(*normr) / normr_loc); 51118a175baeSEmil Constantinescu } 51123c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norm"); 51133c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norma"); 51143c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in normr"); 51153ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 51168a175baeSEmil Constantinescu } 51178a175baeSEmil Constantinescu 51188a175baeSEmil Constantinescu /*@ 51198a175baeSEmil Constantinescu TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances 51208a175baeSEmil Constantinescu 5121c3339decSBarry Smith Collective 51228a175baeSEmil Constantinescu 51234165533cSJose E. Roman Input Parameters: 51248a175baeSEmil Constantinescu + ts - time stepping context 51258a175baeSEmil Constantinescu . E - error vector 51268a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 51278a175baeSEmil Constantinescu . Y - state vector, previous time step 5128bcf0153eSBarry Smith - wnormtype - norm type, either `NORM_2` or `NORM_INFINITY` 51298a175baeSEmil Constantinescu 51304165533cSJose E. Roman Output Parameters: 5131a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 51328a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 5133a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 51348a175baeSEmil Constantinescu 5135bcf0153eSBarry Smith Options Database Key: 51368a175baeSEmil Constantinescu . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 51378a175baeSEmil Constantinescu 51388a175baeSEmil Constantinescu Level: developer 51398a175baeSEmil Constantinescu 51408c38e02aSSatish Balay .seealso: [](ch_ts), `TS`, `VecErrorWeightedNorms()`, `TSErrorWeightedNorm()` 51418a175baeSEmil Constantinescu @*/ 5142d71ae5a4SJacob Faibussowitsch PetscErrorCode TSErrorWeightedENorm(TS ts, Vec E, Vec U, Vec Y, NormType wnormtype, PetscReal *norm, PetscReal *norma, PetscReal *normr) 5143d71ae5a4SJacob Faibussowitsch { 5144037d63e4SStefano Zampini PetscInt norma_loc, norm_loc, normr_loc; 5145037d63e4SStefano Zampini 51468a175baeSEmil Constantinescu PetscFunctionBegin; 5147037d63e4SStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5148037d63e4SStefano 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)); 5149037d63e4SStefano Zampini if (wnormtype == NORM_2) { 5150037d63e4SStefano Zampini if (norm_loc) *norm = PetscSqrtReal(PetscSqr(*norm) / norm_loc); 5151037d63e4SStefano Zampini if (norma_loc) *norma = PetscSqrtReal(PetscSqr(*norma) / norma_loc); 5152037d63e4SStefano Zampini if (normr_loc) *normr = PetscSqrtReal(PetscSqr(*normr) / normr_loc); 5153037d63e4SStefano Zampini } 5154037d63e4SStefano Zampini PetscCheck(!PetscIsInfOrNanScalar(*norm), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norm"); 5155037d63e4SStefano Zampini PetscCheck(!PetscIsInfOrNanScalar(*norma), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norma"); 5156037d63e4SStefano Zampini PetscCheck(!PetscIsInfOrNanScalar(*normr), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in normr"); 51573ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 51588a175baeSEmil Constantinescu } 51598a175baeSEmil Constantinescu 51608d59e960SJed Brown /*@ 51618d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 51628d59e960SJed Brown 5163c3339decSBarry Smith Logically Collective 51648d59e960SJed Brown 51654165533cSJose E. Roman Input Parameters: 51668d59e960SJed Brown + ts - time stepping context 51678d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 51688d59e960SJed Brown 51698d59e960SJed Brown Note: 51708d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 51718d59e960SJed Brown 51728d59e960SJed Brown Level: intermediate 51738d59e960SJed Brown 51741cc06b55SBarry Smith .seealso: [](ch_ts), `TSGetCFLTime()`, `TSADAPTCFL` 51758d59e960SJed Brown @*/ 5176d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetCFLTimeLocal(TS ts, PetscReal cfltime) 5177d71ae5a4SJacob Faibussowitsch { 51788d59e960SJed Brown PetscFunctionBegin; 51798d59e960SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 51808d59e960SJed Brown ts->cfltime_local = cfltime; 51818d59e960SJed Brown ts->cfltime = -1.; 51823ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 51838d59e960SJed Brown } 51848d59e960SJed Brown 51858d59e960SJed Brown /*@ 51868d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 51878d59e960SJed Brown 5188c3339decSBarry Smith Collective 51898d59e960SJed Brown 51904165533cSJose E. Roman Input Parameter: 51918d59e960SJed Brown . ts - time stepping context 51928d59e960SJed Brown 51934165533cSJose E. Roman Output Parameter: 51948d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 51958d59e960SJed Brown 51968d59e960SJed Brown Level: advanced 51978d59e960SJed Brown 51981cc06b55SBarry Smith .seealso: [](ch_ts), `TSSetCFLTimeLocal()` 51998d59e960SJed Brown @*/ 5200d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetCFLTime(TS ts, PetscReal *cfltime) 5201d71ae5a4SJacob Faibussowitsch { 52028d59e960SJed Brown PetscFunctionBegin; 52031e66621cSBarry Smith if (ts->cfltime < 0) PetscCall(MPIU_Allreduce(&ts->cfltime_local, &ts->cfltime, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)ts))); 52048d59e960SJed Brown *cfltime = ts->cfltime; 52053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 52068d59e960SJed Brown } 52078d59e960SJed Brown 5208d6ebe24aSShri Abhyankar /*@ 5209d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 5210d6ebe24aSShri Abhyankar 5211d6ebe24aSShri Abhyankar Input Parameters: 5212bcf0153eSBarry Smith + ts - the `TS` context. 5213d6ebe24aSShri Abhyankar . xl - lower bound. 5214a2b725a8SWilliam Gropp - xu - upper bound. 5215d6ebe24aSShri Abhyankar 52162bd2b0e6SSatish Balay Level: advanced 52172bd2b0e6SSatish Balay 5218bcf0153eSBarry Smith Note: 5219bcf0153eSBarry Smith If this routine is not called then the lower and upper bounds are set to 5220bcf0153eSBarry Smith `PETSC_NINFINITY` and `PETSC_INFINITY` respectively during `SNESSetUp()`. 5221bcf0153eSBarry Smith 52221cc06b55SBarry Smith .seealso: [](ch_ts), `TS` 5223d6ebe24aSShri Abhyankar @*/ 5224d71ae5a4SJacob Faibussowitsch PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 5225d71ae5a4SJacob Faibussowitsch { 5226d6ebe24aSShri Abhyankar SNES snes; 5227d6ebe24aSShri Abhyankar 5228d6ebe24aSShri Abhyankar PetscFunctionBegin; 52299566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 52309566063dSJacob Faibussowitsch PetscCall(SNESVISetVariableBounds(snes, xl, xu)); 52313ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5232d6ebe24aSShri Abhyankar } 5233d6ebe24aSShri Abhyankar 5234f9c1d6abSBarry Smith /*@ 5235f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 5236f9c1d6abSBarry Smith 5237c3339decSBarry Smith Collective 5238f9c1d6abSBarry Smith 5239f9c1d6abSBarry Smith Input Parameters: 5240bcf0153eSBarry Smith + ts - the `TS` context 52412fe279fdSBarry Smith . xr - real part of input argument 52422fe279fdSBarry Smith - xi - imaginary part of input argument 5243f9c1d6abSBarry Smith 5244f9c1d6abSBarry Smith Output Parameters: 52452fe279fdSBarry Smith + yr - real part of function value 52462fe279fdSBarry Smith - yi - imaginary part of function value 5247f9c1d6abSBarry Smith 5248f9c1d6abSBarry Smith Level: developer 5249f9c1d6abSBarry Smith 52501cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSFunction()`, `TSComputeIFunction()` 5251f9c1d6abSBarry Smith @*/ 5252d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeLinearStability(TS ts, PetscReal xr, PetscReal xi, PetscReal *yr, PetscReal *yi) 5253d71ae5a4SJacob Faibussowitsch { 5254f9c1d6abSBarry Smith PetscFunctionBegin; 5255f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5256dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, linearstability, xr, xi, yr, yi); 52573ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5258f9c1d6abSBarry Smith } 525924655328SShri 526024655328SShri /*@ 5261dcb233daSLisandro Dalcin TSRestartStep - Flags the solver to restart the next step 5262dcb233daSLisandro Dalcin 5263c3339decSBarry Smith Collective 5264dcb233daSLisandro Dalcin 5265dcb233daSLisandro Dalcin Input Parameter: 5266bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 5267dcb233daSLisandro Dalcin 5268dcb233daSLisandro Dalcin Level: advanced 5269dcb233daSLisandro Dalcin 5270dcb233daSLisandro Dalcin Notes: 5271bcf0153eSBarry Smith Multistep methods like `TSBDF` or Runge-Kutta methods with FSAL property require restarting the solver in the event of 5272dcb233daSLisandro Dalcin discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution 5273dcb233daSLisandro Dalcin vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For 5274bcf0153eSBarry Smith the sake of correctness and maximum safety, users are expected to call `TSRestart()` whenever they introduce 5275dcb233daSLisandro Dalcin discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with 5276dcb233daSLisandro Dalcin discontinuous source terms). 5277dcb233daSLisandro Dalcin 52781cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSBDF`, `TSSolve()`, `TSSetPreStep()`, `TSSetPostStep()` 5279dcb233daSLisandro Dalcin @*/ 5280d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRestartStep(TS ts) 5281d71ae5a4SJacob Faibussowitsch { 5282dcb233daSLisandro Dalcin PetscFunctionBegin; 5283dcb233daSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5284dcb233daSLisandro Dalcin ts->steprestart = PETSC_TRUE; 52853ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5286dcb233daSLisandro Dalcin } 5287dcb233daSLisandro Dalcin 5288dcb233daSLisandro Dalcin /*@ 528924655328SShri TSRollBack - Rolls back one time step 529024655328SShri 5291c3339decSBarry Smith Collective 529224655328SShri 529324655328SShri Input Parameter: 5294bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 529524655328SShri 529624655328SShri Level: advanced 529724655328SShri 52989dc50cb5SStefano Zampini .seealso: [](ch_ts), `TS`, `TSGetStepRollBack()`, `TSCreate()`, `TSSetUp()`, `TSDestroy()`, `TSSolve()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSInterpolate()` 529924655328SShri @*/ 5300d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRollBack(TS ts) 5301d71ae5a4SJacob Faibussowitsch { 530224655328SShri PetscFunctionBegin; 530324655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 53043c633725SBarry Smith PetscCheck(!ts->steprollback, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "TSRollBack already called"); 5305c61711c8SStefano Zampini PetscTryTypeMethod(ts, rollback); 5306c61711c8SStefano Zampini PetscCall(VecCopy(ts->vec_sol0, ts->vec_sol)); 530724655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 530824655328SShri ts->ptime = ts->ptime_prev; 5309be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime_prev_rollback; 53102808aa04SLisandro Dalcin ts->steps--; 5311b3de5cdeSLisandro Dalcin ts->steprollback = PETSC_TRUE; 53123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 531324655328SShri } 5314aeb4809dSShri Abhyankar 5315ff22ae23SHong Zhang /*@ 53169dc50cb5SStefano Zampini TSGetStepRollBack - Get the internal flag indicating if you are rolling back a step 53179dc50cb5SStefano Zampini 53189dc50cb5SStefano Zampini Not collective 53199dc50cb5SStefano Zampini 53209dc50cb5SStefano Zampini Input Parameter: 53219dc50cb5SStefano Zampini . ts - the `TS` context obtained from `TSCreate()` 53229dc50cb5SStefano Zampini 53239dc50cb5SStefano Zampini Output Parameter: 53249dc50cb5SStefano Zampini . flg - the rollback flag 53259dc50cb5SStefano Zampini 53269dc50cb5SStefano Zampini Level: advanced 53279dc50cb5SStefano Zampini 53289dc50cb5SStefano Zampini .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSRollBack()` 53299dc50cb5SStefano Zampini @*/ 53309dc50cb5SStefano Zampini PetscErrorCode TSGetStepRollBack(TS ts, PetscBool *flg) 53319dc50cb5SStefano Zampini { 53329dc50cb5SStefano Zampini PetscFunctionBegin; 53339dc50cb5SStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 53349dc50cb5SStefano Zampini PetscAssertPointer(flg, 2); 53359dc50cb5SStefano Zampini *flg = ts->steprollback; 53369dc50cb5SStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 53379dc50cb5SStefano Zampini } 53389dc50cb5SStefano Zampini 53399dc50cb5SStefano Zampini /*@ 5340ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 5341ff22ae23SHong Zhang 5342ff22ae23SHong Zhang Input Parameter: 5343bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 5344ff22ae23SHong Zhang 53450429704eSStefano Zampini Output Parameters: 53460429704eSStefano Zampini + ns - the number of stages 53470429704eSStefano Zampini - Y - the current stage vectors 53480429704eSStefano Zampini 5349ff22ae23SHong Zhang Level: advanced 5350ff22ae23SHong Zhang 5351bcf0153eSBarry Smith Note: 5352195e9b02SBarry Smith Both `ns` and `Y` can be `NULL`. 53530429704eSStefano Zampini 53541cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSCreate()` 5355ff22ae23SHong Zhang @*/ 5356d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetStages(TS ts, PetscInt *ns, Vec **Y) 5357d71ae5a4SJacob Faibussowitsch { 5358ff22ae23SHong Zhang PetscFunctionBegin; 5359ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 53604f572ea9SToby Isaac if (ns) PetscAssertPointer(ns, 2); 53614f572ea9SToby Isaac if (Y) PetscAssertPointer(Y, 3); 53620429704eSStefano Zampini if (!ts->ops->getstages) { 53630429704eSStefano Zampini if (ns) *ns = 0; 53640429704eSStefano Zampini if (Y) *Y = NULL; 5365dbbe0bcdSBarry Smith } else PetscUseTypeMethod(ts, getstages, ns, Y); 53663ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5367ff22ae23SHong Zhang } 5368ff22ae23SHong Zhang 5369847ff0e1SMatthew G. Knepley /*@C 5370847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 5371847ff0e1SMatthew G. Knepley 5372c3339decSBarry Smith Collective 5373847ff0e1SMatthew G. Knepley 5374847ff0e1SMatthew G. Knepley Input Parameters: 5375bcf0153eSBarry Smith + ts - the `TS` context 5376847ff0e1SMatthew G. Knepley . t - current timestep 5377847ff0e1SMatthew G. Knepley . U - state vector 5378847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 5379847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 5380847ff0e1SMatthew G. Knepley - ctx - an optional user context 5381847ff0e1SMatthew G. Knepley 5382847ff0e1SMatthew G. Knepley Output Parameters: 5383847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 5384195e9b02SBarry Smith - B - newly computed Jacobian matrix to use with preconditioner (generally the same as `J`) 5385847ff0e1SMatthew G. Knepley 5386847ff0e1SMatthew G. Knepley Level: intermediate 5387847ff0e1SMatthew G. Knepley 5388847ff0e1SMatthew G. Knepley Notes: 5389847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 5390847ff0e1SMatthew G. Knepley 5391847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 5392847ff0e1SMatthew G. Knepley 5393847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 5394847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 5395847ff0e1SMatthew G. Knepley 5396bcf0153eSBarry Smith This will first try to get the coloring from the `DM`. If the `DM` type has no coloring 5397847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 5398847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 5399847ff0e1SMatthew G. Knepley 54001cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetIJacobian()`, `MatFDColoringCreate()`, `MatFDColoringSetFunction()` 5401847ff0e1SMatthew G. Knepley @*/ 5402d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIJacobianDefaultColor(TS ts, PetscReal t, Vec U, Vec Udot, PetscReal shift, Mat J, Mat B, void *ctx) 5403d71ae5a4SJacob Faibussowitsch { 5404847ff0e1SMatthew G. Knepley SNES snes; 5405847ff0e1SMatthew G. Knepley MatFDColoring color; 5406847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 5407847ff0e1SMatthew G. Knepley 5408847ff0e1SMatthew G. Knepley PetscFunctionBegin; 54099566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject)ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL)); 54109566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)B, "TSMatFDColoring", (PetscObject *)&color)); 5411847ff0e1SMatthew G. Knepley if (!color) { 5412847ff0e1SMatthew G. Knepley DM dm; 5413847ff0e1SMatthew G. Knepley ISColoring iscoloring; 5414847ff0e1SMatthew G. Knepley 54159566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 54169566063dSJacob Faibussowitsch PetscCall(DMHasColoring(dm, &hascolor)); 5417847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 54189566063dSJacob Faibussowitsch PetscCall(DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring)); 54199566063dSJacob Faibussowitsch PetscCall(MatFDColoringCreate(B, iscoloring, &color)); 54209566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFunction(color, (PetscErrorCode(*)(void))SNESTSFormFunction, (void *)ts)); 54219566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFromOptions(color)); 54229566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetUp(B, iscoloring, color)); 54239566063dSJacob Faibussowitsch PetscCall(ISColoringDestroy(&iscoloring)); 5424847ff0e1SMatthew G. Knepley } else { 5425847ff0e1SMatthew G. Knepley MatColoring mc; 5426847ff0e1SMatthew G. Knepley 54279566063dSJacob Faibussowitsch PetscCall(MatColoringCreate(B, &mc)); 54289566063dSJacob Faibussowitsch PetscCall(MatColoringSetDistance(mc, 2)); 54299566063dSJacob Faibussowitsch PetscCall(MatColoringSetType(mc, MATCOLORINGSL)); 54309566063dSJacob Faibussowitsch PetscCall(MatColoringSetFromOptions(mc)); 54319566063dSJacob Faibussowitsch PetscCall(MatColoringApply(mc, &iscoloring)); 54329566063dSJacob Faibussowitsch PetscCall(MatColoringDestroy(&mc)); 54339566063dSJacob Faibussowitsch PetscCall(MatFDColoringCreate(B, iscoloring, &color)); 54349566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFunction(color, (PetscErrorCode(*)(void))SNESTSFormFunction, (void *)ts)); 54359566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFromOptions(color)); 54369566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetUp(B, iscoloring, color)); 54379566063dSJacob Faibussowitsch PetscCall(ISColoringDestroy(&iscoloring)); 5438847ff0e1SMatthew G. Knepley } 54399566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject)B, "TSMatFDColoring", (PetscObject)color)); 54409566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)color)); 5441847ff0e1SMatthew G. Knepley } 54429566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 54439566063dSJacob Faibussowitsch PetscCall(MatFDColoringApply(B, color, U, snes)); 5444847ff0e1SMatthew G. Knepley if (J != B) { 54459566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 54469566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 5447847ff0e1SMatthew G. Knepley } 54483ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5449847ff0e1SMatthew G. Knepley } 545093b34091SDebojyoti Ghosh 5451b43aa488SJacob Faibussowitsch /*@C 54526bc98fa9SBarry Smith TSSetFunctionDomainError - Set a function that tests if the current state vector is valid 5453cb9d8021SPierre Barbier de Reuille 5454cb9d8021SPierre Barbier de Reuille Input Parameters: 5455bcf0153eSBarry Smith + ts - the `TS` context 5456bcf0153eSBarry Smith - func - function called within `TSFunctionDomainError()` 54576bc98fa9SBarry Smith 545820f4b53cSBarry Smith Calling sequence of `func`: 54598847d985SBarry Smith + ts - the `TS` context 54606bc98fa9SBarry Smith . time - the current time (of the stage) 54616bc98fa9SBarry Smith . state - the state to check if it is valid 5462a144a63fSStefano Zampini - accept - (output parameter) `PETSC_FALSE` if the state is not acceptable, `PETSC_TRUE` if acceptable 5463cb9d8021SPierre Barbier de Reuille 5464cb9d8021SPierre Barbier de Reuille Level: intermediate 5465cb9d8021SPierre Barbier de Reuille 54666bc98fa9SBarry Smith Notes: 54676bc98fa9SBarry Smith If an implicit ODE solver is being used then, in addition to providing this routine, the 5468bcf0153eSBarry Smith user's code should call `SNESSetFunctionDomainError()` when domain errors occur during 5469bcf0153eSBarry Smith function evaluations where the functions are provided by `TSSetIFunction()` or `TSSetRHSFunction()`. 5470bcf0153eSBarry Smith Use `TSGetSNES()` to obtain the `SNES` object 54716bc98fa9SBarry Smith 5472b43aa488SJacob Faibussowitsch Developer Notes: 5473bcf0153eSBarry Smith The naming of this function is inconsistent with the `SNESSetFunctionDomainError()` 54746bc98fa9SBarry Smith since one takes a function pointer and the other does not. 54756bc98fa9SBarry Smith 54761cc06b55SBarry Smith .seealso: [](ch_ts), `TSAdaptCheckStage()`, `TSFunctionDomainError()`, `SNESSetFunctionDomainError()`, `TSGetSNES()` 5477cb9d8021SPierre Barbier de Reuille @*/ 5478a144a63fSStefano Zampini PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS ts, PetscReal time, Vec state, PetscBool *accept)) 5479d71ae5a4SJacob Faibussowitsch { 5480cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 5481cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5482cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 54833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5484cb9d8021SPierre Barbier de Reuille } 5485cb9d8021SPierre Barbier de Reuille 5486cb9d8021SPierre Barbier de Reuille /*@ 54876bc98fa9SBarry Smith TSFunctionDomainError - Checks if the current state is valid 5488cb9d8021SPierre Barbier de Reuille 5489cb9d8021SPierre Barbier de Reuille Input Parameters: 5490bcf0153eSBarry Smith + ts - the `TS` context 54916bc98fa9SBarry Smith . stagetime - time of the simulation 54926bc98fa9SBarry Smith - Y - state vector to check. 5493cb9d8021SPierre Barbier de Reuille 5494cb9d8021SPierre Barbier de Reuille Output Parameter: 5495bcf0153eSBarry Smith . accept - Set to `PETSC_FALSE` if the current state vector is valid. 549696a0c994SBarry Smith 54976bc98fa9SBarry Smith Level: developer 54986bc98fa9SBarry Smith 5499bcf0153eSBarry Smith Note: 5500bcf0153eSBarry Smith This function is called by the `TS` integration routines and calls the user provided function (set with `TSSetFunctionDomainError()`) 5501bcf0153eSBarry Smith to check if the current state is valid. 5502bcf0153eSBarry Smith 55031cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetFunctionDomainError()` 5504cb9d8021SPierre Barbier de Reuille @*/ 5505d71ae5a4SJacob Faibussowitsch PetscErrorCode TSFunctionDomainError(TS ts, PetscReal stagetime, Vec Y, PetscBool *accept) 5506d71ae5a4SJacob Faibussowitsch { 5507cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 5508cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5509cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 55101e66621cSBarry Smith if (ts->functiondomainerror) PetscCall((*ts->functiondomainerror)(ts, stagetime, Y, accept)); 55113ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5512cb9d8021SPierre Barbier de Reuille } 55131ceb14c0SBarry Smith 5514cc4c1da9SBarry Smith /*@ 5515195e9b02SBarry Smith TSClone - This function clones a time step `TS` object. 551693b34091SDebojyoti Ghosh 5517d083f849SBarry Smith Collective 551893b34091SDebojyoti Ghosh 551993b34091SDebojyoti Ghosh Input Parameter: 5520bcf0153eSBarry Smith . tsin - The input `TS` 552193b34091SDebojyoti Ghosh 552293b34091SDebojyoti Ghosh Output Parameter: 5523bcf0153eSBarry Smith . tsout - The output `TS` (cloned) 55245eca1a21SEmil Constantinescu 55255eca1a21SEmil Constantinescu Level: developer 552693b34091SDebojyoti Ghosh 5527bcf0153eSBarry Smith Notes: 5528bcf0153eSBarry 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. 5529bcf0153eSBarry Smith It will likely be replaced in the future with a mechanism of switching methods on the fly. 5530bcf0153eSBarry Smith 5531195e9b02SBarry 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 5532195e9b02SBarry Smith .vb 5533195e9b02SBarry Smith SNES snes_dup = NULL; 5534195e9b02SBarry Smith TSGetSNES(ts,&snes_dup); 5535195e9b02SBarry Smith TSSetSNES(ts,snes_dup); 5536195e9b02SBarry Smith .ve 5537bcf0153eSBarry Smith 55381cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `TSCreate()`, `TSSetType()`, `TSSetUp()`, `TSDestroy()`, `TSSetProblemType()` 553993b34091SDebojyoti Ghosh @*/ 5540d71ae5a4SJacob Faibussowitsch PetscErrorCode TSClone(TS tsin, TS *tsout) 5541d71ae5a4SJacob Faibussowitsch { 554293b34091SDebojyoti Ghosh TS t; 5543dc846ba4SSatish Balay SNES snes_start; 5544dc846ba4SSatish Balay DM dm; 5545dc846ba4SSatish Balay TSType type; 554693b34091SDebojyoti Ghosh 554793b34091SDebojyoti Ghosh PetscFunctionBegin; 55484f572ea9SToby Isaac PetscAssertPointer(tsin, 1); 554993b34091SDebojyoti Ghosh *tsout = NULL; 555093b34091SDebojyoti Ghosh 55519566063dSJacob Faibussowitsch PetscCall(PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView)); 555293b34091SDebojyoti Ghosh 555393b34091SDebojyoti Ghosh /* General TS description */ 555493b34091SDebojyoti Ghosh t->numbermonitors = 0; 5555d0c080abSJoseph Pusztay t->monitorFrequency = 1; 555693b34091SDebojyoti Ghosh t->setupcalled = 0; 555793b34091SDebojyoti Ghosh t->ksp_its = 0; 555893b34091SDebojyoti Ghosh t->snes_its = 0; 555993b34091SDebojyoti Ghosh t->nwork = 0; 55607d51462cSStefano Zampini t->rhsjacobian.time = PETSC_MIN_REAL; 556193b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 556293b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 556393b34091SDebojyoti Ghosh 55649566063dSJacob Faibussowitsch PetscCall(TSGetSNES(tsin, &snes_start)); 55659566063dSJacob Faibussowitsch PetscCall(TSSetSNES(t, snes_start)); 5566d15a3a53SEmil Constantinescu 55679566063dSJacob Faibussowitsch PetscCall(TSGetDM(tsin, &dm)); 55689566063dSJacob Faibussowitsch PetscCall(TSSetDM(t, dm)); 556993b34091SDebojyoti Ghosh 557093b34091SDebojyoti Ghosh t->adapt = tsin->adapt; 55719566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)t->adapt)); 557293b34091SDebojyoti Ghosh 5573e7069c78SShri t->trajectory = tsin->trajectory; 55749566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)t->trajectory)); 5575e7069c78SShri 5576e7069c78SShri t->event = tsin->event; 55776b10a48eSSatish Balay if (t->event) t->event->refct++; 5578e7069c78SShri 557993b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 558093b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 5581e7069c78SShri t->ptime_prev = tsin->ptime_prev; 558293b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 558393b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 558493b34091SDebojyoti Ghosh t->steps = tsin->steps; 558593b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 558693b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 558793b34091SDebojyoti Ghosh t->atol = tsin->atol; 558893b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 558993b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 559093b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 559193b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 559293b34091SDebojyoti Ghosh 55939566063dSJacob Faibussowitsch PetscCall(TSGetType(tsin, &type)); 55949566063dSJacob Faibussowitsch PetscCall(TSSetType(t, type)); 559593b34091SDebojyoti Ghosh 559693b34091SDebojyoti Ghosh t->vec_sol = NULL; 559793b34091SDebojyoti Ghosh 559893b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 559993b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 560093b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 560193b34091SDebojyoti Ghosh 5602aea10558SJacob Faibussowitsch t->ops[0] = tsin->ops[0]; 560393b34091SDebojyoti Ghosh 56040d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 56050d4fed19SBarry Smith PetscInt i; 56069566063dSJacob Faibussowitsch PetscCall(PetscMalloc((10) * sizeof(void (*)(void)), &((PetscObject)t)->fortran_func_pointers)); 5607ad540459SPierre Jolivet for (i = 0; i < 10; i++) ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 56080d4fed19SBarry Smith } 560993b34091SDebojyoti Ghosh *tsout = t; 56103ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 561193b34091SDebojyoti Ghosh } 5612f3b1f45cSBarry Smith 5613d71ae5a4SJacob Faibussowitsch static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void *ctx, Vec x, Vec y) 5614d71ae5a4SJacob Faibussowitsch { 5615f3b1f45cSBarry Smith TS ts = (TS)ctx; 5616f3b1f45cSBarry Smith 5617f3b1f45cSBarry Smith PetscFunctionBegin; 56189566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts, 0, x, y)); 56193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5620f3b1f45cSBarry Smith } 5621f3b1f45cSBarry Smith 5622f3b1f45cSBarry Smith /*@ 5623bcf0153eSBarry Smith TSRHSJacobianTest - Compares the multiply routine provided to the `MATSHELL` with differencing on the `TS` given RHS function. 5624f3b1f45cSBarry Smith 5625c3339decSBarry Smith Logically Collective 5626f3b1f45cSBarry Smith 56272fe279fdSBarry Smith Input Parameter: 5628b43aa488SJacob Faibussowitsch . ts - the time stepping routine 5629f3b1f45cSBarry Smith 5630f3b1f45cSBarry Smith Output Parameter: 5631bcf0153eSBarry Smith . flg - `PETSC_TRUE` if the multiply is likely correct 5632f3b1f45cSBarry Smith 5633bcf0153eSBarry Smith Options Database Key: 5634f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator 5635f3b1f45cSBarry Smith 5636f3b1f45cSBarry Smith Level: advanced 5637f3b1f45cSBarry Smith 5638bcf0153eSBarry Smith Note: 5639195e9b02SBarry Smith This only works for problems defined using `TSSetRHSFunction()` and Jacobian NOT `TSSetIFunction()` and Jacobian 5640f3b1f45cSBarry Smith 56411cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `Mat`, `MATSHELL`, `MatCreateShell()`, `MatShellGetContext()`, `MatShellGetOperation()`, `MatShellTestMultTranspose()`, `TSRHSJacobianTestTranspose()` 5642f3b1f45cSBarry Smith @*/ 5643d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRHSJacobianTest(TS ts, PetscBool *flg) 5644d71ae5a4SJacob Faibussowitsch { 5645f3b1f45cSBarry Smith Mat J, B; 56468434afd1SBarry Smith TSRHSJacobianFn *func; 5647f3b1f45cSBarry Smith void *ctx; 5648f3b1f45cSBarry Smith 5649f3b1f45cSBarry Smith PetscFunctionBegin; 56509566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts, &J, &B, &func, &ctx)); 56519566063dSJacob Faibussowitsch PetscCall((*func)(ts, 0.0, ts->vec_sol, J, B, ctx)); 56529566063dSJacob Faibussowitsch PetscCall(MatShellTestMult(J, RHSWrapperFunction_TSRHSJacobianTest, ts->vec_sol, ts, flg)); 56533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5654f3b1f45cSBarry Smith } 5655f3b1f45cSBarry Smith 5656cc4c1da9SBarry Smith /*@ 5657bcf0153eSBarry Smith TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the `MATSHELL` with differencing on the `TS` given RHS function. 5658f3b1f45cSBarry Smith 5659c3339decSBarry Smith Logically Collective 5660f3b1f45cSBarry Smith 56612fe279fdSBarry Smith Input Parameter: 5662b43aa488SJacob Faibussowitsch . ts - the time stepping routine 5663f3b1f45cSBarry Smith 5664f3b1f45cSBarry Smith Output Parameter: 5665bcf0153eSBarry Smith . flg - `PETSC_TRUE` if the multiply is likely correct 5666f3b1f45cSBarry Smith 5667bcf0153eSBarry Smith Options Database Key: 5668f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator 5669f3b1f45cSBarry Smith 5670bcf0153eSBarry Smith Level: advanced 5671bcf0153eSBarry Smith 567295452b02SPatrick Sanan Notes: 5673195e9b02SBarry Smith This only works for problems defined using `TSSetRHSFunction()` and Jacobian NOT `TSSetIFunction()` and Jacobian 5674f3b1f45cSBarry Smith 56751cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `Mat`, `MatCreateShell()`, `MatShellGetContext()`, `MatShellGetOperation()`, `MatShellTestMultTranspose()`, `TSRHSJacobianTest()` 5676f3b1f45cSBarry Smith @*/ 5677d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRHSJacobianTestTranspose(TS ts, PetscBool *flg) 5678d71ae5a4SJacob Faibussowitsch { 5679f3b1f45cSBarry Smith Mat J, B; 5680f3b1f45cSBarry Smith void *ctx; 56818434afd1SBarry Smith TSRHSJacobianFn *func; 5682f3b1f45cSBarry Smith 5683f3b1f45cSBarry Smith PetscFunctionBegin; 56849566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts, &J, &B, &func, &ctx)); 56859566063dSJacob Faibussowitsch PetscCall((*func)(ts, 0.0, ts->vec_sol, J, B, ctx)); 56869566063dSJacob Faibussowitsch PetscCall(MatShellTestMultTranspose(J, RHSWrapperFunction_TSRHSJacobianTest, ts->vec_sol, ts, flg)); 56873ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5688f3b1f45cSBarry Smith } 56890fe4d17eSHong Zhang 56900fe4d17eSHong Zhang /*@ 56910fe4d17eSHong Zhang TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used. 56920fe4d17eSHong Zhang 569320f4b53cSBarry Smith Logically Collective 56940fe4d17eSHong Zhang 5695d8d19677SJose E. Roman Input Parameters: 56960fe4d17eSHong Zhang + ts - timestepping context 5697bcf0153eSBarry Smith - use_splitrhsfunction - `PETSC_TRUE` indicates that the split RHSFunction will be used 56980fe4d17eSHong Zhang 5699bcf0153eSBarry Smith Options Database Key: 57000fe4d17eSHong Zhang . -ts_use_splitrhsfunction - <true,false> 57010fe4d17eSHong Zhang 57020fe4d17eSHong Zhang Level: intermediate 57030fe4d17eSHong Zhang 5704bcf0153eSBarry Smith Note: 5705195e9b02SBarry Smith This is only for multirate methods 5706bcf0153eSBarry Smith 57071cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetUseSplitRHSFunction()` 57080fe4d17eSHong Zhang @*/ 5709d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction) 5710d71ae5a4SJacob Faibussowitsch { 57110fe4d17eSHong Zhang PetscFunctionBegin; 57120fe4d17eSHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 57130fe4d17eSHong Zhang ts->use_splitrhsfunction = use_splitrhsfunction; 57143ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 57150fe4d17eSHong Zhang } 57160fe4d17eSHong Zhang 57170fe4d17eSHong Zhang /*@ 57180fe4d17eSHong Zhang TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used. 57190fe4d17eSHong Zhang 572020f4b53cSBarry Smith Not Collective 57210fe4d17eSHong Zhang 57220fe4d17eSHong Zhang Input Parameter: 57230fe4d17eSHong Zhang . ts - timestepping context 57240fe4d17eSHong Zhang 57250fe4d17eSHong Zhang Output Parameter: 5726bcf0153eSBarry Smith . use_splitrhsfunction - `PETSC_TRUE` indicates that the split RHSFunction will be used 57270fe4d17eSHong Zhang 57280fe4d17eSHong Zhang Level: intermediate 57290fe4d17eSHong Zhang 57301cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetUseSplitRHSFunction()` 57310fe4d17eSHong Zhang @*/ 5732d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction) 5733d71ae5a4SJacob Faibussowitsch { 57340fe4d17eSHong Zhang PetscFunctionBegin; 57350fe4d17eSHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 57360fe4d17eSHong Zhang *use_splitrhsfunction = ts->use_splitrhsfunction; 57373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 57380fe4d17eSHong Zhang } 5739d60b7d5cSBarry Smith 5740d60b7d5cSBarry Smith /*@ 5741d60b7d5cSBarry Smith TSSetMatStructure - sets the relationship between the nonzero structure of the RHS Jacobian matrix to the IJacobian matrix. 5742d60b7d5cSBarry Smith 5743c3339decSBarry Smith Logically Collective 5744d60b7d5cSBarry Smith 5745d60b7d5cSBarry Smith Input Parameters: 5746d60b7d5cSBarry Smith + ts - the time-stepper 5747bcf0153eSBarry Smith - str - the structure (the default is `UNKNOWN_NONZERO_PATTERN`) 5748d60b7d5cSBarry Smith 5749d60b7d5cSBarry Smith Level: intermediate 5750d60b7d5cSBarry Smith 5751bcf0153eSBarry Smith Note: 5752d60b7d5cSBarry Smith When the relationship between the nonzero structures is known and supplied the solution process can be much faster 5753d60b7d5cSBarry Smith 57541cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `MatAXPY()`, `MatStructure` 5755d60b7d5cSBarry Smith @*/ 5756d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMatStructure(TS ts, MatStructure str) 5757d71ae5a4SJacob Faibussowitsch { 5758d60b7d5cSBarry Smith PetscFunctionBegin; 5759d60b7d5cSBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5760d60b7d5cSBarry Smith ts->axpy_pattern = str; 57613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5762d60b7d5cSBarry Smith } 57634a658b32SHong Zhang 57644a658b32SHong Zhang /*@ 5765bcf0153eSBarry Smith TSSetTimeSpan - sets the time span. The solution will be computed and stored for each time requested in the span 57664a658b32SHong Zhang 5767c3339decSBarry Smith Collective 57684a658b32SHong Zhang 57694a658b32SHong Zhang Input Parameters: 57704a658b32SHong Zhang + ts - the time-stepper 57714a658b32SHong Zhang . n - number of the time points (>=2) 57724a658b32SHong Zhang - span_times - array of the time points. The first element and the last element are the initial time and the final time respectively. 57734a658b32SHong Zhang 5774bcf0153eSBarry Smith Options Database Key: 57754a658b32SHong Zhang . -ts_time_span <t0,...tf> - Sets the time span 57764a658b32SHong Zhang 5777bcf0153eSBarry Smith Level: intermediate 57784a658b32SHong Zhang 57794a658b32SHong Zhang Notes: 57804a658b32SHong Zhang The elements in tspan must be all increasing. They correspond to the intermediate points for time integration. 5781bcf0153eSBarry Smith `TS_EXACTFINALTIME_MATCHSTEP` must be used to make the last time step in each sub-interval match the intermediate points specified. 5782bcf0153eSBarry Smith The intermediate solutions are saved in a vector array that can be accessed with `TSGetTimeSpanSolutions()`. Thus using time span may 57834a658b32SHong Zhang pressure the memory system when using a large number of span points. 57844a658b32SHong Zhang 57851cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTimeSpan()`, `TSGetTimeSpanSolutions()` 57864a658b32SHong Zhang @*/ 5787d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTimeSpan(TS ts, PetscInt n, PetscReal *span_times) 5788d71ae5a4SJacob Faibussowitsch { 57894a658b32SHong Zhang PetscFunctionBegin; 57904a658b32SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 579163a3b9bcSJacob Faibussowitsch PetscCheck(n >= 2, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Minimum time span size is 2 but %" PetscInt_FMT " is provided", n); 57924a658b32SHong Zhang if (ts->tspan && n != ts->tspan->num_span_times) { 57934a658b32SHong Zhang PetscCall(PetscFree(ts->tspan->span_times)); 57944a658b32SHong Zhang PetscCall(VecDestroyVecs(ts->tspan->num_span_times, &ts->tspan->vecs_sol)); 57954a658b32SHong Zhang PetscCall(PetscMalloc1(n, &ts->tspan->span_times)); 57964a658b32SHong Zhang } 57974a658b32SHong Zhang if (!ts->tspan) { 57984a658b32SHong Zhang TSTimeSpan tspan; 57994a658b32SHong Zhang PetscCall(PetscNew(&tspan)); 58004a658b32SHong Zhang PetscCall(PetscMalloc1(n, &tspan->span_times)); 5801e1db57b0SHong Zhang tspan->reltol = 1e-6; 5802e1db57b0SHong Zhang tspan->abstol = 10 * PETSC_MACHINE_EPSILON; 5803ca4445c7SIlya Fursov tspan->worktol = 0; 58044a658b32SHong Zhang ts->tspan = tspan; 58054a658b32SHong Zhang } 58064a658b32SHong Zhang ts->tspan->num_span_times = n; 58074a658b32SHong Zhang PetscCall(PetscArraycpy(ts->tspan->span_times, span_times, n)); 58084a658b32SHong Zhang PetscCall(TSSetTime(ts, ts->tspan->span_times[0])); 58094a658b32SHong Zhang PetscCall(TSSetMaxTime(ts, ts->tspan->span_times[n - 1])); 58103ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 58114a658b32SHong Zhang } 58124a658b32SHong Zhang 58134a658b32SHong Zhang /*@C 5814195e9b02SBarry Smith TSGetTimeSpan - gets the time span set with `TSSetTimeSpan()` 58154a658b32SHong Zhang 58164a658b32SHong Zhang Not Collective 58174a658b32SHong Zhang 58184a658b32SHong Zhang Input Parameter: 58194a658b32SHong Zhang . ts - the time-stepper 58204a658b32SHong Zhang 58214a658b32SHong Zhang Output Parameters: 58224a658b32SHong Zhang + n - number of the time points (>=2) 5823bcf0153eSBarry Smith - span_times - array of the time points. The first element and the last element are the initial time and the final time respectively. 58244a658b32SHong Zhang 58254a658b32SHong Zhang Level: beginner 58264a658b32SHong Zhang 5827bcf0153eSBarry Smith Note: 5828195e9b02SBarry Smith The values obtained are valid until the `TS` object is destroyed. 5829195e9b02SBarry Smith 5830195e9b02SBarry Smith Both `n` and `span_times` can be `NULL`. 5831bcf0153eSBarry Smith 58321cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetTimeSpan()`, `TSGetTimeSpanSolutions()` 58334a658b32SHong Zhang @*/ 5834cc4c1da9SBarry Smith PetscErrorCode TSGetTimeSpan(TS ts, PetscInt *n, const PetscReal *span_times[]) 5835d71ae5a4SJacob Faibussowitsch { 58364a658b32SHong Zhang PetscFunctionBegin; 58374a658b32SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 58384f572ea9SToby Isaac if (n) PetscAssertPointer(n, 2); 58394f572ea9SToby Isaac if (span_times) PetscAssertPointer(span_times, 3); 58404a658b32SHong Zhang if (!ts->tspan) { 58414a658b32SHong Zhang if (n) *n = 0; 58424a658b32SHong Zhang if (span_times) *span_times = NULL; 58434a658b32SHong Zhang } else { 58444a658b32SHong Zhang if (n) *n = ts->tspan->num_span_times; 58454a658b32SHong Zhang if (span_times) *span_times = ts->tspan->span_times; 58464a658b32SHong Zhang } 58473ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 58484a658b32SHong Zhang } 58494a658b32SHong Zhang 58504a658b32SHong Zhang /*@ 58514a658b32SHong Zhang TSGetTimeSpanSolutions - Get the number of solutions and the solutions at the time points specified by the time span. 58524a658b32SHong Zhang 58534a658b32SHong Zhang Input Parameter: 5854bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 58554a658b32SHong Zhang 58564a658b32SHong Zhang Output Parameters: 58574a658b32SHong Zhang + nsol - the number of solutions 58584a658b32SHong Zhang - Sols - the solution vectors 58594a658b32SHong Zhang 5860bcf0153eSBarry Smith Level: intermediate 58614a658b32SHong Zhang 586240bd4cedSHong Zhang Notes: 5863195e9b02SBarry Smith Both `nsol` and `Sols` can be `NULL`. 58644a658b32SHong Zhang 5865195e9b02SBarry Smith Some time points in the time span may be skipped by `TS` so that `nsol` is less than the number of points specified by `TSSetTimeSpan()`. 5866bcf0153eSBarry Smith For example, manipulating the step size, especially with a reduced precision, may cause `TS` to step over certain points in the span. 5867bcf0153eSBarry Smith 58681cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetTimeSpan()` 58694a658b32SHong Zhang @*/ 5870d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeSpanSolutions(TS ts, PetscInt *nsol, Vec **Sols) 5871d71ae5a4SJacob Faibussowitsch { 58724a658b32SHong Zhang PetscFunctionBegin; 58734a658b32SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 58744f572ea9SToby Isaac if (nsol) PetscAssertPointer(nsol, 2); 58754f572ea9SToby Isaac if (Sols) PetscAssertPointer(Sols, 3); 58764a658b32SHong Zhang if (!ts->tspan) { 58774a658b32SHong Zhang if (nsol) *nsol = 0; 58784a658b32SHong Zhang if (Sols) *Sols = NULL; 58794a658b32SHong Zhang } else { 588040bd4cedSHong Zhang if (nsol) *nsol = ts->tspan->spanctr; 58814a658b32SHong Zhang if (Sols) *Sols = ts->tspan->vecs_sol; 58824a658b32SHong Zhang } 58833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 58844a658b32SHong Zhang } 5885d7cfae9bSHong Zhang 5886cc4c1da9SBarry Smith /*@ 58877b2dc123SHong Zhang TSPruneIJacobianColor - Remove nondiagonal zeros in the Jacobian matrix and update the `MatMFFD` coloring information. 5888d7cfae9bSHong Zhang 5889195e9b02SBarry Smith Collective 5890d7cfae9bSHong Zhang 5891d7cfae9bSHong Zhang Input Parameters: 5892195e9b02SBarry Smith + ts - the `TS` context 5893d7cfae9bSHong Zhang . J - Jacobian matrix (not altered in this routine) 5894195e9b02SBarry Smith - B - newly computed Jacobian matrix to use with preconditioner 5895d7cfae9bSHong Zhang 5896d7cfae9bSHong Zhang Level: intermediate 5897d7cfae9bSHong Zhang 5898d7cfae9bSHong Zhang Notes: 5899195e9b02SBarry Smith This function improves the `MatFDColoring` performance when the Jacobian matrix was over-allocated or contains 5900195e9b02SBarry Smith many constant zeros entries, which is typically the case when the matrix is generated by a `DM` 5901d7cfae9bSHong Zhang and multiple fields are involved. 5902d7cfae9bSHong Zhang 5903d7cfae9bSHong Zhang Users need to make sure that the Jacobian matrix is properly filled to reflect the sparsity 5904195e9b02SBarry Smith structure. For `MatFDColoring`, the values of nonzero entries are not important. So one can 59057b2dc123SHong Zhang usually call `TSComputeIJacobian()` with randomized input vectors to generate a dummy Jacobian. 59067b2dc123SHong Zhang `TSComputeIJacobian()` should be called before `TSSolve()` but after `TSSetUp()`. 5907d7cfae9bSHong Zhang 59081cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `MatFDColoring`, `TSComputeIJacobianDefaultColor()`, `MatEliminateZeros()`, `MatFDColoringCreate()`, `MatFDColoringSetFunction()` 5909d7cfae9bSHong Zhang @*/ 5910d7cfae9bSHong Zhang PetscErrorCode TSPruneIJacobianColor(TS ts, Mat J, Mat B) 5911d7cfae9bSHong Zhang { 5912d7cfae9bSHong Zhang MatColoring mc = NULL; 5913d7cfae9bSHong Zhang ISColoring iscoloring = NULL; 5914d7cfae9bSHong Zhang MatFDColoring matfdcoloring = NULL; 5915d7cfae9bSHong Zhang 5916d7cfae9bSHong Zhang PetscFunctionBegin; 5917d7cfae9bSHong Zhang /* Generate new coloring after eliminating zeros in the matrix */ 591858c11ad4SPierre Jolivet PetscCall(MatEliminateZeros(B, PETSC_TRUE)); 5919d7cfae9bSHong Zhang PetscCall(MatColoringCreate(B, &mc)); 5920d7cfae9bSHong Zhang PetscCall(MatColoringSetDistance(mc, 2)); 5921d7cfae9bSHong Zhang PetscCall(MatColoringSetType(mc, MATCOLORINGSL)); 5922d7cfae9bSHong Zhang PetscCall(MatColoringSetFromOptions(mc)); 5923d7cfae9bSHong Zhang PetscCall(MatColoringApply(mc, &iscoloring)); 5924d7cfae9bSHong Zhang PetscCall(MatColoringDestroy(&mc)); 5925d7cfae9bSHong Zhang /* Replace the old coloring with the new one */ 5926d7cfae9bSHong Zhang PetscCall(MatFDColoringCreate(B, iscoloring, &matfdcoloring)); 5927d7cfae9bSHong Zhang PetscCall(MatFDColoringSetFunction(matfdcoloring, (PetscErrorCode(*)(void))SNESTSFormFunction, (void *)ts)); 5928d7cfae9bSHong Zhang PetscCall(MatFDColoringSetFromOptions(matfdcoloring)); 5929d7cfae9bSHong Zhang PetscCall(MatFDColoringSetUp(B, iscoloring, matfdcoloring)); 5930d7cfae9bSHong Zhang PetscCall(PetscObjectCompose((PetscObject)B, "TSMatFDColoring", (PetscObject)matfdcoloring)); 5931d7cfae9bSHong Zhang PetscCall(PetscObjectDereference((PetscObject)matfdcoloring)); 5932d7cfae9bSHong Zhang PetscCall(ISColoringDestroy(&iscoloring)); 59333ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5934d7cfae9bSHong Zhang } 5935