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)); 232ffb9264SLisandro Dalcin PetscFunctionReturn(0); 242ffb9264SLisandro Dalcin } 252ffb9264SLisandro Dalcin 26bdad233fSMatthew Knepley /*@ 27bcf0153eSBarry Smith TSSetFromOptions - Sets various `TS` parameters from user options. 28bdad233fSMatthew Knepley 29bcf0153eSBarry Smith Collective on ts 30bdad233fSMatthew Knepley 31bdad233fSMatthew Knepley Input Parameter: 32bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 33bdad233fSMatthew Knepley 34bdad233fSMatthew Knepley Options Database Keys: 35bcf0153eSBarry Smith + -ts_type <type> - EULER, BEULER, SUNDIALS, PSEUDO, CN, RK, THETA, ALPHA, GLLE, SSP, GLEE, BSYMP, IRK 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 414dc72f7fSBarry 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 5167b8a455SSatish Balay . -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 6763a3b9bcSJacob Faibussowitsch . -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03" PetscInt_FMT ".vts (filename-%%03" PetscInt_FMT ".vtu) 689e336e28SPatrick Sanan - -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 6953ea634cSHong Zhang 70bcf0153eSBarry Smith Level: beginner 713d5a8a6aSBarry Smith 72bcf0153eSBarry Smith Notes: 73bcf0153eSBarry Smith See `SNESSetFromOptions()` and `KSPSetFromOptions()` for how to control the nonlinear and linear solves used by the time-stepper. 74bcf0153eSBarry Smith 75bcf0153eSBarry Smith Certain `SNES` options get reset for each new nonlinear solver, for example -snes_lag_jacobian <its> and -snes_lag_preconditioner <its>, in order 763d5a8a6aSBarry Smith to retain them over the multiple nonlinear solves that TS uses you mush also provide -snes_lag_jacobian_persists true and 773d5a8a6aSBarry Smith -snes_lag_preconditioner_persists true 783d5a8a6aSBarry Smith 799e336e28SPatrick Sanan Developer Note: 809e336e28SPatrick Sanan We should unify all the -ts_monitor options in the way that -xxx_view has been unified 81bdad233fSMatthew Knepley 82bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetType()` 83bdad233fSMatthew Knepley @*/ 84d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetFromOptions(TS ts) 85d71ae5a4SJacob Faibussowitsch { 86bc952696SBarry Smith PetscBool opt, flg, tflg; 87eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 884a658b32SHong Zhang PetscReal time_step, tspan[100]; 894a658b32SHong Zhang PetscInt nt = PETSC_STATIC_ARRAY_LENGTH(tspan); 9049354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 91d1212d36SBarry Smith char dir[16]; 92cd11d68dSLisandro Dalcin TSIFunction ifun; 936991f827SBarry Smith const char *defaultType; 946991f827SBarry Smith char typeName[256]; 95bdad233fSMatthew Knepley 96bdad233fSMatthew Knepley PetscFunctionBegin; 970700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 986991f827SBarry Smith 999566063dSJacob Faibussowitsch PetscCall(TSRegisterAll()); 1009566063dSJacob Faibussowitsch PetscCall(TSGetIFunction(ts, NULL, &ifun, NULL)); 101cd11d68dSLisandro Dalcin 102d0609cedSBarry Smith PetscObjectOptionsBegin((PetscObject)ts); 1031ef27442SStefano Zampini if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 1041ef27442SStefano Zampini else defaultType = ifun ? TSBEULER : TSEULER; 1059566063dSJacob Faibussowitsch PetscCall(PetscOptionsFList("-ts_type", "TS method", "TSSetType", TSList, defaultType, typeName, 256, &opt)); 1061e66621cSBarry Smith if (opt) PetscCall(TSSetType(ts, typeName)); 1071e66621cSBarry Smith else PetscCall(TSSetType(ts, defaultType)); 108bdad233fSMatthew Knepley 109bdad233fSMatthew Knepley /* Handle generic TS options */ 1109566063dSJacob Faibussowitsch PetscCall(PetscOptionsDeprecated("-ts_final_time", "-ts_max_time", "3.10", NULL)); 1119566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_max_time", "Maximum time to run to", "TSSetMaxTime", ts->max_time, &ts->max_time, NULL)); 1124a658b32SHong Zhang PetscCall(PetscOptionsRealArray("-ts_time_span", "Time span", "TSSetTimeSpan", tspan, &nt, &flg)); 1134a658b32SHong Zhang if (flg) PetscCall(TSSetTimeSpan(ts, nt, tspan)); 1149566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_max_steps", "Maximum number of time steps", "TSSetMaxSteps", ts->max_steps, &ts->max_steps, NULL)); 1159566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_init_time", "Initial time", "TSSetTime", ts->ptime, &ts->ptime, NULL)); 1169566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_dt", "Initial time step", "TSSetTimeStep", ts->time_step, &time_step, &flg)); 1179566063dSJacob Faibussowitsch if (flg) PetscCall(TSSetTimeStep(ts, time_step)); 1189566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum("-ts_exact_final_time", "Option for handling of final time step", "TSSetExactFinalTime", TSExactFinalTimeOptions, (PetscEnum)ts->exact_final_time, (PetscEnum *)&eftopt, &flg)); 1199566063dSJacob Faibussowitsch if (flg) PetscCall(TSSetExactFinalTime(ts, eftopt)); 1209566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_max_snes_failures", "Maximum number of nonlinear solve failures", "TSSetMaxSNESFailures", ts->max_snes_failures, &ts->max_snes_failures, NULL)); 1219566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_max_reject", "Maximum number of step rejections before step fails", "TSSetMaxStepRejections", ts->max_reject, &ts->max_reject, NULL)); 1229566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_error_if_step_fails", "Error if no step succeeds", "TSSetErrorIfStepFails", ts->errorifstepfailed, &ts->errorifstepfailed, NULL)); 1239566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_rtol", "Relative tolerance for local truncation error", "TSSetTolerances", ts->rtol, &ts->rtol, NULL)); 1249566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_atol", "Absolute tolerance for local truncation error", "TSSetTolerances", ts->atol, &ts->atol, NULL)); 125bdad233fSMatthew Knepley 1269566063dSJacob 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)); 1279566063dSJacob 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)); 1289566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_use_splitrhsfunction", "Use the split RHS function for multirate solvers ", "TSSetUseSplitRHSFunction", ts->use_splitrhsfunction, &ts->use_splitrhsfunction, NULL)); 12956f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 13056f85f32SBarry Smith { 13156f85f32SBarry Smith PetscBool set; 13256f85f32SBarry Smith flg = PETSC_FALSE; 1339566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_saws_block", "Block for SAWs memory snooper at end of TSSolve", "PetscObjectSAWsBlock", ((PetscObject)ts)->amspublishblock, &flg, &set)); 1341baa6e33SBarry Smith if (set) PetscCall(PetscObjectSAWsSetBlock((PetscObject)ts, flg)); 13556f85f32SBarry Smith } 13656f85f32SBarry Smith #endif 13756f85f32SBarry Smith 138bdad233fSMatthew Knepley /* Monitor options */ 1399566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_frequency", "Number of time steps between monitor output", "TSMonitorSetFrequency", ts->monitorFrequency, &ts->monitorFrequency, NULL)); 1409566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor", "Monitor time and timestep size", "TSMonitorDefault", TSMonitorDefault, NULL)); 1419566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor_extreme", "Monitor extreme values of the solution", "TSMonitorExtreme", TSMonitorExtreme, NULL)); 1429566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor_solution", "View the solution at each timestep", "TSMonitorSolution", TSMonitorSolution, NULL)); 1439566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_dmswarm_monitor_moments", "Monitor moments of particle distribution", "TSDMSwarmMonitorMoments", TSDMSwarmMonitorMoments, NULL)); 144fde5950dSBarry Smith 1459566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_python", "Use Python function", "TSMonitorSet", NULL, monfilename, sizeof(monfilename), &flg)); 1469566063dSJacob Faibussowitsch if (flg) PetscCall(PetscPythonMonitorSet((PetscObject)ts, monfilename)); 1475180491cSLisandro Dalcin 1489566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_solution", "Monitor solution graphically", "TSMonitorLGSolution", &opt)); 149b3603a34SBarry Smith if (opt) { 1503923b477SBarry Smith PetscInt howoften = 1; 151e669de00SBarry Smith DM dm; 152e669de00SBarry Smith PetscBool net; 153b3603a34SBarry Smith 1549566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_solution", "Monitor solution graphically", "TSMonitorLGSolution", howoften, &howoften, NULL)); 1559566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 1569566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)dm, DMNETWORK, &net)); 157e669de00SBarry Smith if (net) { 158e669de00SBarry Smith TSMonitorLGCtxNetwork ctx; 1599566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxNetworkCreate(ts, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 600, 400, howoften, &ctx)); 1609566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGCtxNetworkSolution, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxNetworkDestroy)); 1619566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_lg_solution_semilogy", "Plot the solution with a semi-log axis", "", ctx->semilogy, &ctx->semilogy, NULL)); 162e669de00SBarry Smith } else { 163e669de00SBarry Smith TSMonitorLGCtx ctx; 1649566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 1659566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGSolution, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy)); 166bdad233fSMatthew Knepley } 167e669de00SBarry Smith } 1686ba87a44SLisandro Dalcin 1699566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_error", "Monitor error graphically", "TSMonitorLGError", &opt)); 170ef20d060SBarry Smith if (opt) { 1710b039ecaSBarry Smith TSMonitorLGCtx ctx; 1723923b477SBarry Smith PetscInt howoften = 1; 173ef20d060SBarry Smith 1749566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_error", "Monitor error graphically", "TSMonitorLGError", howoften, &howoften, NULL)); 1759566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 1769566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGError, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy)); 177ef20d060SBarry Smith } 1789566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor_error", "View the error at each timestep", "TSMonitorError", TSMonitorError, NULL)); 1797cf37e64SBarry Smith 1809566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_timestep", "Monitor timestep size graphically", "TSMonitorLGTimeStep", &opt)); 1816934998bSLisandro Dalcin if (opt) { 1826934998bSLisandro Dalcin TSMonitorLGCtx ctx; 1836934998bSLisandro Dalcin PetscInt howoften = 1; 1846934998bSLisandro Dalcin 1859566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_timestep", "Monitor timestep size graphically", "TSMonitorLGTimeStep", howoften, &howoften, NULL)); 1869566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 1879566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGTimeStep, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy)); 1886934998bSLisandro Dalcin } 1899566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_timestep_log", "Monitor log timestep size graphically", "TSMonitorLGTimeStep", &opt)); 1908b668821SLisandro Dalcin if (opt) { 1918b668821SLisandro Dalcin TSMonitorLGCtx ctx; 1928b668821SLisandro Dalcin PetscInt howoften = 1; 1938b668821SLisandro Dalcin 1949566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_timestep_log", "Monitor log timestep size graphically", "TSMonitorLGTimeStep", howoften, &howoften, NULL)); 1959566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 1969566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGTimeStep, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy)); 1978b668821SLisandro Dalcin ctx->semilogy = PETSC_TRUE; 1988b668821SLisandro Dalcin } 1998b668821SLisandro Dalcin 2009566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_snes_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGSNESIterations", &opt)); 201201da799SBarry Smith if (opt) { 202201da799SBarry Smith TSMonitorLGCtx ctx; 203201da799SBarry Smith PetscInt howoften = 1; 204201da799SBarry Smith 2059566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_snes_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGSNESIterations", howoften, &howoften, NULL)); 2069566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 2079566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGSNESIterations, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy)); 208201da799SBarry Smith } 2099566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_ksp_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGKSPIterations", &opt)); 210201da799SBarry Smith if (opt) { 211201da799SBarry Smith TSMonitorLGCtx ctx; 212201da799SBarry Smith PetscInt howoften = 1; 213201da799SBarry Smith 2149566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_ksp_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGKSPIterations", howoften, &howoften, NULL)); 2159566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 2169566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGKSPIterations, ctx, (PetscErrorCode(*)(void **))TSMonitorLGCtxDestroy)); 217201da799SBarry Smith } 2189566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_sp_eig", "Monitor eigenvalues of linearized operator graphically", "TSMonitorSPEig", &opt)); 2198189c53fSBarry Smith if (opt) { 2208189c53fSBarry Smith TSMonitorSPEigCtx ctx; 2218189c53fSBarry Smith PetscInt howoften = 1; 2228189c53fSBarry Smith 2239566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_sp_eig", "Monitor eigenvalues of linearized operator graphically", "TSMonitorSPEig", howoften, &howoften, NULL)); 2249566063dSJacob Faibussowitsch PetscCall(TSMonitorSPEigCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx)); 2259566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorSPEig, ctx, (PetscErrorCode(*)(void **))TSMonitorSPEigCtxDestroy)); 2268189c53fSBarry Smith } 2279566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_sp_swarm", "Display particle phase from the DMSwarm", "TSMonitorSPSwarm", &opt)); 2281b575b74SJoseph Pusztay if (opt) { 2291b575b74SJoseph Pusztay TSMonitorSPCtx ctx; 230d7462660SMatthew Knepley PetscInt howoften = 1, retain = 0; 2316a5217c0SMatthew G. Knepley PetscBool phase = PETSC_TRUE, create = PETSC_TRUE; 232d7462660SMatthew Knepley 2339371c9d4SSatish Balay for (PetscInt i = 0; i < ts->numbermonitors; ++i) 2349371c9d4SSatish Balay if (ts->monitor[i] == TSMonitorSPSwarmSolution) { 2359371c9d4SSatish Balay create = PETSC_FALSE; 2369371c9d4SSatish Balay break; 2379371c9d4SSatish Balay } 2386a5217c0SMatthew G. Knepley if (create) { 2399566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm", "Display particles phase from the DMSwarm", "TSMonitorSPSwarm", howoften, &howoften, NULL)); 2409566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm_retain", "Retain n points plotted to show trajectory, -1 for all points", "TSMonitorSPSwarm", retain, &retain, NULL)); 2419566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_sp_swarm_phase", "Plot in phase space rather than coordinate space", "TSMonitorSPSwarm", phase, &phase, NULL)); 2429566063dSJacob Faibussowitsch PetscCall(TSMonitorSPCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, retain, phase, &ctx)); 2439566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode(*)(void **))TSMonitorSPCtxDestroy)); 2441b575b74SJoseph Pusztay } 2456a5217c0SMatthew G. Knepley } 246ef20d060SBarry Smith opt = PETSC_FALSE; 2479566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution", "Monitor solution graphically", "TSMonitorDrawSolution", &opt)); 248a7cc72afSBarry Smith if (opt) { 24983a4ac43SBarry Smith TSMonitorDrawCtx ctx; 25083a4ac43SBarry Smith PetscInt howoften = 1; 251a80ad3e0SBarry Smith 2529566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_solution", "Monitor solution graphically", "TSMonitorDrawSolution", howoften, &howoften, NULL)); 2539566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, "Computed Solution", PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx)); 2549566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorDrawSolution, ctx, (PetscErrorCode(*)(void **))TSMonitorDrawCtxDestroy)); 255bdad233fSMatthew Knepley } 256fb1732b5SBarry Smith opt = PETSC_FALSE; 2579566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution_phase", "Monitor solution graphically", "TSMonitorDrawSolutionPhase", &opt)); 2582d5ee99bSBarry Smith if (opt) { 2592d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2602d5ee99bSBarry Smith PetscReal bounds[4]; 2612d5ee99bSBarry Smith PetscInt n = 4; 2622d5ee99bSBarry Smith PetscDraw draw; 2636934998bSLisandro Dalcin PetscDrawAxis axis; 2642d5ee99bSBarry Smith 2659566063dSJacob Faibussowitsch PetscCall(PetscOptionsRealArray("-ts_monitor_draw_solution_phase", "Monitor solution graphically", "TSMonitorDrawSolutionPhase", bounds, &n, NULL)); 2663c633725SBarry Smith PetscCheck(n == 4, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Must provide bounding box of phase field"); 2679566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, 1, &ctx)); 2689566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(ctx->viewer, 0, &draw)); 2699566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDrawAxis(ctx->viewer, 0, &axis)); 2709566063dSJacob Faibussowitsch PetscCall(PetscDrawAxisSetLimits(axis, bounds[0], bounds[2], bounds[1], bounds[3])); 2719566063dSJacob Faibussowitsch PetscCall(PetscDrawAxisSetLabels(axis, "Phase Diagram", "Variable 1", "Variable 2")); 2729566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorDrawSolutionPhase, ctx, (PetscErrorCode(*)(void **))TSMonitorDrawCtxDestroy)); 2732d5ee99bSBarry Smith } 2742d5ee99bSBarry Smith opt = PETSC_FALSE; 2759566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_error", "Monitor error graphically", "TSMonitorDrawError", &opt)); 2763a471f94SBarry Smith if (opt) { 27783a4ac43SBarry Smith TSMonitorDrawCtx ctx; 27883a4ac43SBarry Smith PetscInt howoften = 1; 2793a471f94SBarry Smith 2809566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_error", "Monitor error graphically", "TSMonitorDrawError", howoften, &howoften, NULL)); 2819566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, "Error", PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx)); 2829566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorDrawError, ctx, (PetscErrorCode(*)(void **))TSMonitorDrawCtxDestroy)); 2833a471f94SBarry Smith } 2840ed3bfb6SBarry Smith opt = PETSC_FALSE; 2859566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution_function", "Monitor solution provided by TSMonitorSetSolutionFunction() graphically", "TSMonitorDrawSolutionFunction", &opt)); 2860ed3bfb6SBarry Smith if (opt) { 2870ed3bfb6SBarry Smith TSMonitorDrawCtx ctx; 2880ed3bfb6SBarry Smith PetscInt howoften = 1; 2890ed3bfb6SBarry Smith 2909566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_solution_function", "Monitor solution provided by TSMonitorSetSolutionFunction() graphically", "TSMonitorDrawSolutionFunction", howoften, &howoften, NULL)); 2919566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, "Solution provided by user function", PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx)); 2929566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorDrawSolutionFunction, ctx, (PetscErrorCode(*)(void **))TSMonitorDrawCtxDestroy)); 2930ed3bfb6SBarry Smith } 294fde5950dSBarry Smith 295ed81e22dSJed Brown opt = PETSC_FALSE; 29663a3b9bcSJacob 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)); 297ed81e22dSJed Brown if (flg) { 298ed81e22dSJed Brown const char *ptr, *ptr2; 299ed81e22dSJed Brown char *filetemplate; 30063a3b9bcSJacob 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"); 301ed81e22dSJed Brown /* Do some cursory validation of the input. */ 3029566063dSJacob Faibussowitsch PetscCall(PetscStrstr(monfilename, "%", (char **)&ptr)); 30363a3b9bcSJacob Faibussowitsch PetscCheck(ptr, PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03" PetscInt_FMT ".vts"); 304ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 3059566063dSJacob Faibussowitsch PetscCall(PetscStrchr("DdiouxX", *ptr, (char **)&ptr2)); 30663a3b9bcSJacob 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"); 307ed81e22dSJed Brown if (ptr2) break; 308ed81e22dSJed Brown } 3099566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(monfilename, &filetemplate)); 3109566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorSolutionVTK, filetemplate, (PetscErrorCode(*)(void **))TSMonitorSolutionVTKDestroy)); 311ed81e22dSJed Brown } 312bdad233fSMatthew Knepley 3139566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_dmda_ray", "Display a ray of the solution", "None", "y=0", dir, sizeof(dir), &flg)); 314d1212d36SBarry Smith if (flg) { 315d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 316d1212d36SBarry Smith int ray = 0; 3173ee9839eSMatthew G. Knepley DMDirection ddir; 318d1212d36SBarry Smith DM da; 319d1212d36SBarry Smith PetscMPIInt rank; 320d1212d36SBarry Smith 3213c633725SBarry Smith PetscCheck(dir[1] == '=', PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Unknown ray %s", dir); 3223ee9839eSMatthew G. Knepley if (dir[0] == 'x') ddir = DM_X; 3233ee9839eSMatthew G. Knepley else if (dir[0] == 'y') ddir = DM_Y; 32498921bdaSJacob Faibussowitsch else SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Unknown ray %s", dir); 325d1212d36SBarry Smith sscanf(dir + 2, "%d", &ray); 326d1212d36SBarry Smith 3279566063dSJacob Faibussowitsch PetscCall(PetscInfo(((PetscObject)ts), "Displaying DMDA ray %c = %d\n", dir[0], ray)); 3289566063dSJacob Faibussowitsch PetscCall(PetscNew(&rayctx)); 3299566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &da)); 3309566063dSJacob Faibussowitsch PetscCall(DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter)); 3319566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)ts), &rank)); 3321e66621cSBarry Smith if (rank == 0) PetscCall(PetscViewerDrawOpen(PETSC_COMM_SELF, NULL, NULL, 0, 0, 600, 300, &rayctx->viewer)); 33351b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 3349566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorDMDARay, rayctx, TSMonitorDMDARayDestroy)); 335d1212d36SBarry Smith } 3369566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_lg_dmda_ray", "Display a ray of the solution", "None", "x=0", dir, sizeof(dir), &flg)); 33751b4a12fSMatthew G. Knepley if (flg) { 33851b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 33951b4a12fSMatthew G. Knepley int ray = 0; 3403ee9839eSMatthew G. Knepley DMDirection ddir; 34151b4a12fSMatthew G. Knepley DM da; 34251b4a12fSMatthew G. Knepley PetscInt howoften = 1; 343d1212d36SBarry Smith 3443c633725SBarry Smith PetscCheck(dir[1] == '=', PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 3453ee9839eSMatthew G. Knepley if (dir[0] == 'x') ddir = DM_X; 3463ee9839eSMatthew G. Knepley else if (dir[0] == 'y') ddir = DM_Y; 34798921bdaSJacob Faibussowitsch else SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 34851b4a12fSMatthew G. Knepley sscanf(dir + 2, "%d", &ray); 3491c3436cfSJed Brown 3509566063dSJacob Faibussowitsch PetscCall(PetscInfo(((PetscObject)ts), "Displaying LG DMDA ray %c = %d\n", dir[0], ray)); 3519566063dSJacob Faibussowitsch PetscCall(PetscNew(&rayctx)); 3529566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &da)); 3539566063dSJacob Faibussowitsch PetscCall(DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter)); 3549566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 600, 400, howoften, &rayctx->lgctx)); 3559566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy)); 35651b4a12fSMatthew G. Knepley } 357a7a1495cSBarry Smith 3589566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_envelope", "Monitor maximum and minimum value of each component of the solution", "TSMonitorEnvelope", &opt)); 359b3d3934dSBarry Smith if (opt) { 360b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 361b3d3934dSBarry Smith 3629566063dSJacob Faibussowitsch PetscCall(TSMonitorEnvelopeCtxCreate(ts, &ctx)); 3639566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorEnvelope, ctx, (PetscErrorCode(*)(void **))TSMonitorEnvelopeCtxDestroy)); 364b3d3934dSBarry Smith } 365aee7a9fbSMatthew G. Knepley flg = PETSC_FALSE; 3669566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_cancel", "Remove all monitors", "TSMonitorCancel", flg, &flg, &opt)); 3679566063dSJacob Faibussowitsch if (opt && flg) PetscCall(TSMonitorCancel(ts)); 368b3d3934dSBarry Smith 369847ff0e1SMatthew G. Knepley flg = PETSC_FALSE; 370d7cfae9bSHong Zhang PetscCall(PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeIJacobianDefaultColor", flg, &flg, NULL)); 371847ff0e1SMatthew G. Knepley if (flg) { 372847ff0e1SMatthew G. Knepley DM dm; 373847ff0e1SMatthew G. Knepley 3749371c9d4SSatish Balay PetscCall(TSGetDM(ts, &dm)); 3759371c9d4SSatish Balay PetscCall(DMTSUnsetIJacobianContext_Internal(dm)); 3769566063dSJacob Faibussowitsch PetscCall(TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, NULL)); 3779566063dSJacob Faibussowitsch PetscCall(PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n")); 378847ff0e1SMatthew G. Knepley } 379847ff0e1SMatthew G. Knepley 380d763cef2SBarry Smith /* Handle specific TS options */ 381dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, setfromoptions, PetscOptionsObject); 382fbc52257SHong Zhang 383a7bdc993SLisandro Dalcin /* Handle TSAdapt options */ 3849566063dSJacob Faibussowitsch PetscCall(TSGetAdapt(ts, &ts->adapt)); 3859566063dSJacob Faibussowitsch PetscCall(TSAdaptSetDefaultType(ts->adapt, ts->default_adapt_type)); 386dbbe0bcdSBarry Smith PetscCall(TSAdaptSetFromOptions(ts->adapt, PetscOptionsObject)); 387a7bdc993SLisandro Dalcin 38868bece0bSHong Zhang /* TS trajectory must be set after TS, since it may use some TS options above */ 3894f122a70SLisandro Dalcin tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE; 3909566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_save_trajectory", "Save the solution at each timestep", "TSSetSaveTrajectory", tflg, &tflg, NULL)); 3911baa6e33SBarry Smith if (tflg) PetscCall(TSSetSaveTrajectory(ts)); 392a05bf03eSHong Zhang 393dbbe0bcdSBarry Smith PetscCall(TSAdjointSetFromOptions(ts, PetscOptionsObject)); 394d763cef2SBarry Smith 395d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 396dbbe0bcdSBarry Smith PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)ts, PetscOptionsObject)); 397d0609cedSBarry Smith PetscOptionsEnd(); 398d763cef2SBarry Smith 3991baa6e33SBarry Smith if (ts->trajectory) PetscCall(TSTrajectorySetFromOptions(ts->trajectory, ts)); 40068bece0bSHong Zhang 4011ef27442SStefano Zampini /* why do we have to do this here and not during TSSetUp? */ 4029566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &ts->snes)); 4031ef27442SStefano Zampini if (ts->problem_type == TS_LINEAR) { 4049566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompareAny((PetscObject)ts->snes, &flg, SNESKSPONLY, SNESKSPTRANSPOSEONLY, "")); 4059566063dSJacob Faibussowitsch if (!flg) PetscCall(SNESSetType(ts->snes, SNESKSPONLY)); 4061ef27442SStefano Zampini } 4079566063dSJacob Faibussowitsch PetscCall(SNESSetFromOptions(ts->snes)); 408d763cef2SBarry Smith PetscFunctionReturn(0); 409d763cef2SBarry Smith } 410d763cef2SBarry Smith 411d2daff3dSHong Zhang /*@ 412bcf0153eSBarry Smith TSGetTrajectory - Gets the trajectory from a `TS` if it exists 41378fbdcc8SBarry Smith 414bcf0153eSBarry Smith Collective on ts 41578fbdcc8SBarry Smith 41678fbdcc8SBarry Smith Input Parameters: 417bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 41878fbdcc8SBarry Smith 4197a7aea1fSJed Brown Output Parameters: 420bcf0153eSBarry Smith . tr - the `TSTrajectory` object, if it exists 42178fbdcc8SBarry Smith 42278fbdcc8SBarry Smith Level: advanced 42378fbdcc8SBarry Smith 424bcf0153eSBarry Smith Note: 425bcf0153eSBarry Smith This routine should be called after all `TS` options have been set 42678fbdcc8SBarry Smith 427bcf0153eSBarry Smith .seealso: [](chapter_ts), `TSTrajectory`, `TSAdjointSolve()`, `TSTrajectory`, `TSTrajectoryCreate()` 42878fbdcc8SBarry Smith @*/ 429d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTrajectory(TS ts, TSTrajectory *tr) 430d71ae5a4SJacob Faibussowitsch { 43178fbdcc8SBarry Smith PetscFunctionBegin; 43278fbdcc8SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 43378fbdcc8SBarry Smith *tr = ts->trajectory; 43478fbdcc8SBarry Smith PetscFunctionReturn(0); 43578fbdcc8SBarry Smith } 43678fbdcc8SBarry Smith 43778fbdcc8SBarry Smith /*@ 438bcf0153eSBarry Smith TSSetSaveTrajectory - Causes the `TS` to save its solutions as it iterates forward in time in a `TSTrajectory` object 439d2daff3dSHong Zhang 440bcf0153eSBarry Smith Collective on ts 441d2daff3dSHong Zhang 442f899ff85SJose E. Roman Input Parameter: 443bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 444bc952696SBarry Smith 445bcf0153eSBarry Smith Options Database Keys: 44678fbdcc8SBarry Smith + -ts_save_trajectory - saves the trajectory to a file 44767b8a455SSatish Balay - -ts_trajectory_type type - set trajectory type 44878fbdcc8SBarry Smith 449d2daff3dSHong Zhang Level: intermediate 450d2daff3dSHong Zhang 451bcf0153eSBarry Smith Notes: 452bcf0153eSBarry Smith This routine should be called after all `TS` options have been set 453d2daff3dSHong Zhang 454bcf0153eSBarry Smith The `TSTRAJECTORYVISUALIZATION` files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and 455bcf0153eSBarry Smith MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m 456bcf0153eSBarry Smith 457bcf0153eSBarry Smith .seealso: [](chapter_ts), `TSTrajectory`, `TSGetTrajectory()`, `TSAdjointSolve()` 458d2daff3dSHong Zhang @*/ 459d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetSaveTrajectory(TS ts) 460d71ae5a4SJacob Faibussowitsch { 461d2daff3dSHong Zhang PetscFunctionBegin; 462d2daff3dSHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 46363a3b9bcSJacob Faibussowitsch if (!ts->trajectory) PetscCall(TSTrajectoryCreate(PetscObjectComm((PetscObject)ts), &ts->trajectory)); 464d2daff3dSHong Zhang PetscFunctionReturn(0); 465d2daff3dSHong Zhang } 466d2daff3dSHong Zhang 467a7a1495cSBarry Smith /*@ 468bcf0153eSBarry Smith TSResetTrajectory - Destroys and recreates the internal `TSTrajectory` object 4692d29f1f2SStefano Zampini 470bcf0153eSBarry Smith Collective on ts 4712d29f1f2SStefano Zampini 4722d29f1f2SStefano Zampini Input Parameters: 473bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 4742d29f1f2SStefano Zampini 4752d29f1f2SStefano Zampini Level: intermediate 4762d29f1f2SStefano Zampini 477bcf0153eSBarry Smith .seealso: [](chapter_ts), `TSTrajectory`, `TSGetTrajectory()`, `TSAdjointSolve()`, `TSRemoveTrajectory()` 4782d29f1f2SStefano Zampini @*/ 479d71ae5a4SJacob Faibussowitsch PetscErrorCode TSResetTrajectory(TS ts) 480d71ae5a4SJacob Faibussowitsch { 4812d29f1f2SStefano Zampini PetscFunctionBegin; 4822d29f1f2SStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4832d29f1f2SStefano Zampini if (ts->trajectory) { 4849566063dSJacob Faibussowitsch PetscCall(TSTrajectoryDestroy(&ts->trajectory)); 4859566063dSJacob Faibussowitsch PetscCall(TSTrajectoryCreate(PetscObjectComm((PetscObject)ts), &ts->trajectory)); 4862d29f1f2SStefano Zampini } 4872d29f1f2SStefano Zampini PetscFunctionReturn(0); 4882d29f1f2SStefano Zampini } 4892d29f1f2SStefano Zampini 4902d29f1f2SStefano Zampini /*@ 491bcf0153eSBarry Smith TSRemoveTrajectory - Destroys and removes the internal `TSTrajectory` object from TS 49267a3cfb0SHong Zhang 493bcf0153eSBarry Smith Collective on ts 49467a3cfb0SHong Zhang 49567a3cfb0SHong Zhang Input Parameters: 496bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 49767a3cfb0SHong Zhang 49867a3cfb0SHong Zhang Level: intermediate 49967a3cfb0SHong Zhang 500bcf0153eSBarry Smith .seealso: [](chapter_ts), `TSTrajectory`, `TSResetTrajectory()`, `TSAdjointSolve()` 50167a3cfb0SHong Zhang @*/ 502d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRemoveTrajectory(TS ts) 503d71ae5a4SJacob Faibussowitsch { 50467a3cfb0SHong Zhang PetscFunctionBegin; 50567a3cfb0SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5061e66621cSBarry Smith if (ts->trajectory) PetscCall(TSTrajectoryDestroy(&ts->trajectory)); 50767a3cfb0SHong Zhang PetscFunctionReturn(0); 50867a3cfb0SHong Zhang } 50967a3cfb0SHong Zhang 51067a3cfb0SHong Zhang /*@ 511a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 512bcf0153eSBarry Smith set with `TSSetRHSJacobian()`. 513a7a1495cSBarry Smith 514bcf0153eSBarry Smith Collective on ts 515a7a1495cSBarry Smith 516a7a1495cSBarry Smith Input Parameters: 517bcf0153eSBarry Smith + ts - the `TS` context 518a7a1495cSBarry Smith . t - current timestep 5190910c330SBarry Smith - U - input vector 520a7a1495cSBarry Smith 521a7a1495cSBarry Smith Output Parameters: 522a7a1495cSBarry Smith + A - Jacobian matrix 5236b867d5aSJose E. Roman - B - optional preconditioning matrix 524a7a1495cSBarry Smith 525bcf0153eSBarry Smith Level: developer 526bcf0153eSBarry Smith 527bcf0153eSBarry Smith Note: 528a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 529a7a1495cSBarry Smith is used internally within the nonlinear solvers. 530a7a1495cSBarry Smith 531bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetRHSJacobian()`, `KSPSetOperators()` 532a7a1495cSBarry Smith @*/ 533d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSJacobian(TS ts, PetscReal t, Vec U, Mat A, Mat B) 534d71ae5a4SJacob Faibussowitsch { 535270bf2e7SJed Brown PetscObjectState Ustate; 5366c1e1eecSBarry Smith PetscObjectId Uid; 53724989b8cSPeter Brune DM dm; 538942e3340SBarry Smith DMTS tsdm; 53924989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 54024989b8cSPeter Brune void *ctx; 541b2df71adSDebojyoti Ghosh TSRHSFunction rhsfunction; 542a7a1495cSBarry Smith 543a7a1495cSBarry Smith PetscFunctionBegin; 5440700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5450910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 5460910c330SBarry Smith PetscCheckSameComm(ts, 1, U, 3); 5479566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 5489566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm, &tsdm)); 5499566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, NULL)); 5509566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, &rhsjacobianfunc, &ctx)); 5519566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &Ustate)); 5529566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U, &Uid)); 553971015bcSStefano Zampini 5542663174eSHong Zhang if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) PetscFunctionReturn(0); 555d90be118SSean Farley 55663a3b9bcSJacob 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); 55724989b8cSPeter Brune if (rhsjacobianfunc) { 5589566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_JacobianEval, ts, U, A, B)); 559792fecdfSBarry Smith PetscCallBack("TS callback Jacobian", (*rhsjacobianfunc)(ts, t, U, A, B, ctx)); 560a6ab3590SBarry Smith ts->rhsjacs++; 5619566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_JacobianEval, ts, U, A, B)); 562ef66eb69SBarry Smith } else { 5639566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(A)); 5649566063dSJacob Faibussowitsch if (B && A != B) PetscCall(MatZeroEntries(B)); 565ef66eb69SBarry Smith } 5660e4ef248SJed Brown ts->rhsjacobian.time = t; 567971015bcSStefano Zampini ts->rhsjacobian.shift = 0; 568971015bcSStefano Zampini ts->rhsjacobian.scale = 1.; 5699566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U, &ts->rhsjacobian.Xid)); 5709566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &ts->rhsjacobian.Xstate)); 571a7a1495cSBarry Smith PetscFunctionReturn(0); 572a7a1495cSBarry Smith } 573a7a1495cSBarry Smith 574316643e7SJed Brown /*@ 575bcf0153eSBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function for a `TS` 576d763cef2SBarry Smith 577bcf0153eSBarry Smith Collective on ts 578316643e7SJed Brown 579316643e7SJed Brown Input Parameters: 580bcf0153eSBarry Smith + ts - the `TS` context 581316643e7SJed Brown . t - current time 5820910c330SBarry Smith - U - state vector 583316643e7SJed Brown 584316643e7SJed Brown Output Parameter: 585316643e7SJed Brown . y - right hand side 586316643e7SJed Brown 587bcf0153eSBarry Smith Level: developer 588bcf0153eSBarry Smith 589316643e7SJed Brown Note: 590316643e7SJed Brown Most users should not need to explicitly call this routine, as it 591316643e7SJed Brown is used internally within the nonlinear solvers. 592316643e7SJed Brown 593bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetRHSFunction()`, `TSComputeIFunction()` 594316643e7SJed Brown @*/ 595d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSFunction(TS ts, PetscReal t, Vec U, Vec y) 596d71ae5a4SJacob Faibussowitsch { 59724989b8cSPeter Brune TSRHSFunction rhsfunction; 59824989b8cSPeter Brune TSIFunction ifunction; 59924989b8cSPeter Brune void *ctx; 60024989b8cSPeter Brune DM dm; 60124989b8cSPeter Brune 602d763cef2SBarry Smith PetscFunctionBegin; 6030700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 6040910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 6050700a824SBarry Smith PetscValidHeaderSpecific(y, VEC_CLASSID, 4); 6069566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 6079566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, &ctx)); 6089566063dSJacob Faibussowitsch PetscCall(DMTSGetIFunction(dm, &ifunction, NULL)); 609d763cef2SBarry Smith 6103c633725SBarry Smith PetscCheck(rhsfunction || ifunction, PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "Must call TSSetRHSFunction() and / or TSSetIFunction()"); 611d763cef2SBarry Smith 61224989b8cSPeter Brune if (rhsfunction) { 6139566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_FunctionEval, ts, U, y, 0)); 6149566063dSJacob Faibussowitsch PetscCall(VecLockReadPush(U)); 615792fecdfSBarry Smith PetscCallBack("TS callback right-hand-side", (*rhsfunction)(ts, t, U, y, ctx)); 6169566063dSJacob Faibussowitsch PetscCall(VecLockReadPop(U)); 617a6ab3590SBarry Smith ts->rhsfuncs++; 6189566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_FunctionEval, ts, U, y, 0)); 6191e66621cSBarry Smith } else PetscCall(VecZeroEntries(y)); 620d763cef2SBarry Smith PetscFunctionReturn(0); 621d763cef2SBarry Smith } 622d763cef2SBarry Smith 623ef20d060SBarry Smith /*@ 624ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 625ef20d060SBarry Smith 626bcf0153eSBarry Smith Collective on ts 627ef20d060SBarry Smith 628ef20d060SBarry Smith Input Parameters: 629bcf0153eSBarry Smith + ts - the `TS` context 630ef20d060SBarry Smith - t - current time 631ef20d060SBarry Smith 632ef20d060SBarry Smith Output Parameter: 6330910c330SBarry Smith . U - the solution 634ef20d060SBarry Smith 635ef20d060SBarry Smith Level: developer 636ef20d060SBarry Smith 637bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetSolutionFunction()`, `TSSetRHSFunction()`, `TSComputeIFunction()` 638ef20d060SBarry Smith @*/ 639d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeSolutionFunction(TS ts, PetscReal t, Vec U) 640d71ae5a4SJacob Faibussowitsch { 641ef20d060SBarry Smith TSSolutionFunction solutionfunction; 642ef20d060SBarry Smith void *ctx; 643ef20d060SBarry Smith DM dm; 644ef20d060SBarry Smith 645ef20d060SBarry Smith PetscFunctionBegin; 646ef20d060SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 6470910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 6489566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 6499566063dSJacob Faibussowitsch PetscCall(DMTSGetSolutionFunction(dm, &solutionfunction, &ctx)); 650792fecdfSBarry Smith if (solutionfunction) PetscCallBack("TS callback solution", (*solutionfunction)(ts, t, U, ctx)); 651ef20d060SBarry Smith PetscFunctionReturn(0); 652ef20d060SBarry Smith } 6539b7cd975SBarry Smith /*@ 6549b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 6559b7cd975SBarry Smith 656bcf0153eSBarry Smith Collective on ts 6579b7cd975SBarry Smith 6589b7cd975SBarry Smith Input Parameters: 659bcf0153eSBarry Smith + ts - the `TS` context 6609b7cd975SBarry Smith - t - current time 6619b7cd975SBarry Smith 6629b7cd975SBarry Smith Output Parameter: 6639b7cd975SBarry Smith . U - the function value 6649b7cd975SBarry Smith 6659b7cd975SBarry Smith Level: developer 6669b7cd975SBarry Smith 667bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetSolutionFunction()`, `TSSetRHSFunction()`, `TSComputeIFunction()` 6689b7cd975SBarry Smith @*/ 669d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeForcingFunction(TS ts, PetscReal t, Vec U) 670d71ae5a4SJacob Faibussowitsch { 6719b7cd975SBarry Smith void *ctx; 6729b7cd975SBarry Smith DM dm; 6735f80ce2aSJacob Faibussowitsch TSForcingFunction forcing; 6749b7cd975SBarry Smith 6759b7cd975SBarry Smith PetscFunctionBegin; 6769b7cd975SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 6779b7cd975SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 6789566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 6799566063dSJacob Faibussowitsch PetscCall(DMTSGetForcingFunction(dm, &forcing, &ctx)); 6809b7cd975SBarry Smith 681792fecdfSBarry Smith if (forcing) PetscCallBack("TS callback forcing function", (*forcing)(ts, t, U, ctx)); 6829b7cd975SBarry Smith PetscFunctionReturn(0); 6839b7cd975SBarry Smith } 684ef20d060SBarry Smith 685d71ae5a4SJacob Faibussowitsch static PetscErrorCode TSGetRHSVec_Private(TS ts, Vec *Frhs) 686d71ae5a4SJacob Faibussowitsch { 6872dd45cf8SJed Brown Vec F; 688214bc6a2SJed Brown 689214bc6a2SJed Brown PetscFunctionBegin; 6900298fd71SBarry Smith *Frhs = NULL; 6919566063dSJacob Faibussowitsch PetscCall(TSGetIFunction(ts, &F, NULL, NULL)); 6921e66621cSBarry Smith if (!ts->Frhs) PetscCall(VecDuplicate(F, &ts->Frhs)); 693214bc6a2SJed Brown *Frhs = ts->Frhs; 694214bc6a2SJed Brown PetscFunctionReturn(0); 695214bc6a2SJed Brown } 696214bc6a2SJed Brown 697d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetRHSMats_Private(TS ts, Mat *Arhs, Mat *Brhs) 698d71ae5a4SJacob Faibussowitsch { 699214bc6a2SJed Brown Mat A, B; 70041a1d4d2SBarry Smith TSIJacobian ijacobian; 701214bc6a2SJed Brown 702214bc6a2SJed Brown PetscFunctionBegin; 703c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 704c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 7059566063dSJacob Faibussowitsch PetscCall(TSGetIJacobian(ts, &A, &B, &ijacobian, NULL)); 706214bc6a2SJed Brown if (Arhs) { 707214bc6a2SJed Brown if (!ts->Arhs) { 70841a1d4d2SBarry Smith if (ijacobian) { 7099566063dSJacob Faibussowitsch PetscCall(MatDuplicate(A, MAT_DO_NOT_COPY_VALUES, &ts->Arhs)); 7109566063dSJacob Faibussowitsch PetscCall(TSSetMatStructure(ts, SAME_NONZERO_PATTERN)); 71141a1d4d2SBarry Smith } else { 71241a1d4d2SBarry Smith ts->Arhs = A; 7139566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)A)); 71441a1d4d2SBarry Smith } 7153565c898SBarry Smith } else { 7163565c898SBarry Smith PetscBool flg; 7179566063dSJacob Faibussowitsch PetscCall(SNESGetUseMatrixFree(ts->snes, NULL, &flg)); 7183565c898SBarry Smith /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */ 7193565c898SBarry Smith if (flg && !ijacobian && ts->Arhs == ts->Brhs) { 7209566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)ts->Arhs)); 7213565c898SBarry Smith ts->Arhs = A; 7229566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)A)); 7233565c898SBarry Smith } 724214bc6a2SJed Brown } 725214bc6a2SJed Brown *Arhs = ts->Arhs; 726214bc6a2SJed Brown } 727214bc6a2SJed Brown if (Brhs) { 728214bc6a2SJed Brown if (!ts->Brhs) { 729bdb70873SJed Brown if (A != B) { 73041a1d4d2SBarry Smith if (ijacobian) { 7319566063dSJacob Faibussowitsch PetscCall(MatDuplicate(B, MAT_DO_NOT_COPY_VALUES, &ts->Brhs)); 732bdb70873SJed Brown } else { 73341a1d4d2SBarry Smith ts->Brhs = B; 7349566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)B)); 73541a1d4d2SBarry Smith } 73641a1d4d2SBarry Smith } else { 7379566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)ts->Arhs)); 73851699248SLisandro Dalcin ts->Brhs = ts->Arhs; 739bdb70873SJed Brown } 740214bc6a2SJed Brown } 741214bc6a2SJed Brown *Brhs = ts->Brhs; 742214bc6a2SJed Brown } 743214bc6a2SJed Brown PetscFunctionReturn(0); 744214bc6a2SJed Brown } 745214bc6a2SJed Brown 746316643e7SJed Brown /*@ 7470910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 748316643e7SJed Brown 749bcf0153eSBarry Smith Collective on ts 750316643e7SJed Brown 751316643e7SJed Brown Input Parameters: 752bcf0153eSBarry Smith + ts - the `TS` context 753316643e7SJed Brown . t - current time 7540910c330SBarry Smith . U - state vector 7550910c330SBarry Smith . Udot - time derivative of state vector 756bcf0153eSBarry Smith - imex - flag indicates if the method is `TSIMEX` so that the RHSFunction should be kept separate 757316643e7SJed Brown 758316643e7SJed Brown Output Parameter: 759316643e7SJed Brown . Y - right hand side 760316643e7SJed Brown 761bcf0153eSBarry Smith Level: developer 762bcf0153eSBarry Smith 763316643e7SJed Brown Note: 764316643e7SJed Brown Most users should not need to explicitly call this routine, as it 765316643e7SJed Brown is used internally within the nonlinear solvers. 766316643e7SJed Brown 767316643e7SJed Brown If the user did did not write their equations in implicit form, this 768316643e7SJed Brown function recasts them in implicit form. 769316643e7SJed Brown 770bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetIFunction()`, `TSComputeRHSFunction()` 771316643e7SJed Brown @*/ 772d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIFunction(TS ts, PetscReal t, Vec U, Vec Udot, Vec Y, PetscBool imex) 773d71ae5a4SJacob Faibussowitsch { 77424989b8cSPeter Brune TSIFunction ifunction; 77524989b8cSPeter Brune TSRHSFunction rhsfunction; 77624989b8cSPeter Brune void *ctx; 77724989b8cSPeter Brune DM dm; 778316643e7SJed Brown 779316643e7SJed Brown PetscFunctionBegin; 7800700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 7810910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 7820910c330SBarry Smith PetscValidHeaderSpecific(Udot, VEC_CLASSID, 4); 7830700a824SBarry Smith PetscValidHeaderSpecific(Y, VEC_CLASSID, 5); 784316643e7SJed Brown 7859566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 7869566063dSJacob Faibussowitsch PetscCall(DMTSGetIFunction(dm, &ifunction, &ctx)); 7879566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, NULL)); 78824989b8cSPeter Brune 7893c633725SBarry Smith PetscCheck(rhsfunction || ifunction, PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "Must call TSSetRHSFunction() and / or TSSetIFunction()"); 790d90be118SSean Farley 7919566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_FunctionEval, ts, U, Udot, Y)); 79224989b8cSPeter Brune if (ifunction) { 793792fecdfSBarry Smith PetscCallBack("TS callback implicit function", (*ifunction)(ts, t, U, Udot, Y, ctx)); 794a6ab3590SBarry Smith ts->ifuncs++; 795214bc6a2SJed Brown } 796214bc6a2SJed Brown if (imex) { 7971e66621cSBarry Smith if (!ifunction) PetscCall(VecCopy(Udot, Y)); 79824989b8cSPeter Brune } else if (rhsfunction) { 79924989b8cSPeter Brune if (ifunction) { 800214bc6a2SJed Brown Vec Frhs; 8019566063dSJacob Faibussowitsch PetscCall(TSGetRHSVec_Private(ts, &Frhs)); 8029566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts, t, U, Frhs)); 8039566063dSJacob Faibussowitsch PetscCall(VecAXPY(Y, -1, Frhs)); 8042dd45cf8SJed Brown } else { 8059566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts, t, U, Y)); 8069566063dSJacob Faibussowitsch PetscCall(VecAYPX(Y, -1, Udot)); 807316643e7SJed Brown } 8084a6899ffSJed Brown } 8099566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_FunctionEval, ts, U, Udot, Y)); 810316643e7SJed Brown PetscFunctionReturn(0); 811316643e7SJed Brown } 812316643e7SJed Brown 813cfa8a9a2SHong Zhang /* 814cfa8a9a2SHong Zhang TSRecoverRHSJacobian - Recover the Jacobian matrix so that one can call TSComputeRHSJacobian() on it. 815cfa8a9a2SHong Zhang 816cfa8a9a2SHong Zhang Note: 817cfa8a9a2SHong Zhang This routine is needed when one switches from TSComputeIJacobian() to TSComputeRHSJacobian() because the Jacobian matrix may be shifted or scaled in TSComputeIJacobian(). 818cfa8a9a2SHong Zhang 819cfa8a9a2SHong Zhang */ 820d71ae5a4SJacob Faibussowitsch static PetscErrorCode TSRecoverRHSJacobian(TS ts, Mat A, Mat B) 821d71ae5a4SJacob Faibussowitsch { 822cfa8a9a2SHong Zhang PetscFunctionBegin; 823cfa8a9a2SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 8243c633725SBarry Smith PetscCheck(A == ts->Arhs, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Invalid Amat"); 8253c633725SBarry Smith PetscCheck(B == ts->Brhs, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Invalid Bmat"); 826cfa8a9a2SHong Zhang 8271baa6e33SBarry Smith if (ts->rhsjacobian.shift) PetscCall(MatShift(A, -ts->rhsjacobian.shift)); 82848a46eb9SPierre Jolivet if (ts->rhsjacobian.scale == -1.) PetscCall(MatScale(A, -1)); 829cfa8a9a2SHong Zhang if (B && B == ts->Brhs && A != B) { 8301baa6e33SBarry Smith if (ts->rhsjacobian.shift) PetscCall(MatShift(B, -ts->rhsjacobian.shift)); 8311e66621cSBarry Smith if (ts->rhsjacobian.scale == -1.) PetscCall(MatScale(B, -1)); 832cfa8a9a2SHong Zhang } 833cfa8a9a2SHong Zhang ts->rhsjacobian.shift = 0; 834cfa8a9a2SHong Zhang ts->rhsjacobian.scale = 1.; 835cfa8a9a2SHong Zhang PetscFunctionReturn(0); 836cfa8a9a2SHong Zhang } 837cfa8a9a2SHong Zhang 838316643e7SJed Brown /*@ 839316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 840316643e7SJed Brown 841bcf0153eSBarry Smith Collective on ts 842316643e7SJed Brown 843316643e7SJed Brown Input 844316643e7SJed Brown Input Parameters: 845bcf0153eSBarry Smith + ts - the `TS` context 846316643e7SJed Brown . t - current timestep 8470910c330SBarry Smith . U - state vector 8480910c330SBarry Smith . Udot - time derivative of state vector 849214bc6a2SJed Brown . shift - shift to apply, see note below 850bcf0153eSBarry Smith - imex - flag indicates if the method is `TSIMEX` so that the RHSJacobian should be kept separate 851316643e7SJed Brown 852316643e7SJed Brown Output Parameters: 853316643e7SJed Brown + A - Jacobian matrix 8543565c898SBarry Smith - B - matrix from which the preconditioner is constructed; often the same as A 855316643e7SJed Brown 856bcf0153eSBarry Smith Level: developer 857bcf0153eSBarry Smith 858316643e7SJed Brown Notes: 8590910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 860316643e7SJed Brown 8610910c330SBarry Smith dF/dU + shift*dF/dUdot 862316643e7SJed Brown 863316643e7SJed Brown Most users should not need to explicitly call this routine, as it 864316643e7SJed Brown is used internally within the nonlinear solvers. 865316643e7SJed Brown 866bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetIJacobian()` 86763495f91SJed Brown @*/ 868d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIJacobian(TS ts, PetscReal t, Vec U, Vec Udot, PetscReal shift, Mat A, Mat B, PetscBool imex) 869d71ae5a4SJacob Faibussowitsch { 87024989b8cSPeter Brune TSIJacobian ijacobian; 87124989b8cSPeter Brune TSRHSJacobian rhsjacobian; 87224989b8cSPeter Brune DM dm; 87324989b8cSPeter Brune void *ctx; 874316643e7SJed Brown 875316643e7SJed Brown PetscFunctionBegin; 8760700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 8770910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 8780910c330SBarry Smith PetscValidHeaderSpecific(Udot, VEC_CLASSID, 4); 879316643e7SJed Brown PetscValidPointer(A, 6); 88094ab13aaSBarry Smith PetscValidHeaderSpecific(A, MAT_CLASSID, 6); 881316643e7SJed Brown PetscValidPointer(B, 7); 88294ab13aaSBarry Smith PetscValidHeaderSpecific(B, MAT_CLASSID, 7); 88324989b8cSPeter Brune 8849566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 8859566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm, &ijacobian, &ctx)); 8869566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, &rhsjacobian, NULL)); 88724989b8cSPeter Brune 8883c633725SBarry Smith PetscCheck(rhsjacobian || ijacobian, PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 889316643e7SJed Brown 8909566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_JacobianEval, ts, U, A, B)); 89124989b8cSPeter Brune if (ijacobian) { 892792fecdfSBarry Smith PetscCallBack("TS callback implicit Jacobian", (*ijacobian)(ts, t, U, Udot, shift, A, B, ctx)); 893a6ab3590SBarry Smith ts->ijacs++; 8944a6899ffSJed Brown } 895214bc6a2SJed Brown if (imex) { 896b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 8974c26be97Sstefano_zampini PetscBool assembled; 898971015bcSStefano Zampini if (rhsjacobian) { 899971015bcSStefano Zampini Mat Arhs = NULL; 9009566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts, &Arhs, NULL)); 901971015bcSStefano Zampini if (A == Arhs) { 9023c633725SBarry 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 */ 903971015bcSStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 904971015bcSStefano Zampini } 905971015bcSStefano Zampini } 9069566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(A)); 9079566063dSJacob Faibussowitsch PetscCall(MatAssembled(A, &assembled)); 9084c26be97Sstefano_zampini if (!assembled) { 9099566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); 9109566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); 9114c26be97Sstefano_zampini } 9129566063dSJacob Faibussowitsch PetscCall(MatShift(A, shift)); 91394ab13aaSBarry Smith if (A != B) { 9149566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(B)); 9159566063dSJacob Faibussowitsch PetscCall(MatAssembled(B, &assembled)); 9164c26be97Sstefano_zampini if (!assembled) { 9179566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY)); 9189566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY)); 9194c26be97Sstefano_zampini } 9209566063dSJacob Faibussowitsch PetscCall(MatShift(B, shift)); 921214bc6a2SJed Brown } 922214bc6a2SJed Brown } 923214bc6a2SJed Brown } else { 924e1244c69SJed Brown Mat Arhs = NULL, Brhs = NULL; 9251e66621cSBarry Smith 9261e66621cSBarry Smith /* RHSJacobian needs to be converted to part of IJacobian if exists */ 9271e66621cSBarry Smith if (rhsjacobian) PetscCall(TSGetRHSMats_Private(ts, &Arhs, &Brhs)); 928e8b1e424SHong Zhang if (Arhs == A) { /* No IJacobian matrix, so we only have the RHS matrix */ 929e8b1e424SHong Zhang PetscObjectState Ustate; 930e8b1e424SHong Zhang PetscObjectId Uid; 931e8b1e424SHong Zhang TSRHSFunction rhsfunction; 932e8b1e424SHong Zhang 9339566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, NULL)); 9349566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &Ustate)); 9359566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U, &Uid)); 9369371c9d4SSatish Balay if ((rhsjacobian == TSComputeRHSJacobianConstant || (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && rhsfunction != TSComputeRHSFunctionLinear)) && 9379371c9d4SSatish Balay ts->rhsjacobian.scale == -1.) { /* No need to recompute RHSJacobian */ 9389566063dSJacob Faibussowitsch PetscCall(MatShift(A, shift - ts->rhsjacobian.shift)); /* revert the old shift and add the new shift with a single call to MatShift */ 9391e66621cSBarry Smith if (A != B) PetscCall(MatShift(B, shift - ts->rhsjacobian.shift)); 940e8b1e424SHong Zhang } else { 9413565c898SBarry Smith PetscBool flg; 942e8b1e424SHong Zhang 943e8b1e424SHong Zhang if (ts->rhsjacobian.reuse) { /* Undo the damage */ 944e8b1e424SHong Zhang /* MatScale has a short path for this case. 945e8b1e424SHong Zhang However, this code path is taken the first time TSComputeRHSJacobian is called 946e8b1e424SHong Zhang and the matrices have not been assembled yet */ 9479566063dSJacob Faibussowitsch PetscCall(TSRecoverRHSJacobian(ts, A, B)); 948e8b1e424SHong Zhang } 9499566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts, t, U, A, B)); 9509566063dSJacob Faibussowitsch PetscCall(SNESGetUseMatrixFree(ts->snes, NULL, &flg)); 9513565c898SBarry Smith /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */ 9523565c898SBarry Smith if (!flg) { 9539566063dSJacob Faibussowitsch PetscCall(MatScale(A, -1)); 9549566063dSJacob Faibussowitsch PetscCall(MatShift(A, shift)); 9553565c898SBarry Smith } 95694ab13aaSBarry Smith if (A != B) { 9579566063dSJacob Faibussowitsch PetscCall(MatScale(B, -1)); 9589566063dSJacob Faibussowitsch PetscCall(MatShift(B, shift)); 959316643e7SJed Brown } 960e8b1e424SHong Zhang } 961e8b1e424SHong Zhang ts->rhsjacobian.scale = -1; 962e8b1e424SHong Zhang ts->rhsjacobian.shift = shift; 963d60b7d5cSBarry Smith } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 964e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 9659566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(A)); 9669566063dSJacob Faibussowitsch PetscCall(MatShift(A, shift)); 96794ab13aaSBarry Smith if (A != B) { 9689566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(B)); 9699566063dSJacob Faibussowitsch PetscCall(MatShift(B, shift)); 970214bc6a2SJed Brown } 971316643e7SJed Brown } 9729566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts, t, U, Arhs, Brhs)); 9739566063dSJacob Faibussowitsch PetscCall(MatAXPY(A, -1, Arhs, ts->axpy_pattern)); 9741e66621cSBarry Smith if (A != B) PetscCall(MatAXPY(B, -1, Brhs, ts->axpy_pattern)); 975316643e7SJed Brown } 976316643e7SJed Brown } 9779566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_JacobianEval, ts, U, A, B)); 978316643e7SJed Brown PetscFunctionReturn(0); 979316643e7SJed Brown } 980316643e7SJed Brown 981d763cef2SBarry Smith /*@C 982d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 983b5abc632SBarry Smith where U_t = G(t,u). 984d763cef2SBarry Smith 985bcf0153eSBarry Smith Logically Collective on ts 986d763cef2SBarry Smith 987d763cef2SBarry Smith Input Parameters: 988bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 9890298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 990d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 991d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 9920298fd71SBarry Smith 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)); 1029d763cef2SBarry Smith PetscFunctionReturn(0); 1030d763cef2SBarry Smith } 1031d763cef2SBarry Smith 1032ef20d060SBarry Smith /*@C 1033abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1034ef20d060SBarry Smith 1035bcf0153eSBarry Smith Logically Collective on ts 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 10410298fd71SBarry 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)); 1073ef20d060SBarry Smith PetscFunctionReturn(0); 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 1079bcf0153eSBarry Smith Logically Collective on ts 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 10850298fd71SBarry 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)); 11189b7cd975SBarry Smith PetscFunctionReturn(0); 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 1125bcf0153eSBarry Smith Logically Collective on ts 1126d763cef2SBarry Smith 1127d763cef2SBarry Smith Input Parameters: 1128bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1129e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1130e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1131d763cef2SBarry Smith . f - the Jacobian evaluation routine 1132d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 11330298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1134d763cef2SBarry Smith 1135f7ab8db6SBarry Smith Calling sequence of f: 1136a96d6ef6SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1137d763cef2SBarry Smith 1138d763cef2SBarry Smith + t - current timestep 1139d763cef2SBarry Smith . u - input vector 1140e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1141e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1142d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1143d763cef2SBarry Smith 1144bcf0153eSBarry Smith Level: beginner 1145bcf0153eSBarry Smith 11466cd88445SBarry Smith Notes: 11476cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 11486cd88445SBarry Smith 1149bcf0153eSBarry Smith The `TS` solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1150ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1151d763cef2SBarry Smith 1152bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `SNESComputeJacobianDefaultColor()`, `TSSetRHSFunction()`, `TSRHSJacobianSetReuse()`, `TSSetIJacobian()` 1153d763cef2SBarry Smith @*/ 1154d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetRHSJacobian(TS ts, Mat Amat, Mat Pmat, TSRHSJacobian f, void *ctx) 1155d71ae5a4SJacob Faibussowitsch { 1156089b2837SJed Brown SNES snes; 115724989b8cSPeter Brune DM dm; 115824989b8cSPeter Brune TSIJacobian ijacobian; 1159277b19d0SLisandro Dalcin 1160d763cef2SBarry Smith PetscFunctionBegin; 11610700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1162e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat, MAT_CLASSID, 2); 1163e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat, MAT_CLASSID, 3); 1164e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts, 1, Amat, 2); 1165e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts, 1, Pmat, 3); 1166d763cef2SBarry Smith 11679566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 11689566063dSJacob Faibussowitsch PetscCall(DMTSSetRHSJacobian(dm, f, ctx)); 11699566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm, &ijacobian, NULL)); 11709566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 11711e66621cSBarry Smith if (!ijacobian) PetscCall(SNESSetJacobian(snes, Amat, Pmat, SNESTSFormJacobian, ts)); 1172e5d3d808SBarry Smith if (Amat) { 11739566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)Amat)); 11749566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Arhs)); 1175e5d3d808SBarry Smith ts->Arhs = Amat; 11760e4ef248SJed Brown } 1177e5d3d808SBarry Smith if (Pmat) { 11789566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)Pmat)); 11799566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Brhs)); 1180e5d3d808SBarry Smith ts->Brhs = Pmat; 11810e4ef248SJed Brown } 1182d763cef2SBarry Smith PetscFunctionReturn(0); 1183d763cef2SBarry Smith } 1184d763cef2SBarry Smith 1185316643e7SJed Brown /*@C 1186b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1187316643e7SJed Brown 1188bcf0153eSBarry Smith Logically Collective on ts 1189316643e7SJed Brown 1190316643e7SJed Brown Input Parameters: 1191bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 11920298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1193316643e7SJed Brown . f - the function evaluation routine 11940298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1195316643e7SJed Brown 1196316643e7SJed Brown Calling sequence of f: 11976bc98fa9SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1198316643e7SJed Brown 1199316643e7SJed Brown + t - time at step/stage being solved 1200316643e7SJed Brown . u - state vector 1201316643e7SJed Brown . u_t - time derivative of state vector 1202316643e7SJed Brown . F - function vector 1203316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1204316643e7SJed Brown 1205316643e7SJed Brown Level: beginner 1206316643e7SJed Brown 1207bcf0153eSBarry Smith Note: 1208bcf0153eSBarry Smith The user MUST call either this routine or `TSSetRHSFunction()` to define the ODE. When solving DAEs you must use this function. 1209bcf0153eSBarry Smith 1210bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetRHSJacobian()`, `TSSetRHSFunction()`, `TSSetIJacobian()` 1211316643e7SJed Brown @*/ 1212d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetIFunction(TS ts, Vec r, TSIFunction f, void *ctx) 1213d71ae5a4SJacob Faibussowitsch { 1214089b2837SJed Brown SNES snes; 121551699248SLisandro Dalcin Vec ralloc = NULL; 121624989b8cSPeter Brune DM dm; 1217316643e7SJed Brown 1218316643e7SJed Brown PetscFunctionBegin; 12190700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 122051699248SLisandro Dalcin if (r) PetscValidHeaderSpecific(r, VEC_CLASSID, 2); 122124989b8cSPeter Brune 12229566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 12239566063dSJacob Faibussowitsch PetscCall(DMTSSetIFunction(dm, f, ctx)); 122424989b8cSPeter Brune 12259566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 122651699248SLisandro Dalcin if (!r && !ts->dm && ts->vec_sol) { 12279566063dSJacob Faibussowitsch PetscCall(VecDuplicate(ts->vec_sol, &ralloc)); 122851699248SLisandro Dalcin r = ralloc; 1229e856ceecSJed Brown } 12309566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(snes, r, SNESTSFormFunction, ts)); 12319566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ralloc)); 1232089b2837SJed Brown PetscFunctionReturn(0); 1233089b2837SJed Brown } 1234089b2837SJed Brown 1235089b2837SJed Brown /*@C 1236a5b23f4aSJose E. Roman TSGetIFunction - Returns the vector where the implicit residual is stored and the function/context to compute it. 1237089b2837SJed Brown 1238089b2837SJed Brown Not Collective 1239089b2837SJed Brown 1240089b2837SJed Brown Input Parameter: 1241bcf0153eSBarry Smith . ts - the `TS` context 1242089b2837SJed Brown 1243d8d19677SJose E. Roman Output Parameters: 12440298fd71SBarry Smith + r - vector to hold residual (or NULL) 12450298fd71SBarry Smith . func - the function to compute residual (or NULL) 12460298fd71SBarry Smith - ctx - the function context (or NULL) 1247089b2837SJed Brown 1248089b2837SJed Brown Level: advanced 1249089b2837SJed Brown 1250bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetIFunction()`, `SNESGetFunction()` 1251089b2837SJed Brown @*/ 1252d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetIFunction(TS ts, Vec *r, TSIFunction *func, void **ctx) 1253d71ae5a4SJacob Faibussowitsch { 1254089b2837SJed Brown SNES snes; 125524989b8cSPeter Brune DM dm; 1256089b2837SJed Brown 1257089b2837SJed Brown PetscFunctionBegin; 1258089b2837SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 12599566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 12609566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes, r, NULL, NULL)); 12619566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 12629566063dSJacob Faibussowitsch PetscCall(DMTSGetIFunction(dm, func, ctx)); 1263089b2837SJed Brown PetscFunctionReturn(0); 1264089b2837SJed Brown } 1265089b2837SJed Brown 1266089b2837SJed Brown /*@C 1267089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1268089b2837SJed Brown 1269089b2837SJed Brown Not Collective 1270089b2837SJed Brown 1271089b2837SJed Brown Input Parameter: 1272bcf0153eSBarry Smith . ts - the `TS` context 1273089b2837SJed Brown 1274d8d19677SJose E. Roman Output Parameters: 12750298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 12760298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 12770298fd71SBarry Smith - ctx - the function context (or NULL) 1278089b2837SJed Brown 1279089b2837SJed Brown Level: advanced 1280089b2837SJed Brown 1281bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetRHSFunction()`, `SNESGetFunction()` 1282089b2837SJed Brown @*/ 1283d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetRHSFunction(TS ts, Vec *r, TSRHSFunction *func, void **ctx) 1284d71ae5a4SJacob Faibussowitsch { 1285089b2837SJed Brown SNES snes; 128624989b8cSPeter Brune DM dm; 1287089b2837SJed Brown 1288089b2837SJed Brown PetscFunctionBegin; 1289089b2837SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 12909566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 12919566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes, r, NULL, NULL)); 12929566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 12939566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, func, ctx)); 1294316643e7SJed Brown PetscFunctionReturn(0); 1295316643e7SJed Brown } 1296316643e7SJed Brown 1297316643e7SJed Brown /*@C 1298a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1299bcf0153eSBarry Smith provided with `TSSetIFunction()`. 1300316643e7SJed Brown 1301bcf0153eSBarry Smith Logically Collective on ts 1302316643e7SJed Brown 1303316643e7SJed Brown Input Parameters: 1304bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1305e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1306e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1307316643e7SJed Brown . f - the Jacobian evaluation routine 13080298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1309316643e7SJed Brown 1310316643e7SJed Brown Calling sequence of f: 13116bc98fa9SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1312316643e7SJed Brown 1313316643e7SJed Brown + t - time at step/stage being solved 13141b4a444bSJed Brown . U - state vector 13151b4a444bSJed Brown . U_t - time derivative of state vector 1316316643e7SJed Brown . a - shift 1317e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1318e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1319316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1320316643e7SJed Brown 1321bcf0153eSBarry Smith Level: beginner 1322316643e7SJed Brown 1323bcf0153eSBarry Smith Notes: 1324bcf0153eSBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by `SNES` for the nonlinear solve. 1325bcf0153eSBarry Smith 1326bcf0153eSBarry Smith If you know the operator Amat has a null space you can use `MatSetNullSpace()` and `MatSetTransposeNullSpace()` to supply the null 1327bcf0153eSBarry Smith space to Amat and the `KSP` solvers will automatically use that null space as needed during the solution process. 1328895c21f2SBarry Smith 1329a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1330b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1331a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1332a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1333a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1334a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1335a4f0a591SBarry Smith 13366cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 13376cd88445SBarry Smith 13386cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1339ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1340ca5f011dSBarry Smith 1341bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetIFunction()`, `TSSetRHSJacobian()`, `SNESComputeJacobianDefaultColor()`, `SNESComputeJacobianDefault()`, `TSSetRHSFunction()` 1342316643e7SJed Brown @*/ 1343d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetIJacobian(TS ts, Mat Amat, Mat Pmat, TSIJacobian f, void *ctx) 1344d71ae5a4SJacob Faibussowitsch { 1345089b2837SJed Brown SNES snes; 134624989b8cSPeter Brune DM dm; 1347316643e7SJed Brown 1348316643e7SJed Brown PetscFunctionBegin; 13490700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1350e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat, MAT_CLASSID, 2); 1351e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat, MAT_CLASSID, 3); 1352e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts, 1, Amat, 2); 1353e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts, 1, Pmat, 3); 135424989b8cSPeter Brune 13559566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 13569566063dSJacob Faibussowitsch PetscCall(DMTSSetIJacobian(dm, f, ctx)); 135724989b8cSPeter Brune 13589566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 13599566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes, Amat, Pmat, SNESTSFormJacobian, ts)); 1360316643e7SJed Brown PetscFunctionReturn(0); 1361316643e7SJed Brown } 1362316643e7SJed Brown 1363e1244c69SJed Brown /*@ 1364bcf0153eSBarry Smith TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, `TS` will change the sign and 1365e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1366e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1367e1244c69SJed Brown not been changed by the TS. 1368e1244c69SJed Brown 1369e1244c69SJed Brown Logically Collective 1370e1244c69SJed Brown 13714165533cSJose E. Roman Input Parameters: 1372bcf0153eSBarry Smith + ts - `TS` context obtained from `TSCreate()` 1373bcf0153eSBarry Smith - reuse - `PETSC_TRUE` if the RHS Jacobian 1374e1244c69SJed Brown 1375e1244c69SJed Brown Level: intermediate 1376e1244c69SJed Brown 1377bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetRHSJacobian()`, `TSComputeRHSJacobianConstant()` 1378e1244c69SJed Brown @*/ 1379d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRHSJacobianSetReuse(TS ts, PetscBool reuse) 1380d71ae5a4SJacob Faibussowitsch { 1381e1244c69SJed Brown PetscFunctionBegin; 1382e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1383e1244c69SJed Brown PetscFunctionReturn(0); 1384e1244c69SJed Brown } 1385e1244c69SJed Brown 1386efe9872eSLisandro Dalcin /*@C 1387efe9872eSLisandro Dalcin TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved. 1388efe9872eSLisandro Dalcin 1389bcf0153eSBarry Smith Logically Collective on ts 1390efe9872eSLisandro Dalcin 1391efe9872eSLisandro Dalcin Input Parameters: 1392bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1393efe9872eSLisandro Dalcin . F - vector to hold the residual (or NULL to have it created internally) 1394efe9872eSLisandro Dalcin . fun - the function evaluation routine 1395efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1396efe9872eSLisandro Dalcin 1397efe9872eSLisandro Dalcin Calling sequence of fun: 13986bc98fa9SBarry Smith $ PetscErrorCode fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx); 1399efe9872eSLisandro Dalcin 1400efe9872eSLisandro Dalcin + t - time at step/stage being solved 1401efe9872eSLisandro Dalcin . U - state vector 1402efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1403efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1404efe9872eSLisandro Dalcin . F - function vector 1405efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine (may be NULL) 1406efe9872eSLisandro Dalcin 1407efe9872eSLisandro Dalcin Level: beginner 1408efe9872eSLisandro Dalcin 1409bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetI2Jacobian()`, `TSSetIFunction()`, `TSCreate()`, `TSSetRHSFunction()` 1410efe9872eSLisandro Dalcin @*/ 1411d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetI2Function(TS ts, Vec F, TSI2Function fun, void *ctx) 1412d71ae5a4SJacob Faibussowitsch { 1413efe9872eSLisandro Dalcin DM dm; 1414efe9872eSLisandro Dalcin 1415efe9872eSLisandro Dalcin PetscFunctionBegin; 1416efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1417efe9872eSLisandro Dalcin if (F) PetscValidHeaderSpecific(F, VEC_CLASSID, 2); 14189566063dSJacob Faibussowitsch PetscCall(TSSetIFunction(ts, F, NULL, NULL)); 14199566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 14209566063dSJacob Faibussowitsch PetscCall(DMTSSetI2Function(dm, fun, ctx)); 1421efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1422efe9872eSLisandro Dalcin } 1423efe9872eSLisandro Dalcin 1424efe9872eSLisandro Dalcin /*@C 1425a5b23f4aSJose E. Roman TSGetI2Function - Returns the vector where the implicit residual is stored and the function/context to compute it. 1426efe9872eSLisandro Dalcin 1427efe9872eSLisandro Dalcin Not Collective 1428efe9872eSLisandro Dalcin 1429efe9872eSLisandro Dalcin Input Parameter: 1430bcf0153eSBarry Smith . ts - the `TS` context 1431efe9872eSLisandro Dalcin 1432d8d19677SJose E. Roman Output Parameters: 1433efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL) 1434efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL) 1435efe9872eSLisandro Dalcin - ctx - the function context (or NULL) 1436efe9872eSLisandro Dalcin 1437efe9872eSLisandro Dalcin Level: advanced 1438efe9872eSLisandro Dalcin 1439bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetIFunction()`, `SNESGetFunction()`, `TSCreate()` 1440efe9872eSLisandro Dalcin @*/ 1441d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetI2Function(TS ts, Vec *r, TSI2Function *fun, void **ctx) 1442d71ae5a4SJacob Faibussowitsch { 1443efe9872eSLisandro Dalcin SNES snes; 1444efe9872eSLisandro Dalcin DM dm; 1445efe9872eSLisandro Dalcin 1446efe9872eSLisandro Dalcin PetscFunctionBegin; 1447efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 14489566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 14499566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes, r, NULL, NULL)); 14509566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 14519566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Function(dm, fun, ctx)); 1452efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1453efe9872eSLisandro Dalcin } 1454efe9872eSLisandro Dalcin 1455efe9872eSLisandro Dalcin /*@C 1456bc77d74cSLisandro Dalcin TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t + a*dF/dU_tt 1457bcf0153eSBarry Smith where F(t,U,U_t,U_tt) is the function you provided with `TSSetI2Function()`. 1458efe9872eSLisandro Dalcin 1459bcf0153eSBarry Smith Logically Collective on ts 1460efe9872eSLisandro Dalcin 1461efe9872eSLisandro Dalcin Input Parameters: 1462bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1463efe9872eSLisandro Dalcin . J - Jacobian matrix 1464efe9872eSLisandro Dalcin . P - preconditioning matrix for J (may be same as J) 1465efe9872eSLisandro Dalcin . jac - the Jacobian evaluation routine 1466efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1467efe9872eSLisandro Dalcin 1468efe9872eSLisandro Dalcin Calling sequence of jac: 14696bc98fa9SBarry 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); 1470efe9872eSLisandro Dalcin 1471efe9872eSLisandro Dalcin + t - time at step/stage being solved 1472efe9872eSLisandro Dalcin . U - state vector 1473efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1474efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1475efe9872eSLisandro Dalcin . v - shift for U_t 1476efe9872eSLisandro Dalcin . a - shift for U_tt 1477efe9872eSLisandro 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 1478efe9872eSLisandro Dalcin . P - preconditioning matrix for J, may be same as J 1479efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine 1480efe9872eSLisandro Dalcin 1481bcf0153eSBarry Smith Level: beginner 1482bcf0153eSBarry Smith 1483efe9872eSLisandro Dalcin Notes: 1484bcf0153eSBarry Smith The matrices J and P are exactly the matrices that are used by `SNES` for the nonlinear solve. 1485efe9872eSLisandro Dalcin 1486efe9872eSLisandro Dalcin The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be 1487efe9872eSLisandro 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. 1488efe9872eSLisandro Dalcin The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U where the positive "shift" 1489bc77d74cSLisandro Dalcin parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states. 1490efe9872eSLisandro Dalcin 1491bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetI2Function()`, `TSGetI2Jacobian()` 1492efe9872eSLisandro Dalcin @*/ 1493d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetI2Jacobian(TS ts, Mat J, Mat P, TSI2Jacobian jac, void *ctx) 1494d71ae5a4SJacob Faibussowitsch { 1495efe9872eSLisandro Dalcin DM dm; 1496efe9872eSLisandro Dalcin 1497efe9872eSLisandro Dalcin PetscFunctionBegin; 1498efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1499efe9872eSLisandro Dalcin if (J) PetscValidHeaderSpecific(J, MAT_CLASSID, 2); 1500efe9872eSLisandro Dalcin if (P) PetscValidHeaderSpecific(P, MAT_CLASSID, 3); 15019566063dSJacob Faibussowitsch PetscCall(TSSetIJacobian(ts, J, P, NULL, NULL)); 15029566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 15039566063dSJacob Faibussowitsch PetscCall(DMTSSetI2Jacobian(dm, jac, ctx)); 1504efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1505efe9872eSLisandro Dalcin } 1506efe9872eSLisandro Dalcin 1507efe9872eSLisandro Dalcin /*@C 1508efe9872eSLisandro Dalcin TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep. 1509efe9872eSLisandro Dalcin 1510bcf0153eSBarry Smith Not Collective, but parallel objects are returned if `TS` is parallel 1511efe9872eSLisandro Dalcin 1512efe9872eSLisandro Dalcin Input Parameter: 1513bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 1514efe9872eSLisandro Dalcin 1515efe9872eSLisandro Dalcin Output Parameters: 1516efe9872eSLisandro Dalcin + J - The (approximate) Jacobian of F(t,U,U_t,U_tt) 1517efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J 1518efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices 1519efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine 1520efe9872eSLisandro Dalcin 1521bcf0153eSBarry Smith Level: advanced 1522bcf0153eSBarry Smith 152395452b02SPatrick Sanan Notes: 152495452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 1525efe9872eSLisandro Dalcin 1526bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetTimeStep()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()`, `TSSetI2Jacobian()`, `TSGetI2Function()`, `TSCreate()` 1527efe9872eSLisandro Dalcin @*/ 1528d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetI2Jacobian(TS ts, Mat *J, Mat *P, TSI2Jacobian *jac, void **ctx) 1529d71ae5a4SJacob Faibussowitsch { 1530efe9872eSLisandro Dalcin SNES snes; 1531efe9872eSLisandro Dalcin DM dm; 1532efe9872eSLisandro Dalcin 1533efe9872eSLisandro Dalcin PetscFunctionBegin; 15349566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 15359566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 15369566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes, J, P, NULL, NULL)); 15379566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 15389566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm, jac, ctx)); 1539efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1540efe9872eSLisandro Dalcin } 1541efe9872eSLisandro Dalcin 1542efe9872eSLisandro Dalcin /*@ 1543efe9872eSLisandro Dalcin TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0 1544efe9872eSLisandro Dalcin 1545bcf0153eSBarry Smith Collective on ts 1546efe9872eSLisandro Dalcin 1547efe9872eSLisandro Dalcin Input Parameters: 1548bcf0153eSBarry Smith + ts - the `TS` context 1549efe9872eSLisandro Dalcin . t - current time 1550efe9872eSLisandro Dalcin . U - state vector 1551efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t) 1552efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt) 1553efe9872eSLisandro Dalcin 1554efe9872eSLisandro Dalcin Output Parameter: 1555efe9872eSLisandro Dalcin . F - the residual vector 1556efe9872eSLisandro Dalcin 1557bcf0153eSBarry Smith Level: developer 1558bcf0153eSBarry Smith 1559efe9872eSLisandro Dalcin Note: 1560efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1561efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1562efe9872eSLisandro Dalcin 1563bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetI2Function()`, `TSGetI2Function()` 1564efe9872eSLisandro Dalcin @*/ 1565d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeI2Function(TS ts, PetscReal t, Vec U, Vec V, Vec A, Vec F) 1566d71ae5a4SJacob Faibussowitsch { 1567efe9872eSLisandro Dalcin DM dm; 1568efe9872eSLisandro Dalcin TSI2Function I2Function; 1569efe9872eSLisandro Dalcin void *ctx; 1570efe9872eSLisandro Dalcin TSRHSFunction rhsfunction; 1571efe9872eSLisandro Dalcin 1572efe9872eSLisandro Dalcin PetscFunctionBegin; 1573efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1574efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 1575efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V, VEC_CLASSID, 4); 1576efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A, VEC_CLASSID, 5); 1577efe9872eSLisandro Dalcin PetscValidHeaderSpecific(F, VEC_CLASSID, 6); 1578efe9872eSLisandro Dalcin 15799566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 15809566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Function(dm, &I2Function, &ctx)); 15819566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, NULL)); 1582efe9872eSLisandro Dalcin 1583efe9872eSLisandro Dalcin if (!I2Function) { 15849566063dSJacob Faibussowitsch PetscCall(TSComputeIFunction(ts, t, U, A, F, PETSC_FALSE)); 1585efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1586efe9872eSLisandro Dalcin } 1587efe9872eSLisandro Dalcin 15889566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_FunctionEval, ts, U, V, F)); 1589efe9872eSLisandro Dalcin 1590792fecdfSBarry Smith PetscCallBack("TS callback implicit function", I2Function(ts, t, U, V, A, F, ctx)); 1591efe9872eSLisandro Dalcin 1592efe9872eSLisandro Dalcin if (rhsfunction) { 1593efe9872eSLisandro Dalcin Vec Frhs; 15949566063dSJacob Faibussowitsch PetscCall(TSGetRHSVec_Private(ts, &Frhs)); 15959566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts, t, U, Frhs)); 15969566063dSJacob Faibussowitsch PetscCall(VecAXPY(F, -1, Frhs)); 1597efe9872eSLisandro Dalcin } 1598efe9872eSLisandro Dalcin 15999566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_FunctionEval, ts, U, V, F)); 1600efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1601efe9872eSLisandro Dalcin } 1602efe9872eSLisandro Dalcin 1603efe9872eSLisandro Dalcin /*@ 1604efe9872eSLisandro Dalcin TSComputeI2Jacobian - Evaluates the Jacobian of the DAE 1605efe9872eSLisandro Dalcin 1606bcf0153eSBarry Smith Collective on ts 1607efe9872eSLisandro Dalcin 1608efe9872eSLisandro Dalcin Input Parameters: 1609bcf0153eSBarry Smith + ts - the `TS` context 1610efe9872eSLisandro Dalcin . t - current timestep 1611efe9872eSLisandro Dalcin . U - state vector 1612efe9872eSLisandro Dalcin . V - time derivative of state vector 1613efe9872eSLisandro Dalcin . A - second time derivative of state vector 1614efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below 1615efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below 1616efe9872eSLisandro Dalcin 1617efe9872eSLisandro Dalcin Output Parameters: 1618efe9872eSLisandro Dalcin + J - Jacobian matrix 1619efe9872eSLisandro Dalcin - P - optional preconditioning matrix 1620efe9872eSLisandro Dalcin 1621bcf0153eSBarry Smith Level: developer 1622bcf0153eSBarry Smith 1623efe9872eSLisandro Dalcin Notes: 1624efe9872eSLisandro Dalcin If F(t,U,V,A)=0 is the DAE, the required Jacobian is 1625efe9872eSLisandro Dalcin 1626efe9872eSLisandro Dalcin dF/dU + shiftV*dF/dV + shiftA*dF/dA 1627efe9872eSLisandro Dalcin 1628efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1629efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1630efe9872eSLisandro Dalcin 1631bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetI2Jacobian()` 1632efe9872eSLisandro Dalcin @*/ 1633d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeI2Jacobian(TS ts, PetscReal t, Vec U, Vec V, Vec A, PetscReal shiftV, PetscReal shiftA, Mat J, Mat P) 1634d71ae5a4SJacob Faibussowitsch { 1635efe9872eSLisandro Dalcin DM dm; 1636efe9872eSLisandro Dalcin TSI2Jacobian I2Jacobian; 1637efe9872eSLisandro Dalcin void *ctx; 1638efe9872eSLisandro Dalcin TSRHSJacobian rhsjacobian; 1639efe9872eSLisandro Dalcin 1640efe9872eSLisandro Dalcin PetscFunctionBegin; 1641efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1642efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 1643efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V, VEC_CLASSID, 4); 1644efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A, VEC_CLASSID, 5); 1645efe9872eSLisandro Dalcin PetscValidHeaderSpecific(J, MAT_CLASSID, 8); 1646efe9872eSLisandro Dalcin PetscValidHeaderSpecific(P, MAT_CLASSID, 9); 1647efe9872eSLisandro Dalcin 16489566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 16499566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm, &I2Jacobian, &ctx)); 16509566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, &rhsjacobian, NULL)); 1651efe9872eSLisandro Dalcin 1652efe9872eSLisandro Dalcin if (!I2Jacobian) { 16539566063dSJacob Faibussowitsch PetscCall(TSComputeIJacobian(ts, t, U, A, shiftA, J, P, PETSC_FALSE)); 1654efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1655efe9872eSLisandro Dalcin } 1656efe9872eSLisandro Dalcin 16579566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_JacobianEval, ts, U, J, P)); 1658792fecdfSBarry Smith PetscCallBack("TS callback implicit Jacobian", I2Jacobian(ts, t, U, V, A, shiftV, shiftA, J, P, ctx)); 1659efe9872eSLisandro Dalcin if (rhsjacobian) { 1660d60b7d5cSBarry Smith Mat Jrhs, Prhs; 16619566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts, &Jrhs, &Prhs)); 16629566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts, t, U, Jrhs, Prhs)); 16639566063dSJacob Faibussowitsch PetscCall(MatAXPY(J, -1, Jrhs, ts->axpy_pattern)); 16649566063dSJacob Faibussowitsch if (P != J) PetscCall(MatAXPY(P, -1, Prhs, ts->axpy_pattern)); 1665efe9872eSLisandro Dalcin } 1666efe9872eSLisandro Dalcin 16679566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_JacobianEval, ts, U, J, P)); 1668efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1669efe9872eSLisandro Dalcin } 1670efe9872eSLisandro Dalcin 1671438f35afSJed Brown /*@C 1672438f35afSJed Brown TSSetTransientVariable - sets function to transform from state to transient variables 1673438f35afSJed Brown 1674438f35afSJed Brown Logically Collective 1675438f35afSJed Brown 16764165533cSJose E. Roman Input Parameters: 1677438f35afSJed Brown + ts - time stepping context on which to change the transient variable 1678a96d6ef6SBarry Smith . tvar - a function that transforms to transient variables 1679438f35afSJed Brown - ctx - a context for tvar 1680438f35afSJed Brown 1681a96d6ef6SBarry Smith Calling sequence of tvar: 1682a96d6ef6SBarry Smith $ PetscErrorCode tvar(TS ts,Vec p,Vec c,void *ctx); 1683a96d6ef6SBarry Smith 1684a96d6ef6SBarry Smith + ts - timestep context 16856aad120cSJose E. Roman . p - input vector (primitive form) 1686a96d6ef6SBarry Smith . c - output vector, transient variables (conservative form) 1687a96d6ef6SBarry Smith - ctx - [optional] user-defined function context 1688a96d6ef6SBarry Smith 1689438f35afSJed Brown Level: advanced 1690438f35afSJed Brown 1691438f35afSJed Brown Notes: 1692bcf0153eSBarry Smith This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., `TSBDF`) 1693438f35afSJed Brown can be conservative. In this context, primitive variables P are used to model the state (e.g., because they lead to 1694438f35afSJed Brown well-conditioned formulations even in limiting cases such as low-Mach or zero porosity). The transient variable is 1695438f35afSJed Brown C(P), specified by calling this function. An IFunction thus receives arguments (P, Cdot) and the IJacobian must be 1696438f35afSJed Brown evaluated via the chain rule, as in 1697438f35afSJed Brown 1698438f35afSJed Brown dF/dP + shift * dF/dCdot dC/dP. 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)); 1710438f35afSJed Brown PetscFunctionReturn(0); 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: 1728bcf0153eSBarry 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 } 1748e3c11fc1SJed Brown PetscFunctionReturn(0); 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: 1757e3c11fc1SJed Brown . 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; 1776e3c11fc1SJed Brown PetscFunctionReturn(0); 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 1783bcf0153eSBarry Smith Logically Collective on ts 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; 1804efe9872eSLisandro Dalcin PetscFunctionReturn(0); 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 1813bcf0153eSBarry Smith Not Collective, but Vec 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; 1834efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1835efe9872eSLisandro Dalcin } 1836efe9872eSLisandro Dalcin 183755849f57SBarry Smith /*@C 1838bcf0153eSBarry Smith TSLoad - Loads a `TS` that has been stored in binary with `TSView()`. 183955849f57SBarry Smith 184055849f57SBarry Smith Collective on PetscViewer 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)); 188055849f57SBarry Smith PetscFunctionReturn(0); 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 1891bcf0153eSBarry Smith Collective on ts 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)); 1907fe2efc57SMark PetscFunctionReturn(0); 1908fe2efc57SMark } 1909fe2efc57SMark 19107e2c5f70SBarry Smith /*@C 1911bcf0153eSBarry Smith TSView - Prints the `TS` data structure. 1912d763cef2SBarry Smith 1913bcf0153eSBarry Smith Collective on ts 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)); 2058d763cef2SBarry Smith PetscFunctionReturn(0); 2059d763cef2SBarry Smith } 2060d763cef2SBarry Smith 2061b07ff414SBarry Smith /*@ 2062d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 2063d763cef2SBarry Smith the timesteppers. 2064d763cef2SBarry Smith 2065bcf0153eSBarry Smith Logically Collective on ts 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: 2074bcf0153eSBarry Smith To use this from Fortran you must write a Fortran interface definition for this 2075bcf0153eSBarry 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; 2084d763cef2SBarry Smith PetscFunctionReturn(0); 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: 210295452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 2103daf670e6SBarry 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; 2112d763cef2SBarry Smith PetscFunctionReturn(0); 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; 213680275a0aSLisandro Dalcin PetscFunctionReturn(0); 213780275a0aSLisandro Dalcin } 213880275a0aSLisandro Dalcin 213980275a0aSLisandro Dalcin /*@ 214080275a0aSLisandro Dalcin TSSetStepNumber - Sets the number of steps completed. 214180275a0aSLisandro Dalcin 2142bcf0153eSBarry Smith Logically Collective on ts 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 215780275a0aSLisandro Dalcin 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; 2169d763cef2SBarry Smith PetscFunctionReturn(0); 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 2176bcf0153eSBarry Smith Logically Collective on ts 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; 2192d763cef2SBarry Smith PetscFunctionReturn(0); 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 2200bcf0153eSBarry Smith Logically Collective on ts 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; 2228a43b19c4SJed Brown PetscFunctionReturn(0); 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; 2252f6953c82SLisandro Dalcin PetscFunctionReturn(0); 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; 2276d763cef2SBarry Smith PetscFunctionReturn(0); 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; 2307d763cef2SBarry Smith PetscFunctionReturn(0); 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). 2320b2bf4f3aSDebojyoti Ghosh . n - If v is PETSC_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 232403fe5f5eSDebojyoti Ghosh (may be PETSC_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); 233703fe5f5eSDebojyoti Ghosh PetscFunctionReturn(0); 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)); 23604cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 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)); 23874cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 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); 241157df6a1bSDebojyoti Ghosh PetscFunctionReturn(0); 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 } 2443d763cef2SBarry Smith PetscFunctionReturn(0); 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; 2472bdad233fSMatthew Knepley PetscFunctionReturn(0); 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; 2489303a5415SBarry Smith PetscFunctionReturn(0); 2490303a5415SBarry Smith } 2491303a5415SBarry Smith 2492d763cef2SBarry Smith /*@ 2493303a5415SBarry Smith TSSetUp - Sets up the internal data structures for the later use of a timestepper. 2494d763cef2SBarry Smith 2495bcf0153eSBarry Smith Collective on ts 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); 2523277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 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; 2597277b19d0SLisandro Dalcin PetscFunctionReturn(0); 2598277b19d0SLisandro Dalcin } 2599277b19d0SLisandro Dalcin 2600f6a906c0SBarry Smith /*@ 2601bcf0153eSBarry Smith TSReset - Resets a `TS` context and removes any allocated `Vec`s and `Mat`s. 2602277b19d0SLisandro Dalcin 2603bcf0153eSBarry Smith Collective on ts 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; 2655d763cef2SBarry Smith PetscFunctionReturn(0); 2656d763cef2SBarry Smith } 2657d763cef2SBarry Smith 26581fb7b255SJunchao Zhang /*@C 2659d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2660bcf0153eSBarry Smith with `TSCreate()`. 2661d763cef2SBarry Smith 2662bcf0153eSBarry Smith Collective on ts 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; 26746bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 2675ecf68647SHong Zhang PetscValidHeaderSpecific(*ts, TS_CLASSID, 1); 26769371c9d4SSatish Balay if (--((PetscObject)(*ts))->refct > 0) { 26779371c9d4SSatish Balay *ts = NULL; 26789371c9d4SSatish Balay PetscFunctionReturn(0); 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)); 2701d763cef2SBarry Smith PetscFunctionReturn(0); 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 2724bcf0153eSBarry 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; 2742d763cef2SBarry Smith PetscFunctionReturn(0); 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)); 2776deb2cd25SJed Brown PetscFunctionReturn(0); 2777deb2cd25SJed Brown } 2778deb2cd25SJed Brown 2779d8e5e3e6SSatish Balay /*@ 2780bcf0153eSBarry Smith TSGetKSP - Returns the `KSP` (linear solver) associated with 2781bcf0153eSBarry Smith a `TS` (timestepper) context. 2782d763cef2SBarry Smith 2783bcf0153eSBarry 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`; 2799bcf0153eSBarry 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)); 2814d763cef2SBarry Smith PetscFunctionReturn(0); 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 2822bcf0153eSBarry Smith Logically Collective on ts 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; 2845618ce8baSLisandro Dalcin PetscFunctionReturn(0); 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; 2869618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2870618ce8baSLisandro Dalcin } 2871618ce8baSLisandro Dalcin 2872618ce8baSLisandro Dalcin /*@ 2873618ce8baSLisandro Dalcin TSSetMaxTime - Sets the maximum (or final) time for timestepping. 2874618ce8baSLisandro Dalcin 2875bcf0153eSBarry Smith Logically Collective on ts 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; 2897618ce8baSLisandro Dalcin PetscFunctionReturn(0); 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; 2921618ce8baSLisandro Dalcin PetscFunctionReturn(0); 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)); 2936aaa6c58dSLisandro Dalcin PetscFunctionReturn(0); 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 } 2957adb62b0dSMatthew Knepley PetscFunctionReturn(0); 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; 2974d763cef2SBarry Smith PetscFunctionReturn(0); 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 3003bcf0153eSBarry Smith Logically Collective on ts 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)); 3026d763cef2SBarry Smith PetscFunctionReturn(0); 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 3033bcf0153eSBarry Smith Logically Collective on ts 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; 3053000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3054000e7ae3SMatthew Knepley } 3055000e7ae3SMatthew Knepley 305609ee8438SJed Brown /*@ 3057bcf0153eSBarry Smith TSPreStep - Runs the user-defined pre-step function provided with `TSSetPreStep()` 30583f2090d5SJed Brown 3059bcf0153eSBarry Smith Collective on ts 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 } 30913f2090d5SJed Brown PetscFunctionReturn(0); 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 3098bcf0153eSBarry Smith Logically Collective on ts 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; 3123b8123daeSJed Brown PetscFunctionReturn(0); 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 3130bcf0153eSBarry Smith Logically Collective on ts 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; 31559be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 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 3162bcf0153eSBarry Smith Logically Collective on ts 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; 3189c688d042SShri Abhyankar PetscFunctionReturn(0); 3190c688d042SShri Abhyankar } 3191c688d042SShri Abhyankar 3192b8123daeSJed Brown /*@ 3193bcf0153eSBarry Smith TSPreStage - Runs the user-defined pre-stage function set using `TSSetPreStage()` 3194b8123daeSJed Brown 3195bcf0153eSBarry Smith Collective on ts 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)); 3214b8123daeSJed Brown PetscFunctionReturn(0); 3215b8123daeSJed Brown } 3216b8123daeSJed Brown 32179be3e283SDebojyoti Ghosh /*@ 3218bcf0153eSBarry Smith TSPostStage - Runs the user-defined post-stage function set using `TSSetPostStage()` 32199be3e283SDebojyoti Ghosh 3220bcf0153eSBarry Smith Collective on ts 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)); 32429be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 32439be3e283SDebojyoti Ghosh } 32449be3e283SDebojyoti Ghosh 3245c688d042SShri Abhyankar /*@ 3246bcf0153eSBarry Smith TSPostEvaluate - Runs the user-defined post-evaluate function set using `TSSetPostEvaluate()` 3247c688d042SShri Abhyankar 3248bcf0153eSBarry Smith Collective on ts 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 } 3275c688d042SShri Abhyankar PetscFunctionReturn(0); 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 3282bcf0153eSBarry Smith Logically Collective on ts 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: 3294bcf0153eSBarry Smith The function set by `TSSetPostStep()` is called after each successful step. The solution vector X 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; 3305000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3306000e7ae3SMatthew Knepley } 3307000e7ae3SMatthew Knepley 330809ee8438SJed Brown /*@ 3309bcf0153eSBarry Smith TSPostStep - Runs the user-defined post-step function that was set with `TSSetPotsStep()` 33103f2090d5SJed Brown 3311bcf0153eSBarry Smith Collective on ts 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 } 33433f2090d5SJed Brown PetscFunctionReturn(0); 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 3349bcf0153eSBarry Smith Collective on ts 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); 3372cd652676SJed Brown PetscFunctionReturn(0); 3373cd652676SJed Brown } 3374cd652676SJed Brown 3375d763cef2SBarry Smith /*@ 33766d9e5789SSean Farley TSStep - Steps one time step 3377d763cef2SBarry Smith 3378bcf0153eSBarry Smith Collective on ts 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 34403c633725SBarry Smith PetscCheck(ts->reason >= 0 || !ts->errorifstepfailed || 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]); 34413c633725SBarry Smith PetscCheck(ts->reason >= 0 || !ts->errorifstepfailed, PetscObjectComm((PetscObject)ts), PETSC_ERR_NOT_CONVERGED, "TSStep has failed due to %s", TSConvergedReasons[ts->reason]); 344208c7845fSBarry Smith PetscFunctionReturn(0); 344308c7845fSBarry Smith } 344408c7845fSBarry Smith 344508c7845fSBarry Smith /*@ 34467cbde773SLisandro Dalcin TSEvaluateWLTE - Evaluate the weighted local truncation error norm 34477cbde773SLisandro Dalcin at the end of a time step with a given order of accuracy. 34487cbde773SLisandro Dalcin 3449bcf0153eSBarry Smith Collective on ts 34507cbde773SLisandro Dalcin 34514165533cSJose E. Roman Input Parameters: 34527cbde773SLisandro Dalcin + ts - time stepping context 3453bcf0153eSBarry Smith - wnormtype - norm type, either `NORM_2` or `NORM_INFINITY` 34547cbde773SLisandro Dalcin 345597bb3fdcSJose E. Roman Input/Output Parameter: 3456bcf0153eSBarry Smith . order - optional, desired order for the error evaluation or `PETSC_DECIDE`; 345797bb3fdcSJose E. Roman on output, the actual order of the error evaluation 345897bb3fdcSJose E. Roman 345997bb3fdcSJose E. Roman Output Parameter: 346097bb3fdcSJose E. Roman . wlte - the weighted local truncation error norm 34617cbde773SLisandro Dalcin 34627cbde773SLisandro Dalcin Level: advanced 34637cbde773SLisandro Dalcin 3464bcf0153eSBarry Smith Note: 34657cbde773SLisandro Dalcin If the timestepper cannot evaluate the error in a particular step 34667cbde773SLisandro Dalcin (eg. in the first step or restart steps after event handling), 34677cbde773SLisandro Dalcin this routine returns wlte=-1.0 . 34687cbde773SLisandro Dalcin 3469bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSStep()`, `TSAdapt`, `TSErrorWeightedNorm()` 34707cbde773SLisandro Dalcin @*/ 3471d71ae5a4SJacob Faibussowitsch PetscErrorCode TSEvaluateWLTE(TS ts, NormType wnormtype, PetscInt *order, PetscReal *wlte) 3472d71ae5a4SJacob Faibussowitsch { 34737cbde773SLisandro Dalcin PetscFunctionBegin; 34747cbde773SLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 34757cbde773SLisandro Dalcin PetscValidType(ts, 1); 3476064a246eSJacob Faibussowitsch PetscValidLogicalCollectiveEnum(ts, wnormtype, 2); 34777cbde773SLisandro Dalcin if (order) PetscValidIntPointer(order, 3); 34787cbde773SLisandro Dalcin if (order) PetscValidLogicalCollectiveInt(ts, *order, 3); 34797cbde773SLisandro Dalcin PetscValidRealPointer(wlte, 4); 34803c633725SBarry Smith PetscCheck(wnormtype == NORM_2 || wnormtype == NORM_INFINITY, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "No support for norm type %s", NormTypes[wnormtype]); 3481dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, evaluatewlte, wnormtype, order, wlte); 34827cbde773SLisandro Dalcin PetscFunctionReturn(0); 34837cbde773SLisandro Dalcin } 34847cbde773SLisandro Dalcin 348505175c85SJed Brown /*@ 348605175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 348705175c85SJed Brown 3488bcf0153eSBarry Smith Collective on ts 348905175c85SJed Brown 34904165533cSJose E. Roman Input Parameters: 34911c3436cfSJed Brown + ts - time stepping context 34921c3436cfSJed Brown . order - desired order of accuracy 34930298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 349405175c85SJed Brown 34954165533cSJose E. Roman Output Parameter: 34960910c330SBarry Smith . U - state at the end of the current step 349705175c85SJed Brown 349805175c85SJed Brown Level: advanced 349905175c85SJed Brown 3500108c343cSJed Brown Notes: 3501108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3502108c343cSJed Brown 3503bcf0153eSBarry 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. 3504bcf0153eSBarry Smith 3505bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSStep()`, `TSAdapt` 350605175c85SJed Brown @*/ 3507d71ae5a4SJacob Faibussowitsch PetscErrorCode TSEvaluateStep(TS ts, PetscInt order, Vec U, PetscBool *done) 3508d71ae5a4SJacob Faibussowitsch { 350905175c85SJed Brown PetscFunctionBegin; 351005175c85SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 351105175c85SJed Brown PetscValidType(ts, 1); 35120910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 3513dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, evaluatestep, order, U, done); 351405175c85SJed Brown PetscFunctionReturn(0); 351505175c85SJed Brown } 351605175c85SJed Brown 3517aad739acSMatthew G. Knepley /*@C 35182e61be88SMatthew G. Knepley TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping. 3519aad739acSMatthew G. Knepley 3520aad739acSMatthew G. Knepley Not collective 3521aad739acSMatthew G. Knepley 35224165533cSJose E. Roman Input Parameter: 3523aad739acSMatthew G. Knepley . ts - time stepping context 3524aad739acSMatthew G. Knepley 35254165533cSJose E. Roman Output Parameter: 35262e61be88SMatthew G. Knepley . initConditions - The function which computes an initial condition 3527aad739acSMatthew G. Knepley 3528bcf0153eSBarry Smith The calling sequence for the function is 3529bcf0153eSBarry Smith .vb 3530bcf0153eSBarry Smith initCondition(TS ts, Vec u) 3531bcf0153eSBarry Smith ts - The timestepping context 3532bcf0153eSBarry Smith u - The input vector in which the initial condition is stored 3533bcf0153eSBarry Smith .ve 3534bcf0153eSBarry Smith 3535aad739acSMatthew G. Knepley Level: advanced 3536aad739acSMatthew G. Knepley 3537bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetComputeInitialCondition()`, `TSComputeInitialCondition()` 3538aad739acSMatthew G. Knepley @*/ 3539d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS, Vec)) 3540d71ae5a4SJacob Faibussowitsch { 3541aad739acSMatthew G. Knepley PetscFunctionBegin; 3542aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 35432e61be88SMatthew G. Knepley PetscValidPointer(initCondition, 2); 35442e61be88SMatthew G. Knepley *initCondition = ts->ops->initcondition; 3545aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3546aad739acSMatthew G. Knepley } 3547aad739acSMatthew G. Knepley 3548aad739acSMatthew G. Knepley /*@C 35492e61be88SMatthew G. Knepley TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping. 3550aad739acSMatthew G. Knepley 3551aad739acSMatthew G. Knepley Logically collective on ts 3552aad739acSMatthew G. Knepley 35534165533cSJose E. Roman Input Parameters: 3554aad739acSMatthew G. Knepley + ts - time stepping context 35552e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition 3556aad739acSMatthew G. Knepley 3557a96d6ef6SBarry Smith Calling sequence for initCondition: 3558a96d6ef6SBarry Smith $ PetscErrorCode initCondition(TS ts, Vec u) 3559a96d6ef6SBarry Smith + ts - The timestepping context 3560a96d6ef6SBarry Smith - u - The input vector in which the initial condition is to be stored 3561aad739acSMatthew G. Knepley 3562bcf0153eSBarry Smith Level: advanced 3563bcf0153eSBarry Smith 3564bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetComputeInitialCondition()`, `TSComputeInitialCondition()` 3565aad739acSMatthew G. Knepley @*/ 3566d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS, Vec)) 3567d71ae5a4SJacob Faibussowitsch { 3568aad739acSMatthew G. Knepley PetscFunctionBegin; 3569aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 35702e61be88SMatthew G. Knepley PetscValidFunction(initCondition, 2); 35712e61be88SMatthew G. Knepley ts->ops->initcondition = initCondition; 3572aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3573aad739acSMatthew G. Knepley } 3574aad739acSMatthew G. Knepley 3575aad739acSMatthew G. Knepley /*@ 3576bcf0153eSBarry Smith TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set with `TSSetComputeInitialCondition()` 3577aad739acSMatthew G. Knepley 3578aad739acSMatthew G. Knepley Collective on ts 3579aad739acSMatthew G. Knepley 35804165533cSJose E. Roman Input Parameters: 3581aad739acSMatthew G. Knepley + ts - time stepping context 3582bcf0153eSBarry Smith - u - The `Vec` to store the condition in which will be used in `TSSolve()` 3583aad739acSMatthew G. Knepley 3584aad739acSMatthew G. Knepley Level: advanced 3585aad739acSMatthew G. Knepley 3586bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetComputeInitialCondition()`, `TSSetComputeInitialCondition()`, `TSSolve()` 3587aad739acSMatthew G. Knepley @*/ 3588d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeInitialCondition(TS ts, Vec u) 3589d71ae5a4SJacob Faibussowitsch { 3590aad739acSMatthew G. Knepley PetscFunctionBegin; 3591aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3592aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3593dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, initcondition, u); 3594aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3595aad739acSMatthew G. Knepley } 3596aad739acSMatthew G. Knepley 3597aad739acSMatthew G. Knepley /*@C 3598aad739acSMatthew G. Knepley TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping. 3599aad739acSMatthew G. Knepley 3600aad739acSMatthew G. Knepley Not collective 3601aad739acSMatthew G. Knepley 36024165533cSJose E. Roman Input Parameter: 3603aad739acSMatthew G. Knepley . ts - time stepping context 3604aad739acSMatthew G. Knepley 36054165533cSJose E. Roman Output Parameter: 3606aad739acSMatthew G. Knepley . exactError - The function which computes the solution error 3607aad739acSMatthew G. Knepley 3608a96d6ef6SBarry Smith Calling sequence for exactError: 3609a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u) 3610a96d6ef6SBarry Smith + ts - The timestepping context 3611a96d6ef6SBarry Smith . u - The approximate solution vector 3612a96d6ef6SBarry Smith - e - The input vector in which the error is stored 3613aad739acSMatthew G. Knepley 3614bcf0153eSBarry Smith Level: advanced 3615bcf0153eSBarry Smith 3616bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetComputeExactError()`, `TSComputeExactError()` 3617aad739acSMatthew G. Knepley @*/ 3618d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS, Vec, Vec)) 3619d71ae5a4SJacob Faibussowitsch { 3620aad739acSMatthew G. Knepley PetscFunctionBegin; 3621aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3622aad739acSMatthew G. Knepley PetscValidPointer(exactError, 2); 3623aad739acSMatthew G. Knepley *exactError = ts->ops->exacterror; 3624aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3625aad739acSMatthew G. Knepley } 3626aad739acSMatthew G. Knepley 3627aad739acSMatthew G. Knepley /*@C 3628aad739acSMatthew G. Knepley TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping. 3629aad739acSMatthew G. Knepley 3630aad739acSMatthew G. Knepley Logically collective on ts 3631aad739acSMatthew G. Knepley 36324165533cSJose E. Roman Input Parameters: 3633aad739acSMatthew G. Knepley + ts - time stepping context 3634aad739acSMatthew G. Knepley - exactError - The function which computes the solution error 3635aad739acSMatthew G. Knepley 3636a96d6ef6SBarry Smith Calling sequence for exactError: 3637a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u) 3638a96d6ef6SBarry Smith + ts - The timestepping context 3639a96d6ef6SBarry Smith . u - The approximate solution vector 3640a96d6ef6SBarry Smith - e - The input vector in which the error is stored 3641aad739acSMatthew G. Knepley 3642bcf0153eSBarry Smith Level: advanced 3643bcf0153eSBarry Smith 3644bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetComputeExactError()`, `TSComputeExactError()` 3645aad739acSMatthew G. Knepley @*/ 3646d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS, Vec, Vec)) 3647d71ae5a4SJacob Faibussowitsch { 3648aad739acSMatthew G. Knepley PetscFunctionBegin; 3649aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3650f907fdbfSMatthew G. Knepley PetscValidFunction(exactError, 2); 3651aad739acSMatthew G. Knepley ts->ops->exacterror = exactError; 3652aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3653aad739acSMatthew G. Knepley } 3654aad739acSMatthew G. Knepley 3655aad739acSMatthew G. Knepley /*@ 3656bcf0153eSBarry Smith TSComputeExactError - Compute the solution error for the timestepping using the function previously set with `TSSetComputeExactError()` 3657aad739acSMatthew G. Knepley 3658aad739acSMatthew G. Knepley Collective on ts 3659aad739acSMatthew G. Knepley 36604165533cSJose E. Roman Input Parameters: 3661aad739acSMatthew G. Knepley + ts - time stepping context 3662aad739acSMatthew G. Knepley . u - The approximate solution 3663bcf0153eSBarry Smith - e - The `Vec` used to store the error 3664aad739acSMatthew G. Knepley 3665aad739acSMatthew G. Knepley Level: advanced 3666aad739acSMatthew G. Knepley 3667bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetComputeInitialCondition()`, `TSSetComputeInitialCondition()`, `TSSolve()` 3668aad739acSMatthew G. Knepley @*/ 3669d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e) 3670d71ae5a4SJacob Faibussowitsch { 3671aad739acSMatthew G. Knepley PetscFunctionBegin; 3672aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3673aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3674aad739acSMatthew G. Knepley PetscValidHeaderSpecific(e, VEC_CLASSID, 3); 3675dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, exacterror, u, e); 3676aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3677aad739acSMatthew G. Knepley } 3678aad739acSMatthew G. Knepley 3679b1cb36f3SHong Zhang /*@ 36806a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 36816a4d4014SLisandro Dalcin 3682bcf0153eSBarry Smith Collective on ts 36836a4d4014SLisandro Dalcin 3684d8d19677SJose E. Roman Input Parameters: 3685bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 3686bcf0153eSBarry Smith - u - the solution vector (can be null if `TSSetSolution()` was used and `TSSetExactFinalTime`(ts,`TS_EXACTFINALTIME_MATCHSTEP`) was not used, 368763e21af5SBarry Smith otherwise must contain the initial conditions and will contain the solution at the final requested time 36885a3a76d0SJed Brown 36896a4d4014SLisandro Dalcin Level: beginner 36906a4d4014SLisandro Dalcin 36915a3a76d0SJed Brown Notes: 36925a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 3693bcf0153eSBarry Smith held state accessible by `TSGetSolution()` and `TSGetTime()` because the method may have 36945a3a76d0SJed Brown stepped over the final time. 36955a3a76d0SJed Brown 3696bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSCreate()`, `TSSetSolution()`, `TSStep()`, `TSGetTime()`, `TSGetSolveTime()` 36976a4d4014SLisandro Dalcin @*/ 3698d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSolve(TS ts, Vec u) 3699d71ae5a4SJacob Faibussowitsch { 3700b06615a5SLisandro Dalcin Vec solution; 3701f22f69f0SBarry Smith 37026a4d4014SLisandro Dalcin PetscFunctionBegin; 37030700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3704f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3705303a5415SBarry Smith 37069566063dSJacob Faibussowitsch PetscCall(TSSetExactFinalTimeDefault(ts)); 3707ee41a567SStefano 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 */ 37080910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 37099566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u, &solution)); 37109566063dSJacob Faibussowitsch PetscCall(TSSetSolution(ts, solution)); 37119566063dSJacob Faibussowitsch PetscCall(VecDestroy(&solution)); /* grant ownership */ 37125a3a76d0SJed Brown } 37139566063dSJacob Faibussowitsch PetscCall(VecCopy(u, ts->vec_sol)); 37143c633725SBarry Smith PetscCheck(!ts->forward_solve, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE"); 37151baa6e33SBarry Smith } else if (u) PetscCall(TSSetSolution(ts, u)); 37169566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts)); 37179566063dSJacob Faibussowitsch PetscCall(TSTrajectorySetUp(ts->trajectory, ts)); 3718a6772fa2SLisandro Dalcin 37193c633725SBarry 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>"); 37203c633725SBarry 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()"); 37213c633725SBarry 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"); 37224a658b32SHong 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"); 37234a658b32SHong Zhang 3724e1db57b0SHong 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 */ 37254a658b32SHong Zhang PetscCall(VecCopy(ts->vec_sol, ts->tspan->vecs_sol[0])); 37264a658b32SHong Zhang ts->tspan->spanctr = 1; 37274a658b32SHong Zhang } 3728a6772fa2SLisandro Dalcin 37291baa6e33SBarry Smith if (ts->forward_solve) PetscCall(TSForwardSetUp(ts)); 3730715f1b00SHong Zhang 3731e7069c78SShri /* reset number of steps only when the step is not restarted. ARKIMEX 3732715f1b00SHong Zhang restarts the step after an event. Resetting these counters in such case causes 3733e7069c78SShri TSTrajectory to incorrectly save the output files 3734e7069c78SShri */ 3735715f1b00SHong Zhang /* reset time step and iteration counters */ 37362808aa04SLisandro Dalcin if (!ts->steps) { 37375ef26d82SJed Brown ts->ksp_its = 0; 37385ef26d82SJed Brown ts->snes_its = 0; 3739c610991cSLisandro Dalcin ts->num_snes_failures = 0; 3740c610991cSLisandro Dalcin ts->reject = 0; 37412808aa04SLisandro Dalcin ts->steprestart = PETSC_TRUE; 37422808aa04SLisandro Dalcin ts->steprollback = PETSC_FALSE; 37437d51462cSStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 37442808aa04SLisandro Dalcin } 3745e97c63d7SStefano Zampini 37464a658b32SHong Zhang /* make sure initial time step does not overshoot final time or the next point in tspan */ 3747e97c63d7SStefano Zampini if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) { 37484a658b32SHong Zhang PetscReal maxdt; 3749e97c63d7SStefano Zampini PetscReal dt = ts->time_step; 3750e97c63d7SStefano Zampini 37514a658b32SHong Zhang if (ts->tspan) maxdt = ts->tspan->span_times[ts->tspan->spanctr] - ts->ptime; 37524a658b32SHong Zhang else maxdt = ts->max_time - ts->ptime; 3753e97c63d7SStefano Zampini ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt, maxdt, 10 * PETSC_MACHINE_EPSILON, 0) ? maxdt : dt); 3754e97c63d7SStefano Zampini } 3755193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 3756193ac0bcSJed Brown 3757900f6b5bSMatthew G. Knepley { 3758900f6b5bSMatthew G. Knepley PetscViewer viewer; 3759900f6b5bSMatthew G. Knepley PetscViewerFormat format; 3760900f6b5bSMatthew G. Knepley PetscBool flg; 3761900f6b5bSMatthew G. Knepley static PetscBool incall = PETSC_FALSE; 3762900f6b5bSMatthew G. Knepley 3763900f6b5bSMatthew G. Knepley if (!incall) { 3764900f6b5bSMatthew G. Knepley /* Estimate the convergence rate of the time discretization */ 37659566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts), ((PetscObject)ts)->options, ((PetscObject)ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg)); 3766900f6b5bSMatthew G. Knepley if (flg) { 3767900f6b5bSMatthew G. Knepley PetscConvEst conv; 3768900f6b5bSMatthew G. Knepley DM dm; 3769900f6b5bSMatthew G. Knepley PetscReal *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */ 3770900f6b5bSMatthew G. Knepley PetscInt Nf; 3771f2ed2dc7SMatthew G. Knepley PetscBool checkTemporal = PETSC_TRUE; 3772900f6b5bSMatthew G. Knepley 3773900f6b5bSMatthew G. Knepley incall = PETSC_TRUE; 37749566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject)ts)->prefix, "-ts_convergence_temporal", &checkTemporal, &flg)); 37759566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 37769566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &Nf)); 37779566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(PetscMax(Nf, 1), &alpha)); 37789566063dSJacob Faibussowitsch PetscCall(PetscConvEstCreate(PetscObjectComm((PetscObject)ts), &conv)); 37799566063dSJacob Faibussowitsch PetscCall(PetscConvEstUseTS(conv, checkTemporal)); 37809566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetSolver(conv, (PetscObject)ts)); 37819566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetFromOptions(conv)); 37829566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetUp(conv)); 37839566063dSJacob Faibussowitsch PetscCall(PetscConvEstGetConvRate(conv, alpha)); 37849566063dSJacob Faibussowitsch PetscCall(PetscViewerPushFormat(viewer, format)); 37859566063dSJacob Faibussowitsch PetscCall(PetscConvEstRateView(conv, alpha, viewer)); 37869566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(viewer)); 37879566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&viewer)); 37889566063dSJacob Faibussowitsch PetscCall(PetscConvEstDestroy(&conv)); 37899566063dSJacob Faibussowitsch PetscCall(PetscFree(alpha)); 3790900f6b5bSMatthew G. Knepley incall = PETSC_FALSE; 3791900f6b5bSMatthew G. Knepley } 3792900f6b5bSMatthew G. Knepley } 3793900f6b5bSMatthew G. Knepley } 3794900f6b5bSMatthew G. Knepley 37959566063dSJacob Faibussowitsch PetscCall(TSViewFromOptions(ts, NULL, "-ts_view_pre")); 3796f05ece33SBarry Smith 3797193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 3798dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, solve); 37999566063dSJacob Faibussowitsch if (u) PetscCall(VecCopy(ts->vec_sol, u)); 3800cc708dedSBarry Smith ts->solvetime = ts->ptime; 3801a6772fa2SLisandro Dalcin solution = ts->vec_sol; 3802be5899b3SLisandro Dalcin } else { /* Step the requested number of timesteps. */ 3803db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3804db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3805e7069c78SShri 38062808aa04SLisandro Dalcin if (!ts->steps) { 38079566063dSJacob Faibussowitsch PetscCall(TSTrajectorySet(ts->trajectory, ts, ts->steps, ts->ptime, ts->vec_sol)); 38089566063dSJacob Faibussowitsch PetscCall(TSEventInitialize(ts->event, ts, ts->ptime, ts->vec_sol)); 38092808aa04SLisandro Dalcin } 38106427ac75SLisandro Dalcin 3811e1a7a14fSJed Brown while (!ts->reason) { 38129566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts, ts->steps, ts->ptime, ts->vec_sol)); 38131e66621cSBarry Smith if (!ts->steprollback) PetscCall(TSPreStep(ts)); 38149566063dSJacob Faibussowitsch PetscCall(TSStep(ts)); 38151baa6e33SBarry Smith if (ts->testjacobian) PetscCall(TSRHSJacobianTest(ts, NULL)); 38161baa6e33SBarry Smith if (ts->testjacobiantranspose) PetscCall(TSRHSJacobianTestTranspose(ts, NULL)); 3817cd4cee2dSHong Zhang if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */ 38187b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */ 38199566063dSJacob Faibussowitsch PetscCall(TSForwardCostIntegral(ts)); 38207b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps++; 3821b1cb36f3SHong Zhang } 382258818c2dSLisandro Dalcin if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */ 38237b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */ 38249566063dSJacob Faibussowitsch PetscCall(TSForwardStep(ts)); 38257b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps++; 3826715f1b00SHong Zhang } 38279566063dSJacob Faibussowitsch PetscCall(TSPostEvaluate(ts)); 38289566063dSJacob 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. */ 38291baa6e33SBarry Smith if (ts->steprollback) PetscCall(TSPostEvaluate(ts)); 3830e783b05fSHong Zhang if (!ts->steprollback) { 38319566063dSJacob Faibussowitsch PetscCall(TSTrajectorySet(ts->trajectory, ts, ts->steps, ts->ptime, ts->vec_sol)); 38329566063dSJacob Faibussowitsch PetscCall(TSPostStep(ts)); 3833aeb4809dSShri Abhyankar } 3834193ac0bcSJed Brown } 38359566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts, ts->steps, ts->ptime, ts->vec_sol)); 38366427ac75SLisandro Dalcin 383749354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 38389566063dSJacob Faibussowitsch PetscCall(TSInterpolate(ts, ts->max_time, u)); 3839cc708dedSBarry Smith ts->solvetime = ts->max_time; 3840b06615a5SLisandro Dalcin solution = u; 38419566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts, -1, ts->solvetime, solution)); 38420574a7fbSJed Brown } else { 38439566063dSJacob Faibussowitsch if (u) PetscCall(VecCopy(ts->vec_sol, u)); 3844cc708dedSBarry Smith ts->solvetime = ts->ptime; 3845b06615a5SLisandro Dalcin solution = ts->vec_sol; 38460574a7fbSJed Brown } 3847193ac0bcSJed Brown } 3848d2daff3dSHong Zhang 38499566063dSJacob Faibussowitsch PetscCall(TSViewFromOptions(ts, NULL, "-ts_view")); 38509566063dSJacob Faibussowitsch PetscCall(VecViewFromOptions(solution, (PetscObject)ts, "-ts_view_solution")); 38519566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsBlock((PetscObject)ts)); 38521baa6e33SBarry Smith if (ts->adjoint_solve) PetscCall(TSAdjointSolve(ts)); 38536a4d4014SLisandro Dalcin PetscFunctionReturn(0); 38546a4d4014SLisandro Dalcin } 38556a4d4014SLisandro Dalcin 3856d763cef2SBarry Smith /*@ 3857b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 3858d763cef2SBarry Smith 3859d763cef2SBarry Smith Not Collective 3860d763cef2SBarry Smith 3861d763cef2SBarry Smith Input Parameter: 3862bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 3863d763cef2SBarry Smith 3864d763cef2SBarry Smith Output Parameter: 3865bcf0153eSBarry Smith . t - the current time. This time may not corresponds to the final time set with `TSSetMaxTime()`, use `TSGetSolveTime()`. 3866d763cef2SBarry Smith 3867d763cef2SBarry Smith Level: beginner 3868d763cef2SBarry Smith 3869b8123daeSJed Brown Note: 3870bcf0153eSBarry Smith When called during time step evaluation (e.g. during residual evaluation or via hooks set using `TSSetPreStep()`, 3871bcf0153eSBarry Smith `TSSetPreStage()`, `TSSetPostStage()`, or `TSSetPostStep()`), the time is the time at the start of the step being evaluated. 3872b8123daeSJed Brown 3873bcf0153eSBarry Smith .seealso: [](chapter_ts), TS`, ``TSGetSolveTime()`, `TSSetTime()`, `TSGetTimeStep()`, `TSGetStepNumber()` 3874d763cef2SBarry Smith @*/ 3875d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTime(TS ts, PetscReal *t) 3876d71ae5a4SJacob Faibussowitsch { 3877d763cef2SBarry Smith PetscFunctionBegin; 38780700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3879f7cf8827SBarry Smith PetscValidRealPointer(t, 2); 3880d763cef2SBarry Smith *t = ts->ptime; 3881d763cef2SBarry Smith PetscFunctionReturn(0); 3882d763cef2SBarry Smith } 3883d763cef2SBarry Smith 3884e5e524a1SHong Zhang /*@ 3885e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 3886e5e524a1SHong Zhang 3887e5e524a1SHong Zhang Not Collective 3888e5e524a1SHong Zhang 3889e5e524a1SHong Zhang Input Parameter: 3890bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 3891e5e524a1SHong Zhang 3892e5e524a1SHong Zhang Output Parameter: 3893e5e524a1SHong Zhang . t - the previous time 3894e5e524a1SHong Zhang 3895e5e524a1SHong Zhang Level: beginner 3896e5e524a1SHong Zhang 3897bcf0153eSBarry Smith .seealso: [](chapter_ts), TS`, ``TSGetTime()`, `TSGetSolveTime()`, `TSGetTimeStep()` 3898e5e524a1SHong Zhang @*/ 3899d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetPrevTime(TS ts, PetscReal *t) 3900d71ae5a4SJacob Faibussowitsch { 3901e5e524a1SHong Zhang PetscFunctionBegin; 3902e5e524a1SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3903e5e524a1SHong Zhang PetscValidRealPointer(t, 2); 3904e5e524a1SHong Zhang *t = ts->ptime_prev; 3905e5e524a1SHong Zhang PetscFunctionReturn(0); 3906e5e524a1SHong Zhang } 3907e5e524a1SHong Zhang 39086a4d4014SLisandro Dalcin /*@ 39096a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 39106a4d4014SLisandro Dalcin 3911bcf0153eSBarry Smith Logically Collective on ts 39126a4d4014SLisandro Dalcin 39136a4d4014SLisandro Dalcin Input Parameters: 3914bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 39156a4d4014SLisandro Dalcin - time - the time 39166a4d4014SLisandro Dalcin 39176a4d4014SLisandro Dalcin Level: intermediate 39186a4d4014SLisandro Dalcin 3919bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetTime()`, `TSSetMaxSteps()` 39206a4d4014SLisandro Dalcin @*/ 3921d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTime(TS ts, PetscReal t) 3922d71ae5a4SJacob Faibussowitsch { 39236a4d4014SLisandro Dalcin PetscFunctionBegin; 39240700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3925c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts, t, 2); 39266a4d4014SLisandro Dalcin ts->ptime = t; 39276a4d4014SLisandro Dalcin PetscFunctionReturn(0); 39286a4d4014SLisandro Dalcin } 39296a4d4014SLisandro Dalcin 3930d763cef2SBarry Smith /*@C 3931d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 3932d763cef2SBarry Smith TS options in the database. 3933d763cef2SBarry Smith 3934bcf0153eSBarry Smith Logically Collective on ts 3935d763cef2SBarry Smith 3936d8d19677SJose E. Roman Input Parameters: 3937bcf0153eSBarry Smith + ts - The `TS` context 3938d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3939d763cef2SBarry Smith 3940bcf0153eSBarry Smith Level: advanced 3941bcf0153eSBarry Smith 3942bcf0153eSBarry Smith Note: 3943d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3944d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3945d763cef2SBarry Smith hyphen. 3946d763cef2SBarry Smith 3947bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetFromOptions()`, `TSAppendOptionsPrefix()` 3948d763cef2SBarry Smith @*/ 3949d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetOptionsPrefix(TS ts, const char prefix[]) 3950d71ae5a4SJacob Faibussowitsch { 3951089b2837SJed Brown SNES snes; 3952d763cef2SBarry Smith 3953d763cef2SBarry Smith PetscFunctionBegin; 39540700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 39559566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)ts, prefix)); 39569566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 39579566063dSJacob Faibussowitsch PetscCall(SNESSetOptionsPrefix(snes, prefix)); 3958d763cef2SBarry Smith PetscFunctionReturn(0); 3959d763cef2SBarry Smith } 3960d763cef2SBarry Smith 3961d763cef2SBarry Smith /*@C 3962d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 3963d763cef2SBarry Smith TS options in the database. 3964d763cef2SBarry Smith 3965bcf0153eSBarry Smith Logically Collective on ts 3966d763cef2SBarry Smith 3967d8d19677SJose E. Roman Input Parameters: 3968bcf0153eSBarry Smith + ts - The `TS` context 3969d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3970d763cef2SBarry Smith 3971bcf0153eSBarry Smith Level: advanced 3972bcf0153eSBarry Smith 3973bcf0153eSBarry Smith Note: 3974d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3975d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3976d763cef2SBarry Smith hyphen. 3977d763cef2SBarry Smith 3978bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetOptionsPrefix()`, `TSSetOptionsPrefix()`, `TSSetFromOptions()` 3979d763cef2SBarry Smith @*/ 3980d71ae5a4SJacob Faibussowitsch PetscErrorCode TSAppendOptionsPrefix(TS ts, const char prefix[]) 3981d71ae5a4SJacob Faibussowitsch { 3982089b2837SJed Brown SNES snes; 3983d763cef2SBarry Smith 3984d763cef2SBarry Smith PetscFunctionBegin; 39850700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 39869566063dSJacob Faibussowitsch PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)ts, prefix)); 39879566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 39889566063dSJacob Faibussowitsch PetscCall(SNESAppendOptionsPrefix(snes, prefix)); 3989d763cef2SBarry Smith PetscFunctionReturn(0); 3990d763cef2SBarry Smith } 3991d763cef2SBarry Smith 3992d763cef2SBarry Smith /*@C 3993d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 3994bcf0153eSBarry Smith `TS` options in the database. 3995d763cef2SBarry Smith 3996d763cef2SBarry Smith Not Collective 3997d763cef2SBarry Smith 3998d763cef2SBarry Smith Input Parameter: 3999bcf0153eSBarry Smith . ts - The `TS` context 4000d763cef2SBarry Smith 4001d763cef2SBarry Smith Output Parameter: 4002d763cef2SBarry Smith . prefix - A pointer to the prefix string used 4003d763cef2SBarry Smith 4004d763cef2SBarry Smith Level: intermediate 4005d763cef2SBarry Smith 4006bcf0153eSBarry Smith Fortran Note: 4007bcf0153eSBarry Smith On the fortran side, the user should pass in a string 'prefix' of 4008bcf0153eSBarry Smith sufficient length to hold the prefix. 4009bcf0153eSBarry Smith 4010bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSAppendOptionsPrefix()`, `TSSetFromOptions()` 4011d763cef2SBarry Smith @*/ 4012d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetOptionsPrefix(TS ts, const char *prefix[]) 4013d71ae5a4SJacob Faibussowitsch { 4014d763cef2SBarry Smith PetscFunctionBegin; 40150700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 40164482741eSBarry Smith PetscValidPointer(prefix, 2); 40179566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)ts, prefix)); 4018d763cef2SBarry Smith PetscFunctionReturn(0); 4019d763cef2SBarry Smith } 4020d763cef2SBarry Smith 4021d763cef2SBarry Smith /*@C 4022d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4023d763cef2SBarry Smith 4024bcf0153eSBarry Smith Not Collective, but parallel objects are returned if ts is parallel 4025d763cef2SBarry Smith 4026d763cef2SBarry Smith Input Parameter: 4027bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 4028d763cef2SBarry Smith 4029d763cef2SBarry Smith Output Parameters: 4030e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 4031e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 4032e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 4033e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 4034d763cef2SBarry Smith 4035d763cef2SBarry Smith Level: intermediate 4036d763cef2SBarry Smith 4037bcf0153eSBarry Smith Note: 4038bcf0153eSBarry Smith You can pass in NULL for any return argument you do not need. 4039bcf0153eSBarry Smith 4040bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetTimeStep()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()` 4041d763cef2SBarry Smith 4042d763cef2SBarry Smith @*/ 4043d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetRHSJacobian(TS ts, Mat *Amat, Mat *Pmat, TSRHSJacobian *func, void **ctx) 4044d71ae5a4SJacob Faibussowitsch { 404524989b8cSPeter Brune DM dm; 4046089b2837SJed Brown 4047d763cef2SBarry Smith PetscFunctionBegin; 404823a57915SBarry Smith if (Amat || Pmat) { 404923a57915SBarry Smith SNES snes; 40509566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 40519566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 40529566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes, Amat, Pmat, NULL, NULL)); 405323a57915SBarry Smith } 40549566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 40559566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, func, ctx)); 4056d763cef2SBarry Smith PetscFunctionReturn(0); 4057d763cef2SBarry Smith } 4058d763cef2SBarry Smith 40592eca1d9cSJed Brown /*@C 40602eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 40612eca1d9cSJed Brown 4062bcf0153eSBarry Smith Not Collective, but parallel objects are returned if ts is parallel 40632eca1d9cSJed Brown 40642eca1d9cSJed Brown Input Parameter: 4065bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 40662eca1d9cSJed Brown 40672eca1d9cSJed Brown Output Parameters: 4068e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4069e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 40702eca1d9cSJed Brown . f - The function to compute the matrices 40712eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 40722eca1d9cSJed Brown 40732eca1d9cSJed Brown Level: advanced 40742eca1d9cSJed Brown 4075bcf0153eSBarry Smith Note: 4076bcf0153eSBarry Smith You can pass in NULL for any return argument you do not need. 4077bcf0153eSBarry Smith 4078bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetTimeStep()`, `TSGetRHSJacobian()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()` 40792eca1d9cSJed Brown 40802eca1d9cSJed Brown @*/ 4081d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetIJacobian(TS ts, Mat *Amat, Mat *Pmat, TSIJacobian *f, void **ctx) 4082d71ae5a4SJacob Faibussowitsch { 408324989b8cSPeter Brune DM dm; 40840910c330SBarry Smith 40852eca1d9cSJed Brown PetscFunctionBegin; 4086c0aab802Sstefano_zampini if (Amat || Pmat) { 4087c0aab802Sstefano_zampini SNES snes; 40889566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 40899566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 40909566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes, Amat, Pmat, NULL, NULL)); 4091c0aab802Sstefano_zampini } 40929566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 40939566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm, f, ctx)); 40942eca1d9cSJed Brown PetscFunctionReturn(0); 40952eca1d9cSJed Brown } 40962eca1d9cSJed Brown 4097af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 40986c699258SBarry Smith /*@ 4099bcf0153eSBarry Smith TSSetDM - Sets the `DM` that may be used by some nonlinear solvers or preconditioners under the `TS` 41006c699258SBarry Smith 4101d083f849SBarry Smith Logically Collective on ts 41026c699258SBarry Smith 41036c699258SBarry Smith Input Parameters: 4104bcf0153eSBarry Smith + ts - the `TS` integrator object 41052a808120SBarry Smith - dm - the dm, cannot be NULL 41066c699258SBarry Smith 41076c699258SBarry Smith Level: intermediate 41086c699258SBarry Smith 4109bcf0153eSBarry Smith Notes: 4110bcf0153eSBarry Smith A `DM` can only be used for solving one problem at a time because information about the problem is stored on the `DM`, 4111bcf0153eSBarry Smith even when not using interfaces like `DMTSSetIFunction()`. Use `DMClone()` to get a distinct `DM` when solving 4112bcf0153eSBarry Smith different problems using the same function space. 4113bcf0153eSBarry Smith 4114bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `DM`, `TSGetDM()`, `SNESSetDM()`, `SNESGetDM()` 41156c699258SBarry Smith @*/ 4116d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetDM(TS ts, DM dm) 4117d71ae5a4SJacob Faibussowitsch { 4118089b2837SJed Brown SNES snes; 4119942e3340SBarry Smith DMTS tsdm; 41206c699258SBarry Smith 41216c699258SBarry Smith PetscFunctionBegin; 41220700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 41232a808120SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 2); 41249566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)dm)); 4125942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 41262a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 41279566063dSJacob Faibussowitsch PetscCall(DMCopyDMTS(ts->dm, dm)); 41289566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm, &tsdm)); 41291e66621cSBarry Smith /* Grant write privileges to the replacement DM */ 41301e66621cSBarry Smith if (tsdm->originaldm == ts->dm) tsdm->originaldm = dm; 413124989b8cSPeter Brune } 41329566063dSJacob Faibussowitsch PetscCall(DMDestroy(&ts->dm)); 413324989b8cSPeter Brune } 41346c699258SBarry Smith ts->dm = dm; 4135bbd56ea5SKarl Rupp 41369566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 41379566063dSJacob Faibussowitsch PetscCall(SNESSetDM(snes, dm)); 41386c699258SBarry Smith PetscFunctionReturn(0); 41396c699258SBarry Smith } 41406c699258SBarry Smith 41416c699258SBarry Smith /*@ 4142bcf0153eSBarry Smith TSGetDM - Gets the `DM` that may be used by some preconditioners 41436c699258SBarry Smith 41443f9fe445SBarry Smith Not Collective 41456c699258SBarry Smith 41466c699258SBarry Smith Input Parameter: 4147bcf0153eSBarry Smith . ts - the `TS` 41486c699258SBarry Smith 41496c699258SBarry Smith Output Parameter: 41506c699258SBarry Smith . dm - the dm 41516c699258SBarry Smith 41526c699258SBarry Smith Level: intermediate 41536c699258SBarry Smith 4154bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `DM`, `TSSetDM()`, `SNESSetDM()`, `SNESGetDM()` 41556c699258SBarry Smith @*/ 4156d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetDM(TS ts, DM *dm) 4157d71ae5a4SJacob Faibussowitsch { 41586c699258SBarry Smith PetscFunctionBegin; 41590700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4160496e6a7aSJed Brown if (!ts->dm) { 41619566063dSJacob Faibussowitsch PetscCall(DMShellCreate(PetscObjectComm((PetscObject)ts), &ts->dm)); 41629566063dSJacob Faibussowitsch if (ts->snes) PetscCall(SNESSetDM(ts->snes, ts->dm)); 4163496e6a7aSJed Brown } 41646c699258SBarry Smith *dm = ts->dm; 41656c699258SBarry Smith PetscFunctionReturn(0); 41666c699258SBarry Smith } 41671713a123SBarry Smith 41680f5c6efeSJed Brown /*@ 41690f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 41700f5c6efeSJed Brown 4171bcf0153eSBarry Smith Logically Collective on snes 41720f5c6efeSJed Brown 4173d8d19677SJose E. Roman Input Parameters: 4174d42a1c89SJed Brown + snes - nonlinear solver 41750910c330SBarry Smith . U - the current state at which to evaluate the residual 4176d42a1c89SJed Brown - ctx - user context, must be a TS 41770f5c6efeSJed Brown 41780f5c6efeSJed Brown Output Parameter: 41790f5c6efeSJed Brown . F - the nonlinear residual 41800f5c6efeSJed Brown 41810f5c6efeSJed Brown Level: advanced 41820f5c6efeSJed Brown 4183bcf0153eSBarry Smith Note: 4184bcf0153eSBarry Smith This function is not normally called by users and is automatically registered with the `SNES` used by `TS`. 4185bcf0153eSBarry Smith It is most frequently passed to `MatFDColoringSetFunction()`. 4186bcf0153eSBarry Smith 4187bcf0153eSBarry Smith .seealso: [](chapter_ts), `SNESSetFunction()`, `MatFDColoringSetFunction()` 41880f5c6efeSJed Brown @*/ 4189d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESTSFormFunction(SNES snes, Vec U, Vec F, void *ctx) 4190d71ae5a4SJacob Faibussowitsch { 41910f5c6efeSJed Brown TS ts = (TS)ctx; 41920f5c6efeSJed Brown 41930f5c6efeSJed Brown PetscFunctionBegin; 41940f5c6efeSJed Brown PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 41950910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 2); 41960f5c6efeSJed Brown PetscValidHeaderSpecific(F, VEC_CLASSID, 3); 41970f5c6efeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 4); 41989566063dSJacob Faibussowitsch PetscCall((ts->ops->snesfunction)(snes, U, F, ts)); 41990f5c6efeSJed Brown PetscFunctionReturn(0); 42000f5c6efeSJed Brown } 42010f5c6efeSJed Brown 42020f5c6efeSJed Brown /*@ 42030f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 42040f5c6efeSJed Brown 4205bcf0153eSBarry Smith Collective on snes 42060f5c6efeSJed Brown 4207d8d19677SJose E. Roman Input Parameters: 42080f5c6efeSJed Brown + snes - nonlinear solver 42090910c330SBarry Smith . U - the current state at which to evaluate the residual 4210bcf0153eSBarry Smith - ctx - user context, must be a `TS` 42110f5c6efeSJed Brown 4212d8d19677SJose E. Roman Output Parameters: 42130f5c6efeSJed Brown + A - the Jacobian 42146b867d5aSJose E. Roman - B - the preconditioning matrix (may be the same as A) 42150f5c6efeSJed Brown 42160f5c6efeSJed Brown Level: developer 42170f5c6efeSJed Brown 4218bcf0153eSBarry Smith Note: 4219bcf0153eSBarry Smith This function is not normally called by users and is automatically registered with the `SNES` used by `TS`. 4220bcf0153eSBarry Smith 4221bcf0153eSBarry Smith .seealso: [](chapter_ts), `SNESSetJacobian()` 42220f5c6efeSJed Brown @*/ 4223d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESTSFormJacobian(SNES snes, Vec U, Mat A, Mat B, void *ctx) 4224d71ae5a4SJacob Faibussowitsch { 42250f5c6efeSJed Brown TS ts = (TS)ctx; 42260f5c6efeSJed Brown 42270f5c6efeSJed Brown PetscFunctionBegin; 42280f5c6efeSJed Brown PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 42290910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 2); 42300f5c6efeSJed Brown PetscValidPointer(A, 3); 423194ab13aaSBarry Smith PetscValidHeaderSpecific(A, MAT_CLASSID, 3); 42320f5c6efeSJed Brown PetscValidPointer(B, 4); 423394ab13aaSBarry Smith PetscValidHeaderSpecific(B, MAT_CLASSID, 4); 4234064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(ts, TS_CLASSID, 5); 42359566063dSJacob Faibussowitsch PetscCall((ts->ops->snesjacobian)(snes, U, A, B, ts)); 42360f5c6efeSJed Brown PetscFunctionReturn(0); 42370f5c6efeSJed Brown } 4238325fc9f4SBarry Smith 42390e4ef248SJed Brown /*@C 42409ae8fd06SBarry Smith TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only 42410e4ef248SJed Brown 4242bcf0153eSBarry Smith Collective on ts 42430e4ef248SJed Brown 42444165533cSJose E. Roman Input Parameters: 42450e4ef248SJed Brown + ts - time stepping context 42460e4ef248SJed Brown . t - time at which to evaluate 42470910c330SBarry Smith . U - state at which to evaluate 42480e4ef248SJed Brown - ctx - context 42490e4ef248SJed Brown 42504165533cSJose E. Roman Output Parameter: 42510e4ef248SJed Brown . F - right hand side 42520e4ef248SJed Brown 42530e4ef248SJed Brown Level: intermediate 42540e4ef248SJed Brown 4255bcf0153eSBarry Smith Note: 4256bcf0153eSBarry Smith This function is intended to be passed to `TSSetRHSFunction()` to evaluate the right hand side for linear problems. 4257bcf0153eSBarry Smith The matrix (and optionally the evaluation context) should be passed to `TSSetRHSJacobian()`. 42580e4ef248SJed Brown 4259bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetRHSFunction()`, `TSSetRHSJacobian()`, `TSComputeRHSJacobianConstant()` 42600e4ef248SJed Brown @*/ 4261d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSFunctionLinear(TS ts, PetscReal t, Vec U, Vec F, void *ctx) 4262d71ae5a4SJacob Faibussowitsch { 42630e4ef248SJed Brown Mat Arhs, Brhs; 42640e4ef248SJed Brown 42650e4ef248SJed Brown PetscFunctionBegin; 42669566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts, &Arhs, &Brhs)); 42672663174eSHong Zhang /* undo the damage caused by shifting */ 42689566063dSJacob Faibussowitsch PetscCall(TSRecoverRHSJacobian(ts, Arhs, Brhs)); 42699566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts, t, U, Arhs, Brhs)); 42709566063dSJacob Faibussowitsch PetscCall(MatMult(Arhs, U, F)); 42710e4ef248SJed Brown PetscFunctionReturn(0); 42720e4ef248SJed Brown } 42730e4ef248SJed Brown 42740e4ef248SJed Brown /*@C 42750e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 42760e4ef248SJed Brown 4277bcf0153eSBarry Smith Collective on ts 42780e4ef248SJed Brown 42794165533cSJose E. Roman Input Parameters: 42800e4ef248SJed Brown + ts - time stepping context 42810e4ef248SJed Brown . t - time at which to evaluate 42820910c330SBarry Smith . U - state at which to evaluate 42830e4ef248SJed Brown - ctx - context 42840e4ef248SJed Brown 42854165533cSJose E. Roman Output Parameters: 42860e4ef248SJed Brown + A - pointer to operator 428797bb3fdcSJose E. Roman - B - pointer to preconditioning matrix 42880e4ef248SJed Brown 42890e4ef248SJed Brown Level: intermediate 42900e4ef248SJed Brown 4291bcf0153eSBarry Smith Note: 4292bcf0153eSBarry Smith This function is intended to be passed to `TSSetRHSJacobian()` to evaluate the Jacobian for linear time-independent problems. 42930e4ef248SJed Brown 4294bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetRHSFunction()`, `TSSetRHSJacobian()`, `TSComputeRHSFunctionLinear()` 42950e4ef248SJed Brown @*/ 4296d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSJacobianConstant(TS ts, PetscReal t, Vec U, Mat A, Mat B, void *ctx) 4297d71ae5a4SJacob Faibussowitsch { 42980e4ef248SJed Brown PetscFunctionBegin; 42990e4ef248SJed Brown PetscFunctionReturn(0); 43000e4ef248SJed Brown } 43010e4ef248SJed Brown 43020026cea9SSean Farley /*@C 43030026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 43040026cea9SSean Farley 4305bcf0153eSBarry Smith Collective on ts 43060026cea9SSean Farley 43074165533cSJose E. Roman Input Parameters: 43080026cea9SSean Farley + ts - time stepping context 43090026cea9SSean Farley . t - time at which to evaluate 43100910c330SBarry Smith . U - state at which to evaluate 43110910c330SBarry Smith . Udot - time derivative of state vector 43120026cea9SSean Farley - ctx - context 43130026cea9SSean Farley 43144165533cSJose E. Roman Output Parameter: 43150026cea9SSean Farley . F - left hand side 43160026cea9SSean Farley 43170026cea9SSean Farley Level: intermediate 43180026cea9SSean Farley 43190026cea9SSean Farley Notes: 43200910c330SBarry 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 4321bcf0153eSBarry Smith user is required to write their own `TSComputeIFunction()`. 4322bcf0153eSBarry Smith This function is intended to be passed to `TSSetIFunction()` to evaluate the left hand side for linear problems. 4323bcf0153eSBarry Smith The matrix (and optionally the evaluation context) should be passed to `TSSetIJacobian()`. 43240026cea9SSean Farley 4325bcf0153eSBarry Smith Note that using this function is NOT equivalent to using `TSComputeRHSFunctionLinear()` since that solves Udot = A U 43269ae8fd06SBarry Smith 4327bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetIFunction()`, `TSSetIJacobian()`, `TSComputeIJacobianConstant()`, `TSComputeRHSFunctionLinear()` 43280026cea9SSean Farley @*/ 4329d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIFunctionLinear(TS ts, PetscReal t, Vec U, Vec Udot, Vec F, void *ctx) 4330d71ae5a4SJacob Faibussowitsch { 43310026cea9SSean Farley Mat A, B; 43320026cea9SSean Farley 43330026cea9SSean Farley PetscFunctionBegin; 43349566063dSJacob Faibussowitsch PetscCall(TSGetIJacobian(ts, &A, &B, NULL, NULL)); 43359566063dSJacob Faibussowitsch PetscCall(TSComputeIJacobian(ts, t, U, Udot, 1.0, A, B, PETSC_TRUE)); 43369566063dSJacob Faibussowitsch PetscCall(MatMult(A, Udot, F)); 43370026cea9SSean Farley PetscFunctionReturn(0); 43380026cea9SSean Farley } 43390026cea9SSean Farley 43400026cea9SSean Farley /*@C 4341b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 43420026cea9SSean Farley 4343bcf0153eSBarry Smith Collective on ts 43440026cea9SSean Farley 43454165533cSJose E. Roman Input Parameters: 43460026cea9SSean Farley + ts - time stepping context 43470026cea9SSean Farley . t - time at which to evaluate 43480910c330SBarry Smith . U - state at which to evaluate 43490910c330SBarry Smith . Udot - time derivative of state vector 43500026cea9SSean Farley . shift - shift to apply 43510026cea9SSean Farley - ctx - context 43520026cea9SSean Farley 43534165533cSJose E. Roman Output Parameters: 43540026cea9SSean Farley + A - pointer to operator 435597bb3fdcSJose E. Roman - B - pointer to preconditioning matrix 43560026cea9SSean Farley 4357b41af12eSJed Brown Level: advanced 43580026cea9SSean Farley 43590026cea9SSean Farley Notes: 4360bcf0153eSBarry Smith This function is intended to be passed to `TSSetIJacobian()` to evaluate the Jacobian for linear time-independent problems. 43610026cea9SSean Farley 4362b41af12eSJed Brown It is only appropriate for problems of the form 4363b41af12eSJed Brown 4364b41af12eSJed Brown $ M Udot = F(U,t) 4365b41af12eSJed Brown 4366bcf0153eSBarry Smith where M is constant and F is non-stiff. The user must pass M to `TSSetIJacobian()`. The current implementation only 4367bcf0153eSBarry Smith works with IMEX time integration methods such as `TSROSW` and `TSARKIMEX`, since there is no support for de-constructing 4368b41af12eSJed Brown an implicit operator of the form 4369b41af12eSJed Brown 4370b41af12eSJed Brown $ shift*M + J 4371b41af12eSJed Brown 4372b41af12eSJed 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 4373b41af12eSJed Brown a copy of M or reassemble it when requested. 4374b41af12eSJed Brown 4375bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSROSW`, `TSARKIMEX`, `TSSetIFunction()`, `TSSetIJacobian()`, `TSComputeIFunctionLinear()` 43760026cea9SSean Farley @*/ 4377d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIJacobianConstant(TS ts, PetscReal t, Vec U, Vec Udot, PetscReal shift, Mat A, Mat B, void *ctx) 4378d71ae5a4SJacob Faibussowitsch { 43790026cea9SSean Farley PetscFunctionBegin; 43809566063dSJacob Faibussowitsch PetscCall(MatScale(A, shift / ts->ijacobian.shift)); 4381b41af12eSJed Brown ts->ijacobian.shift = shift; 43820026cea9SSean Farley PetscFunctionReturn(0); 43830026cea9SSean Farley } 4384b41af12eSJed Brown 4385e817cc15SEmil Constantinescu /*@ 4386bcf0153eSBarry Smith TSGetEquationType - Gets the type of the equation that `TS` is solving. 4387e817cc15SEmil Constantinescu 4388e817cc15SEmil Constantinescu Not Collective 4389e817cc15SEmil Constantinescu 4390e817cc15SEmil Constantinescu Input Parameter: 4391bcf0153eSBarry Smith . ts - the `TS` context 4392e817cc15SEmil Constantinescu 4393e817cc15SEmil Constantinescu Output Parameter: 4394bcf0153eSBarry Smith . equation_type - see `TSEquationType` 4395e817cc15SEmil Constantinescu 4396e817cc15SEmil Constantinescu Level: beginner 4397e817cc15SEmil Constantinescu 4398bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetEquationType()`, `TSEquationType` 4399e817cc15SEmil Constantinescu @*/ 4400d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetEquationType(TS ts, TSEquationType *equation_type) 4401d71ae5a4SJacob Faibussowitsch { 4402e817cc15SEmil Constantinescu PetscFunctionBegin; 4403e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4404e817cc15SEmil Constantinescu PetscValidPointer(equation_type, 2); 4405e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 4406e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4407e817cc15SEmil Constantinescu } 4408e817cc15SEmil Constantinescu 4409e817cc15SEmil Constantinescu /*@ 4410bcf0153eSBarry Smith TSSetEquationType - Sets the type of the equation that `TS` is solving. 4411e817cc15SEmil Constantinescu 4412e817cc15SEmil Constantinescu Not Collective 4413e817cc15SEmil Constantinescu 4414d8d19677SJose E. Roman Input Parameters: 4415bcf0153eSBarry Smith + ts - the `TS` context 4416bcf0153eSBarry Smith - equation_type - see `TSEquationType` 4417e817cc15SEmil Constantinescu 4418e817cc15SEmil Constantinescu Level: advanced 4419e817cc15SEmil Constantinescu 4420bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetEquationType()`, `TSEquationType` 4421e817cc15SEmil Constantinescu @*/ 4422d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetEquationType(TS ts, TSEquationType equation_type) 4423d71ae5a4SJacob Faibussowitsch { 4424e817cc15SEmil Constantinescu PetscFunctionBegin; 4425e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4426e817cc15SEmil Constantinescu ts->equation_type = equation_type; 4427e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4428e817cc15SEmil Constantinescu } 44290026cea9SSean Farley 44304af1b03aSJed Brown /*@ 4431bcf0153eSBarry Smith TSGetConvergedReason - Gets the reason the `TS` iteration was stopped. 44324af1b03aSJed Brown 44334af1b03aSJed Brown Not Collective 44344af1b03aSJed Brown 44354af1b03aSJed Brown Input Parameter: 4436bcf0153eSBarry Smith . ts - the `TS` context 44374af1b03aSJed Brown 44384af1b03aSJed Brown Output Parameter: 4439bcf0153eSBarry Smith . reason - negative value indicates diverged, positive value converged, see `TSConvergedReason` or the 44404af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 44414af1b03aSJed Brown 4442487e0bb9SJed Brown Level: beginner 44434af1b03aSJed Brown 4444bcf0153eSBarry Smith Note: 4445bcf0153eSBarry Smith Can only be called after the call to `TSSolve()` is complete. 44464af1b03aSJed Brown 4447bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSolve()`, `TSSetConvergenceTest()`, `TSConvergedReason` 44484af1b03aSJed Brown @*/ 4449d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetConvergedReason(TS ts, TSConvergedReason *reason) 4450d71ae5a4SJacob Faibussowitsch { 44514af1b03aSJed Brown PetscFunctionBegin; 44524af1b03aSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 44534af1b03aSJed Brown PetscValidPointer(reason, 2); 44544af1b03aSJed Brown *reason = ts->reason; 44554af1b03aSJed Brown PetscFunctionReturn(0); 44564af1b03aSJed Brown } 44574af1b03aSJed Brown 4458d6ad946cSShri Abhyankar /*@ 4459bcf0153eSBarry Smith TSSetConvergedReason - Sets the reason for handling the convergence of `TSSolve()`. 4460d6ad946cSShri Abhyankar 44616b221cbeSPatrick Sanan Logically Collective; reason must contain common value 4462d6ad946cSShri Abhyankar 44636b221cbeSPatrick Sanan Input Parameters: 4464bcf0153eSBarry Smith + ts - the `TS` context 4465bcf0153eSBarry Smith - reason - negative value indicates diverged, positive value converged, see `TSConvergedReason` or the 4466d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 4467d6ad946cSShri Abhyankar 4468f5abba47SShri Abhyankar Level: advanced 4469d6ad946cSShri Abhyankar 4470bcf0153eSBarry Smith Note: 4471bcf0153eSBarry Smith Can only be called while `TSSolve()` is active. 4472d6ad946cSShri Abhyankar 4473bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSolve()`, `TSConvergedReason` 4474d6ad946cSShri Abhyankar @*/ 4475d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetConvergedReason(TS ts, TSConvergedReason reason) 4476d71ae5a4SJacob Faibussowitsch { 4477d6ad946cSShri Abhyankar PetscFunctionBegin; 4478d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4479d6ad946cSShri Abhyankar ts->reason = reason; 4480d6ad946cSShri Abhyankar PetscFunctionReturn(0); 4481d6ad946cSShri Abhyankar } 4482d6ad946cSShri Abhyankar 4483cc708dedSBarry Smith /*@ 4484bcf0153eSBarry Smith TSGetSolveTime - Gets the time after a call to `TSSolve()` 4485cc708dedSBarry Smith 4486cc708dedSBarry Smith Not Collective 4487cc708dedSBarry Smith 4488cc708dedSBarry Smith Input Parameter: 4489bcf0153eSBarry Smith . ts - the `TS` context 4490cc708dedSBarry Smith 4491cc708dedSBarry Smith Output Parameter: 4492bcf0153eSBarry Smith . ftime - the final time. This time corresponds to the final time set with `TSSetMaxTime()` 4493cc708dedSBarry Smith 4494487e0bb9SJed Brown Level: beginner 4495cc708dedSBarry Smith 4496bcf0153eSBarry Smith Note: 4497bcf0153eSBarry Smith Can only be called after the call to `TSSolve()` is complete. 4498cc708dedSBarry Smith 4499bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSolve()`, `TSSetConvergenceTest()`, `TSConvergedReason` 4500cc708dedSBarry Smith @*/ 4501d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSolveTime(TS ts, PetscReal *ftime) 4502d71ae5a4SJacob Faibussowitsch { 4503cc708dedSBarry Smith PetscFunctionBegin; 4504cc708dedSBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4505dadcf809SJacob Faibussowitsch PetscValidRealPointer(ftime, 2); 4506cc708dedSBarry Smith *ftime = ts->solvetime; 4507cc708dedSBarry Smith PetscFunctionReturn(0); 4508cc708dedSBarry Smith } 4509cc708dedSBarry Smith 45102c18e0fdSBarry Smith /*@ 45115ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 45129f67acb7SJed Brown used by the time integrator. 45139f67acb7SJed Brown 45149f67acb7SJed Brown Not Collective 45159f67acb7SJed Brown 45169f67acb7SJed Brown Input Parameter: 4517bcf0153eSBarry Smith . ts - `TS` context 45189f67acb7SJed Brown 45199f67acb7SJed Brown Output Parameter: 45209f67acb7SJed Brown . nits - number of nonlinear iterations 45219f67acb7SJed Brown 45229f67acb7SJed Brown Level: intermediate 45239f67acb7SJed Brown 4524bcf0153eSBarry Smith Notes: 4525bcf0153eSBarry Smith This counter is reset to zero for each successive call to `TSSolve()`. 4526bcf0153eSBarry Smith 4527bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSolve()`, `TSGetKSPIterations()` 45289f67acb7SJed Brown @*/ 4529d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSNESIterations(TS ts, PetscInt *nits) 4530d71ae5a4SJacob Faibussowitsch { 45319f67acb7SJed Brown PetscFunctionBegin; 45329f67acb7SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 45339f67acb7SJed Brown PetscValidIntPointer(nits, 2); 45345ef26d82SJed Brown *nits = ts->snes_its; 45359f67acb7SJed Brown PetscFunctionReturn(0); 45369f67acb7SJed Brown } 45379f67acb7SJed Brown 45389f67acb7SJed Brown /*@ 45395ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 45409f67acb7SJed Brown used by the time integrator. 45419f67acb7SJed Brown 45429f67acb7SJed Brown Not Collective 45439f67acb7SJed Brown 45449f67acb7SJed Brown Input Parameter: 4545bcf0153eSBarry Smith . ts - `TS` context 45469f67acb7SJed Brown 45479f67acb7SJed Brown Output Parameter: 45489f67acb7SJed Brown . lits - number of linear iterations 45499f67acb7SJed Brown 45509f67acb7SJed Brown Level: intermediate 45519f67acb7SJed Brown 4552bcf0153eSBarry Smith Note: 4553bcf0153eSBarry Smith This counter is reset to zero for each successive call to `TSSolve()`. 4554bcf0153eSBarry Smith 4555bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSolve()`, `TSGetSNESIterations()`, `SNESGetKSPIterations()` 45569f67acb7SJed Brown @*/ 4557d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetKSPIterations(TS ts, PetscInt *lits) 4558d71ae5a4SJacob Faibussowitsch { 45599f67acb7SJed Brown PetscFunctionBegin; 45609f67acb7SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 45619f67acb7SJed Brown PetscValidIntPointer(lits, 2); 45625ef26d82SJed Brown *lits = ts->ksp_its; 45639f67acb7SJed Brown PetscFunctionReturn(0); 45649f67acb7SJed Brown } 45659f67acb7SJed Brown 4566cef5090cSJed Brown /*@ 4567cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 4568cef5090cSJed Brown 4569cef5090cSJed Brown Not Collective 4570cef5090cSJed Brown 4571cef5090cSJed Brown Input Parameter: 4572bcf0153eSBarry Smith . ts - `TS` context 4573cef5090cSJed Brown 4574cef5090cSJed Brown Output Parameter: 4575cef5090cSJed Brown . rejects - number of steps rejected 4576cef5090cSJed Brown 4577cef5090cSJed Brown Level: intermediate 4578cef5090cSJed Brown 4579bcf0153eSBarry Smith Note: 4580bcf0153eSBarry Smith This counter is reset to zero for each successive call to `TSSolve()`. 4581bcf0153eSBarry Smith 4582bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSolve()`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetSNESFailures()`, `TSSetMaxSNESFailures()`, `TSSetErrorIfStepFails()` 4583cef5090cSJed Brown @*/ 4584d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetStepRejections(TS ts, PetscInt *rejects) 4585d71ae5a4SJacob Faibussowitsch { 4586cef5090cSJed Brown PetscFunctionBegin; 4587cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4588cef5090cSJed Brown PetscValidIntPointer(rejects, 2); 4589cef5090cSJed Brown *rejects = ts->reject; 4590cef5090cSJed Brown PetscFunctionReturn(0); 4591cef5090cSJed Brown } 4592cef5090cSJed Brown 4593cef5090cSJed Brown /*@ 4594bcf0153eSBarry Smith TSGetSNESFailures - Gets the total number of failed `SNES` solves in a `TS` 4595cef5090cSJed Brown 4596cef5090cSJed Brown Not Collective 4597cef5090cSJed Brown 4598cef5090cSJed Brown Input Parameter: 4599bcf0153eSBarry Smith . ts - `TS` context 4600cef5090cSJed Brown 4601cef5090cSJed Brown Output Parameter: 4602cef5090cSJed Brown . fails - number of failed nonlinear solves 4603cef5090cSJed Brown 4604cef5090cSJed Brown Level: intermediate 4605cef5090cSJed Brown 4606bcf0153eSBarry Smith Note: 4607bcf0153eSBarry Smith This counter is reset to zero for each successive call to `TSSolve()`. 4608bcf0153eSBarry Smith 4609bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSolve()`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSSetMaxSNESFailures()` 4610cef5090cSJed Brown @*/ 4611d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSNESFailures(TS ts, PetscInt *fails) 4612d71ae5a4SJacob Faibussowitsch { 4613cef5090cSJed Brown PetscFunctionBegin; 4614cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4615cef5090cSJed Brown PetscValidIntPointer(fails, 2); 4616cef5090cSJed Brown *fails = ts->num_snes_failures; 4617cef5090cSJed Brown PetscFunctionReturn(0); 4618cef5090cSJed Brown } 4619cef5090cSJed Brown 4620cef5090cSJed Brown /*@ 4621bcf0153eSBarry Smith TSSetMaxStepRejections - Sets the maximum number of step rejections before a time step fails 4622cef5090cSJed Brown 4623cef5090cSJed Brown Not Collective 4624cef5090cSJed Brown 4625d8d19677SJose E. Roman Input Parameters: 4626bcf0153eSBarry Smith + ts - `TS` context 4627cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 4628cef5090cSJed Brown 4629cef5090cSJed Brown Options Database Key: 4630cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 4631cef5090cSJed Brown 4632cef5090cSJed Brown Level: intermediate 4633cef5090cSJed Brown 4634bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `SNES`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxSNESFailures()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `TSSetErrorIfStepFails()`, `TSGetConvergedReason()` 4635cef5090cSJed Brown @*/ 4636d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxStepRejections(TS ts, PetscInt rejects) 4637d71ae5a4SJacob Faibussowitsch { 4638cef5090cSJed Brown PetscFunctionBegin; 4639cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4640cef5090cSJed Brown ts->max_reject = rejects; 4641cef5090cSJed Brown PetscFunctionReturn(0); 4642cef5090cSJed Brown } 4643cef5090cSJed Brown 4644cef5090cSJed Brown /*@ 4645bcf0153eSBarry Smith TSSetMaxSNESFailures - Sets the maximum number of failed `SNES` solves 4646cef5090cSJed Brown 4647cef5090cSJed Brown Not Collective 4648cef5090cSJed Brown 4649d8d19677SJose E. Roman Input Parameters: 4650bcf0153eSBarry Smith + ts - `TS` context 4651cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 4652cef5090cSJed Brown 4653cef5090cSJed Brown Options Database Key: 4654cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 4655cef5090cSJed Brown 4656cef5090cSJed Brown Level: intermediate 4657cef5090cSJed Brown 4658bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `SNES`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `SNESGetConvergedReason()`, `TSGetConvergedReason()` 4659cef5090cSJed Brown @*/ 4660d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxSNESFailures(TS ts, PetscInt fails) 4661d71ae5a4SJacob Faibussowitsch { 4662cef5090cSJed Brown PetscFunctionBegin; 4663cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4664cef5090cSJed Brown ts->max_snes_failures = fails; 4665cef5090cSJed Brown PetscFunctionReturn(0); 4666cef5090cSJed Brown } 4667cef5090cSJed Brown 4668cef5090cSJed Brown /*@ 4669bcf0153eSBarry Smith TSSetErrorIfStepFails - Immediately error if no step succeeds 4670cef5090cSJed Brown 4671cef5090cSJed Brown Not Collective 4672cef5090cSJed Brown 4673d8d19677SJose E. Roman Input Parameters: 4674bcf0153eSBarry Smith + ts - `TS` context 4675bcf0153eSBarry Smith - err - `PETSC_TRUE` to error if no step succeeds, `PETSC_FALSE` to return without failure 4676cef5090cSJed Brown 4677cef5090cSJed Brown Options Database Key: 4678cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 4679cef5090cSJed Brown 4680cef5090cSJed Brown Level: intermediate 4681cef5090cSJed Brown 4682bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `TSSetErrorIfStepFails()`, `TSGetConvergedReason()` 4683cef5090cSJed Brown @*/ 4684d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetErrorIfStepFails(TS ts, PetscBool err) 4685d71ae5a4SJacob Faibussowitsch { 4686cef5090cSJed Brown PetscFunctionBegin; 4687cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4688cef5090cSJed Brown ts->errorifstepfailed = err; 4689cef5090cSJed Brown PetscFunctionReturn(0); 4690cef5090cSJed Brown } 4691cef5090cSJed Brown 469284df9cb4SJed Brown /*@ 4693552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 469484df9cb4SJed Brown 4695bcf0153eSBarry Smith Collective on ts if controller has not been created yet 469684df9cb4SJed Brown 46974165533cSJose E. Roman Input Parameter: 4698ed81e22dSJed Brown . ts - time stepping context 469984df9cb4SJed Brown 47004165533cSJose E. Roman Output Parameter: 4701ed81e22dSJed Brown . adapt - adaptive controller 470284df9cb4SJed Brown 470384df9cb4SJed Brown Level: intermediate 470484df9cb4SJed Brown 4705bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSAdapt`, `TSAdaptSetType()`, `TSAdaptChoose()` 470684df9cb4SJed Brown @*/ 4707d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetAdapt(TS ts, TSAdapt *adapt) 4708d71ae5a4SJacob Faibussowitsch { 470984df9cb4SJed Brown PetscFunctionBegin; 471084df9cb4SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4711bec58848SLisandro Dalcin PetscValidPointer(adapt, 2); 471284df9cb4SJed Brown if (!ts->adapt) { 47139566063dSJacob Faibussowitsch PetscCall(TSAdaptCreate(PetscObjectComm((PetscObject)ts), &ts->adapt)); 47149566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)ts->adapt, (PetscObject)ts, 1)); 471584df9cb4SJed Brown } 4716bec58848SLisandro Dalcin *adapt = ts->adapt; 471784df9cb4SJed Brown PetscFunctionReturn(0); 471884df9cb4SJed Brown } 4719d6ebe24aSShri Abhyankar 47201c3436cfSJed Brown /*@ 47211c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 47221c3436cfSJed Brown 47231c3436cfSJed Brown Logically Collective 47241c3436cfSJed Brown 47254165533cSJose E. Roman Input Parameters: 47261c3436cfSJed Brown + ts - time integration context 4727bcf0153eSBarry Smith . atol - scalar absolute tolerances, `PETSC_DECIDE` to leave current value 47280298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 4729bcf0153eSBarry Smith . rtol - scalar relative tolerances, `PETSC_DECIDE` to leave current value 47300298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 47311c3436cfSJed Brown 4732a3cdaa26SBarry Smith Options Database keys: 4733a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 473467b8a455SSatish Balay - -ts_atol <atol> - Absolute tolerance for local truncation error 4735a3cdaa26SBarry Smith 4736bcf0153eSBarry Smith Level: beginner 4737bcf0153eSBarry Smith 47383ff766beSShri Abhyankar Notes: 47393ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 47403ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 47413ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 47423ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 47433ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 47443ff766beSShri Abhyankar differential variables. 47453ff766beSShri Abhyankar 4746bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSAdapt`, `TSErrorWeightedNorm()`, `TSGetTolerances()` 47471c3436cfSJed Brown @*/ 4748d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTolerances(TS ts, PetscReal atol, Vec vatol, PetscReal rtol, Vec vrtol) 4749d71ae5a4SJacob Faibussowitsch { 47501c3436cfSJed Brown PetscFunctionBegin; 4751c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 47521c3436cfSJed Brown if (vatol) { 47539566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)vatol)); 47549566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vatol)); 47551c3436cfSJed Brown ts->vatol = vatol; 47561c3436cfSJed Brown } 4757c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 47581c3436cfSJed Brown if (vrtol) { 47599566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)vrtol)); 47609566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vrtol)); 47611c3436cfSJed Brown ts->vrtol = vrtol; 47621c3436cfSJed Brown } 47631c3436cfSJed Brown PetscFunctionReturn(0); 47641c3436cfSJed Brown } 47651c3436cfSJed Brown 4766c5033834SJed Brown /*@ 4767c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 4768c5033834SJed Brown 4769c5033834SJed Brown Logically Collective 4770c5033834SJed Brown 47714165533cSJose E. Roman Input Parameter: 4772c5033834SJed Brown . ts - time integration context 4773c5033834SJed Brown 47744165533cSJose E. Roman Output Parameters: 47750298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 47760298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 47770298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 47780298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 4779c5033834SJed Brown 4780c5033834SJed Brown Level: beginner 4781c5033834SJed Brown 4782bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSAdapt`, `TSErrorWeightedNorm()`, `TSSetTolerances()` 4783c5033834SJed Brown @*/ 4784d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTolerances(TS ts, PetscReal *atol, Vec *vatol, PetscReal *rtol, Vec *vrtol) 4785d71ae5a4SJacob Faibussowitsch { 4786c5033834SJed Brown PetscFunctionBegin; 4787c5033834SJed Brown if (atol) *atol = ts->atol; 4788c5033834SJed Brown if (vatol) *vatol = ts->vatol; 4789c5033834SJed Brown if (rtol) *rtol = ts->rtol; 4790c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 4791c5033834SJed Brown PetscFunctionReturn(0); 4792c5033834SJed Brown } 4793c5033834SJed Brown 47949c6b16b5SShri Abhyankar /*@ 4795a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 47969c6b16b5SShri Abhyankar 4797bcf0153eSBarry Smith Collective on ts 47989c6b16b5SShri Abhyankar 47994165533cSJose E. Roman Input Parameters: 48009c6b16b5SShri Abhyankar + ts - time stepping context 4801a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 4802a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 48039c6b16b5SShri Abhyankar 48044165533cSJose E. Roman Output Parameters: 4805a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 48067453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 4807a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 48089c6b16b5SShri Abhyankar 48099c6b16b5SShri Abhyankar Level: developer 48109c6b16b5SShri Abhyankar 4811bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSErrorWeightedNorm()`, `TSErrorWeightedNormInfinity()` 48129c6b16b5SShri Abhyankar @*/ 4813d71ae5a4SJacob Faibussowitsch PetscErrorCode TSErrorWeightedNorm2(TS ts, Vec U, Vec Y, PetscReal *norm, PetscReal *norma, PetscReal *normr) 4814d71ae5a4SJacob Faibussowitsch { 48159c6b16b5SShri Abhyankar PetscInt i, n, N, rstart; 48167453f775SEmil Constantinescu PetscInt n_loc, na_loc, nr_loc; 48177453f775SEmil Constantinescu PetscReal n_glb, na_glb, nr_glb; 48189c6b16b5SShri Abhyankar const PetscScalar *u, *y; 48197453f775SEmil Constantinescu PetscReal sum, suma, sumr, gsum, gsuma, gsumr, diff; 48207453f775SEmil Constantinescu PetscReal tol, tola, tolr; 48217453f775SEmil Constantinescu PetscReal err_loc[6], err_glb[6]; 48229c6b16b5SShri Abhyankar 48239c6b16b5SShri Abhyankar PetscFunctionBegin; 48249c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4825a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U, VEC_CLASSID, 2); 4826a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y, VEC_CLASSID, 3); 4827a4868fbcSLisandro Dalcin PetscValidType(U, 2); 4828a4868fbcSLisandro Dalcin PetscValidType(Y, 3); 4829a4868fbcSLisandro Dalcin PetscCheckSameComm(U, 2, Y, 3); 4830dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm, 4); 4831dadcf809SJacob Faibussowitsch PetscValidRealPointer(norma, 5); 4832dadcf809SJacob Faibussowitsch PetscValidRealPointer(normr, 6); 48333c633725SBarry Smith PetscCheck(U != Y, PetscObjectComm((PetscObject)U), PETSC_ERR_ARG_IDN, "U and Y cannot be the same vector"); 48349c6b16b5SShri Abhyankar 48359566063dSJacob Faibussowitsch PetscCall(VecGetSize(U, &N)); 48369566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(U, &n)); 48379566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(U, &rstart, NULL)); 48389566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(U, &u)); 48399566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(Y, &y)); 48409371c9d4SSatish Balay sum = 0.; 48419371c9d4SSatish Balay n_loc = 0; 48429371c9d4SSatish Balay suma = 0.; 48439371c9d4SSatish Balay na_loc = 0; 48449371c9d4SSatish Balay sumr = 0.; 48459371c9d4SSatish Balay nr_loc = 0; 48469c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 48479c6b16b5SShri Abhyankar const PetscScalar *atol, *rtol; 48489566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol, &atol)); 48499566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol, &rtol)); 48509c6b16b5SShri Abhyankar for (i = 0; i < n; i++) { 485176cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 48527453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 48537453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 48547453f775SEmil Constantinescu if (tola > 0.) { 48557453f775SEmil Constantinescu suma += PetscSqr(diff / tola); 48567453f775SEmil Constantinescu na_loc++; 48577453f775SEmil Constantinescu } 48587453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 48597453f775SEmil Constantinescu if (tolr > 0.) { 48607453f775SEmil Constantinescu sumr += PetscSqr(diff / tolr); 48617453f775SEmil Constantinescu nr_loc++; 48627453f775SEmil Constantinescu } 48637453f775SEmil Constantinescu tol = tola + tolr; 48647453f775SEmil Constantinescu if (tol > 0.) { 48657453f775SEmil Constantinescu sum += PetscSqr(diff / tol); 48667453f775SEmil Constantinescu n_loc++; 48677453f775SEmil Constantinescu } 48689c6b16b5SShri Abhyankar } 48699566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol, &atol)); 48709566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol, &rtol)); 48719c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 48729c6b16b5SShri Abhyankar const PetscScalar *atol; 48739566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol, &atol)); 48749c6b16b5SShri Abhyankar for (i = 0; i < n; i++) { 487576cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 48767453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 48777453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 48787453f775SEmil Constantinescu if (tola > 0.) { 48797453f775SEmil Constantinescu suma += PetscSqr(diff / tola); 48807453f775SEmil Constantinescu na_loc++; 48817453f775SEmil Constantinescu } 48827453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 48837453f775SEmil Constantinescu if (tolr > 0.) { 48847453f775SEmil Constantinescu sumr += PetscSqr(diff / tolr); 48857453f775SEmil Constantinescu nr_loc++; 48867453f775SEmil Constantinescu } 48877453f775SEmil Constantinescu tol = tola + tolr; 48887453f775SEmil Constantinescu if (tol > 0.) { 48897453f775SEmil Constantinescu sum += PetscSqr(diff / tol); 48907453f775SEmil Constantinescu n_loc++; 48917453f775SEmil Constantinescu } 48929c6b16b5SShri Abhyankar } 48939566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol, &atol)); 48949c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 48959c6b16b5SShri Abhyankar const PetscScalar *rtol; 48969566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol, &rtol)); 48979c6b16b5SShri Abhyankar for (i = 0; i < n; i++) { 489876cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 48997453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 49007453f775SEmil Constantinescu tola = ts->atol; 49017453f775SEmil Constantinescu if (tola > 0.) { 49027453f775SEmil Constantinescu suma += PetscSqr(diff / tola); 49037453f775SEmil Constantinescu na_loc++; 49047453f775SEmil Constantinescu } 49057453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 49067453f775SEmil Constantinescu if (tolr > 0.) { 49077453f775SEmil Constantinescu sumr += PetscSqr(diff / tolr); 49087453f775SEmil Constantinescu nr_loc++; 49097453f775SEmil Constantinescu } 49107453f775SEmil Constantinescu tol = tola + tolr; 49117453f775SEmil Constantinescu if (tol > 0.) { 49127453f775SEmil Constantinescu sum += PetscSqr(diff / tol); 49137453f775SEmil Constantinescu n_loc++; 49147453f775SEmil Constantinescu } 49159c6b16b5SShri Abhyankar } 49169566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol, &rtol)); 49179c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 49189c6b16b5SShri Abhyankar for (i = 0; i < n; i++) { 491976cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 49207453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 49217453f775SEmil Constantinescu tola = ts->atol; 49227453f775SEmil Constantinescu if (tola > 0.) { 49237453f775SEmil Constantinescu suma += PetscSqr(diff / tola); 49247453f775SEmil Constantinescu na_loc++; 49257453f775SEmil Constantinescu } 49267453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 49277453f775SEmil Constantinescu if (tolr > 0.) { 49287453f775SEmil Constantinescu sumr += PetscSqr(diff / tolr); 49297453f775SEmil Constantinescu nr_loc++; 49307453f775SEmil Constantinescu } 49317453f775SEmil Constantinescu tol = tola + tolr; 49327453f775SEmil Constantinescu if (tol > 0.) { 49337453f775SEmil Constantinescu sum += PetscSqr(diff / tol); 49347453f775SEmil Constantinescu n_loc++; 49357453f775SEmil Constantinescu } 49369c6b16b5SShri Abhyankar } 49379c6b16b5SShri Abhyankar } 49389566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(U, &u)); 49399566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(Y, &y)); 49409c6b16b5SShri Abhyankar 49417453f775SEmil Constantinescu err_loc[0] = sum; 49427453f775SEmil Constantinescu err_loc[1] = suma; 49437453f775SEmil Constantinescu err_loc[2] = sumr; 49447453f775SEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 49457453f775SEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 49467453f775SEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 49477453f775SEmil Constantinescu 49481c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(err_loc, err_glb, 6, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)ts))); 49497453f775SEmil Constantinescu 49507453f775SEmil Constantinescu gsum = err_glb[0]; 49517453f775SEmil Constantinescu gsuma = err_glb[1]; 49527453f775SEmil Constantinescu gsumr = err_glb[2]; 49537453f775SEmil Constantinescu n_glb = err_glb[3]; 49547453f775SEmil Constantinescu na_glb = err_glb[4]; 49557453f775SEmil Constantinescu nr_glb = err_glb[5]; 49567453f775SEmil Constantinescu 4957b1316ef9SEmil Constantinescu *norm = 0.; 49581e66621cSBarry Smith if (n_glb > 0.) *norm = PetscSqrtReal(gsum / n_glb); 4959b1316ef9SEmil Constantinescu *norma = 0.; 49601e66621cSBarry Smith if (na_glb > 0.) *norma = PetscSqrtReal(gsuma / na_glb); 4961b1316ef9SEmil Constantinescu *normr = 0.; 49621e66621cSBarry Smith if (nr_glb > 0.) *normr = PetscSqrtReal(gsumr / nr_glb); 49639c6b16b5SShri Abhyankar 49643c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norm"); 49653c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norma"); 49663c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in normr"); 49679c6b16b5SShri Abhyankar PetscFunctionReturn(0); 49689c6b16b5SShri Abhyankar } 49699c6b16b5SShri Abhyankar 49709c6b16b5SShri Abhyankar /*@ 4971a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 49729c6b16b5SShri Abhyankar 4973bcf0153eSBarry Smith Collective on ts 49749c6b16b5SShri Abhyankar 49754165533cSJose E. Roman Input Parameters: 49769c6b16b5SShri Abhyankar + ts - time stepping context 4977a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 4978a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 49799c6b16b5SShri Abhyankar 49804165533cSJose E. Roman Output Parameters: 4981a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 49827453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 4983a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 49849c6b16b5SShri Abhyankar 49859c6b16b5SShri Abhyankar Level: developer 49869c6b16b5SShri Abhyankar 4987bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSErrorWeightedNorm()`, `TSErrorWeightedNorm2()` 49889c6b16b5SShri Abhyankar @*/ 4989d71ae5a4SJacob Faibussowitsch PetscErrorCode TSErrorWeightedNormInfinity(TS ts, Vec U, Vec Y, PetscReal *norm, PetscReal *norma, PetscReal *normr) 4990d71ae5a4SJacob Faibussowitsch { 49917453f775SEmil Constantinescu PetscInt i, n, N, rstart; 49929c6b16b5SShri Abhyankar const PetscScalar *u, *y; 49937453f775SEmil Constantinescu PetscReal max, gmax, maxa, gmaxa, maxr, gmaxr; 49947453f775SEmil Constantinescu PetscReal tol, tola, tolr, diff; 49957453f775SEmil Constantinescu PetscReal err_loc[3], err_glb[3]; 49969c6b16b5SShri Abhyankar 49979c6b16b5SShri Abhyankar PetscFunctionBegin; 49989c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4999a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U, VEC_CLASSID, 2); 5000a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y, VEC_CLASSID, 3); 5001a4868fbcSLisandro Dalcin PetscValidType(U, 2); 5002a4868fbcSLisandro Dalcin PetscValidType(Y, 3); 5003a4868fbcSLisandro Dalcin PetscCheckSameComm(U, 2, Y, 3); 5004dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm, 4); 5005dadcf809SJacob Faibussowitsch PetscValidRealPointer(norma, 5); 5006dadcf809SJacob Faibussowitsch PetscValidRealPointer(normr, 6); 50073c633725SBarry Smith PetscCheck(U != Y, PetscObjectComm((PetscObject)U), PETSC_ERR_ARG_IDN, "U and Y cannot be the same vector"); 50089c6b16b5SShri Abhyankar 50099566063dSJacob Faibussowitsch PetscCall(VecGetSize(U, &N)); 50109566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(U, &n)); 50119566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(U, &rstart, NULL)); 50129566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(U, &u)); 50139566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(Y, &y)); 50147453f775SEmil Constantinescu 50157453f775SEmil Constantinescu max = 0.; 50167453f775SEmil Constantinescu maxa = 0.; 50177453f775SEmil Constantinescu maxr = 0.; 50187453f775SEmil Constantinescu 50197453f775SEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 50209c6b16b5SShri Abhyankar const PetscScalar *atol, *rtol; 50219566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol, &atol)); 50229566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol, &rtol)); 50237453f775SEmil Constantinescu 50247453f775SEmil Constantinescu for (i = 0; i < n; i++) { 502576cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 50267453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50277453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 50287453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 50297453f775SEmil Constantinescu tol = tola + tolr; 50301e66621cSBarry Smith if (tola > 0.) maxa = PetscMax(maxa, diff / tola); 50311e66621cSBarry Smith if (tolr > 0.) maxr = PetscMax(maxr, diff / tolr); 50321e66621cSBarry Smith if (tol > 0.) max = PetscMax(max, diff / tol); 50339c6b16b5SShri Abhyankar } 50349566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol, &atol)); 50359566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol, &rtol)); 50369c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 50379c6b16b5SShri Abhyankar const PetscScalar *atol; 50389566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol, &atol)); 50397453f775SEmil Constantinescu for (i = 0; i < n; i++) { 504076cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 50417453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50427453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 50437453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 50447453f775SEmil Constantinescu tol = tola + tolr; 50451e66621cSBarry Smith if (tola > 0.) maxa = PetscMax(maxa, diff / tola); 50461e66621cSBarry Smith if (tolr > 0.) maxr = PetscMax(maxr, diff / tolr); 50471e66621cSBarry Smith if (tol > 0.) max = PetscMax(max, diff / tol); 50489c6b16b5SShri Abhyankar } 50499566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol, &atol)); 50509c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 50519c6b16b5SShri Abhyankar const PetscScalar *rtol; 50529566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol, &rtol)); 50537453f775SEmil Constantinescu 50547453f775SEmil Constantinescu for (i = 0; i < n; i++) { 505576cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 50567453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50577453f775SEmil Constantinescu tola = ts->atol; 50587453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 50597453f775SEmil Constantinescu tol = tola + tolr; 50601e66621cSBarry Smith if (tola > 0.) maxa = PetscMax(maxa, diff / tola); 50611e66621cSBarry Smith if (tolr > 0.) maxr = PetscMax(maxr, diff / tolr); 50621e66621cSBarry Smith if (tol > 0.) max = PetscMax(max, diff / tol); 50639c6b16b5SShri Abhyankar } 50649566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol, &rtol)); 50659c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 50667453f775SEmil Constantinescu 50677453f775SEmil Constantinescu for (i = 0; i < n; i++) { 506876cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 50697453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50707453f775SEmil Constantinescu tola = ts->atol; 50717453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 50727453f775SEmil Constantinescu tol = tola + tolr; 50731e66621cSBarry Smith if (tola > 0.) maxa = PetscMax(maxa, diff / tola); 50741e66621cSBarry Smith if (tolr > 0.) maxr = PetscMax(maxr, diff / tolr); 50751e66621cSBarry Smith if (tol > 0.) max = PetscMax(max, diff / tol); 50769c6b16b5SShri Abhyankar } 50779c6b16b5SShri Abhyankar } 50789566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(U, &u)); 50799566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(Y, &y)); 50807453f775SEmil Constantinescu err_loc[0] = max; 50817453f775SEmil Constantinescu err_loc[1] = maxa; 50827453f775SEmil Constantinescu err_loc[2] = maxr; 50831c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(err_loc, err_glb, 3, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject)ts))); 50847453f775SEmil Constantinescu gmax = err_glb[0]; 50857453f775SEmil Constantinescu gmaxa = err_glb[1]; 50867453f775SEmil Constantinescu gmaxr = err_glb[2]; 50879c6b16b5SShri Abhyankar 50889c6b16b5SShri Abhyankar *norm = gmax; 50897453f775SEmil Constantinescu *norma = gmaxa; 50907453f775SEmil Constantinescu *normr = gmaxr; 50913c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norm"); 50923c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norma"); 50933c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in normr"); 50949c6b16b5SShri Abhyankar PetscFunctionReturn(0); 50959c6b16b5SShri Abhyankar } 50969c6b16b5SShri Abhyankar 50971c3436cfSJed Brown /*@ 50988a175baeSEmil Constantinescu TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances 50991c3436cfSJed Brown 5100bcf0153eSBarry Smith Collective on ts 51011c3436cfSJed Brown 51024165533cSJose E. Roman Input Parameters: 51031c3436cfSJed Brown + ts - time stepping context 5104a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5105a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 5106bcf0153eSBarry Smith - wnormtype - norm type, either `NORM_2` or `NORM_INFINITY` 51077619abb3SShri 51084165533cSJose E. Roman Output Parameters: 5109a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 51108a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 5111a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 5112a4868fbcSLisandro Dalcin 5113bcf0153eSBarry Smith Options Database Key: 5114a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 5115a4868fbcSLisandro Dalcin 51161c3436cfSJed Brown Level: developer 51171c3436cfSJed Brown 5118bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSErrorWeightedNormInfinity()`, `TSErrorWeightedNorm2()`, `TSErrorWeightedENorm` 51191c3436cfSJed Brown @*/ 5120d71ae5a4SJacob Faibussowitsch PetscErrorCode TSErrorWeightedNorm(TS ts, Vec U, Vec Y, NormType wnormtype, PetscReal *norm, PetscReal *norma, PetscReal *normr) 5121d71ae5a4SJacob Faibussowitsch { 51221c3436cfSJed Brown PetscFunctionBegin; 51231e66621cSBarry Smith if (wnormtype == NORM_2) PetscCall(TSErrorWeightedNorm2(ts, U, Y, norm, norma, normr)); 51241e66621cSBarry Smith else if (wnormtype == NORM_INFINITY) PetscCall(TSErrorWeightedNormInfinity(ts, U, Y, norm, norma, normr)); 51251e66621cSBarry Smith else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for norm type %s", NormTypes[wnormtype]); 51261c3436cfSJed Brown PetscFunctionReturn(0); 51271c3436cfSJed Brown } 51281c3436cfSJed Brown 51298a175baeSEmil Constantinescu /*@ 51308a175baeSEmil Constantinescu TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances 51318a175baeSEmil Constantinescu 5132bcf0153eSBarry Smith Collective on ts 51338a175baeSEmil Constantinescu 51344165533cSJose E. Roman Input Parameters: 51358a175baeSEmil Constantinescu + ts - time stepping context 51368a175baeSEmil Constantinescu . E - error vector 51378a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 51388a175baeSEmil Constantinescu - Y - state vector, previous time step 51398a175baeSEmil Constantinescu 51404165533cSJose E. Roman Output Parameters: 5141a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 51428a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5143a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 51448a175baeSEmil Constantinescu 51458a175baeSEmil Constantinescu Level: developer 51468a175baeSEmil Constantinescu 5147bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSErrorWeightedENorm()`, `TSErrorWeightedENormInfinity()` 51488a175baeSEmil Constantinescu @*/ 5149d71ae5a4SJacob Faibussowitsch PetscErrorCode TSErrorWeightedENorm2(TS ts, Vec E, Vec U, Vec Y, PetscReal *norm, PetscReal *norma, PetscReal *normr) 5150d71ae5a4SJacob Faibussowitsch { 51518a175baeSEmil Constantinescu PetscInt i, n, N, rstart; 51528a175baeSEmil Constantinescu PetscInt n_loc, na_loc, nr_loc; 51538a175baeSEmil Constantinescu PetscReal n_glb, na_glb, nr_glb; 51548a175baeSEmil Constantinescu const PetscScalar *e, *u, *y; 51558a175baeSEmil Constantinescu PetscReal err, sum, suma, sumr, gsum, gsuma, gsumr; 51568a175baeSEmil Constantinescu PetscReal tol, tola, tolr; 51578a175baeSEmil Constantinescu PetscReal err_loc[6], err_glb[6]; 51588a175baeSEmil Constantinescu 51598a175baeSEmil Constantinescu PetscFunctionBegin; 51608a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 51618a175baeSEmil Constantinescu PetscValidHeaderSpecific(E, VEC_CLASSID, 2); 51628a175baeSEmil Constantinescu PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 51638a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y, VEC_CLASSID, 4); 51648a175baeSEmil Constantinescu PetscValidType(E, 2); 51658a175baeSEmil Constantinescu PetscValidType(U, 3); 51668a175baeSEmil Constantinescu PetscValidType(Y, 4); 51678a175baeSEmil Constantinescu PetscCheckSameComm(E, 2, U, 3); 5168064a246eSJacob Faibussowitsch PetscCheckSameComm(U, 3, Y, 4); 5169dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm, 5); 5170dadcf809SJacob Faibussowitsch PetscValidRealPointer(norma, 6); 5171dadcf809SJacob Faibussowitsch PetscValidRealPointer(normr, 7); 51728a175baeSEmil Constantinescu 51739566063dSJacob Faibussowitsch PetscCall(VecGetSize(E, &N)); 51749566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(E, &n)); 51759566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(E, &rstart, NULL)); 51769566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(E, &e)); 51779566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(U, &u)); 51789566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(Y, &y)); 51799371c9d4SSatish Balay sum = 0.; 51809371c9d4SSatish Balay n_loc = 0; 51819371c9d4SSatish Balay suma = 0.; 51829371c9d4SSatish Balay na_loc = 0; 51839371c9d4SSatish Balay sumr = 0.; 51849371c9d4SSatish Balay nr_loc = 0; 51858a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { 51868a175baeSEmil Constantinescu const PetscScalar *atol, *rtol; 51879566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol, &atol)); 51889566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol, &rtol)); 51898a175baeSEmil Constantinescu for (i = 0; i < n; i++) { 519076cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 51918a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 51928a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 51938a175baeSEmil Constantinescu if (tola > 0.) { 51948a175baeSEmil Constantinescu suma += PetscSqr(err / tola); 51958a175baeSEmil Constantinescu na_loc++; 51968a175baeSEmil Constantinescu } 51978a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 51988a175baeSEmil Constantinescu if (tolr > 0.) { 51998a175baeSEmil Constantinescu sumr += PetscSqr(err / tolr); 52008a175baeSEmil Constantinescu nr_loc++; 52018a175baeSEmil Constantinescu } 52028a175baeSEmil Constantinescu tol = tola + tolr; 52038a175baeSEmil Constantinescu if (tol > 0.) { 52048a175baeSEmil Constantinescu sum += PetscSqr(err / tol); 52058a175baeSEmil Constantinescu n_loc++; 52068a175baeSEmil Constantinescu } 52078a175baeSEmil Constantinescu } 52089566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol, &atol)); 52099566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol, &rtol)); 52108a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 52118a175baeSEmil Constantinescu const PetscScalar *atol; 52129566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol, &atol)); 52138a175baeSEmil Constantinescu for (i = 0; i < n; i++) { 521476cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 52158a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 52168a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 52178a175baeSEmil Constantinescu if (tola > 0.) { 52188a175baeSEmil Constantinescu suma += PetscSqr(err / tola); 52198a175baeSEmil Constantinescu na_loc++; 52208a175baeSEmil Constantinescu } 52218a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 52228a175baeSEmil Constantinescu if (tolr > 0.) { 52238a175baeSEmil Constantinescu sumr += PetscSqr(err / tolr); 52248a175baeSEmil Constantinescu nr_loc++; 52258a175baeSEmil Constantinescu } 52268a175baeSEmil Constantinescu tol = tola + tolr; 52278a175baeSEmil Constantinescu if (tol > 0.) { 52288a175baeSEmil Constantinescu sum += PetscSqr(err / tol); 52298a175baeSEmil Constantinescu n_loc++; 52308a175baeSEmil Constantinescu } 52318a175baeSEmil Constantinescu } 52329566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol, &atol)); 52338a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 52348a175baeSEmil Constantinescu const PetscScalar *rtol; 52359566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol, &rtol)); 52368a175baeSEmil Constantinescu for (i = 0; i < n; i++) { 523776cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 52388a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 52398a175baeSEmil Constantinescu tola = ts->atol; 52408a175baeSEmil Constantinescu if (tola > 0.) { 52418a175baeSEmil Constantinescu suma += PetscSqr(err / tola); 52428a175baeSEmil Constantinescu na_loc++; 52438a175baeSEmil Constantinescu } 52448a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 52458a175baeSEmil Constantinescu if (tolr > 0.) { 52468a175baeSEmil Constantinescu sumr += PetscSqr(err / tolr); 52478a175baeSEmil Constantinescu nr_loc++; 52488a175baeSEmil Constantinescu } 52498a175baeSEmil Constantinescu tol = tola + tolr; 52508a175baeSEmil Constantinescu if (tol > 0.) { 52518a175baeSEmil Constantinescu sum += PetscSqr(err / tol); 52528a175baeSEmil Constantinescu n_loc++; 52538a175baeSEmil Constantinescu } 52548a175baeSEmil Constantinescu } 52559566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol, &rtol)); 52568a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 52578a175baeSEmil Constantinescu for (i = 0; i < n; i++) { 525876cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 52598a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 52608a175baeSEmil Constantinescu tola = ts->atol; 52618a175baeSEmil Constantinescu if (tola > 0.) { 52628a175baeSEmil Constantinescu suma += PetscSqr(err / tola); 52638a175baeSEmil Constantinescu na_loc++; 52648a175baeSEmil Constantinescu } 52658a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 52668a175baeSEmil Constantinescu if (tolr > 0.) { 52678a175baeSEmil Constantinescu sumr += PetscSqr(err / tolr); 52688a175baeSEmil Constantinescu nr_loc++; 52698a175baeSEmil Constantinescu } 52708a175baeSEmil Constantinescu tol = tola + tolr; 52718a175baeSEmil Constantinescu if (tol > 0.) { 52728a175baeSEmil Constantinescu sum += PetscSqr(err / tol); 52738a175baeSEmil Constantinescu n_loc++; 52748a175baeSEmil Constantinescu } 52758a175baeSEmil Constantinescu } 52768a175baeSEmil Constantinescu } 52779566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(E, &e)); 52789566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(U, &u)); 52799566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(Y, &y)); 52808a175baeSEmil Constantinescu 52818a175baeSEmil Constantinescu err_loc[0] = sum; 52828a175baeSEmil Constantinescu err_loc[1] = suma; 52838a175baeSEmil Constantinescu err_loc[2] = sumr; 52848a175baeSEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 52858a175baeSEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 52868a175baeSEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 52878a175baeSEmil Constantinescu 52881c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(err_loc, err_glb, 6, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)ts))); 52898a175baeSEmil Constantinescu 52908a175baeSEmil Constantinescu gsum = err_glb[0]; 52918a175baeSEmil Constantinescu gsuma = err_glb[1]; 52928a175baeSEmil Constantinescu gsumr = err_glb[2]; 52938a175baeSEmil Constantinescu n_glb = err_glb[3]; 52948a175baeSEmil Constantinescu na_glb = err_glb[4]; 52958a175baeSEmil Constantinescu nr_glb = err_glb[5]; 52968a175baeSEmil Constantinescu 52978a175baeSEmil Constantinescu *norm = 0.; 52981e66621cSBarry Smith if (n_glb > 0.) *norm = PetscSqrtReal(gsum / n_glb); 52998a175baeSEmil Constantinescu *norma = 0.; 53001e66621cSBarry Smith if (na_glb > 0.) *norma = PetscSqrtReal(gsuma / na_glb); 53018a175baeSEmil Constantinescu *normr = 0.; 53021e66621cSBarry Smith if (nr_glb > 0.) *normr = PetscSqrtReal(gsumr / nr_glb); 53038a175baeSEmil Constantinescu 53043c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norm"); 53053c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norma"); 53063c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in normr"); 53078a175baeSEmil Constantinescu PetscFunctionReturn(0); 53088a175baeSEmil Constantinescu } 53098a175baeSEmil Constantinescu 53108a175baeSEmil Constantinescu /*@ 53118a175baeSEmil Constantinescu TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances 5312bcf0153eSBarry Smith 5313bcf0153eSBarry Smith Collective on ts 53148a175baeSEmil Constantinescu 53154165533cSJose E. Roman Input Parameters: 53168a175baeSEmil Constantinescu + ts - time stepping context 53178a175baeSEmil Constantinescu . E - error vector 53188a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 53198a175baeSEmil Constantinescu - Y - state vector, previous time step 53208a175baeSEmil Constantinescu 53214165533cSJose E. Roman Output Parameters: 5322a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 53238a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5324a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 53258a175baeSEmil Constantinescu 53268a175baeSEmil Constantinescu Level: developer 53278a175baeSEmil Constantinescu 5328bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSErrorWeightedENorm()`, `TSErrorWeightedENorm2()` 53298a175baeSEmil Constantinescu @*/ 5330d71ae5a4SJacob Faibussowitsch PetscErrorCode TSErrorWeightedENormInfinity(TS ts, Vec E, Vec U, Vec Y, PetscReal *norm, PetscReal *norma, PetscReal *normr) 5331d71ae5a4SJacob Faibussowitsch { 53328a175baeSEmil Constantinescu PetscInt i, n, N, rstart; 53338a175baeSEmil Constantinescu const PetscScalar *e, *u, *y; 53348a175baeSEmil Constantinescu PetscReal err, max, gmax, maxa, gmaxa, maxr, gmaxr; 53358a175baeSEmil Constantinescu PetscReal tol, tola, tolr; 53368a175baeSEmil Constantinescu PetscReal err_loc[3], err_glb[3]; 53378a175baeSEmil Constantinescu 53388a175baeSEmil Constantinescu PetscFunctionBegin; 53398a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 53408a175baeSEmil Constantinescu PetscValidHeaderSpecific(E, VEC_CLASSID, 2); 53418a175baeSEmil Constantinescu PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 53428a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y, VEC_CLASSID, 4); 53438a175baeSEmil Constantinescu PetscValidType(E, 2); 53448a175baeSEmil Constantinescu PetscValidType(U, 3); 53458a175baeSEmil Constantinescu PetscValidType(Y, 4); 53468a175baeSEmil Constantinescu PetscCheckSameComm(E, 2, U, 3); 5347064a246eSJacob Faibussowitsch PetscCheckSameComm(U, 3, Y, 4); 5348dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm, 5); 5349dadcf809SJacob Faibussowitsch PetscValidRealPointer(norma, 6); 5350dadcf809SJacob Faibussowitsch PetscValidRealPointer(normr, 7); 53518a175baeSEmil Constantinescu 53529566063dSJacob Faibussowitsch PetscCall(VecGetSize(E, &N)); 53539566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(E, &n)); 53549566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(E, &rstart, NULL)); 53559566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(E, &e)); 53569566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(U, &u)); 53579566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(Y, &y)); 53588a175baeSEmil Constantinescu 53598a175baeSEmil Constantinescu max = 0.; 53608a175baeSEmil Constantinescu maxa = 0.; 53618a175baeSEmil Constantinescu maxr = 0.; 53628a175baeSEmil Constantinescu 53638a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 53648a175baeSEmil Constantinescu const PetscScalar *atol, *rtol; 53659566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol, &atol)); 53669566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol, &rtol)); 53678a175baeSEmil Constantinescu 53688a175baeSEmil Constantinescu for (i = 0; i < n; i++) { 536976cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 53708a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 53718a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 53728a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 53738a175baeSEmil Constantinescu tol = tola + tolr; 53741e66621cSBarry Smith if (tola > 0.) maxa = PetscMax(maxa, err / tola); 53751e66621cSBarry Smith if (tolr > 0.) maxr = PetscMax(maxr, err / tolr); 53761e66621cSBarry Smith if (tol > 0.) max = PetscMax(max, err / tol); 53778a175baeSEmil Constantinescu } 53789566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol, &atol)); 53799566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol, &rtol)); 53808a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 53818a175baeSEmil Constantinescu const PetscScalar *atol; 53829566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol, &atol)); 53838a175baeSEmil Constantinescu for (i = 0; i < n; i++) { 538476cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 53858a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 53868a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 53878a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 53888a175baeSEmil Constantinescu tol = tola + tolr; 53891e66621cSBarry Smith if (tola > 0.) maxa = PetscMax(maxa, err / tola); 53901e66621cSBarry Smith if (tolr > 0.) maxr = PetscMax(maxr, err / tolr); 53911e66621cSBarry Smith if (tol > 0.) max = PetscMax(max, err / tol); 53928a175baeSEmil Constantinescu } 53939566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol, &atol)); 53948a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 53958a175baeSEmil Constantinescu const PetscScalar *rtol; 53969566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol, &rtol)); 53978a175baeSEmil Constantinescu 53988a175baeSEmil Constantinescu for (i = 0; i < n; i++) { 539976cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 54008a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 54018a175baeSEmil Constantinescu tola = ts->atol; 54028a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 54038a175baeSEmil Constantinescu tol = tola + tolr; 54041e66621cSBarry Smith if (tola > 0.) maxa = PetscMax(maxa, err / tola); 54051e66621cSBarry Smith if (tolr > 0.) maxr = PetscMax(maxr, err / tolr); 54061e66621cSBarry Smith if (tol > 0.) max = PetscMax(max, err / tol); 54078a175baeSEmil Constantinescu } 54089566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol, &rtol)); 54098a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 54108a175baeSEmil Constantinescu 54118a175baeSEmil Constantinescu for (i = 0; i < n; i++) { 541276cddca1SEmil Constantinescu SkipSmallValue(y[i], u[i], ts->adapt->ignore_max); 54138a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 54148a175baeSEmil Constantinescu tola = ts->atol; 54158a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]), PetscAbsScalar(y[i])); 54168a175baeSEmil Constantinescu tol = tola + tolr; 54171e66621cSBarry Smith if (tola > 0.) maxa = PetscMax(maxa, err / tola); 54181e66621cSBarry Smith if (tolr > 0.) maxr = PetscMax(maxr, err / tolr); 54191e66621cSBarry Smith if (tol > 0.) max = PetscMax(max, err / tol); 54208a175baeSEmil Constantinescu } 54218a175baeSEmil Constantinescu } 54229566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(E, &e)); 54239566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(U, &u)); 54249566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(Y, &y)); 54258a175baeSEmil Constantinescu err_loc[0] = max; 54268a175baeSEmil Constantinescu err_loc[1] = maxa; 54278a175baeSEmil Constantinescu err_loc[2] = maxr; 54281c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(err_loc, err_glb, 3, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject)ts))); 54298a175baeSEmil Constantinescu gmax = err_glb[0]; 54308a175baeSEmil Constantinescu gmaxa = err_glb[1]; 54318a175baeSEmil Constantinescu gmaxr = err_glb[2]; 54328a175baeSEmil Constantinescu 54338a175baeSEmil Constantinescu *norm = gmax; 54348a175baeSEmil Constantinescu *norma = gmaxa; 54358a175baeSEmil Constantinescu *normr = gmaxr; 54363c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norm"); 54373c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norma"); 54383c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in normr"); 54398a175baeSEmil Constantinescu PetscFunctionReturn(0); 54408a175baeSEmil Constantinescu } 54418a175baeSEmil Constantinescu 54428a175baeSEmil Constantinescu /*@ 54438a175baeSEmil Constantinescu TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances 54448a175baeSEmil Constantinescu 5445bcf0153eSBarry Smith Collective on ts 54468a175baeSEmil Constantinescu 54474165533cSJose E. Roman Input Parameters: 54488a175baeSEmil Constantinescu + ts - time stepping context 54498a175baeSEmil Constantinescu . E - error vector 54508a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 54518a175baeSEmil Constantinescu . Y - state vector, previous time step 5452bcf0153eSBarry Smith - wnormtype - norm type, either `NORM_2` or `NORM_INFINITY` 54538a175baeSEmil Constantinescu 54544165533cSJose E. Roman Output Parameters: 5455a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 54568a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 5457a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 54588a175baeSEmil Constantinescu 5459bcf0153eSBarry Smith Options Database Key: 54608a175baeSEmil Constantinescu . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 54618a175baeSEmil Constantinescu 54628a175baeSEmil Constantinescu Level: developer 54638a175baeSEmil Constantinescu 5464bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSErrorWeightedENormInfinity()`, `TSErrorWeightedENorm2()`, `TSErrorWeightedNormInfinity()`, `TSErrorWeightedNorm2()` 54658a175baeSEmil Constantinescu @*/ 5466d71ae5a4SJacob Faibussowitsch PetscErrorCode TSErrorWeightedENorm(TS ts, Vec E, Vec U, Vec Y, NormType wnormtype, PetscReal *norm, PetscReal *norma, PetscReal *normr) 5467d71ae5a4SJacob Faibussowitsch { 54688a175baeSEmil Constantinescu PetscFunctionBegin; 54691e66621cSBarry Smith if (wnormtype == NORM_2) PetscCall(TSErrorWeightedENorm2(ts, E, U, Y, norm, norma, normr)); 54701e66621cSBarry Smith else if (wnormtype == NORM_INFINITY) PetscCall(TSErrorWeightedENormInfinity(ts, E, U, Y, norm, norma, normr)); 54711e66621cSBarry Smith else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for norm type %s", NormTypes[wnormtype]); 54728a175baeSEmil Constantinescu PetscFunctionReturn(0); 54738a175baeSEmil Constantinescu } 54748a175baeSEmil Constantinescu 54758d59e960SJed Brown /*@ 54768d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 54778d59e960SJed Brown 5478bcf0153eSBarry Smith Logically Collective on ts 54798d59e960SJed Brown 54804165533cSJose E. Roman Input Parameters: 54818d59e960SJed Brown + ts - time stepping context 54828d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 54838d59e960SJed Brown 54848d59e960SJed Brown Note: 54858d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 54868d59e960SJed Brown 54878d59e960SJed Brown Level: intermediate 54888d59e960SJed Brown 5489bcf0153eSBarry Smith .seealso: [](chapter_ts), `TSGetCFLTime()`, `TSADAPTCFL` 54908d59e960SJed Brown @*/ 5491d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetCFLTimeLocal(TS ts, PetscReal cfltime) 5492d71ae5a4SJacob Faibussowitsch { 54938d59e960SJed Brown PetscFunctionBegin; 54948d59e960SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 54958d59e960SJed Brown ts->cfltime_local = cfltime; 54968d59e960SJed Brown ts->cfltime = -1.; 54978d59e960SJed Brown PetscFunctionReturn(0); 54988d59e960SJed Brown } 54998d59e960SJed Brown 55008d59e960SJed Brown /*@ 55018d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 55028d59e960SJed Brown 5503bcf0153eSBarry Smith Collective on ts 55048d59e960SJed Brown 55054165533cSJose E. Roman Input Parameter: 55068d59e960SJed Brown . ts - time stepping context 55078d59e960SJed Brown 55084165533cSJose E. Roman Output Parameter: 55098d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 55108d59e960SJed Brown 55118d59e960SJed Brown Level: advanced 55128d59e960SJed Brown 5513bcf0153eSBarry Smith .seealso: [](chapter_ts), `TSSetCFLTimeLocal()` 55148d59e960SJed Brown @*/ 5515d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetCFLTime(TS ts, PetscReal *cfltime) 5516d71ae5a4SJacob Faibussowitsch { 55178d59e960SJed Brown PetscFunctionBegin; 55181e66621cSBarry Smith if (ts->cfltime < 0) PetscCall(MPIU_Allreduce(&ts->cfltime_local, &ts->cfltime, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)ts))); 55198d59e960SJed Brown *cfltime = ts->cfltime; 55208d59e960SJed Brown PetscFunctionReturn(0); 55218d59e960SJed Brown } 55228d59e960SJed Brown 5523d6ebe24aSShri Abhyankar /*@ 5524d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 5525d6ebe24aSShri Abhyankar 5526d6ebe24aSShri Abhyankar Input Parameters: 5527bcf0153eSBarry Smith + ts - the `TS` context. 5528d6ebe24aSShri Abhyankar . xl - lower bound. 5529a2b725a8SWilliam Gropp - xu - upper bound. 5530d6ebe24aSShri Abhyankar 55312bd2b0e6SSatish Balay Level: advanced 55322bd2b0e6SSatish Balay 5533bcf0153eSBarry Smith Note: 5534bcf0153eSBarry Smith If this routine is not called then the lower and upper bounds are set to 5535bcf0153eSBarry Smith `PETSC_NINFINITY` and `PETSC_INFINITY` respectively during `SNESSetUp()`. 5536bcf0153eSBarry Smith 5537bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS` 5538d6ebe24aSShri Abhyankar @*/ 5539d71ae5a4SJacob Faibussowitsch PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 5540d71ae5a4SJacob Faibussowitsch { 5541d6ebe24aSShri Abhyankar SNES snes; 5542d6ebe24aSShri Abhyankar 5543d6ebe24aSShri Abhyankar PetscFunctionBegin; 55449566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 55459566063dSJacob Faibussowitsch PetscCall(SNESVISetVariableBounds(snes, xl, xu)); 5546d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 5547d6ebe24aSShri Abhyankar } 5548d6ebe24aSShri Abhyankar 5549f9c1d6abSBarry Smith /*@ 5550f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 5551f9c1d6abSBarry Smith 5552bcf0153eSBarry Smith Collective on ts 5553f9c1d6abSBarry Smith 5554f9c1d6abSBarry Smith Input Parameters: 5555bcf0153eSBarry Smith + ts - the `TS` context 5556f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 5557f9c1d6abSBarry Smith 5558f9c1d6abSBarry Smith Output Parameters: 5559f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 5560f9c1d6abSBarry Smith 5561f9c1d6abSBarry Smith Level: developer 5562f9c1d6abSBarry Smith 5563bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetRHSFunction()`, `TSComputeIFunction()` 5564f9c1d6abSBarry Smith @*/ 5565d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeLinearStability(TS ts, PetscReal xr, PetscReal xi, PetscReal *yr, PetscReal *yi) 5566d71ae5a4SJacob Faibussowitsch { 5567f9c1d6abSBarry Smith PetscFunctionBegin; 5568f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5569dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, linearstability, xr, xi, yr, yi); 5570f9c1d6abSBarry Smith PetscFunctionReturn(0); 5571f9c1d6abSBarry Smith } 557224655328SShri 557324655328SShri /*@ 5574dcb233daSLisandro Dalcin TSRestartStep - Flags the solver to restart the next step 5575dcb233daSLisandro Dalcin 5576bcf0153eSBarry Smith Collective on ts 5577dcb233daSLisandro Dalcin 5578dcb233daSLisandro Dalcin Input Parameter: 5579bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 5580dcb233daSLisandro Dalcin 5581dcb233daSLisandro Dalcin Level: advanced 5582dcb233daSLisandro Dalcin 5583dcb233daSLisandro Dalcin Notes: 5584bcf0153eSBarry Smith Multistep methods like `TSBDF` or Runge-Kutta methods with FSAL property require restarting the solver in the event of 5585dcb233daSLisandro Dalcin discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution 5586dcb233daSLisandro Dalcin vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For 5587bcf0153eSBarry Smith the sake of correctness and maximum safety, users are expected to call `TSRestart()` whenever they introduce 5588dcb233daSLisandro Dalcin discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with 5589dcb233daSLisandro Dalcin discontinuous source terms). 5590dcb233daSLisandro Dalcin 5591bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSBDF`, `TSSolve()`, `TSSetPreStep()`, `TSSetPostStep()` 5592dcb233daSLisandro Dalcin @*/ 5593d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRestartStep(TS ts) 5594d71ae5a4SJacob Faibussowitsch { 5595dcb233daSLisandro Dalcin PetscFunctionBegin; 5596dcb233daSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5597dcb233daSLisandro Dalcin ts->steprestart = PETSC_TRUE; 5598dcb233daSLisandro Dalcin PetscFunctionReturn(0); 5599dcb233daSLisandro Dalcin } 5600dcb233daSLisandro Dalcin 5601dcb233daSLisandro Dalcin /*@ 560224655328SShri TSRollBack - Rolls back one time step 560324655328SShri 5604bcf0153eSBarry Smith Collective on ts 560524655328SShri 560624655328SShri Input Parameter: 5607bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 560824655328SShri 560924655328SShri Level: advanced 561024655328SShri 5611bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSCreate()`, `TSSetUp()`, `TSDestroy()`, `TSSolve()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSInterpolate()` 561224655328SShri @*/ 5613d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRollBack(TS ts) 5614d71ae5a4SJacob Faibussowitsch { 561524655328SShri PetscFunctionBegin; 561624655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 56173c633725SBarry Smith PetscCheck(!ts->steprollback, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "TSRollBack already called"); 5618dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, rollback); 561924655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 562024655328SShri ts->ptime = ts->ptime_prev; 5621be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime_prev_rollback; 56222808aa04SLisandro Dalcin ts->steps--; 5623b3de5cdeSLisandro Dalcin ts->steprollback = PETSC_TRUE; 562424655328SShri PetscFunctionReturn(0); 562524655328SShri } 5626aeb4809dSShri Abhyankar 5627ff22ae23SHong Zhang /*@ 5628ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 5629ff22ae23SHong Zhang 5630ff22ae23SHong Zhang Input Parameter: 5631bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 5632ff22ae23SHong Zhang 56330429704eSStefano Zampini Output Parameters: 56340429704eSStefano Zampini + ns - the number of stages 56350429704eSStefano Zampini - Y - the current stage vectors 56360429704eSStefano Zampini 5637ff22ae23SHong Zhang Level: advanced 5638ff22ae23SHong Zhang 5639bcf0153eSBarry Smith Note: 5640bcf0153eSBarry Smith Both ns and Y can be NULL. 56410429704eSStefano Zampini 5642bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSCreate()` 5643ff22ae23SHong Zhang @*/ 5644d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetStages(TS ts, PetscInt *ns, Vec **Y) 5645d71ae5a4SJacob Faibussowitsch { 5646ff22ae23SHong Zhang PetscFunctionBegin; 5647ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5648dadcf809SJacob Faibussowitsch if (ns) PetscValidIntPointer(ns, 2); 56490429704eSStefano Zampini if (Y) PetscValidPointer(Y, 3); 56500429704eSStefano Zampini if (!ts->ops->getstages) { 56510429704eSStefano Zampini if (ns) *ns = 0; 56520429704eSStefano Zampini if (Y) *Y = NULL; 5653dbbe0bcdSBarry Smith } else PetscUseTypeMethod(ts, getstages, ns, Y); 5654ff22ae23SHong Zhang PetscFunctionReturn(0); 5655ff22ae23SHong Zhang } 5656ff22ae23SHong Zhang 5657847ff0e1SMatthew G. Knepley /*@C 5658847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 5659847ff0e1SMatthew G. Knepley 5660bcf0153eSBarry Smith Collective on ts 5661847ff0e1SMatthew G. Knepley 5662847ff0e1SMatthew G. Knepley Input Parameters: 5663bcf0153eSBarry Smith + ts - the `TS` context 5664847ff0e1SMatthew G. Knepley . t - current timestep 5665847ff0e1SMatthew G. Knepley . U - state vector 5666847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 5667847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 5668847ff0e1SMatthew G. Knepley - ctx - an optional user context 5669847ff0e1SMatthew G. Knepley 5670847ff0e1SMatthew G. Knepley Output Parameters: 5671847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 5672847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 5673847ff0e1SMatthew G. Knepley 5674847ff0e1SMatthew G. Knepley Level: intermediate 5675847ff0e1SMatthew G. Knepley 5676847ff0e1SMatthew G. Knepley Notes: 5677847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 5678847ff0e1SMatthew G. Knepley 5679847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 5680847ff0e1SMatthew G. Knepley 5681847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 5682847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 5683847ff0e1SMatthew G. Knepley 5684bcf0153eSBarry Smith This will first try to get the coloring from the `DM`. If the `DM` type has no coloring 5685847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 5686847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 5687847ff0e1SMatthew G. Knepley 5688bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetIJacobian()`, `MatFDColoringCreate()`, `MatFDColoringSetFunction()` 5689847ff0e1SMatthew G. Knepley @*/ 5690d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIJacobianDefaultColor(TS ts, PetscReal t, Vec U, Vec Udot, PetscReal shift, Mat J, Mat B, void *ctx) 5691d71ae5a4SJacob Faibussowitsch { 5692847ff0e1SMatthew G. Knepley SNES snes; 5693847ff0e1SMatthew G. Knepley MatFDColoring color; 5694847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 5695847ff0e1SMatthew G. Knepley 5696847ff0e1SMatthew G. Knepley PetscFunctionBegin; 56979566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject)ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL)); 56989566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)B, "TSMatFDColoring", (PetscObject *)&color)); 5699847ff0e1SMatthew G. Knepley if (!color) { 5700847ff0e1SMatthew G. Knepley DM dm; 5701847ff0e1SMatthew G. Knepley ISColoring iscoloring; 5702847ff0e1SMatthew G. Knepley 57039566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 57049566063dSJacob Faibussowitsch PetscCall(DMHasColoring(dm, &hascolor)); 5705847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 57069566063dSJacob Faibussowitsch PetscCall(DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring)); 57079566063dSJacob Faibussowitsch PetscCall(MatFDColoringCreate(B, iscoloring, &color)); 57089566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFunction(color, (PetscErrorCode(*)(void))SNESTSFormFunction, (void *)ts)); 57099566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFromOptions(color)); 57109566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetUp(B, iscoloring, color)); 57119566063dSJacob Faibussowitsch PetscCall(ISColoringDestroy(&iscoloring)); 5712847ff0e1SMatthew G. Knepley } else { 5713847ff0e1SMatthew G. Knepley MatColoring mc; 5714847ff0e1SMatthew G. Knepley 57159566063dSJacob Faibussowitsch PetscCall(MatColoringCreate(B, &mc)); 57169566063dSJacob Faibussowitsch PetscCall(MatColoringSetDistance(mc, 2)); 57179566063dSJacob Faibussowitsch PetscCall(MatColoringSetType(mc, MATCOLORINGSL)); 57189566063dSJacob Faibussowitsch PetscCall(MatColoringSetFromOptions(mc)); 57199566063dSJacob Faibussowitsch PetscCall(MatColoringApply(mc, &iscoloring)); 57209566063dSJacob Faibussowitsch PetscCall(MatColoringDestroy(&mc)); 57219566063dSJacob Faibussowitsch PetscCall(MatFDColoringCreate(B, iscoloring, &color)); 57229566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFunction(color, (PetscErrorCode(*)(void))SNESTSFormFunction, (void *)ts)); 57239566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFromOptions(color)); 57249566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetUp(B, iscoloring, color)); 57259566063dSJacob Faibussowitsch PetscCall(ISColoringDestroy(&iscoloring)); 5726847ff0e1SMatthew G. Knepley } 57279566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject)B, "TSMatFDColoring", (PetscObject)color)); 57289566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)color)); 5729847ff0e1SMatthew G. Knepley } 57309566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 57319566063dSJacob Faibussowitsch PetscCall(MatFDColoringApply(B, color, U, snes)); 5732847ff0e1SMatthew G. Knepley if (J != B) { 57339566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 57349566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 5735847ff0e1SMatthew G. Knepley } 5736847ff0e1SMatthew G. Knepley PetscFunctionReturn(0); 5737847ff0e1SMatthew G. Knepley } 573893b34091SDebojyoti Ghosh 5739cb9d8021SPierre Barbier de Reuille /*@ 57406bc98fa9SBarry Smith TSSetFunctionDomainError - Set a function that tests if the current state vector is valid 5741cb9d8021SPierre Barbier de Reuille 5742cb9d8021SPierre Barbier de Reuille Input Parameters: 5743bcf0153eSBarry Smith + ts - the `TS` context 5744bcf0153eSBarry Smith - func - function called within `TSFunctionDomainError()` 57456bc98fa9SBarry Smith 57466bc98fa9SBarry Smith Calling sequence of func: 57476bc98fa9SBarry Smith $ PetscErrorCode func(TS ts,PetscReal time,Vec state,PetscBool reject) 57486bc98fa9SBarry Smith 57496bc98fa9SBarry Smith + ts - the TS context 57506bc98fa9SBarry Smith . time - the current time (of the stage) 57516bc98fa9SBarry Smith . state - the state to check if it is valid 57526bc98fa9SBarry Smith - reject - (output parameter) PETSC_FALSE if the state is acceptable, PETSC_TRUE if not acceptable 5753cb9d8021SPierre Barbier de Reuille 5754cb9d8021SPierre Barbier de Reuille Level: intermediate 5755cb9d8021SPierre Barbier de Reuille 57566bc98fa9SBarry Smith Notes: 57576bc98fa9SBarry Smith If an implicit ODE solver is being used then, in addition to providing this routine, the 5758bcf0153eSBarry Smith user's code should call `SNESSetFunctionDomainError()` when domain errors occur during 5759bcf0153eSBarry Smith function evaluations where the functions are provided by `TSSetIFunction()` or `TSSetRHSFunction()`. 5760bcf0153eSBarry Smith Use `TSGetSNES()` to obtain the `SNES` object 57616bc98fa9SBarry Smith 5762bcf0153eSBarry Smith Developer Note: 5763bcf0153eSBarry Smith The naming of this function is inconsistent with the `SNESSetFunctionDomainError()` 57646bc98fa9SBarry Smith since one takes a function pointer and the other does not. 57656bc98fa9SBarry Smith 5766bcf0153eSBarry Smith .seealso: [](chapter_ts), `TSAdaptCheckStage()`, `TSFunctionDomainError()`, `SNESSetFunctionDomainError()`, `TSGetSNES()` 5767cb9d8021SPierre Barbier de Reuille @*/ 5768cb9d8021SPierre Barbier de Reuille 5769d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS, PetscReal, Vec, PetscBool *)) 5770d71ae5a4SJacob Faibussowitsch { 5771cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 5772cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5773cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 5774cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 5775cb9d8021SPierre Barbier de Reuille } 5776cb9d8021SPierre Barbier de Reuille 5777cb9d8021SPierre Barbier de Reuille /*@ 57786bc98fa9SBarry Smith TSFunctionDomainError - Checks if the current state is valid 5779cb9d8021SPierre Barbier de Reuille 5780cb9d8021SPierre Barbier de Reuille Input Parameters: 5781bcf0153eSBarry Smith + ts - the `TS` context 57826bc98fa9SBarry Smith . stagetime - time of the simulation 57836bc98fa9SBarry Smith - Y - state vector to check. 5784cb9d8021SPierre Barbier de Reuille 5785cb9d8021SPierre Barbier de Reuille Output Parameter: 5786bcf0153eSBarry Smith . accept - Set to `PETSC_FALSE` if the current state vector is valid. 578796a0c994SBarry Smith 57886bc98fa9SBarry Smith Level: developer 57896bc98fa9SBarry Smith 5790bcf0153eSBarry Smith Note: 5791bcf0153eSBarry Smith This function is called by the `TS` integration routines and calls the user provided function (set with `TSSetFunctionDomainError()`) 5792bcf0153eSBarry Smith to check if the current state is valid. 5793bcf0153eSBarry Smith 5794bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetFunctionDomainError()` 5795cb9d8021SPierre Barbier de Reuille @*/ 5796d71ae5a4SJacob Faibussowitsch PetscErrorCode TSFunctionDomainError(TS ts, PetscReal stagetime, Vec Y, PetscBool *accept) 5797d71ae5a4SJacob Faibussowitsch { 5798cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 5799cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5800cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 58011e66621cSBarry Smith if (ts->functiondomainerror) PetscCall((*ts->functiondomainerror)(ts, stagetime, Y, accept)); 5802cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 5803cb9d8021SPierre Barbier de Reuille } 58041ceb14c0SBarry Smith 580593b34091SDebojyoti Ghosh /*@C 5806e5168f73SEmil Constantinescu TSClone - This function clones a time step object. 580793b34091SDebojyoti Ghosh 5808d083f849SBarry Smith Collective 580993b34091SDebojyoti Ghosh 581093b34091SDebojyoti Ghosh Input Parameter: 5811bcf0153eSBarry Smith . tsin - The input `TS` 581293b34091SDebojyoti Ghosh 581393b34091SDebojyoti Ghosh Output Parameter: 5814bcf0153eSBarry Smith . tsout - The output `TS` (cloned) 58155eca1a21SEmil Constantinescu 58165eca1a21SEmil Constantinescu Level: developer 581793b34091SDebojyoti Ghosh 5818bcf0153eSBarry Smith Notes: 5819bcf0153eSBarry 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. 5820bcf0153eSBarry Smith It will likely be replaced in the future with a mechanism of switching methods on the fly. 5821bcf0153eSBarry Smith 5822bcf0153eSBarry 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 `SNES` snes_dup=NULL; `TSGetSNES`(ts,&snes_dup); `TSSetSNES`(ts,snes_dup); 5823bcf0153eSBarry Smith 5824bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `SNES`, `TSCreate()`, `TSSetType()`, `TSSetUp()`, `TSDestroy()`, `TSSetProblemType()` 582593b34091SDebojyoti Ghosh @*/ 5826d71ae5a4SJacob Faibussowitsch PetscErrorCode TSClone(TS tsin, TS *tsout) 5827d71ae5a4SJacob Faibussowitsch { 582893b34091SDebojyoti Ghosh TS t; 5829dc846ba4SSatish Balay SNES snes_start; 5830dc846ba4SSatish Balay DM dm; 5831dc846ba4SSatish Balay TSType type; 583293b34091SDebojyoti Ghosh 583393b34091SDebojyoti Ghosh PetscFunctionBegin; 583493b34091SDebojyoti Ghosh PetscValidPointer(tsin, 1); 583593b34091SDebojyoti Ghosh *tsout = NULL; 583693b34091SDebojyoti Ghosh 58379566063dSJacob Faibussowitsch PetscCall(PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView)); 583893b34091SDebojyoti Ghosh 583993b34091SDebojyoti Ghosh /* General TS description */ 584093b34091SDebojyoti Ghosh t->numbermonitors = 0; 5841d0c080abSJoseph Pusztay t->monitorFrequency = 1; 584293b34091SDebojyoti Ghosh t->setupcalled = 0; 584393b34091SDebojyoti Ghosh t->ksp_its = 0; 584493b34091SDebojyoti Ghosh t->snes_its = 0; 584593b34091SDebojyoti Ghosh t->nwork = 0; 58467d51462cSStefano Zampini t->rhsjacobian.time = PETSC_MIN_REAL; 584793b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 584893b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 584993b34091SDebojyoti Ghosh 58509566063dSJacob Faibussowitsch PetscCall(TSGetSNES(tsin, &snes_start)); 58519566063dSJacob Faibussowitsch PetscCall(TSSetSNES(t, snes_start)); 5852d15a3a53SEmil Constantinescu 58539566063dSJacob Faibussowitsch PetscCall(TSGetDM(tsin, &dm)); 58549566063dSJacob Faibussowitsch PetscCall(TSSetDM(t, dm)); 585593b34091SDebojyoti Ghosh 585693b34091SDebojyoti Ghosh t->adapt = tsin->adapt; 58579566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)t->adapt)); 585893b34091SDebojyoti Ghosh 5859e7069c78SShri t->trajectory = tsin->trajectory; 58609566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)t->trajectory)); 5861e7069c78SShri 5862e7069c78SShri t->event = tsin->event; 58636b10a48eSSatish Balay if (t->event) t->event->refct++; 5864e7069c78SShri 586593b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 586693b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 5867e7069c78SShri t->ptime_prev = tsin->ptime_prev; 586893b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 586993b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 587093b34091SDebojyoti Ghosh t->steps = tsin->steps; 587193b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 587293b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 587393b34091SDebojyoti Ghosh t->atol = tsin->atol; 587493b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 587593b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 587693b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 587793b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 587893b34091SDebojyoti Ghosh 58799566063dSJacob Faibussowitsch PetscCall(TSGetType(tsin, &type)); 58809566063dSJacob Faibussowitsch PetscCall(TSSetType(t, type)); 588193b34091SDebojyoti Ghosh 588293b34091SDebojyoti Ghosh t->vec_sol = NULL; 588393b34091SDebojyoti Ghosh 588493b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 588593b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 588693b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 588793b34091SDebojyoti Ghosh 58889566063dSJacob Faibussowitsch PetscCall(PetscMemcpy(t->ops, tsin->ops, sizeof(struct _TSOps))); 588993b34091SDebojyoti Ghosh 58900d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 58910d4fed19SBarry Smith PetscInt i; 58929566063dSJacob Faibussowitsch PetscCall(PetscMalloc((10) * sizeof(void (*)(void)), &((PetscObject)t)->fortran_func_pointers)); 5893ad540459SPierre Jolivet for (i = 0; i < 10; i++) ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 58940d4fed19SBarry Smith } 589593b34091SDebojyoti Ghosh *tsout = t; 589693b34091SDebojyoti Ghosh PetscFunctionReturn(0); 589793b34091SDebojyoti Ghosh } 5898f3b1f45cSBarry Smith 5899d71ae5a4SJacob Faibussowitsch static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void *ctx, Vec x, Vec y) 5900d71ae5a4SJacob Faibussowitsch { 5901f3b1f45cSBarry Smith TS ts = (TS)ctx; 5902f3b1f45cSBarry Smith 5903f3b1f45cSBarry Smith PetscFunctionBegin; 59049566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts, 0, x, y)); 5905f3b1f45cSBarry Smith PetscFunctionReturn(0); 5906f3b1f45cSBarry Smith } 5907f3b1f45cSBarry Smith 5908f3b1f45cSBarry Smith /*@ 5909bcf0153eSBarry Smith TSRHSJacobianTest - Compares the multiply routine provided to the `MATSHELL` with differencing on the `TS` given RHS function. 5910f3b1f45cSBarry Smith 5911bcf0153eSBarry Smith Logically Collective on ts 5912f3b1f45cSBarry Smith 5913f3b1f45cSBarry Smith Input Parameters: 5914f3b1f45cSBarry Smith TS - the time stepping routine 5915f3b1f45cSBarry Smith 5916f3b1f45cSBarry Smith Output Parameter: 5917bcf0153eSBarry Smith . flg - `PETSC_TRUE` if the multiply is likely correct 5918f3b1f45cSBarry Smith 5919bcf0153eSBarry Smith Options Database Key: 5920f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator 5921f3b1f45cSBarry Smith 5922f3b1f45cSBarry Smith Level: advanced 5923f3b1f45cSBarry Smith 5924bcf0153eSBarry Smith Note: 592595452b02SPatrick Sanan This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian 5926f3b1f45cSBarry Smith 5927bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `Mat`, `MATSHELL`, `MatCreateShell()`, `MatShellGetContext()`, `MatShellGetOperation()`, `MatShellTestMultTranspose()`, `TSRHSJacobianTestTranspose()` 5928f3b1f45cSBarry Smith @*/ 5929d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRHSJacobianTest(TS ts, PetscBool *flg) 5930d71ae5a4SJacob Faibussowitsch { 5931f3b1f45cSBarry Smith Mat J, B; 5932f3b1f45cSBarry Smith TSRHSJacobian func; 5933f3b1f45cSBarry Smith void *ctx; 5934f3b1f45cSBarry Smith 5935f3b1f45cSBarry Smith PetscFunctionBegin; 59369566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts, &J, &B, &func, &ctx)); 59379566063dSJacob Faibussowitsch PetscCall((*func)(ts, 0.0, ts->vec_sol, J, B, ctx)); 59389566063dSJacob Faibussowitsch PetscCall(MatShellTestMult(J, RHSWrapperFunction_TSRHSJacobianTest, ts->vec_sol, ts, flg)); 5939f3b1f45cSBarry Smith PetscFunctionReturn(0); 5940f3b1f45cSBarry Smith } 5941f3b1f45cSBarry Smith 5942f3b1f45cSBarry Smith /*@C 5943bcf0153eSBarry Smith TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the `MATSHELL` with differencing on the `TS` given RHS function. 5944f3b1f45cSBarry Smith 5945bcf0153eSBarry Smith Logically Collective on ts 5946f3b1f45cSBarry Smith 5947f3b1f45cSBarry Smith Input Parameters: 5948f3b1f45cSBarry Smith TS - the time stepping routine 5949f3b1f45cSBarry Smith 5950f3b1f45cSBarry Smith Output Parameter: 5951bcf0153eSBarry Smith . flg - `PETSC_TRUE` if the multiply is likely correct 5952f3b1f45cSBarry Smith 5953bcf0153eSBarry Smith Options Database Key: 5954f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator 5955f3b1f45cSBarry Smith 5956bcf0153eSBarry Smith Level: advanced 5957bcf0153eSBarry Smith 595895452b02SPatrick Sanan Notes: 595995452b02SPatrick Sanan This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian 5960f3b1f45cSBarry Smith 5961bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `Mat`, `MatCreateShell()`, `MatShellGetContext()`, `MatShellGetOperation()`, `MatShellTestMultTranspose()`, `TSRHSJacobianTest()` 5962f3b1f45cSBarry Smith @*/ 5963d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRHSJacobianTestTranspose(TS ts, PetscBool *flg) 5964d71ae5a4SJacob Faibussowitsch { 5965f3b1f45cSBarry Smith Mat J, B; 5966f3b1f45cSBarry Smith void *ctx; 5967f3b1f45cSBarry Smith TSRHSJacobian func; 5968f3b1f45cSBarry Smith 5969f3b1f45cSBarry Smith PetscFunctionBegin; 59709566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts, &J, &B, &func, &ctx)); 59719566063dSJacob Faibussowitsch PetscCall((*func)(ts, 0.0, ts->vec_sol, J, B, ctx)); 59729566063dSJacob Faibussowitsch PetscCall(MatShellTestMultTranspose(J, RHSWrapperFunction_TSRHSJacobianTest, ts->vec_sol, ts, flg)); 5973f3b1f45cSBarry Smith PetscFunctionReturn(0); 5974f3b1f45cSBarry Smith } 59750fe4d17eSHong Zhang 59760fe4d17eSHong Zhang /*@ 59770fe4d17eSHong Zhang TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used. 59780fe4d17eSHong Zhang 59790fe4d17eSHong Zhang Logically collective 59800fe4d17eSHong Zhang 5981d8d19677SJose E. Roman Input Parameters: 59820fe4d17eSHong Zhang + ts - timestepping context 5983bcf0153eSBarry Smith - use_splitrhsfunction - `PETSC_TRUE` indicates that the split RHSFunction will be used 59840fe4d17eSHong Zhang 5985bcf0153eSBarry Smith Options Database Key: 59860fe4d17eSHong Zhang . -ts_use_splitrhsfunction - <true,false> 59870fe4d17eSHong Zhang 59880fe4d17eSHong Zhang Level: intermediate 59890fe4d17eSHong Zhang 5990bcf0153eSBarry Smith Note: 5991bcf0153eSBarry Smith This is only useful for multirate methods 5992bcf0153eSBarry Smith 5993bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetUseSplitRHSFunction()` 59940fe4d17eSHong Zhang @*/ 5995d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction) 5996d71ae5a4SJacob Faibussowitsch { 59970fe4d17eSHong Zhang PetscFunctionBegin; 59980fe4d17eSHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 59990fe4d17eSHong Zhang ts->use_splitrhsfunction = use_splitrhsfunction; 60000fe4d17eSHong Zhang PetscFunctionReturn(0); 60010fe4d17eSHong Zhang } 60020fe4d17eSHong Zhang 60030fe4d17eSHong Zhang /*@ 60040fe4d17eSHong Zhang TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used. 60050fe4d17eSHong Zhang 60060fe4d17eSHong Zhang Not collective 60070fe4d17eSHong Zhang 60080fe4d17eSHong Zhang Input Parameter: 60090fe4d17eSHong Zhang . ts - timestepping context 60100fe4d17eSHong Zhang 60110fe4d17eSHong Zhang Output Parameter: 6012bcf0153eSBarry Smith . use_splitrhsfunction - `PETSC_TRUE` indicates that the split RHSFunction will be used 60130fe4d17eSHong Zhang 60140fe4d17eSHong Zhang Level: intermediate 60150fe4d17eSHong Zhang 6016bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetUseSplitRHSFunction()` 60170fe4d17eSHong Zhang @*/ 6018d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction) 6019d71ae5a4SJacob Faibussowitsch { 60200fe4d17eSHong Zhang PetscFunctionBegin; 60210fe4d17eSHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 60220fe4d17eSHong Zhang *use_splitrhsfunction = ts->use_splitrhsfunction; 60230fe4d17eSHong Zhang PetscFunctionReturn(0); 60240fe4d17eSHong Zhang } 6025d60b7d5cSBarry Smith 6026d60b7d5cSBarry Smith /*@ 6027d60b7d5cSBarry Smith TSSetMatStructure - sets the relationship between the nonzero structure of the RHS Jacobian matrix to the IJacobian matrix. 6028d60b7d5cSBarry Smith 6029d60b7d5cSBarry Smith Logically Collective on ts 6030d60b7d5cSBarry Smith 6031d60b7d5cSBarry Smith Input Parameters: 6032d60b7d5cSBarry Smith + ts - the time-stepper 6033bcf0153eSBarry Smith - str - the structure (the default is `UNKNOWN_NONZERO_PATTERN`) 6034d60b7d5cSBarry Smith 6035d60b7d5cSBarry Smith Level: intermediate 6036d60b7d5cSBarry Smith 6037bcf0153eSBarry Smith Note: 6038d60b7d5cSBarry Smith When the relationship between the nonzero structures is known and supplied the solution process can be much faster 6039d60b7d5cSBarry Smith 6040bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `MatAXPY()`, `MatStructure` 6041d60b7d5cSBarry Smith @*/ 6042d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMatStructure(TS ts, MatStructure str) 6043d71ae5a4SJacob Faibussowitsch { 6044d60b7d5cSBarry Smith PetscFunctionBegin; 6045d60b7d5cSBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 6046d60b7d5cSBarry Smith ts->axpy_pattern = str; 6047d60b7d5cSBarry Smith PetscFunctionReturn(0); 6048d60b7d5cSBarry Smith } 60494a658b32SHong Zhang 60504a658b32SHong Zhang /*@ 6051bcf0153eSBarry Smith TSSetTimeSpan - sets the time span. The solution will be computed and stored for each time requested in the span 60524a658b32SHong Zhang 60534a658b32SHong Zhang Collective on ts 60544a658b32SHong Zhang 60554a658b32SHong Zhang Input Parameters: 60564a658b32SHong Zhang + ts - the time-stepper 60574a658b32SHong Zhang . n - number of the time points (>=2) 60584a658b32SHong Zhang - span_times - array of the time points. The first element and the last element are the initial time and the final time respectively. 60594a658b32SHong Zhang 6060bcf0153eSBarry Smith Options Database Key: 60614a658b32SHong Zhang . -ts_time_span <t0,...tf> - Sets the time span 60624a658b32SHong Zhang 6063bcf0153eSBarry Smith Level: intermediate 60644a658b32SHong Zhang 60654a658b32SHong Zhang Notes: 60664a658b32SHong Zhang The elements in tspan must be all increasing. They correspond to the intermediate points for time integration. 6067bcf0153eSBarry Smith `TS_EXACTFINALTIME_MATCHSTEP` must be used to make the last time step in each sub-interval match the intermediate points specified. 6068bcf0153eSBarry Smith The intermediate solutions are saved in a vector array that can be accessed with `TSGetTimeSpanSolutions()`. Thus using time span may 60694a658b32SHong Zhang pressure the memory system when using a large number of span points. 60704a658b32SHong Zhang 6071bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSGetTimeSpan()`, `TSGetTimeSpanSolutions()` 60724a658b32SHong Zhang @*/ 6073d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTimeSpan(TS ts, PetscInt n, PetscReal *span_times) 6074d71ae5a4SJacob Faibussowitsch { 60754a658b32SHong Zhang PetscFunctionBegin; 60764a658b32SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 607763a3b9bcSJacob Faibussowitsch PetscCheck(n >= 2, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Minimum time span size is 2 but %" PetscInt_FMT " is provided", n); 60784a658b32SHong Zhang if (ts->tspan && n != ts->tspan->num_span_times) { 60794a658b32SHong Zhang PetscCall(PetscFree(ts->tspan->span_times)); 60804a658b32SHong Zhang PetscCall(VecDestroyVecs(ts->tspan->num_span_times, &ts->tspan->vecs_sol)); 60814a658b32SHong Zhang PetscCall(PetscMalloc1(n, &ts->tspan->span_times)); 60824a658b32SHong Zhang } 60834a658b32SHong Zhang if (!ts->tspan) { 60844a658b32SHong Zhang TSTimeSpan tspan; 60854a658b32SHong Zhang PetscCall(PetscNew(&tspan)); 60864a658b32SHong Zhang PetscCall(PetscMalloc1(n, &tspan->span_times)); 6087e1db57b0SHong Zhang tspan->reltol = 1e-6; 6088e1db57b0SHong Zhang tspan->abstol = 10 * PETSC_MACHINE_EPSILON; 60894a658b32SHong Zhang ts->tspan = tspan; 60904a658b32SHong Zhang } 60914a658b32SHong Zhang ts->tspan->num_span_times = n; 60924a658b32SHong Zhang PetscCall(PetscArraycpy(ts->tspan->span_times, span_times, n)); 60934a658b32SHong Zhang PetscCall(TSSetTime(ts, ts->tspan->span_times[0])); 60944a658b32SHong Zhang PetscCall(TSSetMaxTime(ts, ts->tspan->span_times[n - 1])); 60954a658b32SHong Zhang PetscFunctionReturn(0); 60964a658b32SHong Zhang } 60974a658b32SHong Zhang 60984a658b32SHong Zhang /*@C 60994a658b32SHong Zhang TSGetTimeSpan - gets the time span. 61004a658b32SHong Zhang 61014a658b32SHong Zhang Not Collective 61024a658b32SHong Zhang 61034a658b32SHong Zhang Input Parameter: 61044a658b32SHong Zhang . ts - the time-stepper 61054a658b32SHong Zhang 61064a658b32SHong Zhang Output Parameters: 61074a658b32SHong Zhang + n - number of the time points (>=2) 6108bcf0153eSBarry Smith - span_times - array of the time points. The first element and the last element are the initial time and the final time respectively. 6109bcf0153eSBarry Smith The values are valid until the `TS` object is destroyed. 61104a658b32SHong Zhang 61114a658b32SHong Zhang Level: beginner 61124a658b32SHong Zhang 6113bcf0153eSBarry Smith Note: 6114bcf0153eSBarry Smith Both n and span_times can be NULL. 6115bcf0153eSBarry Smith 6116bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetTimeSpan()`, `TSGetTimeSpanSolutions()` 61174a658b32SHong Zhang @*/ 6118d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeSpan(TS ts, PetscInt *n, const PetscReal **span_times) 6119d71ae5a4SJacob Faibussowitsch { 61204a658b32SHong Zhang PetscFunctionBegin; 61214a658b32SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 61224a658b32SHong Zhang if (n) PetscValidIntPointer(n, 2); 61234a658b32SHong Zhang if (span_times) PetscValidPointer(span_times, 3); 61244a658b32SHong Zhang if (!ts->tspan) { 61254a658b32SHong Zhang if (n) *n = 0; 61264a658b32SHong Zhang if (span_times) *span_times = NULL; 61274a658b32SHong Zhang } else { 61284a658b32SHong Zhang if (n) *n = ts->tspan->num_span_times; 61294a658b32SHong Zhang if (span_times) *span_times = ts->tspan->span_times; 61304a658b32SHong Zhang } 61314a658b32SHong Zhang PetscFunctionReturn(0); 61324a658b32SHong Zhang } 61334a658b32SHong Zhang 61344a658b32SHong Zhang /*@ 61354a658b32SHong Zhang TSGetTimeSpanSolutions - Get the number of solutions and the solutions at the time points specified by the time span. 61364a658b32SHong Zhang 61374a658b32SHong Zhang Input Parameter: 6138bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 61394a658b32SHong Zhang 61404a658b32SHong Zhang Output Parameters: 61414a658b32SHong Zhang + nsol - the number of solutions 61424a658b32SHong Zhang - Sols - the solution vectors 61434a658b32SHong Zhang 6144bcf0153eSBarry Smith Level: intermediate 61454a658b32SHong Zhang 614640bd4cedSHong Zhang Notes: 614740bd4cedSHong Zhang Both nsol and Sols can be NULL. 61484a658b32SHong Zhang 6149bcf0153eSBarry 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()`. 6150bcf0153eSBarry Smith For example, manipulating the step size, especially with a reduced precision, may cause `TS` to step over certain points in the span. 6151bcf0153eSBarry Smith 6152bcf0153eSBarry Smith .seealso: [](chapter_ts), `TS`, `TSSetTimeSpan()` 61534a658b32SHong Zhang @*/ 6154d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeSpanSolutions(TS ts, PetscInt *nsol, Vec **Sols) 6155d71ae5a4SJacob Faibussowitsch { 61564a658b32SHong Zhang PetscFunctionBegin; 61574a658b32SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 61584a658b32SHong Zhang if (nsol) PetscValidIntPointer(nsol, 2); 61594a658b32SHong Zhang if (Sols) PetscValidPointer(Sols, 3); 61604a658b32SHong Zhang if (!ts->tspan) { 61614a658b32SHong Zhang if (nsol) *nsol = 0; 61624a658b32SHong Zhang if (Sols) *Sols = NULL; 61634a658b32SHong Zhang } else { 616440bd4cedSHong Zhang if (nsol) *nsol = ts->tspan->spanctr; 61654a658b32SHong Zhang if (Sols) *Sols = ts->tspan->vecs_sol; 61664a658b32SHong Zhang } 61674a658b32SHong Zhang PetscFunctionReturn(0); 61684a658b32SHong Zhang } 6169d7cfae9bSHong Zhang 6170d7cfae9bSHong Zhang /*@C 6171*7b2dc123SHong Zhang TSPruneIJacobianColor - Remove nondiagonal zeros in the Jacobian matrix and update the `MatMFFD` coloring information. 6172d7cfae9bSHong Zhang 6173d7cfae9bSHong Zhang Collective on TS 6174d7cfae9bSHong Zhang 6175d7cfae9bSHong Zhang Input Parameters: 6176d7cfae9bSHong Zhang + ts - the TS context 6177d7cfae9bSHong Zhang . J - Jacobian matrix (not altered in this routine) 6178d7cfae9bSHong Zhang - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 6179d7cfae9bSHong Zhang 6180d7cfae9bSHong Zhang Level: intermediate 6181d7cfae9bSHong Zhang 6182d7cfae9bSHong Zhang Notes: 6183*7b2dc123SHong Zhang This function improves the `MatMFFD` coloring performance when the Jacobian matrix is overallocated or contains 6184d7cfae9bSHong Zhang many constant zeros entries, which is typically the case when the matrix is generated by a DM 6185d7cfae9bSHong Zhang and multiple fields are involved. 6186d7cfae9bSHong Zhang 6187d7cfae9bSHong Zhang Users need to make sure that the Jacobian matrix is properly filled to reflect the sparsity 6188*7b2dc123SHong Zhang structure. For `MatMFFD` coloring, the values of nonzero entries are not important. So one can 6189*7b2dc123SHong Zhang usually call `TSComputeIJacobian()` with randomized input vectors to generate a dummy Jacobian. 6190*7b2dc123SHong Zhang `TSComputeIJacobian()` should be called before `TSSolve()` but after `TSSetUp()`. 6191d7cfae9bSHong Zhang 6192d7cfae9bSHong Zhang .seealso: `TSComputeIJacobianDefaultColor()`, `MatEliminateZeros()`, `MatFDColoringCreate()`, `MatFDColoringSetFunction()` 6193d7cfae9bSHong Zhang @*/ 6194d7cfae9bSHong Zhang PetscErrorCode TSPruneIJacobianColor(TS ts, Mat J, Mat B) 6195d7cfae9bSHong Zhang { 6196d7cfae9bSHong Zhang MatColoring mc = NULL; 6197d7cfae9bSHong Zhang ISColoring iscoloring = NULL; 6198d7cfae9bSHong Zhang MatFDColoring matfdcoloring = NULL; 6199d7cfae9bSHong Zhang 6200d7cfae9bSHong Zhang PetscFunctionBegin; 6201d7cfae9bSHong Zhang /* Generate new coloring after eliminating zeros in the matrix */ 6202d7cfae9bSHong Zhang PetscCall(MatEliminateZeros(B)); 6203d7cfae9bSHong Zhang PetscCall(MatColoringCreate(B, &mc)); 6204d7cfae9bSHong Zhang PetscCall(MatColoringSetDistance(mc, 2)); 6205d7cfae9bSHong Zhang PetscCall(MatColoringSetType(mc, MATCOLORINGSL)); 6206d7cfae9bSHong Zhang PetscCall(MatColoringSetFromOptions(mc)); 6207d7cfae9bSHong Zhang PetscCall(MatColoringApply(mc, &iscoloring)); 6208d7cfae9bSHong Zhang PetscCall(MatColoringDestroy(&mc)); 6209d7cfae9bSHong Zhang /* Replace the old coloring with the new one */ 6210d7cfae9bSHong Zhang PetscCall(MatFDColoringCreate(B, iscoloring, &matfdcoloring)); 6211d7cfae9bSHong Zhang PetscCall(MatFDColoringSetFunction(matfdcoloring, (PetscErrorCode(*)(void))SNESTSFormFunction, (void *)ts)); 6212d7cfae9bSHong Zhang PetscCall(MatFDColoringSetFromOptions(matfdcoloring)); 6213d7cfae9bSHong Zhang PetscCall(MatFDColoringSetUp(B, iscoloring, matfdcoloring)); 6214d7cfae9bSHong Zhang PetscCall(PetscObjectCompose((PetscObject)B, "TSMatFDColoring", (PetscObject)matfdcoloring)); 6215d7cfae9bSHong Zhang PetscCall(PetscObjectDereference((PetscObject)matfdcoloring)); 6216d7cfae9bSHong Zhang PetscCall(ISColoringDestroy(&iscoloring)); 6217d7cfae9bSHong Zhang PetscFunctionReturn(0); 6218d7cfae9bSHong Zhang } 6219