1af0996ceSBarry Smith #include <petsc/private/tsimpl.h> /*I "petscts.h" I*/ 2496e6a7aSJed Brown #include <petscdmshell.h> 31e25c274SJed Brown #include <petscdmda.h> 42d5ee99bSBarry Smith #include <petscviewer.h> 52d5ee99bSBarry Smith #include <petscdraw.h> 6900f6b5bSMatthew G. Knepley #include <petscconvest.h> 7d763cef2SBarry Smith 89371c9d4SSatish Balay #define SkipSmallValue(a, b, tol) \ 99371c9d4SSatish Balay if (PetscAbsScalar(a) < tol || PetscAbsScalar(b) < tol) continue; 101c167fc2SEmil Constantinescu 11d5ba7fb7SMatthew Knepley /* Logging support */ 12d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 13a05bf03eSHong Zhang PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 14d405a339SMatthew Knepley 15c793f718SLisandro Dalcin const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED", "STEPOVER", "INTERPOLATE", "MATCHSTEP", "TSExactFinalTimeOption", "TS_EXACTFINALTIME_", NULL}; 1649354f04SShri Abhyankar 17d71ae5a4SJacob Faibussowitsch static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt, TSAdaptType default_type) 18d71ae5a4SJacob Faibussowitsch { 192ffb9264SLisandro Dalcin PetscFunctionBegin; 20b92453a8SLisandro Dalcin PetscValidHeaderSpecific(adapt, TSADAPT_CLASSID, 1); 21b92453a8SLisandro Dalcin PetscValidCharPointer(default_type, 2); 221e66621cSBarry Smith if (!((PetscObject)adapt)->type_name) PetscCall(TSAdaptSetType(adapt, default_type)); 233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 242ffb9264SLisandro Dalcin } 252ffb9264SLisandro Dalcin 26bdad233fSMatthew Knepley /*@ 27*195e9b02SBarry Smith TSSetFromOptions - Sets various `TS` parameters from the options database 28bdad233fSMatthew Knepley 29c3339decSBarry Smith Collective 30bdad233fSMatthew Knepley 31bdad233fSMatthew Knepley Input Parameter: 32bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 33bdad233fSMatthew Knepley 34bdad233fSMatthew Knepley Options Database Keys: 35*195e9b02SBarry Smith + -ts_type <type> - EULER, BEULER, SUNDIALS, PSEUDO, CN, RK, THETA, ALPHA, GLLE, SSP, GLEE, BSYMP, IRK, see `TSType` 36ef222394SBarry Smith . -ts_save_trajectory - checkpoint the solution at each time-step 37ef85077eSLisandro Dalcin . -ts_max_time <time> - maximum time to compute to 384a658b32SHong Zhang . -ts_time_span <t0,...tf> - sets the time span, 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 41*195e9b02SBarry 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 50f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - test the Jacobian at each iteration against finite difference with RHS function 51*195e9b02SBarry 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 679812b6beSJed Brown -ts_monitor_solution_interval <interval> - output once every interval (default=1) time steps; used with -ts_monitor_solution 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) 699e336e28SPatrick Sanan - -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 7053ea634cSHong Zhang 71bcf0153eSBarry Smith Level: beginner 723d5a8a6aSBarry Smith 73bcf0153eSBarry Smith Notes: 74bcf0153eSBarry Smith See `SNESSetFromOptions()` and `KSPSetFromOptions()` for how to control the nonlinear and linear solves used by the time-stepper. 75bcf0153eSBarry Smith 76*195e9b02SBarry Smith Certain `SNES` options get reset for each new nonlinear solver, for example `-snes_lag_jacobian its` and `-snes_lag_preconditioner its`, in order 77*195e9b02SBarry Smith to retain them over the multiple nonlinear solves that `TS` uses you mush also provide `-snes_lag_jacobian_persists true` and 78*195e9b02SBarry Smith `-snes_lag_preconditioner_persists true` 793d5a8a6aSBarry Smith 809e336e28SPatrick Sanan Developer Note: 819e336e28SPatrick Sanan We should unify all the -ts_monitor options in the way that -xxx_view has been unified 82bdad233fSMatthew Knepley 83bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetType()` 84bdad233fSMatthew Knepley @*/ 85d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetFromOptions(TS ts) 86d71ae5a4SJacob Faibussowitsch { 87bc952696SBarry Smith PetscBool opt, flg, tflg; 88eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 894a658b32SHong Zhang PetscReal time_step, tspan[100]; 904a658b32SHong Zhang PetscInt nt = PETSC_STATIC_ARRAY_LENGTH(tspan); 9149354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 92d1212d36SBarry Smith char dir[16]; 93cd11d68dSLisandro Dalcin TSIFunction ifun; 946991f827SBarry Smith const char *defaultType; 956991f827SBarry Smith char typeName[256]; 96bdad233fSMatthew Knepley 97bdad233fSMatthew Knepley PetscFunctionBegin; 980700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 996991f827SBarry Smith 1009566063dSJacob Faibussowitsch PetscCall(TSRegisterAll()); 1019566063dSJacob Faibussowitsch PetscCall(TSGetIFunction(ts, NULL, &ifun, NULL)); 102cd11d68dSLisandro Dalcin 103d0609cedSBarry Smith PetscObjectOptionsBegin((PetscObject)ts); 1041ef27442SStefano Zampini if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 1051ef27442SStefano Zampini else defaultType = ifun ? TSBEULER : TSEULER; 1069566063dSJacob Faibussowitsch PetscCall(PetscOptionsFList("-ts_type", "TS method", "TSSetType", TSList, defaultType, typeName, 256, &opt)); 1071e66621cSBarry Smith if (opt) PetscCall(TSSetType(ts, typeName)); 1081e66621cSBarry Smith else PetscCall(TSSetType(ts, defaultType)); 109bdad233fSMatthew Knepley 110bdad233fSMatthew Knepley /* Handle generic TS options */ 1119566063dSJacob Faibussowitsch PetscCall(PetscOptionsDeprecated("-ts_final_time", "-ts_max_time", "3.10", NULL)); 1129566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_max_time", "Maximum time to run to", "TSSetMaxTime", ts->max_time, &ts->max_time, NULL)); 1134a658b32SHong Zhang PetscCall(PetscOptionsRealArray("-ts_time_span", "Time span", "TSSetTimeSpan", tspan, &nt, &flg)); 1144a658b32SHong Zhang if (flg) PetscCall(TSSetTimeSpan(ts, nt, tspan)); 1159566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_max_steps", "Maximum number of time steps", "TSSetMaxSteps", ts->max_steps, &ts->max_steps, NULL)); 1169566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_init_time", "Initial time", "TSSetTime", ts->ptime, &ts->ptime, NULL)); 1179566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_dt", "Initial time step", "TSSetTimeStep", ts->time_step, &time_step, &flg)); 1189566063dSJacob Faibussowitsch if (flg) PetscCall(TSSetTimeStep(ts, time_step)); 1199566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum("-ts_exact_final_time", "Option for handling of final time step", "TSSetExactFinalTime", TSExactFinalTimeOptions, (PetscEnum)ts->exact_final_time, (PetscEnum *)&eftopt, &flg)); 1209566063dSJacob Faibussowitsch if (flg) PetscCall(TSSetExactFinalTime(ts, eftopt)); 1219566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_max_snes_failures", "Maximum number of nonlinear solve failures", "TSSetMaxSNESFailures", ts->max_snes_failures, &ts->max_snes_failures, NULL)); 1229566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_max_reject", "Maximum number of step rejections before step fails", "TSSetMaxStepRejections", ts->max_reject, &ts->max_reject, NULL)); 1239566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_error_if_step_fails", "Error if no step succeeds", "TSSetErrorIfStepFails", ts->errorifstepfailed, &ts->errorifstepfailed, NULL)); 1249566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_rtol", "Relative tolerance for local truncation error", "TSSetTolerances", ts->rtol, &ts->rtol, NULL)); 1259566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_atol", "Absolute tolerance for local truncation error", "TSSetTolerances", ts->atol, &ts->atol, NULL)); 126bdad233fSMatthew Knepley 1279566063dSJacob 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)); 1289566063dSJacob 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)); 1299566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_use_splitrhsfunction", "Use the split RHS function for multirate solvers ", "TSSetUseSplitRHSFunction", ts->use_splitrhsfunction, &ts->use_splitrhsfunction, NULL)); 13056f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 13156f85f32SBarry Smith { 13256f85f32SBarry Smith PetscBool set; 13356f85f32SBarry Smith flg = PETSC_FALSE; 1349566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_saws_block", "Block for SAWs memory snooper at end of TSSolve", "PetscObjectSAWsBlock", ((PetscObject)ts)->amspublishblock, &flg, &set)); 1351baa6e33SBarry Smith if (set) PetscCall(PetscObjectSAWsSetBlock((PetscObject)ts, flg)); 13656f85f32SBarry Smith } 13756f85f32SBarry Smith #endif 13856f85f32SBarry Smith 139bdad233fSMatthew Knepley /* Monitor options */ 1409566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_frequency", "Number of time steps between monitor output", "TSMonitorSetFrequency", ts->monitorFrequency, &ts->monitorFrequency, NULL)); 1419566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor", "Monitor time and timestep size", "TSMonitorDefault", TSMonitorDefault, NULL)); 1429566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor_extreme", "Monitor extreme values of the solution", "TSMonitorExtreme", TSMonitorExtreme, NULL)); 1439566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor_solution", "View the solution at each timestep", "TSMonitorSolution", TSMonitorSolution, NULL)); 1449566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_dmswarm_monitor_moments", "Monitor moments of particle distribution", "TSDMSwarmMonitorMoments", TSDMSwarmMonitorMoments, NULL)); 145fde5950dSBarry Smith 1469566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_python", "Use Python function", "TSMonitorSet", NULL, monfilename, sizeof(monfilename), &flg)); 1479566063dSJacob Faibussowitsch if (flg) PetscCall(PetscPythonMonitorSet((PetscObject)ts, monfilename)); 1485180491cSLisandro Dalcin 1499566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_solution", "Monitor solution graphically", "TSMonitorLGSolution", &opt)); 150b3603a34SBarry Smith if (opt) { 1513923b477SBarry Smith PetscInt howoften = 1; 152e669de00SBarry Smith DM dm; 153e669de00SBarry Smith PetscBool net; 154b3603a34SBarry Smith 1559566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_solution", "Monitor solution graphically", "TSMonitorLGSolution", howoften, &howoften, NULL)); 1569566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 1579566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)dm, DMNETWORK, &net)); 158e669de00SBarry Smith if (net) { 159e669de00SBarry Smith TSMonitorLGCtxNetwork ctx; 1609566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxNetworkCreate(ts, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 600, 400, howoften, &ctx)); 1619566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGCtxNetworkSolution, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxNetworkDestroy)); 1629566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_lg_solution_semilogy", "Plot the solution with a semi-log axis", "", ctx->semilogy, &ctx->semilogy, NULL)); 163e669de00SBarry Smith } else { 164e669de00SBarry Smith TSMonitorLGCtx ctx; 1659566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 1669566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGSolution, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy)); 167bdad233fSMatthew Knepley } 168e669de00SBarry Smith } 1696ba87a44SLisandro Dalcin 1709566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_error", "Monitor error graphically", "TSMonitorLGError", &opt)); 171ef20d060SBarry Smith if (opt) { 1720b039ecaSBarry Smith TSMonitorLGCtx ctx; 1733923b477SBarry Smith PetscInt howoften = 1; 174ef20d060SBarry Smith 1759566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_error", "Monitor error graphically", "TSMonitorLGError", howoften, &howoften, NULL)); 1769566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 1779566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGError, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy)); 178ef20d060SBarry Smith } 1799566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor_error", "View the error at each timestep", "TSMonitorError", TSMonitorError, NULL)); 1807cf37e64SBarry Smith 1819566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_timestep", "Monitor timestep size graphically", "TSMonitorLGTimeStep", &opt)); 1826934998bSLisandro Dalcin if (opt) { 1836934998bSLisandro Dalcin TSMonitorLGCtx ctx; 1846934998bSLisandro Dalcin PetscInt howoften = 1; 1856934998bSLisandro Dalcin 1869566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_timestep", "Monitor timestep size graphically", "TSMonitorLGTimeStep", howoften, &howoften, NULL)); 1879566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 1889566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGTimeStep, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy)); 1896934998bSLisandro Dalcin } 1909566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_timestep_log", "Monitor log timestep size graphically", "TSMonitorLGTimeStep", &opt)); 1918b668821SLisandro Dalcin if (opt) { 1928b668821SLisandro Dalcin TSMonitorLGCtx ctx; 1938b668821SLisandro Dalcin PetscInt howoften = 1; 1948b668821SLisandro Dalcin 1959566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_timestep_log", "Monitor log timestep size graphically", "TSMonitorLGTimeStep", howoften, &howoften, NULL)); 1969566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 1979566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGTimeStep, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy)); 1988b668821SLisandro Dalcin ctx->semilogy = PETSC_TRUE; 1998b668821SLisandro Dalcin } 2008b668821SLisandro Dalcin 2019566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_snes_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGSNESIterations", &opt)); 202201da799SBarry Smith if (opt) { 203201da799SBarry Smith TSMonitorLGCtx ctx; 204201da799SBarry Smith PetscInt howoften = 1; 205201da799SBarry Smith 2069566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_snes_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGSNESIterations", howoften, &howoften, NULL)); 2079566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 2089566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGSNESIterations, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy)); 209201da799SBarry Smith } 2109566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_ksp_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGKSPIterations", &opt)); 211201da799SBarry Smith if (opt) { 212201da799SBarry Smith TSMonitorLGCtx ctx; 213201da799SBarry Smith PetscInt howoften = 1; 214201da799SBarry Smith 2159566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_ksp_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGKSPIterations", howoften, &howoften, NULL)); 2169566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 2179566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGKSPIterations, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy)); 218201da799SBarry Smith } 2199566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_sp_eig", "Monitor eigenvalues of linearized operator graphically", "TSMonitorSPEig", &opt)); 2208189c53fSBarry Smith if (opt) { 2218189c53fSBarry Smith TSMonitorSPEigCtx ctx; 2228189c53fSBarry Smith PetscInt howoften = 1; 2238189c53fSBarry Smith 2249566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_sp_eig", "Monitor eigenvalues of linearized operator graphically", "TSMonitorSPEig", howoften, &howoften, NULL)); 2259566063dSJacob Faibussowitsch PetscCall(TSMonitorSPEigCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx)); 2269566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorSPEig, ctx, (PetscErrorCode(*)(void **))TSMonitorSPEigCtxDestroy)); 2278189c53fSBarry Smith } 2289566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_sp_swarm", "Display particle phase from the DMSwarm", "TSMonitorSPSwarm", &opt)); 2291b575b74SJoseph Pusztay if (opt) { 2301b575b74SJoseph Pusztay TSMonitorSPCtx ctx; 231d7462660SMatthew Knepley PetscInt howoften = 1, retain = 0; 2326a5217c0SMatthew G. Knepley PetscBool phase = PETSC_TRUE, create = PETSC_TRUE; 233d7462660SMatthew Knepley 2349371c9d4SSatish Balay for (PetscInt i = 0; i < ts->numbermonitors; ++i) 2359371c9d4SSatish Balay if (ts->monitor[i] == TSMonitorSPSwarmSolution) { 2369371c9d4SSatish Balay create = PETSC_FALSE; 2379371c9d4SSatish Balay break; 2389371c9d4SSatish Balay } 2396a5217c0SMatthew G. Knepley if (create) { 2409566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm", "Display particles phase from the DMSwarm", "TSMonitorSPSwarm", howoften, &howoften, NULL)); 2419566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm_retain", "Retain n points plotted to show trajectory, -1 for all points", "TSMonitorSPSwarm", retain, &retain, NULL)); 2429566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_sp_swarm_phase", "Plot in phase space rather than coordinate space", "TSMonitorSPSwarm", phase, &phase, NULL)); 2439566063dSJacob Faibussowitsch PetscCall(TSMonitorSPCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, retain, phase, &ctx)); 2449566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode(*)(void **))TSMonitorSPCtxDestroy)); 2451b575b74SJoseph Pusztay } 2466a5217c0SMatthew G. Knepley } 247ef20d060SBarry Smith opt = PETSC_FALSE; 2489566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution", "Monitor solution graphically", "TSMonitorDrawSolution", &opt)); 249a7cc72afSBarry Smith if (opt) { 25083a4ac43SBarry Smith TSMonitorDrawCtx ctx; 25183a4ac43SBarry Smith PetscInt howoften = 1; 252a80ad3e0SBarry Smith 2539566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_solution", "Monitor solution graphically", "TSMonitorDrawSolution", howoften, &howoften, NULL)); 2549566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, "Computed Solution", PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx)); 2559566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorDrawSolution, ctx, (PetscErrorCode(*)(void **))TSMonitorDrawCtxDestroy)); 256bdad233fSMatthew Knepley } 257fb1732b5SBarry Smith opt = PETSC_FALSE; 2589566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution_phase", "Monitor solution graphically", "TSMonitorDrawSolutionPhase", &opt)); 2592d5ee99bSBarry Smith if (opt) { 2602d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2612d5ee99bSBarry Smith PetscReal bounds[4]; 2622d5ee99bSBarry Smith PetscInt n = 4; 2632d5ee99bSBarry Smith PetscDraw draw; 2646934998bSLisandro Dalcin PetscDrawAxis axis; 2652d5ee99bSBarry Smith 2669566063dSJacob Faibussowitsch PetscCall(PetscOptionsRealArray("-ts_monitor_draw_solution_phase", "Monitor solution graphically", "TSMonitorDrawSolutionPhase", bounds, &n, NULL)); 2673c633725SBarry Smith PetscCheck(n == 4, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Must provide bounding box of phase field"); 2689566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, 1, &ctx)); 2699566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(ctx->viewer, 0, &draw)); 2709566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDrawAxis(ctx->viewer, 0, &axis)); 2719566063dSJacob Faibussowitsch PetscCall(PetscDrawAxisSetLimits(axis, bounds[0], bounds[2], bounds[1], bounds[3])); 2729566063dSJacob Faibussowitsch PetscCall(PetscDrawAxisSetLabels(axis, "Phase Diagram", "Variable 1", "Variable 2")); 2739566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorDrawSolutionPhase, ctx, (PetscErrorCode(*)(void **))TSMonitorDrawCtxDestroy)); 2742d5ee99bSBarry Smith } 2752d5ee99bSBarry Smith opt = PETSC_FALSE; 2769566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_error", "Monitor error graphically", "TSMonitorDrawError", &opt)); 2773a471f94SBarry Smith if (opt) { 27883a4ac43SBarry Smith TSMonitorDrawCtx ctx; 27983a4ac43SBarry Smith PetscInt howoften = 1; 2803a471f94SBarry Smith 2819566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_error", "Monitor error graphically", "TSMonitorDrawError", howoften, &howoften, NULL)); 2829566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, "Error", PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx)); 2839566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorDrawError, ctx, (PetscErrorCode(*)(void **))TSMonitorDrawCtxDestroy)); 2843a471f94SBarry Smith } 2850ed3bfb6SBarry Smith opt = PETSC_FALSE; 2869566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution_function", "Monitor solution provided by TSMonitorSetSolutionFunction() graphically", "TSMonitorDrawSolutionFunction", &opt)); 2870ed3bfb6SBarry Smith if (opt) { 2880ed3bfb6SBarry Smith TSMonitorDrawCtx ctx; 2890ed3bfb6SBarry Smith PetscInt howoften = 1; 2900ed3bfb6SBarry Smith 2919566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_solution_function", "Monitor solution provided by TSMonitorSetSolutionFunction() graphically", "TSMonitorDrawSolutionFunction", howoften, &howoften, NULL)); 2929566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, "Solution provided by user function", PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx)); 2939566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorDrawSolutionFunction, ctx, (PetscErrorCode(*)(void **))TSMonitorDrawCtxDestroy)); 2940ed3bfb6SBarry Smith } 295fde5950dSBarry Smith 296ed81e22dSJed Brown opt = PETSC_FALSE; 29763a3b9bcSJacob 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)); 298ed81e22dSJed Brown if (flg) { 299bbcf679cSJacob Faibussowitsch const char *ptr = NULL, *ptr2 = NULL; 300ed81e22dSJed Brown char *filetemplate; 30163a3b9bcSJacob Faibussowitsch PetscCheck(monfilename[0], PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03" PetscInt_FMT ".vts"); 302ed81e22dSJed Brown /* Do some cursory validation of the input. */ 3039566063dSJacob Faibussowitsch PetscCall(PetscStrstr(monfilename, "%", (char **)&ptr)); 30463a3b9bcSJacob Faibussowitsch PetscCheck(ptr, PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03" PetscInt_FMT ".vts"); 305ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 3069566063dSJacob Faibussowitsch PetscCall(PetscStrchr("DdiouxX", *ptr, (char **)&ptr2)); 30763a3b9bcSJacob Faibussowitsch PetscCheck(ptr2 || (*ptr >= '0' && *ptr <= '9'), PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03" PetscInt_FMT ".vts"); 308ed81e22dSJed Brown if (ptr2) break; 309ed81e22dSJed Brown } 3109566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(monfilename, &filetemplate)); 3119566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorSolutionVTK, filetemplate, (PetscErrorCode(*)(void **))TSMonitorSolutionVTKDestroy)); 312ed81e22dSJed Brown } 313bdad233fSMatthew Knepley 3149566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_dmda_ray", "Display a ray of the solution", "None", "y=0", dir, sizeof(dir), &flg)); 315d1212d36SBarry Smith if (flg) { 316d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 317d1212d36SBarry Smith int ray = 0; 3183ee9839eSMatthew G. Knepley DMDirection ddir; 319d1212d36SBarry Smith DM da; 320d1212d36SBarry Smith PetscMPIInt rank; 321d1212d36SBarry Smith 3223c633725SBarry Smith PetscCheck(dir[1] == '=', PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Unknown ray %s", dir); 3233ee9839eSMatthew G. Knepley if (dir[0] == 'x') ddir = DM_X; 3243ee9839eSMatthew G. Knepley else if (dir[0] == 'y') ddir = DM_Y; 32598921bdaSJacob Faibussowitsch else SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Unknown ray %s", dir); 326d1212d36SBarry Smith sscanf(dir + 2, "%d", &ray); 327d1212d36SBarry Smith 3289566063dSJacob Faibussowitsch PetscCall(PetscInfo(((PetscObject)ts), "Displaying DMDA ray %c = %d\n", dir[0], ray)); 3299566063dSJacob Faibussowitsch PetscCall(PetscNew(&rayctx)); 3309566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &da)); 3319566063dSJacob Faibussowitsch PetscCall(DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter)); 3329566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)ts), &rank)); 3331e66621cSBarry Smith if (rank == 0) PetscCall(PetscViewerDrawOpen(PETSC_COMM_SELF, NULL, NULL, 0, 0, 600, 300, &rayctx->viewer)); 33451b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 3359566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorDMDARay, rayctx, TSMonitorDMDARayDestroy)); 336d1212d36SBarry Smith } 3379566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_lg_dmda_ray", "Display a ray of the solution", "None", "x=0", dir, sizeof(dir), &flg)); 33851b4a12fSMatthew G. Knepley if (flg) { 33951b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 34051b4a12fSMatthew G. Knepley int ray = 0; 3413ee9839eSMatthew G. Knepley DMDirection ddir; 34251b4a12fSMatthew G. Knepley DM da; 34351b4a12fSMatthew G. Knepley PetscInt howoften = 1; 344d1212d36SBarry Smith 3453c633725SBarry Smith PetscCheck(dir[1] == '=', PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 3463ee9839eSMatthew G. Knepley if (dir[0] == 'x') ddir = DM_X; 3473ee9839eSMatthew G. Knepley else if (dir[0] == 'y') ddir = DM_Y; 34898921bdaSJacob Faibussowitsch else SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 34951b4a12fSMatthew G. Knepley sscanf(dir + 2, "%d", &ray); 3501c3436cfSJed Brown 3519566063dSJacob Faibussowitsch PetscCall(PetscInfo(((PetscObject)ts), "Displaying LG DMDA ray %c = %d\n", dir[0], ray)); 3529566063dSJacob Faibussowitsch PetscCall(PetscNew(&rayctx)); 3539566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &da)); 3549566063dSJacob Faibussowitsch PetscCall(DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter)); 3559566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 600, 400, howoften, &rayctx->lgctx)); 3569566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy)); 35751b4a12fSMatthew G. Knepley } 358a7a1495cSBarry Smith 3599566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_envelope", "Monitor maximum and minimum value of each component of the solution", "TSMonitorEnvelope", &opt)); 360b3d3934dSBarry Smith if (opt) { 361b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 362b3d3934dSBarry Smith 3639566063dSJacob Faibussowitsch PetscCall(TSMonitorEnvelopeCtxCreate(ts, &ctx)); 3649566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorEnvelope, ctx, (PetscErrorCode(*)(void **))TSMonitorEnvelopeCtxDestroy)); 365b3d3934dSBarry Smith } 366aee7a9fbSMatthew G. Knepley flg = PETSC_FALSE; 3679566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_cancel", "Remove all monitors", "TSMonitorCancel", flg, &flg, &opt)); 3689566063dSJacob Faibussowitsch if (opt && flg) PetscCall(TSMonitorCancel(ts)); 369b3d3934dSBarry Smith 370847ff0e1SMatthew G. Knepley flg = PETSC_FALSE; 371d7cfae9bSHong Zhang PetscCall(PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeIJacobianDefaultColor", flg, &flg, NULL)); 372847ff0e1SMatthew G. Knepley if (flg) { 373847ff0e1SMatthew G. Knepley DM dm; 374847ff0e1SMatthew G. Knepley 3759371c9d4SSatish Balay PetscCall(TSGetDM(ts, &dm)); 3769371c9d4SSatish Balay PetscCall(DMTSUnsetIJacobianContext_Internal(dm)); 3779566063dSJacob Faibussowitsch PetscCall(TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, NULL)); 3789566063dSJacob Faibussowitsch PetscCall(PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n")); 379847ff0e1SMatthew G. Knepley } 380847ff0e1SMatthew G. Knepley 381d763cef2SBarry Smith /* Handle specific TS options */ 382dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, setfromoptions, PetscOptionsObject); 383fbc52257SHong Zhang 384a7bdc993SLisandro Dalcin /* Handle TSAdapt options */ 3859566063dSJacob Faibussowitsch PetscCall(TSGetAdapt(ts, &ts->adapt)); 3869566063dSJacob Faibussowitsch PetscCall(TSAdaptSetDefaultType(ts->adapt, ts->default_adapt_type)); 387dbbe0bcdSBarry Smith PetscCall(TSAdaptSetFromOptions(ts->adapt, PetscOptionsObject)); 388a7bdc993SLisandro Dalcin 38968bece0bSHong Zhang /* TS trajectory must be set after TS, since it may use some TS options above */ 3904f122a70SLisandro Dalcin tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE; 3919566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_save_trajectory", "Save the solution at each timestep", "TSSetSaveTrajectory", tflg, &tflg, NULL)); 3921baa6e33SBarry Smith if (tflg) PetscCall(TSSetSaveTrajectory(ts)); 393a05bf03eSHong Zhang 394dbbe0bcdSBarry Smith PetscCall(TSAdjointSetFromOptions(ts, PetscOptionsObject)); 395d763cef2SBarry Smith 396d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 397dbbe0bcdSBarry Smith PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)ts, PetscOptionsObject)); 398d0609cedSBarry Smith PetscOptionsEnd(); 399d763cef2SBarry Smith 4001baa6e33SBarry Smith if (ts->trajectory) PetscCall(TSTrajectorySetFromOptions(ts->trajectory, ts)); 40168bece0bSHong Zhang 4021ef27442SStefano Zampini /* why do we have to do this here and not during TSSetUp? */ 4039566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &ts->snes)); 4041ef27442SStefano Zampini if (ts->problem_type == TS_LINEAR) { 4059566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompareAny((PetscObject)ts->snes, &flg, SNESKSPONLY, SNESKSPTRANSPOSEONLY, "")); 4069566063dSJacob Faibussowitsch if (!flg) PetscCall(SNESSetType(ts->snes, SNESKSPONLY)); 4071ef27442SStefano Zampini } 4089566063dSJacob Faibussowitsch PetscCall(SNESSetFromOptions(ts->snes)); 4093ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 410d763cef2SBarry Smith } 411d763cef2SBarry Smith 412d2daff3dSHong Zhang /*@ 413bcf0153eSBarry Smith TSGetTrajectory - Gets the trajectory from a `TS` if it exists 41478fbdcc8SBarry Smith 415c3339decSBarry Smith Collective 41678fbdcc8SBarry Smith 41778fbdcc8SBarry Smith Input Parameters: 418bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 41978fbdcc8SBarry Smith 4207a7aea1fSJed Brown Output Parameters: 421bcf0153eSBarry Smith . tr - the `TSTrajectory` object, if it exists 42278fbdcc8SBarry Smith 42378fbdcc8SBarry Smith Level: advanced 42478fbdcc8SBarry Smith 425bcf0153eSBarry Smith Note: 426bcf0153eSBarry Smith This routine should be called after all `TS` options have been set 42778fbdcc8SBarry Smith 428*195e9b02SBarry Smith .seealso: [](chapter_ts), `TS`, `TSTrajectory`, `TSAdjointSolve()`, `TSTrajectory`, `TSTrajectoryCreate()` 42978fbdcc8SBarry Smith @*/ 430d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTrajectory(TS ts, TSTrajectory *tr) 431d71ae5a4SJacob Faibussowitsch { 43278fbdcc8SBarry Smith PetscFunctionBegin; 43378fbdcc8SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 43478fbdcc8SBarry Smith *tr = ts->trajectory; 4353ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 43678fbdcc8SBarry Smith } 43778fbdcc8SBarry Smith 43878fbdcc8SBarry Smith /*@ 439bcf0153eSBarry Smith TSSetSaveTrajectory - Causes the `TS` to save its solutions as it iterates forward in time in a `TSTrajectory` object 440d2daff3dSHong Zhang 441c3339decSBarry Smith Collective 442d2daff3dSHong Zhang 443f899ff85SJose E. Roman Input Parameter: 444bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 445bc952696SBarry Smith 446bcf0153eSBarry Smith Options Database Keys: 44778fbdcc8SBarry Smith + -ts_save_trajectory - saves the trajectory to a file 44867b8a455SSatish Balay - -ts_trajectory_type type - set trajectory type 44978fbdcc8SBarry Smith 450d2daff3dSHong Zhang Level: intermediate 451d2daff3dSHong Zhang 452bcf0153eSBarry Smith Notes: 453bcf0153eSBarry Smith This routine should be called after all `TS` options have been set 454d2daff3dSHong Zhang 455bcf0153eSBarry Smith The `TSTRAJECTORYVISUALIZATION` files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and 456bcf0153eSBarry Smith MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m 457bcf0153eSBarry Smith 458*195e9b02SBarry Smith .seealso: [](chapter_ts), `TS`, `TSTrajectory`, `TSGetTrajectory()`, `TSAdjointSolve()` 459d2daff3dSHong Zhang @*/ 460d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetSaveTrajectory(TS ts) 461d71ae5a4SJacob Faibussowitsch { 462d2daff3dSHong Zhang PetscFunctionBegin; 463d2daff3dSHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 46463a3b9bcSJacob Faibussowitsch if (!ts->trajectory) PetscCall(TSTrajectoryCreate(PetscObjectComm((PetscObject)ts), &ts->trajectory)); 4653ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 466d2daff3dSHong Zhang } 467d2daff3dSHong Zhang 468a7a1495cSBarry Smith /*@ 469bcf0153eSBarry Smith TSResetTrajectory - Destroys and recreates the internal `TSTrajectory` object 4702d29f1f2SStefano Zampini 471c3339decSBarry Smith Collective 4722d29f1f2SStefano Zampini 4732d29f1f2SStefano Zampini Input Parameters: 474bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 4752d29f1f2SStefano Zampini 4762d29f1f2SStefano Zampini Level: intermediate 4772d29f1f2SStefano Zampini 478bcf0153eSBarry Smith .seealso: [](chapter_ts), `TSTrajectory`, `TSGetTrajectory()`, `TSAdjointSolve()`, `TSRemoveTrajectory()` 4792d29f1f2SStefano Zampini @*/ 480d71ae5a4SJacob Faibussowitsch PetscErrorCode TSResetTrajectory(TS ts) 481d71ae5a4SJacob Faibussowitsch { 4822d29f1f2SStefano Zampini PetscFunctionBegin; 4832d29f1f2SStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4842d29f1f2SStefano Zampini if (ts->trajectory) { 4859566063dSJacob Faibussowitsch PetscCall(TSTrajectoryDestroy(&ts->trajectory)); 4869566063dSJacob Faibussowitsch PetscCall(TSTrajectoryCreate(PetscObjectComm((PetscObject)ts), &ts->trajectory)); 4872d29f1f2SStefano Zampini } 4883ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4892d29f1f2SStefano Zampini } 4902d29f1f2SStefano Zampini 4912d29f1f2SStefano Zampini /*@ 492*195e9b02SBarry Smith TSRemoveTrajectory - Destroys and removes the internal `TSTrajectory` object from a `TS` 49367a3cfb0SHong Zhang 494c3339decSBarry Smith Collective 49567a3cfb0SHong Zhang 49667a3cfb0SHong Zhang Input Parameters: 497bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 49867a3cfb0SHong Zhang 49967a3cfb0SHong Zhang Level: intermediate 50067a3cfb0SHong Zhang 501bcf0153eSBarry Smith .seealso: [](chapter_ts), `TSTrajectory`, `TSResetTrajectory()`, `TSAdjointSolve()` 50267a3cfb0SHong Zhang @*/ 503d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRemoveTrajectory(TS ts) 504d71ae5a4SJacob Faibussowitsch { 50567a3cfb0SHong Zhang PetscFunctionBegin; 50667a3cfb0SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5071e66621cSBarry Smith if (ts->trajectory) PetscCall(TSTrajectoryDestroy(&ts->trajectory)); 5083ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 50967a3cfb0SHong Zhang } 51067a3cfb0SHong Zhang 51167a3cfb0SHong Zhang /*@ 512a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 513bcf0153eSBarry Smith set with `TSSetRHSJacobian()`. 514a7a1495cSBarry Smith 515c3339decSBarry Smith Collective 516a7a1495cSBarry Smith 517a7a1495cSBarry Smith Input Parameters: 518bcf0153eSBarry Smith + ts - the `TS` context 519a7a1495cSBarry Smith . t - current timestep 5200910c330SBarry Smith - U - input vector 521a7a1495cSBarry Smith 522a7a1495cSBarry Smith Output Parameters: 523a7a1495cSBarry Smith + A - Jacobian matrix 5246b867d5aSJose E. Roman - B - optional preconditioning matrix 525a7a1495cSBarry Smith 526bcf0153eSBarry Smith Level: developer 527bcf0153eSBarry Smith 528bcf0153eSBarry Smith Note: 529a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 530a7a1495cSBarry Smith is used internally within the nonlinear solvers. 531a7a1495cSBarry Smith 532bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetRHSJacobian()`, `KSPSetOperators()` 533a7a1495cSBarry Smith @*/ 534d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSJacobian(TS ts, PetscReal t, Vec U, Mat A, Mat B) 535d71ae5a4SJacob Faibussowitsch { 536270bf2e7SJed Brown PetscObjectState Ustate; 5376c1e1eecSBarry Smith PetscObjectId Uid; 53824989b8cSPeter Brune DM dm; 539942e3340SBarry Smith DMTS tsdm; 54024989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 54124989b8cSPeter Brune void *ctx; 542b2df71adSDebojyoti Ghosh TSRHSFunction rhsfunction; 543a7a1495cSBarry Smith 544a7a1495cSBarry Smith PetscFunctionBegin; 5450700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5460910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 5470910c330SBarry Smith PetscCheckSameComm(ts, 1, U, 3); 5489566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 5499566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm, &tsdm)); 5509566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, NULL)); 5519566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, &rhsjacobianfunc, &ctx)); 5529566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &Ustate)); 5539566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U, &Uid)); 554971015bcSStefano Zampini 5553ba16761SJacob Faibussowitsch if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) PetscFunctionReturn(PETSC_SUCCESS); 556d90be118SSean Farley 55763a3b9bcSJacob 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); 55824989b8cSPeter Brune if (rhsjacobianfunc) { 5599566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_JacobianEval, ts, U, A, B)); 560792fecdfSBarry Smith PetscCallBack("TS callback Jacobian", (*rhsjacobianfunc)(ts, t, U, A, B, ctx)); 561a6ab3590SBarry Smith ts->rhsjacs++; 5629566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_JacobianEval, ts, U, A, B)); 563ef66eb69SBarry Smith } else { 5649566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(A)); 5659566063dSJacob Faibussowitsch if (B && A != B) PetscCall(MatZeroEntries(B)); 566ef66eb69SBarry Smith } 5670e4ef248SJed Brown ts->rhsjacobian.time = t; 568971015bcSStefano Zampini ts->rhsjacobian.shift = 0; 569971015bcSStefano Zampini ts->rhsjacobian.scale = 1.; 5709566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U, &ts->rhsjacobian.Xid)); 5719566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &ts->rhsjacobian.Xstate)); 5723ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 573a7a1495cSBarry Smith } 574a7a1495cSBarry Smith 575316643e7SJed Brown /*@ 576bcf0153eSBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function for a `TS` 577d763cef2SBarry Smith 578c3339decSBarry Smith Collective 579316643e7SJed Brown 580316643e7SJed Brown Input Parameters: 581bcf0153eSBarry Smith + ts - the `TS` context 582316643e7SJed Brown . t - current time 5830910c330SBarry Smith - U - state vector 584316643e7SJed Brown 585316643e7SJed Brown Output Parameter: 586316643e7SJed Brown . y - right hand side 587316643e7SJed Brown 588bcf0153eSBarry Smith Level: developer 589bcf0153eSBarry Smith 590316643e7SJed Brown Note: 591316643e7SJed Brown Most users should not need to explicitly call this routine, as it 592316643e7SJed Brown is used internally within the nonlinear solvers. 593316643e7SJed Brown 594bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetRHSFunction()`, `TSComputeIFunction()` 595316643e7SJed Brown @*/ 596d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSFunction(TS ts, PetscReal t, Vec U, Vec y) 597d71ae5a4SJacob Faibussowitsch { 59824989b8cSPeter Brune TSRHSFunction rhsfunction; 59924989b8cSPeter Brune TSIFunction ifunction; 60024989b8cSPeter Brune void *ctx; 60124989b8cSPeter Brune DM dm; 60224989b8cSPeter Brune 603d763cef2SBarry Smith PetscFunctionBegin; 6040700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 6050910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 6060700a824SBarry Smith PetscValidHeaderSpecific(y, VEC_CLASSID, 4); 6079566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 6089566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, &ctx)); 6099566063dSJacob Faibussowitsch PetscCall(DMTSGetIFunction(dm, &ifunction, NULL)); 610d763cef2SBarry Smith 6113c633725SBarry Smith PetscCheck(rhsfunction || ifunction, PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "Must call TSSetRHSFunction() and / or TSSetIFunction()"); 612d763cef2SBarry Smith 61324989b8cSPeter Brune if (rhsfunction) { 6149566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_FunctionEval, ts, U, y, 0)); 6159566063dSJacob Faibussowitsch PetscCall(VecLockReadPush(U)); 616792fecdfSBarry Smith PetscCallBack("TS callback right-hand-side", (*rhsfunction)(ts, t, U, y, ctx)); 6179566063dSJacob Faibussowitsch PetscCall(VecLockReadPop(U)); 618a6ab3590SBarry Smith ts->rhsfuncs++; 6199566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_FunctionEval, ts, U, y, 0)); 6201e66621cSBarry Smith } else PetscCall(VecZeroEntries(y)); 6213ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 622d763cef2SBarry Smith } 623d763cef2SBarry Smith 624ef20d060SBarry Smith /*@ 625ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 626ef20d060SBarry Smith 627c3339decSBarry Smith Collective 628ef20d060SBarry Smith 629ef20d060SBarry Smith Input Parameters: 630bcf0153eSBarry Smith + ts - the `TS` context 631ef20d060SBarry Smith - t - current time 632ef20d060SBarry Smith 633ef20d060SBarry Smith Output Parameter: 6340910c330SBarry Smith . U - the solution 635ef20d060SBarry Smith 636ef20d060SBarry Smith Level: developer 637ef20d060SBarry Smith 638bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetSolutionFunction()`, `TSSetRHSFunction()`, `TSComputeIFunction()` 639ef20d060SBarry Smith @*/ 640d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeSolutionFunction(TS ts, PetscReal t, Vec U) 641d71ae5a4SJacob Faibussowitsch { 642ef20d060SBarry Smith TSSolutionFunction solutionfunction; 643ef20d060SBarry Smith void *ctx; 644ef20d060SBarry Smith DM dm; 645ef20d060SBarry Smith 646ef20d060SBarry Smith PetscFunctionBegin; 647ef20d060SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 6480910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 6499566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 6509566063dSJacob Faibussowitsch PetscCall(DMTSGetSolutionFunction(dm, &solutionfunction, &ctx)); 651792fecdfSBarry Smith if (solutionfunction) PetscCallBack("TS callback solution", (*solutionfunction)(ts, t, U, ctx)); 6523ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 653ef20d060SBarry Smith } 6549b7cd975SBarry Smith /*@ 6559b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 6569b7cd975SBarry Smith 657c3339decSBarry Smith Collective 6589b7cd975SBarry Smith 6599b7cd975SBarry Smith Input Parameters: 660bcf0153eSBarry Smith + ts - the `TS` context 6619b7cd975SBarry Smith - t - current time 6629b7cd975SBarry Smith 6639b7cd975SBarry Smith Output Parameter: 6649b7cd975SBarry Smith . U - the function value 6659b7cd975SBarry Smith 6669b7cd975SBarry Smith Level: developer 6679b7cd975SBarry Smith 668bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetSolutionFunction()`, `TSSetRHSFunction()`, `TSComputeIFunction()` 6699b7cd975SBarry Smith @*/ 670d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeForcingFunction(TS ts, PetscReal t, Vec U) 671d71ae5a4SJacob Faibussowitsch { 6729b7cd975SBarry Smith void *ctx; 6739b7cd975SBarry Smith DM dm; 6745f80ce2aSJacob Faibussowitsch TSForcingFunction forcing; 6759b7cd975SBarry Smith 6769b7cd975SBarry Smith PetscFunctionBegin; 6779b7cd975SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 6789b7cd975SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 6799566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 6809566063dSJacob Faibussowitsch PetscCall(DMTSGetForcingFunction(dm, &forcing, &ctx)); 6819b7cd975SBarry Smith 682792fecdfSBarry Smith if (forcing) PetscCallBack("TS callback forcing function", (*forcing)(ts, t, U, ctx)); 6833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6849b7cd975SBarry Smith } 685ef20d060SBarry Smith 686d71ae5a4SJacob Faibussowitsch static PetscErrorCode TSGetRHSVec_Private(TS ts, Vec *Frhs) 687d71ae5a4SJacob Faibussowitsch { 6882dd45cf8SJed Brown Vec F; 689214bc6a2SJed Brown 690214bc6a2SJed Brown PetscFunctionBegin; 6910298fd71SBarry Smith *Frhs = NULL; 6929566063dSJacob Faibussowitsch PetscCall(TSGetIFunction(ts, &F, NULL, NULL)); 6931e66621cSBarry Smith if (!ts->Frhs) PetscCall(VecDuplicate(F, &ts->Frhs)); 694214bc6a2SJed Brown *Frhs = ts->Frhs; 6953ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 696214bc6a2SJed Brown } 697214bc6a2SJed Brown 698d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetRHSMats_Private(TS ts, Mat *Arhs, Mat *Brhs) 699d71ae5a4SJacob Faibussowitsch { 700214bc6a2SJed Brown Mat A, B; 70141a1d4d2SBarry Smith TSIJacobian ijacobian; 702214bc6a2SJed Brown 703214bc6a2SJed Brown PetscFunctionBegin; 704c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 705c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 7069566063dSJacob Faibussowitsch PetscCall(TSGetIJacobian(ts, &A, &B, &ijacobian, NULL)); 707214bc6a2SJed Brown if (Arhs) { 708214bc6a2SJed Brown if (!ts->Arhs) { 70941a1d4d2SBarry Smith if (ijacobian) { 7109566063dSJacob Faibussowitsch PetscCall(MatDuplicate(A, MAT_DO_NOT_COPY_VALUES, &ts->Arhs)); 7119566063dSJacob Faibussowitsch PetscCall(TSSetMatStructure(ts, SAME_NONZERO_PATTERN)); 71241a1d4d2SBarry Smith } else { 71341a1d4d2SBarry Smith ts->Arhs = A; 7149566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)A)); 71541a1d4d2SBarry Smith } 7163565c898SBarry Smith } else { 7173565c898SBarry Smith PetscBool flg; 7189566063dSJacob Faibussowitsch PetscCall(SNESGetUseMatrixFree(ts->snes, NULL, &flg)); 7193565c898SBarry Smith /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */ 7203565c898SBarry Smith if (flg && !ijacobian && ts->Arhs == ts->Brhs) { 7219566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)ts->Arhs)); 7223565c898SBarry Smith ts->Arhs = A; 7239566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)A)); 7243565c898SBarry Smith } 725214bc6a2SJed Brown } 726214bc6a2SJed Brown *Arhs = ts->Arhs; 727214bc6a2SJed Brown } 728214bc6a2SJed Brown if (Brhs) { 729214bc6a2SJed Brown if (!ts->Brhs) { 730bdb70873SJed Brown if (A != B) { 73141a1d4d2SBarry Smith if (ijacobian) { 7329566063dSJacob Faibussowitsch PetscCall(MatDuplicate(B, MAT_DO_NOT_COPY_VALUES, &ts->Brhs)); 733bdb70873SJed Brown } else { 73441a1d4d2SBarry Smith ts->Brhs = B; 7359566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)B)); 73641a1d4d2SBarry Smith } 73741a1d4d2SBarry Smith } else { 7389566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)ts->Arhs)); 73951699248SLisandro Dalcin ts->Brhs = ts->Arhs; 740bdb70873SJed Brown } 741214bc6a2SJed Brown } 742214bc6a2SJed Brown *Brhs = ts->Brhs; 743214bc6a2SJed Brown } 7443ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 745214bc6a2SJed Brown } 746214bc6a2SJed Brown 747316643e7SJed Brown /*@ 748*195e9b02SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in the implicit form F(t,U,Udot)=0 749316643e7SJed Brown 750c3339decSBarry Smith Collective 751316643e7SJed Brown 752316643e7SJed Brown Input Parameters: 753bcf0153eSBarry Smith + ts - the `TS` context 754316643e7SJed Brown . t - current time 7550910c330SBarry Smith . U - state vector 7560910c330SBarry Smith . Udot - time derivative of state vector 757bcf0153eSBarry Smith - imex - flag indicates if the method is `TSIMEX` so that the RHSFunction should be kept separate 758316643e7SJed Brown 759316643e7SJed Brown Output Parameter: 760316643e7SJed Brown . Y - right hand side 761316643e7SJed Brown 762bcf0153eSBarry Smith Level: developer 763bcf0153eSBarry Smith 764316643e7SJed Brown Note: 765316643e7SJed Brown Most users should not need to explicitly call this routine, as it 766316643e7SJed Brown is used internally within the nonlinear solvers. 767316643e7SJed Brown 768316643e7SJed Brown If the user did did not write their equations in implicit form, this 769316643e7SJed Brown function recasts them in implicit form. 770316643e7SJed Brown 771bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetIFunction()`, `TSComputeRHSFunction()` 772316643e7SJed Brown @*/ 773d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIFunction(TS ts, PetscReal t, Vec U, Vec Udot, Vec Y, PetscBool imex) 774d71ae5a4SJacob Faibussowitsch { 77524989b8cSPeter Brune TSIFunction ifunction; 77624989b8cSPeter Brune TSRHSFunction rhsfunction; 77724989b8cSPeter Brune void *ctx; 77824989b8cSPeter Brune DM dm; 779316643e7SJed Brown 780316643e7SJed Brown PetscFunctionBegin; 7810700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 7820910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 7830910c330SBarry Smith PetscValidHeaderSpecific(Udot, VEC_CLASSID, 4); 7840700a824SBarry Smith PetscValidHeaderSpecific(Y, VEC_CLASSID, 5); 785316643e7SJed Brown 7869566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 7879566063dSJacob Faibussowitsch PetscCall(DMTSGetIFunction(dm, &ifunction, &ctx)); 7889566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, NULL)); 78924989b8cSPeter Brune 7903c633725SBarry Smith PetscCheck(rhsfunction || ifunction, PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "Must call TSSetRHSFunction() and / or TSSetIFunction()"); 791d90be118SSean Farley 7929566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_FunctionEval, ts, U, Udot, Y)); 79324989b8cSPeter Brune if (ifunction) { 794792fecdfSBarry Smith PetscCallBack("TS callback implicit function", (*ifunction)(ts, t, U, Udot, Y, ctx)); 795a6ab3590SBarry Smith ts->ifuncs++; 796214bc6a2SJed Brown } 797214bc6a2SJed Brown if (imex) { 7981e66621cSBarry Smith if (!ifunction) PetscCall(VecCopy(Udot, Y)); 79924989b8cSPeter Brune } else if (rhsfunction) { 80024989b8cSPeter Brune if (ifunction) { 801214bc6a2SJed Brown Vec Frhs; 8029566063dSJacob Faibussowitsch PetscCall(TSGetRHSVec_Private(ts, &Frhs)); 8039566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts, t, U, Frhs)); 8049566063dSJacob Faibussowitsch PetscCall(VecAXPY(Y, -1, Frhs)); 8052dd45cf8SJed Brown } else { 8069566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts, t, U, Y)); 8079566063dSJacob Faibussowitsch PetscCall(VecAYPX(Y, -1, Udot)); 808316643e7SJed Brown } 8094a6899ffSJed Brown } 8109566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_FunctionEval, ts, U, Udot, Y)); 8113ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 812316643e7SJed Brown } 813316643e7SJed Brown 814cfa8a9a2SHong Zhang /* 815*195e9b02SBarry Smith TSRecoverRHSJacobian - Recover the Jacobian matrix so that one can call `TSComputeRHSJacobian()` on it. 816cfa8a9a2SHong Zhang 817cfa8a9a2SHong Zhang Note: 818*195e9b02SBarry Smith This routine is needed when one switches from `TSComputeIJacobian()` to `TSComputeRHSJacobian()` because the Jacobian matrix may be shifted or scaled in `TSComputeIJacobian()`. 819cfa8a9a2SHong Zhang 820cfa8a9a2SHong Zhang */ 821d71ae5a4SJacob Faibussowitsch static PetscErrorCode TSRecoverRHSJacobian(TS ts, Mat A, Mat B) 822d71ae5a4SJacob Faibussowitsch { 823cfa8a9a2SHong Zhang PetscFunctionBegin; 824cfa8a9a2SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 8253c633725SBarry Smith PetscCheck(A == ts->Arhs, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Invalid Amat"); 8263c633725SBarry Smith PetscCheck(B == ts->Brhs, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Invalid Bmat"); 827cfa8a9a2SHong Zhang 8281baa6e33SBarry Smith if (ts->rhsjacobian.shift) PetscCall(MatShift(A, -ts->rhsjacobian.shift)); 82948a46eb9SPierre Jolivet if (ts->rhsjacobian.scale == -1.) PetscCall(MatScale(A, -1)); 830cfa8a9a2SHong Zhang if (B && B == ts->Brhs && A != B) { 8311baa6e33SBarry Smith if (ts->rhsjacobian.shift) PetscCall(MatShift(B, -ts->rhsjacobian.shift)); 8321e66621cSBarry Smith if (ts->rhsjacobian.scale == -1.) PetscCall(MatScale(B, -1)); 833cfa8a9a2SHong Zhang } 834cfa8a9a2SHong Zhang ts->rhsjacobian.shift = 0; 835cfa8a9a2SHong Zhang ts->rhsjacobian.scale = 1.; 8363ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 837cfa8a9a2SHong Zhang } 838cfa8a9a2SHong Zhang 839316643e7SJed Brown /*@ 840316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 841316643e7SJed Brown 842c3339decSBarry Smith Collective 843316643e7SJed Brown 844316643e7SJed Brown Input 845316643e7SJed Brown Input Parameters: 846bcf0153eSBarry Smith + ts - the `TS` context 847316643e7SJed Brown . t - current timestep 8480910c330SBarry Smith . U - state vector 8490910c330SBarry Smith . Udot - time derivative of state vector 850214bc6a2SJed Brown . shift - shift to apply, see note below 851bcf0153eSBarry Smith - imex - flag indicates if the method is `TSIMEX` so that the RHSJacobian should be kept separate 852316643e7SJed Brown 853316643e7SJed Brown Output Parameters: 854316643e7SJed Brown + A - Jacobian matrix 855*195e9b02SBarry Smith - B - matrix from which the preconditioner is constructed; often the same as `A` 856316643e7SJed Brown 857bcf0153eSBarry Smith Level: developer 858bcf0153eSBarry Smith 859316643e7SJed Brown Notes: 8600910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 861*195e9b02SBarry Smith .vb 8620910c330SBarry Smith dF/dU + shift*dF/dUdot 863*195e9b02SBarry Smith .ve 864316643e7SJed Brown Most users should not need to explicitly call this routine, as it 865316643e7SJed Brown is used internally within the nonlinear solvers. 866316643e7SJed Brown 867bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetIJacobian()` 86863495f91SJed Brown @*/ 869d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIJacobian(TS ts, PetscReal t, Vec U, Vec Udot, PetscReal shift, Mat A, Mat B, PetscBool imex) 870d71ae5a4SJacob Faibussowitsch { 87124989b8cSPeter Brune TSIJacobian ijacobian; 87224989b8cSPeter Brune TSRHSJacobian rhsjacobian; 87324989b8cSPeter Brune DM dm; 87424989b8cSPeter Brune void *ctx; 875316643e7SJed Brown 876316643e7SJed Brown PetscFunctionBegin; 8770700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 8780910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 8790910c330SBarry Smith PetscValidHeaderSpecific(Udot, VEC_CLASSID, 4); 880316643e7SJed Brown PetscValidPointer(A, 6); 88194ab13aaSBarry Smith PetscValidHeaderSpecific(A, MAT_CLASSID, 6); 882316643e7SJed Brown PetscValidPointer(B, 7); 88394ab13aaSBarry Smith PetscValidHeaderSpecific(B, MAT_CLASSID, 7); 88424989b8cSPeter Brune 8859566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 8869566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm, &ijacobian, &ctx)); 8879566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, &rhsjacobian, NULL)); 88824989b8cSPeter Brune 8893c633725SBarry Smith PetscCheck(rhsjacobian || ijacobian, PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 890316643e7SJed Brown 8919566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_JacobianEval, ts, U, A, B)); 89224989b8cSPeter Brune if (ijacobian) { 893792fecdfSBarry Smith PetscCallBack("TS callback implicit Jacobian", (*ijacobian)(ts, t, U, Udot, shift, A, B, ctx)); 894a6ab3590SBarry Smith ts->ijacs++; 8954a6899ffSJed Brown } 896214bc6a2SJed Brown if (imex) { 897b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 8984c26be97Sstefano_zampini PetscBool assembled; 899971015bcSStefano Zampini if (rhsjacobian) { 900971015bcSStefano Zampini Mat Arhs = NULL; 9019566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts, &Arhs, NULL)); 902971015bcSStefano Zampini if (A == Arhs) { 9033c633725SBarry 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 */ 904971015bcSStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 905971015bcSStefano Zampini } 906971015bcSStefano Zampini } 9079566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(A)); 9089566063dSJacob Faibussowitsch PetscCall(MatAssembled(A, &assembled)); 9094c26be97Sstefano_zampini if (!assembled) { 9109566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); 9119566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); 9124c26be97Sstefano_zampini } 9139566063dSJacob Faibussowitsch PetscCall(MatShift(A, shift)); 91494ab13aaSBarry Smith if (A != B) { 9159566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(B)); 9169566063dSJacob Faibussowitsch PetscCall(MatAssembled(B, &assembled)); 9174c26be97Sstefano_zampini if (!assembled) { 9189566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY)); 9199566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY)); 9204c26be97Sstefano_zampini } 9219566063dSJacob Faibussowitsch PetscCall(MatShift(B, shift)); 922214bc6a2SJed Brown } 923214bc6a2SJed Brown } 924214bc6a2SJed Brown } else { 925e1244c69SJed Brown Mat Arhs = NULL, Brhs = NULL; 9261e66621cSBarry Smith 9271e66621cSBarry Smith /* RHSJacobian needs to be converted to part of IJacobian if exists */ 9281e66621cSBarry Smith if (rhsjacobian) PetscCall(TSGetRHSMats_Private(ts, &Arhs, &Brhs)); 929e8b1e424SHong Zhang if (Arhs == A) { /* No IJacobian matrix, so we only have the RHS matrix */ 930e8b1e424SHong Zhang PetscObjectState Ustate; 931e8b1e424SHong Zhang PetscObjectId Uid; 932e8b1e424SHong Zhang TSRHSFunction rhsfunction; 933e8b1e424SHong Zhang 9349566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, NULL)); 9359566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &Ustate)); 9369566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U, &Uid)); 9379371c9d4SSatish Balay if ((rhsjacobian == TSComputeRHSJacobianConstant || (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && rhsfunction != TSComputeRHSFunctionLinear)) && 9389371c9d4SSatish Balay ts->rhsjacobian.scale == -1.) { /* No need to recompute RHSJacobian */ 9399566063dSJacob Faibussowitsch PetscCall(MatShift(A, shift - ts->rhsjacobian.shift)); /* revert the old shift and add the new shift with a single call to MatShift */ 9401e66621cSBarry Smith if (A != B) PetscCall(MatShift(B, shift - ts->rhsjacobian.shift)); 941e8b1e424SHong Zhang } else { 9423565c898SBarry Smith PetscBool flg; 943e8b1e424SHong Zhang 944e8b1e424SHong Zhang if (ts->rhsjacobian.reuse) { /* Undo the damage */ 945e8b1e424SHong Zhang /* MatScale has a short path for this case. 946e8b1e424SHong Zhang However, this code path is taken the first time TSComputeRHSJacobian is called 947e8b1e424SHong Zhang and the matrices have not been assembled yet */ 9489566063dSJacob Faibussowitsch PetscCall(TSRecoverRHSJacobian(ts, A, B)); 949e8b1e424SHong Zhang } 9509566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts, t, U, A, B)); 9519566063dSJacob Faibussowitsch PetscCall(SNESGetUseMatrixFree(ts->snes, NULL, &flg)); 9523565c898SBarry Smith /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */ 9533565c898SBarry Smith if (!flg) { 9549566063dSJacob Faibussowitsch PetscCall(MatScale(A, -1)); 9559566063dSJacob Faibussowitsch PetscCall(MatShift(A, shift)); 9563565c898SBarry Smith } 95794ab13aaSBarry Smith if (A != B) { 9589566063dSJacob Faibussowitsch PetscCall(MatScale(B, -1)); 9599566063dSJacob Faibussowitsch PetscCall(MatShift(B, shift)); 960316643e7SJed Brown } 961e8b1e424SHong Zhang } 962e8b1e424SHong Zhang ts->rhsjacobian.scale = -1; 963e8b1e424SHong Zhang ts->rhsjacobian.shift = shift; 964d60b7d5cSBarry Smith } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 965e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 9669566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(A)); 9679566063dSJacob Faibussowitsch PetscCall(MatShift(A, shift)); 96894ab13aaSBarry Smith if (A != B) { 9699566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(B)); 9709566063dSJacob Faibussowitsch PetscCall(MatShift(B, shift)); 971214bc6a2SJed Brown } 972316643e7SJed Brown } 9739566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts, t, U, Arhs, Brhs)); 9749566063dSJacob Faibussowitsch PetscCall(MatAXPY(A, -1, Arhs, ts->axpy_pattern)); 9751e66621cSBarry Smith if (A != B) PetscCall(MatAXPY(B, -1, Brhs, ts->axpy_pattern)); 976316643e7SJed Brown } 977316643e7SJed Brown } 9789566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_JacobianEval, ts, U, A, B)); 9793ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 980316643e7SJed Brown } 981316643e7SJed Brown 982d763cef2SBarry Smith /*@C 983d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 984b5abc632SBarry Smith where U_t = G(t,u). 985d763cef2SBarry Smith 986c3339decSBarry Smith Logically Collective 987d763cef2SBarry Smith 988d763cef2SBarry Smith Input Parameters: 989bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 990*195e9b02SBarry Smith . r - vector to put the computed right hand side (or `NULL` to have it created) 991d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 992*195e9b02SBarry Smith - ctx - [optional] user-defined context for private data for the function evaluation routine (may be `NULL`) 993d763cef2SBarry Smith 994a96d6ef6SBarry Smith Calling sequence of f: 995a96d6ef6SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec F,void *ctx); 996d763cef2SBarry Smith 997a96d6ef6SBarry Smith + ts - timestep context 998a96d6ef6SBarry Smith . t - current timestep 999d763cef2SBarry Smith . u - input vector 1000d763cef2SBarry Smith . F - function vector 1001d763cef2SBarry Smith - ctx - [optional] user-defined function context 1002d763cef2SBarry Smith 1003d763cef2SBarry Smith Level: beginner 1004d763cef2SBarry Smith 1005bcf0153eSBarry Smith Note: 1006bcf0153eSBarry Smith You must call this function or `TSSetIFunction()` to define your ODE. You cannot use this function when solving a DAE. 10072bbac0d3SBarry Smith 1008bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetRHSJacobian()`, `TSSetIJacobian()`, `TSSetIFunction()` 1009d763cef2SBarry Smith @*/ 1010d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetRHSFunction(TS ts, Vec r, PetscErrorCode (*f)(TS, PetscReal, Vec, Vec, void *), void *ctx) 1011d71ae5a4SJacob Faibussowitsch { 1012089b2837SJed Brown SNES snes; 10130298fd71SBarry Smith Vec ralloc = NULL; 101424989b8cSPeter Brune DM dm; 1015d763cef2SBarry Smith 1016089b2837SJed Brown PetscFunctionBegin; 10170700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1018ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r, VEC_CLASSID, 2); 101924989b8cSPeter Brune 10209566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 10219566063dSJacob Faibussowitsch PetscCall(DMTSSetRHSFunction(dm, f, ctx)); 10229566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 1023e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 10249566063dSJacob Faibussowitsch PetscCall(VecDuplicate(ts->vec_sol, &ralloc)); 1025e856ceecSJed Brown r = ralloc; 1026e856ceecSJed Brown } 10279566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(snes, r, SNESTSFormFunction, ts)); 10289566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ralloc)); 10293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1030d763cef2SBarry Smith } 1031d763cef2SBarry Smith 1032ef20d060SBarry Smith /*@C 1033abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1034ef20d060SBarry Smith 1035c3339decSBarry Smith Logically Collective 1036ef20d060SBarry Smith 1037ef20d060SBarry Smith Input Parameters: 1038bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1039ef20d060SBarry Smith . f - routine for evaluating the solution 1040ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 1041*195e9b02SBarry Smith function evaluation routine (may be `NULL`) 1042ef20d060SBarry Smith 1043a96d6ef6SBarry Smith Calling sequence of f: 1044a96d6ef6SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,void *ctx); 1045ef20d060SBarry Smith 1046ef20d060SBarry Smith + t - current timestep 1047ef20d060SBarry Smith . u - output vector 1048ef20d060SBarry Smith - ctx - [optional] user-defined function context 1049ef20d060SBarry Smith 1050bcf0153eSBarry Smith Options Database Keys: 1051bcf0153eSBarry Smith + -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided `TSSetSolutionFunction()` 1052bcf0153eSBarry Smith - -ts_monitor_draw_error - Monitor error graphically, requires user to have provided `TSSetSolutionFunction()` 1053bcf0153eSBarry Smith 1054bcf0153eSBarry Smith Level: intermediate 10550ed3bfb6SBarry Smith 1056abd5a294SJed Brown Notes: 1057abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 1058abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 1059abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 1060abd5a294SJed Brown 1061bcf0153eSBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, `TSMonitorLGError()` can be used to monitor the error history. 1062abd5a294SJed Brown 1063bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetRHSJacobian()`, `TSSetIJacobian()`, `TSComputeSolutionFunction()`, `TSSetForcingFunction()`, `TSSetSolution()`, `TSGetSolution()`, `TSMonitorLGError()`, `TSMonitorDrawError()` 1064ef20d060SBarry Smith @*/ 1065d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetSolutionFunction(TS ts, PetscErrorCode (*f)(TS, PetscReal, Vec, void *), void *ctx) 1066d71ae5a4SJacob Faibussowitsch { 1067ef20d060SBarry Smith DM dm; 1068ef20d060SBarry Smith 1069ef20d060SBarry Smith PetscFunctionBegin; 1070ef20d060SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 10719566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 10729566063dSJacob Faibussowitsch PetscCall(DMTSSetSolutionFunction(dm, f, ctx)); 10733ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1074ef20d060SBarry Smith } 1075ef20d060SBarry Smith 10769b7cd975SBarry Smith /*@C 10779b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 10789b7cd975SBarry Smith 1079c3339decSBarry Smith Logically Collective 10809b7cd975SBarry Smith 10819b7cd975SBarry Smith Input Parameters: 1082bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1083e162b725SBarry Smith . func - routine for evaluating the forcing function 10849b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 1085*195e9b02SBarry Smith function evaluation routine (may be `NULL`) 10869b7cd975SBarry Smith 10879b7cd975SBarry Smith Calling sequence of func: 10886bc98fa9SBarry Smith $ PetscErrorCode func (TS ts,PetscReal t,Vec f,void *ctx); 10899b7cd975SBarry Smith 10909b7cd975SBarry Smith + t - current timestep 1091e162b725SBarry Smith . f - output vector 10929b7cd975SBarry Smith - ctx - [optional] user-defined function context 10939b7cd975SBarry Smith 1094bcf0153eSBarry Smith Level: intermediate 1095bcf0153eSBarry Smith 10969b7cd975SBarry Smith Notes: 10979b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 1098e162b725SBarry 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 1099e162b725SBarry Smith definition of the problem you are solving and hence possibly introducing bugs. 1100e162b725SBarry Smith 1101e162b725SBarry Smith This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0 1102e162b725SBarry Smith 1103e162b725SBarry 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 1104e162b725SBarry Smith parameters can be passed in the ctx variable. 11059b7cd975SBarry Smith 1106bcf0153eSBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, `TSMonitorLGError()` can be used to monitor the error history. 11079b7cd975SBarry Smith 1108bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetRHSJacobian()`, `TSSetIJacobian()`, `TSComputeSolutionFunction()`, `TSSetSolutionFunction()` 11099b7cd975SBarry Smith @*/ 1110d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetForcingFunction(TS ts, TSForcingFunction func, void *ctx) 1111d71ae5a4SJacob Faibussowitsch { 11129b7cd975SBarry Smith DM dm; 11139b7cd975SBarry Smith 11149b7cd975SBarry Smith PetscFunctionBegin; 11159b7cd975SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 11169566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 11179566063dSJacob Faibussowitsch PetscCall(DMTSSetForcingFunction(dm, func, ctx)); 11183ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 11199b7cd975SBarry Smith } 11209b7cd975SBarry Smith 1121d763cef2SBarry Smith /*@C 1122f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1123b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1124d763cef2SBarry Smith 1125c3339decSBarry Smith Logically Collective 1126d763cef2SBarry Smith 1127d763cef2SBarry Smith Input Parameters: 1128bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1129*195e9b02SBarry Smith . Amat - (approximate) location to store Jacobian matrix entries computed by `f` 1130*195e9b02SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as `Amat`) 1131d763cef2SBarry Smith . f - the Jacobian evaluation routine 1132*195e9b02SBarry Smith - ctx - [optional] user-defined context for private data for the Jacobian evaluation routine (may be `NULL`) 1133d763cef2SBarry Smith 1134f7ab8db6SBarry Smith Calling sequence of f: 1135a96d6ef6SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1136d763cef2SBarry Smith 1137d763cef2SBarry Smith + t - current timestep 1138d763cef2SBarry Smith . u - input vector 1139e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1140*195e9b02SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as `Amat`) 1141d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1142d763cef2SBarry Smith 1143bcf0153eSBarry Smith Level: beginner 1144bcf0153eSBarry Smith 11456cd88445SBarry Smith Notes: 11466cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 11476cd88445SBarry Smith 1148bcf0153eSBarry Smith The `TS` solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1149ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1150d763cef2SBarry Smith 1151bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `SNESComputeJacobianDefaultColor()`, `TSSetRHSFunction()`, `TSRHSJacobianSetReuse()`, `TSSetIJacobian()` 1152d763cef2SBarry Smith @*/ 1153d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetRHSJacobian(TS ts, Mat Amat, Mat Pmat, TSRHSJacobian f, void *ctx) 1154d71ae5a4SJacob Faibussowitsch { 1155089b2837SJed Brown SNES snes; 115624989b8cSPeter Brune DM dm; 115724989b8cSPeter Brune TSIJacobian ijacobian; 1158277b19d0SLisandro Dalcin 1159d763cef2SBarry Smith PetscFunctionBegin; 11600700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1161e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat, MAT_CLASSID, 2); 1162e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat, MAT_CLASSID, 3); 1163e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts, 1, Amat, 2); 1164e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts, 1, Pmat, 3); 1165d763cef2SBarry Smith 11669566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 11679566063dSJacob Faibussowitsch PetscCall(DMTSSetRHSJacobian(dm, f, ctx)); 11689566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm, &ijacobian, NULL)); 11699566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 11701e66621cSBarry Smith if (!ijacobian) PetscCall(SNESSetJacobian(snes, Amat, Pmat, SNESTSFormJacobian, ts)); 1171e5d3d808SBarry Smith if (Amat) { 11729566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)Amat)); 11739566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Arhs)); 1174e5d3d808SBarry Smith ts->Arhs = Amat; 11750e4ef248SJed Brown } 1176e5d3d808SBarry Smith if (Pmat) { 11779566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)Pmat)); 11789566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Brhs)); 1179e5d3d808SBarry Smith ts->Brhs = Pmat; 11800e4ef248SJed Brown } 11813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1182d763cef2SBarry Smith } 1183d763cef2SBarry Smith 1184316643e7SJed Brown /*@C 1185b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1186316643e7SJed Brown 1187c3339decSBarry Smith Logically Collective 1188316643e7SJed Brown 1189316643e7SJed Brown Input Parameters: 1190bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1191*195e9b02SBarry Smith . r - vector to hold the residual (or `NULL` to have it created internally) 1192316643e7SJed Brown . f - the function evaluation routine 1193*195e9b02SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be `NULL`) 1194316643e7SJed Brown 1195316643e7SJed Brown Calling sequence of f: 11966bc98fa9SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1197316643e7SJed Brown 1198316643e7SJed Brown + t - time at step/stage being solved 1199316643e7SJed Brown . u - state vector 1200316643e7SJed Brown . u_t - time derivative of state vector 1201316643e7SJed Brown . F - function vector 1202316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1203316643e7SJed Brown 1204316643e7SJed Brown Level: beginner 1205316643e7SJed Brown 1206bcf0153eSBarry Smith Note: 1207bcf0153eSBarry Smith The user MUST call either this routine or `TSSetRHSFunction()` to define the ODE. When solving DAEs you must use this function. 1208bcf0153eSBarry Smith 1209bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetRHSJacobian()`, `TSSetRHSFunction()`, `TSSetIJacobian()` 1210316643e7SJed Brown @*/ 1211d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetIFunction(TS ts, Vec r, TSIFunction f, void *ctx) 1212d71ae5a4SJacob Faibussowitsch { 1213089b2837SJed Brown SNES snes; 121451699248SLisandro Dalcin Vec ralloc = NULL; 121524989b8cSPeter Brune DM dm; 1216316643e7SJed Brown 1217316643e7SJed Brown PetscFunctionBegin; 12180700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 121951699248SLisandro Dalcin if (r) PetscValidHeaderSpecific(r, VEC_CLASSID, 2); 122024989b8cSPeter Brune 12219566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 12229566063dSJacob Faibussowitsch PetscCall(DMTSSetIFunction(dm, f, ctx)); 122324989b8cSPeter Brune 12249566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 122551699248SLisandro Dalcin if (!r && !ts->dm && ts->vec_sol) { 12269566063dSJacob Faibussowitsch PetscCall(VecDuplicate(ts->vec_sol, &ralloc)); 122751699248SLisandro Dalcin r = ralloc; 1228e856ceecSJed Brown } 12299566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(snes, r, SNESTSFormFunction, ts)); 12309566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ralloc)); 12313ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1232089b2837SJed Brown } 1233089b2837SJed Brown 1234089b2837SJed Brown /*@C 1235a5b23f4aSJose E. Roman TSGetIFunction - Returns the vector where the implicit residual is stored and the function/context to compute it. 1236089b2837SJed Brown 1237089b2837SJed Brown Not Collective 1238089b2837SJed Brown 1239089b2837SJed Brown Input Parameter: 1240bcf0153eSBarry Smith . ts - the `TS` context 1241089b2837SJed Brown 1242d8d19677SJose E. Roman Output Parameters: 1243*195e9b02SBarry Smith + r - vector to hold residual (or `NULL`) 1244*195e9b02SBarry Smith . func - the function to compute residual (or `NULL`) 1245*195e9b02SBarry Smith - ctx - the function context (or `NULL`) 1246089b2837SJed Brown 1247089b2837SJed Brown Level: advanced 1248089b2837SJed Brown 1249bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetIFunction()`, `SNESGetFunction()` 1250089b2837SJed Brown @*/ 1251d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetIFunction(TS ts, Vec *r, TSIFunction *func, void **ctx) 1252d71ae5a4SJacob Faibussowitsch { 1253089b2837SJed Brown SNES snes; 125424989b8cSPeter Brune DM dm; 1255089b2837SJed Brown 1256089b2837SJed Brown PetscFunctionBegin; 1257089b2837SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 12589566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 12599566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes, r, NULL, NULL)); 12609566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 12619566063dSJacob Faibussowitsch PetscCall(DMTSGetIFunction(dm, func, ctx)); 12623ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1263089b2837SJed Brown } 1264089b2837SJed Brown 1265089b2837SJed Brown /*@C 1266089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1267089b2837SJed Brown 1268089b2837SJed Brown Not Collective 1269089b2837SJed Brown 1270089b2837SJed Brown Input Parameter: 1271bcf0153eSBarry Smith . ts - the `TS` context 1272089b2837SJed Brown 1273d8d19677SJose E. Roman Output Parameters: 1274*195e9b02SBarry Smith + r - vector to hold computed right hand side (or `NULL`) 1275*195e9b02SBarry Smith . func - the function to compute right hand side (or `NULL`) 1276*195e9b02SBarry Smith - ctx - the function context (or `NULL`) 1277089b2837SJed Brown 1278089b2837SJed Brown Level: advanced 1279089b2837SJed Brown 1280bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetRHSFunction()`, `SNESGetFunction()` 1281089b2837SJed Brown @*/ 1282d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetRHSFunction(TS ts, Vec *r, TSRHSFunction *func, void **ctx) 1283d71ae5a4SJacob Faibussowitsch { 1284089b2837SJed Brown SNES snes; 128524989b8cSPeter Brune DM dm; 1286089b2837SJed Brown 1287089b2837SJed Brown PetscFunctionBegin; 1288089b2837SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 12899566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 12909566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes, r, NULL, NULL)); 12919566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 12929566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, func, ctx)); 12933ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1294316643e7SJed Brown } 1295316643e7SJed Brown 1296316643e7SJed Brown /*@C 1297a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1298bcf0153eSBarry Smith provided with `TSSetIFunction()`. 1299316643e7SJed Brown 1300c3339decSBarry Smith Logically Collective 1301316643e7SJed Brown 1302316643e7SJed Brown Input Parameters: 1303bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1304*195e9b02SBarry Smith . Amat - (approximate) matrix to store Jacobian entires computed by `f` 1305*195e9b02SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as `Amat`) 1306316643e7SJed Brown . f - the Jacobian evaluation routine 1307*195e9b02SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be `NULL`) 1308316643e7SJed Brown 1309316643e7SJed Brown Calling sequence of f: 13106bc98fa9SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1311316643e7SJed Brown 1312316643e7SJed Brown + t - time at step/stage being solved 13131b4a444bSJed Brown . U - state vector 13141b4a444bSJed Brown . U_t - time derivative of state vector 1315316643e7SJed Brown . a - shift 1316e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1317*195e9b02SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as `Amat` 1318316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1319316643e7SJed Brown 1320bcf0153eSBarry Smith Level: beginner 1321316643e7SJed Brown 1322bcf0153eSBarry Smith Notes: 1323*195e9b02SBarry Smith The matrices `Amat` and `Pmat` are exactly the matrices that are used by `SNES` for the nonlinear solve. 1324bcf0153eSBarry Smith 1325bcf0153eSBarry Smith If you know the operator Amat has a null space you can use `MatSetNullSpace()` and `MatSetTransposeNullSpace()` to supply the null 1326*195e9b02SBarry Smith space to `Amat` and the `KSP` solvers will automatically use that null space as needed during the solution process. 1327895c21f2SBarry Smith 1328a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1329b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1330a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1331a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1332a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1333a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1334a4f0a591SBarry Smith 13356cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 13366cd88445SBarry Smith 1337*195e9b02SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices `Amat` and `Pmat` between the calls to `f` 1338*195e9b02SBarry Smith You should not assume the values are the same in the next call to `f` as you set them in the previous call. 1339ca5f011dSBarry Smith 1340bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetIFunction()`, `TSSetRHSJacobian()`, `SNESComputeJacobianDefaultColor()`, `SNESComputeJacobianDefault()`, `TSSetRHSFunction()` 1341316643e7SJed Brown @*/ 1342d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetIJacobian(TS ts, Mat Amat, Mat Pmat, TSIJacobian f, void *ctx) 1343d71ae5a4SJacob Faibussowitsch { 1344089b2837SJed Brown SNES snes; 134524989b8cSPeter Brune DM dm; 1346316643e7SJed Brown 1347316643e7SJed Brown PetscFunctionBegin; 13480700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1349e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat, MAT_CLASSID, 2); 1350e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat, MAT_CLASSID, 3); 1351e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts, 1, Amat, 2); 1352e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts, 1, Pmat, 3); 135324989b8cSPeter Brune 13549566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 13559566063dSJacob Faibussowitsch PetscCall(DMTSSetIJacobian(dm, f, ctx)); 135624989b8cSPeter Brune 13579566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 13589566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes, Amat, Pmat, SNESTSFormJacobian, ts)); 13593ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1360316643e7SJed Brown } 1361316643e7SJed Brown 1362e1244c69SJed Brown /*@ 1363bcf0153eSBarry Smith TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, `TS` will change the sign and 1364e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1365e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1366*195e9b02SBarry Smith not been changed by the `TS`. 1367e1244c69SJed Brown 1368e1244c69SJed Brown Logically Collective 1369e1244c69SJed Brown 13704165533cSJose E. Roman Input Parameters: 1371bcf0153eSBarry Smith + ts - `TS` context obtained from `TSCreate()` 1372bcf0153eSBarry Smith - reuse - `PETSC_TRUE` if the RHS Jacobian 1373e1244c69SJed Brown 1374e1244c69SJed Brown Level: intermediate 1375e1244c69SJed Brown 1376bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetRHSJacobian()`, `TSComputeRHSJacobianConstant()` 1377e1244c69SJed Brown @*/ 1378d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRHSJacobianSetReuse(TS ts, PetscBool reuse) 1379d71ae5a4SJacob Faibussowitsch { 1380e1244c69SJed Brown PetscFunctionBegin; 1381e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 13823ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1383e1244c69SJed Brown } 1384e1244c69SJed Brown 1385efe9872eSLisandro Dalcin /*@C 1386efe9872eSLisandro Dalcin TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved. 1387efe9872eSLisandro Dalcin 1388c3339decSBarry Smith Logically Collective 1389efe9872eSLisandro Dalcin 1390efe9872eSLisandro Dalcin Input Parameters: 1391bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1392*195e9b02SBarry Smith . F - vector to hold the residual (or `NULL` to have it created internally) 1393efe9872eSLisandro Dalcin . fun - the function evaluation routine 1394*195e9b02SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be `NULL`) 1395efe9872eSLisandro Dalcin 1396efe9872eSLisandro Dalcin Calling sequence of fun: 13976bc98fa9SBarry Smith $ PetscErrorCode fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx); 1398efe9872eSLisandro Dalcin 1399efe9872eSLisandro Dalcin + t - time at step/stage being solved 1400efe9872eSLisandro Dalcin . U - state vector 1401efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1402efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1403efe9872eSLisandro Dalcin . F - function vector 1404*195e9b02SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine (may be `NULL`) 1405efe9872eSLisandro Dalcin 1406efe9872eSLisandro Dalcin Level: beginner 1407efe9872eSLisandro Dalcin 1408bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetI2Jacobian()`, `TSSetIFunction()`, `TSCreate()`, `TSSetRHSFunction()` 1409efe9872eSLisandro Dalcin @*/ 1410d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetI2Function(TS ts, Vec F, TSI2Function fun, void *ctx) 1411d71ae5a4SJacob Faibussowitsch { 1412efe9872eSLisandro Dalcin DM dm; 1413efe9872eSLisandro Dalcin 1414efe9872eSLisandro Dalcin PetscFunctionBegin; 1415efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1416efe9872eSLisandro Dalcin if (F) PetscValidHeaderSpecific(F, VEC_CLASSID, 2); 14179566063dSJacob Faibussowitsch PetscCall(TSSetIFunction(ts, F, NULL, NULL)); 14189566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 14199566063dSJacob Faibussowitsch PetscCall(DMTSSetI2Function(dm, fun, ctx)); 14203ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1421efe9872eSLisandro Dalcin } 1422efe9872eSLisandro Dalcin 1423efe9872eSLisandro Dalcin /*@C 1424a5b23f4aSJose E. Roman TSGetI2Function - Returns the vector where the implicit residual is stored and the function/context to compute it. 1425efe9872eSLisandro Dalcin 1426efe9872eSLisandro Dalcin Not Collective 1427efe9872eSLisandro Dalcin 1428efe9872eSLisandro Dalcin Input Parameter: 1429bcf0153eSBarry Smith . ts - the `TS` context 1430efe9872eSLisandro Dalcin 1431d8d19677SJose E. Roman Output Parameters: 1432*195e9b02SBarry Smith + r - vector to hold residual (or `NULL`) 1433*195e9b02SBarry Smith . fun - the function to compute residual (or `NULL`) 1434*195e9b02SBarry Smith - ctx - the function context (or `NULL`) 1435efe9872eSLisandro Dalcin 1436efe9872eSLisandro Dalcin Level: advanced 1437efe9872eSLisandro Dalcin 1438bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetIFunction()`, `SNESGetFunction()`, `TSCreate()` 1439efe9872eSLisandro Dalcin @*/ 1440d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetI2Function(TS ts, Vec *r, TSI2Function *fun, void **ctx) 1441d71ae5a4SJacob Faibussowitsch { 1442efe9872eSLisandro Dalcin SNES snes; 1443efe9872eSLisandro Dalcin DM dm; 1444efe9872eSLisandro Dalcin 1445efe9872eSLisandro Dalcin PetscFunctionBegin; 1446efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 14479566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 14489566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes, r, NULL, NULL)); 14499566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 14509566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Function(dm, fun, ctx)); 14513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1452efe9872eSLisandro Dalcin } 1453efe9872eSLisandro Dalcin 1454efe9872eSLisandro Dalcin /*@C 1455bc77d74cSLisandro Dalcin TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t + a*dF/dU_tt 1456bcf0153eSBarry Smith where F(t,U,U_t,U_tt) is the function you provided with `TSSetI2Function()`. 1457efe9872eSLisandro Dalcin 1458c3339decSBarry Smith Logically Collective 1459efe9872eSLisandro Dalcin 1460efe9872eSLisandro Dalcin Input Parameters: 1461bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1462*195e9b02SBarry Smith . J - matrix to hold the Jacobian values 1463*195e9b02SBarry Smith . P - matrix for constructing the preconditioner (may be same as `J`) 1464efe9872eSLisandro Dalcin . jac - the Jacobian evaluation routine 1465*195e9b02SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be `NULL`) 1466efe9872eSLisandro Dalcin 1467efe9872eSLisandro Dalcin Calling sequence of jac: 14686bc98fa9SBarry Smith $ PetscErrorCode jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx); 1469efe9872eSLisandro Dalcin 1470efe9872eSLisandro Dalcin + t - time at step/stage being solved 1471efe9872eSLisandro Dalcin . U - state vector 1472efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1473efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1474efe9872eSLisandro Dalcin . v - shift for U_t 1475efe9872eSLisandro Dalcin . a - shift for U_tt 1476efe9872eSLisandro Dalcin . J - Jacobian of G(U) = F(t,U,W+v*U,W'+a*U), equivalent to dF/dU + v*dF/dU_t + a*dF/dU_tt 1477efe9872eSLisandro Dalcin . P - preconditioning matrix for J, may be same as J 1478efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine 1479efe9872eSLisandro Dalcin 1480bcf0153eSBarry Smith Level: beginner 1481bcf0153eSBarry Smith 1482efe9872eSLisandro Dalcin Notes: 1483*195e9b02SBarry Smith The matrices `J` and `P` are exactly the matrices that are used by `SNES` for the nonlinear solve. 1484efe9872eSLisandro Dalcin 1485efe9872eSLisandro Dalcin The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be 1486efe9872eSLisandro 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. 1487efe9872eSLisandro Dalcin The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U where the positive "shift" 1488bc77d74cSLisandro Dalcin parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states. 1489efe9872eSLisandro Dalcin 1490bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetI2Function()`, `TSGetI2Jacobian()` 1491efe9872eSLisandro Dalcin @*/ 1492d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetI2Jacobian(TS ts, Mat J, Mat P, TSI2Jacobian jac, void *ctx) 1493d71ae5a4SJacob Faibussowitsch { 1494efe9872eSLisandro Dalcin DM dm; 1495efe9872eSLisandro Dalcin 1496efe9872eSLisandro Dalcin PetscFunctionBegin; 1497efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1498efe9872eSLisandro Dalcin if (J) PetscValidHeaderSpecific(J, MAT_CLASSID, 2); 1499efe9872eSLisandro Dalcin if (P) PetscValidHeaderSpecific(P, MAT_CLASSID, 3); 15009566063dSJacob Faibussowitsch PetscCall(TSSetIJacobian(ts, J, P, NULL, NULL)); 15019566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 15029566063dSJacob Faibussowitsch PetscCall(DMTSSetI2Jacobian(dm, jac, ctx)); 15033ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1504efe9872eSLisandro Dalcin } 1505efe9872eSLisandro Dalcin 1506efe9872eSLisandro Dalcin /*@C 1507efe9872eSLisandro Dalcin TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep. 1508efe9872eSLisandro Dalcin 1509bcf0153eSBarry Smith Not Collective, but parallel objects are returned if `TS` is parallel 1510efe9872eSLisandro Dalcin 1511efe9872eSLisandro Dalcin Input Parameter: 1512bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 1513efe9872eSLisandro Dalcin 1514efe9872eSLisandro Dalcin Output Parameters: 1515efe9872eSLisandro Dalcin + J - The (approximate) Jacobian of F(t,U,U_t,U_tt) 1516*195e9b02SBarry Smith . P - The matrix from which the preconditioner is constructed, often the same as `J` 1517efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices 1518efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine 1519efe9872eSLisandro Dalcin 1520bcf0153eSBarry Smith Level: advanced 1521bcf0153eSBarry Smith 1522*195e9b02SBarry Smith Note: 1523*195e9b02SBarry Smith You can pass in `NULL` for any return argument you do not need. 1524efe9872eSLisandro Dalcin 1525bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetTimeStep()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()`, `TSSetI2Jacobian()`, `TSGetI2Function()`, `TSCreate()` 1526efe9872eSLisandro Dalcin @*/ 1527d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetI2Jacobian(TS ts, Mat *J, Mat *P, TSI2Jacobian *jac, void **ctx) 1528d71ae5a4SJacob Faibussowitsch { 1529efe9872eSLisandro Dalcin SNES snes; 1530efe9872eSLisandro Dalcin DM dm; 1531efe9872eSLisandro Dalcin 1532efe9872eSLisandro Dalcin PetscFunctionBegin; 15339566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 15349566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 15359566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes, J, P, NULL, NULL)); 15369566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 15379566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm, jac, ctx)); 15383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1539efe9872eSLisandro Dalcin } 1540efe9872eSLisandro Dalcin 1541efe9872eSLisandro Dalcin /*@ 1542efe9872eSLisandro Dalcin TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0 1543efe9872eSLisandro Dalcin 1544c3339decSBarry Smith Collective 1545efe9872eSLisandro Dalcin 1546efe9872eSLisandro Dalcin Input Parameters: 1547bcf0153eSBarry Smith + ts - the `TS` context 1548efe9872eSLisandro Dalcin . t - current time 1549efe9872eSLisandro Dalcin . U - state vector 1550efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t) 1551efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt) 1552efe9872eSLisandro Dalcin 1553efe9872eSLisandro Dalcin Output Parameter: 1554efe9872eSLisandro Dalcin . F - the residual vector 1555efe9872eSLisandro Dalcin 1556bcf0153eSBarry Smith Level: developer 1557bcf0153eSBarry Smith 1558efe9872eSLisandro Dalcin Note: 1559efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1560efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1561efe9872eSLisandro Dalcin 1562bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetI2Function()`, `TSGetI2Function()` 1563efe9872eSLisandro Dalcin @*/ 1564d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeI2Function(TS ts, PetscReal t, Vec U, Vec V, Vec A, Vec F) 1565d71ae5a4SJacob Faibussowitsch { 1566efe9872eSLisandro Dalcin DM dm; 1567efe9872eSLisandro Dalcin TSI2Function I2Function; 1568efe9872eSLisandro Dalcin void *ctx; 1569efe9872eSLisandro Dalcin TSRHSFunction rhsfunction; 1570efe9872eSLisandro Dalcin 1571efe9872eSLisandro Dalcin PetscFunctionBegin; 1572efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1573efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 1574efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V, VEC_CLASSID, 4); 1575efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A, VEC_CLASSID, 5); 1576efe9872eSLisandro Dalcin PetscValidHeaderSpecific(F, VEC_CLASSID, 6); 1577efe9872eSLisandro Dalcin 15789566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 15799566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Function(dm, &I2Function, &ctx)); 15809566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, NULL)); 1581efe9872eSLisandro Dalcin 1582efe9872eSLisandro Dalcin if (!I2Function) { 15839566063dSJacob Faibussowitsch PetscCall(TSComputeIFunction(ts, t, U, A, F, PETSC_FALSE)); 15843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1585efe9872eSLisandro Dalcin } 1586efe9872eSLisandro Dalcin 15879566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_FunctionEval, ts, U, V, F)); 1588efe9872eSLisandro Dalcin 1589792fecdfSBarry Smith PetscCallBack("TS callback implicit function", I2Function(ts, t, U, V, A, F, ctx)); 1590efe9872eSLisandro Dalcin 1591efe9872eSLisandro Dalcin if (rhsfunction) { 1592efe9872eSLisandro Dalcin Vec Frhs; 15939566063dSJacob Faibussowitsch PetscCall(TSGetRHSVec_Private(ts, &Frhs)); 15949566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts, t, U, Frhs)); 15959566063dSJacob Faibussowitsch PetscCall(VecAXPY(F, -1, Frhs)); 1596efe9872eSLisandro Dalcin } 1597efe9872eSLisandro Dalcin 15989566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_FunctionEval, ts, U, V, F)); 15993ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1600efe9872eSLisandro Dalcin } 1601efe9872eSLisandro Dalcin 1602efe9872eSLisandro Dalcin /*@ 1603efe9872eSLisandro Dalcin TSComputeI2Jacobian - Evaluates the Jacobian of the DAE 1604efe9872eSLisandro Dalcin 1605c3339decSBarry Smith Collective 1606efe9872eSLisandro Dalcin 1607efe9872eSLisandro Dalcin Input Parameters: 1608bcf0153eSBarry Smith + ts - the `TS` context 1609efe9872eSLisandro Dalcin . t - current timestep 1610efe9872eSLisandro Dalcin . U - state vector 1611efe9872eSLisandro Dalcin . V - time derivative of state vector 1612efe9872eSLisandro Dalcin . A - second time derivative of state vector 1613efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below 1614efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below 1615efe9872eSLisandro Dalcin 1616efe9872eSLisandro Dalcin Output Parameters: 1617efe9872eSLisandro Dalcin + J - Jacobian matrix 1618efe9872eSLisandro Dalcin - P - optional preconditioning matrix 1619efe9872eSLisandro Dalcin 1620bcf0153eSBarry Smith Level: developer 1621bcf0153eSBarry Smith 1622efe9872eSLisandro Dalcin Notes: 1623efe9872eSLisandro Dalcin If F(t,U,V,A)=0 is the DAE, the required Jacobian is 1624efe9872eSLisandro Dalcin 1625efe9872eSLisandro Dalcin dF/dU + shiftV*dF/dV + shiftA*dF/dA 1626efe9872eSLisandro Dalcin 1627efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1628efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1629efe9872eSLisandro Dalcin 1630bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetI2Jacobian()` 1631efe9872eSLisandro Dalcin @*/ 1632d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeI2Jacobian(TS ts, PetscReal t, Vec U, Vec V, Vec A, PetscReal shiftV, PetscReal shiftA, Mat J, Mat P) 1633d71ae5a4SJacob Faibussowitsch { 1634efe9872eSLisandro Dalcin DM dm; 1635efe9872eSLisandro Dalcin TSI2Jacobian I2Jacobian; 1636efe9872eSLisandro Dalcin void *ctx; 1637efe9872eSLisandro Dalcin TSRHSJacobian rhsjacobian; 1638efe9872eSLisandro Dalcin 1639efe9872eSLisandro Dalcin PetscFunctionBegin; 1640efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1641efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 1642efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V, VEC_CLASSID, 4); 1643efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A, VEC_CLASSID, 5); 1644efe9872eSLisandro Dalcin PetscValidHeaderSpecific(J, MAT_CLASSID, 8); 1645efe9872eSLisandro Dalcin PetscValidHeaderSpecific(P, MAT_CLASSID, 9); 1646efe9872eSLisandro Dalcin 16479566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 16489566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm, &I2Jacobian, &ctx)); 16499566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, &rhsjacobian, NULL)); 1650efe9872eSLisandro Dalcin 1651efe9872eSLisandro Dalcin if (!I2Jacobian) { 16529566063dSJacob Faibussowitsch PetscCall(TSComputeIJacobian(ts, t, U, A, shiftA, J, P, PETSC_FALSE)); 16533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1654efe9872eSLisandro Dalcin } 1655efe9872eSLisandro Dalcin 16569566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_JacobianEval, ts, U, J, P)); 1657792fecdfSBarry Smith PetscCallBack("TS callback implicit Jacobian", I2Jacobian(ts, t, U, V, A, shiftV, shiftA, J, P, ctx)); 1658efe9872eSLisandro Dalcin if (rhsjacobian) { 1659d60b7d5cSBarry Smith Mat Jrhs, Prhs; 16609566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts, &Jrhs, &Prhs)); 16619566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts, t, U, Jrhs, Prhs)); 16629566063dSJacob Faibussowitsch PetscCall(MatAXPY(J, -1, Jrhs, ts->axpy_pattern)); 16639566063dSJacob Faibussowitsch if (P != J) PetscCall(MatAXPY(P, -1, Prhs, ts->axpy_pattern)); 1664efe9872eSLisandro Dalcin } 1665efe9872eSLisandro Dalcin 16669566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_JacobianEval, ts, U, J, P)); 16673ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1668efe9872eSLisandro Dalcin } 1669efe9872eSLisandro Dalcin 1670438f35afSJed Brown /*@C 1671438f35afSJed Brown TSSetTransientVariable - sets function to transform from state to transient variables 1672438f35afSJed Brown 1673438f35afSJed Brown Logically Collective 1674438f35afSJed Brown 16754165533cSJose E. Roman Input Parameters: 1676438f35afSJed Brown + ts - time stepping context on which to change the transient variable 1677a96d6ef6SBarry Smith . tvar - a function that transforms to transient variables 1678438f35afSJed Brown - ctx - a context for tvar 1679438f35afSJed Brown 1680a96d6ef6SBarry Smith Calling sequence of tvar: 1681a96d6ef6SBarry Smith $ PetscErrorCode tvar(TS ts,Vec p,Vec c,void *ctx); 1682a96d6ef6SBarry Smith 1683a96d6ef6SBarry Smith + ts - timestep context 16846aad120cSJose E. Roman . p - input vector (primitive form) 1685a96d6ef6SBarry Smith . c - output vector, transient variables (conservative form) 1686a96d6ef6SBarry Smith - ctx - [optional] user-defined function context 1687a96d6ef6SBarry Smith 1688438f35afSJed Brown Level: advanced 1689438f35afSJed Brown 1690438f35afSJed Brown Notes: 1691bcf0153eSBarry Smith This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., `TSBDF`) 1692438f35afSJed Brown can be conservative. In this context, primitive variables P are used to model the state (e.g., because they lead to 1693438f35afSJed Brown well-conditioned formulations even in limiting cases such as low-Mach or zero porosity). The transient variable is 1694438f35afSJed Brown C(P), specified by calling this function. An IFunction thus receives arguments (P, Cdot) and the IJacobian must be 1695438f35afSJed Brown evaluated via the chain rule, as in 1696*195e9b02SBarry Smith .vb 1697438f35afSJed Brown dF/dP + shift * dF/dCdot dC/dP. 1698*195e9b02SBarry Smith .ve 1699438f35afSJed Brown 1700bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSBDF`, `DMTSSetTransientVariable()`, `DMTSGetTransientVariable()`, `TSSetIFunction()`, `TSSetIJacobian()` 1701438f35afSJed Brown @*/ 1702d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTransientVariable(TS ts, TSTransientVariable tvar, void *ctx) 1703d71ae5a4SJacob Faibussowitsch { 1704438f35afSJed Brown DM dm; 1705438f35afSJed Brown 1706438f35afSJed Brown PetscFunctionBegin; 1707438f35afSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 17089566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 17099566063dSJacob Faibussowitsch PetscCall(DMTSSetTransientVariable(dm, tvar, ctx)); 17103ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1711438f35afSJed Brown } 1712438f35afSJed Brown 1713efe9872eSLisandro Dalcin /*@ 1714e3c11fc1SJed Brown TSComputeTransientVariable - transforms state (primitive) variables to transient (conservative) variables 1715e3c11fc1SJed Brown 1716e3c11fc1SJed Brown Logically Collective 1717e3c11fc1SJed Brown 1718e3c11fc1SJed Brown Input Parameters: 1719e3c11fc1SJed Brown + ts - TS on which to compute 1720e3c11fc1SJed Brown - U - state vector to be transformed to transient variables 1721e3c11fc1SJed Brown 1722e3c11fc1SJed Brown Output Parameters: 1723e3c11fc1SJed Brown . C - transient (conservative) variable 1724e3c11fc1SJed Brown 1725e3c11fc1SJed Brown Level: developer 1726e3c11fc1SJed Brown 1727bcf0153eSBarry Smith Developer Note: 1728*195e9b02SBarry Smith If `DMTSSetTransientVariable()` has not been called, then C is not modified in this routine and C = `NULL` is allowed. 1729bcf0153eSBarry Smith This makes it safe to call without a guard. One can use `TSHasTransientVariable()` to check if transient variables are 1730bcf0153eSBarry Smith being used. 1731bcf0153eSBarry Smith 1732bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSBDF`, `DMTSSetTransientVariable()`, `TSComputeIFunction()`, `TSComputeIJacobian()` 1733e3c11fc1SJed Brown @*/ 1734d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeTransientVariable(TS ts, Vec U, Vec C) 1735d71ae5a4SJacob Faibussowitsch { 1736e3c11fc1SJed Brown DM dm; 1737e3c11fc1SJed Brown DMTS dmts; 1738e3c11fc1SJed Brown 1739e3c11fc1SJed Brown PetscFunctionBegin; 1740e3c11fc1SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1741e3c11fc1SJed Brown PetscValidHeaderSpecific(U, VEC_CLASSID, 2); 17429566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 17439566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm, &dmts)); 1744e3c11fc1SJed Brown if (dmts->ops->transientvar) { 1745e3c11fc1SJed Brown PetscValidHeaderSpecific(C, VEC_CLASSID, 3); 17469566063dSJacob Faibussowitsch PetscCall((*dmts->ops->transientvar)(ts, U, C, dmts->transientvarctx)); 1747e3c11fc1SJed Brown } 17483ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1749e3c11fc1SJed Brown } 1750e3c11fc1SJed Brown 1751e3c11fc1SJed Brown /*@ 1752e3c11fc1SJed Brown TSHasTransientVariable - determine whether transient variables have been set 1753e3c11fc1SJed Brown 1754e3c11fc1SJed Brown Logically Collective 1755e3c11fc1SJed Brown 1756e3c11fc1SJed Brown Input Parameters: 1757*195e9b02SBarry Smith . ts - `TS` on which to compute 1758e3c11fc1SJed Brown 1759e3c11fc1SJed Brown Output Parameters: 1760bcf0153eSBarry Smith . has - `PETSC_TRUE` if transient variables have been set 1761e3c11fc1SJed Brown 1762e3c11fc1SJed Brown Level: developer 1763e3c11fc1SJed Brown 1764bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSBDF`, `DMTSSetTransientVariable()`, `TSComputeTransientVariable()` 1765e3c11fc1SJed Brown @*/ 1766d71ae5a4SJacob Faibussowitsch PetscErrorCode TSHasTransientVariable(TS ts, PetscBool *has) 1767d71ae5a4SJacob Faibussowitsch { 1768e3c11fc1SJed Brown DM dm; 1769e3c11fc1SJed Brown DMTS dmts; 1770e3c11fc1SJed Brown 1771e3c11fc1SJed Brown PetscFunctionBegin; 1772e3c11fc1SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 17739566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 17749566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm, &dmts)); 1775e3c11fc1SJed Brown *has = dmts->ops->transientvar ? PETSC_TRUE : PETSC_FALSE; 17763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1777e3c11fc1SJed Brown } 1778e3c11fc1SJed Brown 1779e3c11fc1SJed Brown /*@ 1780efe9872eSLisandro Dalcin TS2SetSolution - Sets the initial solution and time derivative vectors 1781bcf0153eSBarry Smith for use by the `TS` routines handling second order equations. 1782efe9872eSLisandro Dalcin 1783c3339decSBarry Smith Logically Collective 1784efe9872eSLisandro Dalcin 1785efe9872eSLisandro Dalcin Input Parameters: 1786bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1787efe9872eSLisandro Dalcin . u - the solution vector 1788efe9872eSLisandro Dalcin - v - the time derivative vector 1789efe9872eSLisandro Dalcin 1790efe9872eSLisandro Dalcin Level: beginner 1791efe9872eSLisandro Dalcin 1792bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS` 1793efe9872eSLisandro Dalcin @*/ 1794d71ae5a4SJacob Faibussowitsch PetscErrorCode TS2SetSolution(TS ts, Vec u, Vec v) 1795d71ae5a4SJacob Faibussowitsch { 1796efe9872eSLisandro Dalcin PetscFunctionBegin; 1797efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1798efe9872eSLisandro Dalcin PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 1799efe9872eSLisandro Dalcin PetscValidHeaderSpecific(v, VEC_CLASSID, 3); 18009566063dSJacob Faibussowitsch PetscCall(TSSetSolution(ts, u)); 18019566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)v)); 18029566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_dot)); 1803efe9872eSLisandro Dalcin ts->vec_dot = v; 18043ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1805efe9872eSLisandro Dalcin } 1806efe9872eSLisandro Dalcin 1807efe9872eSLisandro Dalcin /*@ 1808efe9872eSLisandro Dalcin TS2GetSolution - Returns the solution and time derivative at the present timestep 1809efe9872eSLisandro Dalcin for second order equations. It is valid to call this routine inside the function 1810efe9872eSLisandro Dalcin that you are evaluating in order to move to the new timestep. This vector not 1811efe9872eSLisandro Dalcin changed until the solution at the next timestep has been calculated. 1812efe9872eSLisandro Dalcin 1813*195e9b02SBarry Smith Not Collective, but `u` returned is parallel if `TS` is parallel 1814efe9872eSLisandro Dalcin 1815efe9872eSLisandro Dalcin Input Parameter: 1816bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 1817efe9872eSLisandro Dalcin 1818d8d19677SJose E. Roman Output Parameters: 1819efe9872eSLisandro Dalcin + u - the vector containing the solution 1820efe9872eSLisandro Dalcin - v - the vector containing the time derivative 1821efe9872eSLisandro Dalcin 1822efe9872eSLisandro Dalcin Level: intermediate 1823efe9872eSLisandro Dalcin 1824bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TS2SetSolution()`, `TSGetTimeStep()`, `TSGetTime()` 1825efe9872eSLisandro Dalcin @*/ 1826d71ae5a4SJacob Faibussowitsch PetscErrorCode TS2GetSolution(TS ts, Vec *u, Vec *v) 1827d71ae5a4SJacob Faibussowitsch { 1828efe9872eSLisandro Dalcin PetscFunctionBegin; 1829efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1830efe9872eSLisandro Dalcin if (u) PetscValidPointer(u, 2); 1831efe9872eSLisandro Dalcin if (v) PetscValidPointer(v, 3); 1832efe9872eSLisandro Dalcin if (u) *u = ts->vec_sol; 1833efe9872eSLisandro Dalcin if (v) *v = ts->vec_dot; 18343ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1835efe9872eSLisandro Dalcin } 1836efe9872eSLisandro Dalcin 183755849f57SBarry Smith /*@C 1838bcf0153eSBarry Smith TSLoad - Loads a `TS` that has been stored in binary with `TSView()`. 183955849f57SBarry Smith 1840c3339decSBarry Smith Collective 184155849f57SBarry Smith 184255849f57SBarry Smith Input Parameters: 1843bcf0153eSBarry Smith + newdm - the newly loaded `TS`, this needs to have been created with `TSCreate()` or 1844bcf0153eSBarry Smith some related function before a call to `TSLoad()`. 1845bcf0153eSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` 184655849f57SBarry Smith 184755849f57SBarry Smith Level: intermediate 184855849f57SBarry Smith 1849bcf0153eSBarry Smith Note: 1850bcf0153eSBarry Smith The type is determined by the data in the file, any type set into the `TS` before this call is ignored. 185155849f57SBarry Smith 1852bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `PetscViewer`, `PetscViewerBinaryOpen()`, `TSView()`, `MatLoad()`, `VecLoad()` 185355849f57SBarry Smith @*/ 1854d71ae5a4SJacob Faibussowitsch PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 1855d71ae5a4SJacob Faibussowitsch { 185655849f57SBarry Smith PetscBool isbinary; 185755849f57SBarry Smith PetscInt classid; 185855849f57SBarry Smith char type[256]; 18592d53ad75SBarry Smith DMTS sdm; 1860ad6bc421SBarry Smith DM dm; 186155849f57SBarry Smith 186255849f57SBarry Smith PetscFunctionBegin; 1863f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 186455849f57SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 18659566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary)); 18663c633725SBarry Smith PetscCheck(isbinary, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 186755849f57SBarry Smith 18689566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT)); 18693c633725SBarry Smith PetscCheck(classid == TS_FILE_CLASSID, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Not TS next in file"); 18709566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR)); 18719566063dSJacob Faibussowitsch PetscCall(TSSetType(ts, type)); 1872dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, load, viewer); 18739566063dSJacob Faibussowitsch PetscCall(DMCreate(PetscObjectComm((PetscObject)ts), &dm)); 18749566063dSJacob Faibussowitsch PetscCall(DMLoad(dm, viewer)); 18759566063dSJacob Faibussowitsch PetscCall(TSSetDM(ts, dm)); 18769566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(ts->dm, &ts->vec_sol)); 18779566063dSJacob Faibussowitsch PetscCall(VecLoad(ts->vec_sol, viewer)); 18789566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm, &sdm)); 18799566063dSJacob Faibussowitsch PetscCall(DMTSLoad(sdm, viewer)); 18803ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 188155849f57SBarry Smith } 188255849f57SBarry Smith 18839804daf3SBarry Smith #include <petscdraw.h> 1884e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1885e04113cfSBarry Smith #include <petscviewersaws.h> 1886f05ece33SBarry Smith #endif 1887fe2efc57SMark 1888fe2efc57SMark /*@C 1889bcf0153eSBarry Smith TSViewFromOptions - View a `TS` based on values in the options database 1890fe2efc57SMark 1891c3339decSBarry Smith Collective 1892fe2efc57SMark 1893fe2efc57SMark Input Parameters: 1894bcf0153eSBarry Smith + ts - the `TS` context 1895bcf0153eSBarry Smith . obj - Optional object that provides the prefix for the options database keys 1896bcf0153eSBarry Smith - name - command line option string to be passed by user 1897fe2efc57SMark 1898fe2efc57SMark Level: intermediate 1899bcf0153eSBarry Smith 1900bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSView`, `PetscObjectViewFromOptions()`, `TSCreate()` 1901fe2efc57SMark @*/ 1902bcf0153eSBarry Smith PetscErrorCode TSViewFromOptions(TS ts, PetscObject obj, const char name[]) 1903d71ae5a4SJacob Faibussowitsch { 1904fe2efc57SMark PetscFunctionBegin; 1905bcf0153eSBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1906bcf0153eSBarry Smith PetscCall(PetscObjectViewFromOptions((PetscObject)ts, obj, name)); 19073ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1908fe2efc57SMark } 1909fe2efc57SMark 19107e2c5f70SBarry Smith /*@C 1911bcf0153eSBarry Smith TSView - Prints the `TS` data structure. 1912d763cef2SBarry Smith 1913c3339decSBarry Smith Collective 1914d763cef2SBarry Smith 1915d763cef2SBarry Smith Input Parameters: 1916bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1917d763cef2SBarry Smith - viewer - visualization context 1918d763cef2SBarry Smith 1919d763cef2SBarry Smith Options Database Key: 1920bcf0153eSBarry Smith . -ts_view - calls `TSView()` at end of `TSStep()` 1921bcf0153eSBarry Smith 1922bcf0153eSBarry Smith Level: beginner 1923d763cef2SBarry Smith 1924d763cef2SBarry Smith Notes: 1925d763cef2SBarry Smith The available visualization contexts include 1926bcf0153eSBarry Smith + `PETSC_VIEWER_STDOUT_SELF` - standard output (default) 1927bcf0153eSBarry Smith - `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard 1928d763cef2SBarry Smith output where only the first processor opens 1929d763cef2SBarry Smith the file. All other processors send their 1930d763cef2SBarry Smith data to the first processor to print. 1931d763cef2SBarry Smith 1932d763cef2SBarry Smith The user can open an alternative visualization context with 1933bcf0153eSBarry Smith `PetscViewerASCIIOpen()` - output to a specified file. 1934d763cef2SBarry Smith 1935bcf0153eSBarry Smith In the debugger you can do call `TSView`(ts,0) to display the `TS` solver. (The same holds for any PETSc object viewer). 1936595c91d4SBarry Smith 1937bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `PetscViewer`, `PetscViewerASCIIOpen()` 1938d763cef2SBarry Smith @*/ 1939d71ae5a4SJacob Faibussowitsch PetscErrorCode TSView(TS ts, PetscViewer viewer) 1940d71ae5a4SJacob Faibussowitsch { 194119fd82e9SBarry Smith TSType type; 19422b0a91c0SBarry Smith PetscBool iascii, isstring, isundials, isbinary, isdraw; 19432d53ad75SBarry Smith DMTS sdm; 1944e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1945536b137fSBarry Smith PetscBool issaws; 1946f05ece33SBarry Smith #endif 1947d763cef2SBarry Smith 1948d763cef2SBarry Smith PetscFunctionBegin; 19490700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 19501e66621cSBarry Smith if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts), &viewer)); 19510700a824SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 1952c9780b6fSBarry Smith PetscCheckSameComm(ts, 1, viewer, 2); 1953fd16b177SBarry Smith 19549566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 19559566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring)); 19569566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary)); 19579566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw)); 1958e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 19599566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSAWS, &issaws)); 1960f05ece33SBarry Smith #endif 196132077d6dSBarry Smith if (iascii) { 19629566063dSJacob Faibussowitsch PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)ts, viewer)); 1963efd4aadfSBarry Smith if (ts->ops->view) { 19649566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 1965dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, view, viewer); 19669566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 1967efd4aadfSBarry Smith } 19681e66621cSBarry Smith if (ts->max_steps < PETSC_MAX_INT) PetscCall(PetscViewerASCIIPrintf(viewer, " maximum steps=%" PetscInt_FMT "\n", ts->max_steps)); 19691e66621cSBarry Smith if (ts->max_time < PETSC_MAX_REAL) PetscCall(PetscViewerASCIIPrintf(viewer, " maximum time=%g\n", (double)ts->max_time)); 19701e66621cSBarry Smith if (ts->ifuncs) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of I function evaluations=%" PetscInt_FMT "\n", ts->ifuncs)); 19711e66621cSBarry Smith if (ts->ijacs) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of I Jacobian evaluations=%" PetscInt_FMT "\n", ts->ijacs)); 19721e66621cSBarry Smith if (ts->rhsfuncs) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of RHS function evaluations=%" PetscInt_FMT "\n", ts->rhsfuncs)); 19731e66621cSBarry Smith if (ts->rhsjacs) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of RHS Jacobian evaluations=%" PetscInt_FMT "\n", ts->rhsjacs)); 1974efd4aadfSBarry Smith if (ts->usessnes) { 1975efd4aadfSBarry Smith PetscBool lin; 19761e66621cSBarry Smith if (ts->problem_type == TS_NONLINEAR) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of nonlinear solver iterations=%" PetscInt_FMT "\n", ts->snes_its)); 197763a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " total number of linear solver iterations=%" PetscInt_FMT "\n", ts->ksp_its)); 19789566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompareAny((PetscObject)ts->snes, &lin, SNESKSPONLY, SNESKSPTRANSPOSEONLY, "")); 197963a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " total number of %slinear solve failures=%" PetscInt_FMT "\n", lin ? "" : "non", ts->num_snes_failures)); 1980efd4aadfSBarry Smith } 198163a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " total number of rejected steps=%" PetscInt_FMT "\n", ts->reject)); 19821e66621cSBarry Smith if (ts->vrtol) PetscCall(PetscViewerASCIIPrintf(viewer, " using vector of relative error tolerances, ")); 19831e66621cSBarry Smith else PetscCall(PetscViewerASCIIPrintf(viewer, " using relative error tolerance of %g, ", (double)ts->rtol)); 19841e66621cSBarry Smith if (ts->vatol) PetscCall(PetscViewerASCIIPrintf(viewer, " using vector of absolute error tolerances\n")); 19851e66621cSBarry Smith else PetscCall(PetscViewerASCIIPrintf(viewer, " using absolute error tolerance of %g\n", (double)ts->atol)); 19869566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 19879566063dSJacob Faibussowitsch PetscCall(TSAdaptView(ts->adapt, viewer)); 19889566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 19890f5bd95cSBarry Smith } else if (isstring) { 19909566063dSJacob Faibussowitsch PetscCall(TSGetType(ts, &type)); 19919566063dSJacob Faibussowitsch PetscCall(PetscViewerStringSPrintf(viewer, " TSType: %-7.7s", type)); 1992dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, view, viewer); 199355849f57SBarry Smith } else if (isbinary) { 199455849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 199555849f57SBarry Smith MPI_Comm comm; 199655849f57SBarry Smith PetscMPIInt rank; 199755849f57SBarry Smith char type[256]; 199855849f57SBarry Smith 19999566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)ts, &comm)); 20009566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 2001dd400576SPatrick Sanan if (rank == 0) { 20029566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer, &classid, 1, PETSC_INT)); 20039566063dSJacob Faibussowitsch PetscCall(PetscStrncpy(type, ((PetscObject)ts)->type_name, 256)); 20049566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer, type, 256, PETSC_CHAR)); 200555849f57SBarry Smith } 2006dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, view, viewer); 20079566063dSJacob Faibussowitsch if (ts->adapt) PetscCall(TSAdaptView(ts->adapt, viewer)); 20089566063dSJacob Faibussowitsch PetscCall(DMView(ts->dm, viewer)); 20099566063dSJacob Faibussowitsch PetscCall(VecView(ts->vec_sol, viewer)); 20109566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm, &sdm)); 20119566063dSJacob Faibussowitsch PetscCall(DMTSView(sdm, viewer)); 20122b0a91c0SBarry Smith } else if (isdraw) { 20132b0a91c0SBarry Smith PetscDraw draw; 20142b0a91c0SBarry Smith char str[36]; 201589fd9fafSBarry Smith PetscReal x, y, bottom, h; 20162b0a91c0SBarry Smith 20179566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw)); 20189566063dSJacob Faibussowitsch PetscCall(PetscDrawGetCurrentPoint(draw, &x, &y)); 20199566063dSJacob Faibussowitsch PetscCall(PetscStrcpy(str, "TS: ")); 20209566063dSJacob Faibussowitsch PetscCall(PetscStrcat(str, ((PetscObject)ts)->type_name)); 20219566063dSJacob Faibussowitsch PetscCall(PetscDrawStringBoxed(draw, x, y, PETSC_DRAW_BLACK, PETSC_DRAW_BLACK, str, NULL, &h)); 202289fd9fafSBarry Smith bottom = y - h; 20239566063dSJacob Faibussowitsch PetscCall(PetscDrawPushCurrentPoint(draw, x, bottom)); 2024dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, view, viewer); 20259566063dSJacob Faibussowitsch if (ts->adapt) PetscCall(TSAdaptView(ts->adapt, viewer)); 20269566063dSJacob Faibussowitsch if (ts->snes) PetscCall(SNESView(ts->snes, viewer)); 20279566063dSJacob Faibussowitsch PetscCall(PetscDrawPopCurrentPoint(draw)); 2028e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 2029536b137fSBarry Smith } else if (issaws) { 2030d45a07a7SBarry Smith PetscMPIInt rank; 20312657e9d9SBarry Smith const char *name; 20322657e9d9SBarry Smith 20339566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)ts, &name)); 20349566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank)); 2035dd400576SPatrick Sanan if (!((PetscObject)ts)->amsmem && rank == 0) { 2036d45a07a7SBarry Smith char dir[1024]; 2037d45a07a7SBarry Smith 20389566063dSJacob Faibussowitsch PetscCall(PetscObjectViewSAWs((PetscObject)ts, viewer)); 20399566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(dir, 1024, "/PETSc/Objects/%s/time_step", name)); 2040792fecdfSBarry Smith PetscCallSAWs(SAWs_Register, (dir, &ts->steps, 1, SAWs_READ, SAWs_INT)); 20419566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(dir, 1024, "/PETSc/Objects/%s/time", name)); 2042792fecdfSBarry Smith PetscCallSAWs(SAWs_Register, (dir, &ts->ptime, 1, SAWs_READ, SAWs_DOUBLE)); 2043d763cef2SBarry Smith } 2044dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, view, viewer); 2045f05ece33SBarry Smith #endif 2046f05ece33SBarry Smith } 204736a9e3b9SBarry Smith if (ts->snes && ts->usessnes) { 20489566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 20499566063dSJacob Faibussowitsch PetscCall(SNESView(ts->snes, viewer)); 20509566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 205136a9e3b9SBarry Smith } 20529566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm, &sdm)); 20539566063dSJacob Faibussowitsch PetscCall(DMTSView(sdm, viewer)); 2054f05ece33SBarry Smith 20559566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 20569566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)ts, TSSUNDIALS, &isundials)); 20579566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 20583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2059d763cef2SBarry Smith } 2060d763cef2SBarry Smith 2061b07ff414SBarry Smith /*@ 2062d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 2063d763cef2SBarry Smith the timesteppers. 2064d763cef2SBarry Smith 2065c3339decSBarry Smith Logically Collective 2066d763cef2SBarry Smith 2067d763cef2SBarry Smith Input Parameters: 2068bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 2069bcf0153eSBarry Smith - usrP - user context 2070daf670e6SBarry Smith 2071d763cef2SBarry Smith Level: intermediate 2072d763cef2SBarry Smith 2073bcf0153eSBarry Smith Fortran Note: 2074*195e9b02SBarry Smith You must write a Fortran interface definition for this 2075*195e9b02SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the `ctx` argument. 2076bcf0153eSBarry Smith 2077bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetApplicationContext()` 2078d763cef2SBarry Smith @*/ 2079d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetApplicationContext(TS ts, void *usrP) 2080d71ae5a4SJacob Faibussowitsch { 2081d763cef2SBarry Smith PetscFunctionBegin; 20820700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2083d763cef2SBarry Smith ts->user = usrP; 20843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2085d763cef2SBarry Smith } 2086d763cef2SBarry Smith 2087b07ff414SBarry Smith /*@ 2088d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 2089bcf0153eSBarry Smith timestepper that was set with `TSSetApplicationContext()` 2090d763cef2SBarry Smith 2091d763cef2SBarry Smith Not Collective 2092d763cef2SBarry Smith 2093d763cef2SBarry Smith Input Parameter: 2094bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2095d763cef2SBarry Smith 2096d763cef2SBarry Smith Output Parameter: 2097d763cef2SBarry Smith . usrP - user context 2098d763cef2SBarry Smith 2099bcf0153eSBarry Smith Level: intermediate 2100bcf0153eSBarry Smith 2101bcf0153eSBarry Smith Fortran Note: 2102*195e9b02SBarry Smith You must write a Fortran interface definition for this 2103*195e9b02SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the `ctx` argument. 2104daf670e6SBarry Smith 2105bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetApplicationContext()` 2106d763cef2SBarry Smith @*/ 2107d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetApplicationContext(TS ts, void *usrP) 2108d71ae5a4SJacob Faibussowitsch { 2109d763cef2SBarry Smith PetscFunctionBegin; 21100700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2111e71120c6SJed Brown *(void **)usrP = ts->user; 21123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2113d763cef2SBarry Smith } 2114d763cef2SBarry Smith 2115d763cef2SBarry Smith /*@ 2116bcf0153eSBarry Smith TSGetStepNumber - Gets the number of time steps completed. 2117d763cef2SBarry Smith 2118d763cef2SBarry Smith Not Collective 2119d763cef2SBarry Smith 2120d763cef2SBarry Smith Input Parameter: 2121bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2122d763cef2SBarry Smith 2123d763cef2SBarry Smith Output Parameter: 212480275a0aSLisandro Dalcin . steps - number of steps completed so far 2125d763cef2SBarry Smith 2126d763cef2SBarry Smith Level: intermediate 2127d763cef2SBarry Smith 2128bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetTime()`, `TSGetTimeStep()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostStage()`, `TSSetPostStep()` 2129d763cef2SBarry Smith @*/ 2130d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetStepNumber(TS ts, PetscInt *steps) 2131d71ae5a4SJacob Faibussowitsch { 2132d763cef2SBarry Smith PetscFunctionBegin; 21330700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 213480275a0aSLisandro Dalcin PetscValidIntPointer(steps, 2); 213580275a0aSLisandro Dalcin *steps = ts->steps; 21363ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 213780275a0aSLisandro Dalcin } 213880275a0aSLisandro Dalcin 213980275a0aSLisandro Dalcin /*@ 214080275a0aSLisandro Dalcin TSSetStepNumber - Sets the number of steps completed. 214180275a0aSLisandro Dalcin 2142c3339decSBarry Smith Logically Collective 214380275a0aSLisandro Dalcin 214480275a0aSLisandro Dalcin Input Parameters: 2145bcf0153eSBarry Smith + ts - the `TS` context 214680275a0aSLisandro Dalcin - steps - number of steps completed so far 214780275a0aSLisandro Dalcin 2148bcf0153eSBarry Smith Level: developer 2149bcf0153eSBarry Smith 2150bcf0153eSBarry Smith Note: 2151bcf0153eSBarry Smith For most uses of the `TS` solvers the user need not explicitly call 2152bcf0153eSBarry Smith `TSSetStepNumber()`, as the step counter is appropriately updated in 2153bcf0153eSBarry Smith `TSSolve()`/`TSStep()`/`TSRollBack()`. Power users may call this routine to 215480275a0aSLisandro Dalcin reinitialize timestepping by setting the step counter to zero (and time 215580275a0aSLisandro Dalcin to the initial time) to solve a similar problem with different initial 215680275a0aSLisandro Dalcin conditions or parameters. Other possible use case is to continue 2157*195e9b02SBarry Smith timestepping from a previously interrupted run in such a way that `TS` 215880275a0aSLisandro Dalcin monitors will be called with a initial nonzero step counter. 215980275a0aSLisandro Dalcin 2160bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetStepNumber()`, `TSSetTime()`, `TSSetTimeStep()`, `TSSetSolution()` 216180275a0aSLisandro Dalcin @*/ 2162d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetStepNumber(TS ts, PetscInt steps) 2163d71ae5a4SJacob Faibussowitsch { 216480275a0aSLisandro Dalcin PetscFunctionBegin; 216580275a0aSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 216680275a0aSLisandro Dalcin PetscValidLogicalCollectiveInt(ts, steps, 2); 21673c633725SBarry Smith PetscCheck(steps >= 0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Step number must be non-negative"); 216880275a0aSLisandro Dalcin ts->steps = steps; 21693ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2170d763cef2SBarry Smith } 2171d763cef2SBarry Smith 2172d763cef2SBarry Smith /*@ 2173d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 2174d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 2175d763cef2SBarry Smith 2176c3339decSBarry Smith Logically Collective 2177d763cef2SBarry Smith 2178d763cef2SBarry Smith Input Parameters: 2179bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 2180d763cef2SBarry Smith - time_step - the size of the timestep 2181d763cef2SBarry Smith 2182d763cef2SBarry Smith Level: intermediate 2183d763cef2SBarry Smith 2184bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSPSEUDO`, `TSGetTimeStep()`, `TSSetTime()` 2185d763cef2SBarry Smith @*/ 2186d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTimeStep(TS ts, PetscReal time_step) 2187d71ae5a4SJacob Faibussowitsch { 2188d763cef2SBarry Smith PetscFunctionBegin; 21890700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2190c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts, time_step, 2); 2191d763cef2SBarry Smith ts->time_step = time_step; 21923ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2193d763cef2SBarry Smith } 2194d763cef2SBarry Smith 2195a43b19c4SJed Brown /*@ 219649354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 219749354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 2198bcf0153eSBarry Smith or just return at the final time `TS` computed. 2199a43b19c4SJed Brown 2200c3339decSBarry Smith Logically Collective 2201a43b19c4SJed Brown 2202d8d19677SJose E. Roman Input Parameters: 2203a43b19c4SJed Brown + ts - the time-step context 220449354f04SShri Abhyankar - eftopt - exact final time option 2205bcf0153eSBarry Smith .vb 2206bcf0153eSBarry Smith TS_EXACTFINALTIME_STEPOVER - Don't do anything if final time is exceeded 2207bcf0153eSBarry Smith TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time 2208bcf0153eSBarry Smith TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time 2209bcf0153eSBarry Smith .ve 2210a43b19c4SJed Brown 2211bcf0153eSBarry Smith Options Database Key: 2212feed9e9dSBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime 2213feed9e9dSBarry Smith 2214a43b19c4SJed Brown Level: beginner 2215a43b19c4SJed Brown 2216bcf0153eSBarry Smith Note: 2217bcf0153eSBarry Smith If you use the option `TS_EXACTFINALTIME_STEPOVER` the solution may be at a very different time 2218bcf0153eSBarry Smith then the final time you selected. 2219bcf0153eSBarry Smith 2220bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSExactFinalTimeOption`, `TSGetExactFinalTime()` 2221a43b19c4SJed Brown @*/ 2222d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetExactFinalTime(TS ts, TSExactFinalTimeOption eftopt) 2223d71ae5a4SJacob Faibussowitsch { 2224a43b19c4SJed Brown PetscFunctionBegin; 2225a43b19c4SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 222649354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts, eftopt, 2); 222749354f04SShri Abhyankar ts->exact_final_time = eftopt; 22283ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2229a43b19c4SJed Brown } 2230a43b19c4SJed Brown 2231d763cef2SBarry Smith /*@ 2232bcf0153eSBarry Smith TSGetExactFinalTime - Gets the exact final time option set with `TSSetExactFinalTime()` 2233f6953c82SLisandro Dalcin 2234f6953c82SLisandro Dalcin Not Collective 2235f6953c82SLisandro Dalcin 2236f6953c82SLisandro Dalcin Input Parameter: 2237bcf0153eSBarry Smith . ts - the `TS` context 2238f6953c82SLisandro Dalcin 2239f6953c82SLisandro Dalcin Output Parameter: 2240f6953c82SLisandro Dalcin . eftopt - exact final time option 2241f6953c82SLisandro Dalcin 2242f6953c82SLisandro Dalcin Level: beginner 2243f6953c82SLisandro Dalcin 2244bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSExactFinalTimeOption`, `TSSetExactFinalTime()` 2245f6953c82SLisandro Dalcin @*/ 2246d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetExactFinalTime(TS ts, TSExactFinalTimeOption *eftopt) 2247d71ae5a4SJacob Faibussowitsch { 2248f6953c82SLisandro Dalcin PetscFunctionBegin; 2249f6953c82SLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2250f6953c82SLisandro Dalcin PetscValidPointer(eftopt, 2); 2251f6953c82SLisandro Dalcin *eftopt = ts->exact_final_time; 22523ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2253f6953c82SLisandro Dalcin } 2254f6953c82SLisandro Dalcin 2255f6953c82SLisandro Dalcin /*@ 2256d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 2257d763cef2SBarry Smith 2258d763cef2SBarry Smith Not Collective 2259d763cef2SBarry Smith 2260d763cef2SBarry Smith Input Parameter: 2261bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2262d763cef2SBarry Smith 2263d763cef2SBarry Smith Output Parameter: 2264d763cef2SBarry Smith . dt - the current timestep size 2265d763cef2SBarry Smith 2266d763cef2SBarry Smith Level: intermediate 2267d763cef2SBarry Smith 2268bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetTimeStep()`, `TSGetTime()` 2269d763cef2SBarry Smith @*/ 2270d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeStep(TS ts, PetscReal *dt) 2271d71ae5a4SJacob Faibussowitsch { 2272d763cef2SBarry Smith PetscFunctionBegin; 22730700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2274f7cf8827SBarry Smith PetscValidRealPointer(dt, 2); 2275d763cef2SBarry Smith *dt = ts->time_step; 22763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2277d763cef2SBarry Smith } 2278d763cef2SBarry Smith 2279d8e5e3e6SSatish Balay /*@ 2280d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 2281d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 2282d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 2283d763cef2SBarry Smith the solution at the next timestep has been calculated. 2284d763cef2SBarry Smith 2285bcf0153eSBarry Smith Not Collective, but v returned is parallel if ts is parallel 2286d763cef2SBarry Smith 2287d763cef2SBarry Smith Input Parameter: 2288bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2289d763cef2SBarry Smith 2290d763cef2SBarry Smith Output Parameter: 2291d763cef2SBarry Smith . v - the vector containing the solution 2292d763cef2SBarry Smith 2293d763cef2SBarry Smith Level: intermediate 2294d763cef2SBarry Smith 2295bcf0153eSBarry Smith Note: 2296bcf0153eSBarry Smith If you used `TSSetExactFinalTime`(ts,`TS_EXACTFINALTIME_MATCHSTEP`); this does not return the solution at the requested 2297bcf0153eSBarry Smith final time. It returns the solution at the next timestep. 2298d763cef2SBarry Smith 2299bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetTimeStep()`, `TSGetTime()`, `TSGetSolveTime()`, `TSGetSolutionComponents()`, `TSSetSolutionFunction()` 2300d763cef2SBarry Smith @*/ 2301d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSolution(TS ts, Vec *v) 2302d71ae5a4SJacob Faibussowitsch { 2303d763cef2SBarry Smith PetscFunctionBegin; 23040700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 23054482741eSBarry Smith PetscValidPointer(v, 2); 23068737fe31SLisandro Dalcin *v = ts->vec_sol; 23073ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2308d763cef2SBarry Smith } 2309d763cef2SBarry Smith 231003fe5f5eSDebojyoti Ghosh /*@ 2311b2bf4f3aSDebojyoti Ghosh TSGetSolutionComponents - Returns any solution components at the present 231203fe5f5eSDebojyoti Ghosh timestep, if available for the time integration method being used. 2313b2bf4f3aSDebojyoti Ghosh Solution components are quantities that share the same size and 231403fe5f5eSDebojyoti Ghosh structure as the solution vector. 231503fe5f5eSDebojyoti Ghosh 2316bcf0153eSBarry Smith Not Collective, but v returned is parallel if ts is parallel 231703fe5f5eSDebojyoti Ghosh 231803fe5f5eSDebojyoti Ghosh Parameters : 2319bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` (input parameter). 2320*195e9b02SBarry Smith . n - If v is `NULL`, then the number of solution components is 2321b2bf4f3aSDebojyoti Ghosh returned through n, else the n-th solution component is 232203fe5f5eSDebojyoti Ghosh returned in v. 2323a2b725a8SWilliam Gropp - v - the vector containing the n-th solution component 2324*195e9b02SBarry Smith (may be `NULL` to use this function to find out 2325b2bf4f3aSDebojyoti Ghosh the number of solutions components). 232603fe5f5eSDebojyoti Ghosh 23274cdd57e5SDebojyoti Ghosh Level: advanced 232803fe5f5eSDebojyoti Ghosh 2329bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetSolution()` 233003fe5f5eSDebojyoti Ghosh @*/ 2331d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSolutionComponents(TS ts, PetscInt *n, Vec *v) 2332d71ae5a4SJacob Faibussowitsch { 233303fe5f5eSDebojyoti Ghosh PetscFunctionBegin; 233403fe5f5eSDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2335b2bf4f3aSDebojyoti Ghosh if (!ts->ops->getsolutioncomponents) *n = 0; 2336dbbe0bcdSBarry Smith else PetscUseTypeMethod(ts, getsolutioncomponents, n, v); 23373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 233803fe5f5eSDebojyoti Ghosh } 233903fe5f5eSDebojyoti Ghosh 23404cdd57e5SDebojyoti Ghosh /*@ 23414cdd57e5SDebojyoti Ghosh TSGetAuxSolution - Returns an auxiliary solution at the present 23424cdd57e5SDebojyoti Ghosh timestep, if available for the time integration method being used. 23434cdd57e5SDebojyoti Ghosh 2344bcf0153eSBarry Smith Not Collective, but v returned is parallel if ts is parallel 23454cdd57e5SDebojyoti Ghosh 23464cdd57e5SDebojyoti Ghosh Parameters : 2347bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` (input parameter). 2348a2b725a8SWilliam Gropp - v - the vector containing the auxiliary solution 23494cdd57e5SDebojyoti Ghosh 23504cdd57e5SDebojyoti Ghosh Level: intermediate 23514cdd57e5SDebojyoti Ghosh 2352bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetSolution()` 23534cdd57e5SDebojyoti Ghosh @*/ 2354d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetAuxSolution(TS ts, Vec *v) 2355d71ae5a4SJacob Faibussowitsch { 23564cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 23574cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2358dbbe0bcdSBarry Smith if (ts->ops->getauxsolution) PetscUseTypeMethod(ts, getauxsolution, v); 23591e66621cSBarry Smith else PetscCall(VecZeroEntries(*v)); 23603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23614cdd57e5SDebojyoti Ghosh } 23624cdd57e5SDebojyoti Ghosh 23634cdd57e5SDebojyoti Ghosh /*@ 23644cdd57e5SDebojyoti Ghosh TSGetTimeError - Returns the estimated error vector, if the chosen 2365bcf0153eSBarry Smith `TSType` has an error estimation functionality and `TSSetTimeError()` was called 23664cdd57e5SDebojyoti Ghosh 2367bcf0153eSBarry Smith Not Collective, but v returned is parallel if ts is parallel 23689657682dSDebojyoti Ghosh 23694cdd57e5SDebojyoti Ghosh Parameters : 2370bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` (input parameter). 2371657c1e31SEmil Constantinescu . n - current estimate (n=0) or previous one (n=-1) 2372a2b725a8SWilliam Gropp - v - the vector containing the error (same size as the solution). 23734cdd57e5SDebojyoti Ghosh 23744cdd57e5SDebojyoti Ghosh Level: intermediate 23754cdd57e5SDebojyoti Ghosh 2376bcf0153eSBarry Smith Note: 2377bcf0153eSBarry Smith MUST call after `TSSetUp()` 23784cdd57e5SDebojyoti Ghosh 2379bcf0153eSBarry Smith .seealso: [](chapter_ts), `TSGetSolution()`, `TSSetTimeError()` 23804cdd57e5SDebojyoti Ghosh @*/ 2381d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeError(TS ts, PetscInt n, Vec *v) 2382d71ae5a4SJacob Faibussowitsch { 23834cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 23844cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2385dbbe0bcdSBarry Smith if (ts->ops->gettimeerror) PetscUseTypeMethod(ts, gettimeerror, n, v); 23861e66621cSBarry Smith else PetscCall(VecZeroEntries(*v)); 23873ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23884cdd57e5SDebojyoti Ghosh } 23894cdd57e5SDebojyoti Ghosh 239057df6a1bSDebojyoti Ghosh /*@ 239157df6a1bSDebojyoti Ghosh TSSetTimeError - Sets the estimated error vector, if the chosen 2392bcf0153eSBarry Smith `TSType` has an error estimation functionality. This can be used 239357df6a1bSDebojyoti Ghosh to restart such a time integrator with a given error vector. 239457df6a1bSDebojyoti Ghosh 2395bcf0153eSBarry Smith Not Collective, but v returned is parallel if ts is parallel 239657df6a1bSDebojyoti Ghosh 239757df6a1bSDebojyoti Ghosh Parameters : 2398bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` (input parameter). 2399a2b725a8SWilliam Gropp - v - the vector containing the error (same size as the solution). 240057df6a1bSDebojyoti Ghosh 240157df6a1bSDebojyoti Ghosh Level: intermediate 240257df6a1bSDebojyoti Ghosh 2403bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetSolution()`, `TSGetTimeError)` 240457df6a1bSDebojyoti Ghosh @*/ 2405d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTimeError(TS ts, Vec v) 2406d71ae5a4SJacob Faibussowitsch { 240757df6a1bSDebojyoti Ghosh PetscFunctionBegin; 240857df6a1bSDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 24093c633725SBarry Smith PetscCheck(ts->setupcalled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call TSSetUp() first"); 2410dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, settimeerror, v); 24113ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 241257df6a1bSDebojyoti Ghosh } 241357df6a1bSDebojyoti Ghosh 2414bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 2415d8e5e3e6SSatish Balay /*@ 2416bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 2417d763cef2SBarry Smith 2418bdad233fSMatthew Knepley Not collective 2419d763cef2SBarry Smith 2420bdad233fSMatthew Knepley Input Parameters: 2421bcf0153eSBarry Smith + ts - The `TS` 2422bcf0153eSBarry Smith - type - One of `TS_LINEAR`, `TS_NONLINEAR` where these types refer to problems of the forms 2423d763cef2SBarry Smith .vb 24240910c330SBarry Smith U_t - A U = 0 (linear) 24250910c330SBarry Smith U_t - A(t) U = 0 (linear) 24260910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 2427d763cef2SBarry Smith .ve 2428d763cef2SBarry Smith 2429d763cef2SBarry Smith Level: beginner 2430d763cef2SBarry Smith 2431bcf0153eSBarry Smith .seealso: [](chapter_ts), `TSSetUp()`, `TSProblemType`, `TS` 2432d763cef2SBarry Smith @*/ 2433d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 2434d71ae5a4SJacob Faibussowitsch { 2435d763cef2SBarry Smith PetscFunctionBegin; 24360700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2437bdad233fSMatthew Knepley ts->problem_type = type; 24389e2a6581SJed Brown if (type == TS_LINEAR) { 24399e2a6581SJed Brown SNES snes; 24409566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 24419566063dSJacob Faibussowitsch PetscCall(SNESSetType(snes, SNESKSPONLY)); 24429e2a6581SJed Brown } 24433ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2444d763cef2SBarry Smith } 2445d763cef2SBarry Smith 2446bdad233fSMatthew Knepley /*@C 2447bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 2448bdad233fSMatthew Knepley 2449bdad233fSMatthew Knepley Not collective 2450bdad233fSMatthew Knepley 2451bdad233fSMatthew Knepley Input Parameter: 2452bcf0153eSBarry Smith . ts - The `TS` 2453bdad233fSMatthew Knepley 2454bdad233fSMatthew Knepley Output Parameter: 2455bcf0153eSBarry Smith . type - One of `TS_LINEAR`, `TS_NONLINEAR` where these types refer to problems of the forms 2456bdad233fSMatthew Knepley .vb 2457089b2837SJed Brown M U_t = A U 2458089b2837SJed Brown M(t) U_t = A(t) U 2459b5abc632SBarry Smith F(t,U,U_t) 2460bdad233fSMatthew Knepley .ve 2461bdad233fSMatthew Knepley 2462bdad233fSMatthew Knepley Level: beginner 2463bdad233fSMatthew Knepley 2464bcf0153eSBarry Smith .seealso: [](chapter_ts), `TSSetUp()`, `TSProblemType`, `TS` 2465bdad233fSMatthew Knepley @*/ 2466d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 2467d71ae5a4SJacob Faibussowitsch { 2468bdad233fSMatthew Knepley PetscFunctionBegin; 24690700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 24704482741eSBarry Smith PetscValidIntPointer(type, 2); 2471bdad233fSMatthew Knepley *type = ts->problem_type; 24723ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2473bdad233fSMatthew Knepley } 2474d763cef2SBarry Smith 2475303a5415SBarry Smith /* 2476303a5415SBarry 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() 2477303a5415SBarry Smith */ 2478d71ae5a4SJacob Faibussowitsch static PetscErrorCode TSSetExactFinalTimeDefault(TS ts) 2479d71ae5a4SJacob Faibussowitsch { 2480303a5415SBarry Smith PetscBool isnone; 2481303a5415SBarry Smith 2482303a5415SBarry Smith PetscFunctionBegin; 24839566063dSJacob Faibussowitsch PetscCall(TSGetAdapt(ts, &ts->adapt)); 24849566063dSJacob Faibussowitsch PetscCall(TSAdaptSetDefaultType(ts->adapt, ts->default_adapt_type)); 2485303a5415SBarry Smith 24869566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)ts->adapt, TSADAPTNONE, &isnone)); 24871e66621cSBarry Smith if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP; 24881e66621cSBarry Smith else if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) ts->exact_final_time = TS_EXACTFINALTIME_INTERPOLATE; 24893ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2490303a5415SBarry Smith } 2491303a5415SBarry Smith 2492d763cef2SBarry Smith /*@ 2493303a5415SBarry Smith TSSetUp - Sets up the internal data structures for the later use of a timestepper. 2494d763cef2SBarry Smith 2495c3339decSBarry Smith Collective 2496d763cef2SBarry Smith 2497d763cef2SBarry Smith Input Parameter: 2498bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2499d763cef2SBarry Smith 2500d763cef2SBarry Smith Level: advanced 2501d763cef2SBarry Smith 2502bcf0153eSBarry Smith Note: 2503bcf0153eSBarry Smith For basic use of the `TS` solvers the user need not explicitly call 2504bcf0153eSBarry Smith `TSSetUp()`, since these actions will automatically occur during 2505bcf0153eSBarry Smith the call to `TSStep()` or `TSSolve()`. However, if one wishes to control this 2506bcf0153eSBarry Smith phase separately, `TSSetUp()` should be called after `TSCreate()` 2507bcf0153eSBarry Smith and optional routines of the form TSSetXXX(), but before `TSStep()` and `TSSolve()`. 2508bcf0153eSBarry Smith 2509bcf0153eSBarry Smith .seealso: [](chapter_ts), `TSCreate()`, `TS`, `TSStep()`, `TSDestroy()`, `TSSolve()` 2510d763cef2SBarry Smith @*/ 2511d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetUp(TS ts) 2512d71ae5a4SJacob Faibussowitsch { 25136c6b9e74SPeter Brune DM dm; 25146c6b9e74SPeter Brune PetscErrorCode (*func)(SNES, Vec, Vec, void *); 2515d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES, Vec, Mat, Mat, void *); 2516cd11d68dSLisandro Dalcin TSIFunction ifun; 25176c6b9e74SPeter Brune TSIJacobian ijac; 2518efe9872eSLisandro Dalcin TSI2Jacobian i2jac; 25196c6b9e74SPeter Brune TSRHSJacobian rhsjac; 2520d763cef2SBarry Smith 2521d763cef2SBarry Smith PetscFunctionBegin; 25220700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 25233ba16761SJacob Faibussowitsch if (ts->setupcalled) PetscFunctionReturn(PETSC_SUCCESS); 2524277b19d0SLisandro Dalcin 25257adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 25269566063dSJacob Faibussowitsch PetscCall(TSGetIFunction(ts, NULL, &ifun, NULL)); 25279566063dSJacob Faibussowitsch PetscCall(TSSetType(ts, ifun ? TSBEULER : TSEULER)); 2528d763cef2SBarry Smith } 2529277b19d0SLisandro Dalcin 25301a638600SStefano Zampini if (!ts->vec_sol) { 25311e66621cSBarry Smith PetscCheck(ts->dm, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call TSSetSolution() first"); 25329566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(ts->dm, &ts->vec_sol)); 25331a638600SStefano Zampini } 2534277b19d0SLisandro Dalcin 25354a658b32SHong Zhang if (ts->tspan) { 25361e66621cSBarry Smith if (!ts->tspan->vecs_sol) PetscCall(VecDuplicateVecs(ts->vec_sol, ts->tspan->num_span_times, &ts->tspan->vecs_sol)); 25374a658b32SHong Zhang } 2538298bade4SHong Zhang if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */ 25399566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)ts->Jacprhs)); 2540298bade4SHong Zhang ts->Jacp = ts->Jacprhs; 2541298bade4SHong Zhang } 2542298bade4SHong Zhang 2543cd4cee2dSHong Zhang if (ts->quadraturets) { 25449566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts->quadraturets)); 25459566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_costintegrand)); 25469566063dSJacob Faibussowitsch PetscCall(VecDuplicate(ts->quadraturets->vec_sol, &ts->vec_costintegrand)); 2547cd4cee2dSHong Zhang } 2548cd4cee2dSHong Zhang 25499566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts, NULL, NULL, &rhsjac, NULL)); 2550f23ba4b3SHong Zhang if (rhsjac == TSComputeRHSJacobianConstant) { 2551e1244c69SJed Brown Mat Amat, Pmat; 2552e1244c69SJed Brown SNES snes; 25539566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 25549566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes, &Amat, &Pmat, NULL, NULL)); 2555e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 2556e1244c69SJed Brown * have displaced the RHS matrix */ 2557971015bcSStefano Zampini if (Amat && Amat == ts->Arhs) { 2558abc0d4abSBarry 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 */ 25599566063dSJacob Faibussowitsch PetscCall(MatDuplicate(ts->Arhs, MAT_COPY_VALUES, &Amat)); 25609566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes, Amat, NULL, NULL, NULL)); 25619566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Amat)); 2562e1244c69SJed Brown } 2563971015bcSStefano Zampini if (Pmat && Pmat == ts->Brhs) { 25649566063dSJacob Faibussowitsch PetscCall(MatDuplicate(ts->Brhs, MAT_COPY_VALUES, &Pmat)); 25659566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes, NULL, Pmat, NULL, NULL)); 25669566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Pmat)); 2567e1244c69SJed Brown } 2568e1244c69SJed Brown } 25692ffb9264SLisandro Dalcin 25709566063dSJacob Faibussowitsch PetscCall(TSGetAdapt(ts, &ts->adapt)); 25719566063dSJacob Faibussowitsch PetscCall(TSAdaptSetDefaultType(ts->adapt, ts->default_adapt_type)); 25722ffb9264SLisandro Dalcin 2573dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, setup); 2574277b19d0SLisandro Dalcin 25759566063dSJacob Faibussowitsch PetscCall(TSSetExactFinalTimeDefault(ts)); 25762ffb9264SLisandro Dalcin 2577a6772fa2SLisandro Dalcin /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 25786c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 25796c6b9e74SPeter Brune */ 25809566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 25819566063dSJacob Faibussowitsch PetscCall(DMSNESGetFunction(dm, &func, NULL)); 25821e66621cSBarry Smith if (!func) PetscCall(DMSNESSetFunction(dm, SNESTSFormFunction, ts)); 25831e66621cSBarry Smith 2584a6772fa2SLisandro 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. 25856c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 25866c6b9e74SPeter Brune */ 25879566063dSJacob Faibussowitsch PetscCall(DMSNESGetJacobian(dm, &jac, NULL)); 25889566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm, &ijac, NULL)); 25899566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm, &i2jac, NULL)); 25909566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, &rhsjac, NULL)); 25911e66621cSBarry Smith if (!jac && (ijac || i2jac || rhsjac)) PetscCall(DMSNESSetJacobian(dm, SNESTSFormJacobian, ts)); 2592c0517034SDebojyoti Ghosh 2593c0517034SDebojyoti Ghosh /* if time integration scheme has a starting method, call it */ 2594dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, startingmethod); 2595c0517034SDebojyoti Ghosh 2596277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 25973ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2598277b19d0SLisandro Dalcin } 2599277b19d0SLisandro Dalcin 2600f6a906c0SBarry Smith /*@ 2601bcf0153eSBarry Smith TSReset - Resets a `TS` context and removes any allocated `Vec`s and `Mat`s. 2602277b19d0SLisandro Dalcin 2603c3339decSBarry Smith Collective 2604277b19d0SLisandro Dalcin 2605277b19d0SLisandro Dalcin Input Parameter: 2606bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2607277b19d0SLisandro Dalcin 2608277b19d0SLisandro Dalcin Level: beginner 2609277b19d0SLisandro Dalcin 2610bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSCreate()`, `TSSetup()`, `TSDestroy()` 2611277b19d0SLisandro Dalcin @*/ 2612d71ae5a4SJacob Faibussowitsch PetscErrorCode TSReset(TS ts) 2613d71ae5a4SJacob Faibussowitsch { 26141d06f6b3SHong Zhang TS_RHSSplitLink ilink = ts->tsrhssplit, next; 2615277b19d0SLisandro Dalcin 2616277b19d0SLisandro Dalcin PetscFunctionBegin; 2617277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2618b18ea86cSHong Zhang 2619dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, reset); 26209566063dSJacob Faibussowitsch if (ts->snes) PetscCall(SNESReset(ts->snes)); 26219566063dSJacob Faibussowitsch if (ts->adapt) PetscCall(TSAdaptReset(ts->adapt)); 2622bbd56ea5SKarl Rupp 26239566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Arhs)); 26249566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Brhs)); 26259566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->Frhs)); 26269566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_sol)); 26279566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_dot)); 26289566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vatol)); 26299566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vrtol)); 26309566063dSJacob Faibussowitsch PetscCall(VecDestroyVecs(ts->nwork, &ts->work)); 2631bbd56ea5SKarl Rupp 26329566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Jacprhs)); 26339566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Jacp)); 26341baa6e33SBarry Smith if (ts->forward_solve) PetscCall(TSForwardReset(ts)); 2635cd4cee2dSHong Zhang if (ts->quadraturets) { 26369566063dSJacob Faibussowitsch PetscCall(TSReset(ts->quadraturets)); 26379566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_costintegrand)); 2638cd4cee2dSHong Zhang } 26391d06f6b3SHong Zhang while (ilink) { 26401d06f6b3SHong Zhang next = ilink->next; 26419566063dSJacob Faibussowitsch PetscCall(TSDestroy(&ilink->ts)); 26429566063dSJacob Faibussowitsch PetscCall(PetscFree(ilink->splitname)); 26439566063dSJacob Faibussowitsch PetscCall(ISDestroy(&ilink->is)); 26449566063dSJacob Faibussowitsch PetscCall(PetscFree(ilink)); 26451d06f6b3SHong Zhang ilink = next; 264687f4e208SHong Zhang } 26476bf673ebSJoe Pusztay ts->tsrhssplit = NULL; 2648545aaa6fSHong Zhang ts->num_rhs_splits = 0; 26494a658b32SHong Zhang if (ts->tspan) { 26504a658b32SHong Zhang PetscCall(PetscFree(ts->tspan->span_times)); 26514a658b32SHong Zhang PetscCall(VecDestroyVecs(ts->tspan->num_span_times, &ts->tspan->vecs_sol)); 26524a658b32SHong Zhang PetscCall(PetscFree(ts->tspan)); 26534a658b32SHong Zhang } 2654277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 26553ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2656d763cef2SBarry Smith } 2657d763cef2SBarry Smith 26581fb7b255SJunchao Zhang /*@C 2659d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2660bcf0153eSBarry Smith with `TSCreate()`. 2661d763cef2SBarry Smith 2662c3339decSBarry Smith Collective 2663d763cef2SBarry Smith 2664d763cef2SBarry Smith Input Parameter: 2665bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2666d763cef2SBarry Smith 2667d763cef2SBarry Smith Level: beginner 2668d763cef2SBarry Smith 2669bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSCreate()`, `TSSetUp()`, `TSSolve()` 2670d763cef2SBarry Smith @*/ 2671d71ae5a4SJacob Faibussowitsch PetscErrorCode TSDestroy(TS *ts) 2672d71ae5a4SJacob Faibussowitsch { 2673d763cef2SBarry Smith PetscFunctionBegin; 26743ba16761SJacob Faibussowitsch if (!*ts) PetscFunctionReturn(PETSC_SUCCESS); 2675ecf68647SHong Zhang PetscValidHeaderSpecific(*ts, TS_CLASSID, 1); 26769371c9d4SSatish Balay if (--((PetscObject)(*ts))->refct > 0) { 26779371c9d4SSatish Balay *ts = NULL; 26783ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 26799371c9d4SSatish Balay } 2680d763cef2SBarry Smith 26819566063dSJacob Faibussowitsch PetscCall(TSReset(*ts)); 26829566063dSJacob Faibussowitsch PetscCall(TSAdjointReset(*ts)); 26831e66621cSBarry Smith if ((*ts)->forward_solve) PetscCall(TSForwardReset(*ts)); 26841e66621cSBarry Smith 2685e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 26869566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsViewOff((PetscObject)*ts)); 2687dbbe0bcdSBarry Smith PetscTryTypeMethod((*ts), destroy); 26886d4c513bSLisandro Dalcin 26899566063dSJacob Faibussowitsch PetscCall(TSTrajectoryDestroy(&(*ts)->trajectory)); 2690bc952696SBarry Smith 26919566063dSJacob Faibussowitsch PetscCall(TSAdaptDestroy(&(*ts)->adapt)); 26929566063dSJacob Faibussowitsch PetscCall(TSEventDestroy(&(*ts)->event)); 26936427ac75SLisandro Dalcin 26949566063dSJacob Faibussowitsch PetscCall(SNESDestroy(&(*ts)->snes)); 26959566063dSJacob Faibussowitsch PetscCall(DMDestroy(&(*ts)->dm)); 26969566063dSJacob Faibussowitsch PetscCall(TSMonitorCancel((*ts))); 26979566063dSJacob Faibussowitsch PetscCall(TSAdjointMonitorCancel((*ts))); 26986d4c513bSLisandro Dalcin 26999566063dSJacob Faibussowitsch PetscCall(TSDestroy(&(*ts)->quadraturets)); 27009566063dSJacob Faibussowitsch PetscCall(PetscHeaderDestroy(ts)); 27013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2702d763cef2SBarry Smith } 2703d763cef2SBarry Smith 2704d8e5e3e6SSatish Balay /*@ 2705bcf0153eSBarry Smith TSGetSNES - Returns the `SNES` (nonlinear solver) associated with 2706bcf0153eSBarry Smith a `TS` (timestepper) context. Valid only for nonlinear problems. 2707d763cef2SBarry Smith 2708bcf0153eSBarry Smith Not Collective, but snes is parallel if ts is parallel 2709d763cef2SBarry Smith 2710d763cef2SBarry Smith Input Parameter: 2711bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2712d763cef2SBarry Smith 2713d763cef2SBarry Smith Output Parameter: 2714d763cef2SBarry Smith . snes - the nonlinear solver context 2715d763cef2SBarry Smith 2716d763cef2SBarry Smith Level: beginner 2717d763cef2SBarry Smith 2718bcf0153eSBarry Smith Notes: 2719bcf0153eSBarry Smith The user can then directly manipulate the `SNES` context to set various 2720bcf0153eSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2721bcf0153eSBarry Smith `KSP`, and `PC` contexts as well. 2722bcf0153eSBarry Smith 2723bcf0153eSBarry Smith `TSGetSNES()` does not work for integrators that do not use `SNES`; in 2724*195e9b02SBarry Smith this case `TSGetSNES()` returns `NULL` in `snes`. 2725bcf0153eSBarry Smith 2726bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `SNES`, `TSCreate()`, `TSSetUp()`, `TSSolve()` 2727d763cef2SBarry Smith @*/ 2728d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSNES(TS ts, SNES *snes) 2729d71ae5a4SJacob Faibussowitsch { 2730d763cef2SBarry Smith PetscFunctionBegin; 27310700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 27324482741eSBarry Smith PetscValidPointer(snes, 2); 2733d372ba47SLisandro Dalcin if (!ts->snes) { 27349566063dSJacob Faibussowitsch PetscCall(SNESCreate(PetscObjectComm((PetscObject)ts), &ts->snes)); 27359566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptions((PetscObject)ts->snes, ((PetscObject)ts)->options)); 27369566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(ts->snes, NULL, SNESTSFormFunction, ts)); 27379566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)ts->snes, (PetscObject)ts, 1)); 27389566063dSJacob Faibussowitsch if (ts->dm) PetscCall(SNESSetDM(ts->snes, ts->dm)); 27391e66621cSBarry Smith if (ts->problem_type == TS_LINEAR) PetscCall(SNESSetType(ts->snes, SNESKSPONLY)); 2740d372ba47SLisandro Dalcin } 2741d763cef2SBarry Smith *snes = ts->snes; 27423ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2743d763cef2SBarry Smith } 2744d763cef2SBarry Smith 2745deb2cd25SJed Brown /*@ 2746bcf0153eSBarry Smith TSSetSNES - Set the `SNES` (nonlinear solver) to be used by the timestepping context 2747deb2cd25SJed Brown 2748deb2cd25SJed Brown Collective 2749deb2cd25SJed Brown 2750d8d19677SJose E. Roman Input Parameters: 2751bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 2752deb2cd25SJed Brown - snes - the nonlinear solver context 2753deb2cd25SJed Brown 2754deb2cd25SJed Brown Level: developer 2755deb2cd25SJed Brown 2756bcf0153eSBarry Smith Note: 2757bcf0153eSBarry Smith Most users should have the `TS` created by calling `TSGetSNES()` 2758bcf0153eSBarry Smith 2759bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `SNES`, `TSCreate()`, `TSSetUp()`, `TSSolve()`, `TSGetSNES()` 2760deb2cd25SJed Brown @*/ 2761d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetSNES(TS ts, SNES snes) 2762d71ae5a4SJacob Faibussowitsch { 2763d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES, Vec, Mat, Mat, void *); 2764deb2cd25SJed Brown 2765deb2cd25SJed Brown PetscFunctionBegin; 2766deb2cd25SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2767deb2cd25SJed Brown PetscValidHeaderSpecific(snes, SNES_CLASSID, 2); 27689566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)snes)); 27699566063dSJacob Faibussowitsch PetscCall(SNESDestroy(&ts->snes)); 2770bbd56ea5SKarl Rupp 2771deb2cd25SJed Brown ts->snes = snes; 2772bbd56ea5SKarl Rupp 27739566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(ts->snes, NULL, SNESTSFormFunction, ts)); 27749566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(ts->snes, NULL, NULL, &func, NULL)); 27751e66621cSBarry Smith if (func == SNESTSFormJacobian) PetscCall(SNESSetJacobian(ts->snes, NULL, NULL, SNESTSFormJacobian, ts)); 27763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2777deb2cd25SJed Brown } 2778deb2cd25SJed Brown 2779d8e5e3e6SSatish Balay /*@ 2780bcf0153eSBarry Smith TSGetKSP - Returns the `KSP` (linear solver) associated with 2781bcf0153eSBarry Smith a `TS` (timestepper) context. 2782d763cef2SBarry Smith 2783*195e9b02SBarry Smith Not Collective, but `ksp` is parallel if `ts` is parallel 2784d763cef2SBarry Smith 2785d763cef2SBarry Smith Input Parameter: 2786bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2787d763cef2SBarry Smith 2788d763cef2SBarry Smith Output Parameter: 278994b7f48cSBarry Smith . ksp - the nonlinear solver context 2790d763cef2SBarry Smith 2791d763cef2SBarry Smith Level: beginner 2792d763cef2SBarry Smith 2793bcf0153eSBarry Smith Notes: 2794bcf0153eSBarry Smith The user can then directly manipulate the `KSP` context to set various 2795bcf0153eSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2796bcf0153eSBarry Smith `PC` context as well. 2797bcf0153eSBarry Smith 2798bcf0153eSBarry Smith `TSGetKSP()` does not work for integrators that do not use `KSP`; 2799*195e9b02SBarry Smith in this case `TSGetKSP()` returns `NULL` in `ksp`. 2800bcf0153eSBarry Smith 2801bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `SNES`, `KSP`, `TSCreate()`, `TSSetUp()`, `TSSolve()`, `TSGetSNES()` 2802d763cef2SBarry Smith @*/ 2803d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetKSP(TS ts, KSP *ksp) 2804d71ae5a4SJacob Faibussowitsch { 2805089b2837SJed Brown SNES snes; 2806d372ba47SLisandro Dalcin 2807d763cef2SBarry Smith PetscFunctionBegin; 28080700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 28094482741eSBarry Smith PetscValidPointer(ksp, 2); 28103c633725SBarry Smith PetscCheck(((PetscObject)ts)->type_name, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "KSP is not created yet. Call TSSetType() first"); 28113c633725SBarry Smith PetscCheck(ts->problem_type == TS_LINEAR, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Linear only; use TSGetSNES()"); 28129566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 28139566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes, ksp)); 28143ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2815d763cef2SBarry Smith } 2816d763cef2SBarry Smith 2817d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2818d763cef2SBarry Smith 2819adb62b0dSMatthew Knepley /*@ 2820618ce8baSLisandro Dalcin TSSetMaxSteps - Sets the maximum number of steps to use. 2821618ce8baSLisandro Dalcin 2822c3339decSBarry Smith Logically Collective 2823618ce8baSLisandro Dalcin 2824618ce8baSLisandro Dalcin Input Parameters: 2825bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 2826618ce8baSLisandro Dalcin - maxsteps - maximum number of steps to use 2827618ce8baSLisandro Dalcin 2828bcf0153eSBarry Smith Options Database Key: 2829618ce8baSLisandro Dalcin . -ts_max_steps <maxsteps> - Sets maxsteps 2830618ce8baSLisandro Dalcin 2831618ce8baSLisandro Dalcin Level: intermediate 2832618ce8baSLisandro Dalcin 2833bcf0153eSBarry Smith Note: 2834bcf0153eSBarry Smith The default maximum number of steps is 5000 2835bcf0153eSBarry Smith 2836bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetMaxSteps()`, `TSSetMaxTime()`, `TSSetExactFinalTime()` 2837618ce8baSLisandro Dalcin @*/ 2838d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxSteps(TS ts, PetscInt maxsteps) 2839d71ae5a4SJacob Faibussowitsch { 2840618ce8baSLisandro Dalcin PetscFunctionBegin; 2841618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2842618ce8baSLisandro Dalcin PetscValidLogicalCollectiveInt(ts, maxsteps, 2); 28433c633725SBarry Smith PetscCheck(maxsteps >= 0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Maximum number of steps must be non-negative"); 2844618ce8baSLisandro Dalcin ts->max_steps = maxsteps; 28453ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2846618ce8baSLisandro Dalcin } 2847618ce8baSLisandro Dalcin 2848618ce8baSLisandro Dalcin /*@ 2849618ce8baSLisandro Dalcin TSGetMaxSteps - Gets the maximum number of steps to use. 2850618ce8baSLisandro Dalcin 2851618ce8baSLisandro Dalcin Not Collective 2852618ce8baSLisandro Dalcin 2853618ce8baSLisandro Dalcin Input Parameters: 2854bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2855618ce8baSLisandro Dalcin 2856618ce8baSLisandro Dalcin Output Parameter: 2857618ce8baSLisandro Dalcin . maxsteps - maximum number of steps to use 2858618ce8baSLisandro Dalcin 2859618ce8baSLisandro Dalcin Level: advanced 2860618ce8baSLisandro Dalcin 2861bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetMaxSteps()`, `TSGetMaxTime()`, `TSSetMaxTime()` 2862618ce8baSLisandro Dalcin @*/ 2863d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetMaxSteps(TS ts, PetscInt *maxsteps) 2864d71ae5a4SJacob Faibussowitsch { 2865618ce8baSLisandro Dalcin PetscFunctionBegin; 2866618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2867618ce8baSLisandro Dalcin PetscValidIntPointer(maxsteps, 2); 2868618ce8baSLisandro Dalcin *maxsteps = ts->max_steps; 28693ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2870618ce8baSLisandro Dalcin } 2871618ce8baSLisandro Dalcin 2872618ce8baSLisandro Dalcin /*@ 2873618ce8baSLisandro Dalcin TSSetMaxTime - Sets the maximum (or final) time for timestepping. 2874618ce8baSLisandro Dalcin 2875c3339decSBarry Smith Logically Collective 2876618ce8baSLisandro Dalcin 2877618ce8baSLisandro Dalcin Input Parameters: 2878bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 2879618ce8baSLisandro Dalcin - maxtime - final time to step to 2880618ce8baSLisandro Dalcin 2881bcf0153eSBarry Smith Options Database Key: 2882ef85077eSLisandro Dalcin . -ts_max_time <maxtime> - Sets maxtime 2883618ce8baSLisandro Dalcin 2884bcf0153eSBarry Smith Level: intermediate 2885bcf0153eSBarry Smith 2886618ce8baSLisandro Dalcin Notes: 2887618ce8baSLisandro Dalcin The default maximum time is 5.0 2888618ce8baSLisandro Dalcin 2889bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetMaxTime()`, `TSSetMaxSteps()`, `TSSetExactFinalTime()` 2890618ce8baSLisandro Dalcin @*/ 2891d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxTime(TS ts, PetscReal maxtime) 2892d71ae5a4SJacob Faibussowitsch { 2893618ce8baSLisandro Dalcin PetscFunctionBegin; 2894618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2895618ce8baSLisandro Dalcin PetscValidLogicalCollectiveReal(ts, maxtime, 2); 2896618ce8baSLisandro Dalcin ts->max_time = maxtime; 28973ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2898618ce8baSLisandro Dalcin } 2899618ce8baSLisandro Dalcin 2900618ce8baSLisandro Dalcin /*@ 2901618ce8baSLisandro Dalcin TSGetMaxTime - Gets the maximum (or final) time for timestepping. 2902618ce8baSLisandro Dalcin 2903618ce8baSLisandro Dalcin Not Collective 2904618ce8baSLisandro Dalcin 2905618ce8baSLisandro Dalcin Input Parameters: 2906bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2907618ce8baSLisandro Dalcin 2908618ce8baSLisandro Dalcin Output Parameter: 2909618ce8baSLisandro Dalcin . maxtime - final time to step to 2910618ce8baSLisandro Dalcin 2911618ce8baSLisandro Dalcin Level: advanced 2912618ce8baSLisandro Dalcin 2913bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetMaxTime()`, `TSGetMaxSteps()`, `TSSetMaxSteps()` 2914618ce8baSLisandro Dalcin @*/ 2915d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetMaxTime(TS ts, PetscReal *maxtime) 2916d71ae5a4SJacob Faibussowitsch { 2917618ce8baSLisandro Dalcin PetscFunctionBegin; 2918618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2919618ce8baSLisandro Dalcin PetscValidRealPointer(maxtime, 2); 2920618ce8baSLisandro Dalcin *maxtime = ts->max_time; 29213ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2922618ce8baSLisandro Dalcin } 2923618ce8baSLisandro Dalcin 2924618ce8baSLisandro Dalcin /*@ 2925bcf0153eSBarry Smith TSSetInitialTimeStep - Deprecated, use `TSSetTime()` and `TSSetTimeStep()`. 2926edc382c3SSatish Balay 2927edc382c3SSatish Balay Level: deprecated 2928edc382c3SSatish Balay 2929aaa6c58dSLisandro Dalcin @*/ 2930d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetInitialTimeStep(TS ts, PetscReal initial_time, PetscReal time_step) 2931d71ae5a4SJacob Faibussowitsch { 2932aaa6c58dSLisandro Dalcin PetscFunctionBegin; 2933aaa6c58dSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 29349566063dSJacob Faibussowitsch PetscCall(TSSetTime(ts, initial_time)); 29359566063dSJacob Faibussowitsch PetscCall(TSSetTimeStep(ts, time_step)); 29363ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2937aaa6c58dSLisandro Dalcin } 2938aaa6c58dSLisandro Dalcin 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) { 29504482741eSBarry Smith PetscValidIntPointer(maxsteps, 2); 2951adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2952adb62b0dSMatthew Knepley } 2953abc0a331SBarry Smith if (maxtime) { 2954064a246eSJacob Faibussowitsch PetscValidRealPointer(maxtime, 3); 2955adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2956adb62b0dSMatthew Knepley } 29573ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2958adb62b0dSMatthew Knepley } 2959adb62b0dSMatthew Knepley 2960d763cef2SBarry Smith /*@ 2961bcf0153eSBarry Smith TSSetDuration - Deprecated, use `TSSetMaxSteps()` and `TSSetMaxTime()`. 2962edc382c3SSatish Balay 2963edc382c3SSatish Balay Level: deprecated 2964edc382c3SSatish Balay 2965d763cef2SBarry Smith @*/ 2966d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetDuration(TS ts, PetscInt maxsteps, PetscReal maxtime) 2967d71ae5a4SJacob Faibussowitsch { 2968d763cef2SBarry Smith PetscFunctionBegin; 29690700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2970c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts, maxsteps, 2); 2971064a246eSJacob Faibussowitsch PetscValidLogicalCollectiveReal(ts, maxtime, 3); 297239b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 297339b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 29743ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2975d763cef2SBarry Smith } 2976d763cef2SBarry Smith 2977d763cef2SBarry Smith /*@ 2978bcf0153eSBarry Smith TSGetTimeStepNumber - Deprecated, use `TSGetStepNumber()`. 2979edc382c3SSatish Balay 2980edc382c3SSatish Balay Level: deprecated 2981edc382c3SSatish Balay 29825c5f5948SLisandro Dalcin @*/ 2983d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeStepNumber(TS ts, PetscInt *steps) 2984d71ae5a4SJacob Faibussowitsch { 29859371c9d4SSatish Balay return TSGetStepNumber(ts, steps); 29869371c9d4SSatish Balay } 29875c5f5948SLisandro Dalcin 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 3011bcf0153eSBarry Smith .seealso: [](chapter_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 3039000e7ae3SMatthew Knepley Calling sequence of func: 304067b8a455SSatish Balay .vb 304167b8a455SSatish Balay PetscErrorCode func (TS ts); 304267b8a455SSatish Balay .ve 3043000e7ae3SMatthew Knepley 3044000e7ae3SMatthew Knepley Level: intermediate 3045000e7ae3SMatthew Knepley 3046bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetPreStage()`, `TSSetPostStage()`, `TSSetPostStep()`, `TSStep()`, `TSRestartStep()` 3047000e7ae3SMatthew Knepley @*/ 3048d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 3049d71ae5a4SJacob Faibussowitsch { 3050000e7ae3SMatthew Knepley PetscFunctionBegin; 30510700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3052ae60f76fSBarry Smith ts->prestep = func; 30533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3054000e7ae3SMatthew Knepley } 3055000e7ae3SMatthew Knepley 305609ee8438SJed Brown /*@ 3057bcf0153eSBarry Smith TSPreStep - Runs the user-defined pre-step function provided with `TSSetPreStep()` 30583f2090d5SJed Brown 3059c3339decSBarry Smith Collective 30603f2090d5SJed Brown 30613f2090d5SJed Brown Input Parameters: 3062bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 30633f2090d5SJed Brown 30643f2090d5SJed Brown Level: developer 30653f2090d5SJed Brown 3066bcf0153eSBarry Smith Note: 3067bcf0153eSBarry Smith `TSPreStep()` is typically used within time stepping implementations, 3068bcf0153eSBarry Smith so most users would not generally call this routine themselves. 3069bcf0153eSBarry Smith 3070bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetPreStep()`, `TSPreStage()`, `TSPostStage()`, `TSPostStep()` 30713f2090d5SJed Brown @*/ 3072d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPreStep(TS ts) 3073d71ae5a4SJacob Faibussowitsch { 30743f2090d5SJed Brown PetscFunctionBegin; 30750700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3076ae60f76fSBarry Smith if (ts->prestep) { 30775efd42a4SStefano Zampini Vec U; 3078ef8d1ce0SJohann Rudi PetscObjectId idprev; 3079ef8d1ce0SJohann Rudi PetscBool sameObject; 30805efd42a4SStefano Zampini PetscObjectState sprev, spost; 30815efd42a4SStefano Zampini 30829566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts, &U)); 30839566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U, &idprev)); 30849566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &sprev)); 308525e27a38SBarry Smith PetscCallBack("TS callback preset", (*ts->prestep)(ts)); 30869566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts, &U)); 30879566063dSJacob Faibussowitsch PetscCall(PetscObjectCompareId((PetscObject)U, idprev, &sameObject)); 30889566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &spost)); 30899566063dSJacob Faibussowitsch if (!sameObject || sprev != spost) PetscCall(TSRestartStep(ts)); 3090312ce896SJed Brown } 30913ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 30923f2090d5SJed Brown } 30933f2090d5SJed Brown 3094b8123daeSJed Brown /*@C 3095b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 3096b8123daeSJed Brown called once at the beginning of each stage. 3097b8123daeSJed Brown 3098c3339decSBarry Smith Logically Collective 3099b8123daeSJed Brown 3100b8123daeSJed Brown Input Parameters: 3101bcf0153eSBarry Smith + ts - The `TS` context obtained from `TSCreate()` 3102b8123daeSJed Brown - func - The function 3103b8123daeSJed Brown 3104b8123daeSJed Brown Calling sequence of func: 310567b8a455SSatish Balay .vb 310667b8a455SSatish Balay PetscErrorCode func(TS ts, PetscReal stagetime); 310767b8a455SSatish Balay .ve 3108b8123daeSJed Brown 3109b8123daeSJed Brown Level: intermediate 3110b8123daeSJed Brown 3111b8123daeSJed Brown Note: 3112b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 3113bcf0153eSBarry Smith The time step number being computed can be queried using `TSGetStepNumber()` and the total size of the step being 3114bcf0153eSBarry Smith attempted can be obtained using `TSGetTimeStep()`. The time at the start of the step is available via `TSGetTime()`. 3115b8123daeSJed Brown 3116bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetPostStage()`, `TSSetPreStep()`, `TSSetPostStep()`, `TSGetApplicationContext()` 3117b8123daeSJed Brown @*/ 3118d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS, PetscReal)) 3119d71ae5a4SJacob Faibussowitsch { 3120b8123daeSJed Brown PetscFunctionBegin; 3121b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3122ae60f76fSBarry Smith ts->prestage = func; 31233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3124b8123daeSJed Brown } 3125b8123daeSJed Brown 31269be3e283SDebojyoti Ghosh /*@C 3127bcf0153eSBarry Smith TSSetPostStage - Sets the general-purpose function, provided with `TSSetPostStep()`, 31289be3e283SDebojyoti Ghosh called once at the end of each stage. 31299be3e283SDebojyoti Ghosh 3130c3339decSBarry Smith Logically Collective 31319be3e283SDebojyoti Ghosh 31329be3e283SDebojyoti Ghosh Input Parameters: 3133bcf0153eSBarry Smith + ts - The `TS` context obtained from `TSCreate()` 31349be3e283SDebojyoti Ghosh - func - The function 31359be3e283SDebojyoti Ghosh 31369be3e283SDebojyoti Ghosh Calling sequence of func: 313767b8a455SSatish Balay .vb 313867b8a455SSatish Balay PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 313967b8a455SSatish Balay .ve 31409be3e283SDebojyoti Ghosh 31419be3e283SDebojyoti Ghosh Level: intermediate 31429be3e283SDebojyoti Ghosh 31439be3e283SDebojyoti Ghosh Note: 31449be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 3145bcf0153eSBarry Smith The time step number being computed can be queried using `TSGetStepNumber()` and the total size of the step being 3146bcf0153eSBarry Smith attempted can be obtained using `TSGetTimeStep()`. The time at the start of the step is available via `TSGetTime()`. 31479be3e283SDebojyoti Ghosh 3148bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetPreStage()`, `TSSetPreStep()`, `TSSetPostStep()`, `TSGetApplicationContext()` 31499be3e283SDebojyoti Ghosh @*/ 3150d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS, PetscReal, PetscInt, Vec *)) 3151d71ae5a4SJacob Faibussowitsch { 31529be3e283SDebojyoti Ghosh PetscFunctionBegin; 31539be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 31549be3e283SDebojyoti Ghosh ts->poststage = func; 31553ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 31569be3e283SDebojyoti Ghosh } 31579be3e283SDebojyoti Ghosh 3158c688d042SShri Abhyankar /*@C 3159c688d042SShri Abhyankar TSSetPostEvaluate - Sets the general-purpose function 3160c688d042SShri Abhyankar called once at the end of each step evaluation. 3161c688d042SShri Abhyankar 3162c3339decSBarry Smith Logically Collective 3163c688d042SShri Abhyankar 3164c688d042SShri Abhyankar Input Parameters: 3165bcf0153eSBarry Smith + ts - The `TS` context obtained from `TSCreate()` 3166c688d042SShri Abhyankar - func - The function 3167c688d042SShri Abhyankar 3168c688d042SShri Abhyankar Calling sequence of func: 316967b8a455SSatish Balay .vb 317067b8a455SSatish Balay PetscErrorCode func(TS ts); 317167b8a455SSatish Balay .ve 3172c688d042SShri Abhyankar 3173c688d042SShri Abhyankar Level: intermediate 3174c688d042SShri Abhyankar 3175c688d042SShri Abhyankar Note: 3176bcf0153eSBarry Smith Semantically, `TSSetPostEvaluate()` differs from `TSSetPostStep()` since the function it sets is called before event-handling 3177bcf0153eSBarry Smith thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, `TSPostStep()` 3178bcf0153eSBarry Smith may be passed a different solution, possibly changed by the event handler. `TSPostEvaluate()` is called after the next step 3179bcf0153eSBarry Smith solution is evaluated allowing to modify it, if need be. The solution can be obtained with `TSGetSolution()`, the time step 3180bcf0153eSBarry Smith with `TSGetTimeStep()`, and the time at the start of the step is available via `TSGetTime()` 3181c688d042SShri Abhyankar 3182bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetPreStage()`, `TSSetPreStep()`, `TSSetPostStep()`, `TSGetApplicationContext()` 3183c688d042SShri Abhyankar @*/ 3184d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS)) 3185d71ae5a4SJacob Faibussowitsch { 3186c688d042SShri Abhyankar PetscFunctionBegin; 3187c688d042SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3188c688d042SShri Abhyankar ts->postevaluate = func; 31893ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3190c688d042SShri Abhyankar } 3191c688d042SShri Abhyankar 3192b8123daeSJed Brown /*@ 3193bcf0153eSBarry Smith TSPreStage - Runs the user-defined pre-stage function set using `TSSetPreStage()` 3194b8123daeSJed Brown 3195c3339decSBarry Smith Collective 3196b8123daeSJed Brown 3197b8123daeSJed Brown Input Parameters: 3198bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 31999be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 3200b8123daeSJed Brown 3201b8123daeSJed Brown Level: developer 3202b8123daeSJed Brown 3203bcf0153eSBarry Smith Note: 3204bcf0153eSBarry Smith `TSPreStage()` is typically used within time stepping implementations, 3205bcf0153eSBarry Smith most users would not generally call this routine themselves. 3206bcf0153eSBarry Smith 3207bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSPostStage()`, `TSSetPreStep()`, `TSPreStep()`, `TSPostStep()` 3208b8123daeSJed Brown @*/ 3209d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 3210d71ae5a4SJacob Faibussowitsch { 3211b8123daeSJed Brown PetscFunctionBegin; 3212b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 32131e66621cSBarry Smith if (ts->prestage) PetscCallBack("TS callback prestage", (*ts->prestage)(ts, stagetime)); 32143ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3215b8123daeSJed Brown } 3216b8123daeSJed Brown 32179be3e283SDebojyoti Ghosh /*@ 3218bcf0153eSBarry Smith TSPostStage - Runs the user-defined post-stage function set using `TSSetPostStage()` 32199be3e283SDebojyoti Ghosh 3220c3339decSBarry Smith Collective 32219be3e283SDebojyoti Ghosh 32229be3e283SDebojyoti Ghosh Input Parameters: 3223bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 32249be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 32259be3e283SDebojyoti Ghosh stageindex - Stage number 32269be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 32279be3e283SDebojyoti Ghosh of stages) with the stage solutions 32289be3e283SDebojyoti Ghosh 32299be3e283SDebojyoti Ghosh Level: developer 32309be3e283SDebojyoti Ghosh 3231bcf0153eSBarry Smith Note: 3232bcf0153eSBarry Smith `TSPostStage()` is typically used within time stepping implementations, 3233bcf0153eSBarry Smith most users would not generally call this routine themselves. 3234bcf0153eSBarry Smith 3235bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSPreStage()`, `TSSetPreStep()`, `TSPreStep()`, `TSPostStep()` 32369be3e283SDebojyoti Ghosh @*/ 3237d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 3238d71ae5a4SJacob Faibussowitsch { 32399be3e283SDebojyoti Ghosh PetscFunctionBegin; 32409be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 32411e66621cSBarry Smith if (ts->poststage) PetscCallBack("TS callback poststage", (*ts->poststage)(ts, stagetime, stageindex, Y)); 32423ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 32439be3e283SDebojyoti Ghosh } 32449be3e283SDebojyoti Ghosh 3245c688d042SShri Abhyankar /*@ 3246bcf0153eSBarry Smith TSPostEvaluate - Runs the user-defined post-evaluate function set using `TSSetPostEvaluate()` 3247c688d042SShri Abhyankar 3248c3339decSBarry Smith Collective 3249c688d042SShri Abhyankar 3250c688d042SShri Abhyankar Input Parameters: 3251bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 3252c688d042SShri Abhyankar 3253c688d042SShri Abhyankar Level: developer 3254c688d042SShri Abhyankar 3255bcf0153eSBarry Smith Note: 3256bcf0153eSBarry Smith `TSPostEvaluate()` is typically used within time stepping implementations, 3257bcf0153eSBarry Smith most users would not generally call this routine themselves. 3258bcf0153eSBarry Smith 3259bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetPostEvaluate()`, `TSSetPreStep()`, `TSPreStep()`, `TSPostStep()` 3260c688d042SShri Abhyankar @*/ 3261d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPostEvaluate(TS ts) 3262d71ae5a4SJacob Faibussowitsch { 3263c688d042SShri Abhyankar PetscFunctionBegin; 3264c688d042SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3265c688d042SShri Abhyankar if (ts->postevaluate) { 3266dcb233daSLisandro Dalcin Vec U; 3267dcb233daSLisandro Dalcin PetscObjectState sprev, spost; 3268dcb233daSLisandro Dalcin 32699566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts, &U)); 32709566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &sprev)); 327125e27a38SBarry Smith PetscCallBack("TS callback postevaluate", (*ts->postevaluate)(ts)); 32729566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &spost)); 32739566063dSJacob Faibussowitsch if (sprev != spost) PetscCall(TSRestartStep(ts)); 3274c688d042SShri Abhyankar } 32753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3276c688d042SShri Abhyankar } 3277c688d042SShri Abhyankar 3278ac226902SBarry Smith /*@C 3279000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 32803f2090d5SJed Brown called once at the end of each time step. 3281000e7ae3SMatthew Knepley 3282c3339decSBarry Smith Logically Collective 3283000e7ae3SMatthew Knepley 3284000e7ae3SMatthew Knepley Input Parameters: 3285bcf0153eSBarry Smith + ts - The `TS` context obtained from `TSCreate()` 3286000e7ae3SMatthew Knepley - func - The function 3287000e7ae3SMatthew Knepley 3288000e7ae3SMatthew Knepley Calling sequence of func: 3289b8123daeSJed Brown $ func (TS ts); 3290000e7ae3SMatthew Knepley 3291000e7ae3SMatthew Knepley Level: intermediate 3292000e7ae3SMatthew Knepley 3293bcf0153eSBarry Smith Note: 3294*195e9b02SBarry Smith The function set by `TSSetPostStep()` is called after each successful step. The solution vector 3295bcf0153eSBarry Smith obtained by `TSGetSolution()` may be different than that computed at the step end if the event handler 3296bcf0153eSBarry Smith locates an event and `TSPostEvent()` modifies it. Use `TSSetPostEvaluate()` if an unmodified solution is needed instead. 3297bcf0153eSBarry Smith 3298bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostEvaluate()`, `TSGetTimeStep()`, `TSGetStepNumber()`, `TSGetTime()`, `TSRestartStep()` 3299000e7ae3SMatthew Knepley @*/ 3300d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 3301d71ae5a4SJacob Faibussowitsch { 3302000e7ae3SMatthew Knepley PetscFunctionBegin; 33030700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3304ae60f76fSBarry Smith ts->poststep = func; 33053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3306000e7ae3SMatthew Knepley } 3307000e7ae3SMatthew Knepley 330809ee8438SJed Brown /*@ 3309*195e9b02SBarry Smith TSPostStep - Runs the user-defined post-step function that was set with `TSSetPostStep()` 33103f2090d5SJed Brown 3311c3339decSBarry Smith Collective 33123f2090d5SJed Brown 33133f2090d5SJed Brown Input Parameters: 3314bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 33153f2090d5SJed Brown 3316bcf0153eSBarry Smith Note: 3317bcf0153eSBarry Smith `TSPostStep()` is typically used within time stepping implementations, 33183f2090d5SJed Brown so most users would not generally call this routine themselves. 33193f2090d5SJed Brown 33203f2090d5SJed Brown Level: developer 33213f2090d5SJed Brown 3322bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostEvaluate()`, `TSGetTimeStep()`, `TSGetStepNumber()`, `TSGetTime()`, `TSSetPotsStep()` 33233f2090d5SJed Brown @*/ 3324d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPostStep(TS ts) 3325d71ae5a4SJacob Faibussowitsch { 33263f2090d5SJed Brown PetscFunctionBegin; 33270700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3328ae60f76fSBarry Smith if (ts->poststep) { 33295efd42a4SStefano Zampini Vec U; 3330ef8d1ce0SJohann Rudi PetscObjectId idprev; 3331ef8d1ce0SJohann Rudi PetscBool sameObject; 33325efd42a4SStefano Zampini PetscObjectState sprev, spost; 33335efd42a4SStefano Zampini 33349566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts, &U)); 33359566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U, &idprev)); 33369566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &sprev)); 333725e27a38SBarry Smith PetscCallBack("TS callback poststep", (*ts->poststep)(ts)); 33389566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts, &U)); 33399566063dSJacob Faibussowitsch PetscCall(PetscObjectCompareId((PetscObject)U, idprev, &sameObject)); 33409566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &spost)); 33419566063dSJacob Faibussowitsch if (!sameObject || sprev != spost) PetscCall(TSRestartStep(ts)); 334272ac3e02SJed Brown } 33433ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 33443f2090d5SJed Brown } 33453f2090d5SJed Brown 3346cd652676SJed Brown /*@ 3347cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3348cd652676SJed Brown 3349c3339decSBarry Smith Collective 3350cd652676SJed Brown 33514165533cSJose E. Roman Input Parameters: 3352cd652676SJed Brown + ts - time stepping context 3353cd652676SJed Brown - t - time to interpolate to 3354cd652676SJed Brown 33554165533cSJose E. Roman Output Parameter: 33560910c330SBarry Smith . U - state at given time 3357cd652676SJed Brown 3358cd652676SJed Brown Level: intermediate 3359cd652676SJed Brown 3360bcf0153eSBarry Smith Developer Note: 3361bcf0153eSBarry Smith `TSInterpolate()` and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3362cd652676SJed Brown 3363bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetExactFinalTime()`, `TSSolve()` 3364cd652676SJed Brown @*/ 3365d71ae5a4SJacob Faibussowitsch PetscErrorCode TSInterpolate(TS ts, PetscReal t, Vec U) 3366d71ae5a4SJacob Faibussowitsch { 3367cd652676SJed Brown PetscFunctionBegin; 3368cd652676SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3369b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 337063a3b9bcSJacob 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); 3371dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, interpolate, t, U); 33723ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3373cd652676SJed Brown } 3374cd652676SJed Brown 3375d763cef2SBarry Smith /*@ 33766d9e5789SSean Farley TSStep - Steps one time step 3377d763cef2SBarry Smith 3378c3339decSBarry Smith Collective 3379d763cef2SBarry Smith 3380d763cef2SBarry Smith Input Parameter: 3381bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 3382d763cef2SBarry Smith 338327829d71SBarry Smith Level: developer 3384d763cef2SBarry Smith 3385b8123daeSJed Brown Notes: 3386bcf0153eSBarry Smith The public interface for the ODE/DAE solvers is `TSSolve()`, you should almost for sure be using that routine and not this routine. 338727829d71SBarry Smith 3388bcf0153eSBarry Smith The hook set using `TSSetPreStep()` is called before each attempt to take the step. In general, the time step size may 3389b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3390b8123daeSJed Brown 3391bcf0153eSBarry Smith This may over-step the final time provided in `TSSetMaxTime()` depending on the time-step used. `TSSolve()` interpolates to exactly the 3392bcf0153eSBarry Smith time provided in `TSSetMaxTime()`. One can use `TSInterpolate()` to determine an interpolated solution within the final timestep. 339325cb2221SBarry Smith 3394bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSCreate()`, `TSSetUp()`, `TSDestroy()`, `TSSolve()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostStage()`, `TSInterpolate()` 3395d763cef2SBarry Smith @*/ 3396d71ae5a4SJacob Faibussowitsch PetscErrorCode TSStep(TS ts) 3397d71ae5a4SJacob Faibussowitsch { 3398fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3399be5899b3SLisandro Dalcin PetscReal ptime; 3400d763cef2SBarry Smith 3401d763cef2SBarry Smith PetscFunctionBegin; 34020700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3403d0609cedSBarry Smith PetscCall(PetscCitationsRegister("@article{tspaper,\n" 3404fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3405f1d62c27SHong Zhang " author = {Abhyankar, Shrirang and Brown, Jed and Constantinescu, Emil and Ghosh, Debojyoti and Smith, Barry F. and Zhang, Hong},\n" 3406f1d62c27SHong Zhang " journal = {arXiv e-preprints},\n" 3407f1d62c27SHong Zhang " eprint = {1806.01437},\n" 3408f1d62c27SHong Zhang " archivePrefix = {arXiv},\n" 34099371c9d4SSatish Balay " year = {2018}\n}\n", 34109371c9d4SSatish Balay &cite)); 34119566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts)); 34129566063dSJacob Faibussowitsch PetscCall(TSTrajectorySetUp(ts->trajectory, ts)); 3413d405a339SMatthew Knepley 34143c633725SBarry Smith PetscCheck(ts->max_time < PETSC_MAX_REAL || ts->max_steps != PETSC_MAX_INT, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "You must call TSSetMaxTime() or TSSetMaxSteps(), or use -ts_max_time <time> or -ts_max_steps <steps>"); 34153c633725SBarry 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()"); 34163c633725SBarry 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"); 3417a6772fa2SLisandro Dalcin 3418be5899b3SLisandro Dalcin if (!ts->steps) ts->ptime_prev = ts->ptime; 34199371c9d4SSatish Balay ptime = ts->ptime; 34209371c9d4SSatish Balay ts->ptime_prev_rollback = ts->ptime_prev; 34212808aa04SLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3422fc8dbba5SLisandro Dalcin 34239566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_Step, ts, 0, 0, 0)); 3424dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, step); 34259566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_Step, ts, 0, 0, 0)); 3426fc8dbba5SLisandro Dalcin 34279371c9d4SSatish Balay if (ts->tspan && PetscIsCloseAtTol(ts->ptime, ts->tspan->span_times[ts->tspan->spanctr], ts->tspan->reltol * ts->time_step + ts->tspan->abstol, 0) && ts->tspan->spanctr < ts->tspan->num_span_times) 34289371c9d4SSatish Balay PetscCall(VecCopy(ts->vec_sol, ts->tspan->vecs_sol[ts->tspan->spanctr++])); 3429fc8dbba5SLisandro Dalcin if (ts->reason >= 0) { 3430be5899b3SLisandro Dalcin ts->ptime_prev = ptime; 34312808aa04SLisandro Dalcin ts->steps++; 3432be5899b3SLisandro Dalcin ts->steprollback = PETSC_FALSE; 343328d5b5d6SLisandro Dalcin ts->steprestart = PETSC_FALSE; 3434d2daff3dSHong Zhang } 3435fc8dbba5SLisandro Dalcin if (!ts->reason) { 343608c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 343708c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 343808c7845fSBarry Smith } 3439fc8dbba5SLisandro Dalcin 34405c9bbc89SJed Brown if (ts->reason < 0 && ts->errorifstepfailed) { 34415c9bbc89SJed Brown PetscCall(TSMonitorCancel(ts)); 34425c9bbc89SJed Brown 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 make negative to attempt recovery", TSConvergedReasons[ts->reason]); 34435c9bbc89SJed Brown SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_NOT_CONVERGED, "TSStep has failed due to %s", TSConvergedReasons[ts->reason]); 34445c9bbc89SJed Brown } 34453ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 344608c7845fSBarry Smith } 344708c7845fSBarry Smith 344808c7845fSBarry Smith /*@ 34497cbde773SLisandro Dalcin TSEvaluateWLTE - Evaluate the weighted local truncation error norm 34507cbde773SLisandro Dalcin at the end of a time step with a given order of accuracy. 34517cbde773SLisandro Dalcin 3452c3339decSBarry Smith Collective 34537cbde773SLisandro Dalcin 34544165533cSJose E. Roman Input Parameters: 34557cbde773SLisandro Dalcin + ts - time stepping context 3456bcf0153eSBarry Smith - wnormtype - norm type, either `NORM_2` or `NORM_INFINITY` 34577cbde773SLisandro Dalcin 345897bb3fdcSJose E. Roman Input/Output Parameter: 3459bcf0153eSBarry Smith . order - optional, desired order for the error evaluation or `PETSC_DECIDE`; 346097bb3fdcSJose E. Roman on output, the actual order of the error evaluation 346197bb3fdcSJose E. Roman 346297bb3fdcSJose E. Roman Output Parameter: 346397bb3fdcSJose E. Roman . wlte - the weighted local truncation error norm 34647cbde773SLisandro Dalcin 34657cbde773SLisandro Dalcin Level: advanced 34667cbde773SLisandro Dalcin 3467bcf0153eSBarry Smith Note: 34687cbde773SLisandro Dalcin If the timestepper cannot evaluate the error in a particular step 34697cbde773SLisandro Dalcin (eg. in the first step or restart steps after event handling), 34707cbde773SLisandro Dalcin this routine returns wlte=-1.0 . 34717cbde773SLisandro Dalcin 3472bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSStep()`, `TSAdapt`, `TSErrorWeightedNorm()` 34737cbde773SLisandro Dalcin @*/ 3474d71ae5a4SJacob Faibussowitsch PetscErrorCode TSEvaluateWLTE(TS ts, NormType wnormtype, PetscInt *order, PetscReal *wlte) 3475d71ae5a4SJacob Faibussowitsch { 34767cbde773SLisandro Dalcin PetscFunctionBegin; 34777cbde773SLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 34787cbde773SLisandro Dalcin PetscValidType(ts, 1); 3479064a246eSJacob Faibussowitsch PetscValidLogicalCollectiveEnum(ts, wnormtype, 2); 34807cbde773SLisandro Dalcin if (order) PetscValidIntPointer(order, 3); 34817cbde773SLisandro Dalcin if (order) PetscValidLogicalCollectiveInt(ts, *order, 3); 34827cbde773SLisandro Dalcin PetscValidRealPointer(wlte, 4); 34833c633725SBarry Smith PetscCheck(wnormtype == NORM_2 || wnormtype == NORM_INFINITY, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "No support for norm type %s", NormTypes[wnormtype]); 3484dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, evaluatewlte, wnormtype, order, wlte); 34853ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 34867cbde773SLisandro Dalcin } 34877cbde773SLisandro Dalcin 348805175c85SJed Brown /*@ 348905175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 349005175c85SJed Brown 3491c3339decSBarry Smith Collective 349205175c85SJed Brown 34934165533cSJose E. Roman Input Parameters: 34941c3436cfSJed Brown + ts - time stepping context 34951c3436cfSJed Brown . order - desired order of accuracy 3496*195e9b02SBarry Smith - done - whether the step was evaluated at this order (pass `NULL` to generate an error if not available) 349705175c85SJed Brown 34984165533cSJose E. Roman Output Parameter: 34990910c330SBarry Smith . U - state at the end of the current step 350005175c85SJed Brown 350105175c85SJed Brown Level: advanced 350205175c85SJed Brown 3503108c343cSJed Brown Notes: 3504108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3505108c343cSJed Brown 3506bcf0153eSBarry 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. 3507bcf0153eSBarry Smith 3508bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSStep()`, `TSAdapt` 350905175c85SJed Brown @*/ 3510d71ae5a4SJacob Faibussowitsch PetscErrorCode TSEvaluateStep(TS ts, PetscInt order, Vec U, PetscBool *done) 3511d71ae5a4SJacob Faibussowitsch { 351205175c85SJed Brown PetscFunctionBegin; 351305175c85SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 351405175c85SJed Brown PetscValidType(ts, 1); 35150910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 3516dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, evaluatestep, order, U, done); 35173ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 351805175c85SJed Brown } 351905175c85SJed Brown 3520aad739acSMatthew G. Knepley /*@C 35212e61be88SMatthew G. Knepley TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping. 3522aad739acSMatthew G. Knepley 3523aad739acSMatthew G. Knepley Not collective 3524aad739acSMatthew G. Knepley 35254165533cSJose E. Roman Input Parameter: 3526aad739acSMatthew G. Knepley . ts - time stepping context 3527aad739acSMatthew G. Knepley 35284165533cSJose E. Roman Output Parameter: 35292e61be88SMatthew G. Knepley . initConditions - The function which computes an initial condition 3530aad739acSMatthew G. Knepley 3531bcf0153eSBarry Smith The calling sequence for the function is 3532bcf0153eSBarry Smith .vb 3533bcf0153eSBarry Smith initCondition(TS ts, Vec u) 3534bcf0153eSBarry Smith ts - The timestepping context 3535bcf0153eSBarry Smith u - The input vector in which the initial condition is stored 3536bcf0153eSBarry Smith .ve 3537bcf0153eSBarry Smith 3538aad739acSMatthew G. Knepley Level: advanced 3539aad739acSMatthew G. Knepley 3540bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetComputeInitialCondition()`, `TSComputeInitialCondition()` 3541aad739acSMatthew G. Knepley @*/ 3542d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS, Vec)) 3543d71ae5a4SJacob Faibussowitsch { 3544aad739acSMatthew G. Knepley PetscFunctionBegin; 3545aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 35462e61be88SMatthew G. Knepley PetscValidPointer(initCondition, 2); 35472e61be88SMatthew G. Knepley *initCondition = ts->ops->initcondition; 35483ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3549aad739acSMatthew G. Knepley } 3550aad739acSMatthew G. Knepley 3551aad739acSMatthew G. Knepley /*@C 35522e61be88SMatthew G. Knepley TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping. 3553aad739acSMatthew G. Knepley 3554c3339decSBarry Smith Logically collective 3555aad739acSMatthew G. Knepley 35564165533cSJose E. Roman Input Parameters: 3557aad739acSMatthew G. Knepley + ts - time stepping context 35582e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition 3559aad739acSMatthew G. Knepley 3560a96d6ef6SBarry Smith Calling sequence for initCondition: 3561a96d6ef6SBarry Smith $ PetscErrorCode initCondition(TS ts, Vec u) 3562a96d6ef6SBarry Smith + ts - The timestepping context 3563a96d6ef6SBarry Smith - u - The input vector in which the initial condition is to be stored 3564aad739acSMatthew G. Knepley 3565bcf0153eSBarry Smith Level: advanced 3566bcf0153eSBarry Smith 3567bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetComputeInitialCondition()`, `TSComputeInitialCondition()` 3568aad739acSMatthew G. Knepley @*/ 3569d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS, Vec)) 3570d71ae5a4SJacob Faibussowitsch { 3571aad739acSMatthew G. Knepley PetscFunctionBegin; 3572aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 35732e61be88SMatthew G. Knepley PetscValidFunction(initCondition, 2); 35742e61be88SMatthew G. Knepley ts->ops->initcondition = initCondition; 35753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3576aad739acSMatthew G. Knepley } 3577aad739acSMatthew G. Knepley 3578aad739acSMatthew G. Knepley /*@ 3579bcf0153eSBarry Smith TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set with `TSSetComputeInitialCondition()` 3580aad739acSMatthew G. Knepley 3581c3339decSBarry Smith Collective 3582aad739acSMatthew G. Knepley 35834165533cSJose E. Roman Input Parameters: 3584aad739acSMatthew G. Knepley + ts - time stepping context 3585bcf0153eSBarry Smith - u - The `Vec` to store the condition in which will be used in `TSSolve()` 3586aad739acSMatthew G. Knepley 3587aad739acSMatthew G. Knepley Level: advanced 3588aad739acSMatthew G. Knepley 3589bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetComputeInitialCondition()`, `TSSetComputeInitialCondition()`, `TSSolve()` 3590aad739acSMatthew G. Knepley @*/ 3591d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeInitialCondition(TS ts, Vec u) 3592d71ae5a4SJacob Faibussowitsch { 3593aad739acSMatthew G. Knepley PetscFunctionBegin; 3594aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3595aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3596dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, initcondition, u); 35973ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3598aad739acSMatthew G. Knepley } 3599aad739acSMatthew G. Knepley 3600aad739acSMatthew G. Knepley /*@C 3601aad739acSMatthew G. Knepley TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping. 3602aad739acSMatthew G. Knepley 3603aad739acSMatthew G. Knepley Not collective 3604aad739acSMatthew G. Knepley 36054165533cSJose E. Roman Input Parameter: 3606aad739acSMatthew G. Knepley . ts - time stepping context 3607aad739acSMatthew G. Knepley 36084165533cSJose E. Roman Output Parameter: 3609aad739acSMatthew G. Knepley . exactError - The function which computes the solution error 3610aad739acSMatthew G. Knepley 3611a96d6ef6SBarry Smith Calling sequence for exactError: 3612a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u) 3613a96d6ef6SBarry Smith + ts - The timestepping context 3614a96d6ef6SBarry Smith . u - The approximate solution vector 3615a96d6ef6SBarry Smith - e - The input vector in which the error is stored 3616aad739acSMatthew G. Knepley 3617bcf0153eSBarry Smith Level: advanced 3618bcf0153eSBarry Smith 3619bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetComputeExactError()`, `TSComputeExactError()` 3620aad739acSMatthew G. Knepley @*/ 3621d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS, Vec, Vec)) 3622d71ae5a4SJacob Faibussowitsch { 3623aad739acSMatthew G. Knepley PetscFunctionBegin; 3624aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3625aad739acSMatthew G. Knepley PetscValidPointer(exactError, 2); 3626aad739acSMatthew G. Knepley *exactError = ts->ops->exacterror; 36273ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3628aad739acSMatthew G. Knepley } 3629aad739acSMatthew G. Knepley 3630aad739acSMatthew G. Knepley /*@C 3631aad739acSMatthew G. Knepley TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping. 3632aad739acSMatthew G. Knepley 3633c3339decSBarry Smith Logically collective 3634aad739acSMatthew G. Knepley 36354165533cSJose E. Roman Input Parameters: 3636aad739acSMatthew G. Knepley + ts - time stepping context 3637aad739acSMatthew G. Knepley - exactError - The function which computes the solution error 3638aad739acSMatthew G. Knepley 3639a96d6ef6SBarry Smith Calling sequence for exactError: 3640a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u) 3641a96d6ef6SBarry Smith + ts - The timestepping context 3642a96d6ef6SBarry Smith . u - The approximate solution vector 3643a96d6ef6SBarry Smith - e - The input vector in which the error is stored 3644aad739acSMatthew G. Knepley 3645bcf0153eSBarry Smith Level: advanced 3646bcf0153eSBarry Smith 3647bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetComputeExactError()`, `TSComputeExactError()` 3648aad739acSMatthew G. Knepley @*/ 3649d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS, Vec, Vec)) 3650d71ae5a4SJacob Faibussowitsch { 3651aad739acSMatthew G. Knepley PetscFunctionBegin; 3652aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3653f907fdbfSMatthew G. Knepley PetscValidFunction(exactError, 2); 3654aad739acSMatthew G. Knepley ts->ops->exacterror = exactError; 36553ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3656aad739acSMatthew G. Knepley } 3657aad739acSMatthew G. Knepley 3658aad739acSMatthew G. Knepley /*@ 3659bcf0153eSBarry Smith TSComputeExactError - Compute the solution error for the timestepping using the function previously set with `TSSetComputeExactError()` 3660aad739acSMatthew G. Knepley 3661c3339decSBarry Smith Collective 3662aad739acSMatthew G. Knepley 36634165533cSJose E. Roman Input Parameters: 3664aad739acSMatthew G. Knepley + ts - time stepping context 3665aad739acSMatthew G. Knepley . u - The approximate solution 3666bcf0153eSBarry Smith - e - The `Vec` used to store the error 3667aad739acSMatthew G. Knepley 3668aad739acSMatthew G. Knepley Level: advanced 3669aad739acSMatthew G. Knepley 3670bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetComputeInitialCondition()`, `TSSetComputeInitialCondition()`, `TSSolve()` 3671aad739acSMatthew G. Knepley @*/ 3672d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e) 3673d71ae5a4SJacob Faibussowitsch { 3674aad739acSMatthew G. Knepley PetscFunctionBegin; 3675aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3676aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3677aad739acSMatthew G. Knepley PetscValidHeaderSpecific(e, VEC_CLASSID, 3); 3678dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, exacterror, u, e); 36793ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3680aad739acSMatthew G. Knepley } 3681aad739acSMatthew G. Knepley 3682b1cb36f3SHong Zhang /*@ 36836a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 36846a4d4014SLisandro Dalcin 3685c3339decSBarry Smith Collective 36866a4d4014SLisandro Dalcin 3687d8d19677SJose E. Roman Input Parameters: 3688bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 3689bcf0153eSBarry Smith - u - the solution vector (can be null if `TSSetSolution()` was used and `TSSetExactFinalTime`(ts,`TS_EXACTFINALTIME_MATCHSTEP`) was not used, 369063e21af5SBarry Smith otherwise must contain the initial conditions and will contain the solution at the final requested time 36915a3a76d0SJed Brown 36926a4d4014SLisandro Dalcin Level: beginner 36936a4d4014SLisandro Dalcin 36945a3a76d0SJed Brown Notes: 36955a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 3696bcf0153eSBarry Smith held state accessible by `TSGetSolution()` and `TSGetTime()` because the method may have 36975a3a76d0SJed Brown stepped over the final time. 36985a3a76d0SJed Brown 3699bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSCreate()`, `TSSetSolution()`, `TSStep()`, `TSGetTime()`, `TSGetSolveTime()` 37006a4d4014SLisandro Dalcin @*/ 3701d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSolve(TS ts, Vec u) 3702d71ae5a4SJacob Faibussowitsch { 3703b06615a5SLisandro Dalcin Vec solution; 3704f22f69f0SBarry Smith 37056a4d4014SLisandro Dalcin PetscFunctionBegin; 37060700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3707f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3708303a5415SBarry Smith 37099566063dSJacob Faibussowitsch PetscCall(TSSetExactFinalTimeDefault(ts)); 3710ee41a567SStefano 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 */ 37110910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 37129566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u, &solution)); 37139566063dSJacob Faibussowitsch PetscCall(TSSetSolution(ts, solution)); 37149566063dSJacob Faibussowitsch PetscCall(VecDestroy(&solution)); /* grant ownership */ 37155a3a76d0SJed Brown } 37169566063dSJacob Faibussowitsch PetscCall(VecCopy(u, ts->vec_sol)); 37173c633725SBarry Smith PetscCheck(!ts->forward_solve, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE"); 37181baa6e33SBarry Smith } else if (u) PetscCall(TSSetSolution(ts, u)); 37199566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts)); 37209566063dSJacob Faibussowitsch PetscCall(TSTrajectorySetUp(ts->trajectory, ts)); 3721a6772fa2SLisandro Dalcin 37223c633725SBarry Smith PetscCheck(ts->max_time < PETSC_MAX_REAL || ts->max_steps != PETSC_MAX_INT, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "You must call TSSetMaxTime() or TSSetMaxSteps(), or use -ts_max_time <time> or -ts_max_steps <steps>"); 37233c633725SBarry 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()"); 37243c633725SBarry 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"); 37254a658b32SHong Zhang PetscCheck(!(ts->tspan && ts->exact_final_time != TS_EXACTFINALTIME_MATCHSTEP), PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "You must use TS_EXACTFINALTIME_MATCHSTEP when using time span"); 37264a658b32SHong Zhang 3727e1db57b0SHong Zhang if (ts->tspan && PetscIsCloseAtTol(ts->ptime, ts->tspan->span_times[0], ts->tspan->reltol * ts->time_step + ts->tspan->abstol, 0)) { /* starting point in time span */ 37284a658b32SHong Zhang PetscCall(VecCopy(ts->vec_sol, ts->tspan->vecs_sol[0])); 37294a658b32SHong Zhang ts->tspan->spanctr = 1; 37304a658b32SHong Zhang } 3731a6772fa2SLisandro Dalcin 37321baa6e33SBarry Smith if (ts->forward_solve) PetscCall(TSForwardSetUp(ts)); 3733715f1b00SHong Zhang 3734e7069c78SShri /* reset number of steps only when the step is not restarted. ARKIMEX 3735715f1b00SHong Zhang restarts the step after an event. Resetting these counters in such case causes 3736e7069c78SShri TSTrajectory to incorrectly save the output files 3737e7069c78SShri */ 3738715f1b00SHong Zhang /* reset time step and iteration counters */ 37392808aa04SLisandro Dalcin if (!ts->steps) { 37405ef26d82SJed Brown ts->ksp_its = 0; 37415ef26d82SJed Brown ts->snes_its = 0; 3742c610991cSLisandro Dalcin ts->num_snes_failures = 0; 3743c610991cSLisandro Dalcin ts->reject = 0; 37442808aa04SLisandro Dalcin ts->steprestart = PETSC_TRUE; 37452808aa04SLisandro Dalcin ts->steprollback = PETSC_FALSE; 37467d51462cSStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 37472808aa04SLisandro Dalcin } 3748e97c63d7SStefano Zampini 37494a658b32SHong Zhang /* make sure initial time step does not overshoot final time or the next point in tspan */ 3750e97c63d7SStefano Zampini if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) { 37514a658b32SHong Zhang PetscReal maxdt; 3752e97c63d7SStefano Zampini PetscReal dt = ts->time_step; 3753e97c63d7SStefano Zampini 37544a658b32SHong Zhang if (ts->tspan) maxdt = ts->tspan->span_times[ts->tspan->spanctr] - ts->ptime; 37554a658b32SHong Zhang else maxdt = ts->max_time - ts->ptime; 3756e97c63d7SStefano Zampini ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt, maxdt, 10 * PETSC_MACHINE_EPSILON, 0) ? maxdt : dt); 3757e97c63d7SStefano Zampini } 3758193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 3759193ac0bcSJed Brown 3760900f6b5bSMatthew G. Knepley { 3761900f6b5bSMatthew G. Knepley PetscViewer viewer; 3762900f6b5bSMatthew G. Knepley PetscViewerFormat format; 3763900f6b5bSMatthew G. Knepley PetscBool flg; 3764900f6b5bSMatthew G. Knepley static PetscBool incall = PETSC_FALSE; 3765900f6b5bSMatthew G. Knepley 3766900f6b5bSMatthew G. Knepley if (!incall) { 3767900f6b5bSMatthew G. Knepley /* Estimate the convergence rate of the time discretization */ 37689566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts), ((PetscObject)ts)->options, ((PetscObject)ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg)); 3769900f6b5bSMatthew G. Knepley if (flg) { 3770900f6b5bSMatthew G. Knepley PetscConvEst conv; 3771900f6b5bSMatthew G. Knepley DM dm; 3772900f6b5bSMatthew G. Knepley PetscReal *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */ 3773900f6b5bSMatthew G. Knepley PetscInt Nf; 3774f2ed2dc7SMatthew G. Knepley PetscBool checkTemporal = PETSC_TRUE; 3775900f6b5bSMatthew G. Knepley 3776900f6b5bSMatthew G. Knepley incall = PETSC_TRUE; 37779566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject)ts)->prefix, "-ts_convergence_temporal", &checkTemporal, &flg)); 37789566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 37799566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &Nf)); 37809566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(PetscMax(Nf, 1), &alpha)); 37819566063dSJacob Faibussowitsch PetscCall(PetscConvEstCreate(PetscObjectComm((PetscObject)ts), &conv)); 37829566063dSJacob Faibussowitsch PetscCall(PetscConvEstUseTS(conv, checkTemporal)); 37839566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetSolver(conv, (PetscObject)ts)); 37849566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetFromOptions(conv)); 37859566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetUp(conv)); 37869566063dSJacob Faibussowitsch PetscCall(PetscConvEstGetConvRate(conv, alpha)); 37879566063dSJacob Faibussowitsch PetscCall(PetscViewerPushFormat(viewer, format)); 37889566063dSJacob Faibussowitsch PetscCall(PetscConvEstRateView(conv, alpha, viewer)); 37899566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(viewer)); 37909566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&viewer)); 37919566063dSJacob Faibussowitsch PetscCall(PetscConvEstDestroy(&conv)); 37929566063dSJacob Faibussowitsch PetscCall(PetscFree(alpha)); 3793900f6b5bSMatthew G. Knepley incall = PETSC_FALSE; 3794900f6b5bSMatthew G. Knepley } 3795900f6b5bSMatthew G. Knepley } 3796900f6b5bSMatthew G. Knepley } 3797900f6b5bSMatthew G. Knepley 37989566063dSJacob Faibussowitsch PetscCall(TSViewFromOptions(ts, NULL, "-ts_view_pre")); 3799f05ece33SBarry Smith 3800193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 3801dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, solve); 38029566063dSJacob Faibussowitsch if (u) PetscCall(VecCopy(ts->vec_sol, u)); 3803cc708dedSBarry Smith ts->solvetime = ts->ptime; 3804a6772fa2SLisandro Dalcin solution = ts->vec_sol; 3805be5899b3SLisandro Dalcin } else { /* Step the requested number of timesteps. */ 3806db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3807db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3808e7069c78SShri 38092808aa04SLisandro Dalcin if (!ts->steps) { 38109566063dSJacob Faibussowitsch PetscCall(TSTrajectorySet(ts->trajectory, ts, ts->steps, ts->ptime, ts->vec_sol)); 38119566063dSJacob Faibussowitsch PetscCall(TSEventInitialize(ts->event, ts, ts->ptime, ts->vec_sol)); 38122808aa04SLisandro Dalcin } 38136427ac75SLisandro Dalcin 3814e1a7a14fSJed Brown while (!ts->reason) { 38159566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts, ts->steps, ts->ptime, ts->vec_sol)); 38161e66621cSBarry Smith if (!ts->steprollback) PetscCall(TSPreStep(ts)); 38179566063dSJacob Faibussowitsch PetscCall(TSStep(ts)); 38181baa6e33SBarry Smith if (ts->testjacobian) PetscCall(TSRHSJacobianTest(ts, NULL)); 38191baa6e33SBarry Smith if (ts->testjacobiantranspose) PetscCall(TSRHSJacobianTestTranspose(ts, NULL)); 3820cd4cee2dSHong Zhang if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */ 38217b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */ 38229566063dSJacob Faibussowitsch PetscCall(TSForwardCostIntegral(ts)); 38237b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps++; 3824b1cb36f3SHong Zhang } 382558818c2dSLisandro Dalcin if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */ 38267b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */ 38279566063dSJacob Faibussowitsch PetscCall(TSForwardStep(ts)); 38287b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps++; 3829715f1b00SHong Zhang } 38309566063dSJacob Faibussowitsch PetscCall(TSPostEvaluate(ts)); 38319566063dSJacob 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. */ 38321baa6e33SBarry Smith if (ts->steprollback) PetscCall(TSPostEvaluate(ts)); 3833e783b05fSHong Zhang if (!ts->steprollback) { 38349566063dSJacob Faibussowitsch PetscCall(TSTrajectorySet(ts->trajectory, ts, ts->steps, ts->ptime, ts->vec_sol)); 38359566063dSJacob Faibussowitsch PetscCall(TSPostStep(ts)); 3836aeb4809dSShri Abhyankar } 3837193ac0bcSJed Brown } 38389566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts, ts->steps, ts->ptime, ts->vec_sol)); 38396427ac75SLisandro Dalcin 384049354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 38419566063dSJacob Faibussowitsch PetscCall(TSInterpolate(ts, ts->max_time, u)); 3842cc708dedSBarry Smith ts->solvetime = ts->max_time; 3843b06615a5SLisandro Dalcin solution = u; 38449566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts, -1, ts->solvetime, solution)); 38450574a7fbSJed Brown } else { 38469566063dSJacob Faibussowitsch if (u) PetscCall(VecCopy(ts->vec_sol, u)); 3847cc708dedSBarry Smith ts->solvetime = ts->ptime; 3848b06615a5SLisandro Dalcin solution = ts->vec_sol; 38490574a7fbSJed Brown } 3850193ac0bcSJed Brown } 3851d2daff3dSHong Zhang 38529566063dSJacob Faibussowitsch PetscCall(TSViewFromOptions(ts, NULL, "-ts_view")); 38539566063dSJacob Faibussowitsch PetscCall(VecViewFromOptions(solution, (PetscObject)ts, "-ts_view_solution")); 38549566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsBlock((PetscObject)ts)); 38551baa6e33SBarry Smith if (ts->adjoint_solve) PetscCall(TSAdjointSolve(ts)); 38563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 38576a4d4014SLisandro Dalcin } 38586a4d4014SLisandro Dalcin 3859d763cef2SBarry Smith /*@ 3860b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 3861d763cef2SBarry Smith 3862d763cef2SBarry Smith Not Collective 3863d763cef2SBarry Smith 3864d763cef2SBarry Smith Input Parameter: 3865bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 3866d763cef2SBarry Smith 3867d763cef2SBarry Smith Output Parameter: 3868bcf0153eSBarry Smith . t - the current time. This time may not corresponds to the final time set with `TSSetMaxTime()`, use `TSGetSolveTime()`. 3869d763cef2SBarry Smith 3870d763cef2SBarry Smith Level: beginner 3871d763cef2SBarry Smith 3872b8123daeSJed Brown Note: 3873bcf0153eSBarry Smith When called during time step evaluation (e.g. during residual evaluation or via hooks set using `TSSetPreStep()`, 3874bcf0153eSBarry Smith `TSSetPreStage()`, `TSSetPostStage()`, or `TSSetPostStep()`), the time is the time at the start of the step being evaluated. 3875b8123daeSJed Brown 3876bcf0153eSBarry Smith .seealso: [](chapter_ts), TS`, ``TSGetSolveTime()`, `TSSetTime()`, `TSGetTimeStep()`, `TSGetStepNumber()` 3877d763cef2SBarry Smith @*/ 3878d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTime(TS ts, PetscReal *t) 3879d71ae5a4SJacob Faibussowitsch { 3880d763cef2SBarry Smith PetscFunctionBegin; 38810700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3882f7cf8827SBarry Smith PetscValidRealPointer(t, 2); 3883d763cef2SBarry Smith *t = ts->ptime; 38843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3885d763cef2SBarry Smith } 3886d763cef2SBarry Smith 3887e5e524a1SHong Zhang /*@ 3888e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 3889e5e524a1SHong Zhang 3890e5e524a1SHong Zhang Not Collective 3891e5e524a1SHong Zhang 3892e5e524a1SHong Zhang Input Parameter: 3893bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 3894e5e524a1SHong Zhang 3895e5e524a1SHong Zhang Output Parameter: 3896e5e524a1SHong Zhang . t - the previous time 3897e5e524a1SHong Zhang 3898e5e524a1SHong Zhang Level: beginner 3899e5e524a1SHong Zhang 3900bcf0153eSBarry Smith .seealso: [](chapter_ts), TS`, ``TSGetTime()`, `TSGetSolveTime()`, `TSGetTimeStep()` 3901e5e524a1SHong Zhang @*/ 3902d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetPrevTime(TS ts, PetscReal *t) 3903d71ae5a4SJacob Faibussowitsch { 3904e5e524a1SHong Zhang PetscFunctionBegin; 3905e5e524a1SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3906e5e524a1SHong Zhang PetscValidRealPointer(t, 2); 3907e5e524a1SHong Zhang *t = ts->ptime_prev; 39083ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3909e5e524a1SHong Zhang } 3910e5e524a1SHong Zhang 39116a4d4014SLisandro Dalcin /*@ 39126a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 39136a4d4014SLisandro Dalcin 3914c3339decSBarry Smith Logically Collective 39156a4d4014SLisandro Dalcin 39166a4d4014SLisandro Dalcin Input Parameters: 3917bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 39186a4d4014SLisandro Dalcin - time - the time 39196a4d4014SLisandro Dalcin 39206a4d4014SLisandro Dalcin Level: intermediate 39216a4d4014SLisandro Dalcin 3922bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetTime()`, `TSSetMaxSteps()` 39236a4d4014SLisandro Dalcin @*/ 3924d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTime(TS ts, PetscReal t) 3925d71ae5a4SJacob Faibussowitsch { 39266a4d4014SLisandro Dalcin PetscFunctionBegin; 39270700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3928c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts, t, 2); 39296a4d4014SLisandro Dalcin ts->ptime = t; 39303ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 39316a4d4014SLisandro Dalcin } 39326a4d4014SLisandro Dalcin 3933d763cef2SBarry Smith /*@C 3934d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 3935d763cef2SBarry Smith TS options in the database. 3936d763cef2SBarry Smith 3937c3339decSBarry Smith Logically Collective 3938d763cef2SBarry Smith 3939d8d19677SJose E. Roman Input Parameters: 3940bcf0153eSBarry Smith + ts - The `TS` context 3941d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3942d763cef2SBarry Smith 3943bcf0153eSBarry Smith Level: advanced 3944bcf0153eSBarry Smith 3945bcf0153eSBarry Smith Note: 3946d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3947d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3948d763cef2SBarry Smith hyphen. 3949d763cef2SBarry Smith 3950bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetFromOptions()`, `TSAppendOptionsPrefix()` 3951d763cef2SBarry Smith @*/ 3952d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetOptionsPrefix(TS ts, const char prefix[]) 3953d71ae5a4SJacob Faibussowitsch { 3954089b2837SJed Brown SNES snes; 3955d763cef2SBarry Smith 3956d763cef2SBarry Smith PetscFunctionBegin; 39570700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 39589566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)ts, prefix)); 39599566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 39609566063dSJacob Faibussowitsch PetscCall(SNESSetOptionsPrefix(snes, prefix)); 39613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3962d763cef2SBarry Smith } 3963d763cef2SBarry Smith 3964d763cef2SBarry Smith /*@C 3965d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 3966d763cef2SBarry Smith TS options in the database. 3967d763cef2SBarry Smith 3968c3339decSBarry Smith Logically Collective 3969d763cef2SBarry Smith 3970d8d19677SJose E. Roman Input Parameters: 3971bcf0153eSBarry Smith + ts - The `TS` context 3972d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3973d763cef2SBarry Smith 3974bcf0153eSBarry Smith Level: advanced 3975bcf0153eSBarry Smith 3976bcf0153eSBarry Smith Note: 3977d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3978d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3979d763cef2SBarry Smith hyphen. 3980d763cef2SBarry Smith 3981bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetOptionsPrefix()`, `TSSetOptionsPrefix()`, `TSSetFromOptions()` 3982d763cef2SBarry Smith @*/ 3983d71ae5a4SJacob Faibussowitsch PetscErrorCode TSAppendOptionsPrefix(TS ts, const char prefix[]) 3984d71ae5a4SJacob Faibussowitsch { 3985089b2837SJed Brown SNES snes; 3986d763cef2SBarry Smith 3987d763cef2SBarry Smith PetscFunctionBegin; 39880700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 39899566063dSJacob Faibussowitsch PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)ts, prefix)); 39909566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 39919566063dSJacob Faibussowitsch PetscCall(SNESAppendOptionsPrefix(snes, prefix)); 39923ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3993d763cef2SBarry Smith } 3994d763cef2SBarry Smith 3995d763cef2SBarry Smith /*@C 3996d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 3997bcf0153eSBarry Smith `TS` options in the database. 3998d763cef2SBarry Smith 3999d763cef2SBarry Smith Not Collective 4000d763cef2SBarry Smith 4001d763cef2SBarry Smith Input Parameter: 4002bcf0153eSBarry Smith . ts - The `TS` context 4003d763cef2SBarry Smith 4004d763cef2SBarry Smith Output Parameter: 4005d763cef2SBarry Smith . prefix - A pointer to the prefix string used 4006d763cef2SBarry Smith 4007d763cef2SBarry Smith Level: intermediate 4008d763cef2SBarry Smith 4009bcf0153eSBarry Smith Fortran Note: 4010*195e9b02SBarry Smith The user should pass in a string 'prefix' of 4011bcf0153eSBarry Smith sufficient length to hold the prefix. 4012bcf0153eSBarry Smith 4013bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSAppendOptionsPrefix()`, `TSSetFromOptions()` 4014d763cef2SBarry Smith @*/ 4015d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetOptionsPrefix(TS ts, const char *prefix[]) 4016d71ae5a4SJacob Faibussowitsch { 4017d763cef2SBarry Smith PetscFunctionBegin; 40180700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 40194482741eSBarry Smith PetscValidPointer(prefix, 2); 40209566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)ts, prefix)); 40213ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4022d763cef2SBarry Smith } 4023d763cef2SBarry Smith 4024d763cef2SBarry Smith /*@C 4025d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4026d763cef2SBarry Smith 4027bcf0153eSBarry Smith Not Collective, but parallel objects are returned if ts is parallel 4028d763cef2SBarry Smith 4029d763cef2SBarry Smith Input Parameter: 4030bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 4031d763cef2SBarry Smith 4032d763cef2SBarry Smith Output Parameters: 4033*195e9b02SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or `NULL`) 4034*195e9b02SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as `Amat` (or `NULL`) 4035*195e9b02SBarry Smith . func - Function to compute the Jacobian of the RHS (or `NULL`) 4036*195e9b02SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or `NULL`) 4037d763cef2SBarry Smith 4038d763cef2SBarry Smith Level: intermediate 4039d763cef2SBarry Smith 4040bcf0153eSBarry Smith Note: 4041*195e9b02SBarry Smith You can pass in `NULL` for any return argument you do not need. 4042bcf0153eSBarry Smith 4043bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetTimeStep()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()` 4044d763cef2SBarry Smith 4045d763cef2SBarry Smith @*/ 4046d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetRHSJacobian(TS ts, Mat *Amat, Mat *Pmat, TSRHSJacobian *func, void **ctx) 4047d71ae5a4SJacob Faibussowitsch { 404824989b8cSPeter Brune DM dm; 4049089b2837SJed Brown 4050d763cef2SBarry Smith PetscFunctionBegin; 405123a57915SBarry Smith if (Amat || Pmat) { 405223a57915SBarry Smith SNES snes; 40539566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 40549566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 40559566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes, Amat, Pmat, NULL, NULL)); 405623a57915SBarry Smith } 40579566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 40589566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, func, ctx)); 40593ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4060d763cef2SBarry Smith } 4061d763cef2SBarry Smith 40622eca1d9cSJed Brown /*@C 40632eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 40642eca1d9cSJed Brown 4065bcf0153eSBarry Smith Not Collective, but parallel objects are returned if ts is parallel 40662eca1d9cSJed Brown 40672eca1d9cSJed Brown Input Parameter: 4068bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 40692eca1d9cSJed Brown 40702eca1d9cSJed Brown Output Parameters: 4071e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4072*195e9b02SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as `Amat` 40732eca1d9cSJed Brown . f - The function to compute the matrices 40742eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 40752eca1d9cSJed Brown 40762eca1d9cSJed Brown Level: advanced 40772eca1d9cSJed Brown 4078bcf0153eSBarry Smith Note: 4079*195e9b02SBarry Smith You can pass in `NULL` for any return argument you do not need. 4080bcf0153eSBarry Smith 4081bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetTimeStep()`, `TSGetRHSJacobian()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()` 40822eca1d9cSJed Brown @*/ 4083d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetIJacobian(TS ts, Mat *Amat, Mat *Pmat, TSIJacobian *f, void **ctx) 4084d71ae5a4SJacob Faibussowitsch { 408524989b8cSPeter Brune DM dm; 40860910c330SBarry Smith 40872eca1d9cSJed Brown PetscFunctionBegin; 4088c0aab802Sstefano_zampini if (Amat || Pmat) { 4089c0aab802Sstefano_zampini SNES snes; 40909566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 40919566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 40929566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes, Amat, Pmat, NULL, NULL)); 4093c0aab802Sstefano_zampini } 40949566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 40959566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm, f, ctx)); 40963ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 40972eca1d9cSJed Brown } 40982eca1d9cSJed Brown 4099af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 41006c699258SBarry Smith /*@ 4101bcf0153eSBarry Smith TSSetDM - Sets the `DM` that may be used by some nonlinear solvers or preconditioners under the `TS` 41026c699258SBarry Smith 4103c3339decSBarry Smith Logically Collective 41046c699258SBarry Smith 41056c699258SBarry Smith Input Parameters: 4106bcf0153eSBarry Smith + ts - the `TS` integrator object 4107*195e9b02SBarry Smith - dm - the dm, cannot be `NULL` 41086c699258SBarry Smith 41096c699258SBarry Smith Level: intermediate 41106c699258SBarry Smith 4111bcf0153eSBarry Smith Notes: 4112bcf0153eSBarry Smith A `DM` can only be used for solving one problem at a time because information about the problem is stored on the `DM`, 4113bcf0153eSBarry Smith even when not using interfaces like `DMTSSetIFunction()`. Use `DMClone()` to get a distinct `DM` when solving 4114bcf0153eSBarry Smith different problems using the same function space. 4115bcf0153eSBarry Smith 4116bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `DM`, `TSGetDM()`, `SNESSetDM()`, `SNESGetDM()` 41176c699258SBarry Smith @*/ 4118d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetDM(TS ts, DM dm) 4119d71ae5a4SJacob Faibussowitsch { 4120089b2837SJed Brown SNES snes; 4121942e3340SBarry Smith DMTS tsdm; 41226c699258SBarry Smith 41236c699258SBarry Smith PetscFunctionBegin; 41240700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 41252a808120SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 2); 41269566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)dm)); 4127942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 41282a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 41299566063dSJacob Faibussowitsch PetscCall(DMCopyDMTS(ts->dm, dm)); 41309566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm, &tsdm)); 41311e66621cSBarry Smith /* Grant write privileges to the replacement DM */ 41321e66621cSBarry Smith if (tsdm->originaldm == ts->dm) tsdm->originaldm = dm; 413324989b8cSPeter Brune } 41349566063dSJacob Faibussowitsch PetscCall(DMDestroy(&ts->dm)); 413524989b8cSPeter Brune } 41366c699258SBarry Smith ts->dm = dm; 4137bbd56ea5SKarl Rupp 41389566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 41399566063dSJacob Faibussowitsch PetscCall(SNESSetDM(snes, dm)); 41403ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 41416c699258SBarry Smith } 41426c699258SBarry Smith 41436c699258SBarry Smith /*@ 4144bcf0153eSBarry Smith TSGetDM - Gets the `DM` that may be used by some preconditioners 41456c699258SBarry Smith 41463f9fe445SBarry Smith Not Collective 41476c699258SBarry Smith 41486c699258SBarry Smith Input Parameter: 4149bcf0153eSBarry Smith . ts - the `TS` 41506c699258SBarry Smith 41516c699258SBarry Smith Output Parameter: 4152*195e9b02SBarry Smith . dm - the `DM` 41536c699258SBarry Smith 41546c699258SBarry Smith Level: intermediate 41556c699258SBarry Smith 4156bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `DM`, `TSSetDM()`, `SNESSetDM()`, `SNESGetDM()` 41576c699258SBarry Smith @*/ 4158d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetDM(TS ts, DM *dm) 4159d71ae5a4SJacob Faibussowitsch { 41606c699258SBarry Smith PetscFunctionBegin; 41610700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4162496e6a7aSJed Brown if (!ts->dm) { 41639566063dSJacob Faibussowitsch PetscCall(DMShellCreate(PetscObjectComm((PetscObject)ts), &ts->dm)); 41649566063dSJacob Faibussowitsch if (ts->snes) PetscCall(SNESSetDM(ts->snes, ts->dm)); 4165496e6a7aSJed Brown } 41666c699258SBarry Smith *dm = ts->dm; 41673ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 41686c699258SBarry Smith } 41691713a123SBarry Smith 41700f5c6efeSJed Brown /*@ 41710f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 41720f5c6efeSJed Brown 4173c3339decSBarry Smith Logically Collective 41740f5c6efeSJed Brown 4175d8d19677SJose E. Roman Input Parameters: 4176d42a1c89SJed Brown + snes - nonlinear solver 41770910c330SBarry Smith . U - the current state at which to evaluate the residual 4178d42a1c89SJed Brown - ctx - user context, must be a TS 41790f5c6efeSJed Brown 41800f5c6efeSJed Brown Output Parameter: 41810f5c6efeSJed Brown . F - the nonlinear residual 41820f5c6efeSJed Brown 41830f5c6efeSJed Brown Level: advanced 41840f5c6efeSJed Brown 4185bcf0153eSBarry Smith Note: 4186bcf0153eSBarry Smith This function is not normally called by users and is automatically registered with the `SNES` used by `TS`. 4187bcf0153eSBarry Smith It is most frequently passed to `MatFDColoringSetFunction()`. 4188bcf0153eSBarry Smith 4189bcf0153eSBarry Smith .seealso: [](chapter_ts), `SNESSetFunction()`, `MatFDColoringSetFunction()` 41900f5c6efeSJed Brown @*/ 4191d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESTSFormFunction(SNES snes, Vec U, Vec F, void *ctx) 4192d71ae5a4SJacob Faibussowitsch { 41930f5c6efeSJed Brown TS ts = (TS)ctx; 41940f5c6efeSJed Brown 41950f5c6efeSJed Brown PetscFunctionBegin; 41960f5c6efeSJed Brown PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 41970910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 2); 41980f5c6efeSJed Brown PetscValidHeaderSpecific(F, VEC_CLASSID, 3); 41990f5c6efeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 4); 42009566063dSJacob Faibussowitsch PetscCall((ts->ops->snesfunction)(snes, U, F, ts)); 42013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 42020f5c6efeSJed Brown } 42030f5c6efeSJed Brown 42040f5c6efeSJed Brown /*@ 42050f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 42060f5c6efeSJed Brown 4207c3339decSBarry Smith Collective 42080f5c6efeSJed Brown 4209d8d19677SJose E. Roman Input Parameters: 42100f5c6efeSJed Brown + snes - nonlinear solver 42110910c330SBarry Smith . U - the current state at which to evaluate the residual 4212bcf0153eSBarry Smith - ctx - user context, must be a `TS` 42130f5c6efeSJed Brown 4214d8d19677SJose E. Roman Output Parameters: 42150f5c6efeSJed Brown + A - the Jacobian 42166b867d5aSJose E. Roman - B - the preconditioning matrix (may be the same as A) 42170f5c6efeSJed Brown 42180f5c6efeSJed Brown Level: developer 42190f5c6efeSJed Brown 4220bcf0153eSBarry Smith Note: 4221bcf0153eSBarry Smith This function is not normally called by users and is automatically registered with the `SNES` used by `TS`. 4222bcf0153eSBarry Smith 4223bcf0153eSBarry Smith .seealso: [](chapter_ts), `SNESSetJacobian()` 42240f5c6efeSJed Brown @*/ 4225d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESTSFormJacobian(SNES snes, Vec U, Mat A, Mat B, void *ctx) 4226d71ae5a4SJacob Faibussowitsch { 42270f5c6efeSJed Brown TS ts = (TS)ctx; 42280f5c6efeSJed Brown 42290f5c6efeSJed Brown PetscFunctionBegin; 42300f5c6efeSJed Brown PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 42310910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 2); 42320f5c6efeSJed Brown PetscValidPointer(A, 3); 423394ab13aaSBarry Smith PetscValidHeaderSpecific(A, MAT_CLASSID, 3); 42340f5c6efeSJed Brown PetscValidPointer(B, 4); 423594ab13aaSBarry Smith PetscValidHeaderSpecific(B, MAT_CLASSID, 4); 4236064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(ts, TS_CLASSID, 5); 42379566063dSJacob Faibussowitsch PetscCall((ts->ops->snesjacobian)(snes, U, A, B, ts)); 42383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 42390f5c6efeSJed Brown } 4240325fc9f4SBarry Smith 42410e4ef248SJed Brown /*@C 42429ae8fd06SBarry Smith TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only 42430e4ef248SJed Brown 4244c3339decSBarry Smith Collective 42450e4ef248SJed Brown 42464165533cSJose E. Roman Input Parameters: 42470e4ef248SJed Brown + ts - time stepping context 42480e4ef248SJed Brown . t - time at which to evaluate 42490910c330SBarry Smith . U - state at which to evaluate 42500e4ef248SJed Brown - ctx - context 42510e4ef248SJed Brown 42524165533cSJose E. Roman Output Parameter: 42530e4ef248SJed Brown . F - right hand side 42540e4ef248SJed Brown 42550e4ef248SJed Brown Level: intermediate 42560e4ef248SJed Brown 4257bcf0153eSBarry Smith Note: 4258bcf0153eSBarry Smith This function is intended to be passed to `TSSetRHSFunction()` to evaluate the right hand side for linear problems. 4259bcf0153eSBarry Smith The matrix (and optionally the evaluation context) should be passed to `TSSetRHSJacobian()`. 42600e4ef248SJed Brown 4261bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetRHSFunction()`, `TSSetRHSJacobian()`, `TSComputeRHSJacobianConstant()` 42620e4ef248SJed Brown @*/ 4263d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSFunctionLinear(TS ts, PetscReal t, Vec U, Vec F, void *ctx) 4264d71ae5a4SJacob Faibussowitsch { 42650e4ef248SJed Brown Mat Arhs, Brhs; 42660e4ef248SJed Brown 42670e4ef248SJed Brown PetscFunctionBegin; 42689566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts, &Arhs, &Brhs)); 42692663174eSHong Zhang /* undo the damage caused by shifting */ 42709566063dSJacob Faibussowitsch PetscCall(TSRecoverRHSJacobian(ts, Arhs, Brhs)); 42719566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts, t, U, Arhs, Brhs)); 42729566063dSJacob Faibussowitsch PetscCall(MatMult(Arhs, U, F)); 42733ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 42740e4ef248SJed Brown } 42750e4ef248SJed Brown 42760e4ef248SJed Brown /*@C 42770e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 42780e4ef248SJed Brown 4279c3339decSBarry Smith Collective 42800e4ef248SJed Brown 42814165533cSJose E. Roman Input Parameters: 42820e4ef248SJed Brown + ts - time stepping context 42830e4ef248SJed Brown . t - time at which to evaluate 42840910c330SBarry Smith . U - state at which to evaluate 42850e4ef248SJed Brown - ctx - context 42860e4ef248SJed Brown 42874165533cSJose E. Roman Output Parameters: 42880e4ef248SJed Brown + A - pointer to operator 428997bb3fdcSJose E. Roman - B - pointer to preconditioning matrix 42900e4ef248SJed Brown 42910e4ef248SJed Brown Level: intermediate 42920e4ef248SJed Brown 4293bcf0153eSBarry Smith Note: 4294bcf0153eSBarry Smith This function is intended to be passed to `TSSetRHSJacobian()` to evaluate the Jacobian for linear time-independent problems. 42950e4ef248SJed Brown 4296bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetRHSFunction()`, `TSSetRHSJacobian()`, `TSComputeRHSFunctionLinear()` 42970e4ef248SJed Brown @*/ 4298d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSJacobianConstant(TS ts, PetscReal t, Vec U, Mat A, Mat B, void *ctx) 4299d71ae5a4SJacob Faibussowitsch { 43000e4ef248SJed Brown PetscFunctionBegin; 43013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 43020e4ef248SJed Brown } 43030e4ef248SJed Brown 43040026cea9SSean Farley /*@C 43050026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 43060026cea9SSean Farley 4307c3339decSBarry Smith Collective 43080026cea9SSean Farley 43094165533cSJose E. Roman Input Parameters: 43100026cea9SSean Farley + ts - time stepping context 43110026cea9SSean Farley . t - time at which to evaluate 43120910c330SBarry Smith . U - state at which to evaluate 43130910c330SBarry Smith . Udot - time derivative of state vector 43140026cea9SSean Farley - ctx - context 43150026cea9SSean Farley 43164165533cSJose E. Roman Output Parameter: 43170026cea9SSean Farley . F - left hand side 43180026cea9SSean Farley 43190026cea9SSean Farley Level: intermediate 43200026cea9SSean Farley 43210026cea9SSean Farley Notes: 43220910c330SBarry 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 4323bcf0153eSBarry Smith user is required to write their own `TSComputeIFunction()`. 4324bcf0153eSBarry Smith This function is intended to be passed to `TSSetIFunction()` to evaluate the left hand side for linear problems. 4325bcf0153eSBarry Smith The matrix (and optionally the evaluation context) should be passed to `TSSetIJacobian()`. 43260026cea9SSean Farley 4327bcf0153eSBarry Smith Note that using this function is NOT equivalent to using `TSComputeRHSFunctionLinear()` since that solves Udot = A U 43289ae8fd06SBarry Smith 4329bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetIFunction()`, `TSSetIJacobian()`, `TSComputeIJacobianConstant()`, `TSComputeRHSFunctionLinear()` 43300026cea9SSean Farley @*/ 4331d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIFunctionLinear(TS ts, PetscReal t, Vec U, Vec Udot, Vec F, void *ctx) 4332d71ae5a4SJacob Faibussowitsch { 43330026cea9SSean Farley Mat A, B; 43340026cea9SSean Farley 43350026cea9SSean Farley PetscFunctionBegin; 43369566063dSJacob Faibussowitsch PetscCall(TSGetIJacobian(ts, &A, &B, NULL, NULL)); 43379566063dSJacob Faibussowitsch PetscCall(TSComputeIJacobian(ts, t, U, Udot, 1.0, A, B, PETSC_TRUE)); 43389566063dSJacob Faibussowitsch PetscCall(MatMult(A, Udot, F)); 43393ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 43400026cea9SSean Farley } 43410026cea9SSean Farley 43420026cea9SSean Farley /*@C 4343b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 43440026cea9SSean Farley 4345c3339decSBarry Smith Collective 43460026cea9SSean Farley 43474165533cSJose E. Roman Input Parameters: 43480026cea9SSean Farley + ts - time stepping context 43490026cea9SSean Farley . t - time at which to evaluate 43500910c330SBarry Smith . U - state at which to evaluate 43510910c330SBarry Smith . Udot - time derivative of state vector 43520026cea9SSean Farley . shift - shift to apply 43530026cea9SSean Farley - ctx - context 43540026cea9SSean Farley 43554165533cSJose E. Roman Output Parameters: 43560026cea9SSean Farley + A - pointer to operator 435797bb3fdcSJose E. Roman - B - pointer to preconditioning matrix 43580026cea9SSean Farley 4359b41af12eSJed Brown Level: advanced 43600026cea9SSean Farley 43610026cea9SSean Farley Notes: 4362bcf0153eSBarry Smith This function is intended to be passed to `TSSetIJacobian()` to evaluate the Jacobian for linear time-independent problems. 43630026cea9SSean Farley 4364b41af12eSJed Brown It is only appropriate for problems of the form 4365b41af12eSJed Brown 4366b41af12eSJed Brown $ M Udot = F(U,t) 4367b41af12eSJed Brown 4368bcf0153eSBarry Smith where M is constant and F is non-stiff. The user must pass M to `TSSetIJacobian()`. The current implementation only 4369bcf0153eSBarry Smith works with IMEX time integration methods such as `TSROSW` and `TSARKIMEX`, since there is no support for de-constructing 4370b41af12eSJed Brown an implicit operator of the form 4371b41af12eSJed Brown 4372b41af12eSJed Brown $ shift*M + J 4373b41af12eSJed Brown 4374b41af12eSJed 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 4375b41af12eSJed Brown a copy of M or reassemble it when requested. 4376b41af12eSJed Brown 4377bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSROSW`, `TSARKIMEX`, `TSSetIFunction()`, `TSSetIJacobian()`, `TSComputeIFunctionLinear()` 43780026cea9SSean Farley @*/ 4379d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIJacobianConstant(TS ts, PetscReal t, Vec U, Vec Udot, PetscReal shift, Mat A, Mat B, void *ctx) 4380d71ae5a4SJacob Faibussowitsch { 43810026cea9SSean Farley PetscFunctionBegin; 43829566063dSJacob Faibussowitsch PetscCall(MatScale(A, shift / ts->ijacobian.shift)); 4383b41af12eSJed Brown ts->ijacobian.shift = shift; 43843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 43850026cea9SSean Farley } 4386b41af12eSJed Brown 4387e817cc15SEmil Constantinescu /*@ 4388bcf0153eSBarry Smith TSGetEquationType - Gets the type of the equation that `TS` is solving. 4389e817cc15SEmil Constantinescu 4390e817cc15SEmil Constantinescu Not Collective 4391e817cc15SEmil Constantinescu 4392e817cc15SEmil Constantinescu Input Parameter: 4393bcf0153eSBarry Smith . ts - the `TS` context 4394e817cc15SEmil Constantinescu 4395e817cc15SEmil Constantinescu Output Parameter: 4396bcf0153eSBarry Smith . equation_type - see `TSEquationType` 4397e817cc15SEmil Constantinescu 4398e817cc15SEmil Constantinescu Level: beginner 4399e817cc15SEmil Constantinescu 4400bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetEquationType()`, `TSEquationType` 4401e817cc15SEmil Constantinescu @*/ 4402d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetEquationType(TS ts, TSEquationType *equation_type) 4403d71ae5a4SJacob Faibussowitsch { 4404e817cc15SEmil Constantinescu PetscFunctionBegin; 4405e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4406e817cc15SEmil Constantinescu PetscValidPointer(equation_type, 2); 4407e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 44083ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4409e817cc15SEmil Constantinescu } 4410e817cc15SEmil Constantinescu 4411e817cc15SEmil Constantinescu /*@ 4412bcf0153eSBarry Smith TSSetEquationType - Sets the type of the equation that `TS` is solving. 4413e817cc15SEmil Constantinescu 4414e817cc15SEmil Constantinescu Not Collective 4415e817cc15SEmil Constantinescu 4416d8d19677SJose E. Roman Input Parameters: 4417bcf0153eSBarry Smith + ts - the `TS` context 4418bcf0153eSBarry Smith - equation_type - see `TSEquationType` 4419e817cc15SEmil Constantinescu 4420e817cc15SEmil Constantinescu Level: advanced 4421e817cc15SEmil Constantinescu 4422bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetEquationType()`, `TSEquationType` 4423e817cc15SEmil Constantinescu @*/ 4424d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetEquationType(TS ts, TSEquationType equation_type) 4425d71ae5a4SJacob Faibussowitsch { 4426e817cc15SEmil Constantinescu PetscFunctionBegin; 4427e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4428e817cc15SEmil Constantinescu ts->equation_type = equation_type; 44293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4430e817cc15SEmil Constantinescu } 44310026cea9SSean Farley 44324af1b03aSJed Brown /*@ 4433bcf0153eSBarry Smith TSGetConvergedReason - Gets the reason the `TS` iteration was stopped. 44344af1b03aSJed Brown 44354af1b03aSJed Brown Not Collective 44364af1b03aSJed Brown 44374af1b03aSJed Brown Input Parameter: 4438bcf0153eSBarry Smith . ts - the `TS` context 44394af1b03aSJed Brown 44404af1b03aSJed Brown Output Parameter: 4441bcf0153eSBarry Smith . reason - negative value indicates diverged, positive value converged, see `TSConvergedReason` or the 44424af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 44434af1b03aSJed Brown 4444487e0bb9SJed Brown Level: beginner 44454af1b03aSJed Brown 4446bcf0153eSBarry Smith Note: 4447bcf0153eSBarry Smith Can only be called after the call to `TSSolve()` is complete. 44484af1b03aSJed Brown 4449bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSolve()`, `TSSetConvergenceTest()`, `TSConvergedReason` 44504af1b03aSJed Brown @*/ 4451d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetConvergedReason(TS ts, TSConvergedReason *reason) 4452d71ae5a4SJacob Faibussowitsch { 44534af1b03aSJed Brown PetscFunctionBegin; 44544af1b03aSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 44554af1b03aSJed Brown PetscValidPointer(reason, 2); 44564af1b03aSJed Brown *reason = ts->reason; 44573ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 44584af1b03aSJed Brown } 44594af1b03aSJed Brown 4460d6ad946cSShri Abhyankar /*@ 4461bcf0153eSBarry Smith TSSetConvergedReason - Sets the reason for handling the convergence of `TSSolve()`. 4462d6ad946cSShri Abhyankar 44636b221cbeSPatrick Sanan Logically Collective; reason must contain common value 4464d6ad946cSShri Abhyankar 44656b221cbeSPatrick Sanan Input Parameters: 4466bcf0153eSBarry Smith + ts - the `TS` context 4467bcf0153eSBarry Smith - reason - negative value indicates diverged, positive value converged, see `TSConvergedReason` or the 4468d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 4469d6ad946cSShri Abhyankar 4470f5abba47SShri Abhyankar Level: advanced 4471d6ad946cSShri Abhyankar 4472bcf0153eSBarry Smith Note: 4473bcf0153eSBarry Smith Can only be called while `TSSolve()` is active. 4474d6ad946cSShri Abhyankar 4475bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSolve()`, `TSConvergedReason` 4476d6ad946cSShri Abhyankar @*/ 4477d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetConvergedReason(TS ts, TSConvergedReason reason) 4478d71ae5a4SJacob Faibussowitsch { 4479d6ad946cSShri Abhyankar PetscFunctionBegin; 4480d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4481d6ad946cSShri Abhyankar ts->reason = reason; 44823ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4483d6ad946cSShri Abhyankar } 4484d6ad946cSShri Abhyankar 4485cc708dedSBarry Smith /*@ 4486bcf0153eSBarry Smith TSGetSolveTime - Gets the time after a call to `TSSolve()` 4487cc708dedSBarry Smith 4488cc708dedSBarry Smith Not Collective 4489cc708dedSBarry Smith 4490cc708dedSBarry Smith Input Parameter: 4491bcf0153eSBarry Smith . ts - the `TS` context 4492cc708dedSBarry Smith 4493cc708dedSBarry Smith Output Parameter: 4494bcf0153eSBarry Smith . ftime - the final time. This time corresponds to the final time set with `TSSetMaxTime()` 4495cc708dedSBarry Smith 4496487e0bb9SJed Brown Level: beginner 4497cc708dedSBarry Smith 4498bcf0153eSBarry Smith Note: 4499bcf0153eSBarry Smith Can only be called after the call to `TSSolve()` is complete. 4500cc708dedSBarry Smith 4501bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSolve()`, `TSSetConvergenceTest()`, `TSConvergedReason` 4502cc708dedSBarry Smith @*/ 4503d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSolveTime(TS ts, PetscReal *ftime) 4504d71ae5a4SJacob Faibussowitsch { 4505cc708dedSBarry Smith PetscFunctionBegin; 4506cc708dedSBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4507dadcf809SJacob Faibussowitsch PetscValidRealPointer(ftime, 2); 4508cc708dedSBarry Smith *ftime = ts->solvetime; 45093ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4510cc708dedSBarry Smith } 4511cc708dedSBarry Smith 45122c18e0fdSBarry Smith /*@ 45135ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 45149f67acb7SJed Brown used by the time integrator. 45159f67acb7SJed Brown 45169f67acb7SJed Brown Not Collective 45179f67acb7SJed Brown 45189f67acb7SJed Brown Input Parameter: 4519bcf0153eSBarry Smith . ts - `TS` context 45209f67acb7SJed Brown 45219f67acb7SJed Brown Output Parameter: 45229f67acb7SJed Brown . nits - number of nonlinear iterations 45239f67acb7SJed Brown 45249f67acb7SJed Brown Level: intermediate 45259f67acb7SJed Brown 4526*195e9b02SBarry Smith Note: 4527bcf0153eSBarry Smith This counter is reset to zero for each successive call to `TSSolve()`. 4528bcf0153eSBarry Smith 4529bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSolve()`, `TSGetKSPIterations()` 45309f67acb7SJed Brown @*/ 4531d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSNESIterations(TS ts, PetscInt *nits) 4532d71ae5a4SJacob Faibussowitsch { 45339f67acb7SJed Brown PetscFunctionBegin; 45349f67acb7SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 45359f67acb7SJed Brown PetscValidIntPointer(nits, 2); 45365ef26d82SJed Brown *nits = ts->snes_its; 45373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 45389f67acb7SJed Brown } 45399f67acb7SJed Brown 45409f67acb7SJed Brown /*@ 45415ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 45429f67acb7SJed Brown used by the time integrator. 45439f67acb7SJed Brown 45449f67acb7SJed Brown Not Collective 45459f67acb7SJed Brown 45469f67acb7SJed Brown Input Parameter: 4547bcf0153eSBarry Smith . ts - `TS` context 45489f67acb7SJed Brown 45499f67acb7SJed Brown Output Parameter: 45509f67acb7SJed Brown . lits - number of linear iterations 45519f67acb7SJed Brown 45529f67acb7SJed Brown Level: intermediate 45539f67acb7SJed Brown 4554bcf0153eSBarry Smith Note: 4555bcf0153eSBarry Smith This counter is reset to zero for each successive call to `TSSolve()`. 4556bcf0153eSBarry Smith 4557bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSolve()`, `TSGetSNESIterations()`, `SNESGetKSPIterations()` 45589f67acb7SJed Brown @*/ 4559d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetKSPIterations(TS ts, PetscInt *lits) 4560d71ae5a4SJacob Faibussowitsch { 45619f67acb7SJed Brown PetscFunctionBegin; 45629f67acb7SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 45639f67acb7SJed Brown PetscValidIntPointer(lits, 2); 45645ef26d82SJed Brown *lits = ts->ksp_its; 45653ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 45669f67acb7SJed Brown } 45679f67acb7SJed Brown 4568cef5090cSJed Brown /*@ 4569cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 4570cef5090cSJed Brown 4571cef5090cSJed Brown Not Collective 4572cef5090cSJed Brown 4573cef5090cSJed Brown Input Parameter: 4574bcf0153eSBarry Smith . ts - `TS` context 4575cef5090cSJed Brown 4576cef5090cSJed Brown Output Parameter: 4577cef5090cSJed Brown . rejects - number of steps rejected 4578cef5090cSJed Brown 4579cef5090cSJed Brown Level: intermediate 4580cef5090cSJed Brown 4581bcf0153eSBarry Smith Note: 4582bcf0153eSBarry Smith This counter is reset to zero for each successive call to `TSSolve()`. 4583bcf0153eSBarry Smith 4584bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSolve()`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetSNESFailures()`, `TSSetMaxSNESFailures()`, `TSSetErrorIfStepFails()` 4585cef5090cSJed Brown @*/ 4586d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetStepRejections(TS ts, PetscInt *rejects) 4587d71ae5a4SJacob Faibussowitsch { 4588cef5090cSJed Brown PetscFunctionBegin; 4589cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4590cef5090cSJed Brown PetscValidIntPointer(rejects, 2); 4591cef5090cSJed Brown *rejects = ts->reject; 45923ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4593cef5090cSJed Brown } 4594cef5090cSJed Brown 4595cef5090cSJed Brown /*@ 4596bcf0153eSBarry Smith TSGetSNESFailures - Gets the total number of failed `SNES` solves in a `TS` 4597cef5090cSJed Brown 4598cef5090cSJed Brown Not Collective 4599cef5090cSJed Brown 4600cef5090cSJed Brown Input Parameter: 4601bcf0153eSBarry Smith . ts - `TS` context 4602cef5090cSJed Brown 4603cef5090cSJed Brown Output Parameter: 4604cef5090cSJed Brown . fails - number of failed nonlinear solves 4605cef5090cSJed Brown 4606cef5090cSJed Brown Level: intermediate 4607cef5090cSJed Brown 4608bcf0153eSBarry Smith Note: 4609bcf0153eSBarry Smith This counter is reset to zero for each successive call to `TSSolve()`. 4610bcf0153eSBarry Smith 4611bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSolve()`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSSetMaxSNESFailures()` 4612cef5090cSJed Brown @*/ 4613d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSNESFailures(TS ts, PetscInt *fails) 4614d71ae5a4SJacob Faibussowitsch { 4615cef5090cSJed Brown PetscFunctionBegin; 4616cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4617cef5090cSJed Brown PetscValidIntPointer(fails, 2); 4618cef5090cSJed Brown *fails = ts->num_snes_failures; 46193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4620cef5090cSJed Brown } 4621cef5090cSJed Brown 4622cef5090cSJed Brown /*@ 4623bcf0153eSBarry Smith TSSetMaxStepRejections - Sets the maximum number of step rejections before a time step fails 4624cef5090cSJed Brown 4625cef5090cSJed Brown Not Collective 4626cef5090cSJed Brown 4627d8d19677SJose E. Roman Input Parameters: 4628bcf0153eSBarry Smith + ts - `TS` context 4629cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 4630cef5090cSJed Brown 4631cef5090cSJed Brown Options Database Key: 4632cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 4633cef5090cSJed Brown 4634cef5090cSJed Brown Level: intermediate 4635cef5090cSJed Brown 4636bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `SNES`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxSNESFailures()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `TSSetErrorIfStepFails()`, `TSGetConvergedReason()` 4637cef5090cSJed Brown @*/ 4638d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxStepRejections(TS ts, PetscInt rejects) 4639d71ae5a4SJacob Faibussowitsch { 4640cef5090cSJed Brown PetscFunctionBegin; 4641cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4642cef5090cSJed Brown ts->max_reject = rejects; 46433ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4644cef5090cSJed Brown } 4645cef5090cSJed Brown 4646cef5090cSJed Brown /*@ 4647bcf0153eSBarry Smith TSSetMaxSNESFailures - Sets the maximum number of failed `SNES` solves 4648cef5090cSJed Brown 4649cef5090cSJed Brown Not Collective 4650cef5090cSJed Brown 4651d8d19677SJose E. Roman Input Parameters: 4652bcf0153eSBarry Smith + ts - `TS` context 4653cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 4654cef5090cSJed Brown 4655cef5090cSJed Brown Options Database Key: 4656cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 4657cef5090cSJed Brown 4658cef5090cSJed Brown Level: intermediate 4659cef5090cSJed Brown 4660bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `SNES`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `SNESGetConvergedReason()`, `TSGetConvergedReason()` 4661cef5090cSJed Brown @*/ 4662d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxSNESFailures(TS ts, PetscInt fails) 4663d71ae5a4SJacob Faibussowitsch { 4664cef5090cSJed Brown PetscFunctionBegin; 4665cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4666cef5090cSJed Brown ts->max_snes_failures = fails; 46673ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4668cef5090cSJed Brown } 4669cef5090cSJed Brown 4670cef5090cSJed Brown /*@ 4671*195e9b02SBarry Smith TSSetErrorIfStepFails - Immediately error if no step succeeds during `TSSolve()` 4672cef5090cSJed Brown 4673cef5090cSJed Brown Not Collective 4674cef5090cSJed Brown 4675d8d19677SJose E. Roman Input Parameters: 4676bcf0153eSBarry Smith + ts - `TS` context 4677bcf0153eSBarry Smith - err - `PETSC_TRUE` to error if no step succeeds, `PETSC_FALSE` to return without failure 4678cef5090cSJed Brown 4679cef5090cSJed Brown Options Database Key: 4680cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 4681cef5090cSJed Brown 4682cef5090cSJed Brown Level: intermediate 4683cef5090cSJed Brown 4684bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `TSSetErrorIfStepFails()`, `TSGetConvergedReason()` 4685cef5090cSJed Brown @*/ 4686d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetErrorIfStepFails(TS ts, PetscBool err) 4687d71ae5a4SJacob Faibussowitsch { 4688cef5090cSJed Brown PetscFunctionBegin; 4689cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4690cef5090cSJed Brown ts->errorifstepfailed = err; 46913ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4692cef5090cSJed Brown } 4693cef5090cSJed Brown 469484df9cb4SJed Brown /*@ 4695552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 469684df9cb4SJed Brown 4697*195e9b02SBarry Smith Collective on `ts` if controller has not been created yet 469884df9cb4SJed Brown 46994165533cSJose E. Roman Input Parameter: 4700ed81e22dSJed Brown . ts - time stepping context 470184df9cb4SJed Brown 47024165533cSJose E. Roman Output Parameter: 4703ed81e22dSJed Brown . adapt - adaptive controller 470484df9cb4SJed Brown 470584df9cb4SJed Brown Level: intermediate 470684df9cb4SJed Brown 4707bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSAdapt`, `TSAdaptSetType()`, `TSAdaptChoose()` 470884df9cb4SJed Brown @*/ 4709d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetAdapt(TS ts, TSAdapt *adapt) 4710d71ae5a4SJacob Faibussowitsch { 471184df9cb4SJed Brown PetscFunctionBegin; 471284df9cb4SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4713bec58848SLisandro Dalcin PetscValidPointer(adapt, 2); 471484df9cb4SJed Brown if (!ts->adapt) { 47159566063dSJacob Faibussowitsch PetscCall(TSAdaptCreate(PetscObjectComm((PetscObject)ts), &ts->adapt)); 47169566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)ts->adapt, (PetscObject)ts, 1)); 471784df9cb4SJed Brown } 4718bec58848SLisandro Dalcin *adapt = ts->adapt; 47193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 472084df9cb4SJed Brown } 4721d6ebe24aSShri Abhyankar 47221c3436cfSJed Brown /*@ 4723*195e9b02SBarry Smith TSSetTolerances - Set tolerances for local truncation error when using an adaptive controller 47241c3436cfSJed Brown 47251c3436cfSJed Brown Logically Collective 47261c3436cfSJed Brown 47274165533cSJose E. Roman Input Parameters: 47281c3436cfSJed Brown + ts - time integration context 4729bcf0153eSBarry Smith . atol - scalar absolute tolerances, `PETSC_DECIDE` to leave current value 4730*195e9b02SBarry Smith . vatol - vector of absolute tolerances or `NULL`, used in preference to atol if present 4731bcf0153eSBarry Smith . rtol - scalar relative tolerances, `PETSC_DECIDE` to leave current value 4732*195e9b02SBarry Smith - vrtol - vector of relative tolerances or `NULL`, used in preference to atol if present 47331c3436cfSJed Brown 4734*195e9b02SBarry Smith Options Database Keys: 4735a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 473667b8a455SSatish Balay - -ts_atol <atol> - Absolute tolerance for local truncation error 4737a3cdaa26SBarry Smith 4738bcf0153eSBarry Smith Level: beginner 4739bcf0153eSBarry Smith 47403ff766beSShri Abhyankar Notes: 47413ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 47423ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 47433ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 47443ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 47453ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 47463ff766beSShri Abhyankar differential variables. 47473ff766beSShri Abhyankar 4748bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSAdapt`, `TSErrorWeightedNorm()`, `TSGetTolerances()` 47491c3436cfSJed Brown @*/ 4750d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTolerances(TS ts, PetscReal atol, Vec vatol, PetscReal rtol, Vec vrtol) 4751d71ae5a4SJacob Faibussowitsch { 47521c3436cfSJed Brown PetscFunctionBegin; 4753c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 47541c3436cfSJed Brown if (vatol) { 47559566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)vatol)); 47569566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vatol)); 47571c3436cfSJed Brown ts->vatol = vatol; 47581c3436cfSJed Brown } 4759c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 47601c3436cfSJed Brown if (vrtol) { 47619566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)vrtol)); 47629566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vrtol)); 47631c3436cfSJed Brown ts->vrtol = vrtol; 47641c3436cfSJed Brown } 47653ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 47661c3436cfSJed Brown } 47671c3436cfSJed Brown 4768c5033834SJed Brown /*@ 4769c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 4770c5033834SJed Brown 4771c5033834SJed Brown Logically Collective 4772c5033834SJed Brown 47734165533cSJose E. Roman Input Parameter: 4774c5033834SJed Brown . ts - time integration context 4775c5033834SJed Brown 47764165533cSJose E. Roman Output Parameters: 4777*195e9b02SBarry Smith + atol - scalar absolute tolerances, `NULL` to ignore 4778*195e9b02SBarry Smith . vatol - vector of absolute tolerances, `NULL` to ignore 4779*195e9b02SBarry Smith . rtol - scalar relative tolerances, `NULL` to ignore 4780*195e9b02SBarry Smith - vrtol - vector of relative tolerances, `NULL` to ignore 4781c5033834SJed Brown 4782c5033834SJed Brown Level: beginner 4783c5033834SJed Brown 4784bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSAdapt`, `TSErrorWeightedNorm()`, `TSSetTolerances()` 4785c5033834SJed Brown @*/ 4786d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTolerances(TS ts, PetscReal *atol, Vec *vatol, PetscReal *rtol, Vec *vrtol) 4787d71ae5a4SJacob Faibussowitsch { 4788c5033834SJed Brown PetscFunctionBegin; 4789c5033834SJed Brown if (atol) *atol = ts->atol; 4790c5033834SJed Brown if (vatol) *vatol = ts->vatol; 4791c5033834SJed Brown if (rtol) *rtol = ts->rtol; 4792c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 47933ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4794c5033834SJed Brown } 4795c5033834SJed Brown 47969c6b16b5SShri Abhyankar /*@ 4797a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 47989c6b16b5SShri Abhyankar 4799c3339decSBarry Smith Collective 48009c6b16b5SShri Abhyankar 48014165533cSJose E. Roman Input Parameters: 48029c6b16b5SShri Abhyankar + ts - time stepping context 4803a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 4804a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 48059c6b16b5SShri Abhyankar 48064165533cSJose E. Roman Output Parameters: 4807a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 48087453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 4809a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 48109c6b16b5SShri Abhyankar 48119c6b16b5SShri Abhyankar Level: developer 48129c6b16b5SShri Abhyankar 4813bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSErrorWeightedNorm()`, `TSErrorWeightedNormInfinity()` 48149c6b16b5SShri Abhyankar @*/ 4815d71ae5a4SJacob Faibussowitsch PetscErrorCode TSErrorWeightedNorm2(TS ts, Vec U, Vec Y, PetscReal *norm, PetscReal *norma, PetscReal *normr) 4816d71ae5a4SJacob Faibussowitsch { 48179c6b16b5SShri Abhyankar PetscInt i, n, N, rstart; 48187453f775SEmil Constantinescu PetscInt n_loc, na_loc, nr_loc; 48197453f775SEmil Constantinescu PetscReal n_glb, na_glb, nr_glb; 48209c6b16b5SShri Abhyankar const PetscScalar *u, *y; 48217453f775SEmil Constantinescu PetscReal sum, suma, sumr, gsum, gsuma, gsumr, diff; 48227453f775SEmil Constantinescu PetscReal tol, tola, tolr; 48237453f775SEmil Constantinescu PetscReal err_loc[6], err_glb[6]; 48249c6b16b5SShri Abhyankar 48259c6b16b5SShri Abhyankar PetscFunctionBegin; 48269c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4827a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U, VEC_CLASSID, 2); 4828a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y, VEC_CLASSID, 3); 4829a4868fbcSLisandro Dalcin PetscValidType(U, 2); 4830a4868fbcSLisandro Dalcin PetscValidType(Y, 3); 4831a4868fbcSLisandro Dalcin PetscCheckSameComm(U, 2, Y, 3); 4832dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm, 4); 4833dadcf809SJacob Faibussowitsch PetscValidRealPointer(norma, 5); 4834dadcf809SJacob Faibussowitsch PetscValidRealPointer(normr, 6); 48353c633725SBarry Smith PetscCheck(U != Y, PetscObjectComm((PetscObject)U), PETSC_ERR_ARG_IDN, "U and Y cannot be the same vector"); 48369c6b16b5SShri Abhyankar 48379566063dSJacob Faibussowitsch PetscCall(VecGetSize(U, &N)); 48389566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(U, &n)); 48399566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(U, &rstart, NULL)); 48409566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(U, &u)); 48419566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(Y, &y)); 48429371c9d4SSatish Balay sum = 0.; 48439371c9d4SSatish Balay n_loc = 0; 48449371c9d4SSatish Balay suma = 0.; 48459371c9d4SSatish Balay na_loc = 0; 48469371c9d4SSatish Balay sumr = 0.; 48479371c9d4SSatish Balay nr_loc = 0; 48489c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 48499c6b16b5SShri Abhyankar const PetscScalar *atol, *rtol; 48509566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol, &atol)); 48519566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol, &rtol)); 48529c6b16b5SShri Abhyankar for (i = 0; i < n; i++) { 485376cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 48547453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 48557453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 48567453f775SEmil Constantinescu if (tola > 0.) { 48577453f775SEmil Constantinescu suma += PetscSqr(diff / tola); 48587453f775SEmil Constantinescu na_loc++; 48597453f775SEmil Constantinescu } 48607453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 48617453f775SEmil Constantinescu if (tolr > 0.) { 48627453f775SEmil Constantinescu sumr += PetscSqr(diff / tolr); 48637453f775SEmil Constantinescu nr_loc++; 48647453f775SEmil Constantinescu } 48657453f775SEmil Constantinescu tol = tola + tolr; 48667453f775SEmil Constantinescu if (tol > 0.) { 48677453f775SEmil Constantinescu sum += PetscSqr(diff / tol); 48687453f775SEmil Constantinescu n_loc++; 48697453f775SEmil Constantinescu } 48709c6b16b5SShri Abhyankar } 48719566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol, &atol)); 48729566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol, &rtol)); 48739c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 48749c6b16b5SShri Abhyankar const PetscScalar *atol; 48759566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol, &atol)); 48769c6b16b5SShri Abhyankar for (i = 0; i < n; i++) { 487776cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 48787453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 48797453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 48807453f775SEmil Constantinescu if (tola > 0.) { 48817453f775SEmil Constantinescu suma += PetscSqr(diff / tola); 48827453f775SEmil Constantinescu na_loc++; 48837453f775SEmil Constantinescu } 48847453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 48857453f775SEmil Constantinescu if (tolr > 0.) { 48867453f775SEmil Constantinescu sumr += PetscSqr(diff / tolr); 48877453f775SEmil Constantinescu nr_loc++; 48887453f775SEmil Constantinescu } 48897453f775SEmil Constantinescu tol = tola + tolr; 48907453f775SEmil Constantinescu if (tol > 0.) { 48917453f775SEmil Constantinescu sum += PetscSqr(diff / tol); 48927453f775SEmil Constantinescu n_loc++; 48937453f775SEmil Constantinescu } 48949c6b16b5SShri Abhyankar } 48959566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol, &atol)); 48969c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 48979c6b16b5SShri Abhyankar const PetscScalar *rtol; 48989566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol, &rtol)); 48999c6b16b5SShri Abhyankar for (i = 0; i < n; i++) { 490076cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 49017453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 49027453f775SEmil Constantinescu tola = ts->atol; 49037453f775SEmil Constantinescu if (tola > 0.) { 49047453f775SEmil Constantinescu suma += PetscSqr(diff / tola); 49057453f775SEmil Constantinescu na_loc++; 49067453f775SEmil Constantinescu } 49077453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 49087453f775SEmil Constantinescu if (tolr > 0.) { 49097453f775SEmil Constantinescu sumr += PetscSqr(diff / tolr); 49107453f775SEmil Constantinescu nr_loc++; 49117453f775SEmil Constantinescu } 49127453f775SEmil Constantinescu tol = tola + tolr; 49137453f775SEmil Constantinescu if (tol > 0.) { 49147453f775SEmil Constantinescu sum += PetscSqr(diff / tol); 49157453f775SEmil Constantinescu n_loc++; 49167453f775SEmil Constantinescu } 49179c6b16b5SShri Abhyankar } 49189566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol, &rtol)); 49199c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 49209c6b16b5SShri Abhyankar for (i = 0; i < n; i++) { 492176cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 49227453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 49237453f775SEmil Constantinescu tola = ts->atol; 49247453f775SEmil Constantinescu if (tola > 0.) { 49257453f775SEmil Constantinescu suma += PetscSqr(diff / tola); 49267453f775SEmil Constantinescu na_loc++; 49277453f775SEmil Constantinescu } 49287453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 49297453f775SEmil Constantinescu if (tolr > 0.) { 49307453f775SEmil Constantinescu sumr += PetscSqr(diff / tolr); 49317453f775SEmil Constantinescu nr_loc++; 49327453f775SEmil Constantinescu } 49337453f775SEmil Constantinescu tol = tola + tolr; 49347453f775SEmil Constantinescu if (tol > 0.) { 49357453f775SEmil Constantinescu sum += PetscSqr(diff / tol); 49367453f775SEmil Constantinescu n_loc++; 49377453f775SEmil Constantinescu } 49389c6b16b5SShri Abhyankar } 49399c6b16b5SShri Abhyankar } 49409566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(U, &u)); 49419566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(Y, &y)); 49429c6b16b5SShri Abhyankar 49437453f775SEmil Constantinescu err_loc[0] = sum; 49447453f775SEmil Constantinescu err_loc[1] = suma; 49457453f775SEmil Constantinescu err_loc[2] = sumr; 49467453f775SEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 49477453f775SEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 49487453f775SEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 49497453f775SEmil Constantinescu 49501c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(err_loc, err_glb, 6, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)ts))); 49517453f775SEmil Constantinescu 49527453f775SEmil Constantinescu gsum = err_glb[0]; 49537453f775SEmil Constantinescu gsuma = err_glb[1]; 49547453f775SEmil Constantinescu gsumr = err_glb[2]; 49557453f775SEmil Constantinescu n_glb = err_glb[3]; 49567453f775SEmil Constantinescu na_glb = err_glb[4]; 49577453f775SEmil Constantinescu nr_glb = err_glb[5]; 49587453f775SEmil Constantinescu 4959b1316ef9SEmil Constantinescu *norm = 0.; 49601e66621cSBarry Smith if (n_glb > 0.) *norm = PetscSqrtReal(gsum / n_glb); 4961b1316ef9SEmil Constantinescu *norma = 0.; 49621e66621cSBarry Smith if (na_glb > 0.) *norma = PetscSqrtReal(gsuma / na_glb); 4963b1316ef9SEmil Constantinescu *normr = 0.; 49641e66621cSBarry Smith if (nr_glb > 0.) *normr = PetscSqrtReal(gsumr / nr_glb); 49659c6b16b5SShri Abhyankar 49663c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norm"); 49673c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norma"); 49683c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in normr"); 49693ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 49709c6b16b5SShri Abhyankar } 49719c6b16b5SShri Abhyankar 49729c6b16b5SShri Abhyankar /*@ 4973a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 49749c6b16b5SShri Abhyankar 4975c3339decSBarry Smith Collective 49769c6b16b5SShri Abhyankar 49774165533cSJose E. Roman Input Parameters: 49789c6b16b5SShri Abhyankar + ts - time stepping context 4979a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 4980a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 49819c6b16b5SShri Abhyankar 49824165533cSJose E. Roman Output Parameters: 4983a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 49847453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 4985a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 49869c6b16b5SShri Abhyankar 49879c6b16b5SShri Abhyankar Level: developer 49889c6b16b5SShri Abhyankar 4989bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSErrorWeightedNorm()`, `TSErrorWeightedNorm2()` 49909c6b16b5SShri Abhyankar @*/ 4991d71ae5a4SJacob Faibussowitsch PetscErrorCode TSErrorWeightedNormInfinity(TS ts, Vec U, Vec Y, PetscReal *norm, PetscReal *norma, PetscReal *normr) 4992d71ae5a4SJacob Faibussowitsch { 49937453f775SEmil Constantinescu PetscInt i, n, N, rstart; 49949c6b16b5SShri Abhyankar const PetscScalar *u, *y; 49957453f775SEmil Constantinescu PetscReal max, gmax, maxa, gmaxa, maxr, gmaxr; 49967453f775SEmil Constantinescu PetscReal tol, tola, tolr, diff; 49977453f775SEmil Constantinescu PetscReal err_loc[3], err_glb[3]; 49989c6b16b5SShri Abhyankar 49999c6b16b5SShri Abhyankar PetscFunctionBegin; 50009c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5001a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U, VEC_CLASSID, 2); 5002a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y, VEC_CLASSID, 3); 5003a4868fbcSLisandro Dalcin PetscValidType(U, 2); 5004a4868fbcSLisandro Dalcin PetscValidType(Y, 3); 5005a4868fbcSLisandro Dalcin PetscCheckSameComm(U, 2, Y, 3); 5006dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm, 4); 5007dadcf809SJacob Faibussowitsch PetscValidRealPointer(norma, 5); 5008dadcf809SJacob Faibussowitsch PetscValidRealPointer(normr, 6); 50093c633725SBarry Smith PetscCheck(U != Y, PetscObjectComm((PetscObject)U), PETSC_ERR_ARG_IDN, "U and Y cannot be the same vector"); 50109c6b16b5SShri Abhyankar 50119566063dSJacob Faibussowitsch PetscCall(VecGetSize(U, &N)); 50129566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(U, &n)); 50139566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(U, &rstart, NULL)); 50149566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(U, &u)); 50159566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(Y, &y)); 50167453f775SEmil Constantinescu 50177453f775SEmil Constantinescu max = 0.; 50187453f775SEmil Constantinescu maxa = 0.; 50197453f775SEmil Constantinescu maxr = 0.; 50207453f775SEmil Constantinescu 50217453f775SEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 50229c6b16b5SShri Abhyankar const PetscScalar *atol, *rtol; 50239566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol, &atol)); 50249566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol, &rtol)); 50257453f775SEmil Constantinescu 50267453f775SEmil Constantinescu for (i = 0; i < n; i++) { 502776cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 50287453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50297453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 50307453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 50317453f775SEmil Constantinescu tol = tola + tolr; 50321e66621cSBarry Smith if (tola > 0.) maxa = PetscMax(maxa, diff / tola); 50331e66621cSBarry Smith if (tolr > 0.) maxr = PetscMax(maxr, diff / tolr); 50341e66621cSBarry Smith if (tol > 0.) max = PetscMax(max, diff / tol); 50359c6b16b5SShri Abhyankar } 50369566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol, &atol)); 50379566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol, &rtol)); 50389c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 50399c6b16b5SShri Abhyankar const PetscScalar *atol; 50409566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol, &atol)); 50417453f775SEmil Constantinescu for (i = 0; i < n; i++) { 504276cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 50437453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50447453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 50457453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 50467453f775SEmil Constantinescu tol = tola + tolr; 50471e66621cSBarry Smith if (tola > 0.) maxa = PetscMax(maxa, diff / tola); 50481e66621cSBarry Smith if (tolr > 0.) maxr = PetscMax(maxr, diff / tolr); 50491e66621cSBarry Smith if (tol > 0.) max = PetscMax(max, diff / tol); 50509c6b16b5SShri Abhyankar } 50519566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol, &atol)); 50529c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 50539c6b16b5SShri Abhyankar const PetscScalar *rtol; 50549566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol, &rtol)); 50557453f775SEmil Constantinescu 50567453f775SEmil Constantinescu for (i = 0; i < n; i++) { 505776cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 50587453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50597453f775SEmil Constantinescu tola = ts->atol; 50607453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 50617453f775SEmil Constantinescu tol = tola + tolr; 50621e66621cSBarry Smith if (tola > 0.) maxa = PetscMax(maxa, diff / tola); 50631e66621cSBarry Smith if (tolr > 0.) maxr = PetscMax(maxr, diff / tolr); 50641e66621cSBarry Smith if (tol > 0.) max = PetscMax(max, diff / tol); 50659c6b16b5SShri Abhyankar } 50669566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol, &rtol)); 50679c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 50687453f775SEmil Constantinescu 50697453f775SEmil Constantinescu for (i = 0; i < n; i++) { 507076cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 50717453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50727453f775SEmil Constantinescu tola = ts->atol; 50737453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 50747453f775SEmil Constantinescu tol = tola + tolr; 50751e66621cSBarry Smith if (tola > 0.) maxa = PetscMax(maxa, diff / tola); 50761e66621cSBarry Smith if (tolr > 0.) maxr = PetscMax(maxr, diff / tolr); 50771e66621cSBarry Smith if (tol > 0.) max = PetscMax(max, diff / tol); 50789c6b16b5SShri Abhyankar } 50799c6b16b5SShri Abhyankar } 50809566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(U, &u)); 50819566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(Y, &y)); 50827453f775SEmil Constantinescu err_loc[0] = max; 50837453f775SEmil Constantinescu err_loc[1] = maxa; 50847453f775SEmil Constantinescu err_loc[2] = maxr; 50851c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(err_loc, err_glb, 3, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject)ts))); 50867453f775SEmil Constantinescu gmax = err_glb[0]; 50877453f775SEmil Constantinescu gmaxa = err_glb[1]; 50887453f775SEmil Constantinescu gmaxr = err_glb[2]; 50899c6b16b5SShri Abhyankar 50909c6b16b5SShri Abhyankar *norm = gmax; 50917453f775SEmil Constantinescu *norma = gmaxa; 50927453f775SEmil Constantinescu *normr = gmaxr; 50933c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norm"); 50943c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norma"); 50953c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in normr"); 50963ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 50979c6b16b5SShri Abhyankar } 50989c6b16b5SShri Abhyankar 50991c3436cfSJed Brown /*@ 51008a175baeSEmil Constantinescu TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances 51011c3436cfSJed Brown 5102c3339decSBarry Smith Collective 51031c3436cfSJed Brown 51044165533cSJose E. Roman Input Parameters: 51051c3436cfSJed Brown + ts - time stepping context 5106a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5107a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 5108bcf0153eSBarry Smith - wnormtype - norm type, either `NORM_2` or `NORM_INFINITY` 51097619abb3SShri 51104165533cSJose E. Roman Output Parameters: 5111a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 51128a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 5113a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 5114a4868fbcSLisandro Dalcin 5115bcf0153eSBarry Smith Options Database Key: 5116a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 5117a4868fbcSLisandro Dalcin 51181c3436cfSJed Brown Level: developer 51191c3436cfSJed Brown 5120bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSErrorWeightedNormInfinity()`, `TSErrorWeightedNorm2()`, `TSErrorWeightedENorm` 51211c3436cfSJed Brown @*/ 5122d71ae5a4SJacob Faibussowitsch PetscErrorCode TSErrorWeightedNorm(TS ts, Vec U, Vec Y, NormType wnormtype, PetscReal *norm, PetscReal *norma, PetscReal *normr) 5123d71ae5a4SJacob Faibussowitsch { 51241c3436cfSJed Brown PetscFunctionBegin; 51251e66621cSBarry Smith if (wnormtype == NORM_2) PetscCall(TSErrorWeightedNorm2(ts, U, Y, norm, norma, normr)); 51261e66621cSBarry Smith else if (wnormtype == NORM_INFINITY) PetscCall(TSErrorWeightedNormInfinity(ts, U, Y, norm, norma, normr)); 51271e66621cSBarry Smith else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for norm type %s", NormTypes[wnormtype]); 51283ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 51291c3436cfSJed Brown } 51301c3436cfSJed Brown 51318a175baeSEmil Constantinescu /*@ 51328a175baeSEmil Constantinescu TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances 51338a175baeSEmil Constantinescu 5134c3339decSBarry Smith Collective 51358a175baeSEmil Constantinescu 51364165533cSJose E. Roman Input Parameters: 51378a175baeSEmil Constantinescu + ts - time stepping context 51388a175baeSEmil Constantinescu . E - error vector 51398a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 51408a175baeSEmil Constantinescu - Y - state vector, previous time step 51418a175baeSEmil Constantinescu 51424165533cSJose E. Roman Output Parameters: 5143a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 51448a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5145a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 51468a175baeSEmil Constantinescu 51478a175baeSEmil Constantinescu Level: developer 51488a175baeSEmil Constantinescu 5149bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSErrorWeightedENorm()`, `TSErrorWeightedENormInfinity()` 51508a175baeSEmil Constantinescu @*/ 5151d71ae5a4SJacob Faibussowitsch PetscErrorCode TSErrorWeightedENorm2(TS ts, Vec E, Vec U, Vec Y, PetscReal *norm, PetscReal *norma, PetscReal *normr) 5152d71ae5a4SJacob Faibussowitsch { 51538a175baeSEmil Constantinescu PetscInt i, n, N, rstart; 51548a175baeSEmil Constantinescu PetscInt n_loc, na_loc, nr_loc; 51558a175baeSEmil Constantinescu PetscReal n_glb, na_glb, nr_glb; 51568a175baeSEmil Constantinescu const PetscScalar *e, *u, *y; 51578a175baeSEmil Constantinescu PetscReal err, sum, suma, sumr, gsum, gsuma, gsumr; 51588a175baeSEmil Constantinescu PetscReal tol, tola, tolr; 51598a175baeSEmil Constantinescu PetscReal err_loc[6], err_glb[6]; 51608a175baeSEmil Constantinescu 51618a175baeSEmil Constantinescu PetscFunctionBegin; 51628a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 51638a175baeSEmil Constantinescu PetscValidHeaderSpecific(E, VEC_CLASSID, 2); 51648a175baeSEmil Constantinescu PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 51658a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y, VEC_CLASSID, 4); 51668a175baeSEmil Constantinescu PetscValidType(E, 2); 51678a175baeSEmil Constantinescu PetscValidType(U, 3); 51688a175baeSEmil Constantinescu PetscValidType(Y, 4); 51698a175baeSEmil Constantinescu PetscCheckSameComm(E, 2, U, 3); 5170064a246eSJacob Faibussowitsch PetscCheckSameComm(U, 3, Y, 4); 5171dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm, 5); 5172dadcf809SJacob Faibussowitsch PetscValidRealPointer(norma, 6); 5173dadcf809SJacob Faibussowitsch PetscValidRealPointer(normr, 7); 51748a175baeSEmil Constantinescu 51759566063dSJacob Faibussowitsch PetscCall(VecGetSize(E, &N)); 51769566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(E, &n)); 51779566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(E, &rstart, NULL)); 51789566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(E, &e)); 51799566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(U, &u)); 51809566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(Y, &y)); 51819371c9d4SSatish Balay sum = 0.; 51829371c9d4SSatish Balay n_loc = 0; 51839371c9d4SSatish Balay suma = 0.; 51849371c9d4SSatish Balay na_loc = 0; 51859371c9d4SSatish Balay sumr = 0.; 51869371c9d4SSatish Balay nr_loc = 0; 51878a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { 51888a175baeSEmil Constantinescu const PetscScalar *atol, *rtol; 51899566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol, &atol)); 51909566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol, &rtol)); 51918a175baeSEmil Constantinescu for (i = 0; i < n; i++) { 519276cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 51938a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 51948a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 51958a175baeSEmil Constantinescu if (tola > 0.) { 51968a175baeSEmil Constantinescu suma += PetscSqr(err / tola); 51978a175baeSEmil Constantinescu na_loc++; 51988a175baeSEmil Constantinescu } 51998a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 52008a175baeSEmil Constantinescu if (tolr > 0.) { 52018a175baeSEmil Constantinescu sumr += PetscSqr(err / tolr); 52028a175baeSEmil Constantinescu nr_loc++; 52038a175baeSEmil Constantinescu } 52048a175baeSEmil Constantinescu tol = tola + tolr; 52058a175baeSEmil Constantinescu if (tol > 0.) { 52068a175baeSEmil Constantinescu sum += PetscSqr(err / tol); 52078a175baeSEmil Constantinescu n_loc++; 52088a175baeSEmil Constantinescu } 52098a175baeSEmil Constantinescu } 52109566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol, &atol)); 52119566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol, &rtol)); 52128a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 52138a175baeSEmil Constantinescu const PetscScalar *atol; 52149566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol, &atol)); 52158a175baeSEmil Constantinescu for (i = 0; i < n; i++) { 521676cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 52178a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 52188a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 52198a175baeSEmil Constantinescu if (tola > 0.) { 52208a175baeSEmil Constantinescu suma += PetscSqr(err / tola); 52218a175baeSEmil Constantinescu na_loc++; 52228a175baeSEmil Constantinescu } 52238a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 52248a175baeSEmil Constantinescu if (tolr > 0.) { 52258a175baeSEmil Constantinescu sumr += PetscSqr(err / tolr); 52268a175baeSEmil Constantinescu nr_loc++; 52278a175baeSEmil Constantinescu } 52288a175baeSEmil Constantinescu tol = tola + tolr; 52298a175baeSEmil Constantinescu if (tol > 0.) { 52308a175baeSEmil Constantinescu sum += PetscSqr(err / tol); 52318a175baeSEmil Constantinescu n_loc++; 52328a175baeSEmil Constantinescu } 52338a175baeSEmil Constantinescu } 52349566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol, &atol)); 52358a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 52368a175baeSEmil Constantinescu const PetscScalar *rtol; 52379566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol, &rtol)); 52388a175baeSEmil Constantinescu for (i = 0; i < n; i++) { 523976cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 52408a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 52418a175baeSEmil Constantinescu tola = ts->atol; 52428a175baeSEmil Constantinescu if (tola > 0.) { 52438a175baeSEmil Constantinescu suma += PetscSqr(err / tola); 52448a175baeSEmil Constantinescu na_loc++; 52458a175baeSEmil Constantinescu } 52468a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 52478a175baeSEmil Constantinescu if (tolr > 0.) { 52488a175baeSEmil Constantinescu sumr += PetscSqr(err / tolr); 52498a175baeSEmil Constantinescu nr_loc++; 52508a175baeSEmil Constantinescu } 52518a175baeSEmil Constantinescu tol = tola + tolr; 52528a175baeSEmil Constantinescu if (tol > 0.) { 52538a175baeSEmil Constantinescu sum += PetscSqr(err / tol); 52548a175baeSEmil Constantinescu n_loc++; 52558a175baeSEmil Constantinescu } 52568a175baeSEmil Constantinescu } 52579566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol, &rtol)); 52588a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 52598a175baeSEmil Constantinescu for (i = 0; i < n; i++) { 526076cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 52618a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 52628a175baeSEmil Constantinescu tola = ts->atol; 52638a175baeSEmil Constantinescu if (tola > 0.) { 52648a175baeSEmil Constantinescu suma += PetscSqr(err / tola); 52658a175baeSEmil Constantinescu na_loc++; 52668a175baeSEmil Constantinescu } 52678a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 52688a175baeSEmil Constantinescu if (tolr > 0.) { 52698a175baeSEmil Constantinescu sumr += PetscSqr(err / tolr); 52708a175baeSEmil Constantinescu nr_loc++; 52718a175baeSEmil Constantinescu } 52728a175baeSEmil Constantinescu tol = tola + tolr; 52738a175baeSEmil Constantinescu if (tol > 0.) { 52748a175baeSEmil Constantinescu sum += PetscSqr(err / tol); 52758a175baeSEmil Constantinescu n_loc++; 52768a175baeSEmil Constantinescu } 52778a175baeSEmil Constantinescu } 52788a175baeSEmil Constantinescu } 52799566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(E, &e)); 52809566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(U, &u)); 52819566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(Y, &y)); 52828a175baeSEmil Constantinescu 52838a175baeSEmil Constantinescu err_loc[0] = sum; 52848a175baeSEmil Constantinescu err_loc[1] = suma; 52858a175baeSEmil Constantinescu err_loc[2] = sumr; 52868a175baeSEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 52878a175baeSEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 52888a175baeSEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 52898a175baeSEmil Constantinescu 52901c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(err_loc, err_glb, 6, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)ts))); 52918a175baeSEmil Constantinescu 52928a175baeSEmil Constantinescu gsum = err_glb[0]; 52938a175baeSEmil Constantinescu gsuma = err_glb[1]; 52948a175baeSEmil Constantinescu gsumr = err_glb[2]; 52958a175baeSEmil Constantinescu n_glb = err_glb[3]; 52968a175baeSEmil Constantinescu na_glb = err_glb[4]; 52978a175baeSEmil Constantinescu nr_glb = err_glb[5]; 52988a175baeSEmil Constantinescu 52998a175baeSEmil Constantinescu *norm = 0.; 53001e66621cSBarry Smith if (n_glb > 0.) *norm = PetscSqrtReal(gsum / n_glb); 53018a175baeSEmil Constantinescu *norma = 0.; 53021e66621cSBarry Smith if (na_glb > 0.) *norma = PetscSqrtReal(gsuma / na_glb); 53038a175baeSEmil Constantinescu *normr = 0.; 53041e66621cSBarry Smith if (nr_glb > 0.) *normr = PetscSqrtReal(gsumr / nr_glb); 53058a175baeSEmil Constantinescu 53063c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norm"); 53073c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norma"); 53083c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in normr"); 53093ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 53108a175baeSEmil Constantinescu } 53118a175baeSEmil Constantinescu 53128a175baeSEmil Constantinescu /*@ 53138a175baeSEmil Constantinescu TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances 5314bcf0153eSBarry Smith 5315c3339decSBarry Smith Collective 53168a175baeSEmil Constantinescu 53174165533cSJose E. Roman Input Parameters: 53188a175baeSEmil Constantinescu + ts - time stepping context 53198a175baeSEmil Constantinescu . E - error vector 53208a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 53218a175baeSEmil Constantinescu - Y - state vector, previous time step 53228a175baeSEmil Constantinescu 53234165533cSJose E. Roman Output Parameters: 5324a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 53258a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5326a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 53278a175baeSEmil Constantinescu 53288a175baeSEmil Constantinescu Level: developer 53298a175baeSEmil Constantinescu 5330bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSErrorWeightedENorm()`, `TSErrorWeightedENorm2()` 53318a175baeSEmil Constantinescu @*/ 5332d71ae5a4SJacob Faibussowitsch PetscErrorCode TSErrorWeightedENormInfinity(TS ts, Vec E, Vec U, Vec Y, PetscReal *norm, PetscReal *norma, PetscReal *normr) 5333d71ae5a4SJacob Faibussowitsch { 53348a175baeSEmil Constantinescu PetscInt i, n, N, rstart; 53358a175baeSEmil Constantinescu const PetscScalar *e, *u, *y; 53368a175baeSEmil Constantinescu PetscReal err, max, gmax, maxa, gmaxa, maxr, gmaxr; 53378a175baeSEmil Constantinescu PetscReal tol, tola, tolr; 53388a175baeSEmil Constantinescu PetscReal err_loc[3], err_glb[3]; 53398a175baeSEmil Constantinescu 53408a175baeSEmil Constantinescu PetscFunctionBegin; 53418a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 53428a175baeSEmil Constantinescu PetscValidHeaderSpecific(E, VEC_CLASSID, 2); 53438a175baeSEmil Constantinescu PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 53448a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y, VEC_CLASSID, 4); 53458a175baeSEmil Constantinescu PetscValidType(E, 2); 53468a175baeSEmil Constantinescu PetscValidType(U, 3); 53478a175baeSEmil Constantinescu PetscValidType(Y, 4); 53488a175baeSEmil Constantinescu PetscCheckSameComm(E, 2, U, 3); 5349064a246eSJacob Faibussowitsch PetscCheckSameComm(U, 3, Y, 4); 5350dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm, 5); 5351dadcf809SJacob Faibussowitsch PetscValidRealPointer(norma, 6); 5352dadcf809SJacob Faibussowitsch PetscValidRealPointer(normr, 7); 53538a175baeSEmil Constantinescu 53549566063dSJacob Faibussowitsch PetscCall(VecGetSize(E, &N)); 53559566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(E, &n)); 53569566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(E, &rstart, NULL)); 53579566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(E, &e)); 53589566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(U, &u)); 53599566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(Y, &y)); 53608a175baeSEmil Constantinescu 53618a175baeSEmil Constantinescu max = 0.; 53628a175baeSEmil Constantinescu maxa = 0.; 53638a175baeSEmil Constantinescu maxr = 0.; 53648a175baeSEmil Constantinescu 53658a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 53668a175baeSEmil Constantinescu const PetscScalar *atol, *rtol; 53679566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol, &atol)); 53689566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol, &rtol)); 53698a175baeSEmil Constantinescu 53708a175baeSEmil Constantinescu for (i = 0; i < n; i++) { 537176cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 53728a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 53738a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 53748a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 53758a175baeSEmil Constantinescu tol = tola + tolr; 53761e66621cSBarry Smith if (tola > 0.) maxa = PetscMax(maxa, err / tola); 53771e66621cSBarry Smith if (tolr > 0.) maxr = PetscMax(maxr, err / tolr); 53781e66621cSBarry Smith if (tol > 0.) max = PetscMax(max, err / tol); 53798a175baeSEmil Constantinescu } 53809566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol, &atol)); 53819566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol, &rtol)); 53828a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 53838a175baeSEmil Constantinescu const PetscScalar *atol; 53849566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol, &atol)); 53858a175baeSEmil Constantinescu for (i = 0; i < n; i++) { 538676cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 53878a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 53888a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 53898a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 53908a175baeSEmil Constantinescu tol = tola + tolr; 53911e66621cSBarry Smith if (tola > 0.) maxa = PetscMax(maxa, err / tola); 53921e66621cSBarry Smith if (tolr > 0.) maxr = PetscMax(maxr, err / tolr); 53931e66621cSBarry Smith if (tol > 0.) max = PetscMax(max, err / tol); 53948a175baeSEmil Constantinescu } 53959566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol, &atol)); 53968a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 53978a175baeSEmil Constantinescu const PetscScalar *rtol; 53989566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol, &rtol)); 53998a175baeSEmil Constantinescu 54008a175baeSEmil Constantinescu for (i = 0; i < n; i++) { 540176cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 54028a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 54038a175baeSEmil Constantinescu tola = ts->atol; 54048a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 54058a175baeSEmil Constantinescu tol = tola + tolr; 54061e66621cSBarry Smith if (tola > 0.) maxa = PetscMax(maxa, err / tola); 54071e66621cSBarry Smith if (tolr > 0.) maxr = PetscMax(maxr, err / tolr); 54081e66621cSBarry Smith if (tol > 0.) max = PetscMax(max, err / tol); 54098a175baeSEmil Constantinescu } 54109566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol, &rtol)); 54118a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 54128a175baeSEmil Constantinescu 54138a175baeSEmil Constantinescu for (i = 0; i < n; i++) { 541476cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 54158a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 54168a175baeSEmil Constantinescu tola = ts->atol; 54178a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 54188a175baeSEmil Constantinescu tol = tola + tolr; 54191e66621cSBarry Smith if (tola > 0.) maxa = PetscMax(maxa, err / tola); 54201e66621cSBarry Smith if (tolr > 0.) maxr = PetscMax(maxr, err / tolr); 54211e66621cSBarry Smith if (tol > 0.) max = PetscMax(max, err / tol); 54228a175baeSEmil Constantinescu } 54238a175baeSEmil Constantinescu } 54249566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(E, &e)); 54259566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(U, &u)); 54269566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(Y, &y)); 54278a175baeSEmil Constantinescu err_loc[0] = max; 54288a175baeSEmil Constantinescu err_loc[1] = maxa; 54298a175baeSEmil Constantinescu err_loc[2] = maxr; 54301c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(err_loc, err_glb, 3, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject)ts))); 54318a175baeSEmil Constantinescu gmax = err_glb[0]; 54328a175baeSEmil Constantinescu gmaxa = err_glb[1]; 54338a175baeSEmil Constantinescu gmaxr = err_glb[2]; 54348a175baeSEmil Constantinescu 54358a175baeSEmil Constantinescu *norm = gmax; 54368a175baeSEmil Constantinescu *norma = gmaxa; 54378a175baeSEmil Constantinescu *normr = gmaxr; 54383c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norm"); 54393c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norma"); 54403c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in normr"); 54413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 54428a175baeSEmil Constantinescu } 54438a175baeSEmil Constantinescu 54448a175baeSEmil Constantinescu /*@ 54458a175baeSEmil Constantinescu TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances 54468a175baeSEmil Constantinescu 5447c3339decSBarry Smith Collective 54488a175baeSEmil Constantinescu 54494165533cSJose E. Roman Input Parameters: 54508a175baeSEmil Constantinescu + ts - time stepping context 54518a175baeSEmil Constantinescu . E - error vector 54528a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 54538a175baeSEmil Constantinescu . Y - state vector, previous time step 5454bcf0153eSBarry Smith - wnormtype - norm type, either `NORM_2` or `NORM_INFINITY` 54558a175baeSEmil Constantinescu 54564165533cSJose E. Roman Output Parameters: 5457a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 54588a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 5459a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 54608a175baeSEmil Constantinescu 5461bcf0153eSBarry Smith Options Database Key: 54628a175baeSEmil Constantinescu . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 54638a175baeSEmil Constantinescu 54648a175baeSEmil Constantinescu Level: developer 54658a175baeSEmil Constantinescu 5466bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSErrorWeightedENormInfinity()`, `TSErrorWeightedENorm2()`, `TSErrorWeightedNormInfinity()`, `TSErrorWeightedNorm2()` 54678a175baeSEmil Constantinescu @*/ 5468d71ae5a4SJacob Faibussowitsch PetscErrorCode TSErrorWeightedENorm(TS ts, Vec E, Vec U, Vec Y, NormType wnormtype, PetscReal *norm, PetscReal *norma, PetscReal *normr) 5469d71ae5a4SJacob Faibussowitsch { 54708a175baeSEmil Constantinescu PetscFunctionBegin; 54711e66621cSBarry Smith if (wnormtype == NORM_2) PetscCall(TSErrorWeightedENorm2(ts, E, U, Y, norm, norma, normr)); 54721e66621cSBarry Smith else if (wnormtype == NORM_INFINITY) PetscCall(TSErrorWeightedENormInfinity(ts, E, U, Y, norm, norma, normr)); 54731e66621cSBarry Smith else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for norm type %s", NormTypes[wnormtype]); 54743ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 54758a175baeSEmil Constantinescu } 54768a175baeSEmil Constantinescu 54778d59e960SJed Brown /*@ 54788d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 54798d59e960SJed Brown 5480c3339decSBarry Smith Logically Collective 54818d59e960SJed Brown 54824165533cSJose E. Roman Input Parameters: 54838d59e960SJed Brown + ts - time stepping context 54848d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 54858d59e960SJed Brown 54868d59e960SJed Brown Note: 54878d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 54888d59e960SJed Brown 54898d59e960SJed Brown Level: intermediate 54908d59e960SJed Brown 5491bcf0153eSBarry Smith .seealso: [](chapter_ts), `TSGetCFLTime()`, `TSADAPTCFL` 54928d59e960SJed Brown @*/ 5493d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetCFLTimeLocal(TS ts, PetscReal cfltime) 5494d71ae5a4SJacob Faibussowitsch { 54958d59e960SJed Brown PetscFunctionBegin; 54968d59e960SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 54978d59e960SJed Brown ts->cfltime_local = cfltime; 54988d59e960SJed Brown ts->cfltime = -1.; 54993ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 55008d59e960SJed Brown } 55018d59e960SJed Brown 55028d59e960SJed Brown /*@ 55038d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 55048d59e960SJed Brown 5505c3339decSBarry Smith Collective 55068d59e960SJed Brown 55074165533cSJose E. Roman Input Parameter: 55088d59e960SJed Brown . ts - time stepping context 55098d59e960SJed Brown 55104165533cSJose E. Roman Output Parameter: 55118d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 55128d59e960SJed Brown 55138d59e960SJed Brown Level: advanced 55148d59e960SJed Brown 5515bcf0153eSBarry Smith .seealso: [](chapter_ts), `TSSetCFLTimeLocal()` 55168d59e960SJed Brown @*/ 5517d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetCFLTime(TS ts, PetscReal *cfltime) 5518d71ae5a4SJacob Faibussowitsch { 55198d59e960SJed Brown PetscFunctionBegin; 55201e66621cSBarry Smith if (ts->cfltime < 0) PetscCall(MPIU_Allreduce(&ts->cfltime_local, &ts->cfltime, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)ts))); 55218d59e960SJed Brown *cfltime = ts->cfltime; 55223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 55238d59e960SJed Brown } 55248d59e960SJed Brown 5525d6ebe24aSShri Abhyankar /*@ 5526d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 5527d6ebe24aSShri Abhyankar 5528d6ebe24aSShri Abhyankar Input Parameters: 5529bcf0153eSBarry Smith + ts - the `TS` context. 5530d6ebe24aSShri Abhyankar . xl - lower bound. 5531a2b725a8SWilliam Gropp - xu - upper bound. 5532d6ebe24aSShri Abhyankar 55332bd2b0e6SSatish Balay Level: advanced 55342bd2b0e6SSatish Balay 5535bcf0153eSBarry Smith Note: 5536bcf0153eSBarry Smith If this routine is not called then the lower and upper bounds are set to 5537bcf0153eSBarry Smith `PETSC_NINFINITY` and `PETSC_INFINITY` respectively during `SNESSetUp()`. 5538bcf0153eSBarry Smith 5539bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS` 5540d6ebe24aSShri Abhyankar @*/ 5541d71ae5a4SJacob Faibussowitsch PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 5542d71ae5a4SJacob Faibussowitsch { 5543d6ebe24aSShri Abhyankar SNES snes; 5544d6ebe24aSShri Abhyankar 5545d6ebe24aSShri Abhyankar PetscFunctionBegin; 55469566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 55479566063dSJacob Faibussowitsch PetscCall(SNESVISetVariableBounds(snes, xl, xu)); 55483ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5549d6ebe24aSShri Abhyankar } 5550d6ebe24aSShri Abhyankar 5551f9c1d6abSBarry Smith /*@ 5552f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 5553f9c1d6abSBarry Smith 5554c3339decSBarry Smith Collective 5555f9c1d6abSBarry Smith 5556f9c1d6abSBarry Smith Input Parameters: 5557bcf0153eSBarry Smith + ts - the `TS` context 5558f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 5559f9c1d6abSBarry Smith 5560f9c1d6abSBarry Smith Output Parameters: 5561f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 5562f9c1d6abSBarry Smith 5563f9c1d6abSBarry Smith Level: developer 5564f9c1d6abSBarry Smith 5565bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetRHSFunction()`, `TSComputeIFunction()` 5566f9c1d6abSBarry Smith @*/ 5567d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeLinearStability(TS ts, PetscReal xr, PetscReal xi, PetscReal *yr, PetscReal *yi) 5568d71ae5a4SJacob Faibussowitsch { 5569f9c1d6abSBarry Smith PetscFunctionBegin; 5570f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5571dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, linearstability, xr, xi, yr, yi); 55723ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5573f9c1d6abSBarry Smith } 557424655328SShri 557524655328SShri /*@ 5576dcb233daSLisandro Dalcin TSRestartStep - Flags the solver to restart the next step 5577dcb233daSLisandro Dalcin 5578c3339decSBarry Smith Collective 5579dcb233daSLisandro Dalcin 5580dcb233daSLisandro Dalcin Input Parameter: 5581bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 5582dcb233daSLisandro Dalcin 5583dcb233daSLisandro Dalcin Level: advanced 5584dcb233daSLisandro Dalcin 5585dcb233daSLisandro Dalcin Notes: 5586bcf0153eSBarry Smith Multistep methods like `TSBDF` or Runge-Kutta methods with FSAL property require restarting the solver in the event of 5587dcb233daSLisandro Dalcin discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution 5588dcb233daSLisandro Dalcin vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For 5589bcf0153eSBarry Smith the sake of correctness and maximum safety, users are expected to call `TSRestart()` whenever they introduce 5590dcb233daSLisandro Dalcin discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with 5591dcb233daSLisandro Dalcin discontinuous source terms). 5592dcb233daSLisandro Dalcin 5593bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSBDF`, `TSSolve()`, `TSSetPreStep()`, `TSSetPostStep()` 5594dcb233daSLisandro Dalcin @*/ 5595d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRestartStep(TS ts) 5596d71ae5a4SJacob Faibussowitsch { 5597dcb233daSLisandro Dalcin PetscFunctionBegin; 5598dcb233daSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5599dcb233daSLisandro Dalcin ts->steprestart = PETSC_TRUE; 56003ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5601dcb233daSLisandro Dalcin } 5602dcb233daSLisandro Dalcin 5603dcb233daSLisandro Dalcin /*@ 560424655328SShri TSRollBack - Rolls back one time step 560524655328SShri 5606c3339decSBarry Smith Collective 560724655328SShri 560824655328SShri Input Parameter: 5609bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 561024655328SShri 561124655328SShri Level: advanced 561224655328SShri 5613bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSCreate()`, `TSSetUp()`, `TSDestroy()`, `TSSolve()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSInterpolate()` 561424655328SShri @*/ 5615d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRollBack(TS ts) 5616d71ae5a4SJacob Faibussowitsch { 561724655328SShri PetscFunctionBegin; 561824655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 56193c633725SBarry Smith PetscCheck(!ts->steprollback, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "TSRollBack already called"); 5620dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, rollback); 562124655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 562224655328SShri ts->ptime = ts->ptime_prev; 5623be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime_prev_rollback; 56242808aa04SLisandro Dalcin ts->steps--; 5625b3de5cdeSLisandro Dalcin ts->steprollback = PETSC_TRUE; 56263ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 562724655328SShri } 5628aeb4809dSShri Abhyankar 5629ff22ae23SHong Zhang /*@ 5630ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 5631ff22ae23SHong Zhang 5632ff22ae23SHong Zhang Input Parameter: 5633bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 5634ff22ae23SHong Zhang 56350429704eSStefano Zampini Output Parameters: 56360429704eSStefano Zampini + ns - the number of stages 56370429704eSStefano Zampini - Y - the current stage vectors 56380429704eSStefano Zampini 5639ff22ae23SHong Zhang Level: advanced 5640ff22ae23SHong Zhang 5641bcf0153eSBarry Smith Note: 5642*195e9b02SBarry Smith Both `ns` and `Y` can be `NULL`. 56430429704eSStefano Zampini 5644bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSCreate()` 5645ff22ae23SHong Zhang @*/ 5646d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetStages(TS ts, PetscInt *ns, Vec **Y) 5647d71ae5a4SJacob Faibussowitsch { 5648ff22ae23SHong Zhang PetscFunctionBegin; 5649ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5650dadcf809SJacob Faibussowitsch if (ns) PetscValidIntPointer(ns, 2); 56510429704eSStefano Zampini if (Y) PetscValidPointer(Y, 3); 56520429704eSStefano Zampini if (!ts->ops->getstages) { 56530429704eSStefano Zampini if (ns) *ns = 0; 56540429704eSStefano Zampini if (Y) *Y = NULL; 5655dbbe0bcdSBarry Smith } else PetscUseTypeMethod(ts, getstages, ns, Y); 56563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5657ff22ae23SHong Zhang } 5658ff22ae23SHong Zhang 5659847ff0e1SMatthew G. Knepley /*@C 5660847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 5661847ff0e1SMatthew G. Knepley 5662c3339decSBarry Smith Collective 5663847ff0e1SMatthew G. Knepley 5664847ff0e1SMatthew G. Knepley Input Parameters: 5665bcf0153eSBarry Smith + ts - the `TS` context 5666847ff0e1SMatthew G. Knepley . t - current timestep 5667847ff0e1SMatthew G. Knepley . U - state vector 5668847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 5669847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 5670847ff0e1SMatthew G. Knepley - ctx - an optional user context 5671847ff0e1SMatthew G. Knepley 5672847ff0e1SMatthew G. Knepley Output Parameters: 5673847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 5674*195e9b02SBarry Smith - B - newly computed Jacobian matrix to use with preconditioner (generally the same as `J`) 5675847ff0e1SMatthew G. Knepley 5676847ff0e1SMatthew G. Knepley Level: intermediate 5677847ff0e1SMatthew G. Knepley 5678847ff0e1SMatthew G. Knepley Notes: 5679847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 5680847ff0e1SMatthew G. Knepley 5681847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 5682847ff0e1SMatthew G. Knepley 5683847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 5684847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 5685847ff0e1SMatthew G. Knepley 5686bcf0153eSBarry Smith This will first try to get the coloring from the `DM`. If the `DM` type has no coloring 5687847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 5688847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 5689847ff0e1SMatthew G. Knepley 5690bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetIJacobian()`, `MatFDColoringCreate()`, `MatFDColoringSetFunction()` 5691847ff0e1SMatthew G. Knepley @*/ 5692d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIJacobianDefaultColor(TS ts, PetscReal t, Vec U, Vec Udot, PetscReal shift, Mat J, Mat B, void *ctx) 5693d71ae5a4SJacob Faibussowitsch { 5694847ff0e1SMatthew G. Knepley SNES snes; 5695847ff0e1SMatthew G. Knepley MatFDColoring color; 5696847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 5697847ff0e1SMatthew G. Knepley 5698847ff0e1SMatthew G. Knepley PetscFunctionBegin; 56999566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject)ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL)); 57009566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)B, "TSMatFDColoring", (PetscObject *)&color)); 5701847ff0e1SMatthew G. Knepley if (!color) { 5702847ff0e1SMatthew G. Knepley DM dm; 5703847ff0e1SMatthew G. Knepley ISColoring iscoloring; 5704847ff0e1SMatthew G. Knepley 57059566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 57069566063dSJacob Faibussowitsch PetscCall(DMHasColoring(dm, &hascolor)); 5707847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 57089566063dSJacob Faibussowitsch PetscCall(DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring)); 57099566063dSJacob Faibussowitsch PetscCall(MatFDColoringCreate(B, iscoloring, &color)); 57109566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFunction(color, (PetscErrorCode(*)(void))SNESTSFormFunction, (void *)ts)); 57119566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFromOptions(color)); 57129566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetUp(B, iscoloring, color)); 57139566063dSJacob Faibussowitsch PetscCall(ISColoringDestroy(&iscoloring)); 5714847ff0e1SMatthew G. Knepley } else { 5715847ff0e1SMatthew G. Knepley MatColoring mc; 5716847ff0e1SMatthew G. Knepley 57179566063dSJacob Faibussowitsch PetscCall(MatColoringCreate(B, &mc)); 57189566063dSJacob Faibussowitsch PetscCall(MatColoringSetDistance(mc, 2)); 57199566063dSJacob Faibussowitsch PetscCall(MatColoringSetType(mc, MATCOLORINGSL)); 57209566063dSJacob Faibussowitsch PetscCall(MatColoringSetFromOptions(mc)); 57219566063dSJacob Faibussowitsch PetscCall(MatColoringApply(mc, &iscoloring)); 57229566063dSJacob Faibussowitsch PetscCall(MatColoringDestroy(&mc)); 57239566063dSJacob Faibussowitsch PetscCall(MatFDColoringCreate(B, iscoloring, &color)); 57249566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFunction(color, (PetscErrorCode(*)(void))SNESTSFormFunction, (void *)ts)); 57259566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFromOptions(color)); 57269566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetUp(B, iscoloring, color)); 57279566063dSJacob Faibussowitsch PetscCall(ISColoringDestroy(&iscoloring)); 5728847ff0e1SMatthew G. Knepley } 57299566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject)B, "TSMatFDColoring", (PetscObject)color)); 57309566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)color)); 5731847ff0e1SMatthew G. Knepley } 57329566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 57339566063dSJacob Faibussowitsch PetscCall(MatFDColoringApply(B, color, U, snes)); 5734847ff0e1SMatthew G. Knepley if (J != B) { 57359566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 57369566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 5737847ff0e1SMatthew G. Knepley } 57383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5739847ff0e1SMatthew G. Knepley } 574093b34091SDebojyoti Ghosh 5741cb9d8021SPierre Barbier de Reuille /*@ 57426bc98fa9SBarry Smith TSSetFunctionDomainError - Set a function that tests if the current state vector is valid 5743cb9d8021SPierre Barbier de Reuille 5744cb9d8021SPierre Barbier de Reuille Input Parameters: 5745bcf0153eSBarry Smith + ts - the `TS` context 5746bcf0153eSBarry Smith - func - function called within `TSFunctionDomainError()` 57476bc98fa9SBarry Smith 57486bc98fa9SBarry Smith Calling sequence of func: 57496bc98fa9SBarry Smith $ PetscErrorCode func(TS ts,PetscReal time,Vec state,PetscBool reject) 57506bc98fa9SBarry Smith 57516bc98fa9SBarry Smith + ts - the TS context 57526bc98fa9SBarry Smith . time - the current time (of the stage) 57536bc98fa9SBarry Smith . state - the state to check if it is valid 57546bc98fa9SBarry Smith - reject - (output parameter) PETSC_FALSE if the state is acceptable, PETSC_TRUE if not acceptable 5755cb9d8021SPierre Barbier de Reuille 5756cb9d8021SPierre Barbier de Reuille Level: intermediate 5757cb9d8021SPierre Barbier de Reuille 57586bc98fa9SBarry Smith Notes: 57596bc98fa9SBarry Smith If an implicit ODE solver is being used then, in addition to providing this routine, the 5760bcf0153eSBarry Smith user's code should call `SNESSetFunctionDomainError()` when domain errors occur during 5761bcf0153eSBarry Smith function evaluations where the functions are provided by `TSSetIFunction()` or `TSSetRHSFunction()`. 5762bcf0153eSBarry Smith Use `TSGetSNES()` to obtain the `SNES` object 57636bc98fa9SBarry Smith 5764bcf0153eSBarry Smith Developer Note: 5765bcf0153eSBarry Smith The naming of this function is inconsistent with the `SNESSetFunctionDomainError()` 57666bc98fa9SBarry Smith since one takes a function pointer and the other does not. 57676bc98fa9SBarry Smith 5768bcf0153eSBarry Smith .seealso: [](chapter_ts), `TSAdaptCheckStage()`, `TSFunctionDomainError()`, `SNESSetFunctionDomainError()`, `TSGetSNES()` 5769cb9d8021SPierre Barbier de Reuille @*/ 5770cb9d8021SPierre Barbier de Reuille 5771d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS, PetscReal, Vec, PetscBool *)) 5772d71ae5a4SJacob Faibussowitsch { 5773cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 5774cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5775cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 57763ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5777cb9d8021SPierre Barbier de Reuille } 5778cb9d8021SPierre Barbier de Reuille 5779cb9d8021SPierre Barbier de Reuille /*@ 57806bc98fa9SBarry Smith TSFunctionDomainError - Checks if the current state is valid 5781cb9d8021SPierre Barbier de Reuille 5782cb9d8021SPierre Barbier de Reuille Input Parameters: 5783bcf0153eSBarry Smith + ts - the `TS` context 57846bc98fa9SBarry Smith . stagetime - time of the simulation 57856bc98fa9SBarry Smith - Y - state vector to check. 5786cb9d8021SPierre Barbier de Reuille 5787cb9d8021SPierre Barbier de Reuille Output Parameter: 5788bcf0153eSBarry Smith . accept - Set to `PETSC_FALSE` if the current state vector is valid. 578996a0c994SBarry Smith 57906bc98fa9SBarry Smith Level: developer 57916bc98fa9SBarry Smith 5792bcf0153eSBarry Smith Note: 5793bcf0153eSBarry Smith This function is called by the `TS` integration routines and calls the user provided function (set with `TSSetFunctionDomainError()`) 5794bcf0153eSBarry Smith to check if the current state is valid. 5795bcf0153eSBarry Smith 5796bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetFunctionDomainError()` 5797cb9d8021SPierre Barbier de Reuille @*/ 5798d71ae5a4SJacob Faibussowitsch PetscErrorCode TSFunctionDomainError(TS ts, PetscReal stagetime, Vec Y, PetscBool *accept) 5799d71ae5a4SJacob Faibussowitsch { 5800cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 5801cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5802cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 58031e66621cSBarry Smith if (ts->functiondomainerror) PetscCall((*ts->functiondomainerror)(ts, stagetime, Y, accept)); 58043ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5805cb9d8021SPierre Barbier de Reuille } 58061ceb14c0SBarry Smith 580793b34091SDebojyoti Ghosh /*@C 5808*195e9b02SBarry Smith TSClone - This function clones a time step `TS` object. 580993b34091SDebojyoti Ghosh 5810d083f849SBarry Smith Collective 581193b34091SDebojyoti Ghosh 581293b34091SDebojyoti Ghosh Input Parameter: 5813bcf0153eSBarry Smith . tsin - The input `TS` 581493b34091SDebojyoti Ghosh 581593b34091SDebojyoti Ghosh Output Parameter: 5816bcf0153eSBarry Smith . tsout - The output `TS` (cloned) 58175eca1a21SEmil Constantinescu 58185eca1a21SEmil Constantinescu Level: developer 581993b34091SDebojyoti Ghosh 5820bcf0153eSBarry Smith Notes: 5821bcf0153eSBarry 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. 5822bcf0153eSBarry Smith It will likely be replaced in the future with a mechanism of switching methods on the fly. 5823bcf0153eSBarry Smith 5824*195e9b02SBarry 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 5825*195e9b02SBarry Smith .vb 5826*195e9b02SBarry Smith SNES snes_dup = NULL; 5827*195e9b02SBarry Smith TSGetSNES(ts,&snes_dup); 5828*195e9b02SBarry Smith TSSetSNES(ts,snes_dup); 5829*195e9b02SBarry Smith .ve 5830bcf0153eSBarry Smith 5831bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `SNES`, `TSCreate()`, `TSSetType()`, `TSSetUp()`, `TSDestroy()`, `TSSetProblemType()` 583293b34091SDebojyoti Ghosh @*/ 5833d71ae5a4SJacob Faibussowitsch PetscErrorCode TSClone(TS tsin, TS *tsout) 5834d71ae5a4SJacob Faibussowitsch { 583593b34091SDebojyoti Ghosh TS t; 5836dc846ba4SSatish Balay SNES snes_start; 5837dc846ba4SSatish Balay DM dm; 5838dc846ba4SSatish Balay TSType type; 583993b34091SDebojyoti Ghosh 584093b34091SDebojyoti Ghosh PetscFunctionBegin; 584193b34091SDebojyoti Ghosh PetscValidPointer(tsin, 1); 584293b34091SDebojyoti Ghosh *tsout = NULL; 584393b34091SDebojyoti Ghosh 58449566063dSJacob Faibussowitsch PetscCall(PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView)); 584593b34091SDebojyoti Ghosh 584693b34091SDebojyoti Ghosh /* General TS description */ 584793b34091SDebojyoti Ghosh t->numbermonitors = 0; 5848d0c080abSJoseph Pusztay t->monitorFrequency = 1; 584993b34091SDebojyoti Ghosh t->setupcalled = 0; 585093b34091SDebojyoti Ghosh t->ksp_its = 0; 585193b34091SDebojyoti Ghosh t->snes_its = 0; 585293b34091SDebojyoti Ghosh t->nwork = 0; 58537d51462cSStefano Zampini t->rhsjacobian.time = PETSC_MIN_REAL; 585493b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 585593b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 585693b34091SDebojyoti Ghosh 58579566063dSJacob Faibussowitsch PetscCall(TSGetSNES(tsin, &snes_start)); 58589566063dSJacob Faibussowitsch PetscCall(TSSetSNES(t, snes_start)); 5859d15a3a53SEmil Constantinescu 58609566063dSJacob Faibussowitsch PetscCall(TSGetDM(tsin, &dm)); 58619566063dSJacob Faibussowitsch PetscCall(TSSetDM(t, dm)); 586293b34091SDebojyoti Ghosh 586393b34091SDebojyoti Ghosh t->adapt = tsin->adapt; 58649566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)t->adapt)); 586593b34091SDebojyoti Ghosh 5866e7069c78SShri t->trajectory = tsin->trajectory; 58679566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)t->trajectory)); 5868e7069c78SShri 5869e7069c78SShri t->event = tsin->event; 58706b10a48eSSatish Balay if (t->event) t->event->refct++; 5871e7069c78SShri 587293b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 587393b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 5874e7069c78SShri t->ptime_prev = tsin->ptime_prev; 587593b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 587693b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 587793b34091SDebojyoti Ghosh t->steps = tsin->steps; 587893b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 587993b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 588093b34091SDebojyoti Ghosh t->atol = tsin->atol; 588193b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 588293b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 588393b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 588493b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 588593b34091SDebojyoti Ghosh 58869566063dSJacob Faibussowitsch PetscCall(TSGetType(tsin, &type)); 58879566063dSJacob Faibussowitsch PetscCall(TSSetType(t, type)); 588893b34091SDebojyoti Ghosh 588993b34091SDebojyoti Ghosh t->vec_sol = NULL; 589093b34091SDebojyoti Ghosh 589193b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 589293b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 589393b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 589493b34091SDebojyoti Ghosh 58959566063dSJacob Faibussowitsch PetscCall(PetscMemcpy(t->ops, tsin->ops, sizeof(struct _TSOps))); 589693b34091SDebojyoti Ghosh 58970d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 58980d4fed19SBarry Smith PetscInt i; 58999566063dSJacob Faibussowitsch PetscCall(PetscMalloc((10) * sizeof(void (*)(void)), &((PetscObject)t)->fortran_func_pointers)); 5900ad540459SPierre Jolivet for (i = 0; i < 10; i++) ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 59010d4fed19SBarry Smith } 590293b34091SDebojyoti Ghosh *tsout = t; 59033ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 590493b34091SDebojyoti Ghosh } 5905f3b1f45cSBarry Smith 5906d71ae5a4SJacob Faibussowitsch static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void *ctx, Vec x, Vec y) 5907d71ae5a4SJacob Faibussowitsch { 5908f3b1f45cSBarry Smith TS ts = (TS)ctx; 5909f3b1f45cSBarry Smith 5910f3b1f45cSBarry Smith PetscFunctionBegin; 59119566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts, 0, x, y)); 59123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5913f3b1f45cSBarry Smith } 5914f3b1f45cSBarry Smith 5915f3b1f45cSBarry Smith /*@ 5916bcf0153eSBarry Smith TSRHSJacobianTest - Compares the multiply routine provided to the `MATSHELL` with differencing on the `TS` given RHS function. 5917f3b1f45cSBarry Smith 5918c3339decSBarry Smith Logically Collective 5919f3b1f45cSBarry Smith 5920f3b1f45cSBarry Smith Input Parameters: 5921f3b1f45cSBarry Smith TS - the time stepping routine 5922f3b1f45cSBarry Smith 5923f3b1f45cSBarry Smith Output Parameter: 5924bcf0153eSBarry Smith . flg - `PETSC_TRUE` if the multiply is likely correct 5925f3b1f45cSBarry Smith 5926bcf0153eSBarry Smith Options Database Key: 5927f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator 5928f3b1f45cSBarry Smith 5929f3b1f45cSBarry Smith Level: advanced 5930f3b1f45cSBarry Smith 5931bcf0153eSBarry Smith Note: 5932*195e9b02SBarry Smith This only works for problems defined using `TSSetRHSFunction()` and Jacobian NOT `TSSetIFunction()` and Jacobian 5933f3b1f45cSBarry Smith 5934bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `Mat`, `MATSHELL`, `MatCreateShell()`, `MatShellGetContext()`, `MatShellGetOperation()`, `MatShellTestMultTranspose()`, `TSRHSJacobianTestTranspose()` 5935f3b1f45cSBarry Smith @*/ 5936d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRHSJacobianTest(TS ts, PetscBool *flg) 5937d71ae5a4SJacob Faibussowitsch { 5938f3b1f45cSBarry Smith Mat J, B; 5939f3b1f45cSBarry Smith TSRHSJacobian func; 5940f3b1f45cSBarry Smith void *ctx; 5941f3b1f45cSBarry Smith 5942f3b1f45cSBarry Smith PetscFunctionBegin; 59439566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts, &J, &B, &func, &ctx)); 59449566063dSJacob Faibussowitsch PetscCall((*func)(ts, 0.0, ts->vec_sol, J, B, ctx)); 59459566063dSJacob Faibussowitsch PetscCall(MatShellTestMult(J, RHSWrapperFunction_TSRHSJacobianTest, ts->vec_sol, ts, flg)); 59463ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5947f3b1f45cSBarry Smith } 5948f3b1f45cSBarry Smith 5949f3b1f45cSBarry Smith /*@C 5950bcf0153eSBarry Smith TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the `MATSHELL` with differencing on the `TS` given RHS function. 5951f3b1f45cSBarry Smith 5952c3339decSBarry Smith Logically Collective 5953f3b1f45cSBarry Smith 5954f3b1f45cSBarry Smith Input Parameters: 5955f3b1f45cSBarry Smith TS - the time stepping routine 5956f3b1f45cSBarry Smith 5957f3b1f45cSBarry Smith Output Parameter: 5958bcf0153eSBarry Smith . flg - `PETSC_TRUE` if the multiply is likely correct 5959f3b1f45cSBarry Smith 5960bcf0153eSBarry Smith Options Database Key: 5961f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator 5962f3b1f45cSBarry Smith 5963bcf0153eSBarry Smith Level: advanced 5964bcf0153eSBarry Smith 596595452b02SPatrick Sanan Notes: 5966*195e9b02SBarry Smith This only works for problems defined using `TSSetRHSFunction()` and Jacobian NOT `TSSetIFunction()` and Jacobian 5967f3b1f45cSBarry Smith 5968bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `Mat`, `MatCreateShell()`, `MatShellGetContext()`, `MatShellGetOperation()`, `MatShellTestMultTranspose()`, `TSRHSJacobianTest()` 5969f3b1f45cSBarry Smith @*/ 5970d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRHSJacobianTestTranspose(TS ts, PetscBool *flg) 5971d71ae5a4SJacob Faibussowitsch { 5972f3b1f45cSBarry Smith Mat J, B; 5973f3b1f45cSBarry Smith void *ctx; 5974f3b1f45cSBarry Smith TSRHSJacobian func; 5975f3b1f45cSBarry Smith 5976f3b1f45cSBarry Smith PetscFunctionBegin; 59779566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts, &J, &B, &func, &ctx)); 59789566063dSJacob Faibussowitsch PetscCall((*func)(ts, 0.0, ts->vec_sol, J, B, ctx)); 59799566063dSJacob Faibussowitsch PetscCall(MatShellTestMultTranspose(J, RHSWrapperFunction_TSRHSJacobianTest, ts->vec_sol, ts, flg)); 59803ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5981f3b1f45cSBarry Smith } 59820fe4d17eSHong Zhang 59830fe4d17eSHong Zhang /*@ 59840fe4d17eSHong Zhang TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used. 59850fe4d17eSHong Zhang 59860fe4d17eSHong Zhang Logically collective 59870fe4d17eSHong Zhang 5988d8d19677SJose E. Roman Input Parameters: 59890fe4d17eSHong Zhang + ts - timestepping context 5990bcf0153eSBarry Smith - use_splitrhsfunction - `PETSC_TRUE` indicates that the split RHSFunction will be used 59910fe4d17eSHong Zhang 5992bcf0153eSBarry Smith Options Database Key: 59930fe4d17eSHong Zhang . -ts_use_splitrhsfunction - <true,false> 59940fe4d17eSHong Zhang 59950fe4d17eSHong Zhang Level: intermediate 59960fe4d17eSHong Zhang 5997bcf0153eSBarry Smith Note: 5998*195e9b02SBarry Smith This is only for multirate methods 5999bcf0153eSBarry Smith 6000bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetUseSplitRHSFunction()` 60010fe4d17eSHong Zhang @*/ 6002d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction) 6003d71ae5a4SJacob Faibussowitsch { 60040fe4d17eSHong Zhang PetscFunctionBegin; 60050fe4d17eSHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 60060fe4d17eSHong Zhang ts->use_splitrhsfunction = use_splitrhsfunction; 60073ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 60080fe4d17eSHong Zhang } 60090fe4d17eSHong Zhang 60100fe4d17eSHong Zhang /*@ 60110fe4d17eSHong Zhang TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used. 60120fe4d17eSHong Zhang 60130fe4d17eSHong Zhang Not collective 60140fe4d17eSHong Zhang 60150fe4d17eSHong Zhang Input Parameter: 60160fe4d17eSHong Zhang . ts - timestepping context 60170fe4d17eSHong Zhang 60180fe4d17eSHong Zhang Output Parameter: 6019bcf0153eSBarry Smith . use_splitrhsfunction - `PETSC_TRUE` indicates that the split RHSFunction will be used 60200fe4d17eSHong Zhang 60210fe4d17eSHong Zhang Level: intermediate 60220fe4d17eSHong Zhang 6023bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetUseSplitRHSFunction()` 60240fe4d17eSHong Zhang @*/ 6025d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction) 6026d71ae5a4SJacob Faibussowitsch { 60270fe4d17eSHong Zhang PetscFunctionBegin; 60280fe4d17eSHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 60290fe4d17eSHong Zhang *use_splitrhsfunction = ts->use_splitrhsfunction; 60303ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 60310fe4d17eSHong Zhang } 6032d60b7d5cSBarry Smith 6033d60b7d5cSBarry Smith /*@ 6034d60b7d5cSBarry Smith TSSetMatStructure - sets the relationship between the nonzero structure of the RHS Jacobian matrix to the IJacobian matrix. 6035d60b7d5cSBarry Smith 6036c3339decSBarry Smith Logically Collective 6037d60b7d5cSBarry Smith 6038d60b7d5cSBarry Smith Input Parameters: 6039d60b7d5cSBarry Smith + ts - the time-stepper 6040bcf0153eSBarry Smith - str - the structure (the default is `UNKNOWN_NONZERO_PATTERN`) 6041d60b7d5cSBarry Smith 6042d60b7d5cSBarry Smith Level: intermediate 6043d60b7d5cSBarry Smith 6044bcf0153eSBarry Smith Note: 6045d60b7d5cSBarry Smith When the relationship between the nonzero structures is known and supplied the solution process can be much faster 6046d60b7d5cSBarry Smith 6047bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `MatAXPY()`, `MatStructure` 6048d60b7d5cSBarry Smith @*/ 6049d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMatStructure(TS ts, MatStructure str) 6050d71ae5a4SJacob Faibussowitsch { 6051d60b7d5cSBarry Smith PetscFunctionBegin; 6052d60b7d5cSBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 6053d60b7d5cSBarry Smith ts->axpy_pattern = str; 60543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6055d60b7d5cSBarry Smith } 60564a658b32SHong Zhang 60574a658b32SHong Zhang /*@ 6058bcf0153eSBarry Smith TSSetTimeSpan - sets the time span. The solution will be computed and stored for each time requested in the span 60594a658b32SHong Zhang 6060c3339decSBarry Smith Collective 60614a658b32SHong Zhang 60624a658b32SHong Zhang Input Parameters: 60634a658b32SHong Zhang + ts - the time-stepper 60644a658b32SHong Zhang . n - number of the time points (>=2) 60654a658b32SHong Zhang - span_times - array of the time points. The first element and the last element are the initial time and the final time respectively. 60664a658b32SHong Zhang 6067bcf0153eSBarry Smith Options Database Key: 60684a658b32SHong Zhang . -ts_time_span <t0,...tf> - Sets the time span 60694a658b32SHong Zhang 6070bcf0153eSBarry Smith Level: intermediate 60714a658b32SHong Zhang 60724a658b32SHong Zhang Notes: 60734a658b32SHong Zhang The elements in tspan must be all increasing. They correspond to the intermediate points for time integration. 6074bcf0153eSBarry Smith `TS_EXACTFINALTIME_MATCHSTEP` must be used to make the last time step in each sub-interval match the intermediate points specified. 6075bcf0153eSBarry Smith The intermediate solutions are saved in a vector array that can be accessed with `TSGetTimeSpanSolutions()`. Thus using time span may 60764a658b32SHong Zhang pressure the memory system when using a large number of span points. 60774a658b32SHong Zhang 6078bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetTimeSpan()`, `TSGetTimeSpanSolutions()` 60794a658b32SHong Zhang @*/ 6080d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTimeSpan(TS ts, PetscInt n, PetscReal *span_times) 6081d71ae5a4SJacob Faibussowitsch { 60824a658b32SHong Zhang PetscFunctionBegin; 60834a658b32SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 608463a3b9bcSJacob Faibussowitsch PetscCheck(n >= 2, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Minimum time span size is 2 but %" PetscInt_FMT " is provided", n); 60854a658b32SHong Zhang if (ts->tspan && n != ts->tspan->num_span_times) { 60864a658b32SHong Zhang PetscCall(PetscFree(ts->tspan->span_times)); 60874a658b32SHong Zhang PetscCall(VecDestroyVecs(ts->tspan->num_span_times, &ts->tspan->vecs_sol)); 60884a658b32SHong Zhang PetscCall(PetscMalloc1(n, &ts->tspan->span_times)); 60894a658b32SHong Zhang } 60904a658b32SHong Zhang if (!ts->tspan) { 60914a658b32SHong Zhang TSTimeSpan tspan; 60924a658b32SHong Zhang PetscCall(PetscNew(&tspan)); 60934a658b32SHong Zhang PetscCall(PetscMalloc1(n, &tspan->span_times)); 6094e1db57b0SHong Zhang tspan->reltol = 1e-6; 6095e1db57b0SHong Zhang tspan->abstol = 10 * PETSC_MACHINE_EPSILON; 60964a658b32SHong Zhang ts->tspan = tspan; 60974a658b32SHong Zhang } 60984a658b32SHong Zhang ts->tspan->num_span_times = n; 60994a658b32SHong Zhang PetscCall(PetscArraycpy(ts->tspan->span_times, span_times, n)); 61004a658b32SHong Zhang PetscCall(TSSetTime(ts, ts->tspan->span_times[0])); 61014a658b32SHong Zhang PetscCall(TSSetMaxTime(ts, ts->tspan->span_times[n - 1])); 61023ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 61034a658b32SHong Zhang } 61044a658b32SHong Zhang 61054a658b32SHong Zhang /*@C 6106*195e9b02SBarry Smith TSGetTimeSpan - gets the time span set with `TSSetTimeSpan()` 61074a658b32SHong Zhang 61084a658b32SHong Zhang Not Collective 61094a658b32SHong Zhang 61104a658b32SHong Zhang Input Parameter: 61114a658b32SHong Zhang . ts - the time-stepper 61124a658b32SHong Zhang 61134a658b32SHong Zhang Output Parameters: 61144a658b32SHong Zhang + n - number of the time points (>=2) 6115bcf0153eSBarry Smith - span_times - array of the time points. The first element and the last element are the initial time and the final time respectively. 61164a658b32SHong Zhang 61174a658b32SHong Zhang Level: beginner 61184a658b32SHong Zhang 6119bcf0153eSBarry Smith Note: 6120*195e9b02SBarry Smith The values obtained are valid until the `TS` object is destroyed. 6121*195e9b02SBarry Smith 6122*195e9b02SBarry Smith Both `n` and `span_times` can be `NULL`. 6123bcf0153eSBarry Smith 6124bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetTimeSpan()`, `TSGetTimeSpanSolutions()` 61254a658b32SHong Zhang @*/ 6126d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeSpan(TS ts, PetscInt *n, const PetscReal **span_times) 6127d71ae5a4SJacob Faibussowitsch { 61284a658b32SHong Zhang PetscFunctionBegin; 61294a658b32SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 61304a658b32SHong Zhang if (n) PetscValidIntPointer(n, 2); 61314a658b32SHong Zhang if (span_times) PetscValidPointer(span_times, 3); 61324a658b32SHong Zhang if (!ts->tspan) { 61334a658b32SHong Zhang if (n) *n = 0; 61344a658b32SHong Zhang if (span_times) *span_times = NULL; 61354a658b32SHong Zhang } else { 61364a658b32SHong Zhang if (n) *n = ts->tspan->num_span_times; 61374a658b32SHong Zhang if (span_times) *span_times = ts->tspan->span_times; 61384a658b32SHong Zhang } 61393ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 61404a658b32SHong Zhang } 61414a658b32SHong Zhang 61424a658b32SHong Zhang /*@ 61434a658b32SHong Zhang TSGetTimeSpanSolutions - Get the number of solutions and the solutions at the time points specified by the time span. 61444a658b32SHong Zhang 61454a658b32SHong Zhang Input Parameter: 6146bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 61474a658b32SHong Zhang 61484a658b32SHong Zhang Output Parameters: 61494a658b32SHong Zhang + nsol - the number of solutions 61504a658b32SHong Zhang - Sols - the solution vectors 61514a658b32SHong Zhang 6152bcf0153eSBarry Smith Level: intermediate 61534a658b32SHong Zhang 615440bd4cedSHong Zhang Notes: 6155*195e9b02SBarry Smith Both `nsol` and `Sols` can be `NULL`. 61564a658b32SHong Zhang 6157*195e9b02SBarry Smith Some time points in the time span may be skipped by `TS` so that `nsol` is less than the number of points specified by `TSSetTimeSpan()`. 6158bcf0153eSBarry Smith For example, manipulating the step size, especially with a reduced precision, may cause `TS` to step over certain points in the span. 6159bcf0153eSBarry Smith 6160bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetTimeSpan()` 61614a658b32SHong Zhang @*/ 6162d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeSpanSolutions(TS ts, PetscInt *nsol, Vec **Sols) 6163d71ae5a4SJacob Faibussowitsch { 61644a658b32SHong Zhang PetscFunctionBegin; 61654a658b32SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 61664a658b32SHong Zhang if (nsol) PetscValidIntPointer(nsol, 2); 61674a658b32SHong Zhang if (Sols) PetscValidPointer(Sols, 3); 61684a658b32SHong Zhang if (!ts->tspan) { 61694a658b32SHong Zhang if (nsol) *nsol = 0; 61704a658b32SHong Zhang if (Sols) *Sols = NULL; 61714a658b32SHong Zhang } else { 617240bd4cedSHong Zhang if (nsol) *nsol = ts->tspan->spanctr; 61734a658b32SHong Zhang if (Sols) *Sols = ts->tspan->vecs_sol; 61744a658b32SHong Zhang } 61753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 61764a658b32SHong Zhang } 6177d7cfae9bSHong Zhang 6178d7cfae9bSHong Zhang /*@C 61797b2dc123SHong Zhang TSPruneIJacobianColor - Remove nondiagonal zeros in the Jacobian matrix and update the `MatMFFD` coloring information. 6180d7cfae9bSHong Zhang 6181*195e9b02SBarry Smith Collective 6182d7cfae9bSHong Zhang 6183d7cfae9bSHong Zhang Input Parameters: 6184*195e9b02SBarry Smith + ts - the `TS` context 6185d7cfae9bSHong Zhang . J - Jacobian matrix (not altered in this routine) 6186*195e9b02SBarry Smith - B - newly computed Jacobian matrix to use with preconditioner 6187d7cfae9bSHong Zhang 6188d7cfae9bSHong Zhang Level: intermediate 6189d7cfae9bSHong Zhang 6190d7cfae9bSHong Zhang Notes: 6191*195e9b02SBarry Smith This function improves the `MatFDColoring` performance when the Jacobian matrix was over-allocated or contains 6192*195e9b02SBarry Smith many constant zeros entries, which is typically the case when the matrix is generated by a `DM` 6193d7cfae9bSHong Zhang and multiple fields are involved. 6194d7cfae9bSHong Zhang 6195d7cfae9bSHong Zhang Users need to make sure that the Jacobian matrix is properly filled to reflect the sparsity 6196*195e9b02SBarry Smith structure. For `MatFDColoring`, the values of nonzero entries are not important. So one can 61977b2dc123SHong Zhang usually call `TSComputeIJacobian()` with randomized input vectors to generate a dummy Jacobian. 61987b2dc123SHong Zhang `TSComputeIJacobian()` should be called before `TSSolve()` but after `TSSetUp()`. 6199d7cfae9bSHong Zhang 6200*195e9b02SBarry Smith .seealso: [](chapter_ts), `TS`, `MatFDColoring`, `TSComputeIJacobianDefaultColor()`, `MatEliminateZeros()`, `MatFDColoringCreate()`, `MatFDColoringSetFunction()` 6201d7cfae9bSHong Zhang @*/ 6202d7cfae9bSHong Zhang PetscErrorCode TSPruneIJacobianColor(TS ts, Mat J, Mat B) 6203d7cfae9bSHong Zhang { 6204d7cfae9bSHong Zhang MatColoring mc = NULL; 6205d7cfae9bSHong Zhang ISColoring iscoloring = NULL; 6206d7cfae9bSHong Zhang MatFDColoring matfdcoloring = NULL; 6207d7cfae9bSHong Zhang 6208d7cfae9bSHong Zhang PetscFunctionBegin; 6209d7cfae9bSHong Zhang /* Generate new coloring after eliminating zeros in the matrix */ 6210d7cfae9bSHong Zhang PetscCall(MatEliminateZeros(B)); 6211d7cfae9bSHong Zhang PetscCall(MatColoringCreate(B, &mc)); 6212d7cfae9bSHong Zhang PetscCall(MatColoringSetDistance(mc, 2)); 6213d7cfae9bSHong Zhang PetscCall(MatColoringSetType(mc, MATCOLORINGSL)); 6214d7cfae9bSHong Zhang PetscCall(MatColoringSetFromOptions(mc)); 6215d7cfae9bSHong Zhang PetscCall(MatColoringApply(mc, &iscoloring)); 6216d7cfae9bSHong Zhang PetscCall(MatColoringDestroy(&mc)); 6217d7cfae9bSHong Zhang /* Replace the old coloring with the new one */ 6218d7cfae9bSHong Zhang PetscCall(MatFDColoringCreate(B, iscoloring, &matfdcoloring)); 6219d7cfae9bSHong Zhang PetscCall(MatFDColoringSetFunction(matfdcoloring, (PetscErrorCode(*)(void))SNESTSFormFunction, (void *)ts)); 6220d7cfae9bSHong Zhang PetscCall(MatFDColoringSetFromOptions(matfdcoloring)); 6221d7cfae9bSHong Zhang PetscCall(MatFDColoringSetUp(B, iscoloring, matfdcoloring)); 6222d7cfae9bSHong Zhang PetscCall(PetscObjectCompose((PetscObject)B, "TSMatFDColoring", (PetscObject)matfdcoloring)); 6223d7cfae9bSHong Zhang PetscCall(PetscObjectDereference((PetscObject)matfdcoloring)); 6224d7cfae9bSHong Zhang PetscCall(ISColoringDestroy(&iscoloring)); 62253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6226d7cfae9bSHong Zhang } 6227