1af0996ceSBarry Smith #include <petsc/private/tsimpl.h> /*I "petscts.h" I*/ 21e25c274SJed Brown #include <petscdmda.h> 360e16b1bSMatthew G. Knepley #include <petscdmshell.h> 460e16b1bSMatthew G. Knepley #include <petscdmplex.h> // For TSSetFromOptions() 560e16b1bSMatthew G. Knepley #include <petscdmswarm.h> // For TSSetFromOptions() 62d5ee99bSBarry Smith #include <petscviewer.h> 72d5ee99bSBarry Smith #include <petscdraw.h> 8900f6b5bSMatthew G. Knepley #include <petscconvest.h> 9d763cef2SBarry Smith 10d5ba7fb7SMatthew Knepley /* Logging support */ 11d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 12a05bf03eSHong Zhang PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 13d405a339SMatthew Knepley 14c793f718SLisandro Dalcin const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED", "STEPOVER", "INTERPOLATE", "MATCHSTEP", "TSExactFinalTimeOption", "TS_EXACTFINALTIME_", NULL}; 1549354f04SShri Abhyankar 16d71ae5a4SJacob Faibussowitsch static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt, TSAdaptType default_type) 17d71ae5a4SJacob Faibussowitsch { 182ffb9264SLisandro Dalcin PetscFunctionBegin; 19b92453a8SLisandro Dalcin PetscValidHeaderSpecific(adapt, TSADAPT_CLASSID, 1); 204f572ea9SToby Isaac PetscAssertPointer(default_type, 2); 211e66621cSBarry Smith if (!((PetscObject)adapt)->type_name) PetscCall(TSAdaptSetType(adapt, default_type)); 223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 232ffb9264SLisandro Dalcin } 242ffb9264SLisandro Dalcin 25bdad233fSMatthew Knepley /*@ 26195e9b02SBarry Smith TSSetFromOptions - Sets various `TS` parameters from the options database 27bdad233fSMatthew Knepley 28c3339decSBarry Smith Collective 29bdad233fSMatthew Knepley 30bdad233fSMatthew Knepley Input Parameter: 31bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 32bdad233fSMatthew Knepley 33bdad233fSMatthew Knepley Options Database Keys: 34195e9b02SBarry Smith + -ts_type <type> - EULER, BEULER, SUNDIALS, PSEUDO, CN, RK, THETA, ALPHA, GLLE, SSP, GLEE, BSYMP, IRK, see `TSType` 35ef222394SBarry Smith . -ts_save_trajectory - checkpoint the solution at each time-step 36ef85077eSLisandro Dalcin . -ts_max_time <time> - maximum time to compute to 378343f784SJames Wright . -ts_time_span <t0,...tf> - sets the time span, solutions are computed and stored for each indicated time, init_time and max_time are set 388343f784SJames Wright . -ts_eval_times <t0,...tn> - time points where solutions are computed and stored for each indicated time 39ef85077eSLisandro Dalcin . -ts_max_steps <steps> - maximum number of time-steps to take 40ef85077eSLisandro Dalcin . -ts_init_time <time> - initial time to start computation 41195e9b02SBarry Smith . -ts_final_time <time> - final time to compute to (deprecated: use `-ts_max_time`) 423e4cdcaaSBarry Smith . -ts_dt <dt> - initial time step 431628793fSSatish 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 44a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 45a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 46a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 47a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 4867b8a455SSatish Balay . -ts_atol <atol> - Absolute tolerance for local truncation error 49f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function 50b43aa488SJacob Faibussowitsch . -ts_rhs_jacobian_test_mult_transpose - test the Jacobian at each iteration against finite difference with RHS function 51195e9b02SBarry Smith . -ts_adjoint_solve <yes,no> - After solving the ODE/DAE solve the adjoint problem (requires `-ts_save_trajectory`) 52847ff0e1SMatthew G. Knepley . -ts_fd_color - Use finite differences with coloring to compute IJacobian 53bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 54aee7a9fbSMatthew G. Knepley . -ts_monitor_cancel - Cancel all monitors 55de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 56de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 577cf37e64SBarry Smith . -ts_monitor_error - Monitors norm of error 586934998bSLisandro Dalcin . -ts_monitor_lg_timestep - Monitor timestep size graphically 598b668821SLisandro Dalcin . -ts_monitor_lg_timestep_log - Monitor log timestep size graphically 60de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 61de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 62de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 63de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 643e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 653e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 66fde5950dSBarry Smith . -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep 67c17ba870SStefano Zampini . -ts_monitor_solution_interval <interval> - output once every interval (default=1) time steps. Use -1 to only output at the end of the simulation 6863a3b9bcSJacob 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) 69c17ba870SStefano Zampini . -ts_monitor_solution_vtk_interval <interval> - output once every interval (default=1) time steps. Use -1 to only output at the end of the simulation 709e336e28SPatrick Sanan - -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 7153ea634cSHong Zhang 72bcf0153eSBarry Smith Level: beginner 733d5a8a6aSBarry Smith 74bcf0153eSBarry Smith Notes: 75bcf0153eSBarry Smith See `SNESSetFromOptions()` and `KSPSetFromOptions()` for how to control the nonlinear and linear solves used by the time-stepper. 76bcf0153eSBarry Smith 77195e9b02SBarry Smith Certain `SNES` options get reset for each new nonlinear solver, for example `-snes_lag_jacobian its` and `-snes_lag_preconditioner its`, in order 780d56bcf9SIlya Fursov to retain them over the multiple nonlinear solves that `TS` uses you must also provide `-snes_lag_jacobian_persists true` and 79195e9b02SBarry Smith `-snes_lag_preconditioner_persists true` 803d5a8a6aSBarry Smith 81b43aa488SJacob Faibussowitsch Developer Notes: 829e336e28SPatrick Sanan We should unify all the -ts_monitor options in the way that -xxx_view has been unified 83bdad233fSMatthew Knepley 841cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetType()` 85bdad233fSMatthew Knepley @*/ 86d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetFromOptions(TS ts) 87d71ae5a4SJacob Faibussowitsch { 88bc952696SBarry Smith PetscBool opt, flg, tflg; 89eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 90136cf249SJames Wright PetscReal time_step, eval_times[100]; 91136cf249SJames Wright PetscInt num_eval_times = PETSC_STATIC_ARRAY_LENGTH(eval_times); 9249354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 93d1212d36SBarry Smith char dir[16]; 948434afd1SBarry Smith TSIFunctionFn *ifun; 956991f827SBarry Smith const char *defaultType; 966991f827SBarry Smith char typeName[256]; 97bdad233fSMatthew Knepley 98bdad233fSMatthew Knepley PetscFunctionBegin; 990700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1006991f827SBarry Smith 1019566063dSJacob Faibussowitsch PetscCall(TSRegisterAll()); 1029566063dSJacob Faibussowitsch PetscCall(TSGetIFunction(ts, NULL, &ifun, NULL)); 103cd11d68dSLisandro Dalcin 104d0609cedSBarry Smith PetscObjectOptionsBegin((PetscObject)ts); 1051ef27442SStefano Zampini if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 1061ef27442SStefano Zampini else defaultType = ifun ? TSBEULER : TSEULER; 1079566063dSJacob Faibussowitsch PetscCall(PetscOptionsFList("-ts_type", "TS method", "TSSetType", TSList, defaultType, typeName, 256, &opt)); 1081e66621cSBarry Smith if (opt) PetscCall(TSSetType(ts, typeName)); 1091e66621cSBarry Smith else PetscCall(TSSetType(ts, defaultType)); 110bdad233fSMatthew Knepley 111bdad233fSMatthew Knepley /* Handle generic TS options */ 1129566063dSJacob Faibussowitsch PetscCall(PetscOptionsDeprecated("-ts_final_time", "-ts_max_time", "3.10", NULL)); 1139566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_max_time", "Maximum time to run to", "TSSetMaxTime", ts->max_time, &ts->max_time, NULL)); 114136cf249SJames Wright PetscCall(PetscOptionsRealArray("-ts_time_span", "Time span", "TSSetTimeSpan", eval_times, &num_eval_times, &flg)); 115136cf249SJames Wright if (flg) PetscCall(TSSetTimeSpan(ts, num_eval_times, eval_times)); 116136cf249SJames Wright num_eval_times = PETSC_STATIC_ARRAY_LENGTH(eval_times); 117136cf249SJames Wright PetscCall(PetscOptionsRealArray("-ts_eval_times", "Evaluation time points", "TSSetEvaluationTimes", eval_times, &num_eval_times, &opt)); 1188343f784SJames Wright PetscCheck(flg != opt || (!flg && !opt), PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "May not provide -ts_time_span and -ts_eval_times simultaneously"); 119136cf249SJames Wright if (opt) PetscCall(TSSetEvaluationTimes(ts, num_eval_times, eval_times)); 1209566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_max_steps", "Maximum number of time steps", "TSSetMaxSteps", ts->max_steps, &ts->max_steps, NULL)); 1219566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_init_time", "Initial time", "TSSetTime", ts->ptime, &ts->ptime, NULL)); 1229566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_dt", "Initial time step", "TSSetTimeStep", ts->time_step, &time_step, &flg)); 1239566063dSJacob Faibussowitsch if (flg) PetscCall(TSSetTimeStep(ts, time_step)); 1249566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum("-ts_exact_final_time", "Option for handling of final time step", "TSSetExactFinalTime", TSExactFinalTimeOptions, (PetscEnum)ts->exact_final_time, (PetscEnum *)&eftopt, &flg)); 1259566063dSJacob Faibussowitsch if (flg) PetscCall(TSSetExactFinalTime(ts, eftopt)); 12609cb0f53SBarry Smith PetscCall(PetscOptionsInt("-ts_max_snes_failures", "Maximum number of nonlinear solve failures", "TSSetMaxSNESFailures", ts->max_snes_failures, &ts->max_snes_failures, &flg)); 12709cb0f53SBarry Smith if (flg) PetscCall(TSSetMaxSNESFailures(ts, ts->max_snes_failures)); 12809cb0f53SBarry Smith PetscCall(PetscOptionsInt("-ts_max_reject", "Maximum number of step rejections before step fails", "TSSetMaxStepRejections", ts->max_reject, &ts->max_reject, &flg)); 12909cb0f53SBarry Smith if (flg) PetscCall(TSSetMaxStepRejections(ts, ts->max_reject)); 1309566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_error_if_step_fails", "Error if no step succeeds", "TSSetErrorIfStepFails", ts->errorifstepfailed, &ts->errorifstepfailed, NULL)); 13109cb0f53SBarry Smith PetscCall(PetscOptionsBoundedReal("-ts_rtol", "Relative tolerance for local truncation error", "TSSetTolerances", ts->rtol, &ts->rtol, NULL, 0)); 13209cb0f53SBarry Smith PetscCall(PetscOptionsBoundedReal("-ts_atol", "Absolute tolerance for local truncation error", "TSSetTolerances", ts->atol, &ts->atol, NULL, 0)); 133bdad233fSMatthew Knepley 1349566063dSJacob 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)); 1359566063dSJacob 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)); 1369566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_use_splitrhsfunction", "Use the split RHS function for multirate solvers ", "TSSetUseSplitRHSFunction", ts->use_splitrhsfunction, &ts->use_splitrhsfunction, NULL)); 13756f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 13856f85f32SBarry Smith { 13956f85f32SBarry Smith PetscBool set; 14056f85f32SBarry Smith flg = PETSC_FALSE; 1419566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_saws_block", "Block for SAWs memory snooper at end of TSSolve", "PetscObjectSAWsBlock", ((PetscObject)ts)->amspublishblock, &flg, &set)); 1421baa6e33SBarry Smith if (set) PetscCall(PetscObjectSAWsSetBlock((PetscObject)ts, flg)); 14356f85f32SBarry Smith } 14456f85f32SBarry Smith #endif 14556f85f32SBarry Smith 146bdad233fSMatthew Knepley /* Monitor options */ 1479566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_frequency", "Number of time steps between monitor output", "TSMonitorSetFrequency", ts->monitorFrequency, &ts->monitorFrequency, NULL)); 1489566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor", "Monitor time and timestep size", "TSMonitorDefault", TSMonitorDefault, NULL)); 1499566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor_extreme", "Monitor extreme values of the solution", "TSMonitorExtreme", TSMonitorExtreme, NULL)); 1509566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor_solution", "View the solution at each timestep", "TSMonitorSolution", TSMonitorSolution, NULL)); 1519566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_dmswarm_monitor_moments", "Monitor moments of particle distribution", "TSDMSwarmMonitorMoments", TSDMSwarmMonitorMoments, NULL)); 152fde5950dSBarry Smith 1539566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_python", "Use Python function", "TSMonitorSet", NULL, monfilename, sizeof(monfilename), &flg)); 1549566063dSJacob Faibussowitsch if (flg) PetscCall(PetscPythonMonitorSet((PetscObject)ts, monfilename)); 1555180491cSLisandro Dalcin 1569566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_solution", "Monitor solution graphically", "TSMonitorLGSolution", &opt)); 157b3603a34SBarry Smith if (opt) { 1583923b477SBarry Smith PetscInt howoften = 1; 159e669de00SBarry Smith DM dm; 160e669de00SBarry Smith PetscBool net; 161b3603a34SBarry Smith 1629566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_solution", "Monitor solution graphically", "TSMonitorLGSolution", howoften, &howoften, NULL)); 1639566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 1649566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)dm, DMNETWORK, &net)); 165e669de00SBarry Smith if (net) { 166e669de00SBarry Smith TSMonitorLGCtxNetwork ctx; 1679566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxNetworkCreate(ts, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 600, 400, howoften, &ctx)); 16849abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorLGCtxNetworkSolution, ctx, (PetscCtxDestroyFn *)TSMonitorLGCtxNetworkDestroy)); 1699566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_lg_solution_semilogy", "Plot the solution with a semi-log axis", "", ctx->semilogy, &ctx->semilogy, NULL)); 170e669de00SBarry Smith } else { 171e669de00SBarry Smith TSMonitorLGCtx ctx; 1729566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 17349abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorLGSolution, ctx, (PetscCtxDestroyFn *)TSMonitorLGCtxDestroy)); 174bdad233fSMatthew Knepley } 175e669de00SBarry Smith } 1766ba87a44SLisandro Dalcin 1779566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_error", "Monitor error graphically", "TSMonitorLGError", &opt)); 178ef20d060SBarry Smith if (opt) { 1790b039ecaSBarry Smith TSMonitorLGCtx ctx; 1803923b477SBarry Smith PetscInt howoften = 1; 181ef20d060SBarry Smith 1829566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_error", "Monitor error graphically", "TSMonitorLGError", howoften, &howoften, NULL)); 1839566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 18449abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorLGError, ctx, (PetscCtxDestroyFn *)TSMonitorLGCtxDestroy)); 185ef20d060SBarry Smith } 1869566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor_error", "View the error at each timestep", "TSMonitorError", TSMonitorError, NULL)); 1877cf37e64SBarry Smith 1889566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_timestep", "Monitor timestep size graphically", "TSMonitorLGTimeStep", &opt)); 1896934998bSLisandro Dalcin if (opt) { 1906934998bSLisandro Dalcin TSMonitorLGCtx ctx; 1916934998bSLisandro Dalcin PetscInt howoften = 1; 1926934998bSLisandro Dalcin 1939566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_timestep", "Monitor timestep size graphically", "TSMonitorLGTimeStep", howoften, &howoften, NULL)); 1949566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 19549abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorLGTimeStep, ctx, (PetscCtxDestroyFn *)TSMonitorLGCtxDestroy)); 1966934998bSLisandro Dalcin } 1979566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_timestep_log", "Monitor log timestep size graphically", "TSMonitorLGTimeStep", &opt)); 1988b668821SLisandro Dalcin if (opt) { 1998b668821SLisandro Dalcin TSMonitorLGCtx ctx; 2008b668821SLisandro Dalcin PetscInt howoften = 1; 2018b668821SLisandro Dalcin 2029566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_timestep_log", "Monitor log timestep size graphically", "TSMonitorLGTimeStep", howoften, &howoften, NULL)); 2039566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 20449abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorLGTimeStep, ctx, (PetscCtxDestroyFn *)TSMonitorLGCtxDestroy)); 2058b668821SLisandro Dalcin ctx->semilogy = PETSC_TRUE; 2068b668821SLisandro Dalcin } 2078b668821SLisandro Dalcin 2089566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_snes_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGSNESIterations", &opt)); 209201da799SBarry Smith if (opt) { 210201da799SBarry Smith TSMonitorLGCtx ctx; 211201da799SBarry Smith PetscInt howoften = 1; 212201da799SBarry Smith 2139566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_snes_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGSNESIterations", howoften, &howoften, NULL)); 2149566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 21549abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorLGSNESIterations, ctx, (PetscCtxDestroyFn *)TSMonitorLGCtxDestroy)); 216201da799SBarry Smith } 2179566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_ksp_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGKSPIterations", &opt)); 218201da799SBarry Smith if (opt) { 219201da799SBarry Smith TSMonitorLGCtx ctx; 220201da799SBarry Smith PetscInt howoften = 1; 221201da799SBarry Smith 2229566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_ksp_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGKSPIterations", howoften, &howoften, NULL)); 2239566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 22449abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorLGKSPIterations, ctx, (PetscCtxDestroyFn *)TSMonitorLGCtxDestroy)); 225201da799SBarry Smith } 2269566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_sp_eig", "Monitor eigenvalues of linearized operator graphically", "TSMonitorSPEig", &opt)); 2278189c53fSBarry Smith if (opt) { 2288189c53fSBarry Smith TSMonitorSPEigCtx ctx; 2298189c53fSBarry Smith PetscInt howoften = 1; 2308189c53fSBarry Smith 2319566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_sp_eig", "Monitor eigenvalues of linearized operator graphically", "TSMonitorSPEig", howoften, &howoften, NULL)); 2329566063dSJacob Faibussowitsch PetscCall(TSMonitorSPEigCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx)); 23349abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorSPEig, ctx, (PetscCtxDestroyFn *)TSMonitorSPEigCtxDestroy)); 2348189c53fSBarry Smith } 23560e16b1bSMatthew G. Knepley PetscCall(PetscOptionsName("-ts_monitor_sp_swarm", "Display particle phase space from the DMSwarm", "TSMonitorSPSwarm", &opt)); 2361b575b74SJoseph Pusztay if (opt) { 2371b575b74SJoseph Pusztay TSMonitorSPCtx ctx; 238d7462660SMatthew Knepley PetscInt howoften = 1, retain = 0; 23960e16b1bSMatthew G. Knepley PetscBool phase = PETSC_TRUE, create = PETSC_TRUE, multispecies = PETSC_FALSE; 240d7462660SMatthew Knepley 2419371c9d4SSatish Balay for (PetscInt i = 0; i < ts->numbermonitors; ++i) 2429371c9d4SSatish Balay if (ts->monitor[i] == TSMonitorSPSwarmSolution) { 2439371c9d4SSatish Balay create = PETSC_FALSE; 2449371c9d4SSatish Balay break; 2459371c9d4SSatish Balay } 2466a5217c0SMatthew G. Knepley if (create) { 24760e16b1bSMatthew G. Knepley PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm", "Display particles phase space from the DMSwarm", "TSMonitorSPSwarm", howoften, &howoften, NULL)); 2489566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm_retain", "Retain n points plotted to show trajectory, -1 for all points", "TSMonitorSPSwarm", retain, &retain, NULL)); 2499566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_sp_swarm_phase", "Plot in phase space rather than coordinate space", "TSMonitorSPSwarm", phase, &phase, NULL)); 25060e16b1bSMatthew G. Knepley PetscCall(PetscOptionsBool("-ts_monitor_sp_swarm_multi_species", "Color particles by particle species", "TSMonitorSPSwarm", multispecies, &multispecies, NULL)); 25160e16b1bSMatthew G. Knepley PetscCall(TSMonitorSPCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, retain, phase, multispecies, &ctx)); 25249abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscCtxDestroyFn *)TSMonitorSPCtxDestroy)); 2531b575b74SJoseph Pusztay } 2546a5217c0SMatthew G. Knepley } 25560e16b1bSMatthew G. Knepley PetscCall(PetscOptionsName("-ts_monitor_hg_swarm", "Display particle histogram from the DMSwarm", "TSMonitorHGSwarm", &opt)); 25660e16b1bSMatthew G. Knepley if (opt) { 25760e16b1bSMatthew G. Knepley TSMonitorHGCtx ctx; 25860e16b1bSMatthew G. Knepley PetscInt howoften = 1, Ns = 1; 25960e16b1bSMatthew G. Knepley PetscBool velocity = PETSC_FALSE, create = PETSC_TRUE; 26060e16b1bSMatthew G. Knepley 26160e16b1bSMatthew G. Knepley for (PetscInt i = 0; i < ts->numbermonitors; ++i) 26260e16b1bSMatthew G. Knepley if (ts->monitor[i] == TSMonitorHGSwarmSolution) { 26360e16b1bSMatthew G. Knepley create = PETSC_FALSE; 26460e16b1bSMatthew G. Knepley break; 26560e16b1bSMatthew G. Knepley } 26660e16b1bSMatthew G. Knepley if (create) { 26760e16b1bSMatthew G. Knepley DM sw, dm; 26860e16b1bSMatthew G. Knepley PetscInt Nc, Nb; 26960e16b1bSMatthew G. Knepley 27060e16b1bSMatthew G. Knepley PetscCall(TSGetDM(ts, &sw)); 27160e16b1bSMatthew G. Knepley PetscCall(DMSwarmGetCellDM(sw, &dm)); 27260e16b1bSMatthew G. Knepley PetscCall(DMPlexGetHeightStratum(dm, 0, NULL, &Nc)); 27360e16b1bSMatthew G. Knepley Nb = PetscMin(20, PetscMax(10, Nc)); 27460e16b1bSMatthew G. Knepley PetscCall(PetscOptionsInt("-ts_monitor_hg_swarm", "Display particles histogram from the DMSwarm", "TSMonitorHGSwarm", howoften, &howoften, NULL)); 27560e16b1bSMatthew G. Knepley PetscCall(PetscOptionsBool("-ts_monitor_hg_swarm_velocity", "Plot in velocity space rather than coordinate space", "TSMonitorHGSwarm", velocity, &velocity, NULL)); 27660e16b1bSMatthew G. Knepley PetscCall(PetscOptionsInt("-ts_monitor_hg_swarm_species", "Number of species to histogram", "TSMonitorHGSwarm", Ns, &Ns, NULL)); 27760e16b1bSMatthew G. Knepley PetscCall(PetscOptionsInt("-ts_monitor_hg_swarm_bins", "Number of histogram bins", "TSMonitorHGSwarm", Nb, &Nb, NULL)); 27860e16b1bSMatthew G. Knepley PetscCall(TSMonitorHGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, Ns, Nb, velocity, &ctx)); 27949abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorHGSwarmSolution, ctx, (PetscCtxDestroyFn *)TSMonitorHGCtxDestroy)); 28060e16b1bSMatthew G. Knepley } 28160e16b1bSMatthew G. Knepley } 282ef20d060SBarry Smith opt = PETSC_FALSE; 2839566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution", "Monitor solution graphically", "TSMonitorDrawSolution", &opt)); 284a7cc72afSBarry Smith if (opt) { 28583a4ac43SBarry Smith TSMonitorDrawCtx ctx; 28683a4ac43SBarry Smith PetscInt howoften = 1; 287a80ad3e0SBarry Smith 2889566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_solution", "Monitor solution graphically", "TSMonitorDrawSolution", howoften, &howoften, NULL)); 2899566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, "Computed Solution", PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx)); 29049abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorDrawSolution, ctx, (PetscCtxDestroyFn *)TSMonitorDrawCtxDestroy)); 291bdad233fSMatthew Knepley } 292fb1732b5SBarry Smith opt = PETSC_FALSE; 2939566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution_phase", "Monitor solution graphically", "TSMonitorDrawSolutionPhase", &opt)); 2942d5ee99bSBarry Smith if (opt) { 2952d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2962d5ee99bSBarry Smith PetscReal bounds[4]; 2972d5ee99bSBarry Smith PetscInt n = 4; 2982d5ee99bSBarry Smith PetscDraw draw; 2996934998bSLisandro Dalcin PetscDrawAxis axis; 3002d5ee99bSBarry Smith 3019566063dSJacob Faibussowitsch PetscCall(PetscOptionsRealArray("-ts_monitor_draw_solution_phase", "Monitor solution graphically", "TSMonitorDrawSolutionPhase", bounds, &n, NULL)); 3023c633725SBarry Smith PetscCheck(n == 4, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Must provide bounding box of phase field"); 3039566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, 1, &ctx)); 3049566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(ctx->viewer, 0, &draw)); 3059566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDrawAxis(ctx->viewer, 0, &axis)); 3069566063dSJacob Faibussowitsch PetscCall(PetscDrawAxisSetLimits(axis, bounds[0], bounds[2], bounds[1], bounds[3])); 3079566063dSJacob Faibussowitsch PetscCall(PetscDrawAxisSetLabels(axis, "Phase Diagram", "Variable 1", "Variable 2")); 30849abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorDrawSolutionPhase, ctx, (PetscCtxDestroyFn *)TSMonitorDrawCtxDestroy)); 3092d5ee99bSBarry Smith } 3102d5ee99bSBarry Smith opt = PETSC_FALSE; 3119566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_error", "Monitor error graphically", "TSMonitorDrawError", &opt)); 3123a471f94SBarry Smith if (opt) { 31383a4ac43SBarry Smith TSMonitorDrawCtx ctx; 31483a4ac43SBarry Smith PetscInt howoften = 1; 3153a471f94SBarry Smith 3169566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_error", "Monitor error graphically", "TSMonitorDrawError", howoften, &howoften, NULL)); 3179566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, "Error", PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx)); 31849abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorDrawError, ctx, (PetscCtxDestroyFn *)TSMonitorDrawCtxDestroy)); 3193a471f94SBarry Smith } 3200ed3bfb6SBarry Smith opt = PETSC_FALSE; 3219566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution_function", "Monitor solution provided by TSMonitorSetSolutionFunction() graphically", "TSMonitorDrawSolutionFunction", &opt)); 3220ed3bfb6SBarry Smith if (opt) { 3230ed3bfb6SBarry Smith TSMonitorDrawCtx ctx; 3240ed3bfb6SBarry Smith PetscInt howoften = 1; 3250ed3bfb6SBarry Smith 3269566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_solution_function", "Monitor solution provided by TSMonitorSetSolutionFunction() graphically", "TSMonitorDrawSolutionFunction", howoften, &howoften, NULL)); 3279566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, "Solution provided by user function", PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx)); 32849abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorDrawSolutionFunction, ctx, (PetscCtxDestroyFn *)TSMonitorDrawCtxDestroy)); 3290ed3bfb6SBarry Smith } 330fde5950dSBarry Smith 331ed81e22dSJed Brown opt = PETSC_FALSE; 33263a3b9bcSJacob 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)); 333ed81e22dSJed Brown if (flg) { 3347f27e910SStefano Zampini TSMonitorVTKCtx ctx; 3357f27e910SStefano Zampini 3367f27e910SStefano Zampini PetscCall(TSMonitorSolutionVTKCtxCreate(monfilename, &ctx)); 337c17ba870SStefano Zampini PetscCall(PetscOptionsInt("-ts_monitor_solution_vtk_interval", "Save every interval time step (-1 for last step only)", NULL, ctx->interval, &ctx->interval, NULL)); 33849abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, (PetscErrorCode (*)(TS, PetscInt, PetscReal, Vec, void *))TSMonitorSolutionVTK, ctx, (PetscCtxDestroyFn *)TSMonitorSolutionVTKDestroy)); 339ed81e22dSJed Brown } 340bdad233fSMatthew Knepley 3419566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_dmda_ray", "Display a ray of the solution", "None", "y=0", dir, sizeof(dir), &flg)); 342d1212d36SBarry Smith if (flg) { 343d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 344d1212d36SBarry Smith int ray = 0; 3453ee9839eSMatthew G. Knepley DMDirection ddir; 346d1212d36SBarry Smith DM da; 347d1212d36SBarry Smith PetscMPIInt rank; 348d1212d36SBarry Smith 3493c633725SBarry Smith PetscCheck(dir[1] == '=', PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Unknown ray %s", dir); 3503ee9839eSMatthew G. Knepley if (dir[0] == 'x') ddir = DM_X; 3513ee9839eSMatthew G. Knepley else if (dir[0] == 'y') ddir = DM_Y; 35298921bdaSJacob Faibussowitsch else SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Unknown ray %s", dir); 353d1212d36SBarry Smith sscanf(dir + 2, "%d", &ray); 354d1212d36SBarry Smith 355835f2295SStefano Zampini PetscCall(PetscInfo(ts, "Displaying DMDA ray %c = %d\n", dir[0], ray)); 3569566063dSJacob Faibussowitsch PetscCall(PetscNew(&rayctx)); 3579566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &da)); 3589566063dSJacob Faibussowitsch PetscCall(DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter)); 3599566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)ts), &rank)); 3601e66621cSBarry Smith if (rank == 0) PetscCall(PetscViewerDrawOpen(PETSC_COMM_SELF, NULL, NULL, 0, 0, 600, 300, &rayctx->viewer)); 36151b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 3629566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorDMDARay, rayctx, TSMonitorDMDARayDestroy)); 363d1212d36SBarry Smith } 3649566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_lg_dmda_ray", "Display a ray of the solution", "None", "x=0", dir, sizeof(dir), &flg)); 36551b4a12fSMatthew G. Knepley if (flg) { 36651b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 36751b4a12fSMatthew G. Knepley int ray = 0; 3683ee9839eSMatthew G. Knepley DMDirection ddir; 36951b4a12fSMatthew G. Knepley DM da; 37051b4a12fSMatthew G. Knepley PetscInt howoften = 1; 371d1212d36SBarry Smith 3723c633725SBarry Smith PetscCheck(dir[1] == '=', PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 3733ee9839eSMatthew G. Knepley if (dir[0] == 'x') ddir = DM_X; 3743ee9839eSMatthew G. Knepley else if (dir[0] == 'y') ddir = DM_Y; 37598921bdaSJacob Faibussowitsch else SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 37651b4a12fSMatthew G. Knepley sscanf(dir + 2, "%d", &ray); 3771c3436cfSJed Brown 378835f2295SStefano Zampini PetscCall(PetscInfo(ts, "Displaying LG DMDA ray %c = %d\n", dir[0], ray)); 3799566063dSJacob Faibussowitsch PetscCall(PetscNew(&rayctx)); 3809566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &da)); 3819566063dSJacob Faibussowitsch PetscCall(DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter)); 3829566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 600, 400, howoften, &rayctx->lgctx)); 3839566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy)); 38451b4a12fSMatthew G. Knepley } 385a7a1495cSBarry Smith 3869566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_envelope", "Monitor maximum and minimum value of each component of the solution", "TSMonitorEnvelope", &opt)); 387b3d3934dSBarry Smith if (opt) { 388b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 389b3d3934dSBarry Smith 3909566063dSJacob Faibussowitsch PetscCall(TSMonitorEnvelopeCtxCreate(ts, &ctx)); 39149abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorEnvelope, ctx, (PetscCtxDestroyFn *)TSMonitorEnvelopeCtxDestroy)); 392b3d3934dSBarry Smith } 393aee7a9fbSMatthew G. Knepley flg = PETSC_FALSE; 3949566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_cancel", "Remove all monitors", "TSMonitorCancel", flg, &flg, &opt)); 3959566063dSJacob Faibussowitsch if (opt && flg) PetscCall(TSMonitorCancel(ts)); 396b3d3934dSBarry Smith 397847ff0e1SMatthew G. Knepley flg = PETSC_FALSE; 398d7cfae9bSHong Zhang PetscCall(PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeIJacobianDefaultColor", flg, &flg, NULL)); 399847ff0e1SMatthew G. Knepley if (flg) { 400847ff0e1SMatthew G. Knepley DM dm; 401847ff0e1SMatthew G. Knepley 4029371c9d4SSatish Balay PetscCall(TSGetDM(ts, &dm)); 4039371c9d4SSatish Balay PetscCall(DMTSUnsetIJacobianContext_Internal(dm)); 4049566063dSJacob Faibussowitsch PetscCall(TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, NULL)); 4059566063dSJacob Faibussowitsch PetscCall(PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n")); 406847ff0e1SMatthew G. Knepley } 407847ff0e1SMatthew G. Knepley 408d763cef2SBarry Smith /* Handle specific TS options */ 409dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, setfromoptions, PetscOptionsObject); 410fbc52257SHong Zhang 411a7bdc993SLisandro Dalcin /* Handle TSAdapt options */ 4129566063dSJacob Faibussowitsch PetscCall(TSGetAdapt(ts, &ts->adapt)); 4139566063dSJacob Faibussowitsch PetscCall(TSAdaptSetDefaultType(ts->adapt, ts->default_adapt_type)); 414dbbe0bcdSBarry Smith PetscCall(TSAdaptSetFromOptions(ts->adapt, PetscOptionsObject)); 415a7bdc993SLisandro Dalcin 41668bece0bSHong Zhang /* TS trajectory must be set after TS, since it may use some TS options above */ 4174f122a70SLisandro Dalcin tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE; 4189566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_save_trajectory", "Save the solution at each timestep", "TSSetSaveTrajectory", tflg, &tflg, NULL)); 4191baa6e33SBarry Smith if (tflg) PetscCall(TSSetSaveTrajectory(ts)); 420a05bf03eSHong Zhang 421dbbe0bcdSBarry Smith PetscCall(TSAdjointSetFromOptions(ts, PetscOptionsObject)); 422d763cef2SBarry Smith 423d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 424dbbe0bcdSBarry Smith PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)ts, PetscOptionsObject)); 425d0609cedSBarry Smith PetscOptionsEnd(); 426d763cef2SBarry Smith 4271baa6e33SBarry Smith if (ts->trajectory) PetscCall(TSTrajectorySetFromOptions(ts->trajectory, ts)); 42868bece0bSHong Zhang 4291ef27442SStefano Zampini /* why do we have to do this here and not during TSSetUp? */ 4309566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &ts->snes)); 4311ef27442SStefano Zampini if (ts->problem_type == TS_LINEAR) { 4329566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompareAny((PetscObject)ts->snes, &flg, SNESKSPONLY, SNESKSPTRANSPOSEONLY, "")); 4339566063dSJacob Faibussowitsch if (!flg) PetscCall(SNESSetType(ts->snes, SNESKSPONLY)); 4341ef27442SStefano Zampini } 4359566063dSJacob Faibussowitsch PetscCall(SNESSetFromOptions(ts->snes)); 4363ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 437d763cef2SBarry Smith } 438d763cef2SBarry Smith 439d2daff3dSHong Zhang /*@ 440bcf0153eSBarry Smith TSGetTrajectory - Gets the trajectory from a `TS` if it exists 44178fbdcc8SBarry Smith 442c3339decSBarry Smith Collective 44378fbdcc8SBarry Smith 4442fe279fdSBarry Smith Input Parameter: 445bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 44678fbdcc8SBarry Smith 4472fe279fdSBarry Smith Output Parameter: 448bcf0153eSBarry Smith . tr - the `TSTrajectory` object, if it exists 44978fbdcc8SBarry Smith 45078fbdcc8SBarry Smith Level: advanced 45178fbdcc8SBarry Smith 452bcf0153eSBarry Smith Note: 453bcf0153eSBarry Smith This routine should be called after all `TS` options have been set 45478fbdcc8SBarry Smith 455b43aa488SJacob Faibussowitsch .seealso: [](ch_ts), `TS`, `TSTrajectory`, `TSAdjointSolve()`, `TSTrajectoryCreate()` 45678fbdcc8SBarry Smith @*/ 457d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTrajectory(TS ts, TSTrajectory *tr) 458d71ae5a4SJacob Faibussowitsch { 45978fbdcc8SBarry Smith PetscFunctionBegin; 46078fbdcc8SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 46178fbdcc8SBarry Smith *tr = ts->trajectory; 4623ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 46378fbdcc8SBarry Smith } 46478fbdcc8SBarry Smith 46578fbdcc8SBarry Smith /*@ 466bcf0153eSBarry Smith TSSetSaveTrajectory - Causes the `TS` to save its solutions as it iterates forward in time in a `TSTrajectory` object 467d2daff3dSHong Zhang 468c3339decSBarry Smith Collective 469d2daff3dSHong Zhang 470f899ff85SJose E. Roman Input Parameter: 471bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 472bc952696SBarry Smith 473bcf0153eSBarry Smith Options Database Keys: 47478fbdcc8SBarry Smith + -ts_save_trajectory - saves the trajectory to a file 47567b8a455SSatish Balay - -ts_trajectory_type type - set trajectory type 47678fbdcc8SBarry Smith 477d2daff3dSHong Zhang Level: intermediate 478d2daff3dSHong Zhang 479bcf0153eSBarry Smith Notes: 480bcf0153eSBarry Smith This routine should be called after all `TS` options have been set 481d2daff3dSHong Zhang 482bcf0153eSBarry Smith The `TSTRAJECTORYVISUALIZATION` files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and 483bcf0153eSBarry Smith MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m 484bcf0153eSBarry Smith 4851cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSTrajectory`, `TSGetTrajectory()`, `TSAdjointSolve()` 486d2daff3dSHong Zhang @*/ 487d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetSaveTrajectory(TS ts) 488d71ae5a4SJacob Faibussowitsch { 489d2daff3dSHong Zhang PetscFunctionBegin; 490d2daff3dSHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 49163a3b9bcSJacob Faibussowitsch if (!ts->trajectory) PetscCall(TSTrajectoryCreate(PetscObjectComm((PetscObject)ts), &ts->trajectory)); 4923ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 493d2daff3dSHong Zhang } 494d2daff3dSHong Zhang 495a7a1495cSBarry Smith /*@ 496bcf0153eSBarry Smith TSResetTrajectory - Destroys and recreates the internal `TSTrajectory` object 4972d29f1f2SStefano Zampini 498c3339decSBarry Smith Collective 4992d29f1f2SStefano Zampini 5002fe279fdSBarry Smith Input Parameter: 501bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 5022d29f1f2SStefano Zampini 5032d29f1f2SStefano Zampini Level: intermediate 5042d29f1f2SStefano Zampini 5051cc06b55SBarry Smith .seealso: [](ch_ts), `TSTrajectory`, `TSGetTrajectory()`, `TSAdjointSolve()`, `TSRemoveTrajectory()` 5062d29f1f2SStefano Zampini @*/ 507d71ae5a4SJacob Faibussowitsch PetscErrorCode TSResetTrajectory(TS ts) 508d71ae5a4SJacob Faibussowitsch { 5092d29f1f2SStefano Zampini PetscFunctionBegin; 5102d29f1f2SStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5112d29f1f2SStefano Zampini if (ts->trajectory) { 5129566063dSJacob Faibussowitsch PetscCall(TSTrajectoryDestroy(&ts->trajectory)); 5139566063dSJacob Faibussowitsch PetscCall(TSTrajectoryCreate(PetscObjectComm((PetscObject)ts), &ts->trajectory)); 5142d29f1f2SStefano Zampini } 5153ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5162d29f1f2SStefano Zampini } 5172d29f1f2SStefano Zampini 5182d29f1f2SStefano Zampini /*@ 519195e9b02SBarry Smith TSRemoveTrajectory - Destroys and removes the internal `TSTrajectory` object from a `TS` 52067a3cfb0SHong Zhang 521c3339decSBarry Smith Collective 52267a3cfb0SHong Zhang 5232fe279fdSBarry Smith Input Parameter: 524bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 52567a3cfb0SHong Zhang 52667a3cfb0SHong Zhang Level: intermediate 52767a3cfb0SHong Zhang 5281cc06b55SBarry Smith .seealso: [](ch_ts), `TSTrajectory`, `TSResetTrajectory()`, `TSAdjointSolve()` 52967a3cfb0SHong Zhang @*/ 530d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRemoveTrajectory(TS ts) 531d71ae5a4SJacob Faibussowitsch { 53267a3cfb0SHong Zhang PetscFunctionBegin; 53367a3cfb0SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5341e66621cSBarry Smith if (ts->trajectory) PetscCall(TSTrajectoryDestroy(&ts->trajectory)); 5353ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 53667a3cfb0SHong Zhang } 53767a3cfb0SHong Zhang 53867a3cfb0SHong Zhang /*@ 539a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 540bcf0153eSBarry Smith set with `TSSetRHSJacobian()`. 541a7a1495cSBarry Smith 542c3339decSBarry Smith Collective 543a7a1495cSBarry Smith 544a7a1495cSBarry Smith Input Parameters: 545bcf0153eSBarry Smith + ts - the `TS` context 546a7a1495cSBarry Smith . t - current timestep 5470910c330SBarry Smith - U - input vector 548a7a1495cSBarry Smith 549a7a1495cSBarry Smith Output Parameters: 550a7a1495cSBarry Smith + A - Jacobian matrix 5516b867d5aSJose E. Roman - B - optional preconditioning matrix 552a7a1495cSBarry Smith 553bcf0153eSBarry Smith Level: developer 554bcf0153eSBarry Smith 555bcf0153eSBarry Smith Note: 556a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 557a7a1495cSBarry Smith is used internally within the nonlinear solvers. 558a7a1495cSBarry Smith 5591cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSJacobian()`, `KSPSetOperators()` 560a7a1495cSBarry Smith @*/ 561d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSJacobian(TS ts, PetscReal t, Vec U, Mat A, Mat B) 562d71ae5a4SJacob Faibussowitsch { 563270bf2e7SJed Brown PetscObjectState Ustate; 5646c1e1eecSBarry Smith PetscObjectId Uid; 56524989b8cSPeter Brune DM dm; 566942e3340SBarry Smith DMTS tsdm; 5678434afd1SBarry Smith TSRHSJacobianFn *rhsjacobianfunc; 56824989b8cSPeter Brune void *ctx; 5698434afd1SBarry Smith TSRHSFunctionFn *rhsfunction; 570a7a1495cSBarry Smith 571a7a1495cSBarry Smith PetscFunctionBegin; 5720700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5730910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 5740910c330SBarry Smith PetscCheckSameComm(ts, 1, U, 3); 5759566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 5769566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm, &tsdm)); 5779566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, NULL)); 5789566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, &rhsjacobianfunc, &ctx)); 5799566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &Ustate)); 5809566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U, &Uid)); 581971015bcSStefano Zampini 5823ba16761SJacob Faibussowitsch if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) PetscFunctionReturn(PETSC_SUCCESS); 583d90be118SSean Farley 58463a3b9bcSJacob 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); 58524989b8cSPeter Brune if (rhsjacobianfunc) { 586da023ba9SStefano Zampini PetscCall(PetscLogEventBegin(TS_JacobianEval, U, ts, A, B)); 587792fecdfSBarry Smith PetscCallBack("TS callback Jacobian", (*rhsjacobianfunc)(ts, t, U, A, B, ctx)); 588a6ab3590SBarry Smith ts->rhsjacs++; 589da023ba9SStefano Zampini PetscCall(PetscLogEventEnd(TS_JacobianEval, U, ts, A, B)); 590ef66eb69SBarry Smith } else { 5919566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(A)); 5929566063dSJacob Faibussowitsch if (B && A != B) PetscCall(MatZeroEntries(B)); 593ef66eb69SBarry Smith } 5940e4ef248SJed Brown ts->rhsjacobian.time = t; 595971015bcSStefano Zampini ts->rhsjacobian.shift = 0; 596971015bcSStefano Zampini ts->rhsjacobian.scale = 1.; 5979566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U, &ts->rhsjacobian.Xid)); 5989566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &ts->rhsjacobian.Xstate)); 5993ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 600a7a1495cSBarry Smith } 601a7a1495cSBarry Smith 602316643e7SJed Brown /*@ 603bcf0153eSBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function for a `TS` 604d763cef2SBarry Smith 605c3339decSBarry Smith Collective 606316643e7SJed Brown 607316643e7SJed Brown Input Parameters: 608bcf0153eSBarry Smith + ts - the `TS` context 609316643e7SJed Brown . t - current time 6100910c330SBarry Smith - U - state vector 611316643e7SJed Brown 612316643e7SJed Brown Output Parameter: 613dd8e379bSPierre Jolivet . y - right-hand side 614316643e7SJed Brown 615bcf0153eSBarry Smith Level: developer 616bcf0153eSBarry Smith 617316643e7SJed Brown Note: 618316643e7SJed Brown Most users should not need to explicitly call this routine, as it 619316643e7SJed Brown is used internally within the nonlinear solvers. 620316643e7SJed Brown 6211cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSFunction()`, `TSComputeIFunction()` 622316643e7SJed Brown @*/ 623d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSFunction(TS ts, PetscReal t, Vec U, Vec y) 624d71ae5a4SJacob Faibussowitsch { 6258434afd1SBarry Smith TSRHSFunctionFn *rhsfunction; 6268434afd1SBarry Smith TSIFunctionFn *ifunction; 62724989b8cSPeter Brune void *ctx; 62824989b8cSPeter Brune DM dm; 62924989b8cSPeter Brune 630d763cef2SBarry Smith PetscFunctionBegin; 6310700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 6320910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 6330700a824SBarry Smith PetscValidHeaderSpecific(y, VEC_CLASSID, 4); 6349566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 6359566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, &ctx)); 6369566063dSJacob Faibussowitsch PetscCall(DMTSGetIFunction(dm, &ifunction, NULL)); 637d763cef2SBarry Smith 6383c633725SBarry Smith PetscCheck(rhsfunction || ifunction, PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "Must call TSSetRHSFunction() and / or TSSetIFunction()"); 639d763cef2SBarry Smith 64024989b8cSPeter Brune if (rhsfunction) { 641da023ba9SStefano Zampini PetscCall(PetscLogEventBegin(TS_FunctionEval, U, ts, y, 0)); 6429566063dSJacob Faibussowitsch PetscCall(VecLockReadPush(U)); 643792fecdfSBarry Smith PetscCallBack("TS callback right-hand-side", (*rhsfunction)(ts, t, U, y, ctx)); 6449566063dSJacob Faibussowitsch PetscCall(VecLockReadPop(U)); 645a6ab3590SBarry Smith ts->rhsfuncs++; 646da023ba9SStefano Zampini PetscCall(PetscLogEventEnd(TS_FunctionEval, U, ts, y, 0)); 6471e66621cSBarry Smith } else PetscCall(VecZeroEntries(y)); 6483ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 649d763cef2SBarry Smith } 650d763cef2SBarry Smith 651ef20d060SBarry Smith /*@ 652ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 653ef20d060SBarry Smith 654c3339decSBarry Smith Collective 655ef20d060SBarry Smith 656ef20d060SBarry Smith Input Parameters: 657bcf0153eSBarry Smith + ts - the `TS` context 658ef20d060SBarry Smith - t - current time 659ef20d060SBarry Smith 660ef20d060SBarry Smith Output Parameter: 6610910c330SBarry Smith . U - the solution 662ef20d060SBarry Smith 663ef20d060SBarry Smith Level: developer 664ef20d060SBarry Smith 6651cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetSolutionFunction()`, `TSSetRHSFunction()`, `TSComputeIFunction()` 666ef20d060SBarry Smith @*/ 667d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeSolutionFunction(TS ts, PetscReal t, Vec U) 668d71ae5a4SJacob Faibussowitsch { 6698434afd1SBarry Smith TSSolutionFn *solutionfunction; 670ef20d060SBarry Smith void *ctx; 671ef20d060SBarry Smith DM dm; 672ef20d060SBarry Smith 673ef20d060SBarry Smith PetscFunctionBegin; 674ef20d060SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 6750910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 6769566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 6779566063dSJacob Faibussowitsch PetscCall(DMTSGetSolutionFunction(dm, &solutionfunction, &ctx)); 678792fecdfSBarry Smith if (solutionfunction) PetscCallBack("TS callback solution", (*solutionfunction)(ts, t, U, ctx)); 6793ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 680ef20d060SBarry Smith } 6819b7cd975SBarry Smith /*@ 6829b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 6839b7cd975SBarry Smith 684c3339decSBarry Smith Collective 6859b7cd975SBarry Smith 6869b7cd975SBarry Smith Input Parameters: 687bcf0153eSBarry Smith + ts - the `TS` context 6889b7cd975SBarry Smith - t - current time 6899b7cd975SBarry Smith 6909b7cd975SBarry Smith Output Parameter: 6919b7cd975SBarry Smith . U - the function value 6929b7cd975SBarry Smith 6939b7cd975SBarry Smith Level: developer 6949b7cd975SBarry Smith 6951cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetSolutionFunction()`, `TSSetRHSFunction()`, `TSComputeIFunction()` 6969b7cd975SBarry Smith @*/ 697d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeForcingFunction(TS ts, PetscReal t, Vec U) 698d71ae5a4SJacob Faibussowitsch { 6999b7cd975SBarry Smith void *ctx; 7009b7cd975SBarry Smith DM dm; 7018434afd1SBarry Smith TSForcingFn *forcing; 7029b7cd975SBarry Smith 7039b7cd975SBarry Smith PetscFunctionBegin; 7049b7cd975SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 7059b7cd975SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 7069566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 7079566063dSJacob Faibussowitsch PetscCall(DMTSGetForcingFunction(dm, &forcing, &ctx)); 7089b7cd975SBarry Smith 709792fecdfSBarry Smith if (forcing) PetscCallBack("TS callback forcing function", (*forcing)(ts, t, U, ctx)); 7103ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7119b7cd975SBarry Smith } 712ef20d060SBarry Smith 713d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetRHSMats_Private(TS ts, Mat *Arhs, Mat *Brhs) 714d71ae5a4SJacob Faibussowitsch { 715214bc6a2SJed Brown Mat A, B; 7168434afd1SBarry Smith TSIJacobianFn *ijacobian; 717214bc6a2SJed Brown 718214bc6a2SJed Brown PetscFunctionBegin; 719c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 720c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 7219566063dSJacob Faibussowitsch PetscCall(TSGetIJacobian(ts, &A, &B, &ijacobian, NULL)); 722214bc6a2SJed Brown if (Arhs) { 723214bc6a2SJed Brown if (!ts->Arhs) { 72441a1d4d2SBarry Smith if (ijacobian) { 7259566063dSJacob Faibussowitsch PetscCall(MatDuplicate(A, MAT_DO_NOT_COPY_VALUES, &ts->Arhs)); 7269566063dSJacob Faibussowitsch PetscCall(TSSetMatStructure(ts, SAME_NONZERO_PATTERN)); 72741a1d4d2SBarry Smith } else { 72841a1d4d2SBarry Smith ts->Arhs = A; 7299566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)A)); 73041a1d4d2SBarry Smith } 7313565c898SBarry Smith } else { 7323565c898SBarry Smith PetscBool flg; 7339566063dSJacob Faibussowitsch PetscCall(SNESGetUseMatrixFree(ts->snes, NULL, &flg)); 7343565c898SBarry Smith /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */ 7353565c898SBarry Smith if (flg && !ijacobian && ts->Arhs == ts->Brhs) { 7369566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)ts->Arhs)); 7373565c898SBarry Smith ts->Arhs = A; 7389566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)A)); 7393565c898SBarry Smith } 740214bc6a2SJed Brown } 741214bc6a2SJed Brown *Arhs = ts->Arhs; 742214bc6a2SJed Brown } 743214bc6a2SJed Brown if (Brhs) { 744214bc6a2SJed Brown if (!ts->Brhs) { 745bdb70873SJed Brown if (A != B) { 74641a1d4d2SBarry Smith if (ijacobian) { 7479566063dSJacob Faibussowitsch PetscCall(MatDuplicate(B, MAT_DO_NOT_COPY_VALUES, &ts->Brhs)); 748bdb70873SJed Brown } else { 74941a1d4d2SBarry Smith ts->Brhs = B; 7509566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)B)); 75141a1d4d2SBarry Smith } 75241a1d4d2SBarry Smith } else { 7539566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)ts->Arhs)); 75451699248SLisandro Dalcin ts->Brhs = ts->Arhs; 755bdb70873SJed Brown } 756214bc6a2SJed Brown } 757214bc6a2SJed Brown *Brhs = ts->Brhs; 758214bc6a2SJed Brown } 7593ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 760214bc6a2SJed Brown } 761214bc6a2SJed Brown 762316643e7SJed Brown /*@ 763195e9b02SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in the implicit form F(t,U,Udot)=0 764316643e7SJed Brown 765c3339decSBarry Smith Collective 766316643e7SJed Brown 767316643e7SJed Brown Input Parameters: 768bcf0153eSBarry Smith + ts - the `TS` context 769316643e7SJed Brown . t - current time 7700910c330SBarry Smith . U - state vector 7710910c330SBarry Smith . Udot - time derivative of state vector 7723dddbd81SStefano Zampini - imex - flag indicates if the method is `TSARKIMEX` so that the RHSFunction should be kept separate 773316643e7SJed Brown 774316643e7SJed Brown Output Parameter: 775dd8e379bSPierre Jolivet . Y - right-hand side 776316643e7SJed Brown 777bcf0153eSBarry Smith Level: developer 778bcf0153eSBarry Smith 779316643e7SJed Brown Note: 780316643e7SJed Brown Most users should not need to explicitly call this routine, as it 781316643e7SJed Brown is used internally within the nonlinear solvers. 782316643e7SJed Brown 78315229ffcSPierre Jolivet If the user did not write their equations in implicit form, this 784316643e7SJed Brown function recasts them in implicit form. 785316643e7SJed Brown 7861cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetIFunction()`, `TSComputeRHSFunction()` 787316643e7SJed Brown @*/ 788d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIFunction(TS ts, PetscReal t, Vec U, Vec Udot, Vec Y, PetscBool imex) 789d71ae5a4SJacob Faibussowitsch { 7908434afd1SBarry Smith TSIFunctionFn *ifunction; 7918434afd1SBarry Smith TSRHSFunctionFn *rhsfunction; 79224989b8cSPeter Brune void *ctx; 79324989b8cSPeter Brune DM dm; 794316643e7SJed Brown 795316643e7SJed Brown PetscFunctionBegin; 7960700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 7970910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 7980910c330SBarry Smith PetscValidHeaderSpecific(Udot, VEC_CLASSID, 4); 7990700a824SBarry Smith PetscValidHeaderSpecific(Y, VEC_CLASSID, 5); 800316643e7SJed Brown 8019566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 8029566063dSJacob Faibussowitsch PetscCall(DMTSGetIFunction(dm, &ifunction, &ctx)); 8039566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, NULL)); 80424989b8cSPeter Brune 8053c633725SBarry Smith PetscCheck(rhsfunction || ifunction, PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "Must call TSSetRHSFunction() and / or TSSetIFunction()"); 806d90be118SSean Farley 807da023ba9SStefano Zampini PetscCall(PetscLogEventBegin(TS_FunctionEval, U, ts, Udot, Y)); 80824989b8cSPeter Brune if (ifunction) { 809792fecdfSBarry Smith PetscCallBack("TS callback implicit function", (*ifunction)(ts, t, U, Udot, Y, ctx)); 810a6ab3590SBarry Smith ts->ifuncs++; 811214bc6a2SJed Brown } 812214bc6a2SJed Brown if (imex) { 8131e66621cSBarry Smith if (!ifunction) PetscCall(VecCopy(Udot, Y)); 81424989b8cSPeter Brune } else if (rhsfunction) { 81524989b8cSPeter Brune if (ifunction) { 816214bc6a2SJed Brown Vec Frhs; 8178af0501fSStefano Zampini 8188af0501fSStefano Zampini PetscCall(DMGetGlobalVector(dm, &Frhs)); 8199566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts, t, U, Frhs)); 8209566063dSJacob Faibussowitsch PetscCall(VecAXPY(Y, -1, Frhs)); 8218af0501fSStefano Zampini PetscCall(DMRestoreGlobalVector(dm, &Frhs)); 8222dd45cf8SJed Brown } else { 8239566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts, t, U, Y)); 8249566063dSJacob Faibussowitsch PetscCall(VecAYPX(Y, -1, Udot)); 825316643e7SJed Brown } 8264a6899ffSJed Brown } 827da023ba9SStefano Zampini PetscCall(PetscLogEventEnd(TS_FunctionEval, U, ts, Udot, Y)); 8283ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 829316643e7SJed Brown } 830316643e7SJed Brown 831cfa8a9a2SHong Zhang /* 832195e9b02SBarry Smith TSRecoverRHSJacobian - Recover the Jacobian matrix so that one can call `TSComputeRHSJacobian()` on it. 833cfa8a9a2SHong Zhang 834cfa8a9a2SHong Zhang Note: 835195e9b02SBarry Smith This routine is needed when one switches from `TSComputeIJacobian()` to `TSComputeRHSJacobian()` because the Jacobian matrix may be shifted or scaled in `TSComputeIJacobian()`. 836cfa8a9a2SHong Zhang 837cfa8a9a2SHong Zhang */ 838d71ae5a4SJacob Faibussowitsch static PetscErrorCode TSRecoverRHSJacobian(TS ts, Mat A, Mat B) 839d71ae5a4SJacob Faibussowitsch { 840cfa8a9a2SHong Zhang PetscFunctionBegin; 841cfa8a9a2SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 8423c633725SBarry Smith PetscCheck(A == ts->Arhs, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Invalid Amat"); 8433c633725SBarry Smith PetscCheck(B == ts->Brhs, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Invalid Bmat"); 844cfa8a9a2SHong Zhang 8451baa6e33SBarry Smith if (ts->rhsjacobian.shift) PetscCall(MatShift(A, -ts->rhsjacobian.shift)); 84648a46eb9SPierre Jolivet if (ts->rhsjacobian.scale == -1.) PetscCall(MatScale(A, -1)); 847cfa8a9a2SHong Zhang if (B && B == ts->Brhs && A != B) { 8481baa6e33SBarry Smith if (ts->rhsjacobian.shift) PetscCall(MatShift(B, -ts->rhsjacobian.shift)); 8491e66621cSBarry Smith if (ts->rhsjacobian.scale == -1.) PetscCall(MatScale(B, -1)); 850cfa8a9a2SHong Zhang } 851cfa8a9a2SHong Zhang ts->rhsjacobian.shift = 0; 852cfa8a9a2SHong Zhang ts->rhsjacobian.scale = 1.; 8533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 854cfa8a9a2SHong Zhang } 855cfa8a9a2SHong Zhang 856316643e7SJed Brown /*@ 857316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 858316643e7SJed Brown 859c3339decSBarry Smith Collective 860316643e7SJed Brown 861316643e7SJed Brown Input Parameters: 862bcf0153eSBarry Smith + ts - the `TS` context 863316643e7SJed Brown . t - current timestep 8640910c330SBarry Smith . U - state vector 8650910c330SBarry Smith . Udot - time derivative of state vector 866214bc6a2SJed Brown . shift - shift to apply, see note below 8673dddbd81SStefano Zampini - imex - flag indicates if the method is `TSARKIMEX` so that the RHSJacobian should be kept separate 868316643e7SJed Brown 869316643e7SJed Brown Output Parameters: 870316643e7SJed Brown + A - Jacobian matrix 871195e9b02SBarry Smith - B - matrix from which the preconditioner is constructed; often the same as `A` 872316643e7SJed Brown 873bcf0153eSBarry Smith Level: developer 874bcf0153eSBarry Smith 875316643e7SJed Brown Notes: 8760910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 877195e9b02SBarry Smith .vb 8780910c330SBarry Smith dF/dU + shift*dF/dUdot 879195e9b02SBarry Smith .ve 880316643e7SJed Brown Most users should not need to explicitly call this routine, as it 881316643e7SJed Brown is used internally within the nonlinear solvers. 882316643e7SJed Brown 8831cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetIJacobian()` 88463495f91SJed Brown @*/ 885d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIJacobian(TS ts, PetscReal t, Vec U, Vec Udot, PetscReal shift, Mat A, Mat B, PetscBool imex) 886d71ae5a4SJacob Faibussowitsch { 8878434afd1SBarry Smith TSIJacobianFn *ijacobian; 8888434afd1SBarry Smith TSRHSJacobianFn *rhsjacobian; 88924989b8cSPeter Brune DM dm; 89024989b8cSPeter Brune void *ctx; 891316643e7SJed Brown 892316643e7SJed Brown PetscFunctionBegin; 8930700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 8940910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 8950910c330SBarry Smith PetscValidHeaderSpecific(Udot, VEC_CLASSID, 4); 89694ab13aaSBarry Smith PetscValidHeaderSpecific(A, MAT_CLASSID, 6); 89794ab13aaSBarry Smith PetscValidHeaderSpecific(B, MAT_CLASSID, 7); 89824989b8cSPeter Brune 8999566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 9009566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm, &ijacobian, &ctx)); 9019566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, &rhsjacobian, NULL)); 90224989b8cSPeter Brune 9033c633725SBarry Smith PetscCheck(rhsjacobian || ijacobian, PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 904316643e7SJed Brown 905da023ba9SStefano Zampini PetscCall(PetscLogEventBegin(TS_JacobianEval, U, ts, A, B)); 90624989b8cSPeter Brune if (ijacobian) { 907792fecdfSBarry Smith PetscCallBack("TS callback implicit Jacobian", (*ijacobian)(ts, t, U, Udot, shift, A, B, ctx)); 908a6ab3590SBarry Smith ts->ijacs++; 9094a6899ffSJed Brown } 910214bc6a2SJed Brown if (imex) { 911b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 9124c26be97Sstefano_zampini PetscBool assembled; 913971015bcSStefano Zampini if (rhsjacobian) { 914971015bcSStefano Zampini Mat Arhs = NULL; 9159566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts, &Arhs, NULL)); 916971015bcSStefano Zampini if (A == Arhs) { 9173c633725SBarry 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 */ 918971015bcSStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 919971015bcSStefano Zampini } 920971015bcSStefano Zampini } 9219566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(A)); 9229566063dSJacob Faibussowitsch PetscCall(MatAssembled(A, &assembled)); 9234c26be97Sstefano_zampini if (!assembled) { 9249566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); 9259566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); 9264c26be97Sstefano_zampini } 9279566063dSJacob Faibussowitsch PetscCall(MatShift(A, shift)); 92894ab13aaSBarry Smith if (A != B) { 9299566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(B)); 9309566063dSJacob Faibussowitsch PetscCall(MatAssembled(B, &assembled)); 9314c26be97Sstefano_zampini if (!assembled) { 9329566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY)); 9339566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY)); 9344c26be97Sstefano_zampini } 9359566063dSJacob Faibussowitsch PetscCall(MatShift(B, shift)); 936214bc6a2SJed Brown } 937214bc6a2SJed Brown } 938214bc6a2SJed Brown } else { 939e1244c69SJed Brown Mat Arhs = NULL, Brhs = NULL; 9401e66621cSBarry Smith 9411e66621cSBarry Smith /* RHSJacobian needs to be converted to part of IJacobian if exists */ 9421e66621cSBarry Smith if (rhsjacobian) PetscCall(TSGetRHSMats_Private(ts, &Arhs, &Brhs)); 943e8b1e424SHong Zhang if (Arhs == A) { /* No IJacobian matrix, so we only have the RHS matrix */ 944e8b1e424SHong Zhang PetscObjectState Ustate; 945e8b1e424SHong Zhang PetscObjectId Uid; 9468434afd1SBarry Smith TSRHSFunctionFn *rhsfunction; 947e8b1e424SHong Zhang 9489566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, NULL)); 9499566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &Ustate)); 9509566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U, &Uid)); 9519371c9d4SSatish Balay if ((rhsjacobian == TSComputeRHSJacobianConstant || (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && rhsfunction != TSComputeRHSFunctionLinear)) && 9529371c9d4SSatish Balay ts->rhsjacobian.scale == -1.) { /* No need to recompute RHSJacobian */ 9539566063dSJacob Faibussowitsch PetscCall(MatShift(A, shift - ts->rhsjacobian.shift)); /* revert the old shift and add the new shift with a single call to MatShift */ 9541e66621cSBarry Smith if (A != B) PetscCall(MatShift(B, shift - ts->rhsjacobian.shift)); 955e8b1e424SHong Zhang } else { 9563565c898SBarry Smith PetscBool flg; 957e8b1e424SHong Zhang 958e8b1e424SHong Zhang if (ts->rhsjacobian.reuse) { /* Undo the damage */ 959e8b1e424SHong Zhang /* MatScale has a short path for this case. 960e8b1e424SHong Zhang However, this code path is taken the first time TSComputeRHSJacobian is called 961e8b1e424SHong Zhang and the matrices have not been assembled yet */ 9629566063dSJacob Faibussowitsch PetscCall(TSRecoverRHSJacobian(ts, A, B)); 963e8b1e424SHong Zhang } 9649566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts, t, U, A, B)); 9659566063dSJacob Faibussowitsch PetscCall(SNESGetUseMatrixFree(ts->snes, NULL, &flg)); 9663565c898SBarry Smith /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */ 9673565c898SBarry Smith if (!flg) { 9689566063dSJacob Faibussowitsch PetscCall(MatScale(A, -1)); 9699566063dSJacob Faibussowitsch PetscCall(MatShift(A, shift)); 9703565c898SBarry Smith } 97194ab13aaSBarry Smith if (A != B) { 9729566063dSJacob Faibussowitsch PetscCall(MatScale(B, -1)); 9739566063dSJacob Faibussowitsch PetscCall(MatShift(B, shift)); 974316643e7SJed Brown } 975e8b1e424SHong Zhang } 976e8b1e424SHong Zhang ts->rhsjacobian.scale = -1; 977e8b1e424SHong Zhang ts->rhsjacobian.shift = shift; 978d60b7d5cSBarry Smith } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 979e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 9809566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(A)); 9819566063dSJacob Faibussowitsch PetscCall(MatShift(A, shift)); 98294ab13aaSBarry Smith if (A != B) { 9839566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(B)); 9849566063dSJacob Faibussowitsch PetscCall(MatShift(B, shift)); 985214bc6a2SJed Brown } 986316643e7SJed Brown } 9879566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts, t, U, Arhs, Brhs)); 9889566063dSJacob Faibussowitsch PetscCall(MatAXPY(A, -1, Arhs, ts->axpy_pattern)); 9891e66621cSBarry Smith if (A != B) PetscCall(MatAXPY(B, -1, Brhs, ts->axpy_pattern)); 990316643e7SJed Brown } 991316643e7SJed Brown } 992da023ba9SStefano Zampini PetscCall(PetscLogEventEnd(TS_JacobianEval, U, ts, A, B)); 9933ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 994316643e7SJed Brown } 995316643e7SJed Brown 996d763cef2SBarry Smith /*@C 997d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 998b5abc632SBarry Smith where U_t = G(t,u). 999d763cef2SBarry Smith 1000c3339decSBarry Smith Logically Collective 1001d763cef2SBarry Smith 1002d763cef2SBarry Smith Input Parameters: 1003bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1004dd8e379bSPierre Jolivet . r - vector to put the computed right-hand side (or `NULL` to have it created) 1005d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 1006195e9b02SBarry Smith - ctx - [optional] user-defined context for private data for the function evaluation routine (may be `NULL`) 1007d763cef2SBarry Smith 1008d763cef2SBarry Smith Level: beginner 1009d763cef2SBarry Smith 1010bcf0153eSBarry Smith Note: 1011bcf0153eSBarry Smith You must call this function or `TSSetIFunction()` to define your ODE. You cannot use this function when solving a DAE. 10122bbac0d3SBarry Smith 10138434afd1SBarry Smith .seealso: [](ch_ts), `TS`, `TSRHSFunctionFn`, `TSSetRHSJacobian()`, `TSSetIJacobian()`, `TSSetIFunction()` 1014d763cef2SBarry Smith @*/ 10158434afd1SBarry Smith PetscErrorCode TSSetRHSFunction(TS ts, Vec r, TSRHSFunctionFn *f, void *ctx) 1016d71ae5a4SJacob Faibussowitsch { 1017089b2837SJed Brown SNES snes; 10180298fd71SBarry Smith Vec ralloc = NULL; 101924989b8cSPeter Brune DM dm; 1020d763cef2SBarry Smith 1021089b2837SJed Brown PetscFunctionBegin; 10220700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1023ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r, VEC_CLASSID, 2); 102424989b8cSPeter Brune 10259566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 10269566063dSJacob Faibussowitsch PetscCall(DMTSSetRHSFunction(dm, f, ctx)); 10279566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 1028e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 10299566063dSJacob Faibussowitsch PetscCall(VecDuplicate(ts->vec_sol, &ralloc)); 1030e856ceecSJed Brown r = ralloc; 1031e856ceecSJed Brown } 10329566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(snes, r, SNESTSFormFunction, ts)); 10339566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ralloc)); 10343ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1035d763cef2SBarry Smith } 1036d763cef2SBarry Smith 1037ef20d060SBarry Smith /*@C 1038abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1039ef20d060SBarry Smith 1040c3339decSBarry Smith Logically Collective 1041ef20d060SBarry Smith 1042ef20d060SBarry Smith Input Parameters: 1043bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1044ef20d060SBarry Smith . f - routine for evaluating the solution 1045ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 1046195e9b02SBarry Smith function evaluation routine (may be `NULL`) 1047ef20d060SBarry Smith 1048bcf0153eSBarry Smith Options Database Keys: 1049bcf0153eSBarry Smith + -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided `TSSetSolutionFunction()` 1050bcf0153eSBarry Smith - -ts_monitor_draw_error - Monitor error graphically, requires user to have provided `TSSetSolutionFunction()` 1051bcf0153eSBarry Smith 1052bcf0153eSBarry Smith Level: intermediate 10530ed3bfb6SBarry Smith 1054abd5a294SJed Brown Notes: 1055abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 1056abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 1057abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 1058abd5a294SJed Brown 1059bcf0153eSBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, `TSMonitorLGError()` can be used to monitor the error history. 1060abd5a294SJed Brown 10618434afd1SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolutionFn`, `TSSetRHSJacobian()`, `TSSetIJacobian()`, `TSComputeSolutionFunction()`, `TSSetForcingFunction()`, `TSSetSolution()`, `TSGetSolution()`, `TSMonitorLGError()`, `TSMonitorDrawError()` 1062ef20d060SBarry Smith @*/ 10638434afd1SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts, TSSolutionFn *f, void *ctx) 1064d71ae5a4SJacob Faibussowitsch { 1065ef20d060SBarry Smith DM dm; 1066ef20d060SBarry Smith 1067ef20d060SBarry Smith PetscFunctionBegin; 1068ef20d060SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 10699566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 10709566063dSJacob Faibussowitsch PetscCall(DMTSSetSolutionFunction(dm, f, ctx)); 10713ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1072ef20d060SBarry Smith } 1073ef20d060SBarry Smith 10749b7cd975SBarry Smith /*@C 10759b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 10769b7cd975SBarry Smith 1077c3339decSBarry Smith Logically Collective 10789b7cd975SBarry Smith 10799b7cd975SBarry Smith Input Parameters: 1080bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1081e162b725SBarry Smith . func - routine for evaluating the forcing function 108214d0ab18SJacob Faibussowitsch - ctx - [optional] user-defined context for private data for the function evaluation routine 108314d0ab18SJacob Faibussowitsch (may be `NULL`) 10849b7cd975SBarry Smith 1085bcf0153eSBarry Smith Level: intermediate 1086bcf0153eSBarry Smith 10879b7cd975SBarry Smith Notes: 10889b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 1089e162b725SBarry 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 1090e162b725SBarry Smith definition of the problem you are solving and hence possibly introducing bugs. 1091e162b725SBarry Smith 10928847d985SBarry Smith This replaces the ODE F(u,u_t,t) = 0 the `TS` is solving with F(u,u_t,t) - func(t) = 0 1093e162b725SBarry Smith 1094e162b725SBarry 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 1095e162b725SBarry Smith parameters can be passed in the ctx variable. 10969b7cd975SBarry Smith 1097bcf0153eSBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, `TSMonitorLGError()` can be used to monitor the error history. 10989b7cd975SBarry Smith 10998434afd1SBarry Smith .seealso: [](ch_ts), `TS`, `TSForcingFn`, `TSSetRHSJacobian()`, `TSSetIJacobian()`, 110014d0ab18SJacob Faibussowitsch `TSComputeSolutionFunction()`, `TSSetSolutionFunction()` 11019b7cd975SBarry Smith @*/ 11028434afd1SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts, TSForcingFn *func, void *ctx) 1103d71ae5a4SJacob Faibussowitsch { 11049b7cd975SBarry Smith DM dm; 11059b7cd975SBarry Smith 11069b7cd975SBarry Smith PetscFunctionBegin; 11079b7cd975SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 11089566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 11099566063dSJacob Faibussowitsch PetscCall(DMTSSetForcingFunction(dm, func, ctx)); 11103ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 11119b7cd975SBarry Smith } 11129b7cd975SBarry Smith 1113d763cef2SBarry Smith /*@C 1114f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1115b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1116d763cef2SBarry Smith 1117c3339decSBarry Smith Logically Collective 1118d763cef2SBarry Smith 1119d763cef2SBarry Smith Input Parameters: 1120bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1121195e9b02SBarry Smith . Amat - (approximate) location to store Jacobian matrix entries computed by `f` 1122195e9b02SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as `Amat`) 1123d763cef2SBarry Smith . f - the Jacobian evaluation routine 1124195e9b02SBarry Smith - ctx - [optional] user-defined context for private data for the Jacobian evaluation routine (may be `NULL`) 1125d763cef2SBarry Smith 1126bcf0153eSBarry Smith Level: beginner 1127bcf0153eSBarry Smith 11286cd88445SBarry Smith Notes: 11296cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 11306cd88445SBarry Smith 113114d0ab18SJacob Faibussowitsch The `TS` solver may modify the nonzero structure and the entries of the matrices `Amat` and `Pmat` between the calls to `f()` 1132ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1133d763cef2SBarry Smith 11348434afd1SBarry Smith .seealso: [](ch_ts), `TS`, `TSRHSJacobianFn`, `SNESComputeJacobianDefaultColor()`, 11358434afd1SBarry Smith `TSSetRHSFunction()`, `TSRHSJacobianSetReuse()`, `TSSetIJacobian()`, `TSRHSFunctionFn`, `TSIFunctionFn` 1136d763cef2SBarry Smith @*/ 11378434afd1SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts, Mat Amat, Mat Pmat, TSRHSJacobianFn *f, void *ctx) 1138d71ae5a4SJacob Faibussowitsch { 1139089b2837SJed Brown SNES snes; 114024989b8cSPeter Brune DM dm; 11418434afd1SBarry Smith TSIJacobianFn *ijacobian; 1142277b19d0SLisandro Dalcin 1143d763cef2SBarry Smith PetscFunctionBegin; 11440700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1145e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat, MAT_CLASSID, 2); 1146e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat, MAT_CLASSID, 3); 1147e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts, 1, Amat, 2); 1148e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts, 1, Pmat, 3); 1149d763cef2SBarry Smith 11509566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 11519566063dSJacob Faibussowitsch PetscCall(DMTSSetRHSJacobian(dm, f, ctx)); 11529566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm, &ijacobian, NULL)); 11539566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 11541e66621cSBarry Smith if (!ijacobian) PetscCall(SNESSetJacobian(snes, Amat, Pmat, SNESTSFormJacobian, ts)); 1155e5d3d808SBarry Smith if (Amat) { 11569566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)Amat)); 11579566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Arhs)); 1158e5d3d808SBarry Smith ts->Arhs = Amat; 11590e4ef248SJed Brown } 1160e5d3d808SBarry Smith if (Pmat) { 11619566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)Pmat)); 11629566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Brhs)); 1163e5d3d808SBarry Smith ts->Brhs = Pmat; 11640e4ef248SJed Brown } 11653ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1166d763cef2SBarry Smith } 1167d763cef2SBarry Smith 1168316643e7SJed Brown /*@C 1169b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1170316643e7SJed Brown 1171c3339decSBarry Smith Logically Collective 1172316643e7SJed Brown 1173316643e7SJed Brown Input Parameters: 1174bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1175195e9b02SBarry Smith . r - vector to hold the residual (or `NULL` to have it created internally) 1176316643e7SJed Brown . f - the function evaluation routine 1177195e9b02SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be `NULL`) 1178316643e7SJed Brown 1179316643e7SJed Brown Level: beginner 1180316643e7SJed Brown 1181bcf0153eSBarry Smith Note: 1182bcf0153eSBarry Smith The user MUST call either this routine or `TSSetRHSFunction()` to define the ODE. When solving DAEs you must use this function. 1183bcf0153eSBarry Smith 11848434afd1SBarry Smith .seealso: [](ch_ts), `TS`, `TSIFunctionFn`, `TSSetRHSJacobian()`, `TSSetRHSFunction()`, 118514d0ab18SJacob Faibussowitsch `TSSetIJacobian()` 1186316643e7SJed Brown @*/ 11878434afd1SBarry Smith PetscErrorCode TSSetIFunction(TS ts, Vec r, TSIFunctionFn *f, void *ctx) 1188d71ae5a4SJacob Faibussowitsch { 1189089b2837SJed Brown SNES snes; 119051699248SLisandro Dalcin Vec ralloc = NULL; 119124989b8cSPeter Brune DM dm; 1192316643e7SJed Brown 1193316643e7SJed Brown PetscFunctionBegin; 11940700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 119551699248SLisandro Dalcin if (r) PetscValidHeaderSpecific(r, VEC_CLASSID, 2); 119624989b8cSPeter Brune 11979566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 11989566063dSJacob Faibussowitsch PetscCall(DMTSSetIFunction(dm, f, ctx)); 119924989b8cSPeter Brune 12009566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 120151699248SLisandro Dalcin if (!r && !ts->dm && ts->vec_sol) { 12029566063dSJacob Faibussowitsch PetscCall(VecDuplicate(ts->vec_sol, &ralloc)); 120351699248SLisandro Dalcin r = ralloc; 1204e856ceecSJed Brown } 12059566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(snes, r, SNESTSFormFunction, ts)); 12069566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ralloc)); 12073ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1208089b2837SJed Brown } 1209089b2837SJed Brown 1210089b2837SJed Brown /*@C 1211a5b23f4aSJose E. Roman TSGetIFunction - Returns the vector where the implicit residual is stored and the function/context to compute it. 1212089b2837SJed Brown 1213089b2837SJed Brown Not Collective 1214089b2837SJed Brown 1215089b2837SJed Brown Input Parameter: 1216bcf0153eSBarry Smith . ts - the `TS` context 1217089b2837SJed Brown 1218d8d19677SJose E. Roman Output Parameters: 1219195e9b02SBarry Smith + r - vector to hold residual (or `NULL`) 1220195e9b02SBarry Smith . func - the function to compute residual (or `NULL`) 1221195e9b02SBarry Smith - ctx - the function context (or `NULL`) 1222089b2837SJed Brown 1223089b2837SJed Brown Level: advanced 1224089b2837SJed Brown 12251cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetIFunction()`, `SNESGetFunction()` 1226089b2837SJed Brown @*/ 12278434afd1SBarry Smith PetscErrorCode TSGetIFunction(TS ts, Vec *r, TSIFunctionFn **func, void **ctx) 1228d71ae5a4SJacob Faibussowitsch { 1229089b2837SJed Brown SNES snes; 123024989b8cSPeter Brune DM dm; 1231089b2837SJed Brown 1232089b2837SJed Brown PetscFunctionBegin; 1233089b2837SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 12349566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 12359566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes, r, NULL, NULL)); 12369566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 12379566063dSJacob Faibussowitsch PetscCall(DMTSGetIFunction(dm, func, ctx)); 12383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1239089b2837SJed Brown } 1240089b2837SJed Brown 1241089b2837SJed Brown /*@C 1242dd8e379bSPierre Jolivet TSGetRHSFunction - Returns the vector where the right-hand side is stored and the function/context to compute it. 1243089b2837SJed Brown 1244089b2837SJed Brown Not Collective 1245089b2837SJed Brown 1246089b2837SJed Brown Input Parameter: 1247bcf0153eSBarry Smith . ts - the `TS` context 1248089b2837SJed Brown 1249d8d19677SJose E. Roman Output Parameters: 1250dd8e379bSPierre Jolivet + r - vector to hold computed right-hand side (or `NULL`) 1251dd8e379bSPierre Jolivet . func - the function to compute right-hand side (or `NULL`) 1252195e9b02SBarry Smith - ctx - the function context (or `NULL`) 1253089b2837SJed Brown 1254089b2837SJed Brown Level: advanced 1255089b2837SJed Brown 12561cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSFunction()`, `SNESGetFunction()` 1257089b2837SJed Brown @*/ 12588434afd1SBarry Smith PetscErrorCode TSGetRHSFunction(TS ts, Vec *r, TSRHSFunctionFn **func, void **ctx) 1259d71ae5a4SJacob Faibussowitsch { 1260089b2837SJed Brown SNES snes; 126124989b8cSPeter Brune DM dm; 1262089b2837SJed Brown 1263089b2837SJed Brown PetscFunctionBegin; 1264089b2837SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 12659566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 12669566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes, r, NULL, NULL)); 12679566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 12689566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, func, ctx)); 12693ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1270316643e7SJed Brown } 1271316643e7SJed Brown 1272316643e7SJed Brown /*@C 1273a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1274bcf0153eSBarry Smith provided with `TSSetIFunction()`. 1275316643e7SJed Brown 1276c3339decSBarry Smith Logically Collective 1277316643e7SJed Brown 1278316643e7SJed Brown Input Parameters: 1279bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1280aaa8cc7dSPierre Jolivet . Amat - (approximate) matrix to store Jacobian entries computed by `f` 1281195e9b02SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as `Amat`) 1282316643e7SJed Brown . f - the Jacobian evaluation routine 1283195e9b02SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be `NULL`) 1284316643e7SJed Brown 1285bcf0153eSBarry Smith Level: beginner 1286316643e7SJed Brown 1287bcf0153eSBarry Smith Notes: 1288195e9b02SBarry Smith The matrices `Amat` and `Pmat` are exactly the matrices that are used by `SNES` for the nonlinear solve. 1289bcf0153eSBarry Smith 1290bcf0153eSBarry Smith If you know the operator Amat has a null space you can use `MatSetNullSpace()` and `MatSetTransposeNullSpace()` to supply the null 1291195e9b02SBarry Smith space to `Amat` and the `KSP` solvers will automatically use that null space as needed during the solution process. 1292895c21f2SBarry Smith 1293a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1294b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1295a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1296a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1297a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1298a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1299a4f0a591SBarry Smith 13006cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 13016cd88445SBarry Smith 1302195e9b02SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices `Amat` and `Pmat` between the calls to `f` 1303195e9b02SBarry Smith You should not assume the values are the same in the next call to `f` as you set them in the previous call. 1304ca5f011dSBarry Smith 1305bc7fdbb5SPierre Jolivet In case `TSSetRHSJacobian()` is also used in conjunction with a fully-implicit solver, 1306d469ad21SStefano Zampini multilevel linear solvers, e.g. `PCMG`, will likely not work due to the way `TS` handles rhs matrices. 1307d469ad21SStefano Zampini 13088434afd1SBarry Smith .seealso: [](ch_ts), `TS`, `TSIJacobianFn`, `TSSetIFunction()`, `TSSetRHSJacobian()`, 130914d0ab18SJacob Faibussowitsch `SNESComputeJacobianDefaultColor()`, `SNESComputeJacobianDefault()`, `TSSetRHSFunction()` 1310316643e7SJed Brown @*/ 13118434afd1SBarry Smith PetscErrorCode TSSetIJacobian(TS ts, Mat Amat, Mat Pmat, TSIJacobianFn *f, void *ctx) 1312d71ae5a4SJacob Faibussowitsch { 1313089b2837SJed Brown SNES snes; 131424989b8cSPeter Brune DM dm; 1315316643e7SJed Brown 1316316643e7SJed Brown PetscFunctionBegin; 13170700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1318e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat, MAT_CLASSID, 2); 1319e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat, MAT_CLASSID, 3); 1320e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts, 1, Amat, 2); 1321e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts, 1, Pmat, 3); 132224989b8cSPeter Brune 13239566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 13249566063dSJacob Faibussowitsch PetscCall(DMTSSetIJacobian(dm, f, ctx)); 132524989b8cSPeter Brune 13269566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 13279566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes, Amat, Pmat, SNESTSFormJacobian, ts)); 13283ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1329316643e7SJed Brown } 1330316643e7SJed Brown 1331e1244c69SJed Brown /*@ 13328434afd1SBarry Smith TSRHSJacobianSetReuse - restore the RHS Jacobian before calling the user-provided `TSRHSJacobianFn` function again 1333e1244c69SJed Brown 1334e1244c69SJed Brown Logically Collective 1335e1244c69SJed Brown 13364165533cSJose E. Roman Input Parameters: 1337bcf0153eSBarry Smith + ts - `TS` context obtained from `TSCreate()` 1338bcf0153eSBarry Smith - reuse - `PETSC_TRUE` if the RHS Jacobian 1339e1244c69SJed Brown 1340e1244c69SJed Brown Level: intermediate 1341e1244c69SJed Brown 134214d0ab18SJacob Faibussowitsch Notes: 134314d0ab18SJacob Faibussowitsch Without this flag, `TS` will change the sign and shift the RHS Jacobian for a 134414d0ab18SJacob Faibussowitsch finite-time-step implicit solve, in which case the user function will need to recompute the 134514d0ab18SJacob Faibussowitsch entire Jacobian. The `reuse `flag must be set if the evaluation function assumes that the 134614d0ab18SJacob Faibussowitsch matrix entries have not been changed by the `TS`. 134714d0ab18SJacob Faibussowitsch 13481cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSJacobian()`, `TSComputeRHSJacobianConstant()` 1349e1244c69SJed Brown @*/ 1350d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRHSJacobianSetReuse(TS ts, PetscBool reuse) 1351d71ae5a4SJacob Faibussowitsch { 1352e1244c69SJed Brown PetscFunctionBegin; 1353e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 13543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1355e1244c69SJed Brown } 1356e1244c69SJed Brown 1357efe9872eSLisandro Dalcin /*@C 1358efe9872eSLisandro Dalcin TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved. 1359efe9872eSLisandro Dalcin 1360c3339decSBarry Smith Logically Collective 1361efe9872eSLisandro Dalcin 1362efe9872eSLisandro Dalcin Input Parameters: 1363bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1364195e9b02SBarry Smith . F - vector to hold the residual (or `NULL` to have it created internally) 1365efe9872eSLisandro Dalcin . fun - the function evaluation routine 1366195e9b02SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be `NULL`) 1367efe9872eSLisandro Dalcin 1368efe9872eSLisandro Dalcin Level: beginner 1369efe9872eSLisandro Dalcin 13708434afd1SBarry Smith .seealso: [](ch_ts), `TS`, `TSI2FunctionFn`, `TSSetI2Jacobian()`, `TSSetIFunction()`, 137114d0ab18SJacob Faibussowitsch `TSCreate()`, `TSSetRHSFunction()` 1372efe9872eSLisandro Dalcin @*/ 13738434afd1SBarry Smith PetscErrorCode TSSetI2Function(TS ts, Vec F, TSI2FunctionFn *fun, void *ctx) 1374d71ae5a4SJacob Faibussowitsch { 1375efe9872eSLisandro Dalcin DM dm; 1376efe9872eSLisandro Dalcin 1377efe9872eSLisandro Dalcin PetscFunctionBegin; 1378efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1379efe9872eSLisandro Dalcin if (F) PetscValidHeaderSpecific(F, VEC_CLASSID, 2); 13809566063dSJacob Faibussowitsch PetscCall(TSSetIFunction(ts, F, NULL, NULL)); 13819566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 13829566063dSJacob Faibussowitsch PetscCall(DMTSSetI2Function(dm, fun, ctx)); 13833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1384efe9872eSLisandro Dalcin } 1385efe9872eSLisandro Dalcin 1386efe9872eSLisandro Dalcin /*@C 1387a5b23f4aSJose E. Roman TSGetI2Function - Returns the vector where the implicit residual is stored and the function/context to compute it. 1388efe9872eSLisandro Dalcin 1389efe9872eSLisandro Dalcin Not Collective 1390efe9872eSLisandro Dalcin 1391efe9872eSLisandro Dalcin Input Parameter: 1392bcf0153eSBarry Smith . ts - the `TS` context 1393efe9872eSLisandro Dalcin 1394d8d19677SJose E. Roman Output Parameters: 1395195e9b02SBarry Smith + r - vector to hold residual (or `NULL`) 1396195e9b02SBarry Smith . fun - the function to compute residual (or `NULL`) 1397195e9b02SBarry Smith - ctx - the function context (or `NULL`) 1398efe9872eSLisandro Dalcin 1399efe9872eSLisandro Dalcin Level: advanced 1400efe9872eSLisandro Dalcin 14011cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetIFunction()`, `SNESGetFunction()`, `TSCreate()` 1402efe9872eSLisandro Dalcin @*/ 14038434afd1SBarry Smith PetscErrorCode TSGetI2Function(TS ts, Vec *r, TSI2FunctionFn **fun, void **ctx) 1404d71ae5a4SJacob Faibussowitsch { 1405efe9872eSLisandro Dalcin SNES snes; 1406efe9872eSLisandro Dalcin DM dm; 1407efe9872eSLisandro Dalcin 1408efe9872eSLisandro Dalcin PetscFunctionBegin; 1409efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 14109566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 14119566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes, r, NULL, NULL)); 14129566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 14139566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Function(dm, fun, ctx)); 14143ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1415efe9872eSLisandro Dalcin } 1416efe9872eSLisandro Dalcin 1417efe9872eSLisandro Dalcin /*@C 1418bc77d74cSLisandro Dalcin TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t + a*dF/dU_tt 1419bcf0153eSBarry Smith where F(t,U,U_t,U_tt) is the function you provided with `TSSetI2Function()`. 1420efe9872eSLisandro Dalcin 1421c3339decSBarry Smith Logically Collective 1422efe9872eSLisandro Dalcin 1423efe9872eSLisandro Dalcin Input Parameters: 1424bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1425195e9b02SBarry Smith . J - matrix to hold the Jacobian values 1426195e9b02SBarry Smith . P - matrix for constructing the preconditioner (may be same as `J`) 14278434afd1SBarry Smith . jac - the Jacobian evaluation routine, see `TSI2JacobianFn` for the calling sequence 1428195e9b02SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be `NULL`) 1429efe9872eSLisandro Dalcin 1430bcf0153eSBarry Smith Level: beginner 1431bcf0153eSBarry Smith 1432efe9872eSLisandro Dalcin Notes: 1433195e9b02SBarry Smith The matrices `J` and `P` are exactly the matrices that are used by `SNES` for the nonlinear solve. 1434efe9872eSLisandro Dalcin 1435efe9872eSLisandro Dalcin The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be 1436efe9872eSLisandro 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. 1437efe9872eSLisandro Dalcin The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U where the positive "shift" 1438bc77d74cSLisandro Dalcin parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states. 1439efe9872eSLisandro Dalcin 14408434afd1SBarry Smith .seealso: [](ch_ts), `TS`, `TSI2JacobianFn`, `TSSetI2Function()`, `TSGetI2Jacobian()` 1441efe9872eSLisandro Dalcin @*/ 14428434afd1SBarry Smith PetscErrorCode TSSetI2Jacobian(TS ts, Mat J, Mat P, TSI2JacobianFn *jac, void *ctx) 1443d71ae5a4SJacob Faibussowitsch { 1444efe9872eSLisandro Dalcin DM dm; 1445efe9872eSLisandro Dalcin 1446efe9872eSLisandro Dalcin PetscFunctionBegin; 1447efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1448efe9872eSLisandro Dalcin if (J) PetscValidHeaderSpecific(J, MAT_CLASSID, 2); 1449efe9872eSLisandro Dalcin if (P) PetscValidHeaderSpecific(P, MAT_CLASSID, 3); 14509566063dSJacob Faibussowitsch PetscCall(TSSetIJacobian(ts, J, P, NULL, NULL)); 14519566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 14529566063dSJacob Faibussowitsch PetscCall(DMTSSetI2Jacobian(dm, jac, ctx)); 14533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1454efe9872eSLisandro Dalcin } 1455efe9872eSLisandro Dalcin 1456efe9872eSLisandro Dalcin /*@C 1457efe9872eSLisandro Dalcin TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep. 1458efe9872eSLisandro Dalcin 1459bcf0153eSBarry Smith Not Collective, but parallel objects are returned if `TS` is parallel 1460efe9872eSLisandro Dalcin 1461efe9872eSLisandro Dalcin Input Parameter: 1462bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 1463efe9872eSLisandro Dalcin 1464efe9872eSLisandro Dalcin Output Parameters: 1465efe9872eSLisandro Dalcin + J - The (approximate) Jacobian of F(t,U,U_t,U_tt) 1466195e9b02SBarry Smith . P - The matrix from which the preconditioner is constructed, often the same as `J` 1467efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices 1468efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine 1469efe9872eSLisandro Dalcin 1470bcf0153eSBarry Smith Level: advanced 1471bcf0153eSBarry Smith 1472195e9b02SBarry Smith Note: 1473195e9b02SBarry Smith You can pass in `NULL` for any return argument you do not need. 1474efe9872eSLisandro Dalcin 14751cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTimeStep()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()`, `TSSetI2Jacobian()`, `TSGetI2Function()`, `TSCreate()` 1476efe9872eSLisandro Dalcin @*/ 14778434afd1SBarry Smith PetscErrorCode TSGetI2Jacobian(TS ts, Mat *J, Mat *P, TSI2JacobianFn **jac, void **ctx) 1478d71ae5a4SJacob Faibussowitsch { 1479efe9872eSLisandro Dalcin SNES snes; 1480efe9872eSLisandro Dalcin DM dm; 1481efe9872eSLisandro Dalcin 1482efe9872eSLisandro Dalcin PetscFunctionBegin; 14839566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 14849566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 14859566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes, J, P, NULL, NULL)); 14869566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 14879566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm, jac, ctx)); 14883ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1489efe9872eSLisandro Dalcin } 1490efe9872eSLisandro Dalcin 1491efe9872eSLisandro Dalcin /*@ 1492efe9872eSLisandro Dalcin TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0 1493efe9872eSLisandro Dalcin 1494c3339decSBarry Smith Collective 1495efe9872eSLisandro Dalcin 1496efe9872eSLisandro Dalcin Input Parameters: 1497bcf0153eSBarry Smith + ts - the `TS` context 1498efe9872eSLisandro Dalcin . t - current time 1499efe9872eSLisandro Dalcin . U - state vector 1500efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t) 1501efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt) 1502efe9872eSLisandro Dalcin 1503efe9872eSLisandro Dalcin Output Parameter: 1504efe9872eSLisandro Dalcin . F - the residual vector 1505efe9872eSLisandro Dalcin 1506bcf0153eSBarry Smith Level: developer 1507bcf0153eSBarry Smith 1508efe9872eSLisandro Dalcin Note: 1509efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1510efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1511efe9872eSLisandro Dalcin 15121cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetI2Function()`, `TSGetI2Function()` 1513efe9872eSLisandro Dalcin @*/ 1514d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeI2Function(TS ts, PetscReal t, Vec U, Vec V, Vec A, Vec F) 1515d71ae5a4SJacob Faibussowitsch { 1516efe9872eSLisandro Dalcin DM dm; 15178434afd1SBarry Smith TSI2FunctionFn *I2Function; 1518efe9872eSLisandro Dalcin void *ctx; 15198434afd1SBarry Smith TSRHSFunctionFn *rhsfunction; 1520efe9872eSLisandro Dalcin 1521efe9872eSLisandro Dalcin PetscFunctionBegin; 1522efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1523efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 1524efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V, VEC_CLASSID, 4); 1525efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A, VEC_CLASSID, 5); 1526efe9872eSLisandro Dalcin PetscValidHeaderSpecific(F, VEC_CLASSID, 6); 1527efe9872eSLisandro Dalcin 15289566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 15299566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Function(dm, &I2Function, &ctx)); 15309566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, NULL)); 1531efe9872eSLisandro Dalcin 1532efe9872eSLisandro Dalcin if (!I2Function) { 15339566063dSJacob Faibussowitsch PetscCall(TSComputeIFunction(ts, t, U, A, F, PETSC_FALSE)); 15343ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1535efe9872eSLisandro Dalcin } 1536efe9872eSLisandro Dalcin 1537da023ba9SStefano Zampini PetscCall(PetscLogEventBegin(TS_FunctionEval, U, ts, V, F)); 1538efe9872eSLisandro Dalcin 1539792fecdfSBarry Smith PetscCallBack("TS callback implicit function", I2Function(ts, t, U, V, A, F, ctx)); 1540efe9872eSLisandro Dalcin 1541efe9872eSLisandro Dalcin if (rhsfunction) { 1542efe9872eSLisandro Dalcin Vec Frhs; 15438af0501fSStefano Zampini 15448af0501fSStefano Zampini PetscCall(DMGetGlobalVector(dm, &Frhs)); 15459566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts, t, U, Frhs)); 15469566063dSJacob Faibussowitsch PetscCall(VecAXPY(F, -1, Frhs)); 15478af0501fSStefano Zampini PetscCall(DMRestoreGlobalVector(dm, &Frhs)); 1548efe9872eSLisandro Dalcin } 1549efe9872eSLisandro Dalcin 1550da023ba9SStefano Zampini PetscCall(PetscLogEventEnd(TS_FunctionEval, U, ts, V, F)); 15513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1552efe9872eSLisandro Dalcin } 1553efe9872eSLisandro Dalcin 1554efe9872eSLisandro Dalcin /*@ 1555efe9872eSLisandro Dalcin TSComputeI2Jacobian - Evaluates the Jacobian of the DAE 1556efe9872eSLisandro Dalcin 1557c3339decSBarry Smith Collective 1558efe9872eSLisandro Dalcin 1559efe9872eSLisandro Dalcin Input Parameters: 1560bcf0153eSBarry Smith + ts - the `TS` context 1561efe9872eSLisandro Dalcin . t - current timestep 1562efe9872eSLisandro Dalcin . U - state vector 1563efe9872eSLisandro Dalcin . V - time derivative of state vector 1564efe9872eSLisandro Dalcin . A - second time derivative of state vector 1565efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below 1566efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below 1567efe9872eSLisandro Dalcin 1568efe9872eSLisandro Dalcin Output Parameters: 1569efe9872eSLisandro Dalcin + J - Jacobian matrix 1570efe9872eSLisandro Dalcin - P - optional preconditioning matrix 1571efe9872eSLisandro Dalcin 1572bcf0153eSBarry Smith Level: developer 1573bcf0153eSBarry Smith 1574efe9872eSLisandro Dalcin Notes: 1575efe9872eSLisandro Dalcin If F(t,U,V,A)=0 is the DAE, the required Jacobian is 1576efe9872eSLisandro Dalcin 1577efe9872eSLisandro Dalcin dF/dU + shiftV*dF/dV + shiftA*dF/dA 1578efe9872eSLisandro Dalcin 1579efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1580efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1581efe9872eSLisandro Dalcin 15821cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetI2Jacobian()` 1583efe9872eSLisandro Dalcin @*/ 1584d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeI2Jacobian(TS ts, PetscReal t, Vec U, Vec V, Vec A, PetscReal shiftV, PetscReal shiftA, Mat J, Mat P) 1585d71ae5a4SJacob Faibussowitsch { 1586efe9872eSLisandro Dalcin DM dm; 15878434afd1SBarry Smith TSI2JacobianFn *I2Jacobian; 1588efe9872eSLisandro Dalcin void *ctx; 15898434afd1SBarry Smith TSRHSJacobianFn *rhsjacobian; 1590efe9872eSLisandro Dalcin 1591efe9872eSLisandro Dalcin PetscFunctionBegin; 1592efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1593efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 1594efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V, VEC_CLASSID, 4); 1595efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A, VEC_CLASSID, 5); 1596efe9872eSLisandro Dalcin PetscValidHeaderSpecific(J, MAT_CLASSID, 8); 1597efe9872eSLisandro Dalcin PetscValidHeaderSpecific(P, MAT_CLASSID, 9); 1598efe9872eSLisandro Dalcin 15999566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 16009566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm, &I2Jacobian, &ctx)); 16019566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, &rhsjacobian, NULL)); 1602efe9872eSLisandro Dalcin 1603efe9872eSLisandro Dalcin if (!I2Jacobian) { 16049566063dSJacob Faibussowitsch PetscCall(TSComputeIJacobian(ts, t, U, A, shiftA, J, P, PETSC_FALSE)); 16053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1606efe9872eSLisandro Dalcin } 1607efe9872eSLisandro Dalcin 1608da023ba9SStefano Zampini PetscCall(PetscLogEventBegin(TS_JacobianEval, U, ts, J, P)); 1609792fecdfSBarry Smith PetscCallBack("TS callback implicit Jacobian", I2Jacobian(ts, t, U, V, A, shiftV, shiftA, J, P, ctx)); 1610efe9872eSLisandro Dalcin if (rhsjacobian) { 1611d60b7d5cSBarry Smith Mat Jrhs, Prhs; 16129566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts, &Jrhs, &Prhs)); 16139566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts, t, U, Jrhs, Prhs)); 16149566063dSJacob Faibussowitsch PetscCall(MatAXPY(J, -1, Jrhs, ts->axpy_pattern)); 16159566063dSJacob Faibussowitsch if (P != J) PetscCall(MatAXPY(P, -1, Prhs, ts->axpy_pattern)); 1616efe9872eSLisandro Dalcin } 1617efe9872eSLisandro Dalcin 1618da023ba9SStefano Zampini PetscCall(PetscLogEventEnd(TS_JacobianEval, U, ts, J, P)); 16193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1620efe9872eSLisandro Dalcin } 1621efe9872eSLisandro Dalcin 1622438f35afSJed Brown /*@C 1623438f35afSJed Brown TSSetTransientVariable - sets function to transform from state to transient variables 1624438f35afSJed Brown 1625438f35afSJed Brown Logically Collective 1626438f35afSJed Brown 16274165533cSJose E. Roman Input Parameters: 1628438f35afSJed Brown + ts - time stepping context on which to change the transient variable 16298434afd1SBarry Smith . tvar - a function that transforms to transient variables, see `TSTransientVariableFn` for the calling sequence 1630438f35afSJed Brown - ctx - a context for tvar 1631438f35afSJed Brown 1632438f35afSJed Brown Level: advanced 1633438f35afSJed Brown 1634438f35afSJed Brown Notes: 1635bcf0153eSBarry Smith This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., `TSBDF`) 1636438f35afSJed Brown can be conservative. In this context, primitive variables P are used to model the state (e.g., because they lead to 1637438f35afSJed Brown well-conditioned formulations even in limiting cases such as low-Mach or zero porosity). The transient variable is 1638438f35afSJed Brown C(P), specified by calling this function. An IFunction thus receives arguments (P, Cdot) and the IJacobian must be 1639438f35afSJed Brown evaluated via the chain rule, as in 1640195e9b02SBarry Smith .vb 1641438f35afSJed Brown dF/dP + shift * dF/dCdot dC/dP. 1642195e9b02SBarry Smith .ve 1643438f35afSJed Brown 16448434afd1SBarry Smith .seealso: [](ch_ts), `TS`, `TSBDF`, `TSTransientVariableFn`, `DMTSSetTransientVariable()`, `DMTSGetTransientVariable()`, `TSSetIFunction()`, `TSSetIJacobian()` 1645438f35afSJed Brown @*/ 16468434afd1SBarry Smith PetscErrorCode TSSetTransientVariable(TS ts, TSTransientVariableFn *tvar, void *ctx) 1647d71ae5a4SJacob Faibussowitsch { 1648438f35afSJed Brown DM dm; 1649438f35afSJed Brown 1650438f35afSJed Brown PetscFunctionBegin; 1651438f35afSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 16529566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 16539566063dSJacob Faibussowitsch PetscCall(DMTSSetTransientVariable(dm, tvar, ctx)); 16543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1655438f35afSJed Brown } 1656438f35afSJed Brown 1657efe9872eSLisandro Dalcin /*@ 1658e3c11fc1SJed Brown TSComputeTransientVariable - transforms state (primitive) variables to transient (conservative) variables 1659e3c11fc1SJed Brown 1660e3c11fc1SJed Brown Logically Collective 1661e3c11fc1SJed Brown 1662e3c11fc1SJed Brown Input Parameters: 1663e3c11fc1SJed Brown + ts - TS on which to compute 1664e3c11fc1SJed Brown - U - state vector to be transformed to transient variables 1665e3c11fc1SJed Brown 16662fe279fdSBarry Smith Output Parameter: 1667e3c11fc1SJed Brown . C - transient (conservative) variable 1668e3c11fc1SJed Brown 1669e3c11fc1SJed Brown Level: developer 1670e3c11fc1SJed Brown 1671b43aa488SJacob Faibussowitsch Developer Notes: 1672195e9b02SBarry Smith If `DMTSSetTransientVariable()` has not been called, then C is not modified in this routine and C = `NULL` is allowed. 1673bcf0153eSBarry Smith This makes it safe to call without a guard. One can use `TSHasTransientVariable()` to check if transient variables are 1674bcf0153eSBarry Smith being used. 1675bcf0153eSBarry Smith 16761cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSBDF`, `DMTSSetTransientVariable()`, `TSComputeIFunction()`, `TSComputeIJacobian()` 1677e3c11fc1SJed Brown @*/ 1678d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeTransientVariable(TS ts, Vec U, Vec C) 1679d71ae5a4SJacob Faibussowitsch { 1680e3c11fc1SJed Brown DM dm; 1681e3c11fc1SJed Brown DMTS dmts; 1682e3c11fc1SJed Brown 1683e3c11fc1SJed Brown PetscFunctionBegin; 1684e3c11fc1SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1685e3c11fc1SJed Brown PetscValidHeaderSpecific(U, VEC_CLASSID, 2); 16869566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 16879566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm, &dmts)); 1688e3c11fc1SJed Brown if (dmts->ops->transientvar) { 1689e3c11fc1SJed Brown PetscValidHeaderSpecific(C, VEC_CLASSID, 3); 16909566063dSJacob Faibussowitsch PetscCall((*dmts->ops->transientvar)(ts, U, C, dmts->transientvarctx)); 1691e3c11fc1SJed Brown } 16923ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1693e3c11fc1SJed Brown } 1694e3c11fc1SJed Brown 1695e3c11fc1SJed Brown /*@ 1696e3c11fc1SJed Brown TSHasTransientVariable - determine whether transient variables have been set 1697e3c11fc1SJed Brown 1698e3c11fc1SJed Brown Logically Collective 1699e3c11fc1SJed Brown 17002fe279fdSBarry Smith Input Parameter: 1701195e9b02SBarry Smith . ts - `TS` on which to compute 1702e3c11fc1SJed Brown 17032fe279fdSBarry Smith Output Parameter: 1704bcf0153eSBarry Smith . has - `PETSC_TRUE` if transient variables have been set 1705e3c11fc1SJed Brown 1706e3c11fc1SJed Brown Level: developer 1707e3c11fc1SJed Brown 17081cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSBDF`, `DMTSSetTransientVariable()`, `TSComputeTransientVariable()` 1709e3c11fc1SJed Brown @*/ 1710d71ae5a4SJacob Faibussowitsch PetscErrorCode TSHasTransientVariable(TS ts, PetscBool *has) 1711d71ae5a4SJacob Faibussowitsch { 1712e3c11fc1SJed Brown DM dm; 1713e3c11fc1SJed Brown DMTS dmts; 1714e3c11fc1SJed Brown 1715e3c11fc1SJed Brown PetscFunctionBegin; 1716e3c11fc1SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 17179566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 17189566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm, &dmts)); 1719e3c11fc1SJed Brown *has = dmts->ops->transientvar ? PETSC_TRUE : PETSC_FALSE; 17203ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1721e3c11fc1SJed Brown } 1722e3c11fc1SJed Brown 1723e3c11fc1SJed Brown /*@ 1724efe9872eSLisandro Dalcin TS2SetSolution - Sets the initial solution and time derivative vectors 1725bcf0153eSBarry Smith for use by the `TS` routines handling second order equations. 1726efe9872eSLisandro Dalcin 1727c3339decSBarry Smith Logically Collective 1728efe9872eSLisandro Dalcin 1729efe9872eSLisandro Dalcin Input Parameters: 1730bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1731efe9872eSLisandro Dalcin . u - the solution vector 1732efe9872eSLisandro Dalcin - v - the time derivative vector 1733efe9872eSLisandro Dalcin 1734efe9872eSLisandro Dalcin Level: beginner 1735efe9872eSLisandro Dalcin 17361cc06b55SBarry Smith .seealso: [](ch_ts), `TS` 1737efe9872eSLisandro Dalcin @*/ 1738d71ae5a4SJacob Faibussowitsch PetscErrorCode TS2SetSolution(TS ts, Vec u, Vec v) 1739d71ae5a4SJacob Faibussowitsch { 1740efe9872eSLisandro Dalcin PetscFunctionBegin; 1741efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1742efe9872eSLisandro Dalcin PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 1743efe9872eSLisandro Dalcin PetscValidHeaderSpecific(v, VEC_CLASSID, 3); 17449566063dSJacob Faibussowitsch PetscCall(TSSetSolution(ts, u)); 17459566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)v)); 17469566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_dot)); 1747efe9872eSLisandro Dalcin ts->vec_dot = v; 17483ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1749efe9872eSLisandro Dalcin } 1750efe9872eSLisandro Dalcin 1751efe9872eSLisandro Dalcin /*@ 1752efe9872eSLisandro Dalcin TS2GetSolution - Returns the solution and time derivative at the present timestep 175314d0ab18SJacob Faibussowitsch for second order equations. 1754efe9872eSLisandro Dalcin 175514d0ab18SJacob Faibussowitsch Not Collective 1756efe9872eSLisandro Dalcin 1757efe9872eSLisandro Dalcin Input Parameter: 1758bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 1759efe9872eSLisandro Dalcin 1760d8d19677SJose E. Roman Output Parameters: 1761efe9872eSLisandro Dalcin + u - the vector containing the solution 1762efe9872eSLisandro Dalcin - v - the vector containing the time derivative 1763efe9872eSLisandro Dalcin 1764efe9872eSLisandro Dalcin Level: intermediate 1765efe9872eSLisandro Dalcin 176614d0ab18SJacob Faibussowitsch Notes: 176714d0ab18SJacob Faibussowitsch It is valid to call this routine inside the function 176814d0ab18SJacob Faibussowitsch that you are evaluating in order to move to the new timestep. This vector not 176914d0ab18SJacob Faibussowitsch changed until the solution at the next timestep has been calculated. 177014d0ab18SJacob Faibussowitsch 17711cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TS2SetSolution()`, `TSGetTimeStep()`, `TSGetTime()` 1772efe9872eSLisandro Dalcin @*/ 1773d71ae5a4SJacob Faibussowitsch PetscErrorCode TS2GetSolution(TS ts, Vec *u, Vec *v) 1774d71ae5a4SJacob Faibussowitsch { 1775efe9872eSLisandro Dalcin PetscFunctionBegin; 1776efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 17774f572ea9SToby Isaac if (u) PetscAssertPointer(u, 2); 17784f572ea9SToby Isaac if (v) PetscAssertPointer(v, 3); 1779efe9872eSLisandro Dalcin if (u) *u = ts->vec_sol; 1780efe9872eSLisandro Dalcin if (v) *v = ts->vec_dot; 17813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1782efe9872eSLisandro Dalcin } 1783efe9872eSLisandro Dalcin 1784ffeef943SBarry Smith /*@ 1785bcf0153eSBarry Smith TSLoad - Loads a `TS` that has been stored in binary with `TSView()`. 178655849f57SBarry Smith 1787c3339decSBarry Smith Collective 178855849f57SBarry Smith 178955849f57SBarry Smith Input Parameters: 1790b43aa488SJacob Faibussowitsch + ts - the newly loaded `TS`, this needs to have been created with `TSCreate()` or 1791bcf0153eSBarry Smith some related function before a call to `TSLoad()`. 1792bcf0153eSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` 179355849f57SBarry Smith 179455849f57SBarry Smith Level: intermediate 179555849f57SBarry Smith 1796bcf0153eSBarry Smith Note: 1797bcf0153eSBarry Smith The type is determined by the data in the file, any type set into the `TS` before this call is ignored. 179855849f57SBarry Smith 17991cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `PetscViewer`, `PetscViewerBinaryOpen()`, `TSView()`, `MatLoad()`, `VecLoad()` 180055849f57SBarry Smith @*/ 1801d71ae5a4SJacob Faibussowitsch PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 1802d71ae5a4SJacob Faibussowitsch { 180355849f57SBarry Smith PetscBool isbinary; 180455849f57SBarry Smith PetscInt classid; 180555849f57SBarry Smith char type[256]; 18062d53ad75SBarry Smith DMTS sdm; 1807ad6bc421SBarry Smith DM dm; 180855849f57SBarry Smith 180955849f57SBarry Smith PetscFunctionBegin; 1810f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 181155849f57SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 18129566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary)); 18133c633725SBarry Smith PetscCheck(isbinary, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 181455849f57SBarry Smith 18159566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT)); 18163c633725SBarry Smith PetscCheck(classid == TS_FILE_CLASSID, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Not TS next in file"); 18179566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR)); 18189566063dSJacob Faibussowitsch PetscCall(TSSetType(ts, type)); 1819dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, load, viewer); 18209566063dSJacob Faibussowitsch PetscCall(DMCreate(PetscObjectComm((PetscObject)ts), &dm)); 18219566063dSJacob Faibussowitsch PetscCall(DMLoad(dm, viewer)); 18229566063dSJacob Faibussowitsch PetscCall(TSSetDM(ts, dm)); 18239566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(ts->dm, &ts->vec_sol)); 18249566063dSJacob Faibussowitsch PetscCall(VecLoad(ts->vec_sol, viewer)); 18259566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm, &sdm)); 18269566063dSJacob Faibussowitsch PetscCall(DMTSLoad(sdm, viewer)); 18273ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 182855849f57SBarry Smith } 182955849f57SBarry Smith 18309804daf3SBarry Smith #include <petscdraw.h> 1831e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1832e04113cfSBarry Smith #include <petscviewersaws.h> 1833f05ece33SBarry Smith #endif 1834fe2efc57SMark 1835ffeef943SBarry Smith /*@ 1836bcf0153eSBarry Smith TSViewFromOptions - View a `TS` based on values in the options database 1837fe2efc57SMark 1838c3339decSBarry Smith Collective 1839fe2efc57SMark 1840fe2efc57SMark Input Parameters: 1841bcf0153eSBarry Smith + ts - the `TS` context 1842bcf0153eSBarry Smith . obj - Optional object that provides the prefix for the options database keys 1843bcf0153eSBarry Smith - name - command line option string to be passed by user 1844fe2efc57SMark 1845fe2efc57SMark Level: intermediate 1846bcf0153eSBarry Smith 18471cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSView`, `PetscObjectViewFromOptions()`, `TSCreate()` 1848fe2efc57SMark @*/ 1849bcf0153eSBarry Smith PetscErrorCode TSViewFromOptions(TS ts, PetscObject obj, const char name[]) 1850d71ae5a4SJacob Faibussowitsch { 1851fe2efc57SMark PetscFunctionBegin; 1852bcf0153eSBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1853bcf0153eSBarry Smith PetscCall(PetscObjectViewFromOptions((PetscObject)ts, obj, name)); 18543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1855fe2efc57SMark } 1856fe2efc57SMark 1857ffeef943SBarry Smith /*@ 1858bcf0153eSBarry Smith TSView - Prints the `TS` data structure. 1859d763cef2SBarry Smith 1860c3339decSBarry Smith Collective 1861d763cef2SBarry Smith 1862d763cef2SBarry Smith Input Parameters: 1863bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1864d763cef2SBarry Smith - viewer - visualization context 1865d763cef2SBarry Smith 1866d763cef2SBarry Smith Options Database Key: 1867bcf0153eSBarry Smith . -ts_view - calls `TSView()` at end of `TSStep()` 1868bcf0153eSBarry Smith 1869bcf0153eSBarry Smith Level: beginner 1870d763cef2SBarry Smith 1871d763cef2SBarry Smith Notes: 1872d763cef2SBarry Smith The available visualization contexts include 1873bcf0153eSBarry Smith + `PETSC_VIEWER_STDOUT_SELF` - standard output (default) 1874bcf0153eSBarry Smith - `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard 1875d763cef2SBarry Smith output where only the first processor opens 1876d763cef2SBarry Smith the file. All other processors send their 1877d763cef2SBarry Smith data to the first processor to print. 1878d763cef2SBarry Smith 1879d763cef2SBarry Smith The user can open an alternative visualization context with 1880bcf0153eSBarry Smith `PetscViewerASCIIOpen()` - output to a specified file. 1881d763cef2SBarry Smith 1882bcf0153eSBarry Smith In the debugger you can do call `TSView`(ts,0) to display the `TS` solver. (The same holds for any PETSc object viewer). 1883595c91d4SBarry Smith 18841cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `PetscViewer`, `PetscViewerASCIIOpen()` 1885d763cef2SBarry Smith @*/ 1886d71ae5a4SJacob Faibussowitsch PetscErrorCode TSView(TS ts, PetscViewer viewer) 1887d71ae5a4SJacob Faibussowitsch { 188819fd82e9SBarry Smith TSType type; 18892b0a91c0SBarry Smith PetscBool iascii, isstring, isundials, isbinary, isdraw; 18902d53ad75SBarry Smith DMTS sdm; 1891e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1892536b137fSBarry Smith PetscBool issaws; 1893f05ece33SBarry Smith #endif 1894d763cef2SBarry Smith 1895d763cef2SBarry Smith PetscFunctionBegin; 18960700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 18971e66621cSBarry Smith if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts), &viewer)); 18980700a824SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 1899c9780b6fSBarry Smith PetscCheckSameComm(ts, 1, viewer, 2); 1900fd16b177SBarry Smith 19019566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 19029566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring)); 19039566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary)); 19049566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw)); 1905e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 19069566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSAWS, &issaws)); 1907f05ece33SBarry Smith #endif 190832077d6dSBarry Smith if (iascii) { 19099566063dSJacob Faibussowitsch PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)ts, viewer)); 1910efd4aadfSBarry Smith if (ts->ops->view) { 19119566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 1912dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, view, viewer); 19139566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 1914efd4aadfSBarry Smith } 19151690c2aeSBarry Smith if (ts->max_steps < PETSC_INT_MAX) PetscCall(PetscViewerASCIIPrintf(viewer, " maximum steps=%" PetscInt_FMT "\n", ts->max_steps)); 19161e66621cSBarry Smith if (ts->max_time < PETSC_MAX_REAL) PetscCall(PetscViewerASCIIPrintf(viewer, " maximum time=%g\n", (double)ts->max_time)); 19171e66621cSBarry Smith if (ts->ifuncs) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of I function evaluations=%" PetscInt_FMT "\n", ts->ifuncs)); 19181e66621cSBarry Smith if (ts->ijacs) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of I Jacobian evaluations=%" PetscInt_FMT "\n", ts->ijacs)); 19191e66621cSBarry Smith if (ts->rhsfuncs) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of RHS function evaluations=%" PetscInt_FMT "\n", ts->rhsfuncs)); 19201e66621cSBarry Smith if (ts->rhsjacs) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of RHS Jacobian evaluations=%" PetscInt_FMT "\n", ts->rhsjacs)); 1921efd4aadfSBarry Smith if (ts->usessnes) { 1922efd4aadfSBarry Smith PetscBool lin; 19231e66621cSBarry Smith if (ts->problem_type == TS_NONLINEAR) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of nonlinear solver iterations=%" PetscInt_FMT "\n", ts->snes_its)); 192463a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " total number of linear solver iterations=%" PetscInt_FMT "\n", ts->ksp_its)); 19259566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompareAny((PetscObject)ts->snes, &lin, SNESKSPONLY, SNESKSPTRANSPOSEONLY, "")); 192663a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " total number of %slinear solve failures=%" PetscInt_FMT "\n", lin ? "" : "non", ts->num_snes_failures)); 1927efd4aadfSBarry Smith } 192863a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " total number of rejected steps=%" PetscInt_FMT "\n", ts->reject)); 19291e66621cSBarry Smith if (ts->vrtol) PetscCall(PetscViewerASCIIPrintf(viewer, " using vector of relative error tolerances, ")); 19301e66621cSBarry Smith else PetscCall(PetscViewerASCIIPrintf(viewer, " using relative error tolerance of %g, ", (double)ts->rtol)); 19311e66621cSBarry Smith if (ts->vatol) PetscCall(PetscViewerASCIIPrintf(viewer, " using vector of absolute error tolerances\n")); 19321e66621cSBarry Smith else PetscCall(PetscViewerASCIIPrintf(viewer, " using absolute error tolerance of %g\n", (double)ts->atol)); 19339566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 19349566063dSJacob Faibussowitsch PetscCall(TSAdaptView(ts->adapt, viewer)); 19359566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 19360f5bd95cSBarry Smith } else if (isstring) { 19379566063dSJacob Faibussowitsch PetscCall(TSGetType(ts, &type)); 19389566063dSJacob Faibussowitsch PetscCall(PetscViewerStringSPrintf(viewer, " TSType: %-7.7s", type)); 1939dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, view, viewer); 194055849f57SBarry Smith } else if (isbinary) { 194155849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 194255849f57SBarry Smith MPI_Comm comm; 194355849f57SBarry Smith PetscMPIInt rank; 194455849f57SBarry Smith char type[256]; 194555849f57SBarry Smith 19469566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)ts, &comm)); 19479566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 1948dd400576SPatrick Sanan if (rank == 0) { 19499566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer, &classid, 1, PETSC_INT)); 19509566063dSJacob Faibussowitsch PetscCall(PetscStrncpy(type, ((PetscObject)ts)->type_name, 256)); 19519566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer, type, 256, PETSC_CHAR)); 195255849f57SBarry Smith } 1953dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, view, viewer); 19549566063dSJacob Faibussowitsch if (ts->adapt) PetscCall(TSAdaptView(ts->adapt, viewer)); 19559566063dSJacob Faibussowitsch PetscCall(DMView(ts->dm, viewer)); 19569566063dSJacob Faibussowitsch PetscCall(VecView(ts->vec_sol, viewer)); 19579566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm, &sdm)); 19589566063dSJacob Faibussowitsch PetscCall(DMTSView(sdm, viewer)); 19592b0a91c0SBarry Smith } else if (isdraw) { 19602b0a91c0SBarry Smith PetscDraw draw; 19612b0a91c0SBarry Smith char str[36]; 196289fd9fafSBarry Smith PetscReal x, y, bottom, h; 19632b0a91c0SBarry Smith 19649566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw)); 19659566063dSJacob Faibussowitsch PetscCall(PetscDrawGetCurrentPoint(draw, &x, &y)); 1966c6a7a370SJeremy L Thompson PetscCall(PetscStrncpy(str, "TS: ", sizeof(str))); 1967c6a7a370SJeremy L Thompson PetscCall(PetscStrlcat(str, ((PetscObject)ts)->type_name, sizeof(str))); 19689566063dSJacob Faibussowitsch PetscCall(PetscDrawStringBoxed(draw, x, y, PETSC_DRAW_BLACK, PETSC_DRAW_BLACK, str, NULL, &h)); 196989fd9fafSBarry Smith bottom = y - h; 19709566063dSJacob Faibussowitsch PetscCall(PetscDrawPushCurrentPoint(draw, x, bottom)); 1971dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, view, viewer); 19729566063dSJacob Faibussowitsch if (ts->adapt) PetscCall(TSAdaptView(ts->adapt, viewer)); 19739566063dSJacob Faibussowitsch if (ts->snes) PetscCall(SNESView(ts->snes, viewer)); 19749566063dSJacob Faibussowitsch PetscCall(PetscDrawPopCurrentPoint(draw)); 1975e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1976536b137fSBarry Smith } else if (issaws) { 1977d45a07a7SBarry Smith PetscMPIInt rank; 19782657e9d9SBarry Smith const char *name; 19792657e9d9SBarry Smith 19809566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)ts, &name)); 19819566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank)); 1982dd400576SPatrick Sanan if (!((PetscObject)ts)->amsmem && rank == 0) { 1983d45a07a7SBarry Smith char dir[1024]; 1984d45a07a7SBarry Smith 19859566063dSJacob Faibussowitsch PetscCall(PetscObjectViewSAWs((PetscObject)ts, viewer)); 19869566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(dir, 1024, "/PETSc/Objects/%s/time_step", name)); 1987792fecdfSBarry Smith PetscCallSAWs(SAWs_Register, (dir, &ts->steps, 1, SAWs_READ, SAWs_INT)); 19889566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(dir, 1024, "/PETSc/Objects/%s/time", name)); 1989792fecdfSBarry Smith PetscCallSAWs(SAWs_Register, (dir, &ts->ptime, 1, SAWs_READ, SAWs_DOUBLE)); 1990d763cef2SBarry Smith } 1991dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, view, viewer); 1992f05ece33SBarry Smith #endif 1993f05ece33SBarry Smith } 199436a9e3b9SBarry Smith if (ts->snes && ts->usessnes) { 19959566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 19969566063dSJacob Faibussowitsch PetscCall(SNESView(ts->snes, viewer)); 19979566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 199836a9e3b9SBarry Smith } 19999566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm, &sdm)); 20009566063dSJacob Faibussowitsch PetscCall(DMTSView(sdm, viewer)); 2001f05ece33SBarry Smith 20029566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 20039566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)ts, TSSUNDIALS, &isundials)); 20049566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 20053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2006d763cef2SBarry Smith } 2007d763cef2SBarry Smith 2008b07ff414SBarry Smith /*@ 2009ce78bad3SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for the timesteppers that may be accessed, for example inside the user provided 2010ce78bad3SBarry Smith `TS` callbacks with `TSGetApplicationContext()` 2011d763cef2SBarry Smith 2012c3339decSBarry Smith Logically Collective 2013d763cef2SBarry Smith 2014d763cef2SBarry Smith Input Parameters: 2015bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 201649abdd8aSBarry Smith - ctx - user context 2017daf670e6SBarry Smith 2018d763cef2SBarry Smith Level: intermediate 2019d763cef2SBarry Smith 2020ce78bad3SBarry Smith Fortran Note: 2021ce78bad3SBarry Smith This only works when `ctx` is a Fortran derived type (it cannot be a `PetscObject`), we recommend writing a Fortran interface definition for this 2022ce78bad3SBarry Smith function that tells the Fortran compiler the derived data type that is passed in as the `ctx` argument. See `TSGetApplicationContext()` for 2023ce78bad3SBarry Smith an example. 2024bcf0153eSBarry Smith 20251cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetApplicationContext()` 2026d763cef2SBarry Smith @*/ 2027ce78bad3SBarry Smith PetscErrorCode TSSetApplicationContext(TS ts, PeCtx ctx) 2028d71ae5a4SJacob Faibussowitsch { 2029d763cef2SBarry Smith PetscFunctionBegin; 20300700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 203149abdd8aSBarry Smith ts->ctx = ctx; 20323ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2033d763cef2SBarry Smith } 2034d763cef2SBarry Smith 2035b07ff414SBarry Smith /*@ 2036d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 2037bcf0153eSBarry Smith timestepper that was set with `TSSetApplicationContext()` 2038d763cef2SBarry Smith 2039d763cef2SBarry Smith Not Collective 2040d763cef2SBarry Smith 2041d763cef2SBarry Smith Input Parameter: 2042bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2043d763cef2SBarry Smith 2044d763cef2SBarry Smith Output Parameter: 2045ce78bad3SBarry Smith . ctx - a pointer to the user context 2046d763cef2SBarry Smith 2047bcf0153eSBarry Smith Level: intermediate 2048bcf0153eSBarry Smith 2049b43aa488SJacob Faibussowitsch Fortran Notes: 2050ce78bad3SBarry Smith This only works when the context is a Fortran derived type (it cannot be a `PetscObject`) and you **must** write a Fortran interface definition for this 2051ce78bad3SBarry Smith function that tells the Fortran compiler the derived data type that is returned as the `ctx` argument. For example, 2052ce78bad3SBarry Smith .vb 2053ce78bad3SBarry Smith Interface TSGetApplicationContext 2054ce78bad3SBarry Smith Subroutine TSGetApplicationContext(ts,ctx,ierr) 2055ce78bad3SBarry Smith #include <petsc/finclude/petscts.h> 2056ce78bad3SBarry Smith use petscts 2057ce78bad3SBarry Smith TS ts 2058ce78bad3SBarry Smith type(tUsertype), pointer :: ctx 2059ce78bad3SBarry Smith PetscErrorCode ierr 2060ce78bad3SBarry Smith End Subroutine 2061ce78bad3SBarry Smith End Interface TSGetApplicationContext 2062ce78bad3SBarry Smith .ve 2063ce78bad3SBarry Smith 2064*bfe80ac4SPierre Jolivet The prototype for `ctx` must be 2065ce78bad3SBarry Smith .vb 2066ce78bad3SBarry Smith type(tUsertype), pointer :: ctx 2067ce78bad3SBarry Smith .ve 2068daf670e6SBarry Smith 20691cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetApplicationContext()` 2070d763cef2SBarry Smith @*/ 207149abdd8aSBarry Smith PetscErrorCode TSGetApplicationContext(TS ts, void *ctx) 2072d71ae5a4SJacob Faibussowitsch { 2073d763cef2SBarry Smith PetscFunctionBegin; 20740700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 207549abdd8aSBarry Smith *(void **)ctx = ts->ctx; 20763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2077d763cef2SBarry Smith } 2078d763cef2SBarry Smith 2079d763cef2SBarry Smith /*@ 2080bcf0153eSBarry Smith TSGetStepNumber - Gets the number of time steps completed. 2081d763cef2SBarry Smith 2082d763cef2SBarry Smith Not Collective 2083d763cef2SBarry Smith 2084d763cef2SBarry Smith Input Parameter: 2085bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2086d763cef2SBarry Smith 2087d763cef2SBarry Smith Output Parameter: 208880275a0aSLisandro Dalcin . steps - number of steps completed so far 2089d763cef2SBarry Smith 2090d763cef2SBarry Smith Level: intermediate 2091d763cef2SBarry Smith 20921cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTime()`, `TSGetTimeStep()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostStage()`, `TSSetPostStep()` 2093d763cef2SBarry Smith @*/ 2094d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetStepNumber(TS ts, PetscInt *steps) 2095d71ae5a4SJacob Faibussowitsch { 2096d763cef2SBarry Smith PetscFunctionBegin; 20970700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 20984f572ea9SToby Isaac PetscAssertPointer(steps, 2); 209980275a0aSLisandro Dalcin *steps = ts->steps; 21003ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 210180275a0aSLisandro Dalcin } 210280275a0aSLisandro Dalcin 210380275a0aSLisandro Dalcin /*@ 210480275a0aSLisandro Dalcin TSSetStepNumber - Sets the number of steps completed. 210580275a0aSLisandro Dalcin 2106c3339decSBarry Smith Logically Collective 210780275a0aSLisandro Dalcin 210880275a0aSLisandro Dalcin Input Parameters: 2109bcf0153eSBarry Smith + ts - the `TS` context 211080275a0aSLisandro Dalcin - steps - number of steps completed so far 211180275a0aSLisandro Dalcin 2112bcf0153eSBarry Smith Level: developer 2113bcf0153eSBarry Smith 2114bcf0153eSBarry Smith Note: 2115bcf0153eSBarry Smith For most uses of the `TS` solvers the user need not explicitly call 2116bcf0153eSBarry Smith `TSSetStepNumber()`, as the step counter is appropriately updated in 2117bcf0153eSBarry Smith `TSSolve()`/`TSStep()`/`TSRollBack()`. Power users may call this routine to 211880275a0aSLisandro Dalcin reinitialize timestepping by setting the step counter to zero (and time 211980275a0aSLisandro Dalcin to the initial time) to solve a similar problem with different initial 212080275a0aSLisandro Dalcin conditions or parameters. Other possible use case is to continue 2121195e9b02SBarry Smith timestepping from a previously interrupted run in such a way that `TS` 212280275a0aSLisandro Dalcin monitors will be called with a initial nonzero step counter. 212380275a0aSLisandro Dalcin 21241cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetStepNumber()`, `TSSetTime()`, `TSSetTimeStep()`, `TSSetSolution()` 212580275a0aSLisandro Dalcin @*/ 2126d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetStepNumber(TS ts, PetscInt steps) 2127d71ae5a4SJacob Faibussowitsch { 212880275a0aSLisandro Dalcin PetscFunctionBegin; 212980275a0aSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 213080275a0aSLisandro Dalcin PetscValidLogicalCollectiveInt(ts, steps, 2); 21313c633725SBarry Smith PetscCheck(steps >= 0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Step number must be non-negative"); 213280275a0aSLisandro Dalcin ts->steps = steps; 21333ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2134d763cef2SBarry Smith } 2135d763cef2SBarry Smith 2136d763cef2SBarry Smith /*@ 2137d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 2138d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 2139d763cef2SBarry Smith 2140c3339decSBarry Smith Logically Collective 2141d763cef2SBarry Smith 2142d763cef2SBarry Smith Input Parameters: 2143bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 2144d763cef2SBarry Smith - time_step - the size of the timestep 2145d763cef2SBarry Smith 2146d763cef2SBarry Smith Level: intermediate 2147d763cef2SBarry Smith 21481cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSPSEUDO`, `TSGetTimeStep()`, `TSSetTime()` 2149d763cef2SBarry Smith @*/ 2150d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTimeStep(TS ts, PetscReal time_step) 2151d71ae5a4SJacob Faibussowitsch { 2152d763cef2SBarry Smith PetscFunctionBegin; 21530700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2154c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts, time_step, 2); 2155d763cef2SBarry Smith ts->time_step = time_step; 21563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2157d763cef2SBarry Smith } 2158d763cef2SBarry Smith 2159a43b19c4SJed Brown /*@ 216049354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 21610b4b7b1cSBarry Smith match the exact final time, to interpolate the solution to the exact final time, 21620b4b7b1cSBarry Smith or to just return at the final time `TS` computed (which may be slightly larger 21630b4b7b1cSBarry Smith than the requested final time). 2164a43b19c4SJed Brown 2165c3339decSBarry Smith Logically Collective 2166a43b19c4SJed Brown 2167d8d19677SJose E. Roman Input Parameters: 2168a43b19c4SJed Brown + ts - the time-step context 216949354f04SShri Abhyankar - eftopt - exact final time option 2170bcf0153eSBarry Smith .vb 21710b4b7b1cSBarry Smith TS_EXACTFINALTIME_STEPOVER - Don't do anything if final time is exceeded, just use it 21720b4b7b1cSBarry Smith TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time if the final time is exceeded 21730b4b7b1cSBarry Smith TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to ensure the computed final time exactly equals the requested final time 2174bcf0153eSBarry Smith .ve 2175a43b19c4SJed Brown 2176bcf0153eSBarry Smith Options Database Key: 21770b4b7b1cSBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step approach at runtime 2178feed9e9dSBarry Smith 2179a43b19c4SJed Brown Level: beginner 2180a43b19c4SJed Brown 2181bcf0153eSBarry Smith Note: 2182bcf0153eSBarry Smith If you use the option `TS_EXACTFINALTIME_STEPOVER` the solution may be at a very different time 2183bcf0153eSBarry Smith then the final time you selected. 2184bcf0153eSBarry Smith 21851cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSExactFinalTimeOption`, `TSGetExactFinalTime()` 2186a43b19c4SJed Brown @*/ 2187d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetExactFinalTime(TS ts, TSExactFinalTimeOption eftopt) 2188d71ae5a4SJacob Faibussowitsch { 2189a43b19c4SJed Brown PetscFunctionBegin; 2190a43b19c4SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 219149354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts, eftopt, 2); 219249354f04SShri Abhyankar ts->exact_final_time = eftopt; 21933ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2194a43b19c4SJed Brown } 2195a43b19c4SJed Brown 2196d763cef2SBarry Smith /*@ 2197bcf0153eSBarry Smith TSGetExactFinalTime - Gets the exact final time option set with `TSSetExactFinalTime()` 2198f6953c82SLisandro Dalcin 2199f6953c82SLisandro Dalcin Not Collective 2200f6953c82SLisandro Dalcin 2201f6953c82SLisandro Dalcin Input Parameter: 2202bcf0153eSBarry Smith . ts - the `TS` context 2203f6953c82SLisandro Dalcin 2204f6953c82SLisandro Dalcin Output Parameter: 2205f6953c82SLisandro Dalcin . eftopt - exact final time option 2206f6953c82SLisandro Dalcin 2207f6953c82SLisandro Dalcin Level: beginner 2208f6953c82SLisandro Dalcin 22091cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSExactFinalTimeOption`, `TSSetExactFinalTime()` 2210f6953c82SLisandro Dalcin @*/ 2211d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetExactFinalTime(TS ts, TSExactFinalTimeOption *eftopt) 2212d71ae5a4SJacob Faibussowitsch { 2213f6953c82SLisandro Dalcin PetscFunctionBegin; 2214f6953c82SLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 22154f572ea9SToby Isaac PetscAssertPointer(eftopt, 2); 2216f6953c82SLisandro Dalcin *eftopt = ts->exact_final_time; 22173ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2218f6953c82SLisandro Dalcin } 2219f6953c82SLisandro Dalcin 2220f6953c82SLisandro Dalcin /*@ 2221d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 2222d763cef2SBarry Smith 2223d763cef2SBarry Smith Not Collective 2224d763cef2SBarry Smith 2225d763cef2SBarry Smith Input Parameter: 2226bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2227d763cef2SBarry Smith 2228d763cef2SBarry Smith Output Parameter: 2229d763cef2SBarry Smith . dt - the current timestep size 2230d763cef2SBarry Smith 2231d763cef2SBarry Smith Level: intermediate 2232d763cef2SBarry Smith 22331cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetTimeStep()`, `TSGetTime()` 2234d763cef2SBarry Smith @*/ 2235d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeStep(TS ts, PetscReal *dt) 2236d71ae5a4SJacob Faibussowitsch { 2237d763cef2SBarry Smith PetscFunctionBegin; 22380700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 22394f572ea9SToby Isaac PetscAssertPointer(dt, 2); 2240d763cef2SBarry Smith *dt = ts->time_step; 22413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2242d763cef2SBarry Smith } 2243d763cef2SBarry Smith 2244d8e5e3e6SSatish Balay /*@ 2245d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 2246d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 2247d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 2248d763cef2SBarry Smith the solution at the next timestep has been calculated. 2249d763cef2SBarry Smith 2250bcf0153eSBarry Smith Not Collective, but v returned is parallel if ts is parallel 2251d763cef2SBarry Smith 2252d763cef2SBarry Smith Input Parameter: 2253bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2254d763cef2SBarry Smith 2255d763cef2SBarry Smith Output Parameter: 2256d763cef2SBarry Smith . v - the vector containing the solution 2257d763cef2SBarry Smith 2258d763cef2SBarry Smith Level: intermediate 2259d763cef2SBarry Smith 2260bcf0153eSBarry Smith Note: 2261bcf0153eSBarry Smith If you used `TSSetExactFinalTime`(ts,`TS_EXACTFINALTIME_MATCHSTEP`); this does not return the solution at the requested 2262bcf0153eSBarry Smith final time. It returns the solution at the next timestep. 2263d763cef2SBarry Smith 22641cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTimeStep()`, `TSGetTime()`, `TSGetSolveTime()`, `TSGetSolutionComponents()`, `TSSetSolutionFunction()` 2265d763cef2SBarry Smith @*/ 2266d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSolution(TS ts, Vec *v) 2267d71ae5a4SJacob Faibussowitsch { 2268d763cef2SBarry Smith PetscFunctionBegin; 22690700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 22704f572ea9SToby Isaac PetscAssertPointer(v, 2); 22718737fe31SLisandro Dalcin *v = ts->vec_sol; 22723ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2273d763cef2SBarry Smith } 2274d763cef2SBarry Smith 227503fe5f5eSDebojyoti Ghosh /*@ 2276b2bf4f3aSDebojyoti Ghosh TSGetSolutionComponents - Returns any solution components at the present 227703fe5f5eSDebojyoti Ghosh timestep, if available for the time integration method being used. 2278b2bf4f3aSDebojyoti Ghosh Solution components are quantities that share the same size and 227903fe5f5eSDebojyoti Ghosh structure as the solution vector. 228003fe5f5eSDebojyoti Ghosh 2281bcf0153eSBarry Smith Not Collective, but v returned is parallel if ts is parallel 228203fe5f5eSDebojyoti Ghosh 2283b43aa488SJacob Faibussowitsch Input Parameters: 2284bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` (input parameter). 2285195e9b02SBarry Smith . n - If v is `NULL`, then the number of solution components is 2286b2bf4f3aSDebojyoti Ghosh returned through n, else the n-th solution component is 228703fe5f5eSDebojyoti Ghosh returned in v. 2288a2b725a8SWilliam Gropp - v - the vector containing the n-th solution component 2289195e9b02SBarry Smith (may be `NULL` to use this function to find out 2290b2bf4f3aSDebojyoti Ghosh the number of solutions components). 229103fe5f5eSDebojyoti Ghosh 22924cdd57e5SDebojyoti Ghosh Level: advanced 229303fe5f5eSDebojyoti Ghosh 22941cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetSolution()` 229503fe5f5eSDebojyoti Ghosh @*/ 2296d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSolutionComponents(TS ts, PetscInt *n, Vec *v) 2297d71ae5a4SJacob Faibussowitsch { 229803fe5f5eSDebojyoti Ghosh PetscFunctionBegin; 229903fe5f5eSDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2300b2bf4f3aSDebojyoti Ghosh if (!ts->ops->getsolutioncomponents) *n = 0; 2301dbbe0bcdSBarry Smith else PetscUseTypeMethod(ts, getsolutioncomponents, n, v); 23023ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 230303fe5f5eSDebojyoti Ghosh } 230403fe5f5eSDebojyoti Ghosh 23054cdd57e5SDebojyoti Ghosh /*@ 23064cdd57e5SDebojyoti Ghosh TSGetAuxSolution - Returns an auxiliary solution at the present 23074cdd57e5SDebojyoti Ghosh timestep, if available for the time integration method being used. 23084cdd57e5SDebojyoti Ghosh 2309bcf0153eSBarry Smith Not Collective, but v returned is parallel if ts is parallel 23104cdd57e5SDebojyoti Ghosh 2311b43aa488SJacob Faibussowitsch Input Parameters: 2312bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` (input parameter). 2313a2b725a8SWilliam Gropp - v - the vector containing the auxiliary solution 23144cdd57e5SDebojyoti Ghosh 23154cdd57e5SDebojyoti Ghosh Level: intermediate 23164cdd57e5SDebojyoti Ghosh 23171cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetSolution()` 23184cdd57e5SDebojyoti Ghosh @*/ 2319d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetAuxSolution(TS ts, Vec *v) 2320d71ae5a4SJacob Faibussowitsch { 23214cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 23224cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2323dbbe0bcdSBarry Smith if (ts->ops->getauxsolution) PetscUseTypeMethod(ts, getauxsolution, v); 23241e66621cSBarry Smith else PetscCall(VecZeroEntries(*v)); 23253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23264cdd57e5SDebojyoti Ghosh } 23274cdd57e5SDebojyoti Ghosh 23284cdd57e5SDebojyoti Ghosh /*@ 23294cdd57e5SDebojyoti Ghosh TSGetTimeError - Returns the estimated error vector, if the chosen 2330bcf0153eSBarry Smith `TSType` has an error estimation functionality and `TSSetTimeError()` was called 23314cdd57e5SDebojyoti Ghosh 2332bcf0153eSBarry Smith Not Collective, but v returned is parallel if ts is parallel 23339657682dSDebojyoti Ghosh 2334b43aa488SJacob Faibussowitsch Input Parameters: 2335bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` (input parameter). 2336657c1e31SEmil Constantinescu . n - current estimate (n=0) or previous one (n=-1) 2337a2b725a8SWilliam Gropp - v - the vector containing the error (same size as the solution). 23384cdd57e5SDebojyoti Ghosh 23394cdd57e5SDebojyoti Ghosh Level: intermediate 23404cdd57e5SDebojyoti Ghosh 2341bcf0153eSBarry Smith Note: 2342bcf0153eSBarry Smith MUST call after `TSSetUp()` 23434cdd57e5SDebojyoti Ghosh 23441cc06b55SBarry Smith .seealso: [](ch_ts), `TSGetSolution()`, `TSSetTimeError()` 23454cdd57e5SDebojyoti Ghosh @*/ 2346d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeError(TS ts, PetscInt n, Vec *v) 2347d71ae5a4SJacob Faibussowitsch { 23484cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 23494cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2350dbbe0bcdSBarry Smith if (ts->ops->gettimeerror) PetscUseTypeMethod(ts, gettimeerror, n, v); 23511e66621cSBarry Smith else PetscCall(VecZeroEntries(*v)); 23523ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23534cdd57e5SDebojyoti Ghosh } 23544cdd57e5SDebojyoti Ghosh 235557df6a1bSDebojyoti Ghosh /*@ 235657df6a1bSDebojyoti Ghosh TSSetTimeError - Sets the estimated error vector, if the chosen 2357bcf0153eSBarry Smith `TSType` has an error estimation functionality. This can be used 235857df6a1bSDebojyoti Ghosh to restart such a time integrator with a given error vector. 235957df6a1bSDebojyoti Ghosh 2360bcf0153eSBarry Smith Not Collective, but v returned is parallel if ts is parallel 236157df6a1bSDebojyoti Ghosh 2362b43aa488SJacob Faibussowitsch Input Parameters: 2363bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` (input parameter). 2364a2b725a8SWilliam Gropp - v - the vector containing the error (same size as the solution). 236557df6a1bSDebojyoti Ghosh 236657df6a1bSDebojyoti Ghosh Level: intermediate 236757df6a1bSDebojyoti Ghosh 2368b43aa488SJacob Faibussowitsch .seealso: [](ch_ts), `TS`, `TSSetSolution()`, `TSGetTimeError()` 236957df6a1bSDebojyoti Ghosh @*/ 2370d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTimeError(TS ts, Vec v) 2371d71ae5a4SJacob Faibussowitsch { 237257df6a1bSDebojyoti Ghosh PetscFunctionBegin; 237357df6a1bSDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 23743c633725SBarry Smith PetscCheck(ts->setupcalled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call TSSetUp() first"); 2375dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, settimeerror, v); 23763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 237757df6a1bSDebojyoti Ghosh } 237857df6a1bSDebojyoti Ghosh 2379bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 2380d8e5e3e6SSatish Balay /*@ 2381bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 2382d763cef2SBarry Smith 2383bdad233fSMatthew Knepley Not collective 2384d763cef2SBarry Smith 2385bdad233fSMatthew Knepley Input Parameters: 2386bcf0153eSBarry Smith + ts - The `TS` 2387bcf0153eSBarry Smith - type - One of `TS_LINEAR`, `TS_NONLINEAR` where these types refer to problems of the forms 2388d763cef2SBarry Smith .vb 23890910c330SBarry Smith U_t - A U = 0 (linear) 23900910c330SBarry Smith U_t - A(t) U = 0 (linear) 23910910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 2392d763cef2SBarry Smith .ve 2393d763cef2SBarry Smith 2394d763cef2SBarry Smith Level: beginner 2395d763cef2SBarry Smith 23961cc06b55SBarry Smith .seealso: [](ch_ts), `TSSetUp()`, `TSProblemType`, `TS` 2397d763cef2SBarry Smith @*/ 2398d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 2399d71ae5a4SJacob Faibussowitsch { 2400d763cef2SBarry Smith PetscFunctionBegin; 24010700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2402bdad233fSMatthew Knepley ts->problem_type = type; 24039e2a6581SJed Brown if (type == TS_LINEAR) { 24049e2a6581SJed Brown SNES snes; 24059566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 24069566063dSJacob Faibussowitsch PetscCall(SNESSetType(snes, SNESKSPONLY)); 24079e2a6581SJed Brown } 24083ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2409d763cef2SBarry Smith } 2410d763cef2SBarry Smith 2411cc4c1da9SBarry Smith /*@ 2412bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 2413bdad233fSMatthew Knepley 2414bdad233fSMatthew Knepley Not collective 2415bdad233fSMatthew Knepley 2416bdad233fSMatthew Knepley Input Parameter: 2417bcf0153eSBarry Smith . ts - The `TS` 2418bdad233fSMatthew Knepley 2419bdad233fSMatthew Knepley Output Parameter: 2420bcf0153eSBarry Smith . type - One of `TS_LINEAR`, `TS_NONLINEAR` where these types refer to problems of the forms 2421bdad233fSMatthew Knepley .vb 2422089b2837SJed Brown M U_t = A U 2423089b2837SJed Brown M(t) U_t = A(t) U 2424b5abc632SBarry Smith F(t,U,U_t) 2425bdad233fSMatthew Knepley .ve 2426bdad233fSMatthew Knepley 2427bdad233fSMatthew Knepley Level: beginner 2428bdad233fSMatthew Knepley 24291cc06b55SBarry Smith .seealso: [](ch_ts), `TSSetUp()`, `TSProblemType`, `TS` 2430bdad233fSMatthew Knepley @*/ 2431d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 2432d71ae5a4SJacob Faibussowitsch { 2433bdad233fSMatthew Knepley PetscFunctionBegin; 24340700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 24354f572ea9SToby Isaac PetscAssertPointer(type, 2); 2436bdad233fSMatthew Knepley *type = ts->problem_type; 24373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2438bdad233fSMatthew Knepley } 2439d763cef2SBarry Smith 2440303a5415SBarry Smith /* 2441303a5415SBarry 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() 2442303a5415SBarry Smith */ 2443d71ae5a4SJacob Faibussowitsch static PetscErrorCode TSSetExactFinalTimeDefault(TS ts) 2444d71ae5a4SJacob Faibussowitsch { 2445303a5415SBarry Smith PetscBool isnone; 2446303a5415SBarry Smith 2447303a5415SBarry Smith PetscFunctionBegin; 24489566063dSJacob Faibussowitsch PetscCall(TSGetAdapt(ts, &ts->adapt)); 24499566063dSJacob Faibussowitsch PetscCall(TSAdaptSetDefaultType(ts->adapt, ts->default_adapt_type)); 2450303a5415SBarry Smith 24519566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)ts->adapt, TSADAPTNONE, &isnone)); 24521e66621cSBarry Smith if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP; 24531e66621cSBarry Smith else if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) ts->exact_final_time = TS_EXACTFINALTIME_INTERPOLATE; 24543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2455303a5415SBarry Smith } 2456303a5415SBarry Smith 2457d763cef2SBarry Smith /*@ 2458303a5415SBarry Smith TSSetUp - Sets up the internal data structures for the later use of a timestepper. 2459d763cef2SBarry Smith 2460c3339decSBarry Smith Collective 2461d763cef2SBarry Smith 2462d763cef2SBarry Smith Input Parameter: 2463bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2464d763cef2SBarry Smith 2465d763cef2SBarry Smith Level: advanced 2466d763cef2SBarry Smith 2467bcf0153eSBarry Smith Note: 2468bcf0153eSBarry Smith For basic use of the `TS` solvers the user need not explicitly call 2469bcf0153eSBarry Smith `TSSetUp()`, since these actions will automatically occur during 2470bcf0153eSBarry Smith the call to `TSStep()` or `TSSolve()`. However, if one wishes to control this 2471bcf0153eSBarry Smith phase separately, `TSSetUp()` should be called after `TSCreate()` 2472bcf0153eSBarry Smith and optional routines of the form TSSetXXX(), but before `TSStep()` and `TSSolve()`. 2473bcf0153eSBarry Smith 24741cc06b55SBarry Smith .seealso: [](ch_ts), `TSCreate()`, `TS`, `TSStep()`, `TSDestroy()`, `TSSolve()` 2475d763cef2SBarry Smith @*/ 2476d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetUp(TS ts) 2477d71ae5a4SJacob Faibussowitsch { 24786c6b9e74SPeter Brune DM dm; 24796c6b9e74SPeter Brune PetscErrorCode (*func)(SNES, Vec, Vec, void *); 2480d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES, Vec, Mat, Mat, void *); 24818434afd1SBarry Smith TSIFunctionFn *ifun; 24828434afd1SBarry Smith TSIJacobianFn *ijac; 24838434afd1SBarry Smith TSI2JacobianFn *i2jac; 24848434afd1SBarry Smith TSRHSJacobianFn *rhsjac; 2485d763cef2SBarry Smith 2486d763cef2SBarry Smith PetscFunctionBegin; 24870700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 24883ba16761SJacob Faibussowitsch if (ts->setupcalled) PetscFunctionReturn(PETSC_SUCCESS); 2489277b19d0SLisandro Dalcin 24907adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 24919566063dSJacob Faibussowitsch PetscCall(TSGetIFunction(ts, NULL, &ifun, NULL)); 24929566063dSJacob Faibussowitsch PetscCall(TSSetType(ts, ifun ? TSBEULER : TSEULER)); 2493d763cef2SBarry Smith } 2494277b19d0SLisandro Dalcin 24951a638600SStefano Zampini if (!ts->vec_sol) { 24961e66621cSBarry Smith PetscCheck(ts->dm, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call TSSetSolution() first"); 24979566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(ts->dm, &ts->vec_sol)); 24981a638600SStefano Zampini } 2499277b19d0SLisandro Dalcin 2500136cf249SJames Wright if (ts->eval_times) { 2501136cf249SJames Wright if (!ts->eval_times->sol_vecs) PetscCall(VecDuplicateVecs(ts->vec_sol, ts->eval_times->num_time_points, &ts->eval_times->sol_vecs)); 2502136cf249SJames Wright if (!ts->eval_times->sol_times) PetscCall(PetscMalloc1(ts->eval_times->num_time_points, &ts->eval_times->sol_times)); 25034a658b32SHong Zhang } 2504298bade4SHong Zhang if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */ 25059566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)ts->Jacprhs)); 2506298bade4SHong Zhang ts->Jacp = ts->Jacprhs; 2507298bade4SHong Zhang } 2508298bade4SHong Zhang 2509cd4cee2dSHong Zhang if (ts->quadraturets) { 25109566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts->quadraturets)); 25119566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_costintegrand)); 25129566063dSJacob Faibussowitsch PetscCall(VecDuplicate(ts->quadraturets->vec_sol, &ts->vec_costintegrand)); 2513cd4cee2dSHong Zhang } 2514cd4cee2dSHong Zhang 25159566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts, NULL, NULL, &rhsjac, NULL)); 2516f23ba4b3SHong Zhang if (rhsjac == TSComputeRHSJacobianConstant) { 2517e1244c69SJed Brown Mat Amat, Pmat; 2518e1244c69SJed Brown SNES snes; 25199566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 25209566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes, &Amat, &Pmat, NULL, NULL)); 2521e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 2522e1244c69SJed Brown * have displaced the RHS matrix */ 2523971015bcSStefano Zampini if (Amat && Amat == ts->Arhs) { 2524abc0d4abSBarry 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 */ 25259566063dSJacob Faibussowitsch PetscCall(MatDuplicate(ts->Arhs, MAT_COPY_VALUES, &Amat)); 25269566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes, Amat, NULL, NULL, NULL)); 25279566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Amat)); 2528e1244c69SJed Brown } 2529971015bcSStefano Zampini if (Pmat && Pmat == ts->Brhs) { 25309566063dSJacob Faibussowitsch PetscCall(MatDuplicate(ts->Brhs, MAT_COPY_VALUES, &Pmat)); 25319566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes, NULL, Pmat, NULL, NULL)); 25329566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Pmat)); 2533e1244c69SJed Brown } 2534e1244c69SJed Brown } 25352ffb9264SLisandro Dalcin 25369566063dSJacob Faibussowitsch PetscCall(TSGetAdapt(ts, &ts->adapt)); 25379566063dSJacob Faibussowitsch PetscCall(TSAdaptSetDefaultType(ts->adapt, ts->default_adapt_type)); 25382ffb9264SLisandro Dalcin 2539dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, setup); 2540277b19d0SLisandro Dalcin 25419566063dSJacob Faibussowitsch PetscCall(TSSetExactFinalTimeDefault(ts)); 25422ffb9264SLisandro Dalcin 2543a6772fa2SLisandro Dalcin /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 25446c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 25456c6b9e74SPeter Brune */ 25469566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 25479566063dSJacob Faibussowitsch PetscCall(DMSNESGetFunction(dm, &func, NULL)); 25481e66621cSBarry Smith if (!func) PetscCall(DMSNESSetFunction(dm, SNESTSFormFunction, ts)); 25491e66621cSBarry Smith 2550a6772fa2SLisandro 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. 25516c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 25526c6b9e74SPeter Brune */ 25539566063dSJacob Faibussowitsch PetscCall(DMSNESGetJacobian(dm, &jac, NULL)); 25549566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm, &ijac, NULL)); 25559566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm, &i2jac, NULL)); 25569566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, &rhsjac, NULL)); 25571e66621cSBarry Smith if (!jac && (ijac || i2jac || rhsjac)) PetscCall(DMSNESSetJacobian(dm, SNESTSFormJacobian, ts)); 2558c0517034SDebojyoti Ghosh 2559c0517034SDebojyoti Ghosh /* if time integration scheme has a starting method, call it */ 2560dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, startingmethod); 2561c0517034SDebojyoti Ghosh 2562277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 25633ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2564277b19d0SLisandro Dalcin } 2565277b19d0SLisandro Dalcin 2566f6a906c0SBarry Smith /*@ 25670b4b7b1cSBarry Smith TSReset - Resets a `TS` context to the state it was in before `TSSetUp()` was called and removes any allocated `Vec` and `Mat` from its data structures 2568277b19d0SLisandro Dalcin 2569c3339decSBarry Smith Collective 2570277b19d0SLisandro Dalcin 2571277b19d0SLisandro Dalcin Input Parameter: 2572bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2573277b19d0SLisandro Dalcin 25740b4b7b1cSBarry Smith Level: developer 2575277b19d0SLisandro Dalcin 25760b4b7b1cSBarry Smith Notes: 25770b4b7b1cSBarry Smith Any options set on the `TS` object, including those set with `TSSetFromOptions()` remain. 25780b4b7b1cSBarry Smith 25790b4b7b1cSBarry Smith See also `TSSetResize()` to change the size of the system being integrated (for example by adaptive mesh refinement) during the time integration. 25800b4b7b1cSBarry Smith 2581*bfe80ac4SPierre Jolivet .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSSetUp()`, `TSDestroy()`, `TSSetResize()` 2582277b19d0SLisandro Dalcin @*/ 2583d71ae5a4SJacob Faibussowitsch PetscErrorCode TSReset(TS ts) 2584d71ae5a4SJacob Faibussowitsch { 25851d06f6b3SHong Zhang TS_RHSSplitLink ilink = ts->tsrhssplit, next; 2586277b19d0SLisandro Dalcin 2587277b19d0SLisandro Dalcin PetscFunctionBegin; 2588277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2589b18ea86cSHong Zhang 2590dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, reset); 25919566063dSJacob Faibussowitsch if (ts->snes) PetscCall(SNESReset(ts->snes)); 25929566063dSJacob Faibussowitsch if (ts->adapt) PetscCall(TSAdaptReset(ts->adapt)); 2593bbd56ea5SKarl Rupp 25949566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Arhs)); 25959566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Brhs)); 25969566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->Frhs)); 25979566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_sol)); 2598c61711c8SStefano Zampini PetscCall(VecDestroy(&ts->vec_sol0)); 25999566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_dot)); 26009566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vatol)); 26019566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vrtol)); 26029566063dSJacob Faibussowitsch PetscCall(VecDestroyVecs(ts->nwork, &ts->work)); 2603bbd56ea5SKarl Rupp 26049566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Jacprhs)); 26059566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Jacp)); 26061baa6e33SBarry Smith if (ts->forward_solve) PetscCall(TSForwardReset(ts)); 2607cd4cee2dSHong Zhang if (ts->quadraturets) { 26089566063dSJacob Faibussowitsch PetscCall(TSReset(ts->quadraturets)); 26099566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_costintegrand)); 2610cd4cee2dSHong Zhang } 26111d06f6b3SHong Zhang while (ilink) { 26121d06f6b3SHong Zhang next = ilink->next; 26139566063dSJacob Faibussowitsch PetscCall(TSDestroy(&ilink->ts)); 26149566063dSJacob Faibussowitsch PetscCall(PetscFree(ilink->splitname)); 26159566063dSJacob Faibussowitsch PetscCall(ISDestroy(&ilink->is)); 26169566063dSJacob Faibussowitsch PetscCall(PetscFree(ilink)); 26171d06f6b3SHong Zhang ilink = next; 261887f4e208SHong Zhang } 26196bf673ebSJoe Pusztay ts->tsrhssplit = NULL; 2620545aaa6fSHong Zhang ts->num_rhs_splits = 0; 2621136cf249SJames Wright if (ts->eval_times) { 2622136cf249SJames Wright PetscCall(PetscFree(ts->eval_times->time_points)); 2623136cf249SJames Wright PetscCall(PetscFree(ts->eval_times->sol_times)); 2624136cf249SJames Wright PetscCall(VecDestroyVecs(ts->eval_times->num_time_points, &ts->eval_times->sol_vecs)); 2625136cf249SJames Wright PetscCall(PetscFree(ts->eval_times)); 26264a658b32SHong Zhang } 2627b3a72457SStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 2628b3a72457SStefano Zampini ts->rhsjacobian.scale = 1.0; 2629b3a72457SStefano Zampini ts->ijacobian.shift = 1.0; 2630277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 26313ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2632d763cef2SBarry Smith } 2633d763cef2SBarry Smith 263414d0ab18SJacob Faibussowitsch static PetscErrorCode TSResizeReset(TS); 263514d0ab18SJacob Faibussowitsch 26360764c050SBarry Smith /*@ 2637d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2638bcf0153eSBarry Smith with `TSCreate()`. 2639d763cef2SBarry Smith 2640c3339decSBarry Smith Collective 2641d763cef2SBarry Smith 2642d763cef2SBarry Smith Input Parameter: 2643bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2644d763cef2SBarry Smith 2645d763cef2SBarry Smith Level: beginner 2646d763cef2SBarry Smith 26471cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSSetUp()`, `TSSolve()` 2648d763cef2SBarry Smith @*/ 2649d71ae5a4SJacob Faibussowitsch PetscErrorCode TSDestroy(TS *ts) 2650d71ae5a4SJacob Faibussowitsch { 2651d763cef2SBarry Smith PetscFunctionBegin; 26523ba16761SJacob Faibussowitsch if (!*ts) PetscFunctionReturn(PETSC_SUCCESS); 2653ecf68647SHong Zhang PetscValidHeaderSpecific(*ts, TS_CLASSID, 1); 2654f4f49eeaSPierre Jolivet if (--((PetscObject)*ts)->refct > 0) { 26559371c9d4SSatish Balay *ts = NULL; 26563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 26579371c9d4SSatish Balay } 2658d763cef2SBarry Smith 26599566063dSJacob Faibussowitsch PetscCall(TSReset(*ts)); 26609566063dSJacob Faibussowitsch PetscCall(TSAdjointReset(*ts)); 26611e66621cSBarry Smith if ((*ts)->forward_solve) PetscCall(TSForwardReset(*ts)); 26626bd3a4fdSStefano Zampini PetscCall(TSResizeReset(*ts)); 26631e66621cSBarry Smith 2664e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 26659566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsViewOff((PetscObject)*ts)); 2666f4f49eeaSPierre Jolivet PetscTryTypeMethod(*ts, destroy); 26676d4c513bSLisandro Dalcin 26689566063dSJacob Faibussowitsch PetscCall(TSTrajectoryDestroy(&(*ts)->trajectory)); 2669bc952696SBarry Smith 26709566063dSJacob Faibussowitsch PetscCall(TSAdaptDestroy(&(*ts)->adapt)); 26719566063dSJacob Faibussowitsch PetscCall(TSEventDestroy(&(*ts)->event)); 26726427ac75SLisandro Dalcin 26739566063dSJacob Faibussowitsch PetscCall(SNESDestroy(&(*ts)->snes)); 26744bd3aaa3SHong Zhang PetscCall(SNESDestroy(&(*ts)->snesrhssplit)); 26759566063dSJacob Faibussowitsch PetscCall(DMDestroy(&(*ts)->dm)); 2676f4f49eeaSPierre Jolivet PetscCall(TSMonitorCancel(*ts)); 2677f4f49eeaSPierre Jolivet PetscCall(TSAdjointMonitorCancel(*ts)); 26786d4c513bSLisandro Dalcin 26799566063dSJacob Faibussowitsch PetscCall(TSDestroy(&(*ts)->quadraturets)); 26809566063dSJacob Faibussowitsch PetscCall(PetscHeaderDestroy(ts)); 26813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2682d763cef2SBarry Smith } 2683d763cef2SBarry Smith 2684d8e5e3e6SSatish Balay /*@ 2685bcf0153eSBarry Smith TSGetSNES - Returns the `SNES` (nonlinear solver) associated with 2686bcf0153eSBarry Smith a `TS` (timestepper) context. Valid only for nonlinear problems. 2687d763cef2SBarry Smith 2688bcf0153eSBarry Smith Not Collective, but snes is parallel if ts is parallel 2689d763cef2SBarry Smith 2690d763cef2SBarry Smith Input Parameter: 2691bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2692d763cef2SBarry Smith 2693d763cef2SBarry Smith Output Parameter: 2694d763cef2SBarry Smith . snes - the nonlinear solver context 2695d763cef2SBarry Smith 2696d763cef2SBarry Smith Level: beginner 2697d763cef2SBarry Smith 2698bcf0153eSBarry Smith Notes: 2699bcf0153eSBarry Smith The user can then directly manipulate the `SNES` context to set various 2700bcf0153eSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2701bcf0153eSBarry Smith `KSP`, and `PC` contexts as well. 2702bcf0153eSBarry Smith 2703bcf0153eSBarry Smith `TSGetSNES()` does not work for integrators that do not use `SNES`; in 2704195e9b02SBarry Smith this case `TSGetSNES()` returns `NULL` in `snes`. 2705bcf0153eSBarry Smith 27061cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `TSCreate()`, `TSSetUp()`, `TSSolve()` 2707d763cef2SBarry Smith @*/ 2708d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSNES(TS ts, SNES *snes) 2709d71ae5a4SJacob Faibussowitsch { 2710d763cef2SBarry Smith PetscFunctionBegin; 27110700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 27124f572ea9SToby Isaac PetscAssertPointer(snes, 2); 2713d372ba47SLisandro Dalcin if (!ts->snes) { 27149566063dSJacob Faibussowitsch PetscCall(SNESCreate(PetscObjectComm((PetscObject)ts), &ts->snes)); 27159566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptions((PetscObject)ts->snes, ((PetscObject)ts)->options)); 27169566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(ts->snes, NULL, SNESTSFormFunction, ts)); 27179566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)ts->snes, (PetscObject)ts, 1)); 27184bd3aaa3SHong Zhang if (ts->dm) PetscCall(SNESSetDM(ts->snes, ts->dm)); 27191e66621cSBarry Smith if (ts->problem_type == TS_LINEAR) PetscCall(SNESSetType(ts->snes, SNESKSPONLY)); 2720d372ba47SLisandro Dalcin } 2721d763cef2SBarry Smith *snes = ts->snes; 27223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2723d763cef2SBarry Smith } 2724d763cef2SBarry Smith 2725deb2cd25SJed Brown /*@ 27260b4b7b1cSBarry Smith TSSetSNES - Set the `SNES` (nonlinear solver) to be used by the `TS` timestepping context 2727deb2cd25SJed Brown 2728deb2cd25SJed Brown Collective 2729deb2cd25SJed Brown 2730d8d19677SJose E. Roman Input Parameters: 2731bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 2732deb2cd25SJed Brown - snes - the nonlinear solver context 2733deb2cd25SJed Brown 2734deb2cd25SJed Brown Level: developer 2735deb2cd25SJed Brown 2736bcf0153eSBarry Smith Note: 2737bcf0153eSBarry Smith Most users should have the `TS` created by calling `TSGetSNES()` 2738bcf0153eSBarry Smith 27391cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `TSCreate()`, `TSSetUp()`, `TSSolve()`, `TSGetSNES()` 2740deb2cd25SJed Brown @*/ 2741d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetSNES(TS ts, SNES snes) 2742d71ae5a4SJacob Faibussowitsch { 2743d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES, Vec, Mat, Mat, void *); 2744deb2cd25SJed Brown 2745deb2cd25SJed Brown PetscFunctionBegin; 2746deb2cd25SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2747deb2cd25SJed Brown PetscValidHeaderSpecific(snes, SNES_CLASSID, 2); 27489566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)snes)); 27499566063dSJacob Faibussowitsch PetscCall(SNESDestroy(&ts->snes)); 2750bbd56ea5SKarl Rupp 2751deb2cd25SJed Brown ts->snes = snes; 2752bbd56ea5SKarl Rupp 27539566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(ts->snes, NULL, SNESTSFormFunction, ts)); 27549566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(ts->snes, NULL, NULL, &func, NULL)); 27551e66621cSBarry Smith if (func == SNESTSFormJacobian) PetscCall(SNESSetJacobian(ts->snes, NULL, NULL, SNESTSFormJacobian, ts)); 27563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2757deb2cd25SJed Brown } 2758deb2cd25SJed Brown 2759d8e5e3e6SSatish Balay /*@ 2760bcf0153eSBarry Smith TSGetKSP - Returns the `KSP` (linear solver) associated with 2761bcf0153eSBarry Smith a `TS` (timestepper) context. 2762d763cef2SBarry Smith 2763195e9b02SBarry Smith Not Collective, but `ksp` is parallel if `ts` is parallel 2764d763cef2SBarry Smith 2765d763cef2SBarry Smith Input Parameter: 2766bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2767d763cef2SBarry Smith 2768d763cef2SBarry Smith Output Parameter: 276994b7f48cSBarry Smith . ksp - the nonlinear solver context 2770d763cef2SBarry Smith 2771d763cef2SBarry Smith Level: beginner 2772d763cef2SBarry Smith 2773bcf0153eSBarry Smith Notes: 2774bcf0153eSBarry Smith The user can then directly manipulate the `KSP` context to set various 2775bcf0153eSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2776bcf0153eSBarry Smith `PC` context as well. 2777bcf0153eSBarry Smith 2778bcf0153eSBarry Smith `TSGetKSP()` does not work for integrators that do not use `KSP`; 2779195e9b02SBarry Smith in this case `TSGetKSP()` returns `NULL` in `ksp`. 2780bcf0153eSBarry Smith 27811cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `KSP`, `TSCreate()`, `TSSetUp()`, `TSSolve()`, `TSGetSNES()` 2782d763cef2SBarry Smith @*/ 2783d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetKSP(TS ts, KSP *ksp) 2784d71ae5a4SJacob Faibussowitsch { 2785089b2837SJed Brown SNES snes; 2786d372ba47SLisandro Dalcin 2787d763cef2SBarry Smith PetscFunctionBegin; 27880700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 27894f572ea9SToby Isaac PetscAssertPointer(ksp, 2); 27903c633725SBarry Smith PetscCheck(((PetscObject)ts)->type_name, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "KSP is not created yet. Call TSSetType() first"); 27913c633725SBarry Smith PetscCheck(ts->problem_type == TS_LINEAR, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Linear only; use TSGetSNES()"); 27929566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 27939566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes, ksp)); 27943ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2795d763cef2SBarry Smith } 2796d763cef2SBarry Smith 2797d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2798d763cef2SBarry Smith 2799adb62b0dSMatthew Knepley /*@ 2800618ce8baSLisandro Dalcin TSSetMaxSteps - Sets the maximum number of steps to use. 2801618ce8baSLisandro Dalcin 2802c3339decSBarry Smith Logically Collective 2803618ce8baSLisandro Dalcin 2804618ce8baSLisandro Dalcin Input Parameters: 2805bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 2806618ce8baSLisandro Dalcin - maxsteps - maximum number of steps to use 2807618ce8baSLisandro Dalcin 2808bcf0153eSBarry Smith Options Database Key: 2809618ce8baSLisandro Dalcin . -ts_max_steps <maxsteps> - Sets maxsteps 2810618ce8baSLisandro Dalcin 2811618ce8baSLisandro Dalcin Level: intermediate 2812618ce8baSLisandro Dalcin 2813bcf0153eSBarry Smith Note: 281409cb0f53SBarry Smith Use `PETSC_DETERMINE` to reset the maximum number of steps to the default from when the object's type was set 281509cb0f53SBarry Smith 281609cb0f53SBarry Smith The default maximum number of steps is 5,000 281709cb0f53SBarry Smith 281809cb0f53SBarry Smith Fortran Note: 281909cb0f53SBarry Smith Use `PETSC_DETERMINE_INTEGER` 2820bcf0153eSBarry Smith 28211cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetMaxSteps()`, `TSSetMaxTime()`, `TSSetExactFinalTime()` 2822618ce8baSLisandro Dalcin @*/ 2823d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxSteps(TS ts, PetscInt maxsteps) 2824d71ae5a4SJacob Faibussowitsch { 2825618ce8baSLisandro Dalcin PetscFunctionBegin; 2826618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2827618ce8baSLisandro Dalcin PetscValidLogicalCollectiveInt(ts, maxsteps, 2); 282809cb0f53SBarry Smith if (maxsteps == PETSC_DETERMINE) { 282909cb0f53SBarry Smith ts->max_steps = ts->default_max_steps; 283009cb0f53SBarry Smith } else { 28313c633725SBarry Smith PetscCheck(maxsteps >= 0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Maximum number of steps must be non-negative"); 2832618ce8baSLisandro Dalcin ts->max_steps = maxsteps; 283309cb0f53SBarry Smith } 28343ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2835618ce8baSLisandro Dalcin } 2836618ce8baSLisandro Dalcin 2837618ce8baSLisandro Dalcin /*@ 2838618ce8baSLisandro Dalcin TSGetMaxSteps - Gets the maximum number of steps to use. 2839618ce8baSLisandro Dalcin 2840618ce8baSLisandro Dalcin Not Collective 2841618ce8baSLisandro Dalcin 28422fe279fdSBarry Smith Input Parameter: 2843bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2844618ce8baSLisandro Dalcin 2845618ce8baSLisandro Dalcin Output Parameter: 2846618ce8baSLisandro Dalcin . maxsteps - maximum number of steps to use 2847618ce8baSLisandro Dalcin 2848618ce8baSLisandro Dalcin Level: advanced 2849618ce8baSLisandro Dalcin 28501cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetMaxSteps()`, `TSGetMaxTime()`, `TSSetMaxTime()` 2851618ce8baSLisandro Dalcin @*/ 2852d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetMaxSteps(TS ts, PetscInt *maxsteps) 2853d71ae5a4SJacob Faibussowitsch { 2854618ce8baSLisandro Dalcin PetscFunctionBegin; 2855618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 28564f572ea9SToby Isaac PetscAssertPointer(maxsteps, 2); 2857618ce8baSLisandro Dalcin *maxsteps = ts->max_steps; 28583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2859618ce8baSLisandro Dalcin } 2860618ce8baSLisandro Dalcin 2861618ce8baSLisandro Dalcin /*@ 2862618ce8baSLisandro Dalcin TSSetMaxTime - Sets the maximum (or final) time for timestepping. 2863618ce8baSLisandro Dalcin 2864c3339decSBarry Smith Logically Collective 2865618ce8baSLisandro Dalcin 2866618ce8baSLisandro Dalcin Input Parameters: 2867bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 2868618ce8baSLisandro Dalcin - maxtime - final time to step to 2869618ce8baSLisandro Dalcin 2870bcf0153eSBarry Smith Options Database Key: 2871ef85077eSLisandro Dalcin . -ts_max_time <maxtime> - Sets maxtime 2872618ce8baSLisandro Dalcin 2873bcf0153eSBarry Smith Level: intermediate 2874bcf0153eSBarry Smith 2875618ce8baSLisandro Dalcin Notes: 287609cb0f53SBarry Smith Use `PETSC_DETERMINE` to reset the maximum time to the default from when the object's type was set 287709cb0f53SBarry Smith 2878618ce8baSLisandro Dalcin The default maximum time is 5.0 2879618ce8baSLisandro Dalcin 288009cb0f53SBarry Smith Fortran Note: 288109cb0f53SBarry Smith Use `PETSC_DETERMINE_REAL` 288209cb0f53SBarry Smith 28831cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetMaxTime()`, `TSSetMaxSteps()`, `TSSetExactFinalTime()` 2884618ce8baSLisandro Dalcin @*/ 2885d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxTime(TS ts, PetscReal maxtime) 2886d71ae5a4SJacob Faibussowitsch { 2887618ce8baSLisandro Dalcin PetscFunctionBegin; 2888618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2889618ce8baSLisandro Dalcin PetscValidLogicalCollectiveReal(ts, maxtime, 2); 289009cb0f53SBarry Smith if (maxtime == PETSC_DETERMINE) { 289109cb0f53SBarry Smith ts->max_time = ts->default_max_time; 289209cb0f53SBarry Smith } else { 2893618ce8baSLisandro Dalcin ts->max_time = maxtime; 289409cb0f53SBarry Smith } 28953ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2896618ce8baSLisandro Dalcin } 2897618ce8baSLisandro Dalcin 2898618ce8baSLisandro Dalcin /*@ 2899618ce8baSLisandro Dalcin TSGetMaxTime - Gets the maximum (or final) time for timestepping. 2900618ce8baSLisandro Dalcin 2901618ce8baSLisandro Dalcin Not Collective 2902618ce8baSLisandro Dalcin 29032fe279fdSBarry Smith Input Parameter: 2904bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2905618ce8baSLisandro Dalcin 2906618ce8baSLisandro Dalcin Output Parameter: 2907618ce8baSLisandro Dalcin . maxtime - final time to step to 2908618ce8baSLisandro Dalcin 2909618ce8baSLisandro Dalcin Level: advanced 2910618ce8baSLisandro Dalcin 29111cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetMaxTime()`, `TSGetMaxSteps()`, `TSSetMaxSteps()` 2912618ce8baSLisandro Dalcin @*/ 2913d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetMaxTime(TS ts, PetscReal *maxtime) 2914d71ae5a4SJacob Faibussowitsch { 2915618ce8baSLisandro Dalcin PetscFunctionBegin; 2916618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 29174f572ea9SToby Isaac PetscAssertPointer(maxtime, 2); 2918618ce8baSLisandro Dalcin *maxtime = ts->max_time; 29193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2920618ce8baSLisandro Dalcin } 2921618ce8baSLisandro Dalcin 292214d0ab18SJacob Faibussowitsch // PetscClangLinter pragma disable: -fdoc-* 2923618ce8baSLisandro Dalcin /*@ 2924bcf0153eSBarry Smith TSSetInitialTimeStep - Deprecated, use `TSSetTime()` and `TSSetTimeStep()`. 2925edc382c3SSatish Balay 2926edc382c3SSatish Balay Level: deprecated 2927edc382c3SSatish Balay 2928aaa6c58dSLisandro Dalcin @*/ 2929d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetInitialTimeStep(TS ts, PetscReal initial_time, PetscReal time_step) 2930d71ae5a4SJacob Faibussowitsch { 2931aaa6c58dSLisandro Dalcin PetscFunctionBegin; 2932aaa6c58dSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 29339566063dSJacob Faibussowitsch PetscCall(TSSetTime(ts, initial_time)); 29349566063dSJacob Faibussowitsch PetscCall(TSSetTimeStep(ts, time_step)); 29353ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2936aaa6c58dSLisandro Dalcin } 2937aaa6c58dSLisandro Dalcin 293814d0ab18SJacob Faibussowitsch // PetscClangLinter pragma disable: -fdoc-* 2939aaa6c58dSLisandro Dalcin /*@ 2940bcf0153eSBarry Smith TSGetDuration - Deprecated, use `TSGetMaxSteps()` and `TSGetMaxTime()`. 2941edc382c3SSatish Balay 2942edc382c3SSatish Balay Level: deprecated 2943edc382c3SSatish Balay 2944adb62b0dSMatthew Knepley @*/ 2945d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2946d71ae5a4SJacob Faibussowitsch { 2947adb62b0dSMatthew Knepley PetscFunctionBegin; 29480700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2949abc0a331SBarry Smith if (maxsteps) { 29504f572ea9SToby Isaac PetscAssertPointer(maxsteps, 2); 2951adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2952adb62b0dSMatthew Knepley } 2953abc0a331SBarry Smith if (maxtime) { 29544f572ea9SToby Isaac PetscAssertPointer(maxtime, 3); 2955adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2956adb62b0dSMatthew Knepley } 29573ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2958adb62b0dSMatthew Knepley } 2959adb62b0dSMatthew Knepley 296014d0ab18SJacob Faibussowitsch // PetscClangLinter pragma disable: -fdoc-* 2961d763cef2SBarry Smith /*@ 2962bcf0153eSBarry Smith TSSetDuration - Deprecated, use `TSSetMaxSteps()` and `TSSetMaxTime()`. 2963edc382c3SSatish Balay 2964edc382c3SSatish Balay Level: deprecated 2965edc382c3SSatish Balay 2966d763cef2SBarry Smith @*/ 2967d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetDuration(TS ts, PetscInt maxsteps, PetscReal maxtime) 2968d71ae5a4SJacob Faibussowitsch { 2969d763cef2SBarry Smith PetscFunctionBegin; 2970835f2295SStefano Zampini if (maxsteps != PETSC_CURRENT) PetscCall(TSSetMaxSteps(ts, maxsteps)); 297109cb0f53SBarry Smith if (maxtime != (PetscReal)PETSC_CURRENT) PetscCall(TSSetMaxTime(ts, maxtime)); 29723ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2973d763cef2SBarry Smith } 2974d763cef2SBarry Smith 297514d0ab18SJacob Faibussowitsch // PetscClangLinter pragma disable: -fdoc-* 2976d763cef2SBarry Smith /*@ 2977bcf0153eSBarry Smith TSGetTimeStepNumber - Deprecated, use `TSGetStepNumber()`. 2978edc382c3SSatish Balay 2979edc382c3SSatish Balay Level: deprecated 2980edc382c3SSatish Balay 29815c5f5948SLisandro Dalcin @*/ 2982d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeStepNumber(TS ts, PetscInt *steps) 2983d71ae5a4SJacob Faibussowitsch { 29849371c9d4SSatish Balay return TSGetStepNumber(ts, steps); 29859371c9d4SSatish Balay } 29865c5f5948SLisandro Dalcin 298714d0ab18SJacob Faibussowitsch // PetscClangLinter pragma disable: -fdoc-* 298819eac22cSLisandro Dalcin /*@ 2989bcf0153eSBarry Smith TSGetTotalSteps - Deprecated, use `TSGetStepNumber()`. 2990edc382c3SSatish Balay 2991edc382c3SSatish Balay Level: deprecated 2992edc382c3SSatish Balay 29934f4e0956SLisandro Dalcin @*/ 2994d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTotalSteps(TS ts, PetscInt *steps) 2995d71ae5a4SJacob Faibussowitsch { 29969371c9d4SSatish Balay return TSGetStepNumber(ts, steps); 29979371c9d4SSatish Balay } 29984f4e0956SLisandro Dalcin 29994f4e0956SLisandro Dalcin /*@ 3000d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 3001bcf0153eSBarry Smith for use by the `TS` routines. 3002d763cef2SBarry Smith 3003c3339decSBarry Smith Logically Collective 3004d763cef2SBarry Smith 3005d763cef2SBarry Smith Input Parameters: 3006bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 30070910c330SBarry Smith - u - the solution vector 3008d763cef2SBarry Smith 3009d763cef2SBarry Smith Level: beginner 3010d763cef2SBarry Smith 30111cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetSolutionFunction()`, `TSGetSolution()`, `TSCreate()` 3012d763cef2SBarry Smith @*/ 3013d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetSolution(TS ts, Vec u) 3014d71ae5a4SJacob Faibussowitsch { 3015496e6a7aSJed Brown DM dm; 30168737fe31SLisandro Dalcin 3017d763cef2SBarry Smith PetscFunctionBegin; 30180700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 30190910c330SBarry Smith PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 30209566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)u)); 30219566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_sol)); 30220910c330SBarry Smith ts->vec_sol = u; 3023bbd56ea5SKarl Rupp 30249566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 30259566063dSJacob Faibussowitsch PetscCall(DMShellSetGlobalVector(dm, u)); 30263ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3027d763cef2SBarry Smith } 3028d763cef2SBarry Smith 3029ac226902SBarry Smith /*@C 3030000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 30313f2090d5SJed Brown called once at the beginning of each time step. 3032000e7ae3SMatthew Knepley 3033c3339decSBarry Smith Logically Collective 3034000e7ae3SMatthew Knepley 3035000e7ae3SMatthew Knepley Input Parameters: 3036bcf0153eSBarry Smith + ts - The `TS` context obtained from `TSCreate()` 3037000e7ae3SMatthew Knepley - func - The function 3038000e7ae3SMatthew Knepley 303920f4b53cSBarry Smith Calling sequence of `func`: 304014d0ab18SJacob Faibussowitsch . ts - the `TS` context 3041000e7ae3SMatthew Knepley 3042000e7ae3SMatthew Knepley Level: intermediate 3043000e7ae3SMatthew Knepley 30441cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStage()`, `TSSetPostStage()`, `TSSetPostStep()`, `TSStep()`, `TSRestartStep()` 3045000e7ae3SMatthew Knepley @*/ 304614d0ab18SJacob Faibussowitsch PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS ts)) 3047d71ae5a4SJacob Faibussowitsch { 3048000e7ae3SMatthew Knepley PetscFunctionBegin; 30490700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3050ae60f76fSBarry Smith ts->prestep = func; 30513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3052000e7ae3SMatthew Knepley } 3053000e7ae3SMatthew Knepley 305409ee8438SJed Brown /*@ 3055bcf0153eSBarry Smith TSPreStep - Runs the user-defined pre-step function provided with `TSSetPreStep()` 30563f2090d5SJed Brown 3057c3339decSBarry Smith Collective 30583f2090d5SJed Brown 30592fe279fdSBarry Smith Input Parameter: 3060bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 30613f2090d5SJed Brown 30623f2090d5SJed Brown Level: developer 30633f2090d5SJed Brown 3064bcf0153eSBarry Smith Note: 3065bcf0153eSBarry Smith `TSPreStep()` is typically used within time stepping implementations, 3066bcf0153eSBarry Smith so most users would not generally call this routine themselves. 3067bcf0153eSBarry Smith 30681cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStep()`, `TSPreStage()`, `TSPostStage()`, `TSPostStep()` 30693f2090d5SJed Brown @*/ 3070d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPreStep(TS ts) 3071d71ae5a4SJacob Faibussowitsch { 30723f2090d5SJed Brown PetscFunctionBegin; 30730700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3074ae60f76fSBarry Smith if (ts->prestep) { 30755efd42a4SStefano Zampini Vec U; 3076ef8d1ce0SJohann Rudi PetscObjectId idprev; 3077ef8d1ce0SJohann Rudi PetscBool sameObject; 30785efd42a4SStefano Zampini PetscObjectState sprev, spost; 30795efd42a4SStefano Zampini 30809566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts, &U)); 30819566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U, &idprev)); 30829566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &sprev)); 308325e27a38SBarry Smith PetscCallBack("TS callback preset", (*ts->prestep)(ts)); 30849566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts, &U)); 30859566063dSJacob Faibussowitsch PetscCall(PetscObjectCompareId((PetscObject)U, idprev, &sameObject)); 30869566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &spost)); 30879566063dSJacob Faibussowitsch if (!sameObject || sprev != spost) PetscCall(TSRestartStep(ts)); 3088312ce896SJed Brown } 30893ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 30903f2090d5SJed Brown } 30913f2090d5SJed Brown 3092b8123daeSJed Brown /*@C 3093b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 3094b8123daeSJed Brown called once at the beginning of each stage. 3095b8123daeSJed Brown 3096c3339decSBarry Smith Logically Collective 3097b8123daeSJed Brown 3098b8123daeSJed Brown Input Parameters: 3099bcf0153eSBarry Smith + ts - The `TS` context obtained from `TSCreate()` 3100b8123daeSJed Brown - func - The function 3101b8123daeSJed Brown 310220f4b53cSBarry Smith Calling sequence of `func`: 310314d0ab18SJacob Faibussowitsch + ts - the `TS` context 310414d0ab18SJacob Faibussowitsch - stagetime - the stage time 3105b8123daeSJed Brown 3106b8123daeSJed Brown Level: intermediate 3107b8123daeSJed Brown 3108b8123daeSJed Brown Note: 3109b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 3110bcf0153eSBarry Smith The time step number being computed can be queried using `TSGetStepNumber()` and the total size of the step being 3111bcf0153eSBarry Smith attempted can be obtained using `TSGetTimeStep()`. The time at the start of the step is available via `TSGetTime()`. 3112b8123daeSJed Brown 31131cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPostStage()`, `TSSetPreStep()`, `TSSetPostStep()`, `TSGetApplicationContext()` 3114b8123daeSJed Brown @*/ 311514d0ab18SJacob Faibussowitsch PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS ts, PetscReal stagetime)) 3116d71ae5a4SJacob Faibussowitsch { 3117b8123daeSJed Brown PetscFunctionBegin; 3118b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3119ae60f76fSBarry Smith ts->prestage = func; 31203ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3121b8123daeSJed Brown } 3122b8123daeSJed Brown 31239be3e283SDebojyoti Ghosh /*@C 31240d56bcf9SIlya Fursov TSSetPostStage - Sets the general-purpose function 31259be3e283SDebojyoti Ghosh called once at the end of each stage. 31269be3e283SDebojyoti Ghosh 3127c3339decSBarry Smith Logically Collective 31289be3e283SDebojyoti Ghosh 31299be3e283SDebojyoti Ghosh Input Parameters: 3130bcf0153eSBarry Smith + ts - The `TS` context obtained from `TSCreate()` 31319be3e283SDebojyoti Ghosh - func - The function 31329be3e283SDebojyoti Ghosh 313320f4b53cSBarry Smith Calling sequence of `func`: 313414d0ab18SJacob Faibussowitsch + ts - the `TS` context 313514d0ab18SJacob Faibussowitsch . stagetime - the stage time 313614d0ab18SJacob Faibussowitsch . stageindex - the stage index 313714d0ab18SJacob Faibussowitsch - Y - Array of vectors (of size = total number of stages) with the stage solutions 31389be3e283SDebojyoti Ghosh 31399be3e283SDebojyoti Ghosh Level: intermediate 31409be3e283SDebojyoti Ghosh 31419be3e283SDebojyoti Ghosh Note: 31429be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 3143bcf0153eSBarry Smith The time step number being computed can be queried using `TSGetStepNumber()` and the total size of the step being 3144bcf0153eSBarry Smith attempted can be obtained using `TSGetTimeStep()`. The time at the start of the step is available via `TSGetTime()`. 31459be3e283SDebojyoti Ghosh 31461cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStage()`, `TSSetPreStep()`, `TSSetPostStep()`, `TSGetApplicationContext()` 31479be3e283SDebojyoti Ghosh @*/ 314814d0ab18SJacob Faibussowitsch PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)) 3149d71ae5a4SJacob Faibussowitsch { 31509be3e283SDebojyoti Ghosh PetscFunctionBegin; 31519be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 31529be3e283SDebojyoti Ghosh ts->poststage = func; 31533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 31549be3e283SDebojyoti Ghosh } 31559be3e283SDebojyoti Ghosh 3156c688d042SShri Abhyankar /*@C 3157c688d042SShri Abhyankar TSSetPostEvaluate - Sets the general-purpose function 31580d56bcf9SIlya Fursov called at the end of each step evaluation. 3159c688d042SShri Abhyankar 3160c3339decSBarry Smith Logically Collective 3161c688d042SShri Abhyankar 3162c688d042SShri Abhyankar Input Parameters: 3163bcf0153eSBarry Smith + ts - The `TS` context obtained from `TSCreate()` 3164c688d042SShri Abhyankar - func - The function 3165c688d042SShri Abhyankar 316620f4b53cSBarry Smith Calling sequence of `func`: 316714d0ab18SJacob Faibussowitsch . ts - the `TS` context 3168c688d042SShri Abhyankar 3169c688d042SShri Abhyankar Level: intermediate 3170c688d042SShri Abhyankar 3171c688d042SShri Abhyankar Note: 31720d56bcf9SIlya Fursov The function set by `TSSetPostEvaluate()` is called after the solution is evaluated, or after the step rollback. 31730d56bcf9SIlya Fursov Inside the `func` callback, the solution vector can be obtained with `TSGetSolution()`, and modified, if need be. 31740d56bcf9SIlya Fursov The time step can be obtained with `TSGetTimeStep()`, and the time at the start of the step - via `TSGetTime()`. 31750d56bcf9SIlya Fursov The potential changes to the solution vector introduced by event handling (`postevent()`) are not relevant for `TSSetPostEvaluate()`, 31760d56bcf9SIlya Fursov but are relevant for `TSSetPostStep()`, according to the function call scheme in `TSSolve()`, as shown below 31770d56bcf9SIlya Fursov .vb 31780d56bcf9SIlya Fursov ... 31790d56bcf9SIlya Fursov Step() 31800d56bcf9SIlya Fursov PostEvaluate() 31810d56bcf9SIlya Fursov EventHandling() 31820d56bcf9SIlya Fursov step_rollback ? PostEvaluate() : PostStep() 31830d56bcf9SIlya Fursov ... 31840d56bcf9SIlya Fursov .ve 31850d56bcf9SIlya Fursov where EventHandling() may result in one of the following three outcomes 31860d56bcf9SIlya Fursov .vb 31870d56bcf9SIlya Fursov (1) | successful step | solution intact 31880d56bcf9SIlya Fursov (2) | successful step | solution modified by `postevent()` 31890d56bcf9SIlya Fursov (3) | step_rollback | solution rolled back 31900d56bcf9SIlya Fursov .ve 3191c688d042SShri Abhyankar 31921cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStage()`, `TSSetPreStep()`, `TSSetPostStep()`, `TSGetApplicationContext()` 3193c688d042SShri Abhyankar @*/ 319414d0ab18SJacob Faibussowitsch PetscErrorCode TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS ts)) 3195d71ae5a4SJacob Faibussowitsch { 3196c688d042SShri Abhyankar PetscFunctionBegin; 3197c688d042SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3198c688d042SShri Abhyankar ts->postevaluate = func; 31993ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3200c688d042SShri Abhyankar } 3201c688d042SShri Abhyankar 3202b8123daeSJed Brown /*@ 3203bcf0153eSBarry Smith TSPreStage - Runs the user-defined pre-stage function set using `TSSetPreStage()` 3204b8123daeSJed Brown 3205c3339decSBarry Smith Collective 3206b8123daeSJed Brown 3207b8123daeSJed Brown Input Parameters: 320814d0ab18SJacob Faibussowitsch + ts - The `TS` context obtained from `TSCreate()` 320914d0ab18SJacob Faibussowitsch - stagetime - The absolute time of the current stage 3210b8123daeSJed Brown 3211b8123daeSJed Brown Level: developer 3212b8123daeSJed Brown 3213bcf0153eSBarry Smith Note: 3214bcf0153eSBarry Smith `TSPreStage()` is typically used within time stepping implementations, 3215bcf0153eSBarry Smith most users would not generally call this routine themselves. 3216bcf0153eSBarry Smith 32171cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSPostStage()`, `TSSetPreStep()`, `TSPreStep()`, `TSPostStep()` 3218b8123daeSJed Brown @*/ 3219d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 3220d71ae5a4SJacob Faibussowitsch { 3221b8123daeSJed Brown PetscFunctionBegin; 3222b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 32231e66621cSBarry Smith if (ts->prestage) PetscCallBack("TS callback prestage", (*ts->prestage)(ts, stagetime)); 32243ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3225b8123daeSJed Brown } 3226b8123daeSJed Brown 32279be3e283SDebojyoti Ghosh /*@ 3228bcf0153eSBarry Smith TSPostStage - Runs the user-defined post-stage function set using `TSSetPostStage()` 32299be3e283SDebojyoti Ghosh 3230c3339decSBarry Smith Collective 32319be3e283SDebojyoti Ghosh 32329be3e283SDebojyoti Ghosh Input Parameters: 323314d0ab18SJacob Faibussowitsch + ts - The `TS` context obtained from `TSCreate()` 323414d0ab18SJacob Faibussowitsch . stagetime - The absolute time of the current stage 323514d0ab18SJacob Faibussowitsch . stageindex - Stage number 323614d0ab18SJacob Faibussowitsch - Y - Array of vectors (of size = total number of stages) with the stage solutions 32379be3e283SDebojyoti Ghosh 32389be3e283SDebojyoti Ghosh Level: developer 32399be3e283SDebojyoti Ghosh 3240bcf0153eSBarry Smith Note: 3241bcf0153eSBarry Smith `TSPostStage()` is typically used within time stepping implementations, 3242bcf0153eSBarry Smith most users would not generally call this routine themselves. 3243bcf0153eSBarry Smith 32441cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSPreStage()`, `TSSetPreStep()`, `TSPreStep()`, `TSPostStep()` 32459be3e283SDebojyoti Ghosh @*/ 3246d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 3247d71ae5a4SJacob Faibussowitsch { 32489be3e283SDebojyoti Ghosh PetscFunctionBegin; 32499be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 32501e66621cSBarry Smith if (ts->poststage) PetscCallBack("TS callback poststage", (*ts->poststage)(ts, stagetime, stageindex, Y)); 32513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 32529be3e283SDebojyoti Ghosh } 32539be3e283SDebojyoti Ghosh 3254c688d042SShri Abhyankar /*@ 3255bcf0153eSBarry Smith TSPostEvaluate - Runs the user-defined post-evaluate function set using `TSSetPostEvaluate()` 3256c688d042SShri Abhyankar 3257c3339decSBarry Smith Collective 3258c688d042SShri Abhyankar 32592fe279fdSBarry Smith Input Parameter: 3260bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 3261c688d042SShri Abhyankar 3262c688d042SShri Abhyankar Level: developer 3263c688d042SShri Abhyankar 3264bcf0153eSBarry Smith Note: 3265bcf0153eSBarry Smith `TSPostEvaluate()` is typically used within time stepping implementations, 3266bcf0153eSBarry Smith most users would not generally call this routine themselves. 3267bcf0153eSBarry Smith 32681cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPostEvaluate()`, `TSSetPreStep()`, `TSPreStep()`, `TSPostStep()` 3269c688d042SShri Abhyankar @*/ 3270d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPostEvaluate(TS ts) 3271d71ae5a4SJacob Faibussowitsch { 3272c688d042SShri Abhyankar PetscFunctionBegin; 3273c688d042SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3274c688d042SShri Abhyankar if (ts->postevaluate) { 3275dcb233daSLisandro Dalcin Vec U; 3276dcb233daSLisandro Dalcin PetscObjectState sprev, spost; 3277dcb233daSLisandro Dalcin 32789566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts, &U)); 32799566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &sprev)); 328025e27a38SBarry Smith PetscCallBack("TS callback postevaluate", (*ts->postevaluate)(ts)); 32819566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &spost)); 32829566063dSJacob Faibussowitsch if (sprev != spost) PetscCall(TSRestartStep(ts)); 3283c688d042SShri Abhyankar } 32843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3285c688d042SShri Abhyankar } 3286c688d042SShri Abhyankar 3287ac226902SBarry Smith /*@C 3288000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 32890d56bcf9SIlya Fursov called once at the end of each successful time step. 3290000e7ae3SMatthew Knepley 3291c3339decSBarry Smith Logically Collective 3292000e7ae3SMatthew Knepley 3293000e7ae3SMatthew Knepley Input Parameters: 3294bcf0153eSBarry Smith + ts - The `TS` context obtained from `TSCreate()` 3295000e7ae3SMatthew Knepley - func - The function 3296000e7ae3SMatthew Knepley 329720f4b53cSBarry Smith Calling sequence of `func`: 329814d0ab18SJacob Faibussowitsch . ts - the `TS` context 3299000e7ae3SMatthew Knepley 3300000e7ae3SMatthew Knepley Level: intermediate 3301000e7ae3SMatthew Knepley 3302bcf0153eSBarry Smith Note: 33030d56bcf9SIlya Fursov The function set by `TSSetPostStep()` is called after each successful step. If the event handler locates an event at the 33040d56bcf9SIlya Fursov given step, and `postevent()` modifies the solution vector, the solution vector obtained by `TSGetSolution()` inside `func` will 33050d56bcf9SIlya Fursov contain the changes. To get the solution without these changes, use `TSSetPostEvaluate()` to set the appropriate callback. 33060d56bcf9SIlya Fursov The scheme of the relevant function calls in `TSSolve()` is shown below 33070d56bcf9SIlya Fursov .vb 33080d56bcf9SIlya Fursov ... 33090d56bcf9SIlya Fursov Step() 33100d56bcf9SIlya Fursov PostEvaluate() 33110d56bcf9SIlya Fursov EventHandling() 33120d56bcf9SIlya Fursov step_rollback ? PostEvaluate() : PostStep() 33130d56bcf9SIlya Fursov ... 33140d56bcf9SIlya Fursov .ve 33150d56bcf9SIlya Fursov where EventHandling() may result in one of the following three outcomes 33160d56bcf9SIlya Fursov .vb 33170d56bcf9SIlya Fursov (1) | successful step | solution intact 33180d56bcf9SIlya Fursov (2) | successful step | solution modified by `postevent()` 33190d56bcf9SIlya Fursov (3) | step_rollback | solution rolled back 33200d56bcf9SIlya Fursov .ve 3321bcf0153eSBarry Smith 33221cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostEvaluate()`, `TSGetTimeStep()`, `TSGetStepNumber()`, `TSGetTime()`, `TSRestartStep()` 3323000e7ae3SMatthew Knepley @*/ 332414d0ab18SJacob Faibussowitsch PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS ts)) 3325d71ae5a4SJacob Faibussowitsch { 3326000e7ae3SMatthew Knepley PetscFunctionBegin; 33270700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3328ae60f76fSBarry Smith ts->poststep = func; 33293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3330000e7ae3SMatthew Knepley } 3331000e7ae3SMatthew Knepley 333209ee8438SJed Brown /*@ 3333195e9b02SBarry Smith TSPostStep - Runs the user-defined post-step function that was set with `TSSetPostStep()` 33343f2090d5SJed Brown 3335c3339decSBarry Smith Collective 33363f2090d5SJed Brown 33372fe279fdSBarry Smith Input Parameter: 3338bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 33393f2090d5SJed Brown 3340bcf0153eSBarry Smith Note: 3341bcf0153eSBarry Smith `TSPostStep()` is typically used within time stepping implementations, 33423f2090d5SJed Brown so most users would not generally call this routine themselves. 33433f2090d5SJed Brown 33443f2090d5SJed Brown Level: developer 33453f2090d5SJed Brown 3346*bfe80ac4SPierre Jolivet .seealso: [](ch_ts), `TS`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostEvaluate()`, `TSGetTimeStep()`, `TSGetStepNumber()`, `TSGetTime()`, `TSSetPostStep()` 33473f2090d5SJed Brown @*/ 3348d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPostStep(TS ts) 3349d71ae5a4SJacob Faibussowitsch { 33503f2090d5SJed Brown PetscFunctionBegin; 33510700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3352ae60f76fSBarry Smith if (ts->poststep) { 33535efd42a4SStefano Zampini Vec U; 3354ef8d1ce0SJohann Rudi PetscObjectId idprev; 3355ef8d1ce0SJohann Rudi PetscBool sameObject; 33565efd42a4SStefano Zampini PetscObjectState sprev, spost; 33575efd42a4SStefano Zampini 33589566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts, &U)); 33599566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U, &idprev)); 33609566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &sprev)); 336125e27a38SBarry Smith PetscCallBack("TS callback poststep", (*ts->poststep)(ts)); 33629566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts, &U)); 33639566063dSJacob Faibussowitsch PetscCall(PetscObjectCompareId((PetscObject)U, idprev, &sameObject)); 33649566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &spost)); 33659566063dSJacob Faibussowitsch if (!sameObject || sprev != spost) PetscCall(TSRestartStep(ts)); 336672ac3e02SJed Brown } 33673ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 33683f2090d5SJed Brown } 33693f2090d5SJed Brown 3370cd652676SJed Brown /*@ 3371cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3372cd652676SJed Brown 3373c3339decSBarry Smith Collective 3374cd652676SJed Brown 33754165533cSJose E. Roman Input Parameters: 3376cd652676SJed Brown + ts - time stepping context 3377cd652676SJed Brown - t - time to interpolate to 3378cd652676SJed Brown 33794165533cSJose E. Roman Output Parameter: 33800910c330SBarry Smith . U - state at given time 3381cd652676SJed Brown 3382cd652676SJed Brown Level: intermediate 3383cd652676SJed Brown 3384b43aa488SJacob Faibussowitsch Developer Notes: 3385bcf0153eSBarry Smith `TSInterpolate()` and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3386cd652676SJed Brown 33871cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetExactFinalTime()`, `TSSolve()` 3388cd652676SJed Brown @*/ 3389d71ae5a4SJacob Faibussowitsch PetscErrorCode TSInterpolate(TS ts, PetscReal t, Vec U) 3390d71ae5a4SJacob Faibussowitsch { 3391cd652676SJed Brown PetscFunctionBegin; 3392cd652676SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3393b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 339463a3b9bcSJacob 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); 3395dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, interpolate, t, U); 33963ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3397cd652676SJed Brown } 3398cd652676SJed Brown 3399d763cef2SBarry Smith /*@ 34006d9e5789SSean Farley TSStep - Steps one time step 3401d763cef2SBarry Smith 3402c3339decSBarry Smith Collective 3403d763cef2SBarry Smith 3404d763cef2SBarry Smith Input Parameter: 3405bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 3406d763cef2SBarry Smith 340727829d71SBarry Smith Level: developer 3408d763cef2SBarry Smith 3409b8123daeSJed Brown Notes: 3410bcf0153eSBarry Smith The public interface for the ODE/DAE solvers is `TSSolve()`, you should almost for sure be using that routine and not this routine. 341127829d71SBarry Smith 3412bcf0153eSBarry Smith The hook set using `TSSetPreStep()` is called before each attempt to take the step. In general, the time step size may 3413b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3414b8123daeSJed Brown 3415bcf0153eSBarry Smith This may over-step the final time provided in `TSSetMaxTime()` depending on the time-step used. `TSSolve()` interpolates to exactly the 3416bcf0153eSBarry Smith time provided in `TSSetMaxTime()`. One can use `TSInterpolate()` to determine an interpolated solution within the final timestep. 341725cb2221SBarry Smith 34181cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSSetUp()`, `TSDestroy()`, `TSSolve()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostStage()`, `TSInterpolate()` 3419d763cef2SBarry Smith @*/ 3420d71ae5a4SJacob Faibussowitsch PetscErrorCode TSStep(TS ts) 3421d71ae5a4SJacob Faibussowitsch { 3422fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3423be5899b3SLisandro Dalcin PetscReal ptime; 3424d763cef2SBarry Smith 3425d763cef2SBarry Smith PetscFunctionBegin; 34260700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3427d0609cedSBarry Smith PetscCall(PetscCitationsRegister("@article{tspaper,\n" 3428fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3429f1d62c27SHong Zhang " author = {Abhyankar, Shrirang and Brown, Jed and Constantinescu, Emil and Ghosh, Debojyoti and Smith, Barry F. and Zhang, Hong},\n" 3430f1d62c27SHong Zhang " journal = {arXiv e-preprints},\n" 3431f1d62c27SHong Zhang " eprint = {1806.01437},\n" 3432f1d62c27SHong Zhang " archivePrefix = {arXiv},\n" 34339371c9d4SSatish Balay " year = {2018}\n}\n", 34349371c9d4SSatish Balay &cite)); 34359566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts)); 34369566063dSJacob Faibussowitsch PetscCall(TSTrajectorySetUp(ts->trajectory, ts)); 3437136cf249SJames Wright if (ts->eval_times) 3438136cf249SJames Wright ts->eval_times->worktol = 0; /* In each step of TSSolve() 'eval_times->worktol' will be meaningfully defined (later) only once: 3439ca4445c7SIlya Fursov in TSAdaptChoose() or TSEvent_dt_cap(), and then reused till the end of the step */ 3440d405a339SMatthew Knepley 34411690c2aeSBarry Smith PetscCheck(ts->max_time < PETSC_MAX_REAL || ts->max_steps != PETSC_INT_MAX, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "You must call TSSetMaxTime() or TSSetMaxSteps(), or use -ts_max_time <time> or -ts_max_steps <steps>"); 34423c633725SBarry 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()"); 34433c633725SBarry 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"); 3444a6772fa2SLisandro Dalcin 3445c61711c8SStefano Zampini if (!ts->vec_sol0) PetscCall(VecDuplicate(ts->vec_sol, &ts->vec_sol0)); 3446c61711c8SStefano Zampini PetscCall(VecCopy(ts->vec_sol, ts->vec_sol0)); 3447c61711c8SStefano Zampini ts->time_step0 = ts->time_step; 3448c61711c8SStefano Zampini 3449be5899b3SLisandro Dalcin if (!ts->steps) ts->ptime_prev = ts->ptime; 34509371c9d4SSatish Balay ptime = ts->ptime; 3451c61711c8SStefano Zampini 34529371c9d4SSatish Balay ts->ptime_prev_rollback = ts->ptime_prev; 34532808aa04SLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3454fc8dbba5SLisandro Dalcin 34559566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_Step, ts, 0, 0, 0)); 3456dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, step); 34579566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_Step, ts, 0, 0, 0)); 3458fc8dbba5SLisandro Dalcin 3459fc8dbba5SLisandro Dalcin if (ts->reason >= 0) { 3460be5899b3SLisandro Dalcin ts->ptime_prev = ptime; 34612808aa04SLisandro Dalcin ts->steps++; 3462be5899b3SLisandro Dalcin ts->steprollback = PETSC_FALSE; 346328d5b5d6SLisandro Dalcin ts->steprestart = PETSC_FALSE; 3464c6bf8827SStefano Zampini ts->stepresize = PETSC_FALSE; 3465d2daff3dSHong Zhang } 3466fc8dbba5SLisandro Dalcin 34675c9bbc89SJed Brown if (ts->reason < 0 && ts->errorifstepfailed) { 34685c9bbc89SJed Brown PetscCall(TSMonitorCancel(ts)); 346909cb0f53SBarry 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]); 34705c9bbc89SJed Brown SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_NOT_CONVERGED, "TSStep has failed due to %s", TSConvergedReasons[ts->reason]); 34715c9bbc89SJed Brown } 34723ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 347308c7845fSBarry Smith } 347408c7845fSBarry Smith 347508c7845fSBarry Smith /*@ 34767cbde773SLisandro Dalcin TSEvaluateWLTE - Evaluate the weighted local truncation error norm 34777cbde773SLisandro Dalcin at the end of a time step with a given order of accuracy. 34787cbde773SLisandro Dalcin 3479c3339decSBarry Smith Collective 34807cbde773SLisandro Dalcin 34814165533cSJose E. Roman Input Parameters: 34827cbde773SLisandro Dalcin + ts - time stepping context 3483bcf0153eSBarry Smith - wnormtype - norm type, either `NORM_2` or `NORM_INFINITY` 34847cbde773SLisandro Dalcin 348597bb3fdcSJose E. Roman Input/Output Parameter: 3486bcf0153eSBarry Smith . order - optional, desired order for the error evaluation or `PETSC_DECIDE`; 348797bb3fdcSJose E. Roman on output, the actual order of the error evaluation 348897bb3fdcSJose E. Roman 348997bb3fdcSJose E. Roman Output Parameter: 349097bb3fdcSJose E. Roman . wlte - the weighted local truncation error norm 34917cbde773SLisandro Dalcin 34927cbde773SLisandro Dalcin Level: advanced 34937cbde773SLisandro Dalcin 3494bcf0153eSBarry Smith Note: 34957cbde773SLisandro Dalcin If the timestepper cannot evaluate the error in a particular step 34967cbde773SLisandro Dalcin (eg. in the first step or restart steps after event handling), 34977cbde773SLisandro Dalcin this routine returns wlte=-1.0 . 34987cbde773SLisandro Dalcin 34991cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSStep()`, `TSAdapt`, `TSErrorWeightedNorm()` 35007cbde773SLisandro Dalcin @*/ 3501d71ae5a4SJacob Faibussowitsch PetscErrorCode TSEvaluateWLTE(TS ts, NormType wnormtype, PetscInt *order, PetscReal *wlte) 3502d71ae5a4SJacob Faibussowitsch { 35037cbde773SLisandro Dalcin PetscFunctionBegin; 35047cbde773SLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 35057cbde773SLisandro Dalcin PetscValidType(ts, 1); 3506064a246eSJacob Faibussowitsch PetscValidLogicalCollectiveEnum(ts, wnormtype, 2); 35074f572ea9SToby Isaac if (order) PetscAssertPointer(order, 3); 35087cbde773SLisandro Dalcin if (order) PetscValidLogicalCollectiveInt(ts, *order, 3); 35094f572ea9SToby Isaac PetscAssertPointer(wlte, 4); 35103c633725SBarry Smith PetscCheck(wnormtype == NORM_2 || wnormtype == NORM_INFINITY, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "No support for norm type %s", NormTypes[wnormtype]); 3511dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, evaluatewlte, wnormtype, order, wlte); 35123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 35137cbde773SLisandro Dalcin } 35147cbde773SLisandro Dalcin 351505175c85SJed Brown /*@ 351605175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 351705175c85SJed Brown 3518c3339decSBarry Smith Collective 351905175c85SJed Brown 35204165533cSJose E. Roman Input Parameters: 35211c3436cfSJed Brown + ts - time stepping context 35221c3436cfSJed Brown . order - desired order of accuracy 3523195e9b02SBarry Smith - done - whether the step was evaluated at this order (pass `NULL` to generate an error if not available) 352405175c85SJed Brown 35254165533cSJose E. Roman Output Parameter: 35260910c330SBarry Smith . U - state at the end of the current step 352705175c85SJed Brown 352805175c85SJed Brown Level: advanced 352905175c85SJed Brown 3530108c343cSJed Brown Notes: 3531108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3532108c343cSJed Brown 3533bcf0153eSBarry 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. 3534bcf0153eSBarry Smith 35351cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSStep()`, `TSAdapt` 353605175c85SJed Brown @*/ 3537d71ae5a4SJacob Faibussowitsch PetscErrorCode TSEvaluateStep(TS ts, PetscInt order, Vec U, PetscBool *done) 3538d71ae5a4SJacob Faibussowitsch { 353905175c85SJed Brown PetscFunctionBegin; 354005175c85SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 354105175c85SJed Brown PetscValidType(ts, 1); 35420910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 3543dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, evaluatestep, order, U, done); 35443ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 354505175c85SJed Brown } 354605175c85SJed Brown 3547aad739acSMatthew G. Knepley /*@C 35482e61be88SMatthew G. Knepley TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping. 3549aad739acSMatthew G. Knepley 3550aad739acSMatthew G. Knepley Not collective 3551aad739acSMatthew G. Knepley 35524165533cSJose E. Roman Input Parameter: 3553aad739acSMatthew G. Knepley . ts - time stepping context 3554aad739acSMatthew G. Knepley 35554165533cSJose E. Roman Output Parameter: 3556b43aa488SJacob Faibussowitsch . initCondition - The function which computes an initial condition 3557aad739acSMatthew G. Knepley 355820f4b53cSBarry Smith Calling sequence of `initCondition`: 355920f4b53cSBarry Smith + ts - The timestepping context 356020f4b53cSBarry Smith - u - The input vector in which the initial condition is stored 3561bcf0153eSBarry Smith 3562aad739acSMatthew G. Knepley Level: advanced 3563aad739acSMatthew G. Knepley 35641cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetComputeInitialCondition()`, `TSComputeInitialCondition()` 3565aad739acSMatthew G. Knepley @*/ 356614d0ab18SJacob Faibussowitsch PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS ts, Vec u)) 3567d71ae5a4SJacob Faibussowitsch { 3568aad739acSMatthew G. Knepley PetscFunctionBegin; 3569aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 35704f572ea9SToby Isaac PetscAssertPointer(initCondition, 2); 35712e61be88SMatthew G. Knepley *initCondition = ts->ops->initcondition; 35723ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3573aad739acSMatthew G. Knepley } 3574aad739acSMatthew G. Knepley 3575aad739acSMatthew G. Knepley /*@C 35762e61be88SMatthew G. Knepley TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping. 3577aad739acSMatthew G. Knepley 3578c3339decSBarry Smith Logically collective 3579aad739acSMatthew G. Knepley 35804165533cSJose E. Roman Input Parameters: 3581aad739acSMatthew G. Knepley + ts - time stepping context 35822e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition 3583aad739acSMatthew G. Knepley 358420f4b53cSBarry Smith Calling sequence of `initCondition`: 3585a96d6ef6SBarry Smith + ts - The timestepping context 358614d0ab18SJacob Faibussowitsch - e - The input vector in which the initial condition is to be stored 3587aad739acSMatthew G. Knepley 3588bcf0153eSBarry Smith Level: advanced 3589bcf0153eSBarry Smith 35901cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetComputeInitialCondition()`, `TSComputeInitialCondition()` 3591aad739acSMatthew G. Knepley @*/ 359214d0ab18SJacob Faibussowitsch PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS ts, Vec e)) 3593d71ae5a4SJacob Faibussowitsch { 3594aad739acSMatthew G. Knepley PetscFunctionBegin; 3595aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 35962e61be88SMatthew G. Knepley PetscValidFunction(initCondition, 2); 35972e61be88SMatthew G. Knepley ts->ops->initcondition = initCondition; 35983ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3599aad739acSMatthew G. Knepley } 3600aad739acSMatthew G. Knepley 3601aad739acSMatthew G. Knepley /*@ 3602bcf0153eSBarry Smith TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set with `TSSetComputeInitialCondition()` 3603aad739acSMatthew G. Knepley 3604c3339decSBarry Smith Collective 3605aad739acSMatthew G. Knepley 36064165533cSJose E. Roman Input Parameters: 3607aad739acSMatthew G. Knepley + ts - time stepping context 3608bcf0153eSBarry Smith - u - The `Vec` to store the condition in which will be used in `TSSolve()` 3609aad739acSMatthew G. Knepley 3610aad739acSMatthew G. Knepley Level: advanced 3611aad739acSMatthew G. Knepley 36121cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetComputeInitialCondition()`, `TSSetComputeInitialCondition()`, `TSSolve()` 3613aad739acSMatthew G. Knepley @*/ 3614d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeInitialCondition(TS ts, Vec u) 3615d71ae5a4SJacob Faibussowitsch { 3616aad739acSMatthew G. Knepley PetscFunctionBegin; 3617aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3618aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3619dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, initcondition, u); 36203ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3621aad739acSMatthew G. Knepley } 3622aad739acSMatthew G. Knepley 3623aad739acSMatthew G. Knepley /*@C 3624aad739acSMatthew G. Knepley TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping. 3625aad739acSMatthew G. Knepley 3626aad739acSMatthew G. Knepley Not collective 3627aad739acSMatthew G. Knepley 36284165533cSJose E. Roman Input Parameter: 3629aad739acSMatthew G. Knepley . ts - time stepping context 3630aad739acSMatthew G. Knepley 36314165533cSJose E. Roman Output Parameter: 3632aad739acSMatthew G. Knepley . exactError - The function which computes the solution error 3633aad739acSMatthew G. Knepley 363420f4b53cSBarry Smith Calling sequence of `exactError`: 3635a96d6ef6SBarry Smith + ts - The timestepping context 3636a96d6ef6SBarry Smith . u - The approximate solution vector 363720f4b53cSBarry Smith - e - The vector in which the error is stored 3638aad739acSMatthew G. Knepley 3639bcf0153eSBarry Smith Level: advanced 3640bcf0153eSBarry Smith 364142747ad1SJacob Faibussowitsch .seealso: [](ch_ts), `TS`, `TSComputeExactError()` 3642aad739acSMatthew G. Knepley @*/ 364314d0ab18SJacob Faibussowitsch PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS ts, Vec u, Vec e)) 3644d71ae5a4SJacob Faibussowitsch { 3645aad739acSMatthew G. Knepley PetscFunctionBegin; 3646aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 36474f572ea9SToby Isaac PetscAssertPointer(exactError, 2); 3648aad739acSMatthew G. Knepley *exactError = ts->ops->exacterror; 36493ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3650aad739acSMatthew G. Knepley } 3651aad739acSMatthew G. Knepley 3652aad739acSMatthew G. Knepley /*@C 3653aad739acSMatthew G. Knepley TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping. 3654aad739acSMatthew G. Knepley 3655c3339decSBarry Smith Logically collective 3656aad739acSMatthew G. Knepley 36574165533cSJose E. Roman Input Parameters: 3658aad739acSMatthew G. Knepley + ts - time stepping context 3659aad739acSMatthew G. Knepley - exactError - The function which computes the solution error 3660aad739acSMatthew G. Knepley 366120f4b53cSBarry Smith Calling sequence of `exactError`: 3662a96d6ef6SBarry Smith + ts - The timestepping context 3663a96d6ef6SBarry Smith . u - The approximate solution vector 366420f4b53cSBarry Smith - e - The vector in which the error is stored 3665aad739acSMatthew G. Knepley 3666bcf0153eSBarry Smith Level: advanced 3667bcf0153eSBarry Smith 36681cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetComputeExactError()`, `TSComputeExactError()` 3669aad739acSMatthew G. Knepley @*/ 367014d0ab18SJacob Faibussowitsch PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS ts, Vec u, Vec e)) 3671d71ae5a4SJacob Faibussowitsch { 3672aad739acSMatthew G. Knepley PetscFunctionBegin; 3673aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3674f907fdbfSMatthew G. Knepley PetscValidFunction(exactError, 2); 3675aad739acSMatthew G. Knepley ts->ops->exacterror = exactError; 36763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3677aad739acSMatthew G. Knepley } 3678aad739acSMatthew G. Knepley 3679aad739acSMatthew G. Knepley /*@ 3680bcf0153eSBarry Smith TSComputeExactError - Compute the solution error for the timestepping using the function previously set with `TSSetComputeExactError()` 3681aad739acSMatthew G. Knepley 3682c3339decSBarry Smith Collective 3683aad739acSMatthew G. Knepley 36844165533cSJose E. Roman Input Parameters: 3685aad739acSMatthew G. Knepley + ts - time stepping context 3686aad739acSMatthew G. Knepley . u - The approximate solution 3687bcf0153eSBarry Smith - e - The `Vec` used to store the error 3688aad739acSMatthew G. Knepley 3689aad739acSMatthew G. Knepley Level: advanced 3690aad739acSMatthew G. Knepley 36911cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetComputeInitialCondition()`, `TSSetComputeInitialCondition()`, `TSSolve()` 3692aad739acSMatthew G. Knepley @*/ 3693d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e) 3694d71ae5a4SJacob Faibussowitsch { 3695aad739acSMatthew G. Knepley PetscFunctionBegin; 3696aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3697aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3698aad739acSMatthew G. Knepley PetscValidHeaderSpecific(e, VEC_CLASSID, 3); 3699dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, exacterror, u, e); 37003ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3701aad739acSMatthew G. Knepley } 3702aad739acSMatthew G. Knepley 37036bd3a4fdSStefano Zampini /*@C 37046bd3a4fdSStefano Zampini TSSetResize - Sets the resize callbacks. 37056bd3a4fdSStefano Zampini 37066bd3a4fdSStefano Zampini Logically Collective 37076bd3a4fdSStefano Zampini 37086bd3a4fdSStefano Zampini Input Parameters: 37096bd3a4fdSStefano Zampini + ts - The `TS` context obtained from `TSCreate()` 3710ecc87898SStefano Zampini . rollback - Whether a resize will restart the step 37116bd3a4fdSStefano Zampini . setup - The setup function 371214d0ab18SJacob Faibussowitsch . transfer - The transfer function 371314d0ab18SJacob Faibussowitsch - ctx - [optional] The user-defined context 37146bd3a4fdSStefano Zampini 37156bd3a4fdSStefano Zampini Calling sequence of `setup`: 37168847d985SBarry Smith + ts - the `TS` context 37176bd3a4fdSStefano Zampini . step - the current step 37186bd3a4fdSStefano Zampini . time - the current time 37196bd3a4fdSStefano Zampini . state - the current vector of state 37206bd3a4fdSStefano Zampini . resize - (output parameter) `PETSC_TRUE` if need resizing, `PETSC_FALSE` otherwise 37216bd3a4fdSStefano Zampini - ctx - user defined context 37226bd3a4fdSStefano Zampini 37236bd3a4fdSStefano Zampini Calling sequence of `transfer`: 37248847d985SBarry Smith + ts - the `TS` context 37256bd3a4fdSStefano Zampini . nv - the number of vectors to be transferred 37266bd3a4fdSStefano Zampini . vecsin - array of vectors to be transferred 37276bd3a4fdSStefano Zampini . vecsout - array of transferred vectors 37286bd3a4fdSStefano Zampini - ctx - user defined context 37296bd3a4fdSStefano Zampini 37306bd3a4fdSStefano Zampini Notes: 3731ecc87898SStefano Zampini The `setup` function is called inside `TSSolve()` after `TSEventHandler()` or after `TSPostStep()` 3732ecc87898SStefano Zampini depending on the `rollback` value: if `rollback` is true, then these callbacks behave as error indicators 3733ecc87898SStefano Zampini and will flag the need to remesh and restart the current step. Otherwise, they will simply flag the solver 3734ecc87898SStefano Zampini that the size of the discrete problem has changed. 3735ecc87898SStefano Zampini In both cases, the solver will collect the needed vectors that will be 3736ecc87898SStefano Zampini transferred from the old to the new sizes using the `transfer` callback. These vectors will include the 3737ecc87898SStefano Zampini current solution vector, and other vectors needed by the specific solver used. 37386bd3a4fdSStefano Zampini For example, `TSBDF` uses previous solutions vectors to solve for the next time step. 37396bd3a4fdSStefano Zampini Other application specific objects associated with the solver, i.e. Jacobian matrices and `DM`, 37406bd3a4fdSStefano Zampini will be automatically reset if the sizes are changed and they must be specified again by the user 37416bd3a4fdSStefano Zampini inside the `transfer` function. 37426bd3a4fdSStefano Zampini The input and output arrays passed to `transfer` are allocated by PETSc. 37436bd3a4fdSStefano Zampini Vectors in `vecsout` must be created by the user. 37446bd3a4fdSStefano Zampini Ownership of vectors in `vecsout` is transferred to PETSc. 37456bd3a4fdSStefano Zampini 37466bd3a4fdSStefano Zampini Level: advanced 37476bd3a4fdSStefano Zampini 37486bd3a4fdSStefano Zampini .seealso: [](ch_ts), `TS`, `TSSetDM()`, `TSSetIJacobian()`, `TSSetRHSJacobian()` 37496bd3a4fdSStefano Zampini @*/ 3750ecc87898SStefano 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) 37516bd3a4fdSStefano Zampini { 37526bd3a4fdSStefano Zampini PetscFunctionBegin; 37536bd3a4fdSStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3754ecc87898SStefano Zampini ts->resizerollback = rollback; 37556bd3a4fdSStefano Zampini ts->resizesetup = setup; 37566bd3a4fdSStefano Zampini ts->resizetransfer = transfer; 37576bd3a4fdSStefano Zampini ts->resizectx = ctx; 37586bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 37596bd3a4fdSStefano Zampini } 37606bd3a4fdSStefano Zampini 376114d0ab18SJacob Faibussowitsch /* 37626bd3a4fdSStefano Zampini TSResizeRegisterOrRetrieve - Register or import vectors transferred with `TSResize()`. 37636bd3a4fdSStefano Zampini 37646bd3a4fdSStefano Zampini Collective 37656bd3a4fdSStefano Zampini 37666bd3a4fdSStefano Zampini Input Parameters: 37676bd3a4fdSStefano Zampini + ts - The `TS` context obtained from `TSCreate()` 37686bd3a4fdSStefano 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. 37696bd3a4fdSStefano Zampini 37706bd3a4fdSStefano Zampini Level: developer 37716bd3a4fdSStefano Zampini 37726bd3a4fdSStefano Zampini Note: 37736bd3a4fdSStefano Zampini `TSResizeRegisterOrRetrieve()` is declared PETSC_INTERN since it is 37746bd3a4fdSStefano Zampini used within time stepping implementations, 37756bd3a4fdSStefano Zampini so most users would not generally call this routine themselves. 37766bd3a4fdSStefano Zampini 37776bd3a4fdSStefano Zampini .seealso: [](ch_ts), `TS`, `TSSetResize()` 37786bd3a4fdSStefano Zampini @*/ 377914d0ab18SJacob Faibussowitsch static PetscErrorCode TSResizeRegisterOrRetrieve(TS ts, PetscBool flg) 37806bd3a4fdSStefano Zampini { 37816bd3a4fdSStefano Zampini PetscFunctionBegin; 37826bd3a4fdSStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 37836bd3a4fdSStefano Zampini PetscTryTypeMethod(ts, resizeregister, flg); 37846bd3a4fdSStefano Zampini /* PetscTryTypeMethod(adapt, resizeregister, flg); */ 37856bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 37866bd3a4fdSStefano Zampini } 37876bd3a4fdSStefano Zampini 378814d0ab18SJacob Faibussowitsch static PetscErrorCode TSResizeReset(TS ts) 37896bd3a4fdSStefano Zampini { 37906bd3a4fdSStefano Zampini PetscFunctionBegin; 37916bd3a4fdSStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 37926bd3a4fdSStefano Zampini PetscCall(PetscObjectListDestroy(&ts->resizetransferobjs)); 37936bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 37946bd3a4fdSStefano Zampini } 37956bd3a4fdSStefano Zampini 37966bd3a4fdSStefano Zampini static PetscErrorCode TSResizeTransferVecs(TS ts, PetscInt cnt, Vec vecsin[], Vec vecsout[]) 37976bd3a4fdSStefano Zampini { 37986bd3a4fdSStefano Zampini PetscFunctionBegin; 37996bd3a4fdSStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 38006bd3a4fdSStefano Zampini PetscValidLogicalCollectiveInt(ts, cnt, 2); 38016bd3a4fdSStefano Zampini for (PetscInt i = 0; i < cnt; i++) PetscCall(VecLockReadPush(vecsin[i])); 38026bd3a4fdSStefano Zampini if (ts->resizetransfer) { 38036bd3a4fdSStefano Zampini PetscCall(PetscInfo(ts, "Transferring %" PetscInt_FMT " vectors\n", cnt)); 38046bd3a4fdSStefano Zampini PetscCallBack("TS callback resize transfer", (*ts->resizetransfer)(ts, cnt, vecsin, vecsout, ts->resizectx)); 38056bd3a4fdSStefano Zampini } 38066bd3a4fdSStefano Zampini for (PetscInt i = 0; i < cnt; i++) PetscCall(VecLockReadPop(vecsin[i])); 38076bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 38086bd3a4fdSStefano Zampini } 38096bd3a4fdSStefano Zampini 38106bd3a4fdSStefano Zampini /*@C 38116bd3a4fdSStefano Zampini TSResizeRegisterVec - Register a vector to be transferred with `TSResize()`. 38126bd3a4fdSStefano Zampini 38136bd3a4fdSStefano Zampini Collective 38146bd3a4fdSStefano Zampini 38156bd3a4fdSStefano Zampini Input Parameters: 38166bd3a4fdSStefano Zampini + ts - The `TS` context obtained from `TSCreate()` 3817baca6076SPierre Jolivet . name - A string identifying the vector 38186bd3a4fdSStefano Zampini - vec - The vector 38196bd3a4fdSStefano Zampini 38206bd3a4fdSStefano Zampini Level: developer 38216bd3a4fdSStefano Zampini 38226bd3a4fdSStefano Zampini Note: 38236bd3a4fdSStefano Zampini `TSResizeRegisterVec()` is typically used within time stepping implementations, 38246bd3a4fdSStefano Zampini so most users would not generally call this routine themselves. 38256bd3a4fdSStefano Zampini 38266bd3a4fdSStefano Zampini .seealso: [](ch_ts), `TS`, `TSSetResize()`, `TSResize()`, `TSResizeRetrieveVec()` 38276bd3a4fdSStefano Zampini @*/ 3828cc4c1da9SBarry Smith PetscErrorCode TSResizeRegisterVec(TS ts, const char name[], Vec vec) 38296bd3a4fdSStefano Zampini { 38306bd3a4fdSStefano Zampini PetscFunctionBegin; 38316bd3a4fdSStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 38324f572ea9SToby Isaac PetscAssertPointer(name, 2); 38336bd3a4fdSStefano Zampini if (vec) PetscValidHeaderSpecific(vec, VEC_CLASSID, 3); 38346bd3a4fdSStefano Zampini PetscCall(PetscObjectListAdd(&ts->resizetransferobjs, name, (PetscObject)vec)); 38356bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 38366bd3a4fdSStefano Zampini } 38376bd3a4fdSStefano Zampini 38386bd3a4fdSStefano Zampini /*@C 38396bd3a4fdSStefano Zampini TSResizeRetrieveVec - Retrieve a vector registered with `TSResizeRegisterVec()`. 38406bd3a4fdSStefano Zampini 38416bd3a4fdSStefano Zampini Collective 38426bd3a4fdSStefano Zampini 38436bd3a4fdSStefano Zampini Input Parameters: 38446bd3a4fdSStefano Zampini + ts - The `TS` context obtained from `TSCreate()` 3845baca6076SPierre Jolivet . name - A string identifying the vector 38466bd3a4fdSStefano Zampini - vec - The vector 38476bd3a4fdSStefano Zampini 38486bd3a4fdSStefano Zampini Level: developer 38496bd3a4fdSStefano Zampini 38506bd3a4fdSStefano Zampini Note: 38516bd3a4fdSStefano Zampini `TSResizeRetrieveVec()` is typically used within time stepping implementations, 38526bd3a4fdSStefano Zampini so most users would not generally call this routine themselves. 38536bd3a4fdSStefano Zampini 38546bd3a4fdSStefano Zampini .seealso: [](ch_ts), `TS`, `TSSetResize()`, `TSResize()`, `TSResizeRegisterVec()` 38556bd3a4fdSStefano Zampini @*/ 3856cc4c1da9SBarry Smith PetscErrorCode TSResizeRetrieveVec(TS ts, const char name[], Vec *vec) 38576bd3a4fdSStefano Zampini { 38586bd3a4fdSStefano Zampini PetscFunctionBegin; 38596bd3a4fdSStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 38604f572ea9SToby Isaac PetscAssertPointer(name, 2); 38614f572ea9SToby Isaac PetscAssertPointer(vec, 3); 38626bd3a4fdSStefano Zampini PetscCall(PetscObjectListFind(ts->resizetransferobjs, name, (PetscObject *)vec)); 38636bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 38646bd3a4fdSStefano Zampini } 38656bd3a4fdSStefano Zampini 38666bd3a4fdSStefano Zampini static PetscErrorCode TSResizeGetVecArray(TS ts, PetscInt *nv, const char **names[], Vec *vecs[]) 38676bd3a4fdSStefano Zampini { 38686bd3a4fdSStefano Zampini PetscInt cnt; 38696bd3a4fdSStefano Zampini PetscObjectList tmp; 38706bd3a4fdSStefano Zampini Vec *vecsin = NULL; 38716bd3a4fdSStefano Zampini const char **namesin = NULL; 38726bd3a4fdSStefano Zampini 38736bd3a4fdSStefano Zampini PetscFunctionBegin; 38746bd3a4fdSStefano Zampini for (tmp = ts->resizetransferobjs, cnt = 0; tmp; tmp = tmp->next) 38756bd3a4fdSStefano Zampini if (tmp->obj && tmp->obj->classid == VEC_CLASSID) cnt++; 387603ba6ac9SBarry Smith if (names) PetscCall(PetscMalloc1(cnt, &namesin)); 387703ba6ac9SBarry Smith if (vecs) PetscCall(PetscMalloc1(cnt, &vecsin)); 38786bd3a4fdSStefano Zampini for (tmp = ts->resizetransferobjs, cnt = 0; tmp; tmp = tmp->next) { 38796bd3a4fdSStefano Zampini if (tmp->obj && tmp->obj->classid == VEC_CLASSID) { 38806bd3a4fdSStefano Zampini if (vecs) vecsin[cnt] = (Vec)tmp->obj; 38816bd3a4fdSStefano Zampini if (names) namesin[cnt] = tmp->name; 38826bd3a4fdSStefano Zampini cnt++; 38836bd3a4fdSStefano Zampini } 38846bd3a4fdSStefano Zampini } 38856bd3a4fdSStefano Zampini if (nv) *nv = cnt; 38866bd3a4fdSStefano Zampini if (names) *names = namesin; 38876bd3a4fdSStefano Zampini if (vecs) *vecs = vecsin; 38886bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 38896bd3a4fdSStefano Zampini } 38906bd3a4fdSStefano Zampini 38916bd3a4fdSStefano Zampini /*@ 38926bd3a4fdSStefano Zampini TSResize - Runs the user-defined transfer functions provided with `TSSetResize()` 38936bd3a4fdSStefano Zampini 38946bd3a4fdSStefano Zampini Collective 38956bd3a4fdSStefano Zampini 38966bd3a4fdSStefano Zampini Input Parameter: 38976bd3a4fdSStefano Zampini . ts - The `TS` context obtained from `TSCreate()` 38986bd3a4fdSStefano Zampini 38996bd3a4fdSStefano Zampini Level: developer 39006bd3a4fdSStefano Zampini 39016bd3a4fdSStefano Zampini Note: 39026bd3a4fdSStefano Zampini `TSResize()` is typically used within time stepping implementations, 39036bd3a4fdSStefano Zampini so most users would not generally call this routine themselves. 39046bd3a4fdSStefano Zampini 39056bd3a4fdSStefano Zampini .seealso: [](ch_ts), `TS`, `TSSetResize()` 39066bd3a4fdSStefano Zampini @*/ 39076bd3a4fdSStefano Zampini PetscErrorCode TSResize(TS ts) 39086bd3a4fdSStefano Zampini { 39096bd3a4fdSStefano Zampini PetscInt nv = 0; 39106bd3a4fdSStefano Zampini const char **names = NULL; 39116bd3a4fdSStefano Zampini Vec *vecsin = NULL; 39126bd3a4fdSStefano Zampini const char *solname = "ts:vec_sol"; 39136bd3a4fdSStefano Zampini 39146bd3a4fdSStefano Zampini PetscFunctionBegin; 39156bd3a4fdSStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3916ecc87898SStefano Zampini if (!ts->resizesetup) PetscFunctionReturn(PETSC_SUCCESS); 39176bd3a4fdSStefano Zampini if (ts->resizesetup) { 39186bd3a4fdSStefano Zampini PetscCall(VecLockReadPush(ts->vec_sol)); 3919c6bf8827SStefano Zampini PetscCallBack("TS callback resize setup", (*ts->resizesetup)(ts, ts->steps, ts->ptime, ts->vec_sol, &ts->stepresize, ts->resizectx)); 39206bd3a4fdSStefano Zampini PetscCall(VecLockReadPop(ts->vec_sol)); 3921c6bf8827SStefano Zampini if (ts->stepresize) { 3922ecc87898SStefano Zampini if (ts->resizerollback) { 3923ecc87898SStefano Zampini PetscCall(TSRollBack(ts)); 3924ecc87898SStefano Zampini ts->time_step = ts->time_step0; 3925ecc87898SStefano Zampini } 39266bd3a4fdSStefano Zampini PetscCall(TSResizeRegisterVec(ts, solname, ts->vec_sol)); 39276bd3a4fdSStefano Zampini PetscCall(TSResizeRegisterOrRetrieve(ts, PETSC_TRUE)); /* specific impls register their own objects */ 39286bd3a4fdSStefano Zampini } 39296bd3a4fdSStefano Zampini } 39306bd3a4fdSStefano Zampini 39316bd3a4fdSStefano Zampini PetscCall(TSResizeGetVecArray(ts, &nv, &names, &vecsin)); 39326bd3a4fdSStefano Zampini if (nv) { 39336bd3a4fdSStefano Zampini Vec *vecsout, vecsol; 39346bd3a4fdSStefano Zampini 39356bd3a4fdSStefano Zampini /* Reset internal objects */ 39366bd3a4fdSStefano Zampini PetscCall(TSReset(ts)); 39376bd3a4fdSStefano Zampini 3938ecc87898SStefano Zampini /* Transfer needed vectors (users can call SetJacobian, SetDM, etc. here) */ 39396bd3a4fdSStefano Zampini PetscCall(PetscCalloc1(nv, &vecsout)); 39406bd3a4fdSStefano Zampini PetscCall(TSResizeTransferVecs(ts, nv, vecsin, vecsout)); 39416bd3a4fdSStefano Zampini for (PetscInt i = 0; i < nv; i++) { 3942ecc87898SStefano Zampini const char *name; 3943ecc87898SStefano Zampini char *oname; 3944ecc87898SStefano Zampini 3945ecc87898SStefano Zampini PetscCall(PetscObjectGetName((PetscObject)vecsin[i], &name)); 3946ecc87898SStefano Zampini PetscCall(PetscStrallocpy(name, &oname)); 39476bd3a4fdSStefano Zampini PetscCall(TSResizeRegisterVec(ts, names[i], vecsout[i])); 3948ecc87898SStefano Zampini if (vecsout[i]) PetscCall(PetscObjectSetName((PetscObject)vecsout[i], oname)); 3949ecc87898SStefano Zampini PetscCall(PetscFree(oname)); 39506bd3a4fdSStefano Zampini PetscCall(VecDestroy(&vecsout[i])); 39516bd3a4fdSStefano Zampini } 39526bd3a4fdSStefano Zampini PetscCall(PetscFree(vecsout)); 39536bd3a4fdSStefano Zampini PetscCall(TSResizeRegisterOrRetrieve(ts, PETSC_FALSE)); /* specific impls import the transferred objects */ 39546bd3a4fdSStefano Zampini 39556bd3a4fdSStefano Zampini PetscCall(TSResizeRetrieveVec(ts, solname, &vecsol)); 39566bd3a4fdSStefano Zampini if (vecsol) PetscCall(TSSetSolution(ts, vecsol)); 39576bd3a4fdSStefano Zampini PetscAssert(ts->vec_sol, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_NULL, "Missing TS solution"); 39586bd3a4fdSStefano Zampini } 39596bd3a4fdSStefano Zampini 39606bd3a4fdSStefano Zampini PetscCall(PetscFree(names)); 39616bd3a4fdSStefano Zampini PetscCall(PetscFree(vecsin)); 39626bd3a4fdSStefano Zampini PetscCall(TSResizeReset(ts)); 39636bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 39646bd3a4fdSStefano Zampini } 39656bd3a4fdSStefano Zampini 3966b1cb36f3SHong Zhang /*@ 39676a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 39686a4d4014SLisandro Dalcin 3969c3339decSBarry Smith Collective 39706a4d4014SLisandro Dalcin 3971d8d19677SJose E. Roman Input Parameters: 3972bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 3973bcf0153eSBarry Smith - u - the solution vector (can be null if `TSSetSolution()` was used and `TSSetExactFinalTime`(ts,`TS_EXACTFINALTIME_MATCHSTEP`) was not used, 39740b4b7b1cSBarry Smith otherwise it must contain the initial conditions and will contain the solution at the final requested time 39755a3a76d0SJed Brown 39766a4d4014SLisandro Dalcin Level: beginner 39776a4d4014SLisandro Dalcin 39785a3a76d0SJed Brown Notes: 39795a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 3980bcf0153eSBarry Smith held state accessible by `TSGetSolution()` and `TSGetTime()` because the method may have 39815a3a76d0SJed Brown stepped over the final time. 39825a3a76d0SJed Brown 39831cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSSetSolution()`, `TSStep()`, `TSGetTime()`, `TSGetSolveTime()` 39846a4d4014SLisandro Dalcin @*/ 3985d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSolve(TS ts, Vec u) 3986d71ae5a4SJacob Faibussowitsch { 3987b06615a5SLisandro Dalcin Vec solution; 3988f22f69f0SBarry Smith 39896a4d4014SLisandro Dalcin PetscFunctionBegin; 39900700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3991f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3992303a5415SBarry Smith 39939566063dSJacob Faibussowitsch PetscCall(TSSetExactFinalTimeDefault(ts)); 3994ee41a567SStefano 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 */ 39950910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 39969566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u, &solution)); 39979566063dSJacob Faibussowitsch PetscCall(TSSetSolution(ts, solution)); 39989566063dSJacob Faibussowitsch PetscCall(VecDestroy(&solution)); /* grant ownership */ 39995a3a76d0SJed Brown } 40009566063dSJacob Faibussowitsch PetscCall(VecCopy(u, ts->vec_sol)); 40013c633725SBarry Smith PetscCheck(!ts->forward_solve, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE"); 40021baa6e33SBarry Smith } else if (u) PetscCall(TSSetSolution(ts, u)); 40039566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts)); 40049566063dSJacob Faibussowitsch PetscCall(TSTrajectorySetUp(ts->trajectory, ts)); 4005a6772fa2SLisandro Dalcin 40061690c2aeSBarry Smith PetscCheck(ts->max_time < PETSC_MAX_REAL || ts->max_steps != PETSC_INT_MAX, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "You must call TSSetMaxTime() or TSSetMaxSteps(), or use -ts_max_time <time> or -ts_max_steps <steps>"); 40073c633725SBarry 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()"); 40083c633725SBarry 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"); 4009136cf249SJames Wright PetscCheck(!(ts->eval_times && ts->exact_final_time != TS_EXACTFINALTIME_MATCHSTEP), PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "You must use TS_EXACTFINALTIME_MATCHSTEP when using time span or evaluation times"); 40104a658b32SHong Zhang 4011136cf249SJames Wright if (ts->eval_times) { 4012136cf249SJames Wright for (PetscInt i = 0; i < ts->eval_times->num_time_points; i++) { 4013136cf249SJames Wright PetscBool is_close = PetscIsCloseAtTol(ts->ptime, ts->eval_times->time_points[i], ts->eval_times->reltol * ts->time_step + ts->eval_times->abstol, 0); 4014136cf249SJames Wright if (ts->ptime <= ts->eval_times->time_points[i] || is_close) { 4015136cf249SJames Wright ts->eval_times->time_point_idx = i; 4016136cf249SJames Wright if (is_close) { /* starting point in evaluation times */ 4017136cf249SJames Wright PetscCall(VecCopy(ts->vec_sol, ts->eval_times->sol_vecs[ts->eval_times->sol_ctr])); 4018136cf249SJames Wright ts->eval_times->sol_times[ts->eval_times->sol_ctr] = ts->ptime; 4019136cf249SJames Wright ts->eval_times->sol_ctr++; 4020136cf249SJames Wright ts->eval_times->time_point_idx++; 40218343f784SJames Wright } 40228343f784SJames Wright break; 40238343f784SJames Wright } 40248343f784SJames Wright } 40254a658b32SHong Zhang } 4026a6772fa2SLisandro Dalcin 40271baa6e33SBarry Smith if (ts->forward_solve) PetscCall(TSForwardSetUp(ts)); 4028715f1b00SHong Zhang 4029e7069c78SShri /* reset number of steps only when the step is not restarted. ARKIMEX 4030715f1b00SHong Zhang restarts the step after an event. Resetting these counters in such case causes 4031e7069c78SShri TSTrajectory to incorrectly save the output files 4032e7069c78SShri */ 4033715f1b00SHong Zhang /* reset time step and iteration counters */ 40342808aa04SLisandro Dalcin if (!ts->steps) { 40355ef26d82SJed Brown ts->ksp_its = 0; 40365ef26d82SJed Brown ts->snes_its = 0; 4037c610991cSLisandro Dalcin ts->num_snes_failures = 0; 4038c610991cSLisandro Dalcin ts->reject = 0; 40392808aa04SLisandro Dalcin ts->steprestart = PETSC_TRUE; 40402808aa04SLisandro Dalcin ts->steprollback = PETSC_FALSE; 4041c6bf8827SStefano Zampini ts->stepresize = PETSC_FALSE; 40427d51462cSStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 40432808aa04SLisandro Dalcin } 4044e97c63d7SStefano Zampini 4045136cf249SJames Wright /* make sure initial time step does not overshoot final time or the next point in evaluation times */ 4046e97c63d7SStefano Zampini if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) { 40474a658b32SHong Zhang PetscReal maxdt; 4048e97c63d7SStefano Zampini PetscReal dt = ts->time_step; 4049e97c63d7SStefano Zampini 4050136cf249SJames Wright if (ts->eval_times) maxdt = ts->eval_times->time_points[ts->eval_times->time_point_idx] - ts->ptime; 40514a658b32SHong Zhang else maxdt = ts->max_time - ts->ptime; 4052e97c63d7SStefano Zampini ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt, maxdt, 10 * PETSC_MACHINE_EPSILON, 0) ? maxdt : dt); 4053e97c63d7SStefano Zampini } 4054193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 4055193ac0bcSJed Brown 4056900f6b5bSMatthew G. Knepley { 4057900f6b5bSMatthew G. Knepley PetscViewer viewer; 4058900f6b5bSMatthew G. Knepley PetscViewerFormat format; 4059900f6b5bSMatthew G. Knepley PetscBool flg; 4060900f6b5bSMatthew G. Knepley static PetscBool incall = PETSC_FALSE; 4061900f6b5bSMatthew G. Knepley 4062900f6b5bSMatthew G. Knepley if (!incall) { 4063900f6b5bSMatthew G. Knepley /* Estimate the convergence rate of the time discretization */ 4064648c30bcSBarry Smith PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)ts), ((PetscObject)ts)->options, ((PetscObject)ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg)); 4065900f6b5bSMatthew G. Knepley if (flg) { 4066900f6b5bSMatthew G. Knepley PetscConvEst conv; 4067900f6b5bSMatthew G. Knepley DM dm; 4068900f6b5bSMatthew G. Knepley PetscReal *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */ 4069900f6b5bSMatthew G. Knepley PetscInt Nf; 4070f2ed2dc7SMatthew G. Knepley PetscBool checkTemporal = PETSC_TRUE; 4071900f6b5bSMatthew G. Knepley 4072900f6b5bSMatthew G. Knepley incall = PETSC_TRUE; 40739566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject)ts)->prefix, "-ts_convergence_temporal", &checkTemporal, &flg)); 40749566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 40759566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &Nf)); 40769566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(PetscMax(Nf, 1), &alpha)); 40779566063dSJacob Faibussowitsch PetscCall(PetscConvEstCreate(PetscObjectComm((PetscObject)ts), &conv)); 40789566063dSJacob Faibussowitsch PetscCall(PetscConvEstUseTS(conv, checkTemporal)); 40799566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetSolver(conv, (PetscObject)ts)); 40809566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetFromOptions(conv)); 40819566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetUp(conv)); 40829566063dSJacob Faibussowitsch PetscCall(PetscConvEstGetConvRate(conv, alpha)); 40839566063dSJacob Faibussowitsch PetscCall(PetscViewerPushFormat(viewer, format)); 40849566063dSJacob Faibussowitsch PetscCall(PetscConvEstRateView(conv, alpha, viewer)); 40859566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(viewer)); 4086648c30bcSBarry Smith PetscCall(PetscViewerDestroy(&viewer)); 40879566063dSJacob Faibussowitsch PetscCall(PetscConvEstDestroy(&conv)); 40889566063dSJacob Faibussowitsch PetscCall(PetscFree(alpha)); 4089900f6b5bSMatthew G. Knepley incall = PETSC_FALSE; 4090900f6b5bSMatthew G. Knepley } 4091900f6b5bSMatthew G. Knepley } 4092900f6b5bSMatthew G. Knepley } 4093900f6b5bSMatthew G. Knepley 40949566063dSJacob Faibussowitsch PetscCall(TSViewFromOptions(ts, NULL, "-ts_view_pre")); 4095f05ece33SBarry Smith 4096193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 4097dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, solve); 40989566063dSJacob Faibussowitsch if (u) PetscCall(VecCopy(ts->vec_sol, u)); 4099cc708dedSBarry Smith ts->solvetime = ts->ptime; 4100a6772fa2SLisandro Dalcin solution = ts->vec_sol; 4101be5899b3SLisandro Dalcin } else { /* Step the requested number of timesteps. */ 4102db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 4103db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 4104e7069c78SShri 41052808aa04SLisandro Dalcin if (!ts->steps) { 41069566063dSJacob Faibussowitsch PetscCall(TSTrajectorySet(ts->trajectory, ts, ts->steps, ts->ptime, ts->vec_sol)); 41079566063dSJacob Faibussowitsch PetscCall(TSEventInitialize(ts->event, ts, ts->ptime, ts->vec_sol)); 41082808aa04SLisandro Dalcin } 41096427ac75SLisandro Dalcin 4110e1a7a14fSJed Brown while (!ts->reason) { 41119566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts, ts->steps, ts->ptime, ts->vec_sol)); 4112c6bf8827SStefano Zampini if (!ts->steprollback || (ts->stepresize && ts->resizerollback)) PetscCall(TSPreStep(ts)); 41139566063dSJacob Faibussowitsch PetscCall(TSStep(ts)); 41141baa6e33SBarry Smith if (ts->testjacobian) PetscCall(TSRHSJacobianTest(ts, NULL)); 41151baa6e33SBarry Smith if (ts->testjacobiantranspose) PetscCall(TSRHSJacobianTestTranspose(ts, NULL)); 4116cd4cee2dSHong Zhang if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */ 41177b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */ 41189566063dSJacob Faibussowitsch PetscCall(TSForwardCostIntegral(ts)); 41197b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps++; 4120b1cb36f3SHong Zhang } 412158818c2dSLisandro Dalcin if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */ 41227b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */ 41239566063dSJacob Faibussowitsch PetscCall(TSForwardStep(ts)); 41247b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps++; 4125715f1b00SHong Zhang } 41269566063dSJacob Faibussowitsch PetscCall(TSPostEvaluate(ts)); 41279566063dSJacob 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. */ 41281baa6e33SBarry Smith if (ts->steprollback) PetscCall(TSPostEvaluate(ts)); 4129ecc87898SStefano Zampini if (!ts->steprollback && ts->resizerollback) PetscCall(TSResize(ts)); 413090d719a4SStefano Zampini /* check convergence */ 413190d719a4SStefano Zampini if (!ts->reason) { 413290d719a4SStefano Zampini if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 413390d719a4SStefano Zampini else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 413490d719a4SStefano Zampini } 4135e783b05fSHong Zhang if (!ts->steprollback) { 41369566063dSJacob Faibussowitsch PetscCall(TSTrajectorySet(ts->trajectory, ts, ts->steps, ts->ptime, ts->vec_sol)); 41379566063dSJacob Faibussowitsch PetscCall(TSPostStep(ts)); 4138ecc87898SStefano Zampini if (!ts->resizerollback) PetscCall(TSResize(ts)); 4139ca4445c7SIlya Fursov 414026077dccSJames Wright if (ts->eval_times && ts->eval_times->time_point_idx < ts->eval_times->num_time_points && ts->reason >= 0) { 4141136cf249SJames Wright PetscCheck(ts->eval_times->worktol > 0, PetscObjectComm((PetscObject)ts), PETSC_ERR_PLIB, "Unexpected state !(eval_times->worktol > 0) in TSSolve()"); 4142136cf249SJames Wright if (PetscIsCloseAtTol(ts->ptime, ts->eval_times->time_points[ts->eval_times->time_point_idx], ts->eval_times->worktol, 0)) { 4143136cf249SJames Wright ts->eval_times->sol_times[ts->eval_times->sol_ctr] = ts->ptime; 4144136cf249SJames Wright PetscCall(VecCopy(ts->vec_sol, ts->eval_times->sol_vecs[ts->eval_times->sol_ctr])); 4145136cf249SJames Wright ts->eval_times->sol_ctr++; 4146136cf249SJames Wright ts->eval_times->time_point_idx++; 41478343f784SJames Wright } 4148ca4445c7SIlya Fursov } 4149aeb4809dSShri Abhyankar } 4150193ac0bcSJed Brown } 41519566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts, ts->steps, ts->ptime, ts->vec_sol)); 41526427ac75SLisandro Dalcin 415349354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 41545dbbf88eSStefano Zampini if (!u) u = ts->vec_sol; 41559566063dSJacob Faibussowitsch PetscCall(TSInterpolate(ts, ts->max_time, u)); 4156cc708dedSBarry Smith ts->solvetime = ts->max_time; 4157b06615a5SLisandro Dalcin solution = u; 41589566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts, -1, ts->solvetime, solution)); 41590574a7fbSJed Brown } else { 41609566063dSJacob Faibussowitsch if (u) PetscCall(VecCopy(ts->vec_sol, u)); 4161cc708dedSBarry Smith ts->solvetime = ts->ptime; 4162b06615a5SLisandro Dalcin solution = ts->vec_sol; 41630574a7fbSJed Brown } 4164193ac0bcSJed Brown } 4165d2daff3dSHong Zhang 41669566063dSJacob Faibussowitsch PetscCall(TSViewFromOptions(ts, NULL, "-ts_view")); 41679566063dSJacob Faibussowitsch PetscCall(VecViewFromOptions(solution, (PetscObject)ts, "-ts_view_solution")); 41689566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsBlock((PetscObject)ts)); 41691baa6e33SBarry Smith if (ts->adjoint_solve) PetscCall(TSAdjointSolve(ts)); 41703ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 41716a4d4014SLisandro Dalcin } 41726a4d4014SLisandro Dalcin 4173d763cef2SBarry Smith /*@ 4174b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 4175d763cef2SBarry Smith 4176d763cef2SBarry Smith Not Collective 4177d763cef2SBarry Smith 4178d763cef2SBarry Smith Input Parameter: 4179bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 4180d763cef2SBarry Smith 4181d763cef2SBarry Smith Output Parameter: 4182bcf0153eSBarry Smith . t - the current time. This time may not corresponds to the final time set with `TSSetMaxTime()`, use `TSGetSolveTime()`. 4183d763cef2SBarry Smith 4184d763cef2SBarry Smith Level: beginner 4185d763cef2SBarry Smith 4186b8123daeSJed Brown Note: 4187bcf0153eSBarry Smith When called during time step evaluation (e.g. during residual evaluation or via hooks set using `TSSetPreStep()`, 4188bcf0153eSBarry Smith `TSSetPreStage()`, `TSSetPostStage()`, or `TSSetPostStep()`), the time is the time at the start of the step being evaluated. 4189b8123daeSJed Brown 4190a94f484eSPierre Jolivet .seealso: [](ch_ts), `TS`, `TSGetSolveTime()`, `TSSetTime()`, `TSGetTimeStep()`, `TSGetStepNumber()` 4191d763cef2SBarry Smith @*/ 4192d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTime(TS ts, PetscReal *t) 4193d71ae5a4SJacob Faibussowitsch { 4194d763cef2SBarry Smith PetscFunctionBegin; 41950700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 41964f572ea9SToby Isaac PetscAssertPointer(t, 2); 4197d763cef2SBarry Smith *t = ts->ptime; 41983ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4199d763cef2SBarry Smith } 4200d763cef2SBarry Smith 4201e5e524a1SHong Zhang /*@ 4202e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 4203e5e524a1SHong Zhang 4204e5e524a1SHong Zhang Not Collective 4205e5e524a1SHong Zhang 4206e5e524a1SHong Zhang Input Parameter: 4207bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 4208e5e524a1SHong Zhang 4209e5e524a1SHong Zhang Output Parameter: 4210e5e524a1SHong Zhang . t - the previous time 4211e5e524a1SHong Zhang 4212e5e524a1SHong Zhang Level: beginner 4213e5e524a1SHong Zhang 4214a94f484eSPierre Jolivet .seealso: [](ch_ts), `TS`, `TSGetTime()`, `TSGetSolveTime()`, `TSGetTimeStep()` 4215e5e524a1SHong Zhang @*/ 4216d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetPrevTime(TS ts, PetscReal *t) 4217d71ae5a4SJacob Faibussowitsch { 4218e5e524a1SHong Zhang PetscFunctionBegin; 4219e5e524a1SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 42204f572ea9SToby Isaac PetscAssertPointer(t, 2); 4221e5e524a1SHong Zhang *t = ts->ptime_prev; 42223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4223e5e524a1SHong Zhang } 4224e5e524a1SHong Zhang 42256a4d4014SLisandro Dalcin /*@ 42266a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 42276a4d4014SLisandro Dalcin 4228c3339decSBarry Smith Logically Collective 42296a4d4014SLisandro Dalcin 42306a4d4014SLisandro Dalcin Input Parameters: 4231bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 4232b43aa488SJacob Faibussowitsch - t - the time 42336a4d4014SLisandro Dalcin 42346a4d4014SLisandro Dalcin Level: intermediate 42356a4d4014SLisandro Dalcin 42361cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTime()`, `TSSetMaxSteps()` 42376a4d4014SLisandro Dalcin @*/ 4238d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTime(TS ts, PetscReal t) 4239d71ae5a4SJacob Faibussowitsch { 42406a4d4014SLisandro Dalcin PetscFunctionBegin; 42410700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4242c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts, t, 2); 42436a4d4014SLisandro Dalcin ts->ptime = t; 42443ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 42456a4d4014SLisandro Dalcin } 42466a4d4014SLisandro Dalcin 4247cc4c1da9SBarry Smith /*@ 4248d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 4249d763cef2SBarry Smith TS options in the database. 4250d763cef2SBarry Smith 4251c3339decSBarry Smith Logically Collective 4252d763cef2SBarry Smith 4253d8d19677SJose E. Roman Input Parameters: 4254bcf0153eSBarry Smith + ts - The `TS` context 4255d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4256d763cef2SBarry Smith 4257bcf0153eSBarry Smith Level: advanced 4258bcf0153eSBarry Smith 4259bcf0153eSBarry Smith Note: 4260d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4261d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4262d763cef2SBarry Smith hyphen. 4263d763cef2SBarry Smith 42641cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetFromOptions()`, `TSAppendOptionsPrefix()` 4265d763cef2SBarry Smith @*/ 4266d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetOptionsPrefix(TS ts, const char prefix[]) 4267d71ae5a4SJacob Faibussowitsch { 4268089b2837SJed Brown SNES snes; 4269d763cef2SBarry Smith 4270d763cef2SBarry Smith PetscFunctionBegin; 42710700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 42729566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)ts, prefix)); 42739566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 42749566063dSJacob Faibussowitsch PetscCall(SNESSetOptionsPrefix(snes, prefix)); 42753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4276d763cef2SBarry Smith } 4277d763cef2SBarry Smith 4278cc4c1da9SBarry Smith /*@ 4279d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 4280d763cef2SBarry Smith TS options in the database. 4281d763cef2SBarry Smith 4282c3339decSBarry Smith Logically Collective 4283d763cef2SBarry Smith 4284d8d19677SJose E. Roman Input Parameters: 4285bcf0153eSBarry Smith + ts - The `TS` context 4286d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4287d763cef2SBarry Smith 4288bcf0153eSBarry Smith Level: advanced 4289bcf0153eSBarry Smith 4290bcf0153eSBarry Smith Note: 4291d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4292d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4293d763cef2SBarry Smith hyphen. 4294d763cef2SBarry Smith 42951cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetOptionsPrefix()`, `TSSetOptionsPrefix()`, `TSSetFromOptions()` 4296d763cef2SBarry Smith @*/ 4297d71ae5a4SJacob Faibussowitsch PetscErrorCode TSAppendOptionsPrefix(TS ts, const char prefix[]) 4298d71ae5a4SJacob Faibussowitsch { 4299089b2837SJed Brown SNES snes; 4300d763cef2SBarry Smith 4301d763cef2SBarry Smith PetscFunctionBegin; 43020700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 43039566063dSJacob Faibussowitsch PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)ts, prefix)); 43049566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 43059566063dSJacob Faibussowitsch PetscCall(SNESAppendOptionsPrefix(snes, prefix)); 43063ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4307d763cef2SBarry Smith } 4308d763cef2SBarry Smith 4309cc4c1da9SBarry Smith /*@ 4310d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 4311bcf0153eSBarry Smith `TS` options in the database. 4312d763cef2SBarry Smith 4313d763cef2SBarry Smith Not Collective 4314d763cef2SBarry Smith 4315d763cef2SBarry Smith Input Parameter: 4316bcf0153eSBarry Smith . ts - The `TS` context 4317d763cef2SBarry Smith 4318d763cef2SBarry Smith Output Parameter: 4319d763cef2SBarry Smith . prefix - A pointer to the prefix string used 4320d763cef2SBarry Smith 4321d763cef2SBarry Smith Level: intermediate 4322d763cef2SBarry Smith 4323b43aa488SJacob Faibussowitsch Fortran Notes: 4324195e9b02SBarry Smith The user should pass in a string 'prefix' of 4325bcf0153eSBarry Smith sufficient length to hold the prefix. 4326bcf0153eSBarry Smith 43271cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSAppendOptionsPrefix()`, `TSSetFromOptions()` 4328d763cef2SBarry Smith @*/ 4329d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetOptionsPrefix(TS ts, const char *prefix[]) 4330d71ae5a4SJacob Faibussowitsch { 4331d763cef2SBarry Smith PetscFunctionBegin; 43320700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 43334f572ea9SToby Isaac PetscAssertPointer(prefix, 2); 43349566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)ts, prefix)); 43353ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4336d763cef2SBarry Smith } 4337d763cef2SBarry Smith 4338d763cef2SBarry Smith /*@C 4339d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4340d763cef2SBarry Smith 4341bcf0153eSBarry Smith Not Collective, but parallel objects are returned if ts is parallel 4342d763cef2SBarry Smith 4343d763cef2SBarry Smith Input Parameter: 4344bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 4345d763cef2SBarry Smith 4346d763cef2SBarry Smith Output Parameters: 4347195e9b02SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or `NULL`) 4348195e9b02SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as `Amat` (or `NULL`) 4349195e9b02SBarry Smith . func - Function to compute the Jacobian of the RHS (or `NULL`) 4350195e9b02SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or `NULL`) 4351d763cef2SBarry Smith 4352d763cef2SBarry Smith Level: intermediate 4353d763cef2SBarry Smith 4354bcf0153eSBarry Smith Note: 4355195e9b02SBarry Smith You can pass in `NULL` for any return argument you do not need. 4356bcf0153eSBarry Smith 43571cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTimeStep()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()` 4358d763cef2SBarry Smith 4359d763cef2SBarry Smith @*/ 43608434afd1SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts, Mat *Amat, Mat *Pmat, TSRHSJacobianFn **func, void **ctx) 4361d71ae5a4SJacob Faibussowitsch { 436224989b8cSPeter Brune DM dm; 4363089b2837SJed Brown 4364d763cef2SBarry Smith PetscFunctionBegin; 436523a57915SBarry Smith if (Amat || Pmat) { 436623a57915SBarry Smith SNES snes; 43679566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 43689566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 43699566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes, Amat, Pmat, NULL, NULL)); 437023a57915SBarry Smith } 43719566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 43729566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, func, ctx)); 43733ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4374d763cef2SBarry Smith } 4375d763cef2SBarry Smith 43762eca1d9cSJed Brown /*@C 43772eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 43782eca1d9cSJed Brown 4379bcf0153eSBarry Smith Not Collective, but parallel objects are returned if ts is parallel 43802eca1d9cSJed Brown 43812eca1d9cSJed Brown Input Parameter: 4382bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 43832eca1d9cSJed Brown 43842eca1d9cSJed Brown Output Parameters: 4385e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4386195e9b02SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as `Amat` 43872eca1d9cSJed Brown . f - The function to compute the matrices 43882eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 43892eca1d9cSJed Brown 43902eca1d9cSJed Brown Level: advanced 43912eca1d9cSJed Brown 4392bcf0153eSBarry Smith Note: 4393195e9b02SBarry Smith You can pass in `NULL` for any return argument you do not need. 4394bcf0153eSBarry Smith 43951cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTimeStep()`, `TSGetRHSJacobian()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()` 43962eca1d9cSJed Brown @*/ 43978434afd1SBarry Smith PetscErrorCode TSGetIJacobian(TS ts, Mat *Amat, Mat *Pmat, TSIJacobianFn **f, void **ctx) 4398d71ae5a4SJacob Faibussowitsch { 439924989b8cSPeter Brune DM dm; 44000910c330SBarry Smith 44012eca1d9cSJed Brown PetscFunctionBegin; 4402c0aab802Sstefano_zampini if (Amat || Pmat) { 4403c0aab802Sstefano_zampini SNES snes; 44049566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 44059566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 44069566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes, Amat, Pmat, NULL, NULL)); 4407c0aab802Sstefano_zampini } 44089566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 44099566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm, f, ctx)); 44103ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 44112eca1d9cSJed Brown } 44122eca1d9cSJed Brown 4413af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 44146c699258SBarry Smith /*@ 4415bcf0153eSBarry Smith TSSetDM - Sets the `DM` that may be used by some nonlinear solvers or preconditioners under the `TS` 44166c699258SBarry Smith 4417c3339decSBarry Smith Logically Collective 44186c699258SBarry Smith 44196c699258SBarry Smith Input Parameters: 4420bcf0153eSBarry Smith + ts - the `TS` integrator object 4421195e9b02SBarry Smith - dm - the dm, cannot be `NULL` 44226c699258SBarry Smith 44236c699258SBarry Smith Level: intermediate 44246c699258SBarry Smith 4425bcf0153eSBarry Smith Notes: 4426bcf0153eSBarry Smith A `DM` can only be used for solving one problem at a time because information about the problem is stored on the `DM`, 4427bcf0153eSBarry Smith even when not using interfaces like `DMTSSetIFunction()`. Use `DMClone()` to get a distinct `DM` when solving 4428bcf0153eSBarry Smith different problems using the same function space. 4429bcf0153eSBarry Smith 44301cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `DM`, `TSGetDM()`, `SNESSetDM()`, `SNESGetDM()` 44316c699258SBarry Smith @*/ 4432d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetDM(TS ts, DM dm) 4433d71ae5a4SJacob Faibussowitsch { 4434089b2837SJed Brown SNES snes; 4435942e3340SBarry Smith DMTS tsdm; 44366c699258SBarry Smith 44376c699258SBarry Smith PetscFunctionBegin; 44380700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 44392a808120SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 2); 44409566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)dm)); 4441942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 44422a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 44439566063dSJacob Faibussowitsch PetscCall(DMCopyDMTS(ts->dm, dm)); 44449566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm, &tsdm)); 44451e66621cSBarry Smith /* Grant write privileges to the replacement DM */ 44461e66621cSBarry Smith if (tsdm->originaldm == ts->dm) tsdm->originaldm = dm; 444724989b8cSPeter Brune } 44489566063dSJacob Faibussowitsch PetscCall(DMDestroy(&ts->dm)); 444924989b8cSPeter Brune } 44506c699258SBarry Smith ts->dm = dm; 4451bbd56ea5SKarl Rupp 44529566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 44539566063dSJacob Faibussowitsch PetscCall(SNESSetDM(snes, dm)); 44543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 44556c699258SBarry Smith } 44566c699258SBarry Smith 44576c699258SBarry Smith /*@ 4458bcf0153eSBarry Smith TSGetDM - Gets the `DM` that may be used by some preconditioners 44596c699258SBarry Smith 44603f9fe445SBarry Smith Not Collective 44616c699258SBarry Smith 44626c699258SBarry Smith Input Parameter: 4463bcf0153eSBarry Smith . ts - the `TS` 44646c699258SBarry Smith 44656c699258SBarry Smith Output Parameter: 4466195e9b02SBarry Smith . dm - the `DM` 44676c699258SBarry Smith 44686c699258SBarry Smith Level: intermediate 44696c699258SBarry Smith 44701cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `DM`, `TSSetDM()`, `SNESSetDM()`, `SNESGetDM()` 44716c699258SBarry Smith @*/ 4472d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetDM(TS ts, DM *dm) 4473d71ae5a4SJacob Faibussowitsch { 44746c699258SBarry Smith PetscFunctionBegin; 44750700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4476496e6a7aSJed Brown if (!ts->dm) { 44779566063dSJacob Faibussowitsch PetscCall(DMShellCreate(PetscObjectComm((PetscObject)ts), &ts->dm)); 44784bd3aaa3SHong Zhang if (ts->snes) PetscCall(SNESSetDM(ts->snes, ts->dm)); 4479496e6a7aSJed Brown } 44806c699258SBarry Smith *dm = ts->dm; 44813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 44826c699258SBarry Smith } 44831713a123SBarry Smith 44840f5c6efeSJed Brown /*@ 44850f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 44860f5c6efeSJed Brown 4487c3339decSBarry Smith Logically Collective 44880f5c6efeSJed Brown 4489d8d19677SJose E. Roman Input Parameters: 4490d42a1c89SJed Brown + snes - nonlinear solver 44910910c330SBarry Smith . U - the current state at which to evaluate the residual 4492d42a1c89SJed Brown - ctx - user context, must be a TS 44930f5c6efeSJed Brown 44940f5c6efeSJed Brown Output Parameter: 44950f5c6efeSJed Brown . F - the nonlinear residual 44960f5c6efeSJed Brown 44970f5c6efeSJed Brown Level: advanced 44980f5c6efeSJed Brown 4499bcf0153eSBarry Smith Note: 4500bcf0153eSBarry Smith This function is not normally called by users and is automatically registered with the `SNES` used by `TS`. 4501bcf0153eSBarry Smith It is most frequently passed to `MatFDColoringSetFunction()`. 4502bcf0153eSBarry Smith 45031cc06b55SBarry Smith .seealso: [](ch_ts), `SNESSetFunction()`, `MatFDColoringSetFunction()` 45040f5c6efeSJed Brown @*/ 4505d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESTSFormFunction(SNES snes, Vec U, Vec F, void *ctx) 4506d71ae5a4SJacob Faibussowitsch { 45070f5c6efeSJed Brown TS ts = (TS)ctx; 45080f5c6efeSJed Brown 45090f5c6efeSJed Brown PetscFunctionBegin; 45100f5c6efeSJed Brown PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 45110910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 2); 45120f5c6efeSJed Brown PetscValidHeaderSpecific(F, VEC_CLASSID, 3); 45130f5c6efeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 4); 4514346ce620SStefano Zampini PetscCheck(ts->ops->snesfunction, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "No method snesfunction for TS of type %s", ((PetscObject)ts)->type_name); 4515346ce620SStefano Zampini PetscCall((*ts->ops->snesfunction)(snes, U, F, ts)); 45163ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 45170f5c6efeSJed Brown } 45180f5c6efeSJed Brown 45190f5c6efeSJed Brown /*@ 45200f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 45210f5c6efeSJed Brown 4522c3339decSBarry Smith Collective 45230f5c6efeSJed Brown 4524d8d19677SJose E. Roman Input Parameters: 45250f5c6efeSJed Brown + snes - nonlinear solver 45260910c330SBarry Smith . U - the current state at which to evaluate the residual 4527bcf0153eSBarry Smith - ctx - user context, must be a `TS` 45280f5c6efeSJed Brown 4529d8d19677SJose E. Roman Output Parameters: 45300f5c6efeSJed Brown + A - the Jacobian 45316b867d5aSJose E. Roman - B - the preconditioning matrix (may be the same as A) 45320f5c6efeSJed Brown 45330f5c6efeSJed Brown Level: developer 45340f5c6efeSJed Brown 4535bcf0153eSBarry Smith Note: 4536bcf0153eSBarry Smith This function is not normally called by users and is automatically registered with the `SNES` used by `TS`. 4537bcf0153eSBarry Smith 45381cc06b55SBarry Smith .seealso: [](ch_ts), `SNESSetJacobian()` 45390f5c6efeSJed Brown @*/ 4540d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESTSFormJacobian(SNES snes, Vec U, Mat A, Mat B, void *ctx) 4541d71ae5a4SJacob Faibussowitsch { 45420f5c6efeSJed Brown TS ts = (TS)ctx; 45430f5c6efeSJed Brown 45440f5c6efeSJed Brown PetscFunctionBegin; 45450f5c6efeSJed Brown PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 45460910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 2); 454794ab13aaSBarry Smith PetscValidHeaderSpecific(A, MAT_CLASSID, 3); 454894ab13aaSBarry Smith PetscValidHeaderSpecific(B, MAT_CLASSID, 4); 4549064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(ts, TS_CLASSID, 5); 4550346ce620SStefano Zampini PetscCheck(ts->ops->snesjacobian, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "No method snesjacobian for TS of type %s", ((PetscObject)ts)->type_name); 4551346ce620SStefano Zampini PetscCall((*ts->ops->snesjacobian)(snes, U, A, B, ts)); 45523ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 45530f5c6efeSJed Brown } 4554325fc9f4SBarry Smith 45550e4ef248SJed Brown /*@C 4556dd8e379bSPierre Jolivet TSComputeRHSFunctionLinear - Evaluate the right-hand side via the user-provided Jacobian, for linear problems Udot = A U only 45570e4ef248SJed Brown 4558c3339decSBarry Smith Collective 45590e4ef248SJed Brown 45604165533cSJose E. Roman Input Parameters: 45610e4ef248SJed Brown + ts - time stepping context 45620e4ef248SJed Brown . t - time at which to evaluate 45630910c330SBarry Smith . U - state at which to evaluate 45640e4ef248SJed Brown - ctx - context 45650e4ef248SJed Brown 45664165533cSJose E. Roman Output Parameter: 4567dd8e379bSPierre Jolivet . F - right-hand side 45680e4ef248SJed Brown 45690e4ef248SJed Brown Level: intermediate 45700e4ef248SJed Brown 4571bcf0153eSBarry Smith Note: 4572dd8e379bSPierre Jolivet This function is intended to be passed to `TSSetRHSFunction()` to evaluate the right-hand side for linear problems. 4573bcf0153eSBarry Smith The matrix (and optionally the evaluation context) should be passed to `TSSetRHSJacobian()`. 45740e4ef248SJed Brown 45751cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSFunction()`, `TSSetRHSJacobian()`, `TSComputeRHSJacobianConstant()` 45760e4ef248SJed Brown @*/ 4577d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSFunctionLinear(TS ts, PetscReal t, Vec U, Vec F, void *ctx) 4578d71ae5a4SJacob Faibussowitsch { 45790e4ef248SJed Brown Mat Arhs, Brhs; 45800e4ef248SJed Brown 45810e4ef248SJed Brown PetscFunctionBegin; 45829566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts, &Arhs, &Brhs)); 45832663174eSHong Zhang /* undo the damage caused by shifting */ 45849566063dSJacob Faibussowitsch PetscCall(TSRecoverRHSJacobian(ts, Arhs, Brhs)); 45859566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts, t, U, Arhs, Brhs)); 45869566063dSJacob Faibussowitsch PetscCall(MatMult(Arhs, U, F)); 45873ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 45880e4ef248SJed Brown } 45890e4ef248SJed Brown 45900e4ef248SJed Brown /*@C 45910e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 45920e4ef248SJed Brown 4593c3339decSBarry Smith Collective 45940e4ef248SJed Brown 45954165533cSJose E. Roman Input Parameters: 45960e4ef248SJed Brown + ts - time stepping context 45970e4ef248SJed Brown . t - time at which to evaluate 45980910c330SBarry Smith . U - state at which to evaluate 45990e4ef248SJed Brown - ctx - context 46000e4ef248SJed Brown 46014165533cSJose E. Roman Output Parameters: 46020e4ef248SJed Brown + A - pointer to operator 460397bb3fdcSJose E. Roman - B - pointer to preconditioning matrix 46040e4ef248SJed Brown 46050e4ef248SJed Brown Level: intermediate 46060e4ef248SJed Brown 4607bcf0153eSBarry Smith Note: 4608bcf0153eSBarry Smith This function is intended to be passed to `TSSetRHSJacobian()` to evaluate the Jacobian for linear time-independent problems. 46090e4ef248SJed Brown 46101cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSFunction()`, `TSSetRHSJacobian()`, `TSComputeRHSFunctionLinear()` 46110e4ef248SJed Brown @*/ 4612d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSJacobianConstant(TS ts, PetscReal t, Vec U, Mat A, Mat B, void *ctx) 4613d71ae5a4SJacob Faibussowitsch { 46140e4ef248SJed Brown PetscFunctionBegin; 46153ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 46160e4ef248SJed Brown } 46170e4ef248SJed Brown 46180026cea9SSean Farley /*@C 46190026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 46200026cea9SSean Farley 4621c3339decSBarry Smith Collective 46220026cea9SSean Farley 46234165533cSJose E. Roman Input Parameters: 46240026cea9SSean Farley + ts - time stepping context 46250026cea9SSean Farley . t - time at which to evaluate 46260910c330SBarry Smith . U - state at which to evaluate 46270910c330SBarry Smith . Udot - time derivative of state vector 46280026cea9SSean Farley - ctx - context 46290026cea9SSean Farley 46304165533cSJose E. Roman Output Parameter: 46310026cea9SSean Farley . F - left hand side 46320026cea9SSean Farley 46330026cea9SSean Farley Level: intermediate 46340026cea9SSean Farley 46350026cea9SSean Farley Notes: 46360910c330SBarry 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 4637bcf0153eSBarry Smith user is required to write their own `TSComputeIFunction()`. 4638bcf0153eSBarry Smith This function is intended to be passed to `TSSetIFunction()` to evaluate the left hand side for linear problems. 4639bcf0153eSBarry Smith The matrix (and optionally the evaluation context) should be passed to `TSSetIJacobian()`. 46400026cea9SSean Farley 4641bcf0153eSBarry Smith Note that using this function is NOT equivalent to using `TSComputeRHSFunctionLinear()` since that solves Udot = A U 46429ae8fd06SBarry Smith 46431cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetIFunction()`, `TSSetIJacobian()`, `TSComputeIJacobianConstant()`, `TSComputeRHSFunctionLinear()` 46440026cea9SSean Farley @*/ 4645d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIFunctionLinear(TS ts, PetscReal t, Vec U, Vec Udot, Vec F, void *ctx) 4646d71ae5a4SJacob Faibussowitsch { 46470026cea9SSean Farley Mat A, B; 46480026cea9SSean Farley 46490026cea9SSean Farley PetscFunctionBegin; 46509566063dSJacob Faibussowitsch PetscCall(TSGetIJacobian(ts, &A, &B, NULL, NULL)); 46519566063dSJacob Faibussowitsch PetscCall(TSComputeIJacobian(ts, t, U, Udot, 1.0, A, B, PETSC_TRUE)); 46529566063dSJacob Faibussowitsch PetscCall(MatMult(A, Udot, F)); 46533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 46540026cea9SSean Farley } 46550026cea9SSean Farley 46560026cea9SSean Farley /*@C 46578434afd1SBarry Smith TSComputeIJacobianConstant - Reuses the matrix previously computed with the provided `TSIJacobianFn` for a semi-implicit DAE or ODE 46580026cea9SSean Farley 4659c3339decSBarry Smith Collective 46600026cea9SSean Farley 46614165533cSJose E. Roman Input Parameters: 46620026cea9SSean Farley + ts - time stepping context 46630026cea9SSean Farley . t - time at which to evaluate 46640910c330SBarry Smith . U - state at which to evaluate 46650910c330SBarry Smith . Udot - time derivative of state vector 46660026cea9SSean Farley . shift - shift to apply 46670026cea9SSean Farley - ctx - context 46680026cea9SSean Farley 46694165533cSJose E. Roman Output Parameters: 46700026cea9SSean Farley + A - pointer to operator 4671d1c5d1fcSBarry Smith - B - pointer to matrix from which the preconditioner is built (often `A`) 46720026cea9SSean Farley 4673b41af12eSJed Brown Level: advanced 46740026cea9SSean Farley 46750026cea9SSean Farley Notes: 4676bcf0153eSBarry Smith This function is intended to be passed to `TSSetIJacobian()` to evaluate the Jacobian for linear time-independent problems. 46770026cea9SSean Farley 4678b41af12eSJed Brown It is only appropriate for problems of the form 4679b41af12eSJed Brown 4680d1c5d1fcSBarry Smith $$ 4681d1c5d1fcSBarry Smith M \dot{U} = F(U,t) 4682d1c5d1fcSBarry Smith $$ 4683b41af12eSJed Brown 4684bcf0153eSBarry Smith where M is constant and F is non-stiff. The user must pass M to `TSSetIJacobian()`. The current implementation only 4685bcf0153eSBarry Smith works with IMEX time integration methods such as `TSROSW` and `TSARKIMEX`, since there is no support for de-constructing 4686b41af12eSJed Brown an implicit operator of the form 4687b41af12eSJed Brown 4688d1c5d1fcSBarry Smith $$ 4689d1c5d1fcSBarry Smith shift*M + J 4690d1c5d1fcSBarry Smith $$ 4691b41af12eSJed Brown 4692b41af12eSJed 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 4693b41af12eSJed Brown a copy of M or reassemble it when requested. 4694b41af12eSJed Brown 46951cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSROSW`, `TSARKIMEX`, `TSSetIFunction()`, `TSSetIJacobian()`, `TSComputeIFunctionLinear()` 46960026cea9SSean Farley @*/ 4697d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIJacobianConstant(TS ts, PetscReal t, Vec U, Vec Udot, PetscReal shift, Mat A, Mat B, void *ctx) 4698d71ae5a4SJacob Faibussowitsch { 46990026cea9SSean Farley PetscFunctionBegin; 47009566063dSJacob Faibussowitsch PetscCall(MatScale(A, shift / ts->ijacobian.shift)); 4701b41af12eSJed Brown ts->ijacobian.shift = shift; 47023ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 47030026cea9SSean Farley } 4704b41af12eSJed Brown 4705e817cc15SEmil Constantinescu /*@ 4706bcf0153eSBarry Smith TSGetEquationType - Gets the type of the equation that `TS` is solving. 4707e817cc15SEmil Constantinescu 4708e817cc15SEmil Constantinescu Not Collective 4709e817cc15SEmil Constantinescu 4710e817cc15SEmil Constantinescu Input Parameter: 4711bcf0153eSBarry Smith . ts - the `TS` context 4712e817cc15SEmil Constantinescu 4713e817cc15SEmil Constantinescu Output Parameter: 4714bcf0153eSBarry Smith . equation_type - see `TSEquationType` 4715e817cc15SEmil Constantinescu 4716e817cc15SEmil Constantinescu Level: beginner 4717e817cc15SEmil Constantinescu 47181cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetEquationType()`, `TSEquationType` 4719e817cc15SEmil Constantinescu @*/ 4720d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetEquationType(TS ts, TSEquationType *equation_type) 4721d71ae5a4SJacob Faibussowitsch { 4722e817cc15SEmil Constantinescu PetscFunctionBegin; 4723e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 47244f572ea9SToby Isaac PetscAssertPointer(equation_type, 2); 4725e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 47263ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4727e817cc15SEmil Constantinescu } 4728e817cc15SEmil Constantinescu 4729e817cc15SEmil Constantinescu /*@ 4730bcf0153eSBarry Smith TSSetEquationType - Sets the type of the equation that `TS` is solving. 4731e817cc15SEmil Constantinescu 4732e817cc15SEmil Constantinescu Not Collective 4733e817cc15SEmil Constantinescu 4734d8d19677SJose E. Roman Input Parameters: 4735bcf0153eSBarry Smith + ts - the `TS` context 4736bcf0153eSBarry Smith - equation_type - see `TSEquationType` 4737e817cc15SEmil Constantinescu 4738e817cc15SEmil Constantinescu Level: advanced 4739e817cc15SEmil Constantinescu 47401cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetEquationType()`, `TSEquationType` 4741e817cc15SEmil Constantinescu @*/ 4742d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetEquationType(TS ts, TSEquationType equation_type) 4743d71ae5a4SJacob Faibussowitsch { 4744e817cc15SEmil Constantinescu PetscFunctionBegin; 4745e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4746e817cc15SEmil Constantinescu ts->equation_type = equation_type; 47473ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4748e817cc15SEmil Constantinescu } 47490026cea9SSean Farley 47504af1b03aSJed Brown /*@ 4751bcf0153eSBarry Smith TSGetConvergedReason - Gets the reason the `TS` iteration was stopped. 47524af1b03aSJed Brown 47534af1b03aSJed Brown Not Collective 47544af1b03aSJed Brown 47554af1b03aSJed Brown Input Parameter: 4756bcf0153eSBarry Smith . ts - the `TS` context 47574af1b03aSJed Brown 47584af1b03aSJed Brown Output Parameter: 4759bcf0153eSBarry Smith . reason - negative value indicates diverged, positive value converged, see `TSConvergedReason` or the 47604af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 47614af1b03aSJed Brown 4762487e0bb9SJed Brown Level: beginner 47634af1b03aSJed Brown 4764bcf0153eSBarry Smith Note: 4765bcf0153eSBarry Smith Can only be called after the call to `TSSolve()` is complete. 47664af1b03aSJed Brown 47677d66147cSPierre Jolivet .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSConvergedReason` 47684af1b03aSJed Brown @*/ 4769d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetConvergedReason(TS ts, TSConvergedReason *reason) 4770d71ae5a4SJacob Faibussowitsch { 47714af1b03aSJed Brown PetscFunctionBegin; 47724af1b03aSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 47734f572ea9SToby Isaac PetscAssertPointer(reason, 2); 47744af1b03aSJed Brown *reason = ts->reason; 47753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 47764af1b03aSJed Brown } 47774af1b03aSJed Brown 4778d6ad946cSShri Abhyankar /*@ 4779bcf0153eSBarry Smith TSSetConvergedReason - Sets the reason for handling the convergence of `TSSolve()`. 4780d6ad946cSShri Abhyankar 47816b221cbeSPatrick Sanan Logically Collective; reason must contain common value 4782d6ad946cSShri Abhyankar 47836b221cbeSPatrick Sanan Input Parameters: 4784bcf0153eSBarry Smith + ts - the `TS` context 4785bcf0153eSBarry Smith - reason - negative value indicates diverged, positive value converged, see `TSConvergedReason` or the 4786d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 4787d6ad946cSShri Abhyankar 4788f5abba47SShri Abhyankar Level: advanced 4789d6ad946cSShri Abhyankar 4790bcf0153eSBarry Smith Note: 4791bcf0153eSBarry Smith Can only be called while `TSSolve()` is active. 4792d6ad946cSShri Abhyankar 47931cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSConvergedReason` 4794d6ad946cSShri Abhyankar @*/ 4795d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetConvergedReason(TS ts, TSConvergedReason reason) 4796d71ae5a4SJacob Faibussowitsch { 4797d6ad946cSShri Abhyankar PetscFunctionBegin; 4798d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4799d6ad946cSShri Abhyankar ts->reason = reason; 48003ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4801d6ad946cSShri Abhyankar } 4802d6ad946cSShri Abhyankar 4803cc708dedSBarry Smith /*@ 4804bcf0153eSBarry Smith TSGetSolveTime - Gets the time after a call to `TSSolve()` 4805cc708dedSBarry Smith 4806cc708dedSBarry Smith Not Collective 4807cc708dedSBarry Smith 4808cc708dedSBarry Smith Input Parameter: 4809bcf0153eSBarry Smith . ts - the `TS` context 4810cc708dedSBarry Smith 4811cc708dedSBarry Smith Output Parameter: 4812bcf0153eSBarry Smith . ftime - the final time. This time corresponds to the final time set with `TSSetMaxTime()` 4813cc708dedSBarry Smith 4814487e0bb9SJed Brown Level: beginner 4815cc708dedSBarry Smith 4816bcf0153eSBarry Smith Note: 4817bcf0153eSBarry Smith Can only be called after the call to `TSSolve()` is complete. 4818cc708dedSBarry Smith 48197d66147cSPierre Jolivet .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSConvergedReason` 4820cc708dedSBarry Smith @*/ 4821d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSolveTime(TS ts, PetscReal *ftime) 4822d71ae5a4SJacob Faibussowitsch { 4823cc708dedSBarry Smith PetscFunctionBegin; 4824cc708dedSBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 48254f572ea9SToby Isaac PetscAssertPointer(ftime, 2); 4826cc708dedSBarry Smith *ftime = ts->solvetime; 48273ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4828cc708dedSBarry Smith } 4829cc708dedSBarry Smith 48302c18e0fdSBarry Smith /*@ 48315ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 48329f67acb7SJed Brown used by the time integrator. 48339f67acb7SJed Brown 48349f67acb7SJed Brown Not Collective 48359f67acb7SJed Brown 48369f67acb7SJed Brown Input Parameter: 4837bcf0153eSBarry Smith . ts - `TS` context 48389f67acb7SJed Brown 48399f67acb7SJed Brown Output Parameter: 48409f67acb7SJed Brown . nits - number of nonlinear iterations 48419f67acb7SJed Brown 48429f67acb7SJed Brown Level: intermediate 48439f67acb7SJed Brown 4844195e9b02SBarry Smith Note: 4845bcf0153eSBarry Smith This counter is reset to zero for each successive call to `TSSolve()`. 4846bcf0153eSBarry Smith 48471cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSGetKSPIterations()` 48489f67acb7SJed Brown @*/ 4849d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSNESIterations(TS ts, PetscInt *nits) 4850d71ae5a4SJacob Faibussowitsch { 48519f67acb7SJed Brown PetscFunctionBegin; 48529f67acb7SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 48534f572ea9SToby Isaac PetscAssertPointer(nits, 2); 48545ef26d82SJed Brown *nits = ts->snes_its; 48553ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 48569f67acb7SJed Brown } 48579f67acb7SJed Brown 48589f67acb7SJed Brown /*@ 48595ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 48609f67acb7SJed Brown used by the time integrator. 48619f67acb7SJed Brown 48629f67acb7SJed Brown Not Collective 48639f67acb7SJed Brown 48649f67acb7SJed Brown Input Parameter: 4865bcf0153eSBarry Smith . ts - `TS` context 48669f67acb7SJed Brown 48679f67acb7SJed Brown Output Parameter: 48689f67acb7SJed Brown . lits - number of linear iterations 48699f67acb7SJed Brown 48709f67acb7SJed Brown Level: intermediate 48719f67acb7SJed Brown 4872bcf0153eSBarry Smith Note: 4873bcf0153eSBarry Smith This counter is reset to zero for each successive call to `TSSolve()`. 4874bcf0153eSBarry Smith 4875*bfe80ac4SPierre Jolivet .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSGetSNESIterations()` 48769f67acb7SJed Brown @*/ 4877d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetKSPIterations(TS ts, PetscInt *lits) 4878d71ae5a4SJacob Faibussowitsch { 48799f67acb7SJed Brown PetscFunctionBegin; 48809f67acb7SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 48814f572ea9SToby Isaac PetscAssertPointer(lits, 2); 48825ef26d82SJed Brown *lits = ts->ksp_its; 48833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 48849f67acb7SJed Brown } 48859f67acb7SJed Brown 4886cef5090cSJed Brown /*@ 4887cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 4888cef5090cSJed Brown 4889cef5090cSJed Brown Not Collective 4890cef5090cSJed Brown 4891cef5090cSJed Brown Input Parameter: 4892bcf0153eSBarry Smith . ts - `TS` context 4893cef5090cSJed Brown 4894cef5090cSJed Brown Output Parameter: 4895cef5090cSJed Brown . rejects - number of steps rejected 4896cef5090cSJed Brown 4897cef5090cSJed Brown Level: intermediate 4898cef5090cSJed Brown 4899bcf0153eSBarry Smith Note: 4900bcf0153eSBarry Smith This counter is reset to zero for each successive call to `TSSolve()`. 4901bcf0153eSBarry Smith 49021cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetSNESFailures()`, `TSSetMaxSNESFailures()`, `TSSetErrorIfStepFails()` 4903cef5090cSJed Brown @*/ 4904d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetStepRejections(TS ts, PetscInt *rejects) 4905d71ae5a4SJacob Faibussowitsch { 4906cef5090cSJed Brown PetscFunctionBegin; 4907cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 49084f572ea9SToby Isaac PetscAssertPointer(rejects, 2); 4909cef5090cSJed Brown *rejects = ts->reject; 49103ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4911cef5090cSJed Brown } 4912cef5090cSJed Brown 4913cef5090cSJed Brown /*@ 4914bcf0153eSBarry Smith TSGetSNESFailures - Gets the total number of failed `SNES` solves in a `TS` 4915cef5090cSJed Brown 4916cef5090cSJed Brown Not Collective 4917cef5090cSJed Brown 4918cef5090cSJed Brown Input Parameter: 4919bcf0153eSBarry Smith . ts - `TS` context 4920cef5090cSJed Brown 4921cef5090cSJed Brown Output Parameter: 4922cef5090cSJed Brown . fails - number of failed nonlinear solves 4923cef5090cSJed Brown 4924cef5090cSJed Brown Level: intermediate 4925cef5090cSJed Brown 4926bcf0153eSBarry Smith Note: 4927bcf0153eSBarry Smith This counter is reset to zero for each successive call to `TSSolve()`. 4928bcf0153eSBarry Smith 49291cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSSetMaxSNESFailures()` 4930cef5090cSJed Brown @*/ 4931d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSNESFailures(TS ts, PetscInt *fails) 4932d71ae5a4SJacob Faibussowitsch { 4933cef5090cSJed Brown PetscFunctionBegin; 4934cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 49354f572ea9SToby Isaac PetscAssertPointer(fails, 2); 4936cef5090cSJed Brown *fails = ts->num_snes_failures; 49373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4938cef5090cSJed Brown } 4939cef5090cSJed Brown 4940cef5090cSJed Brown /*@ 4941bcf0153eSBarry Smith TSSetMaxStepRejections - Sets the maximum number of step rejections before a time step fails 4942cef5090cSJed Brown 4943cef5090cSJed Brown Not Collective 4944cef5090cSJed Brown 4945d8d19677SJose E. Roman Input Parameters: 4946bcf0153eSBarry Smith + ts - `TS` context 494709cb0f53SBarry Smith - rejects - maximum number of rejected steps, pass `PETSC_UNLIMITED` for unlimited 4948cef5090cSJed Brown 4949cef5090cSJed Brown Options Database Key: 4950cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 4951cef5090cSJed Brown 4952cef5090cSJed Brown Level: intermediate 4953cef5090cSJed Brown 495409cb0f53SBarry Smith Developer Note: 495509cb0f53SBarry Smith The options database name is incorrect. 495609cb0f53SBarry Smith 49571cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxSNESFailures()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `TSSetErrorIfStepFails()`, `TSGetConvergedReason()` 4958cef5090cSJed Brown @*/ 4959d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxStepRejections(TS ts, PetscInt rejects) 4960d71ae5a4SJacob Faibussowitsch { 4961cef5090cSJed Brown PetscFunctionBegin; 4962cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 496309cb0f53SBarry Smith if (rejects == PETSC_UNLIMITED || rejects == -1) { 496409cb0f53SBarry Smith ts->max_reject = PETSC_UNLIMITED; 496509cb0f53SBarry Smith } else { 496609cb0f53SBarry Smith PetscCheck(rejects >= 0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Cannot have a negative maximum number of rejections"); 4967cef5090cSJed Brown ts->max_reject = rejects; 496809cb0f53SBarry Smith } 49693ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4970cef5090cSJed Brown } 4971cef5090cSJed Brown 4972cef5090cSJed Brown /*@ 4973bcf0153eSBarry Smith TSSetMaxSNESFailures - Sets the maximum number of failed `SNES` solves 4974cef5090cSJed Brown 4975cef5090cSJed Brown Not Collective 4976cef5090cSJed Brown 4977d8d19677SJose E. Roman Input Parameters: 4978bcf0153eSBarry Smith + ts - `TS` context 497909cb0f53SBarry Smith - fails - maximum number of failed nonlinear solves, pass `PETSC_UNLIMITED` to allow any number of failures. 4980cef5090cSJed Brown 4981cef5090cSJed Brown Options Database Key: 4982cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 4983cef5090cSJed Brown 4984cef5090cSJed Brown Level: intermediate 4985cef5090cSJed Brown 49861cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `SNESGetConvergedReason()`, `TSGetConvergedReason()` 4987cef5090cSJed Brown @*/ 4988d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxSNESFailures(TS ts, PetscInt fails) 4989d71ae5a4SJacob Faibussowitsch { 4990cef5090cSJed Brown PetscFunctionBegin; 4991cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 499209cb0f53SBarry Smith if (fails == PETSC_UNLIMITED || fails == -1) { 499309cb0f53SBarry Smith ts->max_snes_failures = PETSC_UNLIMITED; 499409cb0f53SBarry Smith } else { 499509cb0f53SBarry Smith PetscCheck(fails >= 0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Cannot have a negative maximum number of failures"); 4996cef5090cSJed Brown ts->max_snes_failures = fails; 499709cb0f53SBarry Smith } 49983ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4999cef5090cSJed Brown } 5000cef5090cSJed Brown 5001cef5090cSJed Brown /*@ 5002195e9b02SBarry Smith TSSetErrorIfStepFails - Immediately error if no step succeeds during `TSSolve()` 5003cef5090cSJed Brown 5004cef5090cSJed Brown Not Collective 5005cef5090cSJed Brown 5006d8d19677SJose E. Roman Input Parameters: 5007bcf0153eSBarry Smith + ts - `TS` context 5008bcf0153eSBarry Smith - err - `PETSC_TRUE` to error if no step succeeds, `PETSC_FALSE` to return without failure 5009cef5090cSJed Brown 5010cef5090cSJed Brown Options Database Key: 5011cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 5012cef5090cSJed Brown 5013cef5090cSJed Brown Level: intermediate 5014cef5090cSJed Brown 501542747ad1SJacob Faibussowitsch .seealso: [](ch_ts), `TS`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `TSGetConvergedReason()` 5016cef5090cSJed Brown @*/ 5017d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetErrorIfStepFails(TS ts, PetscBool err) 5018d71ae5a4SJacob Faibussowitsch { 5019cef5090cSJed Brown PetscFunctionBegin; 5020cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5021cef5090cSJed Brown ts->errorifstepfailed = err; 50223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5023cef5090cSJed Brown } 5024cef5090cSJed Brown 502584df9cb4SJed Brown /*@ 5026552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 502784df9cb4SJed Brown 50288f14a041SBarry Smith Collective if controller has not yet been created 502984df9cb4SJed Brown 50304165533cSJose E. Roman Input Parameter: 5031ed81e22dSJed Brown . ts - time stepping context 503284df9cb4SJed Brown 50334165533cSJose E. Roman Output Parameter: 5034ed81e22dSJed Brown . adapt - adaptive controller 503584df9cb4SJed Brown 503684df9cb4SJed Brown Level: intermediate 503784df9cb4SJed Brown 50381cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSAdapt`, `TSAdaptSetType()`, `TSAdaptChoose()` 503984df9cb4SJed Brown @*/ 5040d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetAdapt(TS ts, TSAdapt *adapt) 5041d71ae5a4SJacob Faibussowitsch { 504284df9cb4SJed Brown PetscFunctionBegin; 504384df9cb4SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 50444f572ea9SToby Isaac PetscAssertPointer(adapt, 2); 504584df9cb4SJed Brown if (!ts->adapt) { 50469566063dSJacob Faibussowitsch PetscCall(TSAdaptCreate(PetscObjectComm((PetscObject)ts), &ts->adapt)); 50479566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)ts->adapt, (PetscObject)ts, 1)); 504884df9cb4SJed Brown } 5049bec58848SLisandro Dalcin *adapt = ts->adapt; 50503ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 505184df9cb4SJed Brown } 5052d6ebe24aSShri Abhyankar 50531c3436cfSJed Brown /*@ 5054195e9b02SBarry Smith TSSetTolerances - Set tolerances for local truncation error when using an adaptive controller 50551c3436cfSJed Brown 50561c3436cfSJed Brown Logically Collective 50571c3436cfSJed Brown 50584165533cSJose E. Roman Input Parameters: 50591c3436cfSJed Brown + ts - time integration context 506009cb0f53SBarry Smith . atol - scalar absolute tolerances 506109cb0f53SBarry Smith . vatol - vector of absolute tolerances or `NULL`, used in preference to `atol` if present 506209cb0f53SBarry Smith . rtol - scalar relative tolerances 506309cb0f53SBarry Smith - vrtol - vector of relative tolerances or `NULL`, used in preference to `rtol` if present 50641c3436cfSJed Brown 5065195e9b02SBarry Smith Options Database Keys: 5066a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 506767b8a455SSatish Balay - -ts_atol <atol> - Absolute tolerance for local truncation error 5068a3cdaa26SBarry Smith 5069bcf0153eSBarry Smith Level: beginner 5070bcf0153eSBarry Smith 50713ff766beSShri Abhyankar Notes: 507209cb0f53SBarry Smith `PETSC_CURRENT` or `PETSC_DETERMINE` may be used for `atol` or `rtol` to indicate the current value 507309cb0f53SBarry Smith or the default value from when the object's type was set. 507409cb0f53SBarry Smith 50753ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 50763ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 50773ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 50783ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 50793ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 50803ff766beSShri Abhyankar differential variables. 50813ff766beSShri Abhyankar 508209cb0f53SBarry Smith Fortran Note: 508309cb0f53SBarry Smith Use `PETSC_CURRENT_INTEGER` or `PETSC_DETERMINE_INTEGER`. 508409cb0f53SBarry Smith 50851cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSAdapt`, `TSErrorWeightedNorm()`, `TSGetTolerances()` 50861c3436cfSJed Brown @*/ 5087d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTolerances(TS ts, PetscReal atol, Vec vatol, PetscReal rtol, Vec vrtol) 5088d71ae5a4SJacob Faibussowitsch { 50891c3436cfSJed Brown PetscFunctionBegin; 509009cb0f53SBarry Smith if (atol == (PetscReal)PETSC_DETERMINE) { 5091dd460d27SBarry Smith ts->atol = ts->default_atol; 509209cb0f53SBarry Smith } else if (atol != (PetscReal)PETSC_CURRENT) { 509309cb0f53SBarry Smith PetscCheck(atol >= 0.0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Absolute tolerance %g must be non-negative", (double)atol); 509409cb0f53SBarry Smith ts->atol = atol; 509509cb0f53SBarry Smith } 509609cb0f53SBarry Smith 50971c3436cfSJed Brown if (vatol) { 50989566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)vatol)); 50999566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vatol)); 51001c3436cfSJed Brown ts->vatol = vatol; 51011c3436cfSJed Brown } 510209cb0f53SBarry Smith 510309cb0f53SBarry Smith if (rtol == (PetscReal)PETSC_DETERMINE) { 5104dd460d27SBarry Smith ts->rtol = ts->default_rtol; 510509cb0f53SBarry Smith } else if (rtol != (PetscReal)PETSC_CURRENT) { 510609cb0f53SBarry Smith PetscCheck(rtol >= 0.0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Relative tolerance %g must be non-negative", (double)rtol); 510709cb0f53SBarry Smith ts->rtol = rtol; 510809cb0f53SBarry Smith } 510909cb0f53SBarry Smith 51101c3436cfSJed Brown if (vrtol) { 51119566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)vrtol)); 51129566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vrtol)); 51131c3436cfSJed Brown ts->vrtol = vrtol; 51141c3436cfSJed Brown } 51153ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 51161c3436cfSJed Brown } 51171c3436cfSJed Brown 5118c5033834SJed Brown /*@ 5119c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 5120c5033834SJed Brown 5121c5033834SJed Brown Logically Collective 5122c5033834SJed Brown 51234165533cSJose E. Roman Input Parameter: 5124c5033834SJed Brown . ts - time integration context 5125c5033834SJed Brown 51264165533cSJose E. Roman Output Parameters: 5127195e9b02SBarry Smith + atol - scalar absolute tolerances, `NULL` to ignore 5128195e9b02SBarry Smith . vatol - vector of absolute tolerances, `NULL` to ignore 5129195e9b02SBarry Smith . rtol - scalar relative tolerances, `NULL` to ignore 5130195e9b02SBarry Smith - vrtol - vector of relative tolerances, `NULL` to ignore 5131c5033834SJed Brown 5132c5033834SJed Brown Level: beginner 5133c5033834SJed Brown 51341cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSAdapt`, `TSErrorWeightedNorm()`, `TSSetTolerances()` 5135c5033834SJed Brown @*/ 5136d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTolerances(TS ts, PetscReal *atol, Vec *vatol, PetscReal *rtol, Vec *vrtol) 5137d71ae5a4SJacob Faibussowitsch { 5138c5033834SJed Brown PetscFunctionBegin; 5139c5033834SJed Brown if (atol) *atol = ts->atol; 5140c5033834SJed Brown if (vatol) *vatol = ts->vatol; 5141c5033834SJed Brown if (rtol) *rtol = ts->rtol; 5142c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 51433ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5144c5033834SJed Brown } 5145c5033834SJed Brown 51469c6b16b5SShri Abhyankar /*@ 51478a175baeSEmil Constantinescu TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances 51481c3436cfSJed Brown 5149c3339decSBarry Smith Collective 51501c3436cfSJed Brown 51514165533cSJose E. Roman Input Parameters: 51521c3436cfSJed Brown + ts - time stepping context 5153a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5154a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 5155bcf0153eSBarry Smith - wnormtype - norm type, either `NORM_2` or `NORM_INFINITY` 51567619abb3SShri 51574165533cSJose E. Roman Output Parameters: 5158a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 51598a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 5160a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 5161a4868fbcSLisandro Dalcin 5162bcf0153eSBarry Smith Options Database Key: 5163a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 5164a4868fbcSLisandro Dalcin 51651c3436cfSJed Brown Level: developer 51661c3436cfSJed Brown 51678c38e02aSSatish Balay .seealso: [](ch_ts), `TS`, `VecErrorWeightedNorms()`, `TSErrorWeightedENorm()` 51681c3436cfSJed Brown @*/ 5169d71ae5a4SJacob Faibussowitsch PetscErrorCode TSErrorWeightedNorm(TS ts, Vec U, Vec Y, NormType wnormtype, PetscReal *norm, PetscReal *norma, PetscReal *normr) 5170d71ae5a4SJacob Faibussowitsch { 5171037d63e4SStefano Zampini PetscInt norma_loc, norm_loc, normr_loc; 51728a175baeSEmil Constantinescu 51738a175baeSEmil Constantinescu PetscFunctionBegin; 51748a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5175037d63e4SStefano 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)); 5176037d63e4SStefano Zampini if (wnormtype == NORM_2) { 5177037d63e4SStefano Zampini if (norm_loc) *norm = PetscSqrtReal(PetscSqr(*norm) / norm_loc); 5178037d63e4SStefano Zampini if (norma_loc) *norma = PetscSqrtReal(PetscSqr(*norma) / norma_loc); 5179037d63e4SStefano Zampini if (normr_loc) *normr = PetscSqrtReal(PetscSqr(*normr) / normr_loc); 51808a175baeSEmil Constantinescu } 51813c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norm"); 51823c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norma"); 51833c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in normr"); 51843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 51858a175baeSEmil Constantinescu } 51868a175baeSEmil Constantinescu 51878a175baeSEmil Constantinescu /*@ 51888a175baeSEmil Constantinescu TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances 51898a175baeSEmil Constantinescu 5190c3339decSBarry Smith Collective 51918a175baeSEmil Constantinescu 51924165533cSJose E. Roman Input Parameters: 51938a175baeSEmil Constantinescu + ts - time stepping context 51948a175baeSEmil Constantinescu . E - error vector 51958a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 51968a175baeSEmil Constantinescu . Y - state vector, previous time step 5197bcf0153eSBarry Smith - wnormtype - norm type, either `NORM_2` or `NORM_INFINITY` 51988a175baeSEmil Constantinescu 51994165533cSJose E. Roman Output Parameters: 5200a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 52018a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 5202a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 52038a175baeSEmil Constantinescu 5204bcf0153eSBarry Smith Options Database Key: 52058a175baeSEmil Constantinescu . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 52068a175baeSEmil Constantinescu 52078a175baeSEmil Constantinescu Level: developer 52088a175baeSEmil Constantinescu 52098c38e02aSSatish Balay .seealso: [](ch_ts), `TS`, `VecErrorWeightedNorms()`, `TSErrorWeightedNorm()` 52108a175baeSEmil Constantinescu @*/ 5211d71ae5a4SJacob Faibussowitsch PetscErrorCode TSErrorWeightedENorm(TS ts, Vec E, Vec U, Vec Y, NormType wnormtype, PetscReal *norm, PetscReal *norma, PetscReal *normr) 5212d71ae5a4SJacob Faibussowitsch { 5213037d63e4SStefano Zampini PetscInt norma_loc, norm_loc, normr_loc; 5214037d63e4SStefano Zampini 52158a175baeSEmil Constantinescu PetscFunctionBegin; 5216037d63e4SStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5217037d63e4SStefano 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)); 5218037d63e4SStefano Zampini if (wnormtype == NORM_2) { 5219037d63e4SStefano Zampini if (norm_loc) *norm = PetscSqrtReal(PetscSqr(*norm) / norm_loc); 5220037d63e4SStefano Zampini if (norma_loc) *norma = PetscSqrtReal(PetscSqr(*norma) / norma_loc); 5221037d63e4SStefano Zampini if (normr_loc) *normr = PetscSqrtReal(PetscSqr(*normr) / normr_loc); 5222037d63e4SStefano Zampini } 5223037d63e4SStefano Zampini PetscCheck(!PetscIsInfOrNanScalar(*norm), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norm"); 5224037d63e4SStefano Zampini PetscCheck(!PetscIsInfOrNanScalar(*norma), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norma"); 5225037d63e4SStefano Zampini PetscCheck(!PetscIsInfOrNanScalar(*normr), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in normr"); 52263ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 52278a175baeSEmil Constantinescu } 52288a175baeSEmil Constantinescu 52298d59e960SJed Brown /*@ 52308d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 52318d59e960SJed Brown 5232c3339decSBarry Smith Logically Collective 52338d59e960SJed Brown 52344165533cSJose E. Roman Input Parameters: 52358d59e960SJed Brown + ts - time stepping context 52368d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 52378d59e960SJed Brown 52388d59e960SJed Brown Note: 52398d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 52408d59e960SJed Brown 52418d59e960SJed Brown Level: intermediate 52428d59e960SJed Brown 52431cc06b55SBarry Smith .seealso: [](ch_ts), `TSGetCFLTime()`, `TSADAPTCFL` 52448d59e960SJed Brown @*/ 5245d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetCFLTimeLocal(TS ts, PetscReal cfltime) 5246d71ae5a4SJacob Faibussowitsch { 52478d59e960SJed Brown PetscFunctionBegin; 52488d59e960SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 52498d59e960SJed Brown ts->cfltime_local = cfltime; 52508d59e960SJed Brown ts->cfltime = -1.; 52513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 52528d59e960SJed Brown } 52538d59e960SJed Brown 52548d59e960SJed Brown /*@ 52558d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 52568d59e960SJed Brown 5257c3339decSBarry Smith Collective 52588d59e960SJed Brown 52594165533cSJose E. Roman Input Parameter: 52608d59e960SJed Brown . ts - time stepping context 52618d59e960SJed Brown 52624165533cSJose E. Roman Output Parameter: 52638d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 52648d59e960SJed Brown 52658d59e960SJed Brown Level: advanced 52668d59e960SJed Brown 52671cc06b55SBarry Smith .seealso: [](ch_ts), `TSSetCFLTimeLocal()` 52688d59e960SJed Brown @*/ 5269d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetCFLTime(TS ts, PetscReal *cfltime) 5270d71ae5a4SJacob Faibussowitsch { 52718d59e960SJed Brown PetscFunctionBegin; 5272462c564dSBarry Smith if (ts->cfltime < 0) PetscCallMPI(MPIU_Allreduce(&ts->cfltime_local, &ts->cfltime, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)ts))); 52738d59e960SJed Brown *cfltime = ts->cfltime; 52743ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 52758d59e960SJed Brown } 52768d59e960SJed Brown 5277d6ebe24aSShri Abhyankar /*@ 5278d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 5279d6ebe24aSShri Abhyankar 5280d6ebe24aSShri Abhyankar Input Parameters: 5281bcf0153eSBarry Smith + ts - the `TS` context. 5282d6ebe24aSShri Abhyankar . xl - lower bound. 5283a2b725a8SWilliam Gropp - xu - upper bound. 5284d6ebe24aSShri Abhyankar 52852bd2b0e6SSatish Balay Level: advanced 52862bd2b0e6SSatish Balay 5287bcf0153eSBarry Smith Note: 5288bcf0153eSBarry Smith If this routine is not called then the lower and upper bounds are set to 5289bcf0153eSBarry Smith `PETSC_NINFINITY` and `PETSC_INFINITY` respectively during `SNESSetUp()`. 5290bcf0153eSBarry Smith 52911cc06b55SBarry Smith .seealso: [](ch_ts), `TS` 5292d6ebe24aSShri Abhyankar @*/ 5293d71ae5a4SJacob Faibussowitsch PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 5294d71ae5a4SJacob Faibussowitsch { 5295d6ebe24aSShri Abhyankar SNES snes; 5296d6ebe24aSShri Abhyankar 5297d6ebe24aSShri Abhyankar PetscFunctionBegin; 52989566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 52999566063dSJacob Faibussowitsch PetscCall(SNESVISetVariableBounds(snes, xl, xu)); 53003ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5301d6ebe24aSShri Abhyankar } 5302d6ebe24aSShri Abhyankar 5303f9c1d6abSBarry Smith /*@ 5304f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 5305f9c1d6abSBarry Smith 5306c3339decSBarry Smith Collective 5307f9c1d6abSBarry Smith 5308f9c1d6abSBarry Smith Input Parameters: 5309bcf0153eSBarry Smith + ts - the `TS` context 53102fe279fdSBarry Smith . xr - real part of input argument 53112fe279fdSBarry Smith - xi - imaginary part of input argument 5312f9c1d6abSBarry Smith 5313f9c1d6abSBarry Smith Output Parameters: 53142fe279fdSBarry Smith + yr - real part of function value 53152fe279fdSBarry Smith - yi - imaginary part of function value 5316f9c1d6abSBarry Smith 5317f9c1d6abSBarry Smith Level: developer 5318f9c1d6abSBarry Smith 53191cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSFunction()`, `TSComputeIFunction()` 5320f9c1d6abSBarry Smith @*/ 5321d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeLinearStability(TS ts, PetscReal xr, PetscReal xi, PetscReal *yr, PetscReal *yi) 5322d71ae5a4SJacob Faibussowitsch { 5323f9c1d6abSBarry Smith PetscFunctionBegin; 5324f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5325dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, linearstability, xr, xi, yr, yi); 53263ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5327f9c1d6abSBarry Smith } 532824655328SShri 532924655328SShri /*@ 5330dcb233daSLisandro Dalcin TSRestartStep - Flags the solver to restart the next step 5331dcb233daSLisandro Dalcin 5332c3339decSBarry Smith Collective 5333dcb233daSLisandro Dalcin 5334dcb233daSLisandro Dalcin Input Parameter: 5335bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 5336dcb233daSLisandro Dalcin 5337dcb233daSLisandro Dalcin Level: advanced 5338dcb233daSLisandro Dalcin 5339dcb233daSLisandro Dalcin Notes: 5340bcf0153eSBarry Smith Multistep methods like `TSBDF` or Runge-Kutta methods with FSAL property require restarting the solver in the event of 5341dcb233daSLisandro Dalcin discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution 5342dcb233daSLisandro Dalcin vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For 5343bcf0153eSBarry Smith the sake of correctness and maximum safety, users are expected to call `TSRestart()` whenever they introduce 5344dcb233daSLisandro Dalcin discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with 5345dcb233daSLisandro Dalcin discontinuous source terms). 5346dcb233daSLisandro Dalcin 53471cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSBDF`, `TSSolve()`, `TSSetPreStep()`, `TSSetPostStep()` 5348dcb233daSLisandro Dalcin @*/ 5349d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRestartStep(TS ts) 5350d71ae5a4SJacob Faibussowitsch { 5351dcb233daSLisandro Dalcin PetscFunctionBegin; 5352dcb233daSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5353dcb233daSLisandro Dalcin ts->steprestart = PETSC_TRUE; 53543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5355dcb233daSLisandro Dalcin } 5356dcb233daSLisandro Dalcin 5357dcb233daSLisandro Dalcin /*@ 535824655328SShri TSRollBack - Rolls back one time step 535924655328SShri 5360c3339decSBarry Smith Collective 536124655328SShri 536224655328SShri Input Parameter: 5363bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 536424655328SShri 536524655328SShri Level: advanced 536624655328SShri 53679dc50cb5SStefano Zampini .seealso: [](ch_ts), `TS`, `TSGetStepRollBack()`, `TSCreate()`, `TSSetUp()`, `TSDestroy()`, `TSSolve()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSInterpolate()` 536824655328SShri @*/ 5369d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRollBack(TS ts) 5370d71ae5a4SJacob Faibussowitsch { 537124655328SShri PetscFunctionBegin; 537224655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 53733c633725SBarry Smith PetscCheck(!ts->steprollback, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "TSRollBack already called"); 5374c61711c8SStefano Zampini PetscTryTypeMethod(ts, rollback); 5375c61711c8SStefano Zampini PetscCall(VecCopy(ts->vec_sol0, ts->vec_sol)); 537624655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 537724655328SShri ts->ptime = ts->ptime_prev; 5378be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime_prev_rollback; 53792808aa04SLisandro Dalcin ts->steps--; 5380b3de5cdeSLisandro Dalcin ts->steprollback = PETSC_TRUE; 53813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 538224655328SShri } 5383aeb4809dSShri Abhyankar 5384ff22ae23SHong Zhang /*@ 53859dc50cb5SStefano Zampini TSGetStepRollBack - Get the internal flag indicating if you are rolling back a step 53869dc50cb5SStefano Zampini 53879dc50cb5SStefano Zampini Not collective 53889dc50cb5SStefano Zampini 53899dc50cb5SStefano Zampini Input Parameter: 53909dc50cb5SStefano Zampini . ts - the `TS` context obtained from `TSCreate()` 53919dc50cb5SStefano Zampini 53929dc50cb5SStefano Zampini Output Parameter: 53939dc50cb5SStefano Zampini . flg - the rollback flag 53949dc50cb5SStefano Zampini 53959dc50cb5SStefano Zampini Level: advanced 53969dc50cb5SStefano Zampini 53979dc50cb5SStefano Zampini .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSRollBack()` 53989dc50cb5SStefano Zampini @*/ 53999dc50cb5SStefano Zampini PetscErrorCode TSGetStepRollBack(TS ts, PetscBool *flg) 54009dc50cb5SStefano Zampini { 54019dc50cb5SStefano Zampini PetscFunctionBegin; 54029dc50cb5SStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 54039dc50cb5SStefano Zampini PetscAssertPointer(flg, 2); 54049dc50cb5SStefano Zampini *flg = ts->steprollback; 54059dc50cb5SStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 54069dc50cb5SStefano Zampini } 54079dc50cb5SStefano Zampini 54089dc50cb5SStefano Zampini /*@ 5409c6bf8827SStefano Zampini TSGetStepResize - Get the internal flag indicating if the current step is after a resize. 5410c6bf8827SStefano Zampini 5411c6bf8827SStefano Zampini Not collective 5412c6bf8827SStefano Zampini 5413c6bf8827SStefano Zampini Input Parameter: 5414c6bf8827SStefano Zampini . ts - the `TS` context obtained from `TSCreate()` 5415c6bf8827SStefano Zampini 5416c6bf8827SStefano Zampini Output Parameter: 5417c6bf8827SStefano Zampini . flg - the resize flag 5418c6bf8827SStefano Zampini 5419c6bf8827SStefano Zampini Level: advanced 5420c6bf8827SStefano Zampini 5421c6bf8827SStefano Zampini .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSSetResize()` 5422c6bf8827SStefano Zampini @*/ 5423c6bf8827SStefano Zampini PetscErrorCode TSGetStepResize(TS ts, PetscBool *flg) 5424c6bf8827SStefano Zampini { 5425c6bf8827SStefano Zampini PetscFunctionBegin; 5426c6bf8827SStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5427c6bf8827SStefano Zampini PetscAssertPointer(flg, 2); 5428c6bf8827SStefano Zampini *flg = ts->stepresize; 5429c6bf8827SStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 5430c6bf8827SStefano Zampini } 5431c6bf8827SStefano Zampini 5432c6bf8827SStefano Zampini /*@ 5433ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 5434ff22ae23SHong Zhang 5435ff22ae23SHong Zhang Input Parameter: 5436bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 5437ff22ae23SHong Zhang 54380429704eSStefano Zampini Output Parameters: 54390429704eSStefano Zampini + ns - the number of stages 54400429704eSStefano Zampini - Y - the current stage vectors 54410429704eSStefano Zampini 5442ff22ae23SHong Zhang Level: advanced 5443ff22ae23SHong Zhang 5444bcf0153eSBarry Smith Note: 5445195e9b02SBarry Smith Both `ns` and `Y` can be `NULL`. 54460429704eSStefano Zampini 54471cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSCreate()` 5448ff22ae23SHong Zhang @*/ 5449d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetStages(TS ts, PetscInt *ns, Vec **Y) 5450d71ae5a4SJacob Faibussowitsch { 5451ff22ae23SHong Zhang PetscFunctionBegin; 5452ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 54534f572ea9SToby Isaac if (ns) PetscAssertPointer(ns, 2); 54544f572ea9SToby Isaac if (Y) PetscAssertPointer(Y, 3); 54550429704eSStefano Zampini if (!ts->ops->getstages) { 54560429704eSStefano Zampini if (ns) *ns = 0; 54570429704eSStefano Zampini if (Y) *Y = NULL; 5458dbbe0bcdSBarry Smith } else PetscUseTypeMethod(ts, getstages, ns, Y); 54593ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5460ff22ae23SHong Zhang } 5461ff22ae23SHong Zhang 5462847ff0e1SMatthew G. Knepley /*@C 5463847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 5464847ff0e1SMatthew G. Knepley 5465c3339decSBarry Smith Collective 5466847ff0e1SMatthew G. Knepley 5467847ff0e1SMatthew G. Knepley Input Parameters: 5468bcf0153eSBarry Smith + ts - the `TS` context 5469847ff0e1SMatthew G. Knepley . t - current timestep 5470847ff0e1SMatthew G. Knepley . U - state vector 5471847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 5472847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 5473847ff0e1SMatthew G. Knepley - ctx - an optional user context 5474847ff0e1SMatthew G. Knepley 5475847ff0e1SMatthew G. Knepley Output Parameters: 5476847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 5477195e9b02SBarry Smith - B - newly computed Jacobian matrix to use with preconditioner (generally the same as `J`) 5478847ff0e1SMatthew G. Knepley 5479847ff0e1SMatthew G. Knepley Level: intermediate 5480847ff0e1SMatthew G. Knepley 5481847ff0e1SMatthew G. Knepley Notes: 5482847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 5483847ff0e1SMatthew G. Knepley 5484847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 5485847ff0e1SMatthew G. Knepley 5486847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 5487847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 5488847ff0e1SMatthew G. Knepley 5489bcf0153eSBarry Smith This will first try to get the coloring from the `DM`. If the `DM` type has no coloring 5490847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 5491847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 5492847ff0e1SMatthew G. Knepley 54931cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetIJacobian()`, `MatFDColoringCreate()`, `MatFDColoringSetFunction()` 5494847ff0e1SMatthew G. Knepley @*/ 5495d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIJacobianDefaultColor(TS ts, PetscReal t, Vec U, Vec Udot, PetscReal shift, Mat J, Mat B, void *ctx) 5496d71ae5a4SJacob Faibussowitsch { 5497847ff0e1SMatthew G. Knepley SNES snes; 5498847ff0e1SMatthew G. Knepley MatFDColoring color; 5499847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 5500847ff0e1SMatthew G. Knepley 5501847ff0e1SMatthew G. Knepley PetscFunctionBegin; 55029566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject)ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL)); 55039566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)B, "TSMatFDColoring", (PetscObject *)&color)); 5504847ff0e1SMatthew G. Knepley if (!color) { 5505847ff0e1SMatthew G. Knepley DM dm; 5506847ff0e1SMatthew G. Knepley ISColoring iscoloring; 5507847ff0e1SMatthew G. Knepley 55089566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 55099566063dSJacob Faibussowitsch PetscCall(DMHasColoring(dm, &hascolor)); 5510847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 55119566063dSJacob Faibussowitsch PetscCall(DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring)); 55129566063dSJacob Faibussowitsch PetscCall(MatFDColoringCreate(B, iscoloring, &color)); 55139566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFunction(color, (PetscErrorCode (*)(void))SNESTSFormFunction, (void *)ts)); 55149566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFromOptions(color)); 55159566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetUp(B, iscoloring, color)); 55169566063dSJacob Faibussowitsch PetscCall(ISColoringDestroy(&iscoloring)); 5517847ff0e1SMatthew G. Knepley } else { 5518847ff0e1SMatthew G. Knepley MatColoring mc; 5519847ff0e1SMatthew G. Knepley 55209566063dSJacob Faibussowitsch PetscCall(MatColoringCreate(B, &mc)); 55219566063dSJacob Faibussowitsch PetscCall(MatColoringSetDistance(mc, 2)); 55229566063dSJacob Faibussowitsch PetscCall(MatColoringSetType(mc, MATCOLORINGSL)); 55239566063dSJacob Faibussowitsch PetscCall(MatColoringSetFromOptions(mc)); 55249566063dSJacob Faibussowitsch PetscCall(MatColoringApply(mc, &iscoloring)); 55259566063dSJacob Faibussowitsch PetscCall(MatColoringDestroy(&mc)); 55269566063dSJacob Faibussowitsch PetscCall(MatFDColoringCreate(B, iscoloring, &color)); 55279566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFunction(color, (PetscErrorCode (*)(void))SNESTSFormFunction, (void *)ts)); 55289566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFromOptions(color)); 55299566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetUp(B, iscoloring, color)); 55309566063dSJacob Faibussowitsch PetscCall(ISColoringDestroy(&iscoloring)); 5531847ff0e1SMatthew G. Knepley } 55329566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject)B, "TSMatFDColoring", (PetscObject)color)); 55339566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)color)); 5534847ff0e1SMatthew G. Knepley } 55359566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 55369566063dSJacob Faibussowitsch PetscCall(MatFDColoringApply(B, color, U, snes)); 5537847ff0e1SMatthew G. Knepley if (J != B) { 55389566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 55399566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 5540847ff0e1SMatthew G. Knepley } 55413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5542847ff0e1SMatthew G. Knepley } 554393b34091SDebojyoti Ghosh 5544b43aa488SJacob Faibussowitsch /*@C 55456bc98fa9SBarry Smith TSSetFunctionDomainError - Set a function that tests if the current state vector is valid 5546cb9d8021SPierre Barbier de Reuille 5547cb9d8021SPierre Barbier de Reuille Input Parameters: 5548bcf0153eSBarry Smith + ts - the `TS` context 5549bcf0153eSBarry Smith - func - function called within `TSFunctionDomainError()` 55506bc98fa9SBarry Smith 555120f4b53cSBarry Smith Calling sequence of `func`: 55528847d985SBarry Smith + ts - the `TS` context 55536bc98fa9SBarry Smith . time - the current time (of the stage) 55546bc98fa9SBarry Smith . state - the state to check if it is valid 5555a144a63fSStefano Zampini - accept - (output parameter) `PETSC_FALSE` if the state is not acceptable, `PETSC_TRUE` if acceptable 5556cb9d8021SPierre Barbier de Reuille 5557cb9d8021SPierre Barbier de Reuille Level: intermediate 5558cb9d8021SPierre Barbier de Reuille 55596bc98fa9SBarry Smith Notes: 55606bc98fa9SBarry Smith If an implicit ODE solver is being used then, in addition to providing this routine, the 5561bcf0153eSBarry Smith user's code should call `SNESSetFunctionDomainError()` when domain errors occur during 5562bcf0153eSBarry Smith function evaluations where the functions are provided by `TSSetIFunction()` or `TSSetRHSFunction()`. 5563bcf0153eSBarry Smith Use `TSGetSNES()` to obtain the `SNES` object 55646bc98fa9SBarry Smith 5565b43aa488SJacob Faibussowitsch Developer Notes: 5566bcf0153eSBarry Smith The naming of this function is inconsistent with the `SNESSetFunctionDomainError()` 55676bc98fa9SBarry Smith since one takes a function pointer and the other does not. 55686bc98fa9SBarry Smith 55691cc06b55SBarry Smith .seealso: [](ch_ts), `TSAdaptCheckStage()`, `TSFunctionDomainError()`, `SNESSetFunctionDomainError()`, `TSGetSNES()` 5570cb9d8021SPierre Barbier de Reuille @*/ 5571a144a63fSStefano Zampini PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS ts, PetscReal time, Vec state, PetscBool *accept)) 5572d71ae5a4SJacob Faibussowitsch { 5573cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 5574cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5575cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 55763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5577cb9d8021SPierre Barbier de Reuille } 5578cb9d8021SPierre Barbier de Reuille 5579cb9d8021SPierre Barbier de Reuille /*@ 55806bc98fa9SBarry Smith TSFunctionDomainError - Checks if the current state is valid 5581cb9d8021SPierre Barbier de Reuille 5582cb9d8021SPierre Barbier de Reuille Input Parameters: 5583bcf0153eSBarry Smith + ts - the `TS` context 55846bc98fa9SBarry Smith . stagetime - time of the simulation 55856bc98fa9SBarry Smith - Y - state vector to check. 5586cb9d8021SPierre Barbier de Reuille 5587cb9d8021SPierre Barbier de Reuille Output Parameter: 5588bcf0153eSBarry Smith . accept - Set to `PETSC_FALSE` if the current state vector is valid. 558996a0c994SBarry Smith 55906bc98fa9SBarry Smith Level: developer 55916bc98fa9SBarry Smith 5592bcf0153eSBarry Smith Note: 5593bcf0153eSBarry Smith This function is called by the `TS` integration routines and calls the user provided function (set with `TSSetFunctionDomainError()`) 5594bcf0153eSBarry Smith to check if the current state is valid. 5595bcf0153eSBarry Smith 55961cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetFunctionDomainError()` 5597cb9d8021SPierre Barbier de Reuille @*/ 5598d71ae5a4SJacob Faibussowitsch PetscErrorCode TSFunctionDomainError(TS ts, PetscReal stagetime, Vec Y, PetscBool *accept) 5599d71ae5a4SJacob Faibussowitsch { 5600cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 5601cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5602cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 56031e66621cSBarry Smith if (ts->functiondomainerror) PetscCall((*ts->functiondomainerror)(ts, stagetime, Y, accept)); 56043ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5605cb9d8021SPierre Barbier de Reuille } 56061ceb14c0SBarry Smith 5607cc4c1da9SBarry Smith /*@ 5608195e9b02SBarry Smith TSClone - This function clones a time step `TS` object. 560993b34091SDebojyoti Ghosh 5610d083f849SBarry Smith Collective 561193b34091SDebojyoti Ghosh 561293b34091SDebojyoti Ghosh Input Parameter: 5613bcf0153eSBarry Smith . tsin - The input `TS` 561493b34091SDebojyoti Ghosh 561593b34091SDebojyoti Ghosh Output Parameter: 5616bcf0153eSBarry Smith . tsout - The output `TS` (cloned) 56175eca1a21SEmil Constantinescu 56185eca1a21SEmil Constantinescu Level: developer 561993b34091SDebojyoti Ghosh 5620bcf0153eSBarry Smith Notes: 5621bcf0153eSBarry 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. 5622bcf0153eSBarry Smith It will likely be replaced in the future with a mechanism of switching methods on the fly. 5623bcf0153eSBarry Smith 5624195e9b02SBarry 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 5625195e9b02SBarry Smith .vb 5626195e9b02SBarry Smith SNES snes_dup = NULL; 5627195e9b02SBarry Smith TSGetSNES(ts,&snes_dup); 5628195e9b02SBarry Smith TSSetSNES(ts,snes_dup); 5629195e9b02SBarry Smith .ve 5630bcf0153eSBarry Smith 56311cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `TSCreate()`, `TSSetType()`, `TSSetUp()`, `TSDestroy()`, `TSSetProblemType()` 563293b34091SDebojyoti Ghosh @*/ 5633d71ae5a4SJacob Faibussowitsch PetscErrorCode TSClone(TS tsin, TS *tsout) 5634d71ae5a4SJacob Faibussowitsch { 563593b34091SDebojyoti Ghosh TS t; 5636dc846ba4SSatish Balay SNES snes_start; 5637dc846ba4SSatish Balay DM dm; 5638dc846ba4SSatish Balay TSType type; 563993b34091SDebojyoti Ghosh 564093b34091SDebojyoti Ghosh PetscFunctionBegin; 56414f572ea9SToby Isaac PetscAssertPointer(tsin, 1); 564293b34091SDebojyoti Ghosh *tsout = NULL; 564393b34091SDebojyoti Ghosh 56449566063dSJacob Faibussowitsch PetscCall(PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView)); 564593b34091SDebojyoti Ghosh 564693b34091SDebojyoti Ghosh /* General TS description */ 564793b34091SDebojyoti Ghosh t->numbermonitors = 0; 5648d0c080abSJoseph Pusztay t->monitorFrequency = 1; 564993b34091SDebojyoti Ghosh t->setupcalled = 0; 565093b34091SDebojyoti Ghosh t->ksp_its = 0; 565193b34091SDebojyoti Ghosh t->snes_its = 0; 565293b34091SDebojyoti Ghosh t->nwork = 0; 56537d51462cSStefano Zampini t->rhsjacobian.time = PETSC_MIN_REAL; 565493b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 565593b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 565693b34091SDebojyoti Ghosh 56579566063dSJacob Faibussowitsch PetscCall(TSGetSNES(tsin, &snes_start)); 56589566063dSJacob Faibussowitsch PetscCall(TSSetSNES(t, snes_start)); 5659d15a3a53SEmil Constantinescu 56609566063dSJacob Faibussowitsch PetscCall(TSGetDM(tsin, &dm)); 56619566063dSJacob Faibussowitsch PetscCall(TSSetDM(t, dm)); 566293b34091SDebojyoti Ghosh 566393b34091SDebojyoti Ghosh t->adapt = tsin->adapt; 56649566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)t->adapt)); 566593b34091SDebojyoti Ghosh 5666e7069c78SShri t->trajectory = tsin->trajectory; 56679566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)t->trajectory)); 5668e7069c78SShri 5669e7069c78SShri t->event = tsin->event; 56706b10a48eSSatish Balay if (t->event) t->event->refct++; 5671e7069c78SShri 567293b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 567393b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 5674e7069c78SShri t->ptime_prev = tsin->ptime_prev; 567593b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 567693b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 567793b34091SDebojyoti Ghosh t->steps = tsin->steps; 567893b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 567993b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 568093b34091SDebojyoti Ghosh t->atol = tsin->atol; 568193b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 568293b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 568393b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 568493b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 568593b34091SDebojyoti Ghosh 56869566063dSJacob Faibussowitsch PetscCall(TSGetType(tsin, &type)); 56879566063dSJacob Faibussowitsch PetscCall(TSSetType(t, type)); 568893b34091SDebojyoti Ghosh 568993b34091SDebojyoti Ghosh t->vec_sol = NULL; 569093b34091SDebojyoti Ghosh 569193b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 569293b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 569393b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 569493b34091SDebojyoti Ghosh 5695aea10558SJacob Faibussowitsch t->ops[0] = tsin->ops[0]; 569693b34091SDebojyoti Ghosh 56970d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 56980d4fed19SBarry Smith PetscInt i; 56999566063dSJacob Faibussowitsch PetscCall(PetscMalloc((10) * sizeof(void (*)(void)), &((PetscObject)t)->fortran_func_pointers)); 5700ad540459SPierre Jolivet for (i = 0; i < 10; i++) ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 57010d4fed19SBarry Smith } 570293b34091SDebojyoti Ghosh *tsout = t; 57033ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 570493b34091SDebojyoti Ghosh } 5705f3b1f45cSBarry Smith 5706d71ae5a4SJacob Faibussowitsch static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void *ctx, Vec x, Vec y) 5707d71ae5a4SJacob Faibussowitsch { 5708f3b1f45cSBarry Smith TS ts = (TS)ctx; 5709f3b1f45cSBarry Smith 5710f3b1f45cSBarry Smith PetscFunctionBegin; 57119566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts, 0, x, y)); 57123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5713f3b1f45cSBarry Smith } 5714f3b1f45cSBarry Smith 5715f3b1f45cSBarry Smith /*@ 5716bcf0153eSBarry Smith TSRHSJacobianTest - Compares the multiply routine provided to the `MATSHELL` with differencing on the `TS` given RHS function. 5717f3b1f45cSBarry Smith 5718c3339decSBarry Smith Logically Collective 5719f3b1f45cSBarry Smith 57202fe279fdSBarry Smith Input Parameter: 5721b43aa488SJacob Faibussowitsch . ts - the time stepping routine 5722f3b1f45cSBarry Smith 5723f3b1f45cSBarry Smith Output Parameter: 5724bcf0153eSBarry Smith . flg - `PETSC_TRUE` if the multiply is likely correct 5725f3b1f45cSBarry Smith 5726bcf0153eSBarry Smith Options Database Key: 5727f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator 5728f3b1f45cSBarry Smith 5729f3b1f45cSBarry Smith Level: advanced 5730f3b1f45cSBarry Smith 5731bcf0153eSBarry Smith Note: 5732195e9b02SBarry Smith This only works for problems defined using `TSSetRHSFunction()` and Jacobian NOT `TSSetIFunction()` and Jacobian 5733f3b1f45cSBarry Smith 57341cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `Mat`, `MATSHELL`, `MatCreateShell()`, `MatShellGetContext()`, `MatShellGetOperation()`, `MatShellTestMultTranspose()`, `TSRHSJacobianTestTranspose()` 5735f3b1f45cSBarry Smith @*/ 5736d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRHSJacobianTest(TS ts, PetscBool *flg) 5737d71ae5a4SJacob Faibussowitsch { 5738f3b1f45cSBarry Smith Mat J, B; 57398434afd1SBarry Smith TSRHSJacobianFn *func; 5740f3b1f45cSBarry Smith void *ctx; 5741f3b1f45cSBarry Smith 5742f3b1f45cSBarry Smith PetscFunctionBegin; 57439566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts, &J, &B, &func, &ctx)); 57449566063dSJacob Faibussowitsch PetscCall((*func)(ts, 0.0, ts->vec_sol, J, B, ctx)); 57459566063dSJacob Faibussowitsch PetscCall(MatShellTestMult(J, RHSWrapperFunction_TSRHSJacobianTest, ts->vec_sol, ts, flg)); 57463ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5747f3b1f45cSBarry Smith } 5748f3b1f45cSBarry Smith 5749cc4c1da9SBarry Smith /*@ 5750bcf0153eSBarry Smith TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the `MATSHELL` with differencing on the `TS` given RHS function. 5751f3b1f45cSBarry Smith 5752c3339decSBarry Smith Logically Collective 5753f3b1f45cSBarry Smith 57542fe279fdSBarry Smith Input Parameter: 5755b43aa488SJacob Faibussowitsch . ts - the time stepping routine 5756f3b1f45cSBarry Smith 5757f3b1f45cSBarry Smith Output Parameter: 5758bcf0153eSBarry Smith . flg - `PETSC_TRUE` if the multiply is likely correct 5759f3b1f45cSBarry Smith 5760bcf0153eSBarry Smith Options Database Key: 5761f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator 5762f3b1f45cSBarry Smith 5763bcf0153eSBarry Smith Level: advanced 5764bcf0153eSBarry Smith 576595452b02SPatrick Sanan Notes: 5766195e9b02SBarry Smith This only works for problems defined using `TSSetRHSFunction()` and Jacobian NOT `TSSetIFunction()` and Jacobian 5767f3b1f45cSBarry Smith 57681cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `Mat`, `MatCreateShell()`, `MatShellGetContext()`, `MatShellGetOperation()`, `MatShellTestMultTranspose()`, `TSRHSJacobianTest()` 5769f3b1f45cSBarry Smith @*/ 5770d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRHSJacobianTestTranspose(TS ts, PetscBool *flg) 5771d71ae5a4SJacob Faibussowitsch { 5772f3b1f45cSBarry Smith Mat J, B; 5773f3b1f45cSBarry Smith void *ctx; 57748434afd1SBarry Smith TSRHSJacobianFn *func; 5775f3b1f45cSBarry Smith 5776f3b1f45cSBarry Smith PetscFunctionBegin; 57779566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts, &J, &B, &func, &ctx)); 57789566063dSJacob Faibussowitsch PetscCall((*func)(ts, 0.0, ts->vec_sol, J, B, ctx)); 57799566063dSJacob Faibussowitsch PetscCall(MatShellTestMultTranspose(J, RHSWrapperFunction_TSRHSJacobianTest, ts->vec_sol, ts, flg)); 57803ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5781f3b1f45cSBarry Smith } 57820fe4d17eSHong Zhang 57830fe4d17eSHong Zhang /*@ 57840fe4d17eSHong Zhang TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used. 57850fe4d17eSHong Zhang 578620f4b53cSBarry Smith Logically Collective 57870fe4d17eSHong Zhang 5788d8d19677SJose E. Roman Input Parameters: 57890fe4d17eSHong Zhang + ts - timestepping context 5790bcf0153eSBarry Smith - use_splitrhsfunction - `PETSC_TRUE` indicates that the split RHSFunction will be used 57910fe4d17eSHong Zhang 5792bcf0153eSBarry Smith Options Database Key: 57930fe4d17eSHong Zhang . -ts_use_splitrhsfunction - <true,false> 57940fe4d17eSHong Zhang 57950fe4d17eSHong Zhang Level: intermediate 57960fe4d17eSHong Zhang 5797bcf0153eSBarry Smith Note: 5798195e9b02SBarry Smith This is only for multirate methods 5799bcf0153eSBarry Smith 58001cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetUseSplitRHSFunction()` 58010fe4d17eSHong Zhang @*/ 5802d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction) 5803d71ae5a4SJacob Faibussowitsch { 58040fe4d17eSHong Zhang PetscFunctionBegin; 58050fe4d17eSHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 58060fe4d17eSHong Zhang ts->use_splitrhsfunction = use_splitrhsfunction; 58073ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 58080fe4d17eSHong Zhang } 58090fe4d17eSHong Zhang 58100fe4d17eSHong Zhang /*@ 58110fe4d17eSHong Zhang TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used. 58120fe4d17eSHong Zhang 581320f4b53cSBarry Smith Not Collective 58140fe4d17eSHong Zhang 58150fe4d17eSHong Zhang Input Parameter: 58160fe4d17eSHong Zhang . ts - timestepping context 58170fe4d17eSHong Zhang 58180fe4d17eSHong Zhang Output Parameter: 5819bcf0153eSBarry Smith . use_splitrhsfunction - `PETSC_TRUE` indicates that the split RHSFunction will be used 58200fe4d17eSHong Zhang 58210fe4d17eSHong Zhang Level: intermediate 58220fe4d17eSHong Zhang 58231cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetUseSplitRHSFunction()` 58240fe4d17eSHong Zhang @*/ 5825d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction) 5826d71ae5a4SJacob Faibussowitsch { 58270fe4d17eSHong Zhang PetscFunctionBegin; 58280fe4d17eSHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 58290fe4d17eSHong Zhang *use_splitrhsfunction = ts->use_splitrhsfunction; 58303ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 58310fe4d17eSHong Zhang } 5832d60b7d5cSBarry Smith 5833d60b7d5cSBarry Smith /*@ 5834d60b7d5cSBarry Smith TSSetMatStructure - sets the relationship between the nonzero structure of the RHS Jacobian matrix to the IJacobian matrix. 5835d60b7d5cSBarry Smith 5836c3339decSBarry Smith Logically Collective 5837d60b7d5cSBarry Smith 5838d60b7d5cSBarry Smith Input Parameters: 5839d60b7d5cSBarry Smith + ts - the time-stepper 5840bcf0153eSBarry Smith - str - the structure (the default is `UNKNOWN_NONZERO_PATTERN`) 5841d60b7d5cSBarry Smith 5842d60b7d5cSBarry Smith Level: intermediate 5843d60b7d5cSBarry Smith 5844bcf0153eSBarry Smith Note: 5845d60b7d5cSBarry Smith When the relationship between the nonzero structures is known and supplied the solution process can be much faster 5846d60b7d5cSBarry Smith 58471cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `MatAXPY()`, `MatStructure` 5848d60b7d5cSBarry Smith @*/ 5849d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMatStructure(TS ts, MatStructure str) 5850d71ae5a4SJacob Faibussowitsch { 5851d60b7d5cSBarry Smith PetscFunctionBegin; 5852d60b7d5cSBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5853d60b7d5cSBarry Smith ts->axpy_pattern = str; 58543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5855d60b7d5cSBarry Smith } 58564a658b32SHong Zhang 58574a658b32SHong Zhang /*@ 58588343f784SJames Wright TSSetEvaluationTimes - sets the evaluation points. The solution will be computed and stored for each time requested 58594a658b32SHong Zhang 5860c3339decSBarry Smith Collective 58614a658b32SHong Zhang 58624a658b32SHong Zhang Input Parameters: 58634a658b32SHong Zhang + ts - the time-stepper 58648343f784SJames Wright . n - number of the time points 58658343f784SJames Wright - time_points - array of the time points 58664a658b32SHong Zhang 5867bcf0153eSBarry Smith Options Database Key: 58688343f784SJames Wright . -ts_eval_times <t0,...tn> - Sets the evaluation times 58694a658b32SHong Zhang 5870bcf0153eSBarry Smith Level: intermediate 58714a658b32SHong Zhang 58724a658b32SHong Zhang Notes: 58738343f784SJames Wright The elements in `time_points` must be all increasing. They correspond to the intermediate points for time integration. 58744a658b32SHong Zhang 58758343f784SJames Wright `TS_EXACTFINALTIME_MATCHSTEP` must be used to make the last time step in each sub-interval match the intermediate points specified. 58768343f784SJames Wright 5877c80d56d9SJames Wright The intermediate solutions are saved in a vector array that can be accessed with `TSGetEvaluationSolutions()`. Thus using evaluation times may 58788343f784SJames Wright pressure the memory system when using a large number of time points. 58798343f784SJames Wright 5880c80d56d9SJames Wright .seealso: [](ch_ts), `TS`, `TSGetEvaluationTimes()`, `TSGetEvaluationSolutions()`, `TSSetTimeSpan()` 58814a658b32SHong Zhang @*/ 58828343f784SJames Wright PetscErrorCode TSSetEvaluationTimes(TS ts, PetscInt n, PetscReal *time_points) 5883d71ae5a4SJacob Faibussowitsch { 58848343f784SJames Wright PetscBool is_sorted; 58858343f784SJames Wright 58864a658b32SHong Zhang PetscFunctionBegin; 58874a658b32SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5888136cf249SJames Wright if (ts->eval_times && n != ts->eval_times->num_time_points) { 5889136cf249SJames Wright PetscCall(PetscFree(ts->eval_times->time_points)); 5890136cf249SJames Wright PetscCall(VecDestroyVecs(ts->eval_times->num_time_points, &ts->eval_times->sol_vecs)); 5891136cf249SJames Wright PetscCall(PetscMalloc1(n, &ts->eval_times->time_points)); 58924a658b32SHong Zhang } 5893136cf249SJames Wright if (!ts->eval_times) { 5894136cf249SJames Wright TSEvaluationTimes eval_times; 5895136cf249SJames Wright PetscCall(PetscNew(&eval_times)); 5896136cf249SJames Wright PetscCall(PetscMalloc1(n, &eval_times->time_points)); 5897136cf249SJames Wright eval_times->reltol = 1e-6; 5898136cf249SJames Wright eval_times->abstol = 10 * PETSC_MACHINE_EPSILON; 5899136cf249SJames Wright eval_times->worktol = 0; 5900136cf249SJames Wright ts->eval_times = eval_times; 59014a658b32SHong Zhang } 5902136cf249SJames Wright ts->eval_times->num_time_points = n; 59038343f784SJames Wright PetscCall(PetscSortedReal(n, time_points, &is_sorted)); 59048343f784SJames Wright PetscCheck(is_sorted, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "time_points array must be sorted"); 5905136cf249SJames Wright PetscCall(PetscArraycpy(ts->eval_times->time_points, time_points, n)); 59068343f784SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 59078343f784SJames Wright } 59088343f784SJames Wright 59098343f784SJames Wright /*@C 59108343f784SJames Wright TSGetEvaluationTimes - gets the evaluation times set with `TSSetEvaluationTimes()` 59118343f784SJames Wright 59128343f784SJames Wright Not Collective 59138343f784SJames Wright 59148343f784SJames Wright Input Parameter: 59158343f784SJames Wright . ts - the time-stepper 59168343f784SJames Wright 59178343f784SJames Wright Output Parameters: 59188343f784SJames Wright + n - number of the time points 59198343f784SJames Wright - time_points - array of the time points 59208343f784SJames Wright 59218343f784SJames Wright Level: beginner 59228343f784SJames Wright 59238343f784SJames Wright Note: 59248343f784SJames Wright The values obtained are valid until the `TS` object is destroyed. 59258343f784SJames Wright 59268343f784SJames Wright Both `n` and `time_points` can be `NULL`. 59278343f784SJames Wright 59288343f784SJames Wright Also used to see time points set by `TSSetTimeSpan()`. 59298343f784SJames Wright 5930c80d56d9SJames Wright .seealso: [](ch_ts), `TS`, `TSSetEvaluationTimes()`, `TSGetEvaluationSolutions()` 59318343f784SJames Wright @*/ 59328343f784SJames Wright PetscErrorCode TSGetEvaluationTimes(TS ts, PetscInt *n, const PetscReal *time_points[]) 59338343f784SJames Wright { 59348343f784SJames Wright PetscFunctionBegin; 59358343f784SJames Wright PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 59368343f784SJames Wright if (n) PetscAssertPointer(n, 2); 59378343f784SJames Wright if (time_points) PetscAssertPointer(time_points, 3); 5938136cf249SJames Wright if (!ts->eval_times) { 59398343f784SJames Wright if (n) *n = 0; 59408343f784SJames Wright if (time_points) *time_points = NULL; 59418343f784SJames Wright } else { 5942136cf249SJames Wright if (n) *n = ts->eval_times->num_time_points; 5943136cf249SJames Wright if (time_points) *time_points = ts->eval_times->time_points; 59448343f784SJames Wright } 59458343f784SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 59468343f784SJames Wright } 59478343f784SJames Wright 59488343f784SJames Wright /*@C 5949c80d56d9SJames Wright TSGetEvaluationSolutions - Get the number of solutions and the solutions at the evaluation time points specified 59508343f784SJames Wright 59518343f784SJames Wright Input Parameter: 59528343f784SJames Wright . ts - the `TS` context obtained from `TSCreate()` 59538343f784SJames Wright 59548343f784SJames Wright Output Parameters: 59558343f784SJames Wright + nsol - the number of solutions 59568343f784SJames Wright . sol_times - array of solution times corresponding to the solution vectors. See note below 59578343f784SJames Wright - Sols - the solution vectors 59588343f784SJames Wright 59598343f784SJames Wright Level: intermediate 59608343f784SJames Wright 59618343f784SJames Wright Notes: 59628343f784SJames Wright Both `nsol` and `Sols` can be `NULL`. 59638343f784SJames Wright 59648343f784SJames Wright Some time points in the evaluation points may be skipped by `TS` so that `nsol` is less than the number of points specified by `TSSetEvaluationTimes()`. 59658343f784SJames Wright For example, manipulating the step size, especially with a reduced precision, may cause `TS` to step over certain evaluation times. 59668343f784SJames Wright 59678343f784SJames Wright Also used to see view solutions requested by `TSSetTimeSpan()`. 59688343f784SJames Wright 59698343f784SJames Wright .seealso: [](ch_ts), `TS`, `TSSetEvaluationTimes()`, `TSGetEvaluationTimes()` 59708343f784SJames Wright @*/ 5971c80d56d9SJames Wright PetscErrorCode TSGetEvaluationSolutions(TS ts, PetscInt *nsol, const PetscReal *sol_times[], Vec **Sols) 59728343f784SJames Wright { 59738343f784SJames Wright PetscFunctionBegin; 59748343f784SJames Wright PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 59758343f784SJames Wright if (nsol) PetscAssertPointer(nsol, 2); 59768343f784SJames Wright if (sol_times) PetscAssertPointer(sol_times, 3); 59778343f784SJames Wright if (Sols) PetscAssertPointer(Sols, 4); 5978136cf249SJames Wright if (!ts->eval_times) { 59798343f784SJames Wright if (nsol) *nsol = 0; 59808343f784SJames Wright if (sol_times) *sol_times = NULL; 59818343f784SJames Wright if (Sols) *Sols = NULL; 59828343f784SJames Wright } else { 5983136cf249SJames Wright if (nsol) *nsol = ts->eval_times->sol_ctr; 5984136cf249SJames Wright if (sol_times) *sol_times = ts->eval_times->sol_times; 5985136cf249SJames Wright if (Sols) *Sols = ts->eval_times->sol_vecs; 59868343f784SJames Wright } 59878343f784SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 59888343f784SJames Wright } 59898343f784SJames Wright 59908343f784SJames Wright /*@ 59918343f784SJames Wright TSSetTimeSpan - sets the time span. The solution will be computed and stored for each time requested in the span 59928343f784SJames Wright 59938343f784SJames Wright Collective 59948343f784SJames Wright 59958343f784SJames Wright Input Parameters: 59968343f784SJames Wright + ts - the time-stepper 59978343f784SJames Wright . n - number of the time points (>=2) 59988343f784SJames Wright - span_times - array of the time points. The first element and the last element are the initial time and the final time respectively. 59998343f784SJames Wright 60008343f784SJames Wright Options Database Key: 60018343f784SJames Wright . -ts_time_span <t0,...tf> - Sets the time span 60028343f784SJames Wright 60038343f784SJames Wright Level: intermediate 60048343f784SJames Wright 60058343f784SJames Wright Notes: 6006c80d56d9SJames Wright This function is identical to `TSSetEvaluationTimes()`, except that it also sets the initial time and final time for the `ts` to the first and last `span_times` entries. 60078343f784SJames Wright 60088343f784SJames Wright The elements in tspan must be all increasing. They correspond to the intermediate points for time integration. 60098343f784SJames Wright 60108343f784SJames Wright `TS_EXACTFINALTIME_MATCHSTEP` must be used to make the last time step in each sub-interval match the intermediate points specified. 60118343f784SJames Wright 6012c80d56d9SJames Wright The intermediate solutions are saved in a vector array that can be accessed with `TSGetEvaluationSolutions()`. Thus using time span may 60138343f784SJames Wright pressure the memory system when using a large number of span points. 60148343f784SJames Wright 6015c80d56d9SJames Wright .seealso: [](ch_ts), `TS`, `TSSetEvaluationTimes()`, `TSGetEvaluationTimes()`, `TSGetEvaluationSolutions()` 60168343f784SJames Wright @*/ 60178343f784SJames Wright PetscErrorCode TSSetTimeSpan(TS ts, PetscInt n, PetscReal *span_times) 60188343f784SJames Wright { 60198343f784SJames Wright PetscFunctionBegin; 60208343f784SJames Wright PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 60218343f784SJames Wright PetscCheck(n >= 2, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Minimum time span size is 2 but %" PetscInt_FMT " is provided", n); 60228343f784SJames Wright PetscCall(TSSetEvaluationTimes(ts, n, span_times)); 60238343f784SJames Wright PetscCall(TSSetTime(ts, span_times[0])); 60248343f784SJames Wright PetscCall(TSSetMaxTime(ts, span_times[n - 1])); 60253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 60264a658b32SHong Zhang } 60274a658b32SHong Zhang 6028cc4c1da9SBarry Smith /*@ 60297b2dc123SHong Zhang TSPruneIJacobianColor - Remove nondiagonal zeros in the Jacobian matrix and update the `MatMFFD` coloring information. 6030d7cfae9bSHong Zhang 6031195e9b02SBarry Smith Collective 6032d7cfae9bSHong Zhang 6033d7cfae9bSHong Zhang Input Parameters: 6034195e9b02SBarry Smith + ts - the `TS` context 6035d7cfae9bSHong Zhang . J - Jacobian matrix (not altered in this routine) 6036195e9b02SBarry Smith - B - newly computed Jacobian matrix to use with preconditioner 6037d7cfae9bSHong Zhang 6038d7cfae9bSHong Zhang Level: intermediate 6039d7cfae9bSHong Zhang 6040d7cfae9bSHong Zhang Notes: 6041195e9b02SBarry Smith This function improves the `MatFDColoring` performance when the Jacobian matrix was over-allocated or contains 6042195e9b02SBarry Smith many constant zeros entries, which is typically the case when the matrix is generated by a `DM` 6043d7cfae9bSHong Zhang and multiple fields are involved. 6044d7cfae9bSHong Zhang 6045d7cfae9bSHong Zhang Users need to make sure that the Jacobian matrix is properly filled to reflect the sparsity 6046195e9b02SBarry Smith structure. For `MatFDColoring`, the values of nonzero entries are not important. So one can 60477b2dc123SHong Zhang usually call `TSComputeIJacobian()` with randomized input vectors to generate a dummy Jacobian. 60487b2dc123SHong Zhang `TSComputeIJacobian()` should be called before `TSSolve()` but after `TSSetUp()`. 6049d7cfae9bSHong Zhang 60501cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `MatFDColoring`, `TSComputeIJacobianDefaultColor()`, `MatEliminateZeros()`, `MatFDColoringCreate()`, `MatFDColoringSetFunction()` 6051d7cfae9bSHong Zhang @*/ 6052d7cfae9bSHong Zhang PetscErrorCode TSPruneIJacobianColor(TS ts, Mat J, Mat B) 6053d7cfae9bSHong Zhang { 6054d7cfae9bSHong Zhang MatColoring mc = NULL; 6055d7cfae9bSHong Zhang ISColoring iscoloring = NULL; 6056d7cfae9bSHong Zhang MatFDColoring matfdcoloring = NULL; 6057d7cfae9bSHong Zhang 6058d7cfae9bSHong Zhang PetscFunctionBegin; 6059d7cfae9bSHong Zhang /* Generate new coloring after eliminating zeros in the matrix */ 606058c11ad4SPierre Jolivet PetscCall(MatEliminateZeros(B, PETSC_TRUE)); 6061d7cfae9bSHong Zhang PetscCall(MatColoringCreate(B, &mc)); 6062d7cfae9bSHong Zhang PetscCall(MatColoringSetDistance(mc, 2)); 6063d7cfae9bSHong Zhang PetscCall(MatColoringSetType(mc, MATCOLORINGSL)); 6064d7cfae9bSHong Zhang PetscCall(MatColoringSetFromOptions(mc)); 6065d7cfae9bSHong Zhang PetscCall(MatColoringApply(mc, &iscoloring)); 6066d7cfae9bSHong Zhang PetscCall(MatColoringDestroy(&mc)); 6067d7cfae9bSHong Zhang /* Replace the old coloring with the new one */ 6068d7cfae9bSHong Zhang PetscCall(MatFDColoringCreate(B, iscoloring, &matfdcoloring)); 6069d7cfae9bSHong Zhang PetscCall(MatFDColoringSetFunction(matfdcoloring, (PetscErrorCode (*)(void))SNESTSFormFunction, (void *)ts)); 6070d7cfae9bSHong Zhang PetscCall(MatFDColoringSetFromOptions(matfdcoloring)); 6071d7cfae9bSHong Zhang PetscCall(MatFDColoringSetUp(B, iscoloring, matfdcoloring)); 6072d7cfae9bSHong Zhang PetscCall(PetscObjectCompose((PetscObject)B, "TSMatFDColoring", (PetscObject)matfdcoloring)); 6073d7cfae9bSHong Zhang PetscCall(PetscObjectDereference((PetscObject)matfdcoloring)); 6074d7cfae9bSHong Zhang PetscCall(ISColoringDestroy(&iscoloring)); 60753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6076d7cfae9bSHong Zhang } 6077