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 81c167fc2SEmil Constantinescu #define SkipSmallValue(a,b,tol) if (PetscAbsScalar(a)< tol || PetscAbsScalar(b)< tol) continue; 91c167fc2SEmil Constantinescu 10d5ba7fb7SMatthew Knepley /* Logging support */ 11d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 12a05bf03eSHong Zhang PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 13d405a339SMatthew Knepley 14c793f718SLisandro Dalcin const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",NULL}; 1549354f04SShri Abhyankar 162ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type) 172ffb9264SLisandro Dalcin { 182ffb9264SLisandro Dalcin PetscFunctionBegin; 19b92453a8SLisandro Dalcin PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1); 20b92453a8SLisandro Dalcin PetscValidCharPointer(default_type,2); 212ffb9264SLisandro Dalcin if (!((PetscObject)adapt)->type_name) { 229566063dSJacob Faibussowitsch PetscCall(TSAdaptSetType(adapt,default_type)); 232ffb9264SLisandro Dalcin } 242ffb9264SLisandro Dalcin PetscFunctionReturn(0); 252ffb9264SLisandro Dalcin } 262ffb9264SLisandro Dalcin 27bdad233fSMatthew Knepley /*@ 28bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 29bdad233fSMatthew Knepley 30bdad233fSMatthew Knepley Collective on TS 31bdad233fSMatthew Knepley 32bdad233fSMatthew Knepley Input Parameter: 33bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 34bdad233fSMatthew Knepley 35bdad233fSMatthew Knepley Options Database Keys: 36d2567f34SHong Zhang + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE, TSBSYMP, TSIRK 37ef222394SBarry Smith . -ts_save_trajectory - checkpoint the solution at each time-step 38ef85077eSLisandro Dalcin . -ts_max_time <time> - maximum time to compute to 394a658b32SHong Zhang . -ts_time_span <t0,...tf> - sets the time span, solutions are computed and stored for each indicated time 40ef85077eSLisandro Dalcin . -ts_max_steps <steps> - maximum number of time-steps to take 41ef85077eSLisandro Dalcin . -ts_init_time <time> - initial time to start computation 424dc72f7fSBarry Smith . -ts_final_time <time> - final time to compute to (deprecated: use -ts_max_time) 433e4cdcaaSBarry Smith . -ts_dt <dt> - initial time step 441628793fSSatish Balay . -ts_exact_final_time <stepover,interpolate,matchstep> - whether to stop at the exact given final time and how to compute the solution at that time 45a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 46a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 47a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 48a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 4967b8a455SSatish Balay . -ts_atol <atol> - Absolute tolerance for local truncation error 50f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function 51f3b1f45cSBarry 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 5267b8a455SSatish Balay . -ts_adjoint_solve <yes,no> - After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory) 53847ff0e1SMatthew G. Knepley . -ts_fd_color - Use finite differences with coloring to compute IJacobian 54bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 55aee7a9fbSMatthew G. Knepley . -ts_monitor_cancel - Cancel all monitors 56de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 57de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 587cf37e64SBarry Smith . -ts_monitor_error - Monitors norm of error 596934998bSLisandro Dalcin . -ts_monitor_lg_timestep - Monitor timestep size graphically 608b668821SLisandro Dalcin . -ts_monitor_lg_timestep_log - Monitor log timestep size graphically 61de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 62de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 63de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 64de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 653e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 663e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 67fde5950dSBarry Smith . -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep 6863a3b9bcSJacob Faibussowitsch . -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03" PetscInt_FMT ".vts (filename-%%03" PetscInt_FMT ".vtu) 699e336e28SPatrick Sanan - -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 7053ea634cSHong Zhang 713d5a8a6aSBarry Smith Notes: 723d5a8a6aSBarry Smith See SNESSetFromOptions() and KSPSetFromOptions() for how to control the nonlinear and linear solves used by the time-stepper. 733d5a8a6aSBarry Smith 743d5a8a6aSBarry Smith Certain SNES options get reset for each new nonlinear solver, for example -snes_lag_jacobian <its> and -snes_lag_preconditioner <its>, in order 753d5a8a6aSBarry Smith to retain them over the multiple nonlinear solves that TS uses you mush also provide -snes_lag_jacobian_persists true and 763d5a8a6aSBarry Smith -snes_lag_preconditioner_persists true 773d5a8a6aSBarry Smith 789e336e28SPatrick Sanan Developer Note: 799e336e28SPatrick Sanan We should unify all the -ts_monitor options in the way that -xxx_view has been unified 80bdad233fSMatthew Knepley 81bdad233fSMatthew Knepley Level: beginner 82bdad233fSMatthew Knepley 83db781477SPatrick Sanan .seealso: `TSGetType()` 84bdad233fSMatthew Knepley @*/ 857087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 86bdad233fSMatthew Knepley { 87bc952696SBarry Smith PetscBool opt,flg,tflg; 88eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 894a658b32SHong Zhang PetscReal time_step,tspan[100]; 904a658b32SHong Zhang PetscInt nt = PETSC_STATIC_ARRAY_LENGTH(tspan); 9149354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 92d1212d36SBarry Smith char dir[16]; 93cd11d68dSLisandro Dalcin TSIFunction ifun; 946991f827SBarry Smith const char *defaultType; 956991f827SBarry Smith char typeName[256]; 96bdad233fSMatthew Knepley 97bdad233fSMatthew Knepley PetscFunctionBegin; 980700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 996991f827SBarry Smith 1009566063dSJacob Faibussowitsch PetscCall(TSRegisterAll()); 1019566063dSJacob Faibussowitsch PetscCall(TSGetIFunction(ts,NULL,&ifun,NULL)); 102cd11d68dSLisandro Dalcin 103d0609cedSBarry Smith PetscObjectOptionsBegin((PetscObject)ts); 1041ef27442SStefano Zampini if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 1051ef27442SStefano Zampini else defaultType = ifun ? TSBEULER : TSEULER; 1069566063dSJacob Faibussowitsch PetscCall(PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt)); 1076991f827SBarry Smith if (opt) { 1089566063dSJacob Faibussowitsch PetscCall(TSSetType(ts,typeName)); 1096991f827SBarry Smith } else { 1109566063dSJacob Faibussowitsch PetscCall(TSSetType(ts,defaultType)); 1116991f827SBarry Smith } 112bdad233fSMatthew Knepley 113bdad233fSMatthew Knepley /* Handle generic TS options */ 1149566063dSJacob Faibussowitsch PetscCall(PetscOptionsDeprecated("-ts_final_time","-ts_max_time","3.10",NULL)); 1159566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL)); 1164a658b32SHong Zhang PetscCall(PetscOptionsRealArray("-ts_time_span","Time span","TSSetTimeSpan",tspan,&nt,&flg)); 1174a658b32SHong Zhang if (flg) PetscCall(TSSetTimeSpan(ts,nt,tspan)); 1189566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL)); 1199566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL)); 1209566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg)); 1219566063dSJacob Faibussowitsch if (flg) PetscCall(TSSetTimeStep(ts,time_step)); 1229566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum("-ts_exact_final_time","Option for handling of final time step","TSSetExactFinalTime",TSExactFinalTimeOptions,(PetscEnum)ts->exact_final_time,(PetscEnum*)&eftopt,&flg)); 1239566063dSJacob Faibussowitsch if (flg) PetscCall(TSSetExactFinalTime(ts,eftopt)); 1249566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,NULL)); 1259566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL)); 1269566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL)); 1279566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL)); 1289566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL)); 129bdad233fSMatthew Knepley 1309566063dSJacob 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)); 1319566063dSJacob 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)); 1329566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_use_splitrhsfunction","Use the split RHS function for multirate solvers ","TSSetUseSplitRHSFunction",ts->use_splitrhsfunction,&ts->use_splitrhsfunction,NULL)); 13356f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 13456f85f32SBarry Smith { 13556f85f32SBarry Smith PetscBool set; 13656f85f32SBarry Smith flg = PETSC_FALSE; 1379566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set)); 13856f85f32SBarry Smith if (set) { 1399566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsSetBlock((PetscObject)ts,flg)); 14056f85f32SBarry Smith } 14156f85f32SBarry Smith } 14256f85f32SBarry Smith #endif 14356f85f32SBarry Smith 144bdad233fSMatthew Knepley /* Monitor options */ 1459566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_frequency", "Number of time steps between monitor output", "TSMonitorSetFrequency", ts->monitorFrequency, &ts->monitorFrequency, NULL)); 1469566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL)); 1479566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts,"-ts_monitor_extreme","Monitor extreme values of the solution","TSMonitorExtreme",TSMonitorExtreme,NULL)); 1489566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL)); 1499566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts,"-ts_dmswarm_monitor_moments","Monitor moments of particle distribution","TSDMSwarmMonitorMoments",TSDMSwarmMonitorMoments,NULL)); 150fde5950dSBarry Smith 1519566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",NULL,monfilename,sizeof(monfilename),&flg)); 1529566063dSJacob Faibussowitsch if (flg) PetscCall(PetscPythonMonitorSet((PetscObject)ts,monfilename)); 1535180491cSLisandro Dalcin 1549566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt)); 155b3603a34SBarry Smith if (opt) { 1563923b477SBarry Smith PetscInt howoften = 1; 157e669de00SBarry Smith DM dm; 158e669de00SBarry Smith PetscBool net; 159b3603a34SBarry Smith 1609566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL)); 1619566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 1629566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)dm,DMNETWORK,&net)); 163e669de00SBarry Smith if (net) { 164e669de00SBarry Smith TSMonitorLGCtxNetwork ctx; 1659566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxNetworkCreate(ts,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx)); 1669566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorLGCtxNetworkSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxNetworkDestroy)); 1679566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_lg_solution_semilogy","Plot the solution with a semi-log axis","",ctx->semilogy,&ctx->semilogy,NULL)); 168e669de00SBarry Smith } else { 169e669de00SBarry Smith TSMonitorLGCtx ctx; 1709566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx)); 1719566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy)); 172bdad233fSMatthew Knepley } 173e669de00SBarry Smith } 1746ba87a44SLisandro Dalcin 1759566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt)); 176ef20d060SBarry Smith if (opt) { 1770b039ecaSBarry Smith TSMonitorLGCtx ctx; 1783923b477SBarry Smith PetscInt howoften = 1; 179ef20d060SBarry Smith 1809566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL)); 1819566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx)); 1829566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy)); 183ef20d060SBarry Smith } 1849566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts,"-ts_monitor_error","View the error at each timestep","TSMonitorError",TSMonitorError,NULL)); 1857cf37e64SBarry Smith 1869566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt)); 1876934998bSLisandro Dalcin if (opt) { 1886934998bSLisandro Dalcin TSMonitorLGCtx ctx; 1896934998bSLisandro Dalcin PetscInt howoften = 1; 1906934998bSLisandro Dalcin 1919566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL)); 1929566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx)); 1939566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy)); 1946934998bSLisandro Dalcin } 1959566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt)); 1968b668821SLisandro Dalcin if (opt) { 1978b668821SLisandro Dalcin TSMonitorLGCtx ctx; 1988b668821SLisandro Dalcin PetscInt howoften = 1; 1998b668821SLisandro Dalcin 2009566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL)); 2019566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx)); 2029566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy)); 2038b668821SLisandro Dalcin ctx->semilogy = PETSC_TRUE; 2048b668821SLisandro Dalcin } 2058b668821SLisandro Dalcin 2069566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt)); 207201da799SBarry Smith if (opt) { 208201da799SBarry Smith TSMonitorLGCtx ctx; 209201da799SBarry Smith PetscInt howoften = 1; 210201da799SBarry Smith 2119566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL)); 2129566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx)); 2139566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy)); 214201da799SBarry Smith } 2159566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt)); 216201da799SBarry Smith if (opt) { 217201da799SBarry Smith TSMonitorLGCtx ctx; 218201da799SBarry Smith PetscInt howoften = 1; 219201da799SBarry Smith 2209566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL)); 2219566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx)); 2229566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy)); 223201da799SBarry Smith } 2249566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt)); 2258189c53fSBarry Smith if (opt) { 2268189c53fSBarry Smith TSMonitorSPEigCtx ctx; 2278189c53fSBarry Smith PetscInt howoften = 1; 2288189c53fSBarry Smith 2299566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL)); 2309566063dSJacob Faibussowitsch PetscCall(TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx)); 2319566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy)); 2328189c53fSBarry Smith } 2339566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_sp_swarm","Display particle phase from the DMSwarm","TSMonitorSPSwarm",&opt)); 2341b575b74SJoseph Pusztay if (opt) { 2351b575b74SJoseph Pusztay TSMonitorSPCtx ctx; 236d7462660SMatthew Knepley PetscInt howoften = 1, retain = 0; 2376a5217c0SMatthew G. Knepley PetscBool phase = PETSC_TRUE, create = PETSC_TRUE; 238d7462660SMatthew Knepley 2396a5217c0SMatthew G. Knepley for (PetscInt i = 0; i < ts->numbermonitors; ++i) if (ts->monitor[i] == TSMonitorSPSwarmSolution) {create = PETSC_FALSE;break;} 2406a5217c0SMatthew G. Knepley if (create) { 2419566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm","Display particles phase from the DMSwarm", "TSMonitorSPSwarm", howoften, &howoften, NULL)); 2429566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm_retain", "Retain n points plotted to show trajectory, -1 for all points", "TSMonitorSPSwarm", retain, &retain, NULL)); 2439566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_sp_swarm_phase", "Plot in phase space rather than coordinate space", "TSMonitorSPSwarm", phase, &phase, NULL)); 2449566063dSJacob Faibussowitsch PetscCall(TSMonitorSPCtxCreate(PetscObjectComm((PetscObject) ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, retain, phase, &ctx)); 2459566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode (*)(void**))TSMonitorSPCtxDestroy)); 2461b575b74SJoseph Pusztay } 2476a5217c0SMatthew G. Knepley } 248ef20d060SBarry Smith opt = PETSC_FALSE; 2499566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt)); 250a7cc72afSBarry Smith if (opt) { 25183a4ac43SBarry Smith TSMonitorDrawCtx ctx; 25283a4ac43SBarry Smith PetscInt howoften = 1; 253a80ad3e0SBarry Smith 2549566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL)); 2559566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx)); 2569566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy)); 257bdad233fSMatthew Knepley } 258fb1732b5SBarry Smith opt = PETSC_FALSE; 2599566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt)); 2602d5ee99bSBarry Smith if (opt) { 2612d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2622d5ee99bSBarry Smith PetscReal bounds[4]; 2632d5ee99bSBarry Smith PetscInt n = 4; 2642d5ee99bSBarry Smith PetscDraw draw; 2656934998bSLisandro Dalcin PetscDrawAxis axis; 2662d5ee99bSBarry Smith 2679566063dSJacob Faibussowitsch PetscCall(PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL)); 2683c633725SBarry Smith PetscCheck(n == 4,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 2699566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx)); 2709566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(ctx->viewer,0,&draw)); 2719566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis)); 2729566063dSJacob Faibussowitsch PetscCall(PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3])); 2739566063dSJacob Faibussowitsch PetscCall(PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2")); 2749566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy)); 2752d5ee99bSBarry Smith } 2762d5ee99bSBarry Smith opt = PETSC_FALSE; 2779566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt)); 2783a471f94SBarry Smith if (opt) { 27983a4ac43SBarry Smith TSMonitorDrawCtx ctx; 28083a4ac43SBarry Smith PetscInt howoften = 1; 2813a471f94SBarry Smith 2829566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL)); 2839566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx)); 2849566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy)); 2853a471f94SBarry Smith } 2860ed3bfb6SBarry Smith opt = PETSC_FALSE; 2879566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt)); 2880ed3bfb6SBarry Smith if (opt) { 2890ed3bfb6SBarry Smith TSMonitorDrawCtx ctx; 2900ed3bfb6SBarry Smith PetscInt howoften = 1; 2910ed3bfb6SBarry Smith 2929566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL)); 2939566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx)); 2949566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy)); 2950ed3bfb6SBarry Smith } 296fde5950dSBarry Smith 297ed81e22dSJed Brown opt = PETSC_FALSE; 29863a3b9bcSJacob 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)); 299ed81e22dSJed Brown if (flg) { 300ed81e22dSJed Brown const char *ptr,*ptr2; 301ed81e22dSJed Brown char *filetemplate; 30263a3b9bcSJacob 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"); 303ed81e22dSJed Brown /* Do some cursory validation of the input. */ 3049566063dSJacob Faibussowitsch PetscCall(PetscStrstr(monfilename,"%",(char**)&ptr)); 30563a3b9bcSJacob Faibussowitsch PetscCheck(ptr,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03" PetscInt_FMT ".vts"); 306ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 3079566063dSJacob Faibussowitsch PetscCall(PetscStrchr("DdiouxX",*ptr,(char**)&ptr2)); 30863a3b9bcSJacob 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"); 309ed81e22dSJed Brown if (ptr2) break; 310ed81e22dSJed Brown } 3119566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(monfilename,&filetemplate)); 3129566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy)); 313ed81e22dSJed Brown } 314bdad233fSMatthew Knepley 3159566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,sizeof(dir),&flg)); 316d1212d36SBarry Smith if (flg) { 317d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 318d1212d36SBarry Smith int ray = 0; 3193ee9839eSMatthew G. Knepley DMDirection ddir; 320d1212d36SBarry Smith DM da; 321d1212d36SBarry Smith PetscMPIInt rank; 322d1212d36SBarry Smith 3233c633725SBarry Smith PetscCheck(dir[1] == '=',PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 3243ee9839eSMatthew G. Knepley if (dir[0] == 'x') ddir = DM_X; 3253ee9839eSMatthew G. Knepley else if (dir[0] == 'y') ddir = DM_Y; 32698921bdaSJacob Faibussowitsch else SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 327d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 328d1212d36SBarry Smith 3299566063dSJacob Faibussowitsch PetscCall(PetscInfo(((PetscObject)ts),"Displaying DMDA ray %c = %d\n",dir[0],ray)); 3309566063dSJacob Faibussowitsch PetscCall(PetscNew(&rayctx)); 3319566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&da)); 3329566063dSJacob Faibussowitsch PetscCall(DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter)); 3339566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank)); 334dd400576SPatrick Sanan if (rank == 0) { 3359566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawOpen(PETSC_COMM_SELF,NULL,NULL,0,0,600,300,&rayctx->viewer)); 336d1212d36SBarry Smith } 33751b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 3389566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy)); 339d1212d36SBarry Smith } 3409566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,sizeof(dir),&flg)); 34151b4a12fSMatthew G. Knepley if (flg) { 34251b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 34351b4a12fSMatthew G. Knepley int ray = 0; 3443ee9839eSMatthew G. Knepley DMDirection ddir; 34551b4a12fSMatthew G. Knepley DM da; 34651b4a12fSMatthew G. Knepley PetscInt howoften = 1; 347d1212d36SBarry Smith 3483c633725SBarry Smith PetscCheck(dir[1] == '=',PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 3493ee9839eSMatthew G. Knepley if (dir[0] == 'x') ddir = DM_X; 3503ee9839eSMatthew G. Knepley else if (dir[0] == 'y') ddir = DM_Y; 35198921bdaSJacob Faibussowitsch else SETERRQ(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 35251b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 3531c3436cfSJed Brown 3549566063dSJacob Faibussowitsch PetscCall(PetscInfo(((PetscObject) ts),"Displaying LG DMDA ray %c = %d\n", dir[0], ray)); 3559566063dSJacob Faibussowitsch PetscCall(PetscNew(&rayctx)); 3569566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &da)); 3579566063dSJacob Faibussowitsch PetscCall(DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter)); 3589566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx)); 3599566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy)); 36051b4a12fSMatthew G. Knepley } 361a7a1495cSBarry Smith 3629566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt)); 363b3d3934dSBarry Smith if (opt) { 364b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 365b3d3934dSBarry Smith 3669566063dSJacob Faibussowitsch PetscCall(TSMonitorEnvelopeCtxCreate(ts,&ctx)); 3679566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy)); 368b3d3934dSBarry Smith } 369aee7a9fbSMatthew G. Knepley flg = PETSC_FALSE; 3709566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_cancel","Remove all monitors","TSMonitorCancel",flg,&flg,&opt)); 3719566063dSJacob Faibussowitsch if (opt && flg) PetscCall(TSMonitorCancel(ts)); 372b3d3934dSBarry Smith 373847ff0e1SMatthew G. Knepley flg = PETSC_FALSE; 3749566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL)); 375847ff0e1SMatthew G. Knepley if (flg) { 376847ff0e1SMatthew G. Knepley DM dm; 377847ff0e1SMatthew G. Knepley DMTS tdm; 378847ff0e1SMatthew G. Knepley 3799566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 3809566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm, &tdm)); 381847ff0e1SMatthew G. Knepley tdm->ijacobianctx = NULL; 3829566063dSJacob Faibussowitsch PetscCall(TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, NULL)); 3839566063dSJacob Faibussowitsch PetscCall(PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n")); 384847ff0e1SMatthew G. Knepley } 385847ff0e1SMatthew G. Knepley 386d763cef2SBarry Smith /* Handle specific TS options */ 387d763cef2SBarry Smith if (ts->ops->setfromoptions) { 3889566063dSJacob Faibussowitsch PetscCall((*ts->ops->setfromoptions)(PetscOptionsObject,ts)); 389d763cef2SBarry Smith } 390fbc52257SHong Zhang 391a7bdc993SLisandro Dalcin /* Handle TSAdapt options */ 3929566063dSJacob Faibussowitsch PetscCall(TSGetAdapt(ts,&ts->adapt)); 3939566063dSJacob Faibussowitsch PetscCall(TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type)); 3949566063dSJacob Faibussowitsch PetscCall(TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt)); 395a7bdc993SLisandro Dalcin 39668bece0bSHong Zhang /* TS trajectory must be set after TS, since it may use some TS options above */ 3974f122a70SLisandro Dalcin tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE; 3989566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL)); 39968bece0bSHong Zhang if (tflg) { 4009566063dSJacob Faibussowitsch PetscCall(TSSetSaveTrajectory(ts)); 40168bece0bSHong Zhang } 402a05bf03eSHong Zhang 4039566063dSJacob Faibussowitsch PetscCall(TSAdjointSetFromOptions(PetscOptionsObject,ts)); 404d763cef2SBarry Smith 405d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 4069566063dSJacob Faibussowitsch PetscCall(PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts)); 407d0609cedSBarry Smith PetscOptionsEnd(); 408d763cef2SBarry Smith 4094f122a70SLisandro Dalcin if (ts->trajectory) { 4109566063dSJacob Faibussowitsch PetscCall(TSTrajectorySetFromOptions(ts->trajectory,ts)); 411d763cef2SBarry Smith } 41268bece0bSHong Zhang 4131ef27442SStefano Zampini /* why do we have to do this here and not during TSSetUp? */ 4149566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&ts->snes)); 4151ef27442SStefano Zampini if (ts->problem_type == TS_LINEAR) { 4169566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompareAny((PetscObject)ts->snes,&flg,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"")); 4179566063dSJacob Faibussowitsch if (!flg) PetscCall(SNESSetType(ts->snes,SNESKSPONLY)); 4181ef27442SStefano Zampini } 4199566063dSJacob Faibussowitsch PetscCall(SNESSetFromOptions(ts->snes)); 420d763cef2SBarry Smith PetscFunctionReturn(0); 421d763cef2SBarry Smith } 422d763cef2SBarry Smith 423d2daff3dSHong Zhang /*@ 42478fbdcc8SBarry Smith TSGetTrajectory - Gets the trajectory from a TS if it exists 42578fbdcc8SBarry Smith 42678fbdcc8SBarry Smith Collective on TS 42778fbdcc8SBarry Smith 42878fbdcc8SBarry Smith Input Parameters: 42978fbdcc8SBarry Smith . ts - the TS context obtained from TSCreate() 43078fbdcc8SBarry Smith 4317a7aea1fSJed Brown Output Parameters: 43278fbdcc8SBarry Smith . tr - the TSTrajectory object, if it exists 43378fbdcc8SBarry Smith 43478fbdcc8SBarry Smith Note: This routine should be called after all TS options have been set 43578fbdcc8SBarry Smith 43678fbdcc8SBarry Smith Level: advanced 43778fbdcc8SBarry Smith 438db781477SPatrick Sanan .seealso: `TSGetTrajectory()`, `TSAdjointSolve()`, `TSTrajectory`, `TSTrajectoryCreate()` 43978fbdcc8SBarry Smith 44078fbdcc8SBarry Smith @*/ 44178fbdcc8SBarry Smith PetscErrorCode TSGetTrajectory(TS ts,TSTrajectory *tr) 44278fbdcc8SBarry Smith { 44378fbdcc8SBarry Smith PetscFunctionBegin; 44478fbdcc8SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 44578fbdcc8SBarry Smith *tr = ts->trajectory; 44678fbdcc8SBarry Smith PetscFunctionReturn(0); 44778fbdcc8SBarry Smith } 44878fbdcc8SBarry Smith 44978fbdcc8SBarry Smith /*@ 450bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 451d2daff3dSHong Zhang 452d2daff3dSHong Zhang Collective on TS 453d2daff3dSHong Zhang 454f899ff85SJose E. Roman Input Parameter: 455bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 456bc952696SBarry Smith 45778fbdcc8SBarry Smith Options Database: 45878fbdcc8SBarry Smith + -ts_save_trajectory - saves the trajectory to a file 45967b8a455SSatish Balay - -ts_trajectory_type type - set trajectory type 46078fbdcc8SBarry Smith 46168bece0bSHong Zhang Note: This routine should be called after all TS options have been set 462d2daff3dSHong Zhang 463c3a89c15SBarry Smith The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and 46478fbdcc8SBarry Smith MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m 46578fbdcc8SBarry Smith 466d2daff3dSHong Zhang Level: intermediate 467d2daff3dSHong Zhang 468db781477SPatrick Sanan .seealso: `TSGetTrajectory()`, `TSAdjointSolve()` 469d2daff3dSHong Zhang 470d2daff3dSHong Zhang @*/ 471bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 472d2daff3dSHong Zhang { 473d2daff3dSHong Zhang PetscFunctionBegin; 474d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 47563a3b9bcSJacob Faibussowitsch if (!ts->trajectory) PetscCall(TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory)); 476d2daff3dSHong Zhang PetscFunctionReturn(0); 477d2daff3dSHong Zhang } 478d2daff3dSHong Zhang 479a7a1495cSBarry Smith /*@ 4802d29f1f2SStefano Zampini TSResetTrajectory - Destroys and recreates the internal TSTrajectory object 4812d29f1f2SStefano Zampini 4822d29f1f2SStefano Zampini Collective on TS 4832d29f1f2SStefano Zampini 4842d29f1f2SStefano Zampini Input Parameters: 4852d29f1f2SStefano Zampini . ts - the TS context obtained from TSCreate() 4862d29f1f2SStefano Zampini 4872d29f1f2SStefano Zampini Level: intermediate 4882d29f1f2SStefano Zampini 489db781477SPatrick Sanan .seealso: `TSGetTrajectory()`, `TSAdjointSolve()`, `TSRemoveTrajectory()` 4902d29f1f2SStefano Zampini 4912d29f1f2SStefano Zampini @*/ 4922d29f1f2SStefano Zampini PetscErrorCode TSResetTrajectory(TS ts) 4932d29f1f2SStefano Zampini { 4942d29f1f2SStefano Zampini PetscFunctionBegin; 4952d29f1f2SStefano Zampini PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4962d29f1f2SStefano Zampini if (ts->trajectory) { 4979566063dSJacob Faibussowitsch PetscCall(TSTrajectoryDestroy(&ts->trajectory)); 4989566063dSJacob Faibussowitsch PetscCall(TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory)); 4992d29f1f2SStefano Zampini } 5002d29f1f2SStefano Zampini PetscFunctionReturn(0); 5012d29f1f2SStefano Zampini } 5022d29f1f2SStefano Zampini 5032d29f1f2SStefano Zampini /*@ 50467a3cfb0SHong Zhang TSRemoveTrajectory - Destroys and removes the internal TSTrajectory object from TS 50567a3cfb0SHong Zhang 50667a3cfb0SHong Zhang Collective on TS 50767a3cfb0SHong Zhang 50867a3cfb0SHong Zhang Input Parameters: 50967a3cfb0SHong Zhang . ts - the TS context obtained from TSCreate() 51067a3cfb0SHong Zhang 51167a3cfb0SHong Zhang Level: intermediate 51267a3cfb0SHong Zhang 513db781477SPatrick Sanan .seealso: `TSResetTrajectory()`, `TSAdjointSolve()` 51467a3cfb0SHong Zhang 51567a3cfb0SHong Zhang @*/ 51667a3cfb0SHong Zhang PetscErrorCode TSRemoveTrajectory(TS ts) 51767a3cfb0SHong Zhang { 51867a3cfb0SHong Zhang PetscFunctionBegin; 51967a3cfb0SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 52067a3cfb0SHong Zhang if (ts->trajectory) { 5219566063dSJacob Faibussowitsch PetscCall(TSTrajectoryDestroy(&ts->trajectory)); 52267a3cfb0SHong Zhang } 52367a3cfb0SHong Zhang PetscFunctionReturn(0); 52467a3cfb0SHong Zhang } 52567a3cfb0SHong Zhang 52667a3cfb0SHong Zhang /*@ 527a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 528a7a1495cSBarry Smith set with TSSetRHSJacobian(). 529a7a1495cSBarry Smith 530d083f849SBarry Smith Collective on TS 531a7a1495cSBarry Smith 532a7a1495cSBarry Smith Input Parameters: 533316643e7SJed Brown + ts - the TS context 534a7a1495cSBarry Smith . t - current timestep 5350910c330SBarry Smith - U - input vector 536a7a1495cSBarry Smith 537a7a1495cSBarry Smith Output Parameters: 538a7a1495cSBarry Smith + A - Jacobian matrix 5396b867d5aSJose E. Roman - B - optional preconditioning matrix 540a7a1495cSBarry Smith 541a7a1495cSBarry Smith Notes: 542a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 543a7a1495cSBarry Smith is used internally within the nonlinear solvers. 544a7a1495cSBarry Smith 545a7a1495cSBarry Smith Level: developer 546a7a1495cSBarry Smith 547db781477SPatrick Sanan .seealso: `TSSetRHSJacobian()`, `KSPSetOperators()` 548a7a1495cSBarry Smith @*/ 549d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 550a7a1495cSBarry Smith { 551270bf2e7SJed Brown PetscObjectState Ustate; 5526c1e1eecSBarry Smith PetscObjectId Uid; 55324989b8cSPeter Brune DM dm; 554942e3340SBarry Smith DMTS tsdm; 55524989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 55624989b8cSPeter Brune void *ctx; 557b2df71adSDebojyoti Ghosh TSRHSFunction rhsfunction; 558a7a1495cSBarry Smith 559a7a1495cSBarry Smith PetscFunctionBegin; 5600700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5610910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5620910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 5639566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 5649566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm,&tsdm)); 5659566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,NULL)); 5669566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx)); 5679566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&Ustate)); 5689566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U,&Uid)); 569971015bcSStefano Zampini 5702663174eSHong Zhang if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) PetscFunctionReturn(0); 571d90be118SSean Farley 57263a3b9bcSJacob 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); 57324989b8cSPeter Brune if (rhsjacobianfunc) { 5749566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_JacobianEval,ts,U,A,B)); 575a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 5769566063dSJacob Faibussowitsch PetscCall((*rhsjacobianfunc)(ts,t,U,A,B,ctx)); 577a7a1495cSBarry Smith PetscStackPop; 578a6ab3590SBarry Smith ts->rhsjacs++; 5799566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_JacobianEval,ts,U,A,B)); 580ef66eb69SBarry Smith } else { 5819566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(A)); 5829566063dSJacob Faibussowitsch if (B && A != B) PetscCall(MatZeroEntries(B)); 583ef66eb69SBarry Smith } 5840e4ef248SJed Brown ts->rhsjacobian.time = t; 585971015bcSStefano Zampini ts->rhsjacobian.shift = 0; 586971015bcSStefano Zampini ts->rhsjacobian.scale = 1.; 5879566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid)); 5889566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate)); 589a7a1495cSBarry Smith PetscFunctionReturn(0); 590a7a1495cSBarry Smith } 591a7a1495cSBarry Smith 592316643e7SJed Brown /*@ 593d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 594d763cef2SBarry Smith 595d083f849SBarry Smith Collective on TS 596316643e7SJed Brown 597316643e7SJed Brown Input Parameters: 598316643e7SJed Brown + ts - the TS context 599316643e7SJed Brown . t - current time 6000910c330SBarry Smith - U - state vector 601316643e7SJed Brown 602316643e7SJed Brown Output Parameter: 603316643e7SJed Brown . y - right hand side 604316643e7SJed Brown 605316643e7SJed Brown Note: 606316643e7SJed Brown Most users should not need to explicitly call this routine, as it 607316643e7SJed Brown is used internally within the nonlinear solvers. 608316643e7SJed Brown 609316643e7SJed Brown Level: developer 610316643e7SJed Brown 611db781477SPatrick Sanan .seealso: `TSSetRHSFunction()`, `TSComputeIFunction()` 612316643e7SJed Brown @*/ 6130910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 614d763cef2SBarry Smith { 61524989b8cSPeter Brune TSRHSFunction rhsfunction; 61624989b8cSPeter Brune TSIFunction ifunction; 61724989b8cSPeter Brune void *ctx; 61824989b8cSPeter Brune DM dm; 61924989b8cSPeter Brune 620d763cef2SBarry Smith PetscFunctionBegin; 6210700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6220910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6230700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 6249566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 6259566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,&ctx)); 6269566063dSJacob Faibussowitsch PetscCall(DMTSGetIFunction(dm,&ifunction,NULL)); 627d763cef2SBarry Smith 6283c633725SBarry Smith PetscCheck(rhsfunction || ifunction,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 629d763cef2SBarry Smith 63024989b8cSPeter Brune if (rhsfunction) { 6319566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_FunctionEval,ts,U,y,0)); 6329566063dSJacob Faibussowitsch PetscCall(VecLockReadPush(U)); 633d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 6349566063dSJacob Faibussowitsch PetscCall((*rhsfunction)(ts,t,U,y,ctx)); 635d763cef2SBarry Smith PetscStackPop; 6369566063dSJacob Faibussowitsch PetscCall(VecLockReadPop(U)); 637a6ab3590SBarry Smith ts->rhsfuncs++; 6389566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_FunctionEval,ts,U,y,0)); 639214bc6a2SJed Brown } else { 6409566063dSJacob Faibussowitsch PetscCall(VecZeroEntries(y)); 641b2cd27e8SJed Brown } 642d763cef2SBarry Smith PetscFunctionReturn(0); 643d763cef2SBarry Smith } 644d763cef2SBarry Smith 645ef20d060SBarry Smith /*@ 646ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 647ef20d060SBarry Smith 648d083f849SBarry Smith Collective on TS 649ef20d060SBarry Smith 650ef20d060SBarry Smith Input Parameters: 651ef20d060SBarry Smith + ts - the TS context 652ef20d060SBarry Smith - t - current time 653ef20d060SBarry Smith 654ef20d060SBarry Smith Output Parameter: 6550910c330SBarry Smith . U - the solution 656ef20d060SBarry Smith 657ef20d060SBarry Smith Note: 658ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 659ef20d060SBarry Smith is used internally within the nonlinear solvers. 660ef20d060SBarry Smith 661ef20d060SBarry Smith Level: developer 662ef20d060SBarry Smith 663db781477SPatrick Sanan .seealso: `TSSetSolutionFunction()`, `TSSetRHSFunction()`, `TSComputeIFunction()` 664ef20d060SBarry Smith @*/ 6650910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 666ef20d060SBarry Smith { 667ef20d060SBarry Smith TSSolutionFunction solutionfunction; 668ef20d060SBarry Smith void *ctx; 669ef20d060SBarry Smith DM dm; 670ef20d060SBarry Smith 671ef20d060SBarry Smith PetscFunctionBegin; 672ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6730910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6749566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 6759566063dSJacob Faibussowitsch PetscCall(DMTSGetSolutionFunction(dm,&solutionfunction,&ctx)); 676ef20d060SBarry Smith 677ef20d060SBarry Smith if (solutionfunction) { 6789b7cd975SBarry Smith PetscStackPush("TS user solution function"); 6799566063dSJacob Faibussowitsch PetscCall((*solutionfunction)(ts,t,U,ctx)); 680ef20d060SBarry Smith PetscStackPop; 681ef20d060SBarry Smith } 682ef20d060SBarry Smith PetscFunctionReturn(0); 683ef20d060SBarry Smith } 6849b7cd975SBarry Smith /*@ 6859b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 6869b7cd975SBarry Smith 687d083f849SBarry Smith Collective on TS 6889b7cd975SBarry Smith 6899b7cd975SBarry Smith Input Parameters: 6909b7cd975SBarry Smith + ts - the TS context 6919b7cd975SBarry Smith - t - current time 6929b7cd975SBarry Smith 6939b7cd975SBarry Smith Output Parameter: 6949b7cd975SBarry Smith . U - the function value 6959b7cd975SBarry Smith 6969b7cd975SBarry Smith Note: 6979b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 6989b7cd975SBarry Smith is used internally within the nonlinear solvers. 6999b7cd975SBarry Smith 7009b7cd975SBarry Smith Level: developer 7019b7cd975SBarry Smith 702db781477SPatrick Sanan .seealso: `TSSetSolutionFunction()`, `TSSetRHSFunction()`, `TSComputeIFunction()` 7039b7cd975SBarry Smith @*/ 7049b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 7059b7cd975SBarry Smith { 7069b7cd975SBarry Smith void *ctx; 7079b7cd975SBarry Smith DM dm; 7085f80ce2aSJacob Faibussowitsch TSForcingFunction forcing; 7099b7cd975SBarry Smith 7109b7cd975SBarry Smith PetscFunctionBegin; 7119b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7129b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7139566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 7149566063dSJacob Faibussowitsch PetscCall(DMTSGetForcingFunction(dm,&forcing,&ctx)); 7159b7cd975SBarry Smith 7169b7cd975SBarry Smith if (forcing) { 7179b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 7189566063dSJacob Faibussowitsch PetscCall((*forcing)(ts,t,U,ctx)); 7199b7cd975SBarry Smith PetscStackPop; 7209b7cd975SBarry Smith } 7219b7cd975SBarry Smith PetscFunctionReturn(0); 7229b7cd975SBarry Smith } 723ef20d060SBarry Smith 724214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 725214bc6a2SJed Brown { 7262dd45cf8SJed Brown Vec F; 727214bc6a2SJed Brown 728214bc6a2SJed Brown PetscFunctionBegin; 7290298fd71SBarry Smith *Frhs = NULL; 7309566063dSJacob Faibussowitsch PetscCall(TSGetIFunction(ts,&F,NULL,NULL)); 731214bc6a2SJed Brown if (!ts->Frhs) { 7329566063dSJacob Faibussowitsch PetscCall(VecDuplicate(F,&ts->Frhs)); 733214bc6a2SJed Brown } 734214bc6a2SJed Brown *Frhs = ts->Frhs; 735214bc6a2SJed Brown PetscFunctionReturn(0); 736214bc6a2SJed Brown } 737214bc6a2SJed Brown 738971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 739214bc6a2SJed Brown { 740214bc6a2SJed Brown Mat A,B; 74141a1d4d2SBarry Smith TSIJacobian ijacobian; 742214bc6a2SJed Brown 743214bc6a2SJed Brown PetscFunctionBegin; 744c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 745c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 7469566063dSJacob Faibussowitsch PetscCall(TSGetIJacobian(ts,&A,&B,&ijacobian,NULL)); 747214bc6a2SJed Brown if (Arhs) { 748214bc6a2SJed Brown if (!ts->Arhs) { 74941a1d4d2SBarry Smith if (ijacobian) { 7509566063dSJacob Faibussowitsch PetscCall(MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs)); 7519566063dSJacob Faibussowitsch PetscCall(TSSetMatStructure(ts,SAME_NONZERO_PATTERN)); 75241a1d4d2SBarry Smith } else { 75341a1d4d2SBarry Smith ts->Arhs = A; 7549566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)A)); 75541a1d4d2SBarry Smith } 7563565c898SBarry Smith } else { 7573565c898SBarry Smith PetscBool flg; 7589566063dSJacob Faibussowitsch PetscCall(SNESGetUseMatrixFree(ts->snes,NULL,&flg)); 7593565c898SBarry Smith /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */ 7603565c898SBarry Smith if (flg && !ijacobian && ts->Arhs == ts->Brhs) { 7619566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)ts->Arhs)); 7623565c898SBarry Smith ts->Arhs = A; 7639566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)A)); 7643565c898SBarry Smith } 765214bc6a2SJed Brown } 766214bc6a2SJed Brown *Arhs = ts->Arhs; 767214bc6a2SJed Brown } 768214bc6a2SJed Brown if (Brhs) { 769214bc6a2SJed Brown if (!ts->Brhs) { 770bdb70873SJed Brown if (A != B) { 77141a1d4d2SBarry Smith if (ijacobian) { 7729566063dSJacob Faibussowitsch PetscCall(MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs)); 773bdb70873SJed Brown } else { 77441a1d4d2SBarry Smith ts->Brhs = B; 7759566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)B)); 77641a1d4d2SBarry Smith } 77741a1d4d2SBarry Smith } else { 7789566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)ts->Arhs)); 77951699248SLisandro Dalcin ts->Brhs = ts->Arhs; 780bdb70873SJed Brown } 781214bc6a2SJed Brown } 782214bc6a2SJed Brown *Brhs = ts->Brhs; 783214bc6a2SJed Brown } 784214bc6a2SJed Brown PetscFunctionReturn(0); 785214bc6a2SJed Brown } 786214bc6a2SJed Brown 787316643e7SJed Brown /*@ 7880910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 789316643e7SJed Brown 790d083f849SBarry Smith Collective on TS 791316643e7SJed Brown 792316643e7SJed Brown Input Parameters: 793316643e7SJed Brown + ts - the TS context 794316643e7SJed Brown . t - current time 7950910c330SBarry Smith . U - state vector 7960910c330SBarry Smith . Udot - time derivative of state vector 797214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 798316643e7SJed Brown 799316643e7SJed Brown Output Parameter: 800316643e7SJed Brown . Y - right hand side 801316643e7SJed Brown 802316643e7SJed Brown Note: 803316643e7SJed Brown Most users should not need to explicitly call this routine, as it 804316643e7SJed Brown is used internally within the nonlinear solvers. 805316643e7SJed Brown 806316643e7SJed Brown If the user did did not write their equations in implicit form, this 807316643e7SJed Brown function recasts them in implicit form. 808316643e7SJed Brown 809316643e7SJed Brown Level: developer 810316643e7SJed Brown 811db781477SPatrick Sanan .seealso: `TSSetIFunction()`, `TSComputeRHSFunction()` 812316643e7SJed Brown @*/ 8130910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 814316643e7SJed Brown { 81524989b8cSPeter Brune TSIFunction ifunction; 81624989b8cSPeter Brune TSRHSFunction rhsfunction; 81724989b8cSPeter Brune void *ctx; 81824989b8cSPeter Brune DM dm; 819316643e7SJed Brown 820316643e7SJed Brown PetscFunctionBegin; 8210700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8220910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8230910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 8240700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 825316643e7SJed Brown 8269566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 8279566063dSJacob Faibussowitsch PetscCall(DMTSGetIFunction(dm,&ifunction,&ctx)); 8289566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,NULL)); 82924989b8cSPeter Brune 8303c633725SBarry Smith PetscCheck(rhsfunction || ifunction,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 831d90be118SSean Farley 8329566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y)); 83324989b8cSPeter Brune if (ifunction) { 834316643e7SJed Brown PetscStackPush("TS user implicit function"); 8359566063dSJacob Faibussowitsch PetscCall((*ifunction)(ts,t,U,Udot,Y,ctx)); 836316643e7SJed Brown PetscStackPop; 837a6ab3590SBarry Smith ts->ifuncs++; 838214bc6a2SJed Brown } 839214bc6a2SJed Brown if (imex) { 84024989b8cSPeter Brune if (!ifunction) { 8419566063dSJacob Faibussowitsch PetscCall(VecCopy(Udot,Y)); 8422dd45cf8SJed Brown } 84324989b8cSPeter Brune } else if (rhsfunction) { 84424989b8cSPeter Brune if (ifunction) { 845214bc6a2SJed Brown Vec Frhs; 8469566063dSJacob Faibussowitsch PetscCall(TSGetRHSVec_Private(ts,&Frhs)); 8479566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts,t,U,Frhs)); 8489566063dSJacob Faibussowitsch PetscCall(VecAXPY(Y,-1,Frhs)); 8492dd45cf8SJed Brown } else { 8509566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts,t,U,Y)); 8519566063dSJacob Faibussowitsch PetscCall(VecAYPX(Y,-1,Udot)); 852316643e7SJed Brown } 8534a6899ffSJed Brown } 8549566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y)); 855316643e7SJed Brown PetscFunctionReturn(0); 856316643e7SJed Brown } 857316643e7SJed Brown 858cfa8a9a2SHong Zhang /* 859cfa8a9a2SHong Zhang TSRecoverRHSJacobian - Recover the Jacobian matrix so that one can call TSComputeRHSJacobian() on it. 860cfa8a9a2SHong Zhang 861cfa8a9a2SHong Zhang Note: 862cfa8a9a2SHong Zhang This routine is needed when one switches from TSComputeIJacobian() to TSComputeRHSJacobian() because the Jacobian matrix may be shifted or scaled in TSComputeIJacobian(). 863cfa8a9a2SHong Zhang 864cfa8a9a2SHong Zhang */ 865cfa8a9a2SHong Zhang static PetscErrorCode TSRecoverRHSJacobian(TS ts,Mat A,Mat B) 866cfa8a9a2SHong Zhang { 867cfa8a9a2SHong Zhang PetscFunctionBegin; 868cfa8a9a2SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8693c633725SBarry Smith PetscCheck(A == ts->Arhs,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Invalid Amat"); 8703c633725SBarry Smith PetscCheck(B == ts->Brhs,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Invalid Bmat"); 871cfa8a9a2SHong Zhang 872cfa8a9a2SHong Zhang if (ts->rhsjacobian.shift) { 8739566063dSJacob Faibussowitsch PetscCall(MatShift(A,-ts->rhsjacobian.shift)); 874cfa8a9a2SHong Zhang } 875cfa8a9a2SHong Zhang if (ts->rhsjacobian.scale == -1.) { 8769566063dSJacob Faibussowitsch PetscCall(MatScale(A,-1)); 877cfa8a9a2SHong Zhang } 878cfa8a9a2SHong Zhang if (B && B == ts->Brhs && A != B) { 879cfa8a9a2SHong Zhang if (ts->rhsjacobian.shift) { 8809566063dSJacob Faibussowitsch PetscCall(MatShift(B,-ts->rhsjacobian.shift)); 881cfa8a9a2SHong Zhang } 882cfa8a9a2SHong Zhang if (ts->rhsjacobian.scale == -1.) { 8839566063dSJacob Faibussowitsch PetscCall(MatScale(B,-1)); 884cfa8a9a2SHong Zhang } 885cfa8a9a2SHong Zhang } 886cfa8a9a2SHong Zhang ts->rhsjacobian.shift = 0; 887cfa8a9a2SHong Zhang ts->rhsjacobian.scale = 1.; 888cfa8a9a2SHong Zhang PetscFunctionReturn(0); 889cfa8a9a2SHong Zhang } 890cfa8a9a2SHong Zhang 891316643e7SJed Brown /*@ 892316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 893316643e7SJed Brown 894d083f849SBarry Smith Collective on TS 895316643e7SJed Brown 896316643e7SJed Brown Input 897316643e7SJed Brown Input Parameters: 898316643e7SJed Brown + ts - the TS context 899316643e7SJed Brown . t - current timestep 9000910c330SBarry Smith . U - state vector 9010910c330SBarry Smith . Udot - time derivative of state vector 902214bc6a2SJed Brown . shift - shift to apply, see note below 903214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 904316643e7SJed Brown 905316643e7SJed Brown Output Parameters: 906316643e7SJed Brown + A - Jacobian matrix 9073565c898SBarry Smith - B - matrix from which the preconditioner is constructed; often the same as A 908316643e7SJed Brown 909316643e7SJed Brown Notes: 9100910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 911316643e7SJed Brown 9120910c330SBarry Smith dF/dU + shift*dF/dUdot 913316643e7SJed Brown 914316643e7SJed Brown Most users should not need to explicitly call this routine, as it 915316643e7SJed Brown is used internally within the nonlinear solvers. 916316643e7SJed Brown 917316643e7SJed Brown Level: developer 918316643e7SJed Brown 919db781477SPatrick Sanan .seealso: `TSSetIJacobian()` 92063495f91SJed Brown @*/ 921d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 922316643e7SJed Brown { 92324989b8cSPeter Brune TSIJacobian ijacobian; 92424989b8cSPeter Brune TSRHSJacobian rhsjacobian; 92524989b8cSPeter Brune DM dm; 92624989b8cSPeter Brune void *ctx; 927316643e7SJed Brown 928316643e7SJed Brown PetscFunctionBegin; 9290700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9300910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 9310910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 932316643e7SJed Brown PetscValidPointer(A,6); 93394ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 934316643e7SJed Brown PetscValidPointer(B,7); 93594ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 93624989b8cSPeter Brune 9379566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 9389566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm,&ijacobian,&ctx)); 9399566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm,&rhsjacobian,NULL)); 94024989b8cSPeter Brune 9413c633725SBarry Smith PetscCheck(rhsjacobian || ijacobian,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 942316643e7SJed Brown 9439566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_JacobianEval,ts,U,A,B)); 94424989b8cSPeter Brune if (ijacobian) { 945316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 9469566063dSJacob Faibussowitsch PetscCall((*ijacobian)(ts,t,U,Udot,shift,A,B,ctx)); 947a6ab3590SBarry Smith ts->ijacs++; 948316643e7SJed Brown PetscStackPop; 9494a6899ffSJed Brown } 950214bc6a2SJed Brown if (imex) { 951b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 9524c26be97Sstefano_zampini PetscBool assembled; 953971015bcSStefano Zampini if (rhsjacobian) { 954971015bcSStefano Zampini Mat Arhs = NULL; 9559566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts,&Arhs,NULL)); 956971015bcSStefano Zampini if (A == Arhs) { 9573c633725SBarry 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 */ 958971015bcSStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 959971015bcSStefano Zampini } 960971015bcSStefano Zampini } 9619566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(A)); 9629566063dSJacob Faibussowitsch PetscCall(MatAssembled(A,&assembled)); 9634c26be97Sstefano_zampini if (!assembled) { 9649566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY)); 9659566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY)); 9664c26be97Sstefano_zampini } 9679566063dSJacob Faibussowitsch PetscCall(MatShift(A,shift)); 96894ab13aaSBarry Smith if (A != B) { 9699566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(B)); 9709566063dSJacob Faibussowitsch PetscCall(MatAssembled(B,&assembled)); 9714c26be97Sstefano_zampini if (!assembled) { 9729566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY)); 9739566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY)); 9744c26be97Sstefano_zampini } 9759566063dSJacob Faibussowitsch PetscCall(MatShift(B,shift)); 976214bc6a2SJed Brown } 977214bc6a2SJed Brown } 978214bc6a2SJed Brown } else { 979e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 980e8b1e424SHong Zhang if (rhsjacobian) { /* RHSJacobian needs to be converted to part of IJacobian if exists */ 9819566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts,&Arhs,&Brhs)); 982e1244c69SJed Brown } 983e8b1e424SHong Zhang if (Arhs == A) { /* No IJacobian matrix, so we only have the RHS matrix */ 984e8b1e424SHong Zhang PetscObjectState Ustate; 985e8b1e424SHong Zhang PetscObjectId Uid; 986e8b1e424SHong Zhang TSRHSFunction rhsfunction; 987e8b1e424SHong Zhang 9889566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,NULL)); 9899566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&Ustate)); 9909566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U,&Uid)); 991097a96a2SHong Zhang if ((rhsjacobian == TSComputeRHSJacobianConstant || (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && rhsfunction != TSComputeRHSFunctionLinear)) && ts->rhsjacobian.scale == -1.) { /* No need to recompute RHSJacobian */ 9929566063dSJacob Faibussowitsch PetscCall(MatShift(A,shift-ts->rhsjacobian.shift)); /* revert the old shift and add the new shift with a single call to MatShift */ 99334d322a1SHong Zhang if (A != B) { 9949566063dSJacob Faibussowitsch PetscCall(MatShift(B,shift-ts->rhsjacobian.shift)); 99534d322a1SHong Zhang } 996e8b1e424SHong Zhang } else { 9973565c898SBarry Smith PetscBool flg; 998e8b1e424SHong Zhang 999e8b1e424SHong Zhang if (ts->rhsjacobian.reuse) { /* Undo the damage */ 1000e8b1e424SHong Zhang /* MatScale has a short path for this case. 1001e8b1e424SHong Zhang However, this code path is taken the first time TSComputeRHSJacobian is called 1002e8b1e424SHong Zhang and the matrices have not been assembled yet */ 10039566063dSJacob Faibussowitsch PetscCall(TSRecoverRHSJacobian(ts,A,B)); 1004e8b1e424SHong Zhang } 10059566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts,t,U,A,B)); 10069566063dSJacob Faibussowitsch PetscCall(SNESGetUseMatrixFree(ts->snes,NULL,&flg)); 10073565c898SBarry Smith /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */ 10083565c898SBarry Smith if (!flg) { 10099566063dSJacob Faibussowitsch PetscCall(MatScale(A,-1)); 10109566063dSJacob Faibussowitsch PetscCall(MatShift(A,shift)); 10113565c898SBarry Smith } 101294ab13aaSBarry Smith if (A != B) { 10139566063dSJacob Faibussowitsch PetscCall(MatScale(B,-1)); 10149566063dSJacob Faibussowitsch PetscCall(MatShift(B,shift)); 1015316643e7SJed Brown } 1016e8b1e424SHong Zhang } 1017e8b1e424SHong Zhang ts->rhsjacobian.scale = -1; 1018e8b1e424SHong Zhang ts->rhsjacobian.shift = shift; 1019d60b7d5cSBarry Smith } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 1020e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 10219566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(A)); 10229566063dSJacob Faibussowitsch PetscCall(MatShift(A,shift)); 102394ab13aaSBarry Smith if (A != B) { 10249566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(B)); 10259566063dSJacob Faibussowitsch PetscCall(MatShift(B,shift)); 1026214bc6a2SJed Brown } 1027316643e7SJed Brown } 10289566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts,t,U,Arhs,Brhs)); 10299566063dSJacob Faibussowitsch PetscCall(MatAXPY(A,-1,Arhs,ts->axpy_pattern)); 103094ab13aaSBarry Smith if (A != B) { 10319566063dSJacob Faibussowitsch PetscCall(MatAXPY(B,-1,Brhs,ts->axpy_pattern)); 1032316643e7SJed Brown } 1033316643e7SJed Brown } 1034316643e7SJed Brown } 10359566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_JacobianEval,ts,U,A,B)); 1036316643e7SJed Brown PetscFunctionReturn(0); 1037316643e7SJed Brown } 1038316643e7SJed Brown 1039d763cef2SBarry Smith /*@C 1040d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 1041b5abc632SBarry Smith where U_t = G(t,u). 1042d763cef2SBarry Smith 10433f9fe445SBarry Smith Logically Collective on TS 1044d763cef2SBarry Smith 1045d763cef2SBarry Smith Input Parameters: 1046d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 10470298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 1048d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 1049d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 10500298fd71SBarry Smith function evaluation routine (may be NULL) 1051d763cef2SBarry Smith 1052a96d6ef6SBarry Smith Calling sequence of f: 1053a96d6ef6SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec F,void *ctx); 1054d763cef2SBarry Smith 1055a96d6ef6SBarry Smith + ts - timestep context 1056a96d6ef6SBarry Smith . t - current timestep 1057d763cef2SBarry Smith . u - input vector 1058d763cef2SBarry Smith . F - function vector 1059d763cef2SBarry Smith - ctx - [optional] user-defined function context 1060d763cef2SBarry Smith 1061d763cef2SBarry Smith Level: beginner 1062d763cef2SBarry Smith 106395452b02SPatrick Sanan Notes: 106495452b02SPatrick Sanan You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 10652bbac0d3SBarry Smith 1066db781477SPatrick Sanan .seealso: `TSSetRHSJacobian()`, `TSSetIJacobian()`, `TSSetIFunction()` 1067d763cef2SBarry Smith @*/ 1068089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 1069d763cef2SBarry Smith { 1070089b2837SJed Brown SNES snes; 10710298fd71SBarry Smith Vec ralloc = NULL; 107224989b8cSPeter Brune DM dm; 1073d763cef2SBarry Smith 1074089b2837SJed Brown PetscFunctionBegin; 10750700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1076ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 107724989b8cSPeter Brune 10789566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 10799566063dSJacob Faibussowitsch PetscCall(DMTSSetRHSFunction(dm,f,ctx)); 10809566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 1081e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 10829566063dSJacob Faibussowitsch PetscCall(VecDuplicate(ts->vec_sol,&ralloc)); 1083e856ceecSJed Brown r = ralloc; 1084e856ceecSJed Brown } 10859566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(snes,r,SNESTSFormFunction,ts)); 10869566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ralloc)); 1087d763cef2SBarry Smith PetscFunctionReturn(0); 1088d763cef2SBarry Smith } 1089d763cef2SBarry Smith 1090ef20d060SBarry Smith /*@C 1091abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1092ef20d060SBarry Smith 1093ef20d060SBarry Smith Logically Collective on TS 1094ef20d060SBarry Smith 1095ef20d060SBarry Smith Input Parameters: 1096ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 1097ef20d060SBarry Smith . f - routine for evaluating the solution 1098ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 10990298fd71SBarry Smith function evaluation routine (may be NULL) 1100ef20d060SBarry Smith 1101a96d6ef6SBarry Smith Calling sequence of f: 1102a96d6ef6SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,void *ctx); 1103ef20d060SBarry Smith 1104ef20d060SBarry Smith + t - current timestep 1105ef20d060SBarry Smith . u - output vector 1106ef20d060SBarry Smith - ctx - [optional] user-defined function context 1107ef20d060SBarry Smith 11080ed3bfb6SBarry Smith Options Database: 11090ed3bfb6SBarry Smith + -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction() 11100ed3bfb6SBarry Smith - -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction() 11110ed3bfb6SBarry Smith 1112abd5a294SJed Brown Notes: 1113abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 1114abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 1115abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 1116abd5a294SJed Brown 11174f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 1118abd5a294SJed Brown 1119ef20d060SBarry Smith Level: beginner 1120ef20d060SBarry Smith 1121db781477SPatrick Sanan .seealso: `TSSetRHSJacobian()`, `TSSetIJacobian()`, `TSComputeSolutionFunction()`, `TSSetForcingFunction()`, `TSSetSolution()`, `TSGetSolution()`, `TSMonitorLGError()`, `TSMonitorDrawError()` 1122ef20d060SBarry Smith @*/ 1123ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 1124ef20d060SBarry Smith { 1125ef20d060SBarry Smith DM dm; 1126ef20d060SBarry Smith 1127ef20d060SBarry Smith PetscFunctionBegin; 1128ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11299566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 11309566063dSJacob Faibussowitsch PetscCall(DMTSSetSolutionFunction(dm,f,ctx)); 1131ef20d060SBarry Smith PetscFunctionReturn(0); 1132ef20d060SBarry Smith } 1133ef20d060SBarry Smith 11349b7cd975SBarry Smith /*@C 11359b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 11369b7cd975SBarry Smith 11379b7cd975SBarry Smith Logically Collective on TS 11389b7cd975SBarry Smith 11399b7cd975SBarry Smith Input Parameters: 11409b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 1141e162b725SBarry Smith . func - routine for evaluating the forcing function 11429b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 11430298fd71SBarry Smith function evaluation routine (may be NULL) 11449b7cd975SBarry Smith 11459b7cd975SBarry Smith Calling sequence of func: 11466bc98fa9SBarry Smith $ PetscErrorCode func (TS ts,PetscReal t,Vec f,void *ctx); 11479b7cd975SBarry Smith 11489b7cd975SBarry Smith + t - current timestep 1149e162b725SBarry Smith . f - output vector 11509b7cd975SBarry Smith - ctx - [optional] user-defined function context 11519b7cd975SBarry Smith 11529b7cd975SBarry Smith Notes: 11539b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 1154e162b725SBarry 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 1155e162b725SBarry Smith definition of the problem you are solving and hence possibly introducing bugs. 1156e162b725SBarry Smith 1157e162b725SBarry Smith This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0 1158e162b725SBarry Smith 1159e162b725SBarry 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 1160e162b725SBarry Smith parameters can be passed in the ctx variable. 11619b7cd975SBarry Smith 11629b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 11639b7cd975SBarry Smith 11649b7cd975SBarry Smith Level: beginner 11659b7cd975SBarry Smith 1166db781477SPatrick Sanan .seealso: `TSSetRHSJacobian()`, `TSSetIJacobian()`, `TSComputeSolutionFunction()`, `TSSetSolutionFunction()` 11679b7cd975SBarry Smith @*/ 1168e162b725SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx) 11699b7cd975SBarry Smith { 11709b7cd975SBarry Smith DM dm; 11719b7cd975SBarry Smith 11729b7cd975SBarry Smith PetscFunctionBegin; 11739b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11749566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 11759566063dSJacob Faibussowitsch PetscCall(DMTSSetForcingFunction(dm,func,ctx)); 11769b7cd975SBarry Smith PetscFunctionReturn(0); 11779b7cd975SBarry Smith } 11789b7cd975SBarry Smith 1179d763cef2SBarry Smith /*@C 1180f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1181b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1182d763cef2SBarry Smith 11833f9fe445SBarry Smith Logically Collective on TS 1184d763cef2SBarry Smith 1185d763cef2SBarry Smith Input Parameters: 1186d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1187e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1188e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1189d763cef2SBarry Smith . f - the Jacobian evaluation routine 1190d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 11910298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1192d763cef2SBarry Smith 1193f7ab8db6SBarry Smith Calling sequence of f: 1194a96d6ef6SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1195d763cef2SBarry Smith 1196d763cef2SBarry Smith + t - current timestep 1197d763cef2SBarry Smith . u - input vector 1198e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1199e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1200d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1201d763cef2SBarry Smith 12026cd88445SBarry Smith Notes: 12036cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 12046cd88445SBarry Smith 12056cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1206ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1207d763cef2SBarry Smith 1208d763cef2SBarry Smith Level: beginner 1209d763cef2SBarry Smith 1210db781477SPatrick Sanan .seealso: `SNESComputeJacobianDefaultColor()`, `TSSetRHSFunction()`, `TSRHSJacobianSetReuse()`, `TSSetIJacobian()` 1211d763cef2SBarry Smith 1212d763cef2SBarry Smith @*/ 1213e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1214d763cef2SBarry Smith { 1215089b2837SJed Brown SNES snes; 121624989b8cSPeter Brune DM dm; 121724989b8cSPeter Brune TSIJacobian ijacobian; 1218277b19d0SLisandro Dalcin 1219d763cef2SBarry Smith PetscFunctionBegin; 12200700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1221e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1222e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1223e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1224e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1225d763cef2SBarry Smith 12269566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 12279566063dSJacob Faibussowitsch PetscCall(DMTSSetRHSJacobian(dm,f,ctx)); 12289566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm,&ijacobian,NULL)); 12299566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 12305f659677SPeter Brune if (!ijacobian) { 12319566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts)); 12320e4ef248SJed Brown } 1233e5d3d808SBarry Smith if (Amat) { 12349566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)Amat)); 12359566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Arhs)); 1236e5d3d808SBarry Smith ts->Arhs = Amat; 12370e4ef248SJed Brown } 1238e5d3d808SBarry Smith if (Pmat) { 12399566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)Pmat)); 12409566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Brhs)); 1241e5d3d808SBarry Smith ts->Brhs = Pmat; 12420e4ef248SJed Brown } 1243d763cef2SBarry Smith PetscFunctionReturn(0); 1244d763cef2SBarry Smith } 1245d763cef2SBarry Smith 1246316643e7SJed Brown /*@C 1247b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1248316643e7SJed Brown 12493f9fe445SBarry Smith Logically Collective on TS 1250316643e7SJed Brown 1251316643e7SJed Brown Input Parameters: 1252316643e7SJed Brown + ts - the TS context obtained from TSCreate() 12530298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1254316643e7SJed Brown . f - the function evaluation routine 12550298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1256316643e7SJed Brown 1257316643e7SJed Brown Calling sequence of f: 12586bc98fa9SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1259316643e7SJed Brown 1260316643e7SJed Brown + t - time at step/stage being solved 1261316643e7SJed Brown . u - state vector 1262316643e7SJed Brown . u_t - time derivative of state vector 1263316643e7SJed Brown . F - function vector 1264316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1265316643e7SJed Brown 1266316643e7SJed Brown Important: 12672bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1268316643e7SJed Brown 1269316643e7SJed Brown Level: beginner 1270316643e7SJed Brown 1271db781477SPatrick Sanan .seealso: `TSSetRHSJacobian()`, `TSSetRHSFunction()`, `TSSetIJacobian()` 1272316643e7SJed Brown @*/ 127351699248SLisandro Dalcin PetscErrorCode TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx) 1274316643e7SJed Brown { 1275089b2837SJed Brown SNES snes; 127651699248SLisandro Dalcin Vec ralloc = NULL; 127724989b8cSPeter Brune DM dm; 1278316643e7SJed Brown 1279316643e7SJed Brown PetscFunctionBegin; 12800700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 128151699248SLisandro Dalcin if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 128224989b8cSPeter Brune 12839566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 12849566063dSJacob Faibussowitsch PetscCall(DMTSSetIFunction(dm,f,ctx)); 128524989b8cSPeter Brune 12869566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 128751699248SLisandro Dalcin if (!r && !ts->dm && ts->vec_sol) { 12889566063dSJacob Faibussowitsch PetscCall(VecDuplicate(ts->vec_sol,&ralloc)); 128951699248SLisandro Dalcin r = ralloc; 1290e856ceecSJed Brown } 12919566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(snes,r,SNESTSFormFunction,ts)); 12929566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ralloc)); 1293089b2837SJed Brown PetscFunctionReturn(0); 1294089b2837SJed Brown } 1295089b2837SJed Brown 1296089b2837SJed Brown /*@C 1297a5b23f4aSJose E. Roman TSGetIFunction - Returns the vector where the implicit residual is stored and the function/context to compute it. 1298089b2837SJed Brown 1299089b2837SJed Brown Not Collective 1300089b2837SJed Brown 1301089b2837SJed Brown Input Parameter: 1302089b2837SJed Brown . ts - the TS context 1303089b2837SJed Brown 1304d8d19677SJose E. Roman Output Parameters: 13050298fd71SBarry Smith + r - vector to hold residual (or NULL) 13060298fd71SBarry Smith . func - the function to compute residual (or NULL) 13070298fd71SBarry Smith - ctx - the function context (or NULL) 1308089b2837SJed Brown 1309089b2837SJed Brown Level: advanced 1310089b2837SJed Brown 1311db781477SPatrick Sanan .seealso: `TSSetIFunction()`, `SNESGetFunction()` 1312089b2837SJed Brown @*/ 1313089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1314089b2837SJed Brown { 1315089b2837SJed Brown SNES snes; 131624989b8cSPeter Brune DM dm; 1317089b2837SJed Brown 1318089b2837SJed Brown PetscFunctionBegin; 1319089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13209566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 13219566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes,r,NULL,NULL)); 13229566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 13239566063dSJacob Faibussowitsch PetscCall(DMTSGetIFunction(dm,func,ctx)); 1324089b2837SJed Brown PetscFunctionReturn(0); 1325089b2837SJed Brown } 1326089b2837SJed Brown 1327089b2837SJed Brown /*@C 1328089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1329089b2837SJed Brown 1330089b2837SJed Brown Not Collective 1331089b2837SJed Brown 1332089b2837SJed Brown Input Parameter: 1333089b2837SJed Brown . ts - the TS context 1334089b2837SJed Brown 1335d8d19677SJose E. Roman Output Parameters: 13360298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 13370298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 13380298fd71SBarry Smith - ctx - the function context (or NULL) 1339089b2837SJed Brown 1340089b2837SJed Brown Level: advanced 1341089b2837SJed Brown 1342db781477SPatrick Sanan .seealso: `TSSetRHSFunction()`, `SNESGetFunction()` 1343089b2837SJed Brown @*/ 1344089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1345089b2837SJed Brown { 1346089b2837SJed Brown SNES snes; 134724989b8cSPeter Brune DM dm; 1348089b2837SJed Brown 1349089b2837SJed Brown PetscFunctionBegin; 1350089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13519566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 13529566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes,r,NULL,NULL)); 13539566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 13549566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm,func,ctx)); 1355316643e7SJed Brown PetscFunctionReturn(0); 1356316643e7SJed Brown } 1357316643e7SJed Brown 1358316643e7SJed Brown /*@C 1359a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1360ae8867d6SBarry Smith provided with TSSetIFunction(). 1361316643e7SJed Brown 13623f9fe445SBarry Smith Logically Collective on TS 1363316643e7SJed Brown 1364316643e7SJed Brown Input Parameters: 1365316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1366e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1367e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1368316643e7SJed Brown . f - the Jacobian evaluation routine 13690298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1370316643e7SJed Brown 1371316643e7SJed Brown Calling sequence of f: 13726bc98fa9SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1373316643e7SJed Brown 1374316643e7SJed Brown + t - time at step/stage being solved 13751b4a444bSJed Brown . U - state vector 13761b4a444bSJed Brown . U_t - time derivative of state vector 1377316643e7SJed Brown . a - shift 1378e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1379e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1380316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1381316643e7SJed Brown 1382316643e7SJed Brown Notes: 1383e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1384316643e7SJed Brown 1385895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 1386895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 1387895c21f2SBarry Smith 1388a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1389b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1390a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1391a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1392a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1393a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1394a4f0a591SBarry Smith 13956cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 13966cd88445SBarry Smith 13976cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1398ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1399ca5f011dSBarry Smith 1400316643e7SJed Brown Level: beginner 1401316643e7SJed Brown 1402db781477SPatrick Sanan .seealso: `TSSetIFunction()`, `TSSetRHSJacobian()`, `SNESComputeJacobianDefaultColor()`, `SNESComputeJacobianDefault()`, `TSSetRHSFunction()` 1403316643e7SJed Brown 1404316643e7SJed Brown @*/ 1405e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1406316643e7SJed Brown { 1407089b2837SJed Brown SNES snes; 140824989b8cSPeter Brune DM dm; 1409316643e7SJed Brown 1410316643e7SJed Brown PetscFunctionBegin; 14110700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1412e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1413e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1414e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1415e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 141624989b8cSPeter Brune 14179566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 14189566063dSJacob Faibussowitsch PetscCall(DMTSSetIJacobian(dm,f,ctx)); 141924989b8cSPeter Brune 14209566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 14219566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts)); 1422316643e7SJed Brown PetscFunctionReturn(0); 1423316643e7SJed Brown } 1424316643e7SJed Brown 1425e1244c69SJed Brown /*@ 1426e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1427e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1428e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1429e1244c69SJed Brown not been changed by the TS. 1430e1244c69SJed Brown 1431e1244c69SJed Brown Logically Collective 1432e1244c69SJed Brown 14334165533cSJose E. Roman Input Parameters: 1434e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1435e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1436e1244c69SJed Brown 1437e1244c69SJed Brown Level: intermediate 1438e1244c69SJed Brown 1439db781477SPatrick Sanan .seealso: `TSSetRHSJacobian()`, `TSComputeRHSJacobianConstant()` 1440e1244c69SJed Brown @*/ 1441e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1442e1244c69SJed Brown { 1443e1244c69SJed Brown PetscFunctionBegin; 1444e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1445e1244c69SJed Brown PetscFunctionReturn(0); 1446e1244c69SJed Brown } 1447e1244c69SJed Brown 1448efe9872eSLisandro Dalcin /*@C 1449efe9872eSLisandro Dalcin TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved. 1450efe9872eSLisandro Dalcin 1451efe9872eSLisandro Dalcin Logically Collective on TS 1452efe9872eSLisandro Dalcin 1453efe9872eSLisandro Dalcin Input Parameters: 1454efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1455efe9872eSLisandro Dalcin . F - vector to hold the residual (or NULL to have it created internally) 1456efe9872eSLisandro Dalcin . fun - the function evaluation routine 1457efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1458efe9872eSLisandro Dalcin 1459efe9872eSLisandro Dalcin Calling sequence of fun: 14606bc98fa9SBarry Smith $ PetscErrorCode fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx); 1461efe9872eSLisandro Dalcin 1462efe9872eSLisandro Dalcin + t - time at step/stage being solved 1463efe9872eSLisandro Dalcin . U - state vector 1464efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1465efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1466efe9872eSLisandro Dalcin . F - function vector 1467efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine (may be NULL) 1468efe9872eSLisandro Dalcin 1469efe9872eSLisandro Dalcin Level: beginner 1470efe9872eSLisandro Dalcin 1471db781477SPatrick Sanan .seealso: `TSSetI2Jacobian()`, `TSSetIFunction()`, `TSCreate()`, `TSSetRHSFunction()` 1472efe9872eSLisandro Dalcin @*/ 1473efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx) 1474efe9872eSLisandro Dalcin { 1475efe9872eSLisandro Dalcin DM dm; 1476efe9872eSLisandro Dalcin 1477efe9872eSLisandro Dalcin PetscFunctionBegin; 1478efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1479efe9872eSLisandro Dalcin if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2); 14809566063dSJacob Faibussowitsch PetscCall(TSSetIFunction(ts,F,NULL,NULL)); 14819566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 14829566063dSJacob Faibussowitsch PetscCall(DMTSSetI2Function(dm,fun,ctx)); 1483efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1484efe9872eSLisandro Dalcin } 1485efe9872eSLisandro Dalcin 1486efe9872eSLisandro Dalcin /*@C 1487a5b23f4aSJose E. Roman TSGetI2Function - Returns the vector where the implicit residual is stored and the function/context to compute it. 1488efe9872eSLisandro Dalcin 1489efe9872eSLisandro Dalcin Not Collective 1490efe9872eSLisandro Dalcin 1491efe9872eSLisandro Dalcin Input Parameter: 1492efe9872eSLisandro Dalcin . ts - the TS context 1493efe9872eSLisandro Dalcin 1494d8d19677SJose E. Roman Output Parameters: 1495efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL) 1496efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL) 1497efe9872eSLisandro Dalcin - ctx - the function context (or NULL) 1498efe9872eSLisandro Dalcin 1499efe9872eSLisandro Dalcin Level: advanced 1500efe9872eSLisandro Dalcin 1501db781477SPatrick Sanan .seealso: `TSSetIFunction()`, `SNESGetFunction()`, `TSCreate()` 1502efe9872eSLisandro Dalcin @*/ 1503efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx) 1504efe9872eSLisandro Dalcin { 1505efe9872eSLisandro Dalcin SNES snes; 1506efe9872eSLisandro Dalcin DM dm; 1507efe9872eSLisandro Dalcin 1508efe9872eSLisandro Dalcin PetscFunctionBegin; 1509efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 15109566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 15119566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes,r,NULL,NULL)); 15129566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 15139566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Function(dm,fun,ctx)); 1514efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1515efe9872eSLisandro Dalcin } 1516efe9872eSLisandro Dalcin 1517efe9872eSLisandro Dalcin /*@C 1518bc77d74cSLisandro Dalcin TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t + a*dF/dU_tt 1519efe9872eSLisandro Dalcin where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function(). 1520efe9872eSLisandro Dalcin 1521efe9872eSLisandro Dalcin Logically Collective on TS 1522efe9872eSLisandro Dalcin 1523efe9872eSLisandro Dalcin Input Parameters: 1524efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1525efe9872eSLisandro Dalcin . J - Jacobian matrix 1526efe9872eSLisandro Dalcin . P - preconditioning matrix for J (may be same as J) 1527efe9872eSLisandro Dalcin . jac - the Jacobian evaluation routine 1528efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1529efe9872eSLisandro Dalcin 1530efe9872eSLisandro Dalcin Calling sequence of jac: 15316bc98fa9SBarry 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); 1532efe9872eSLisandro Dalcin 1533efe9872eSLisandro Dalcin + t - time at step/stage being solved 1534efe9872eSLisandro Dalcin . U - state vector 1535efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1536efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1537efe9872eSLisandro Dalcin . v - shift for U_t 1538efe9872eSLisandro Dalcin . a - shift for U_tt 1539efe9872eSLisandro 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 1540efe9872eSLisandro Dalcin . P - preconditioning matrix for J, may be same as J 1541efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine 1542efe9872eSLisandro Dalcin 1543efe9872eSLisandro Dalcin Notes: 1544efe9872eSLisandro Dalcin The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve. 1545efe9872eSLisandro Dalcin 1546efe9872eSLisandro Dalcin The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be 1547efe9872eSLisandro 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. 1548efe9872eSLisandro Dalcin The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U where the positive "shift" 1549bc77d74cSLisandro Dalcin parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states. 1550efe9872eSLisandro Dalcin 1551efe9872eSLisandro Dalcin Level: beginner 1552efe9872eSLisandro Dalcin 1553db781477SPatrick Sanan .seealso: `TSSetI2Function()`, `TSGetI2Jacobian()` 1554efe9872eSLisandro Dalcin @*/ 1555efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx) 1556efe9872eSLisandro Dalcin { 1557efe9872eSLisandro Dalcin DM dm; 1558efe9872eSLisandro Dalcin 1559efe9872eSLisandro Dalcin PetscFunctionBegin; 1560efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1561efe9872eSLisandro Dalcin if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2); 1562efe9872eSLisandro Dalcin if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3); 15639566063dSJacob Faibussowitsch PetscCall(TSSetIJacobian(ts,J,P,NULL,NULL)); 15649566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 15659566063dSJacob Faibussowitsch PetscCall(DMTSSetI2Jacobian(dm,jac,ctx)); 1566efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1567efe9872eSLisandro Dalcin } 1568efe9872eSLisandro Dalcin 1569efe9872eSLisandro Dalcin /*@C 1570efe9872eSLisandro Dalcin TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep. 1571efe9872eSLisandro Dalcin 1572efe9872eSLisandro Dalcin Not Collective, but parallel objects are returned if TS is parallel 1573efe9872eSLisandro Dalcin 1574efe9872eSLisandro Dalcin Input Parameter: 1575efe9872eSLisandro Dalcin . ts - The TS context obtained from TSCreate() 1576efe9872eSLisandro Dalcin 1577efe9872eSLisandro Dalcin Output Parameters: 1578efe9872eSLisandro Dalcin + J - The (approximate) Jacobian of F(t,U,U_t,U_tt) 1579efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J 1580efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices 1581efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine 1582efe9872eSLisandro Dalcin 158395452b02SPatrick Sanan Notes: 158495452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 1585efe9872eSLisandro Dalcin 1586efe9872eSLisandro Dalcin Level: advanced 1587efe9872eSLisandro Dalcin 1588db781477SPatrick Sanan .seealso: `TSGetTimeStep()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()`, `TSSetI2Jacobian()`, `TSGetI2Function()`, `TSCreate()` 1589efe9872eSLisandro Dalcin 1590efe9872eSLisandro Dalcin @*/ 1591efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx) 1592efe9872eSLisandro Dalcin { 1593efe9872eSLisandro Dalcin SNES snes; 1594efe9872eSLisandro Dalcin DM dm; 1595efe9872eSLisandro Dalcin 1596efe9872eSLisandro Dalcin PetscFunctionBegin; 15979566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 15989566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 15999566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes,J,P,NULL,NULL)); 16009566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 16019566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm,jac,ctx)); 1602efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1603efe9872eSLisandro Dalcin } 1604efe9872eSLisandro Dalcin 1605efe9872eSLisandro Dalcin /*@ 1606efe9872eSLisandro Dalcin TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0 1607efe9872eSLisandro Dalcin 1608d083f849SBarry Smith Collective on TS 1609efe9872eSLisandro Dalcin 1610efe9872eSLisandro Dalcin Input Parameters: 1611efe9872eSLisandro Dalcin + ts - the TS context 1612efe9872eSLisandro Dalcin . t - current time 1613efe9872eSLisandro Dalcin . U - state vector 1614efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t) 1615efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt) 1616efe9872eSLisandro Dalcin 1617efe9872eSLisandro Dalcin Output Parameter: 1618efe9872eSLisandro Dalcin . F - the residual vector 1619efe9872eSLisandro Dalcin 1620efe9872eSLisandro Dalcin Note: 1621efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1622efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1623efe9872eSLisandro Dalcin 1624efe9872eSLisandro Dalcin Level: developer 1625efe9872eSLisandro Dalcin 1626db781477SPatrick Sanan .seealso: `TSSetI2Function()`, `TSGetI2Function()` 1627efe9872eSLisandro Dalcin @*/ 1628efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F) 1629efe9872eSLisandro Dalcin { 1630efe9872eSLisandro Dalcin DM dm; 1631efe9872eSLisandro Dalcin TSI2Function I2Function; 1632efe9872eSLisandro Dalcin void *ctx; 1633efe9872eSLisandro Dalcin TSRHSFunction rhsfunction; 1634efe9872eSLisandro Dalcin 1635efe9872eSLisandro Dalcin PetscFunctionBegin; 1636efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1637efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1638efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1639efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1640efe9872eSLisandro Dalcin PetscValidHeaderSpecific(F,VEC_CLASSID,6); 1641efe9872eSLisandro Dalcin 16429566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 16439566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Function(dm,&I2Function,&ctx)); 16449566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,NULL)); 1645efe9872eSLisandro Dalcin 1646efe9872eSLisandro Dalcin if (!I2Function) { 16479566063dSJacob Faibussowitsch PetscCall(TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE)); 1648efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1649efe9872eSLisandro Dalcin } 1650efe9872eSLisandro Dalcin 16519566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_FunctionEval,ts,U,V,F)); 1652efe9872eSLisandro Dalcin 1653efe9872eSLisandro Dalcin PetscStackPush("TS user implicit function"); 16549566063dSJacob Faibussowitsch PetscCall(I2Function(ts,t,U,V,A,F,ctx)); 1655efe9872eSLisandro Dalcin PetscStackPop; 1656efe9872eSLisandro Dalcin 1657efe9872eSLisandro Dalcin if (rhsfunction) { 1658efe9872eSLisandro Dalcin Vec Frhs; 16599566063dSJacob Faibussowitsch PetscCall(TSGetRHSVec_Private(ts,&Frhs)); 16609566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts,t,U,Frhs)); 16619566063dSJacob Faibussowitsch PetscCall(VecAXPY(F,-1,Frhs)); 1662efe9872eSLisandro Dalcin } 1663efe9872eSLisandro Dalcin 16649566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_FunctionEval,ts,U,V,F)); 1665efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1666efe9872eSLisandro Dalcin } 1667efe9872eSLisandro Dalcin 1668efe9872eSLisandro Dalcin /*@ 1669efe9872eSLisandro Dalcin TSComputeI2Jacobian - Evaluates the Jacobian of the DAE 1670efe9872eSLisandro Dalcin 1671d083f849SBarry Smith Collective on TS 1672efe9872eSLisandro Dalcin 1673efe9872eSLisandro Dalcin Input Parameters: 1674efe9872eSLisandro Dalcin + ts - the TS context 1675efe9872eSLisandro Dalcin . t - current timestep 1676efe9872eSLisandro Dalcin . U - state vector 1677efe9872eSLisandro Dalcin . V - time derivative of state vector 1678efe9872eSLisandro Dalcin . A - second time derivative of state vector 1679efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below 1680efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below 1681efe9872eSLisandro Dalcin 1682efe9872eSLisandro Dalcin Output Parameters: 1683efe9872eSLisandro Dalcin + J - Jacobian matrix 1684efe9872eSLisandro Dalcin - P - optional preconditioning matrix 1685efe9872eSLisandro Dalcin 1686efe9872eSLisandro Dalcin Notes: 1687efe9872eSLisandro Dalcin If F(t,U,V,A)=0 is the DAE, the required Jacobian is 1688efe9872eSLisandro Dalcin 1689efe9872eSLisandro Dalcin dF/dU + shiftV*dF/dV + shiftA*dF/dA 1690efe9872eSLisandro Dalcin 1691efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1692efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1693efe9872eSLisandro Dalcin 1694efe9872eSLisandro Dalcin Level: developer 1695efe9872eSLisandro Dalcin 1696db781477SPatrick Sanan .seealso: `TSSetI2Jacobian()` 1697efe9872eSLisandro Dalcin @*/ 1698efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P) 1699efe9872eSLisandro Dalcin { 1700efe9872eSLisandro Dalcin DM dm; 1701efe9872eSLisandro Dalcin TSI2Jacobian I2Jacobian; 1702efe9872eSLisandro Dalcin void *ctx; 1703efe9872eSLisandro Dalcin TSRHSJacobian rhsjacobian; 1704efe9872eSLisandro Dalcin 1705efe9872eSLisandro Dalcin PetscFunctionBegin; 1706efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1707efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1708efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1709efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1710efe9872eSLisandro Dalcin PetscValidHeaderSpecific(J,MAT_CLASSID,8); 1711efe9872eSLisandro Dalcin PetscValidHeaderSpecific(P,MAT_CLASSID,9); 1712efe9872eSLisandro Dalcin 17139566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 17149566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx)); 17159566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm,&rhsjacobian,NULL)); 1716efe9872eSLisandro Dalcin 1717efe9872eSLisandro Dalcin if (!I2Jacobian) { 17189566063dSJacob Faibussowitsch PetscCall(TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE)); 1719efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1720efe9872eSLisandro Dalcin } 1721efe9872eSLisandro Dalcin 17229566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_JacobianEval,ts,U,J,P)); 1723efe9872eSLisandro Dalcin 1724efe9872eSLisandro Dalcin PetscStackPush("TS user implicit Jacobian"); 17259566063dSJacob Faibussowitsch PetscCall(I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx)); 1726efe9872eSLisandro Dalcin PetscStackPop; 1727efe9872eSLisandro Dalcin 1728efe9872eSLisandro Dalcin if (rhsjacobian) { 1729d60b7d5cSBarry Smith Mat Jrhs,Prhs; 17309566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts,&Jrhs,&Prhs)); 17319566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs)); 17329566063dSJacob Faibussowitsch PetscCall(MatAXPY(J,-1,Jrhs,ts->axpy_pattern)); 17339566063dSJacob Faibussowitsch if (P != J) PetscCall(MatAXPY(P,-1,Prhs,ts->axpy_pattern)); 1734efe9872eSLisandro Dalcin } 1735efe9872eSLisandro Dalcin 17369566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_JacobianEval,ts,U,J,P)); 1737efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1738efe9872eSLisandro Dalcin } 1739efe9872eSLisandro Dalcin 1740438f35afSJed Brown /*@C 1741438f35afSJed Brown TSSetTransientVariable - sets function to transform from state to transient variables 1742438f35afSJed Brown 1743438f35afSJed Brown Logically Collective 1744438f35afSJed Brown 17454165533cSJose E. Roman Input Parameters: 1746438f35afSJed Brown + ts - time stepping context on which to change the transient variable 1747a96d6ef6SBarry Smith . tvar - a function that transforms to transient variables 1748438f35afSJed Brown - ctx - a context for tvar 1749438f35afSJed Brown 1750a96d6ef6SBarry Smith Calling sequence of tvar: 1751a96d6ef6SBarry Smith $ PetscErrorCode tvar(TS ts,Vec p,Vec c,void *ctx); 1752a96d6ef6SBarry Smith 1753a96d6ef6SBarry Smith + ts - timestep context 1754a96d6ef6SBarry Smith . p - input vector (primative form) 1755a96d6ef6SBarry Smith . c - output vector, transient variables (conservative form) 1756a96d6ef6SBarry Smith - ctx - [optional] user-defined function context 1757a96d6ef6SBarry Smith 1758438f35afSJed Brown Level: advanced 1759438f35afSJed Brown 1760438f35afSJed Brown Notes: 1761438f35afSJed Brown This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., TSBDF) 1762438f35afSJed Brown can be conservative. In this context, primitive variables P are used to model the state (e.g., because they lead to 1763438f35afSJed Brown well-conditioned formulations even in limiting cases such as low-Mach or zero porosity). The transient variable is 1764438f35afSJed Brown C(P), specified by calling this function. An IFunction thus receives arguments (P, Cdot) and the IJacobian must be 1765438f35afSJed Brown evaluated via the chain rule, as in 1766438f35afSJed Brown 1767438f35afSJed Brown dF/dP + shift * dF/dCdot dC/dP. 1768438f35afSJed Brown 1769db781477SPatrick Sanan .seealso: `DMTSSetTransientVariable()`, `DMTSGetTransientVariable()`, `TSSetIFunction()`, `TSSetIJacobian()` 1770438f35afSJed Brown @*/ 1771438f35afSJed Brown PetscErrorCode TSSetTransientVariable(TS ts,TSTransientVariable tvar,void *ctx) 1772438f35afSJed Brown { 1773438f35afSJed Brown DM dm; 1774438f35afSJed Brown 1775438f35afSJed Brown PetscFunctionBegin; 1776438f35afSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 17779566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 17789566063dSJacob Faibussowitsch PetscCall(DMTSSetTransientVariable(dm,tvar,ctx)); 1779438f35afSJed Brown PetscFunctionReturn(0); 1780438f35afSJed Brown } 1781438f35afSJed Brown 1782efe9872eSLisandro Dalcin /*@ 1783e3c11fc1SJed Brown TSComputeTransientVariable - transforms state (primitive) variables to transient (conservative) variables 1784e3c11fc1SJed Brown 1785e3c11fc1SJed Brown Logically Collective 1786e3c11fc1SJed Brown 1787e3c11fc1SJed Brown Input Parameters: 1788e3c11fc1SJed Brown + ts - TS on which to compute 1789e3c11fc1SJed Brown - U - state vector to be transformed to transient variables 1790e3c11fc1SJed Brown 1791e3c11fc1SJed Brown Output Parameters: 1792e3c11fc1SJed Brown . C - transient (conservative) variable 1793e3c11fc1SJed Brown 1794e3c11fc1SJed Brown Developer Notes: 1795e3c11fc1SJed Brown If DMTSSetTransientVariable() has not been called, then C is not modified in this routine and C=NULL is allowed. 1796e3c11fc1SJed Brown This makes it safe to call without a guard. One can use TSHasTransientVariable() to check if transient variables are 1797e3c11fc1SJed Brown being used. 1798e3c11fc1SJed Brown 1799e3c11fc1SJed Brown Level: developer 1800e3c11fc1SJed Brown 1801db781477SPatrick Sanan .seealso: `DMTSSetTransientVariable()`, `TSComputeIFunction()`, `TSComputeIJacobian()` 1802e3c11fc1SJed Brown @*/ 1803e3c11fc1SJed Brown PetscErrorCode TSComputeTransientVariable(TS ts,Vec U,Vec C) 1804e3c11fc1SJed Brown { 1805e3c11fc1SJed Brown DM dm; 1806e3c11fc1SJed Brown DMTS dmts; 1807e3c11fc1SJed Brown 1808e3c11fc1SJed Brown PetscFunctionBegin; 1809e3c11fc1SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1810e3c11fc1SJed Brown PetscValidHeaderSpecific(U,VEC_CLASSID,2); 18119566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 18129566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm,&dmts)); 1813e3c11fc1SJed Brown if (dmts->ops->transientvar) { 1814e3c11fc1SJed Brown PetscValidHeaderSpecific(C,VEC_CLASSID,3); 18159566063dSJacob Faibussowitsch PetscCall((*dmts->ops->transientvar)(ts,U,C,dmts->transientvarctx)); 1816e3c11fc1SJed Brown } 1817e3c11fc1SJed Brown PetscFunctionReturn(0); 1818e3c11fc1SJed Brown } 1819e3c11fc1SJed Brown 1820e3c11fc1SJed Brown /*@ 1821e3c11fc1SJed Brown TSHasTransientVariable - determine whether transient variables have been set 1822e3c11fc1SJed Brown 1823e3c11fc1SJed Brown Logically Collective 1824e3c11fc1SJed Brown 1825e3c11fc1SJed Brown Input Parameters: 1826e3c11fc1SJed Brown . ts - TS on which to compute 1827e3c11fc1SJed Brown 1828e3c11fc1SJed Brown Output Parameters: 1829e3c11fc1SJed Brown . has - PETSC_TRUE if transient variables have been set 1830e3c11fc1SJed Brown 1831e3c11fc1SJed Brown Level: developer 1832e3c11fc1SJed Brown 1833db781477SPatrick Sanan .seealso: `DMTSSetTransientVariable()`, `TSComputeTransientVariable()` 1834e3c11fc1SJed Brown @*/ 1835e3c11fc1SJed Brown PetscErrorCode TSHasTransientVariable(TS ts,PetscBool *has) 1836e3c11fc1SJed Brown { 1837e3c11fc1SJed Brown DM dm; 1838e3c11fc1SJed Brown DMTS dmts; 1839e3c11fc1SJed Brown 1840e3c11fc1SJed Brown PetscFunctionBegin; 1841e3c11fc1SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 18429566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 18439566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm,&dmts)); 1844e3c11fc1SJed Brown *has = dmts->ops->transientvar ? PETSC_TRUE : PETSC_FALSE; 1845e3c11fc1SJed Brown PetscFunctionReturn(0); 1846e3c11fc1SJed Brown } 1847e3c11fc1SJed Brown 1848e3c11fc1SJed Brown /*@ 1849efe9872eSLisandro Dalcin TS2SetSolution - Sets the initial solution and time derivative vectors 1850efe9872eSLisandro Dalcin for use by the TS routines handling second order equations. 1851efe9872eSLisandro Dalcin 1852d083f849SBarry Smith Logically Collective on TS 1853efe9872eSLisandro Dalcin 1854efe9872eSLisandro Dalcin Input Parameters: 1855efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1856efe9872eSLisandro Dalcin . u - the solution vector 1857efe9872eSLisandro Dalcin - v - the time derivative vector 1858efe9872eSLisandro Dalcin 1859efe9872eSLisandro Dalcin Level: beginner 1860efe9872eSLisandro Dalcin 1861efe9872eSLisandro Dalcin @*/ 1862efe9872eSLisandro Dalcin PetscErrorCode TS2SetSolution(TS ts,Vec u,Vec v) 1863efe9872eSLisandro Dalcin { 1864efe9872eSLisandro Dalcin PetscFunctionBegin; 1865efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1866efe9872eSLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 1867efe9872eSLisandro Dalcin PetscValidHeaderSpecific(v,VEC_CLASSID,3); 18689566063dSJacob Faibussowitsch PetscCall(TSSetSolution(ts,u)); 18699566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)v)); 18709566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_dot)); 1871efe9872eSLisandro Dalcin ts->vec_dot = v; 1872efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1873efe9872eSLisandro Dalcin } 1874efe9872eSLisandro Dalcin 1875efe9872eSLisandro Dalcin /*@ 1876efe9872eSLisandro Dalcin TS2GetSolution - Returns the solution and time derivative at the present timestep 1877efe9872eSLisandro Dalcin for second order equations. It is valid to call this routine inside the function 1878efe9872eSLisandro Dalcin that you are evaluating in order to move to the new timestep. This vector not 1879efe9872eSLisandro Dalcin changed until the solution at the next timestep has been calculated. 1880efe9872eSLisandro Dalcin 1881efe9872eSLisandro Dalcin Not Collective, but Vec returned is parallel if TS is parallel 1882efe9872eSLisandro Dalcin 1883efe9872eSLisandro Dalcin Input Parameter: 1884efe9872eSLisandro Dalcin . ts - the TS context obtained from TSCreate() 1885efe9872eSLisandro Dalcin 1886d8d19677SJose E. Roman Output Parameters: 1887efe9872eSLisandro Dalcin + u - the vector containing the solution 1888efe9872eSLisandro Dalcin - v - the vector containing the time derivative 1889efe9872eSLisandro Dalcin 1890efe9872eSLisandro Dalcin Level: intermediate 1891efe9872eSLisandro Dalcin 1892db781477SPatrick Sanan .seealso: `TS2SetSolution()`, `TSGetTimeStep()`, `TSGetTime()` 1893efe9872eSLisandro Dalcin 1894efe9872eSLisandro Dalcin @*/ 1895efe9872eSLisandro Dalcin PetscErrorCode TS2GetSolution(TS ts,Vec *u,Vec *v) 1896efe9872eSLisandro Dalcin { 1897efe9872eSLisandro Dalcin PetscFunctionBegin; 1898efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1899efe9872eSLisandro Dalcin if (u) PetscValidPointer(u,2); 1900efe9872eSLisandro Dalcin if (v) PetscValidPointer(v,3); 1901efe9872eSLisandro Dalcin if (u) *u = ts->vec_sol; 1902efe9872eSLisandro Dalcin if (v) *v = ts->vec_dot; 1903efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1904efe9872eSLisandro Dalcin } 1905efe9872eSLisandro Dalcin 190655849f57SBarry Smith /*@C 190755849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 190855849f57SBarry Smith 190955849f57SBarry Smith Collective on PetscViewer 191055849f57SBarry Smith 191155849f57SBarry Smith Input Parameters: 191255849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 191355849f57SBarry Smith some related function before a call to TSLoad(). 191455849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 191555849f57SBarry Smith 191655849f57SBarry Smith Level: intermediate 191755849f57SBarry Smith 191855849f57SBarry Smith Notes: 191955849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 192055849f57SBarry Smith 192155849f57SBarry Smith Notes for advanced users: 192255849f57SBarry Smith Most users should not need to know the details of the binary storage 192355849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 192455849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 192555849f57SBarry Smith format is 192655849f57SBarry Smith .vb 192755849f57SBarry Smith has not yet been determined 192855849f57SBarry Smith .ve 192955849f57SBarry Smith 1930db781477SPatrick Sanan .seealso: `PetscViewerBinaryOpen()`, `TSView()`, `MatLoad()`, `VecLoad()` 193155849f57SBarry Smith @*/ 1932f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 193355849f57SBarry Smith { 193455849f57SBarry Smith PetscBool isbinary; 193555849f57SBarry Smith PetscInt classid; 193655849f57SBarry Smith char type[256]; 19372d53ad75SBarry Smith DMTS sdm; 1938ad6bc421SBarry Smith DM dm; 193955849f57SBarry Smith 194055849f57SBarry Smith PetscFunctionBegin; 1941f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 194255849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 19439566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary)); 19443c633725SBarry Smith PetscCheck(isbinary,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 194555849f57SBarry Smith 19469566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT)); 19473c633725SBarry Smith PetscCheck(classid == TS_FILE_CLASSID,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 19489566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR)); 19499566063dSJacob Faibussowitsch PetscCall(TSSetType(ts, type)); 1950f2c2a1b9SBarry Smith if (ts->ops->load) { 19519566063dSJacob Faibussowitsch PetscCall((*ts->ops->load)(ts,viewer)); 1952f2c2a1b9SBarry Smith } 19539566063dSJacob Faibussowitsch PetscCall(DMCreate(PetscObjectComm((PetscObject)ts),&dm)); 19549566063dSJacob Faibussowitsch PetscCall(DMLoad(dm,viewer)); 19559566063dSJacob Faibussowitsch PetscCall(TSSetDM(ts,dm)); 19569566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(ts->dm,&ts->vec_sol)); 19579566063dSJacob Faibussowitsch PetscCall(VecLoad(ts->vec_sol,viewer)); 19589566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm,&sdm)); 19599566063dSJacob Faibussowitsch PetscCall(DMTSLoad(sdm,viewer)); 196055849f57SBarry Smith PetscFunctionReturn(0); 196155849f57SBarry Smith } 196255849f57SBarry Smith 19639804daf3SBarry Smith #include <petscdraw.h> 1964e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1965e04113cfSBarry Smith #include <petscviewersaws.h> 1966f05ece33SBarry Smith #endif 1967fe2efc57SMark 1968fe2efc57SMark /*@C 1969fe2efc57SMark TSViewFromOptions - View from Options 1970fe2efc57SMark 1971fe2efc57SMark Collective on TS 1972fe2efc57SMark 1973fe2efc57SMark Input Parameters: 1974fe2efc57SMark + A - the application ordering context 1975736c3998SJose E. Roman . obj - Optional object 1976736c3998SJose E. Roman - name - command line option 1977fe2efc57SMark 1978fe2efc57SMark Level: intermediate 1979db781477SPatrick Sanan .seealso: `TS`, `TSView`, `PetscObjectViewFromOptions()`, `TSCreate()` 1980fe2efc57SMark @*/ 1981fe2efc57SMark PetscErrorCode TSViewFromOptions(TS A,PetscObject obj,const char name[]) 1982fe2efc57SMark { 1983fe2efc57SMark PetscFunctionBegin; 1984fe2efc57SMark PetscValidHeaderSpecific(A,TS_CLASSID,1); 19859566063dSJacob Faibussowitsch PetscCall(PetscObjectViewFromOptions((PetscObject)A,obj,name)); 1986fe2efc57SMark PetscFunctionReturn(0); 1987fe2efc57SMark } 1988fe2efc57SMark 19897e2c5f70SBarry Smith /*@C 1990d763cef2SBarry Smith TSView - Prints the TS data structure. 1991d763cef2SBarry Smith 19924c49b128SBarry Smith Collective on TS 1993d763cef2SBarry Smith 1994d763cef2SBarry Smith Input Parameters: 1995d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1996d763cef2SBarry Smith - viewer - visualization context 1997d763cef2SBarry Smith 1998d763cef2SBarry Smith Options Database Key: 1999d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 2000d763cef2SBarry Smith 2001d763cef2SBarry Smith Notes: 2002d763cef2SBarry Smith The available visualization contexts include 2003b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 2004b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 2005d763cef2SBarry Smith output where only the first processor opens 2006d763cef2SBarry Smith the file. All other processors send their 2007d763cef2SBarry Smith data to the first processor to print. 2008d763cef2SBarry Smith 2009d763cef2SBarry Smith The user can open an alternative visualization context with 2010b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 2011d763cef2SBarry Smith 2012595c91d4SBarry Smith In the debugger you can do "call TSView(ts,0)" to display the TS solver. (The same holds for any PETSc object viewer). 2013595c91d4SBarry Smith 2014d763cef2SBarry Smith Level: beginner 2015d763cef2SBarry Smith 2016db781477SPatrick Sanan .seealso: `PetscViewerASCIIOpen()` 2017d763cef2SBarry Smith @*/ 20187087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 2019d763cef2SBarry Smith { 202019fd82e9SBarry Smith TSType type; 20212b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 20222d53ad75SBarry Smith DMTS sdm; 2023e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 2024536b137fSBarry Smith PetscBool issaws; 2025f05ece33SBarry Smith #endif 2026d763cef2SBarry Smith 2027d763cef2SBarry Smith PetscFunctionBegin; 20280700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20293050cee2SBarry Smith if (!viewer) { 20309566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer)); 20313050cee2SBarry Smith } 20320700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 2033c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 2034fd16b177SBarry Smith 20359566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii)); 20369566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring)); 20379566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary)); 20389566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw)); 2039e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 20409566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws)); 2041f05ece33SBarry Smith #endif 204232077d6dSBarry Smith if (iascii) { 20439566063dSJacob Faibussowitsch PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer)); 2044efd4aadfSBarry Smith if (ts->ops->view) { 20459566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 20469566063dSJacob Faibussowitsch PetscCall((*ts->ops->view)(ts,viewer)); 20479566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 2048efd4aadfSBarry Smith } 2049ef85077eSLisandro Dalcin if (ts->max_steps < PETSC_MAX_INT) { 205063a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," maximum steps=%" PetscInt_FMT "\n",ts->max_steps)); 2051ef85077eSLisandro Dalcin } 2052ef85077eSLisandro Dalcin if (ts->max_time < PETSC_MAX_REAL) { 20539566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time)); 2054ef85077eSLisandro Dalcin } 2055a6ab3590SBarry Smith if (ts->ifuncs) { 205663a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of I function evaluations=%" PetscInt_FMT "\n",ts->ifuncs)); 2057a6ab3590SBarry Smith } 2058a6ab3590SBarry Smith if (ts->ijacs) { 205963a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of I Jacobian evaluations=%" PetscInt_FMT "\n",ts->ijacs)); 2060a6ab3590SBarry Smith } 2061a6ab3590SBarry Smith if (ts->rhsfuncs) { 206263a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of RHS function evaluations=%" PetscInt_FMT "\n",ts->rhsfuncs)); 2063a6ab3590SBarry Smith } 2064a6ab3590SBarry Smith if (ts->rhsjacs) { 206563a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of RHS Jacobian evaluations=%" PetscInt_FMT "\n",ts->rhsjacs)); 2066a6ab3590SBarry Smith } 2067efd4aadfSBarry Smith if (ts->usessnes) { 2068efd4aadfSBarry Smith PetscBool lin; 2069d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 207063a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%" PetscInt_FMT "\n",ts->snes_its)); 2071d763cef2SBarry Smith } 207263a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%" PetscInt_FMT "\n",ts->ksp_its)); 20739566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"")); 207463a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of %slinear solve failures=%" PetscInt_FMT "\n",lin ? "" : "non",ts->num_snes_failures)); 2075efd4aadfSBarry Smith } 207663a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of rejected steps=%" PetscInt_FMT "\n",ts->reject)); 2077a0af407cSBarry Smith if (ts->vrtol) { 20789566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," using vector of relative error tolerances, ")); 2079a0af407cSBarry Smith } else { 20809566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," using relative error tolerance of %g, ",(double)ts->rtol)); 2081a0af407cSBarry Smith } 2082a0af407cSBarry Smith if (ts->vatol) { 20839566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," using vector of absolute error tolerances\n")); 2084a0af407cSBarry Smith } else { 20859566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," using absolute error tolerance of %g\n",(double)ts->atol)); 2086a0af407cSBarry Smith } 20879566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 20889566063dSJacob Faibussowitsch PetscCall(TSAdaptView(ts->adapt,viewer)); 20899566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 20900f5bd95cSBarry Smith } else if (isstring) { 20919566063dSJacob Faibussowitsch PetscCall(TSGetType(ts,&type)); 20929566063dSJacob Faibussowitsch PetscCall(PetscViewerStringSPrintf(viewer," TSType: %-7.7s",type)); 20939566063dSJacob Faibussowitsch if (ts->ops->view) PetscCall((*ts->ops->view)(ts,viewer)); 209455849f57SBarry Smith } else if (isbinary) { 209555849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 209655849f57SBarry Smith MPI_Comm comm; 209755849f57SBarry Smith PetscMPIInt rank; 209855849f57SBarry Smith char type[256]; 209955849f57SBarry Smith 21009566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)ts,&comm)); 21019566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm,&rank)); 2102dd400576SPatrick Sanan if (rank == 0) { 21039566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT)); 21049566063dSJacob Faibussowitsch PetscCall(PetscStrncpy(type,((PetscObject)ts)->type_name,256)); 21059566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR)); 210655849f57SBarry Smith } 210755849f57SBarry Smith if (ts->ops->view) { 21089566063dSJacob Faibussowitsch PetscCall((*ts->ops->view)(ts,viewer)); 210955849f57SBarry Smith } 21109566063dSJacob Faibussowitsch if (ts->adapt) PetscCall(TSAdaptView(ts->adapt,viewer)); 21119566063dSJacob Faibussowitsch PetscCall(DMView(ts->dm,viewer)); 21129566063dSJacob Faibussowitsch PetscCall(VecView(ts->vec_sol,viewer)); 21139566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm,&sdm)); 21149566063dSJacob Faibussowitsch PetscCall(DMTSView(sdm,viewer)); 21152b0a91c0SBarry Smith } else if (isdraw) { 21162b0a91c0SBarry Smith PetscDraw draw; 21172b0a91c0SBarry Smith char str[36]; 211889fd9fafSBarry Smith PetscReal x,y,bottom,h; 21192b0a91c0SBarry Smith 21209566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(viewer,0,&draw)); 21219566063dSJacob Faibussowitsch PetscCall(PetscDrawGetCurrentPoint(draw,&x,&y)); 21229566063dSJacob Faibussowitsch PetscCall(PetscStrcpy(str,"TS: ")); 21239566063dSJacob Faibussowitsch PetscCall(PetscStrcat(str,((PetscObject)ts)->type_name)); 21249566063dSJacob Faibussowitsch PetscCall(PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h)); 212589fd9fafSBarry Smith bottom = y - h; 21269566063dSJacob Faibussowitsch PetscCall(PetscDrawPushCurrentPoint(draw,x,bottom)); 21272b0a91c0SBarry Smith if (ts->ops->view) { 21289566063dSJacob Faibussowitsch PetscCall((*ts->ops->view)(ts,viewer)); 21292b0a91c0SBarry Smith } 21309566063dSJacob Faibussowitsch if (ts->adapt) PetscCall(TSAdaptView(ts->adapt,viewer)); 21319566063dSJacob Faibussowitsch if (ts->snes) PetscCall(SNESView(ts->snes,viewer)); 21329566063dSJacob Faibussowitsch PetscCall(PetscDrawPopCurrentPoint(draw)); 2133e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 2134536b137fSBarry Smith } else if (issaws) { 2135d45a07a7SBarry Smith PetscMPIInt rank; 21362657e9d9SBarry Smith const char *name; 21372657e9d9SBarry Smith 21389566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)ts,&name)); 21399566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); 2140dd400576SPatrick Sanan if (!((PetscObject)ts)->amsmem && rank == 0) { 2141d45a07a7SBarry Smith char dir[1024]; 2142d45a07a7SBarry Smith 21439566063dSJacob Faibussowitsch PetscCall(PetscObjectViewSAWs((PetscObject)ts,viewer)); 21449566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name)); 21452657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 21469566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name)); 21472657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 2148d763cef2SBarry Smith } 21490acecf5bSBarry Smith if (ts->ops->view) { 21509566063dSJacob Faibussowitsch PetscCall((*ts->ops->view)(ts,viewer)); 21510acecf5bSBarry Smith } 2152f05ece33SBarry Smith #endif 2153f05ece33SBarry Smith } 215436a9e3b9SBarry Smith if (ts->snes && ts->usessnes) { 21559566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 21569566063dSJacob Faibussowitsch PetscCall(SNESView(ts->snes,viewer)); 21579566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 215836a9e3b9SBarry Smith } 21599566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm,&sdm)); 21609566063dSJacob Faibussowitsch PetscCall(DMTSView(sdm,viewer)); 2161f05ece33SBarry Smith 21629566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 21639566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials)); 21649566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 2165d763cef2SBarry Smith PetscFunctionReturn(0); 2166d763cef2SBarry Smith } 2167d763cef2SBarry Smith 2168b07ff414SBarry Smith /*@ 2169d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 2170d763cef2SBarry Smith the timesteppers. 2171d763cef2SBarry Smith 21723f9fe445SBarry Smith Logically Collective on TS 2173d763cef2SBarry Smith 2174d763cef2SBarry Smith Input Parameters: 2175d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2176d763cef2SBarry Smith - usrP - optional user context 2177d763cef2SBarry Smith 217895452b02SPatrick Sanan Fortran Notes: 217995452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 2180daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2181daf670e6SBarry Smith 2182d763cef2SBarry Smith Level: intermediate 2183d763cef2SBarry Smith 2184db781477SPatrick Sanan .seealso: `TSGetApplicationContext()` 2185d763cef2SBarry Smith @*/ 21867087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 2187d763cef2SBarry Smith { 2188d763cef2SBarry Smith PetscFunctionBegin; 21890700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2190d763cef2SBarry Smith ts->user = usrP; 2191d763cef2SBarry Smith PetscFunctionReturn(0); 2192d763cef2SBarry Smith } 2193d763cef2SBarry Smith 2194b07ff414SBarry Smith /*@ 2195d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 2196d763cef2SBarry Smith timestepper. 2197d763cef2SBarry Smith 2198d763cef2SBarry Smith Not Collective 2199d763cef2SBarry Smith 2200d763cef2SBarry Smith Input Parameter: 2201d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2202d763cef2SBarry Smith 2203d763cef2SBarry Smith Output Parameter: 2204d763cef2SBarry Smith . usrP - user context 2205d763cef2SBarry Smith 220695452b02SPatrick Sanan Fortran Notes: 220795452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 2208daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2209daf670e6SBarry Smith 2210d763cef2SBarry Smith Level: intermediate 2211d763cef2SBarry Smith 2212db781477SPatrick Sanan .seealso: `TSSetApplicationContext()` 2213d763cef2SBarry Smith @*/ 2214e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 2215d763cef2SBarry Smith { 2216d763cef2SBarry Smith PetscFunctionBegin; 22170700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2218e71120c6SJed Brown *(void**)usrP = ts->user; 2219d763cef2SBarry Smith PetscFunctionReturn(0); 2220d763cef2SBarry Smith } 2221d763cef2SBarry Smith 2222d763cef2SBarry Smith /*@ 222380275a0aSLisandro Dalcin TSGetStepNumber - Gets the number of steps completed. 2224d763cef2SBarry Smith 2225d763cef2SBarry Smith Not Collective 2226d763cef2SBarry Smith 2227d763cef2SBarry Smith Input Parameter: 2228d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2229d763cef2SBarry Smith 2230d763cef2SBarry Smith Output Parameter: 223180275a0aSLisandro Dalcin . steps - number of steps completed so far 2232d763cef2SBarry Smith 2233d763cef2SBarry Smith Level: intermediate 2234d763cef2SBarry Smith 2235db781477SPatrick Sanan .seealso: `TSGetTime()`, `TSGetTimeStep()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostStage()`, `TSSetPostStep()` 2236d763cef2SBarry Smith @*/ 223780275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps) 2238d763cef2SBarry Smith { 2239d763cef2SBarry Smith PetscFunctionBegin; 22400700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 224180275a0aSLisandro Dalcin PetscValidIntPointer(steps,2); 224280275a0aSLisandro Dalcin *steps = ts->steps; 224380275a0aSLisandro Dalcin PetscFunctionReturn(0); 224480275a0aSLisandro Dalcin } 224580275a0aSLisandro Dalcin 224680275a0aSLisandro Dalcin /*@ 224780275a0aSLisandro Dalcin TSSetStepNumber - Sets the number of steps completed. 224880275a0aSLisandro Dalcin 224980275a0aSLisandro Dalcin Logically Collective on TS 225080275a0aSLisandro Dalcin 225180275a0aSLisandro Dalcin Input Parameters: 225280275a0aSLisandro Dalcin + ts - the TS context 225380275a0aSLisandro Dalcin - steps - number of steps completed so far 225480275a0aSLisandro Dalcin 225580275a0aSLisandro Dalcin Notes: 225680275a0aSLisandro Dalcin For most uses of the TS solvers the user need not explicitly call 225780275a0aSLisandro Dalcin TSSetStepNumber(), as the step counter is appropriately updated in 225880275a0aSLisandro Dalcin TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to 225980275a0aSLisandro Dalcin reinitialize timestepping by setting the step counter to zero (and time 226080275a0aSLisandro Dalcin to the initial time) to solve a similar problem with different initial 226180275a0aSLisandro Dalcin conditions or parameters. Other possible use case is to continue 226280275a0aSLisandro Dalcin timestepping from a previously interrupted run in such a way that TS 226380275a0aSLisandro Dalcin monitors will be called with a initial nonzero step counter. 226480275a0aSLisandro Dalcin 226580275a0aSLisandro Dalcin Level: advanced 226680275a0aSLisandro Dalcin 2267db781477SPatrick Sanan .seealso: `TSGetStepNumber()`, `TSSetTime()`, `TSSetTimeStep()`, `TSSetSolution()` 226880275a0aSLisandro Dalcin @*/ 226980275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps) 227080275a0aSLisandro Dalcin { 227180275a0aSLisandro Dalcin PetscFunctionBegin; 227280275a0aSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 227380275a0aSLisandro Dalcin PetscValidLogicalCollectiveInt(ts,steps,2); 22743c633725SBarry Smith PetscCheck(steps >= 0,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative"); 227580275a0aSLisandro Dalcin ts->steps = steps; 2276d763cef2SBarry Smith PetscFunctionReturn(0); 2277d763cef2SBarry Smith } 2278d763cef2SBarry Smith 2279d763cef2SBarry Smith /*@ 2280d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 2281d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 2282d763cef2SBarry Smith 22833f9fe445SBarry Smith Logically Collective on TS 2284d763cef2SBarry Smith 2285d763cef2SBarry Smith Input Parameters: 2286d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2287d763cef2SBarry Smith - time_step - the size of the timestep 2288d763cef2SBarry Smith 2289d763cef2SBarry Smith Level: intermediate 2290d763cef2SBarry Smith 2291db781477SPatrick Sanan .seealso: `TSGetTimeStep()`, `TSSetTime()` 2292d763cef2SBarry Smith 2293d763cef2SBarry Smith @*/ 22947087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 2295d763cef2SBarry Smith { 2296d763cef2SBarry Smith PetscFunctionBegin; 22970700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2298c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 2299d763cef2SBarry Smith ts->time_step = time_step; 2300d763cef2SBarry Smith PetscFunctionReturn(0); 2301d763cef2SBarry Smith } 2302d763cef2SBarry Smith 2303a43b19c4SJed Brown /*@ 230449354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 230549354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 230649354f04SShri Abhyankar or just return at the final time TS computed. 2307a43b19c4SJed Brown 2308a43b19c4SJed Brown Logically Collective on TS 2309a43b19c4SJed Brown 2310d8d19677SJose E. Roman Input Parameters: 2311a43b19c4SJed Brown + ts - the time-step context 231249354f04SShri Abhyankar - eftopt - exact final time option 2313a43b19c4SJed Brown 2314feed9e9dSBarry Smith $ TS_EXACTFINALTIME_STEPOVER - Don't do anything if final time is exceeded 2315feed9e9dSBarry Smith $ TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time 2316feed9e9dSBarry Smith $ TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time 2317feed9e9dSBarry Smith 2318feed9e9dSBarry Smith Options Database: 2319feed9e9dSBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime 2320feed9e9dSBarry Smith 2321ee346746SBarry Smith Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time 2322ee346746SBarry Smith then the final time you selected. 2323ee346746SBarry Smith 2324a43b19c4SJed Brown Level: beginner 2325a43b19c4SJed Brown 2326db781477SPatrick Sanan .seealso: `TSExactFinalTimeOption`, `TSGetExactFinalTime()` 2327a43b19c4SJed Brown @*/ 232849354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 2329a43b19c4SJed Brown { 2330a43b19c4SJed Brown PetscFunctionBegin; 2331a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 233249354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 233349354f04SShri Abhyankar ts->exact_final_time = eftopt; 2334a43b19c4SJed Brown PetscFunctionReturn(0); 2335a43b19c4SJed Brown } 2336a43b19c4SJed Brown 2337d763cef2SBarry Smith /*@ 2338f6953c82SLisandro Dalcin TSGetExactFinalTime - Gets the exact final time option. 2339f6953c82SLisandro Dalcin 2340f6953c82SLisandro Dalcin Not Collective 2341f6953c82SLisandro Dalcin 2342f6953c82SLisandro Dalcin Input Parameter: 2343f6953c82SLisandro Dalcin . ts - the TS context 2344f6953c82SLisandro Dalcin 2345f6953c82SLisandro Dalcin Output Parameter: 2346f6953c82SLisandro Dalcin . eftopt - exact final time option 2347f6953c82SLisandro Dalcin 2348f6953c82SLisandro Dalcin Level: beginner 2349f6953c82SLisandro Dalcin 2350db781477SPatrick Sanan .seealso: `TSExactFinalTimeOption`, `TSSetExactFinalTime()` 2351f6953c82SLisandro Dalcin @*/ 2352f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt) 2353f6953c82SLisandro Dalcin { 2354f6953c82SLisandro Dalcin PetscFunctionBegin; 2355f6953c82SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2356f6953c82SLisandro Dalcin PetscValidPointer(eftopt,2); 2357f6953c82SLisandro Dalcin *eftopt = ts->exact_final_time; 2358f6953c82SLisandro Dalcin PetscFunctionReturn(0); 2359f6953c82SLisandro Dalcin } 2360f6953c82SLisandro Dalcin 2361f6953c82SLisandro Dalcin /*@ 2362d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 2363d763cef2SBarry Smith 2364d763cef2SBarry Smith Not Collective 2365d763cef2SBarry Smith 2366d763cef2SBarry Smith Input Parameter: 2367d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2368d763cef2SBarry Smith 2369d763cef2SBarry Smith Output Parameter: 2370d763cef2SBarry Smith . dt - the current timestep size 2371d763cef2SBarry Smith 2372d763cef2SBarry Smith Level: intermediate 2373d763cef2SBarry Smith 2374db781477SPatrick Sanan .seealso: `TSSetTimeStep()`, `TSGetTime()` 2375d763cef2SBarry Smith 2376d763cef2SBarry Smith @*/ 23777087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 2378d763cef2SBarry Smith { 2379d763cef2SBarry Smith PetscFunctionBegin; 23800700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2381f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 2382d763cef2SBarry Smith *dt = ts->time_step; 2383d763cef2SBarry Smith PetscFunctionReturn(0); 2384d763cef2SBarry Smith } 2385d763cef2SBarry Smith 2386d8e5e3e6SSatish Balay /*@ 2387d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 2388d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 2389d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 2390d763cef2SBarry Smith the solution at the next timestep has been calculated. 2391d763cef2SBarry Smith 2392d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 2393d763cef2SBarry Smith 2394d763cef2SBarry Smith Input Parameter: 2395d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2396d763cef2SBarry Smith 2397d763cef2SBarry Smith Output Parameter: 2398d763cef2SBarry Smith . v - the vector containing the solution 2399d763cef2SBarry Smith 240063e21af5SBarry Smith Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested 240163e21af5SBarry Smith final time. It returns the solution at the next timestep. 240263e21af5SBarry Smith 2403d763cef2SBarry Smith Level: intermediate 2404d763cef2SBarry Smith 2405db781477SPatrick Sanan .seealso: `TSGetTimeStep()`, `TSGetTime()`, `TSGetSolveTime()`, `TSGetSolutionComponents()`, `TSSetSolutionFunction()` 2406d763cef2SBarry Smith 2407d763cef2SBarry Smith @*/ 24087087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 2409d763cef2SBarry Smith { 2410d763cef2SBarry Smith PetscFunctionBegin; 24110700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 24124482741eSBarry Smith PetscValidPointer(v,2); 24138737fe31SLisandro Dalcin *v = ts->vec_sol; 2414d763cef2SBarry Smith PetscFunctionReturn(0); 2415d763cef2SBarry Smith } 2416d763cef2SBarry Smith 241703fe5f5eSDebojyoti Ghosh /*@ 2418b2bf4f3aSDebojyoti Ghosh TSGetSolutionComponents - Returns any solution components at the present 241903fe5f5eSDebojyoti Ghosh timestep, if available for the time integration method being used. 2420b2bf4f3aSDebojyoti Ghosh Solution components are quantities that share the same size and 242103fe5f5eSDebojyoti Ghosh structure as the solution vector. 242203fe5f5eSDebojyoti Ghosh 242303fe5f5eSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 242403fe5f5eSDebojyoti Ghosh 242503fe5f5eSDebojyoti Ghosh Parameters : 2426a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2427b2bf4f3aSDebojyoti Ghosh . n - If v is PETSC_NULL, then the number of solution components is 2428b2bf4f3aSDebojyoti Ghosh returned through n, else the n-th solution component is 242903fe5f5eSDebojyoti Ghosh returned in v. 2430a2b725a8SWilliam Gropp - v - the vector containing the n-th solution component 243103fe5f5eSDebojyoti Ghosh (may be PETSC_NULL to use this function to find out 2432b2bf4f3aSDebojyoti Ghosh the number of solutions components). 243303fe5f5eSDebojyoti Ghosh 24344cdd57e5SDebojyoti Ghosh Level: advanced 243503fe5f5eSDebojyoti Ghosh 2436db781477SPatrick Sanan .seealso: `TSGetSolution()` 243703fe5f5eSDebojyoti Ghosh 243803fe5f5eSDebojyoti Ghosh @*/ 2439b2bf4f3aSDebojyoti Ghosh PetscErrorCode TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v) 244003fe5f5eSDebojyoti Ghosh { 244103fe5f5eSDebojyoti Ghosh PetscFunctionBegin; 244203fe5f5eSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2443b2bf4f3aSDebojyoti Ghosh if (!ts->ops->getsolutioncomponents) *n = 0; 244403fe5f5eSDebojyoti Ghosh else { 24459566063dSJacob Faibussowitsch PetscCall((*ts->ops->getsolutioncomponents)(ts,n,v)); 244603fe5f5eSDebojyoti Ghosh } 244703fe5f5eSDebojyoti Ghosh PetscFunctionReturn(0); 244803fe5f5eSDebojyoti Ghosh } 244903fe5f5eSDebojyoti Ghosh 24504cdd57e5SDebojyoti Ghosh /*@ 24514cdd57e5SDebojyoti Ghosh TSGetAuxSolution - Returns an auxiliary solution at the present 24524cdd57e5SDebojyoti Ghosh timestep, if available for the time integration method being used. 24534cdd57e5SDebojyoti Ghosh 24544cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 24554cdd57e5SDebojyoti Ghosh 24564cdd57e5SDebojyoti Ghosh Parameters : 2457a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2458a2b725a8SWilliam Gropp - v - the vector containing the auxiliary solution 24594cdd57e5SDebojyoti Ghosh 24604cdd57e5SDebojyoti Ghosh Level: intermediate 24614cdd57e5SDebojyoti Ghosh 2462db781477SPatrick Sanan .seealso: `TSGetSolution()` 24634cdd57e5SDebojyoti Ghosh 24644cdd57e5SDebojyoti Ghosh @*/ 24654cdd57e5SDebojyoti Ghosh PetscErrorCode TSGetAuxSolution(TS ts,Vec *v) 24664cdd57e5SDebojyoti Ghosh { 24674cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 24684cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2469f6356ec7SDebojyoti Ghosh if (ts->ops->getauxsolution) { 24709566063dSJacob Faibussowitsch PetscCall((*ts->ops->getauxsolution)(ts,v)); 2471f6356ec7SDebojyoti Ghosh } else { 24729566063dSJacob Faibussowitsch PetscCall(VecZeroEntries(*v)); 2473f6356ec7SDebojyoti Ghosh } 24744cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 24754cdd57e5SDebojyoti Ghosh } 24764cdd57e5SDebojyoti Ghosh 24774cdd57e5SDebojyoti Ghosh /*@ 24784cdd57e5SDebojyoti Ghosh TSGetTimeError - Returns the estimated error vector, if the chosen 24794cdd57e5SDebojyoti Ghosh TSType has an error estimation functionality. 24804cdd57e5SDebojyoti Ghosh 24814cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 24824cdd57e5SDebojyoti Ghosh 24839657682dSDebojyoti Ghosh Note: MUST call after TSSetUp() 24849657682dSDebojyoti Ghosh 24854cdd57e5SDebojyoti Ghosh Parameters : 2486a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2487657c1e31SEmil Constantinescu . n - current estimate (n=0) or previous one (n=-1) 2488a2b725a8SWilliam Gropp - v - the vector containing the error (same size as the solution). 24894cdd57e5SDebojyoti Ghosh 24904cdd57e5SDebojyoti Ghosh Level: intermediate 24914cdd57e5SDebojyoti Ghosh 2492db781477SPatrick Sanan .seealso: `TSGetSolution()`, `TSSetTimeError()` 24934cdd57e5SDebojyoti Ghosh 24944cdd57e5SDebojyoti Ghosh @*/ 24950a01e1b2SEmil Constantinescu PetscErrorCode TSGetTimeError(TS ts,PetscInt n,Vec *v) 24964cdd57e5SDebojyoti Ghosh { 24974cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 24984cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2499f6356ec7SDebojyoti Ghosh if (ts->ops->gettimeerror) { 25009566063dSJacob Faibussowitsch PetscCall((*ts->ops->gettimeerror)(ts,n,v)); 2501f6356ec7SDebojyoti Ghosh } else { 25029566063dSJacob Faibussowitsch PetscCall(VecZeroEntries(*v)); 2503f6356ec7SDebojyoti Ghosh } 25044cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 25054cdd57e5SDebojyoti Ghosh } 25064cdd57e5SDebojyoti Ghosh 250757df6a1bSDebojyoti Ghosh /*@ 250857df6a1bSDebojyoti Ghosh TSSetTimeError - Sets the estimated error vector, if the chosen 250957df6a1bSDebojyoti Ghosh TSType has an error estimation functionality. This can be used 251057df6a1bSDebojyoti Ghosh to restart such a time integrator with a given error vector. 251157df6a1bSDebojyoti Ghosh 251257df6a1bSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 251357df6a1bSDebojyoti Ghosh 251457df6a1bSDebojyoti Ghosh Parameters : 2515a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2516a2b725a8SWilliam Gropp - v - the vector containing the error (same size as the solution). 251757df6a1bSDebojyoti Ghosh 251857df6a1bSDebojyoti Ghosh Level: intermediate 251957df6a1bSDebojyoti Ghosh 2520db781477SPatrick Sanan .seealso: `TSSetSolution()`, `TSGetTimeError)` 252157df6a1bSDebojyoti Ghosh 252257df6a1bSDebojyoti Ghosh @*/ 252357df6a1bSDebojyoti Ghosh PetscErrorCode TSSetTimeError(TS ts,Vec v) 252457df6a1bSDebojyoti Ghosh { 252557df6a1bSDebojyoti Ghosh PetscFunctionBegin; 252657df6a1bSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 25273c633725SBarry Smith PetscCheck(ts->setupcalled,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first"); 252857df6a1bSDebojyoti Ghosh if (ts->ops->settimeerror) { 25299566063dSJacob Faibussowitsch PetscCall((*ts->ops->settimeerror)(ts,v)); 253057df6a1bSDebojyoti Ghosh } 253157df6a1bSDebojyoti Ghosh PetscFunctionReturn(0); 253257df6a1bSDebojyoti Ghosh } 253357df6a1bSDebojyoti Ghosh 2534bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 2535d8e5e3e6SSatish Balay /*@ 2536bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 2537d763cef2SBarry Smith 2538bdad233fSMatthew Knepley Not collective 2539d763cef2SBarry Smith 2540bdad233fSMatthew Knepley Input Parameters: 2541bdad233fSMatthew Knepley + ts - The TS 2542bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2543d763cef2SBarry Smith .vb 25440910c330SBarry Smith U_t - A U = 0 (linear) 25450910c330SBarry Smith U_t - A(t) U = 0 (linear) 25460910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 2547d763cef2SBarry Smith .ve 2548d763cef2SBarry Smith 2549d763cef2SBarry Smith Level: beginner 2550d763cef2SBarry Smith 2551db781477SPatrick Sanan .seealso: `TSSetUp()`, `TSProblemType`, `TS` 2552d763cef2SBarry Smith @*/ 25537087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 2554a7cc72afSBarry Smith { 2555d763cef2SBarry Smith PetscFunctionBegin; 25560700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2557bdad233fSMatthew Knepley ts->problem_type = type; 25589e2a6581SJed Brown if (type == TS_LINEAR) { 25599e2a6581SJed Brown SNES snes; 25609566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 25619566063dSJacob Faibussowitsch PetscCall(SNESSetType(snes,SNESKSPONLY)); 25629e2a6581SJed Brown } 2563d763cef2SBarry Smith PetscFunctionReturn(0); 2564d763cef2SBarry Smith } 2565d763cef2SBarry Smith 2566bdad233fSMatthew Knepley /*@C 2567bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 2568bdad233fSMatthew Knepley 2569bdad233fSMatthew Knepley Not collective 2570bdad233fSMatthew Knepley 2571bdad233fSMatthew Knepley Input Parameter: 2572bdad233fSMatthew Knepley . ts - The TS 2573bdad233fSMatthew Knepley 2574bdad233fSMatthew Knepley Output Parameter: 2575bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2576bdad233fSMatthew Knepley .vb 2577089b2837SJed Brown M U_t = A U 2578089b2837SJed Brown M(t) U_t = A(t) U 2579b5abc632SBarry Smith F(t,U,U_t) 2580bdad233fSMatthew Knepley .ve 2581bdad233fSMatthew Knepley 2582bdad233fSMatthew Knepley Level: beginner 2583bdad233fSMatthew Knepley 2584db781477SPatrick Sanan .seealso: `TSSetUp()`, `TSProblemType`, `TS` 2585bdad233fSMatthew Knepley @*/ 25867087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 2587a7cc72afSBarry Smith { 2588bdad233fSMatthew Knepley PetscFunctionBegin; 25890700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 25904482741eSBarry Smith PetscValidIntPointer(type,2); 2591bdad233fSMatthew Knepley *type = ts->problem_type; 2592bdad233fSMatthew Knepley PetscFunctionReturn(0); 2593bdad233fSMatthew Knepley } 2594d763cef2SBarry Smith 2595303a5415SBarry Smith /* 2596303a5415SBarry 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() 2597303a5415SBarry Smith */ 2598303a5415SBarry Smith static PetscErrorCode TSSetExactFinalTimeDefault(TS ts) 2599303a5415SBarry Smith { 2600303a5415SBarry Smith PetscBool isnone; 2601303a5415SBarry Smith 2602303a5415SBarry Smith PetscFunctionBegin; 26039566063dSJacob Faibussowitsch PetscCall(TSGetAdapt(ts,&ts->adapt)); 26049566063dSJacob Faibussowitsch PetscCall(TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type)); 2605303a5415SBarry Smith 26069566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone)); 2607303a5415SBarry Smith if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) { 2608303a5415SBarry Smith ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP; 2609303a5415SBarry Smith } else if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) { 2610303a5415SBarry Smith ts->exact_final_time = TS_EXACTFINALTIME_INTERPOLATE; 2611303a5415SBarry Smith } 2612303a5415SBarry Smith PetscFunctionReturn(0); 2613303a5415SBarry Smith } 2614303a5415SBarry Smith 2615d763cef2SBarry Smith /*@ 2616303a5415SBarry Smith TSSetUp - Sets up the internal data structures for the later use of a timestepper. 2617d763cef2SBarry Smith 2618d763cef2SBarry Smith Collective on TS 2619d763cef2SBarry Smith 2620d763cef2SBarry Smith Input Parameter: 2621d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2622d763cef2SBarry Smith 2623d763cef2SBarry Smith Notes: 2624d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 2625d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 2626141bd67dSStefano Zampini the call to TSStep() or TSSolve(). However, if one wishes to control this 2627d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 2628141bd67dSStefano Zampini and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve(). 2629d763cef2SBarry Smith 2630d763cef2SBarry Smith Level: advanced 2631d763cef2SBarry Smith 2632db781477SPatrick Sanan .seealso: `TSCreate()`, `TSStep()`, `TSDestroy()`, `TSSolve()` 2633d763cef2SBarry Smith @*/ 26347087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 2635d763cef2SBarry Smith { 26366c6b9e74SPeter Brune DM dm; 26376c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 2638d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 2639cd11d68dSLisandro Dalcin TSIFunction ifun; 26406c6b9e74SPeter Brune TSIJacobian ijac; 2641efe9872eSLisandro Dalcin TSI2Jacobian i2jac; 26426c6b9e74SPeter Brune TSRHSJacobian rhsjac; 2643d763cef2SBarry Smith 2644d763cef2SBarry Smith PetscFunctionBegin; 26450700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2646277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 2647277b19d0SLisandro Dalcin 26487adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 26499566063dSJacob Faibussowitsch PetscCall(TSGetIFunction(ts,NULL,&ifun,NULL)); 26509566063dSJacob Faibussowitsch PetscCall(TSSetType(ts,ifun ? TSBEULER : TSEULER)); 2651d763cef2SBarry Smith } 2652277b19d0SLisandro Dalcin 26531a638600SStefano Zampini if (!ts->vec_sol) { 26541a638600SStefano Zampini if (ts->dm) { 26559566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(ts->dm,&ts->vec_sol)); 26561a638600SStefano Zampini } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 26571a638600SStefano Zampini } 2658277b19d0SLisandro Dalcin 26594a658b32SHong Zhang if (ts->tspan) { 26604a658b32SHong Zhang if (!ts->tspan->vecs_sol) { 26614a658b32SHong Zhang PetscCall(VecDuplicateVecs(ts->vec_sol,ts->tspan->num_span_times,&ts->tspan->vecs_sol)); 26624a658b32SHong Zhang } 26634a658b32SHong Zhang } 2664298bade4SHong Zhang if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */ 26659566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)ts->Jacprhs)); 2666298bade4SHong Zhang ts->Jacp = ts->Jacprhs; 2667298bade4SHong Zhang } 2668298bade4SHong Zhang 2669cd4cee2dSHong Zhang if (ts->quadraturets) { 26709566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts->quadraturets)); 26719566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_costintegrand)); 26729566063dSJacob Faibussowitsch PetscCall(VecDuplicate(ts->quadraturets->vec_sol,&ts->vec_costintegrand)); 2673cd4cee2dSHong Zhang } 2674cd4cee2dSHong Zhang 26759566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL)); 2676f23ba4b3SHong Zhang if (rhsjac == TSComputeRHSJacobianConstant) { 2677e1244c69SJed Brown Mat Amat,Pmat; 2678e1244c69SJed Brown SNES snes; 26799566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 26809566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL)); 2681e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 2682e1244c69SJed Brown * have displaced the RHS matrix */ 2683971015bcSStefano Zampini if (Amat && Amat == ts->Arhs) { 2684abc0d4abSBarry 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 */ 26859566063dSJacob Faibussowitsch PetscCall(MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat)); 26869566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes,Amat,NULL,NULL,NULL)); 26879566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Amat)); 2688e1244c69SJed Brown } 2689971015bcSStefano Zampini if (Pmat && Pmat == ts->Brhs) { 26909566063dSJacob Faibussowitsch PetscCall(MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat)); 26919566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes,NULL,Pmat,NULL,NULL)); 26929566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Pmat)); 2693e1244c69SJed Brown } 2694e1244c69SJed Brown } 26952ffb9264SLisandro Dalcin 26969566063dSJacob Faibussowitsch PetscCall(TSGetAdapt(ts,&ts->adapt)); 26979566063dSJacob Faibussowitsch PetscCall(TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type)); 26982ffb9264SLisandro Dalcin 2699277b19d0SLisandro Dalcin if (ts->ops->setup) { 27009566063dSJacob Faibussowitsch PetscCall((*ts->ops->setup)(ts)); 2701277b19d0SLisandro Dalcin } 2702277b19d0SLisandro Dalcin 27039566063dSJacob Faibussowitsch PetscCall(TSSetExactFinalTimeDefault(ts)); 27042ffb9264SLisandro Dalcin 2705a6772fa2SLisandro Dalcin /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 27066c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 27076c6b9e74SPeter Brune */ 27089566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 27099566063dSJacob Faibussowitsch PetscCall(DMSNESGetFunction(dm,&func,NULL)); 27106c6b9e74SPeter Brune if (!func) { 27119566063dSJacob Faibussowitsch PetscCall(DMSNESSetFunction(dm,SNESTSFormFunction,ts)); 27126c6b9e74SPeter Brune } 2713a6772fa2SLisandro 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. 27146c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 27156c6b9e74SPeter Brune */ 27169566063dSJacob Faibussowitsch PetscCall(DMSNESGetJacobian(dm,&jac,NULL)); 27179566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm,&ijac,NULL)); 27189566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm,&i2jac,NULL)); 27199566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm,&rhsjac,NULL)); 2720efe9872eSLisandro Dalcin if (!jac && (ijac || i2jac || rhsjac)) { 27219566063dSJacob Faibussowitsch PetscCall(DMSNESSetJacobian(dm,SNESTSFormJacobian,ts)); 27226c6b9e74SPeter Brune } 2723c0517034SDebojyoti Ghosh 2724c0517034SDebojyoti Ghosh /* if time integration scheme has a starting method, call it */ 2725c0517034SDebojyoti Ghosh if (ts->ops->startingmethod) { 27269566063dSJacob Faibussowitsch PetscCall((*ts->ops->startingmethod)(ts)); 2727c0517034SDebojyoti Ghosh } 2728c0517034SDebojyoti Ghosh 2729277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 2730277b19d0SLisandro Dalcin PetscFunctionReturn(0); 2731277b19d0SLisandro Dalcin } 2732277b19d0SLisandro Dalcin 2733f6a906c0SBarry Smith /*@ 2734277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 2735277b19d0SLisandro Dalcin 2736277b19d0SLisandro Dalcin Collective on TS 2737277b19d0SLisandro Dalcin 2738277b19d0SLisandro Dalcin Input Parameter: 2739277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 2740277b19d0SLisandro Dalcin 2741277b19d0SLisandro Dalcin Level: beginner 2742277b19d0SLisandro Dalcin 2743db781477SPatrick Sanan .seealso: `TSCreate()`, `TSSetup()`, `TSDestroy()` 2744277b19d0SLisandro Dalcin @*/ 2745277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 2746277b19d0SLisandro Dalcin { 27471d06f6b3SHong Zhang TS_RHSSplitLink ilink = ts->tsrhssplit,next; 2748277b19d0SLisandro Dalcin 2749277b19d0SLisandro Dalcin PetscFunctionBegin; 2750277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2751b18ea86cSHong Zhang 2752277b19d0SLisandro Dalcin if (ts->ops->reset) { 27539566063dSJacob Faibussowitsch PetscCall((*ts->ops->reset)(ts)); 2754277b19d0SLisandro Dalcin } 27559566063dSJacob Faibussowitsch if (ts->snes) PetscCall(SNESReset(ts->snes)); 27569566063dSJacob Faibussowitsch if (ts->adapt) PetscCall(TSAdaptReset(ts->adapt)); 2757bbd56ea5SKarl Rupp 27589566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Arhs)); 27599566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Brhs)); 27609566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->Frhs)); 27619566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_sol)); 27629566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_dot)); 27639566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vatol)); 27649566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vrtol)); 27659566063dSJacob Faibussowitsch PetscCall(VecDestroyVecs(ts->nwork,&ts->work)); 2766bbd56ea5SKarl Rupp 27679566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Jacprhs)); 27689566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Jacp)); 2769ecf68647SHong Zhang if (ts->forward_solve) { 27709566063dSJacob Faibussowitsch PetscCall(TSForwardReset(ts)); 2771ecf68647SHong Zhang } 2772cd4cee2dSHong Zhang if (ts->quadraturets) { 27739566063dSJacob Faibussowitsch PetscCall(TSReset(ts->quadraturets)); 27749566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_costintegrand)); 2775cd4cee2dSHong Zhang } 27761d06f6b3SHong Zhang while (ilink) { 27771d06f6b3SHong Zhang next = ilink->next; 27789566063dSJacob Faibussowitsch PetscCall(TSDestroy(&ilink->ts)); 27799566063dSJacob Faibussowitsch PetscCall(PetscFree(ilink->splitname)); 27809566063dSJacob Faibussowitsch PetscCall(ISDestroy(&ilink->is)); 27819566063dSJacob Faibussowitsch PetscCall(PetscFree(ilink)); 27821d06f6b3SHong Zhang ilink = next; 278387f4e208SHong Zhang } 27846bf673ebSJoe Pusztay ts->tsrhssplit = NULL; 2785545aaa6fSHong Zhang ts->num_rhs_splits = 0; 27864a658b32SHong Zhang if (ts->tspan) { 27874a658b32SHong Zhang PetscCall(PetscFree(ts->tspan->span_times)); 27884a658b32SHong Zhang PetscCall(VecDestroyVecs(ts->tspan->num_span_times,&ts->tspan->vecs_sol)); 27894a658b32SHong Zhang PetscCall(PetscFree(ts->tspan)); 27904a658b32SHong Zhang } 2791277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 2792d763cef2SBarry Smith PetscFunctionReturn(0); 2793d763cef2SBarry Smith } 2794d763cef2SBarry Smith 27951fb7b255SJunchao Zhang /*@C 2796d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2797d763cef2SBarry Smith with TSCreate(). 2798d763cef2SBarry Smith 2799d763cef2SBarry Smith Collective on TS 2800d763cef2SBarry Smith 2801d763cef2SBarry Smith Input Parameter: 2802d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2803d763cef2SBarry Smith 2804d763cef2SBarry Smith Level: beginner 2805d763cef2SBarry Smith 2806db781477SPatrick Sanan .seealso: `TSCreate()`, `TSSetUp()`, `TSSolve()` 2807d763cef2SBarry Smith @*/ 28086bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2809d763cef2SBarry Smith { 2810d763cef2SBarry Smith PetscFunctionBegin; 28116bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 2812ecf68647SHong Zhang PetscValidHeaderSpecific(*ts,TS_CLASSID,1); 2813c793f718SLisandro Dalcin if (--((PetscObject)(*ts))->refct > 0) {*ts = NULL; PetscFunctionReturn(0);} 2814d763cef2SBarry Smith 28159566063dSJacob Faibussowitsch PetscCall(TSReset(*ts)); 28169566063dSJacob Faibussowitsch PetscCall(TSAdjointReset(*ts)); 2817ecf68647SHong Zhang if ((*ts)->forward_solve) { 28189566063dSJacob Faibussowitsch PetscCall(TSForwardReset(*ts)); 2819ecf68647SHong Zhang } 2820e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 28219566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsViewOff((PetscObject)*ts)); 28229566063dSJacob Faibussowitsch if ((*ts)->ops->destroy) PetscCall((*(*ts)->ops->destroy)((*ts))); 28236d4c513bSLisandro Dalcin 28249566063dSJacob Faibussowitsch PetscCall(TSTrajectoryDestroy(&(*ts)->trajectory)); 2825bc952696SBarry Smith 28269566063dSJacob Faibussowitsch PetscCall(TSAdaptDestroy(&(*ts)->adapt)); 28279566063dSJacob Faibussowitsch PetscCall(TSEventDestroy(&(*ts)->event)); 28286427ac75SLisandro Dalcin 28299566063dSJacob Faibussowitsch PetscCall(SNESDestroy(&(*ts)->snes)); 28309566063dSJacob Faibussowitsch PetscCall(DMDestroy(&(*ts)->dm)); 28319566063dSJacob Faibussowitsch PetscCall(TSMonitorCancel((*ts))); 28329566063dSJacob Faibussowitsch PetscCall(TSAdjointMonitorCancel((*ts))); 28336d4c513bSLisandro Dalcin 28349566063dSJacob Faibussowitsch PetscCall(TSDestroy(&(*ts)->quadraturets)); 28359566063dSJacob Faibussowitsch PetscCall(PetscHeaderDestroy(ts)); 2836d763cef2SBarry Smith PetscFunctionReturn(0); 2837d763cef2SBarry Smith } 2838d763cef2SBarry Smith 2839d8e5e3e6SSatish Balay /*@ 2840d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2841d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2842d763cef2SBarry Smith 2843d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2844d763cef2SBarry Smith 2845d763cef2SBarry Smith Input Parameter: 2846d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2847d763cef2SBarry Smith 2848d763cef2SBarry Smith Output Parameter: 2849d763cef2SBarry Smith . snes - the nonlinear solver context 2850d763cef2SBarry Smith 2851d763cef2SBarry Smith Notes: 2852d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2853d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 285494b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2855d763cef2SBarry Smith 2856d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 28570298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2858d763cef2SBarry Smith 2859d763cef2SBarry Smith Level: beginner 2860d763cef2SBarry Smith 2861d763cef2SBarry Smith @*/ 28627087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2863d763cef2SBarry Smith { 2864d763cef2SBarry Smith PetscFunctionBegin; 28650700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 28664482741eSBarry Smith PetscValidPointer(snes,2); 2867d372ba47SLisandro Dalcin if (!ts->snes) { 28689566063dSJacob Faibussowitsch PetscCall(SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes)); 28699566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options)); 28709566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts)); 28719566063dSJacob Faibussowitsch PetscCall(PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes)); 28729566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1)); 28739566063dSJacob Faibussowitsch if (ts->dm) PetscCall(SNESSetDM(ts->snes,ts->dm)); 28749e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 28759566063dSJacob Faibussowitsch PetscCall(SNESSetType(ts->snes,SNESKSPONLY)); 28769e2a6581SJed Brown } 2877d372ba47SLisandro Dalcin } 2878d763cef2SBarry Smith *snes = ts->snes; 2879d763cef2SBarry Smith PetscFunctionReturn(0); 2880d763cef2SBarry Smith } 2881d763cef2SBarry Smith 2882deb2cd25SJed Brown /*@ 2883deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2884deb2cd25SJed Brown 2885deb2cd25SJed Brown Collective 2886deb2cd25SJed Brown 2887d8d19677SJose E. Roman Input Parameters: 2888deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2889deb2cd25SJed Brown - snes - the nonlinear solver context 2890deb2cd25SJed Brown 2891deb2cd25SJed Brown Notes: 2892deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2893deb2cd25SJed Brown 2894deb2cd25SJed Brown Level: developer 2895deb2cd25SJed Brown 2896deb2cd25SJed Brown @*/ 2897deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2898deb2cd25SJed Brown { 2899d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2900deb2cd25SJed Brown 2901deb2cd25SJed Brown PetscFunctionBegin; 2902deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2903deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 29049566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)snes)); 29059566063dSJacob Faibussowitsch PetscCall(SNESDestroy(&ts->snes)); 2906bbd56ea5SKarl Rupp 2907deb2cd25SJed Brown ts->snes = snes; 2908bbd56ea5SKarl Rupp 29099566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts)); 29109566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL)); 2911740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 29129566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts)); 2913740132f1SEmil Constantinescu } 2914deb2cd25SJed Brown PetscFunctionReturn(0); 2915deb2cd25SJed Brown } 2916deb2cd25SJed Brown 2917d8e5e3e6SSatish Balay /*@ 291894b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2919d763cef2SBarry Smith a TS (timestepper) context. 2920d763cef2SBarry Smith 292194b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2922d763cef2SBarry Smith 2923d763cef2SBarry Smith Input Parameter: 2924d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2925d763cef2SBarry Smith 2926d763cef2SBarry Smith Output Parameter: 292794b7f48cSBarry Smith . ksp - the nonlinear solver context 2928d763cef2SBarry Smith 2929d763cef2SBarry Smith Notes: 293094b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2931d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2932d763cef2SBarry Smith KSP and PC contexts as well. 2933d763cef2SBarry Smith 293494b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 29350298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2936d763cef2SBarry Smith 2937d763cef2SBarry Smith Level: beginner 2938d763cef2SBarry Smith 2939d763cef2SBarry Smith @*/ 29407087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2941d763cef2SBarry Smith { 2942089b2837SJed Brown SNES snes; 2943d372ba47SLisandro Dalcin 2944d763cef2SBarry Smith PetscFunctionBegin; 29450700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 29464482741eSBarry Smith PetscValidPointer(ksp,2); 29473c633725SBarry Smith PetscCheck(((PetscObject)ts)->type_name,PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 29483c633725SBarry Smith PetscCheck(ts->problem_type == TS_LINEAR,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 29499566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 29509566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes,ksp)); 2951d763cef2SBarry Smith PetscFunctionReturn(0); 2952d763cef2SBarry Smith } 2953d763cef2SBarry Smith 2954d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2955d763cef2SBarry Smith 2956adb62b0dSMatthew Knepley /*@ 2957618ce8baSLisandro Dalcin TSSetMaxSteps - Sets the maximum number of steps to use. 2958618ce8baSLisandro Dalcin 2959618ce8baSLisandro Dalcin Logically Collective on TS 2960618ce8baSLisandro Dalcin 2961618ce8baSLisandro Dalcin Input Parameters: 2962618ce8baSLisandro Dalcin + ts - the TS context obtained from TSCreate() 2963618ce8baSLisandro Dalcin - maxsteps - maximum number of steps to use 2964618ce8baSLisandro Dalcin 2965618ce8baSLisandro Dalcin Options Database Keys: 2966618ce8baSLisandro Dalcin . -ts_max_steps <maxsteps> - Sets maxsteps 2967618ce8baSLisandro Dalcin 2968618ce8baSLisandro Dalcin Notes: 2969618ce8baSLisandro Dalcin The default maximum number of steps is 5000 2970618ce8baSLisandro Dalcin 2971618ce8baSLisandro Dalcin Level: intermediate 2972618ce8baSLisandro Dalcin 2973db781477SPatrick Sanan .seealso: `TSGetMaxSteps()`, `TSSetMaxTime()`, `TSSetExactFinalTime()` 2974618ce8baSLisandro Dalcin @*/ 2975618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps) 2976618ce8baSLisandro Dalcin { 2977618ce8baSLisandro Dalcin PetscFunctionBegin; 2978618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2979618ce8baSLisandro Dalcin PetscValidLogicalCollectiveInt(ts,maxsteps,2); 29803c633725SBarry Smith PetscCheck(maxsteps >= 0,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative"); 2981618ce8baSLisandro Dalcin ts->max_steps = maxsteps; 2982618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2983618ce8baSLisandro Dalcin } 2984618ce8baSLisandro Dalcin 2985618ce8baSLisandro Dalcin /*@ 2986618ce8baSLisandro Dalcin TSGetMaxSteps - Gets the maximum number of steps to use. 2987618ce8baSLisandro Dalcin 2988618ce8baSLisandro Dalcin Not Collective 2989618ce8baSLisandro Dalcin 2990618ce8baSLisandro Dalcin Input Parameters: 2991618ce8baSLisandro Dalcin . ts - the TS context obtained from TSCreate() 2992618ce8baSLisandro Dalcin 2993618ce8baSLisandro Dalcin Output Parameter: 2994618ce8baSLisandro Dalcin . maxsteps - maximum number of steps to use 2995618ce8baSLisandro Dalcin 2996618ce8baSLisandro Dalcin Level: advanced 2997618ce8baSLisandro Dalcin 2998db781477SPatrick Sanan .seealso: `TSSetMaxSteps()`, `TSGetMaxTime()`, `TSSetMaxTime()` 2999618ce8baSLisandro Dalcin @*/ 3000618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps) 3001618ce8baSLisandro Dalcin { 3002618ce8baSLisandro Dalcin PetscFunctionBegin; 3003618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3004618ce8baSLisandro Dalcin PetscValidIntPointer(maxsteps,2); 3005618ce8baSLisandro Dalcin *maxsteps = ts->max_steps; 3006618ce8baSLisandro Dalcin PetscFunctionReturn(0); 3007618ce8baSLisandro Dalcin } 3008618ce8baSLisandro Dalcin 3009618ce8baSLisandro Dalcin /*@ 3010618ce8baSLisandro Dalcin TSSetMaxTime - Sets the maximum (or final) time for timestepping. 3011618ce8baSLisandro Dalcin 3012618ce8baSLisandro Dalcin Logically Collective on TS 3013618ce8baSLisandro Dalcin 3014618ce8baSLisandro Dalcin Input Parameters: 3015618ce8baSLisandro Dalcin + ts - the TS context obtained from TSCreate() 3016618ce8baSLisandro Dalcin - maxtime - final time to step to 3017618ce8baSLisandro Dalcin 3018618ce8baSLisandro Dalcin Options Database Keys: 3019ef85077eSLisandro Dalcin . -ts_max_time <maxtime> - Sets maxtime 3020618ce8baSLisandro Dalcin 3021618ce8baSLisandro Dalcin Notes: 3022618ce8baSLisandro Dalcin The default maximum time is 5.0 3023618ce8baSLisandro Dalcin 3024618ce8baSLisandro Dalcin Level: intermediate 3025618ce8baSLisandro Dalcin 3026db781477SPatrick Sanan .seealso: `TSGetMaxTime()`, `TSSetMaxSteps()`, `TSSetExactFinalTime()` 3027618ce8baSLisandro Dalcin @*/ 3028618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime) 3029618ce8baSLisandro Dalcin { 3030618ce8baSLisandro Dalcin PetscFunctionBegin; 3031618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3032618ce8baSLisandro Dalcin PetscValidLogicalCollectiveReal(ts,maxtime,2); 3033618ce8baSLisandro Dalcin ts->max_time = maxtime; 3034618ce8baSLisandro Dalcin PetscFunctionReturn(0); 3035618ce8baSLisandro Dalcin } 3036618ce8baSLisandro Dalcin 3037618ce8baSLisandro Dalcin /*@ 3038618ce8baSLisandro Dalcin TSGetMaxTime - Gets the maximum (or final) time for timestepping. 3039618ce8baSLisandro Dalcin 3040618ce8baSLisandro Dalcin Not Collective 3041618ce8baSLisandro Dalcin 3042618ce8baSLisandro Dalcin Input Parameters: 3043618ce8baSLisandro Dalcin . ts - the TS context obtained from TSCreate() 3044618ce8baSLisandro Dalcin 3045618ce8baSLisandro Dalcin Output Parameter: 3046618ce8baSLisandro Dalcin . maxtime - final time to step to 3047618ce8baSLisandro Dalcin 3048618ce8baSLisandro Dalcin Level: advanced 3049618ce8baSLisandro Dalcin 3050db781477SPatrick Sanan .seealso: `TSSetMaxTime()`, `TSGetMaxSteps()`, `TSSetMaxSteps()` 3051618ce8baSLisandro Dalcin @*/ 3052618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime) 3053618ce8baSLisandro Dalcin { 3054618ce8baSLisandro Dalcin PetscFunctionBegin; 3055618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3056618ce8baSLisandro Dalcin PetscValidRealPointer(maxtime,2); 3057618ce8baSLisandro Dalcin *maxtime = ts->max_time; 3058618ce8baSLisandro Dalcin PetscFunctionReturn(0); 3059618ce8baSLisandro Dalcin } 3060618ce8baSLisandro Dalcin 3061618ce8baSLisandro Dalcin /*@ 3062aaa6c58dSLisandro Dalcin TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep(). 3063edc382c3SSatish Balay 3064edc382c3SSatish Balay Level: deprecated 3065edc382c3SSatish Balay 3066aaa6c58dSLisandro Dalcin @*/ 3067aaa6c58dSLisandro Dalcin PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 3068aaa6c58dSLisandro Dalcin { 3069aaa6c58dSLisandro Dalcin PetscFunctionBegin; 3070aaa6c58dSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 30719566063dSJacob Faibussowitsch PetscCall(TSSetTime(ts,initial_time)); 30729566063dSJacob Faibussowitsch PetscCall(TSSetTimeStep(ts,time_step)); 3073aaa6c58dSLisandro Dalcin PetscFunctionReturn(0); 3074aaa6c58dSLisandro Dalcin } 3075aaa6c58dSLisandro Dalcin 3076aaa6c58dSLisandro Dalcin /*@ 307719eac22cSLisandro Dalcin TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime(). 3078edc382c3SSatish Balay 3079edc382c3SSatish Balay Level: deprecated 3080edc382c3SSatish Balay 3081adb62b0dSMatthew Knepley @*/ 30827087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 3083adb62b0dSMatthew Knepley { 3084adb62b0dSMatthew Knepley PetscFunctionBegin; 30850700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3086abc0a331SBarry Smith if (maxsteps) { 30874482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 3088adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 3089adb62b0dSMatthew Knepley } 3090abc0a331SBarry Smith if (maxtime) { 3091064a246eSJacob Faibussowitsch PetscValidRealPointer(maxtime,3); 3092adb62b0dSMatthew Knepley *maxtime = ts->max_time; 3093adb62b0dSMatthew Knepley } 3094adb62b0dSMatthew Knepley PetscFunctionReturn(0); 3095adb62b0dSMatthew Knepley } 3096adb62b0dSMatthew Knepley 3097d763cef2SBarry Smith /*@ 309819eac22cSLisandro Dalcin TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime(). 3099edc382c3SSatish Balay 3100edc382c3SSatish Balay Level: deprecated 3101edc382c3SSatish Balay 3102d763cef2SBarry Smith @*/ 31037087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 3104d763cef2SBarry Smith { 3105d763cef2SBarry Smith PetscFunctionBegin; 31060700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3107c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 3108064a246eSJacob Faibussowitsch PetscValidLogicalCollectiveReal(ts,maxtime,3); 310939b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 311039b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 3111d763cef2SBarry Smith PetscFunctionReturn(0); 3112d763cef2SBarry Smith } 3113d763cef2SBarry Smith 3114d763cef2SBarry Smith /*@ 31155c5f5948SLisandro Dalcin TSGetTimeStepNumber - Deprecated, use TSGetStepNumber(). 3116edc382c3SSatish Balay 3117edc382c3SSatish Balay Level: deprecated 3118edc382c3SSatish Balay 31195c5f5948SLisandro Dalcin @*/ 3120e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); } 31215c5f5948SLisandro Dalcin 312219eac22cSLisandro Dalcin /*@ 31234f4e0956SLisandro Dalcin TSGetTotalSteps - Deprecated, use TSGetStepNumber(). 3124edc382c3SSatish Balay 3125edc382c3SSatish Balay Level: deprecated 3126edc382c3SSatish Balay 31274f4e0956SLisandro Dalcin @*/ 31284f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); } 31294f4e0956SLisandro Dalcin 31304f4e0956SLisandro Dalcin /*@ 3131d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 3132d763cef2SBarry Smith for use by the TS routines. 3133d763cef2SBarry Smith 3134d083f849SBarry Smith Logically Collective on TS 3135d763cef2SBarry Smith 3136d763cef2SBarry Smith Input Parameters: 3137d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 31380910c330SBarry Smith - u - the solution vector 3139d763cef2SBarry Smith 3140d763cef2SBarry Smith Level: beginner 3141d763cef2SBarry Smith 3142db781477SPatrick Sanan .seealso: `TSSetSolutionFunction()`, `TSGetSolution()`, `TSCreate()` 3143d763cef2SBarry Smith @*/ 31440910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 3145d763cef2SBarry Smith { 3146496e6a7aSJed Brown DM dm; 31478737fe31SLisandro Dalcin 3148d763cef2SBarry Smith PetscFunctionBegin; 31490700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31500910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 31519566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)u)); 31529566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_sol)); 31530910c330SBarry Smith ts->vec_sol = u; 3154bbd56ea5SKarl Rupp 31559566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 31569566063dSJacob Faibussowitsch PetscCall(DMShellSetGlobalVector(dm,u)); 3157d763cef2SBarry Smith PetscFunctionReturn(0); 3158d763cef2SBarry Smith } 3159d763cef2SBarry Smith 3160ac226902SBarry Smith /*@C 3161000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 31623f2090d5SJed Brown called once at the beginning of each time step. 3163000e7ae3SMatthew Knepley 31643f9fe445SBarry Smith Logically Collective on TS 3165000e7ae3SMatthew Knepley 3166000e7ae3SMatthew Knepley Input Parameters: 3167000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3168000e7ae3SMatthew Knepley - func - The function 3169000e7ae3SMatthew Knepley 3170000e7ae3SMatthew Knepley Calling sequence of func: 317167b8a455SSatish Balay .vb 317267b8a455SSatish Balay PetscErrorCode func (TS ts); 317367b8a455SSatish Balay .ve 3174000e7ae3SMatthew Knepley 3175000e7ae3SMatthew Knepley Level: intermediate 3176000e7ae3SMatthew Knepley 3177db781477SPatrick Sanan .seealso: `TSSetPreStage()`, `TSSetPostStage()`, `TSSetPostStep()`, `TSStep()`, `TSRestartStep()` 3178000e7ae3SMatthew Knepley @*/ 31797087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 3180000e7ae3SMatthew Knepley { 3181000e7ae3SMatthew Knepley PetscFunctionBegin; 31820700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3183ae60f76fSBarry Smith ts->prestep = func; 3184000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3185000e7ae3SMatthew Knepley } 3186000e7ae3SMatthew Knepley 318709ee8438SJed Brown /*@ 31883f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 31893f2090d5SJed Brown 31903f2090d5SJed Brown Collective on TS 31913f2090d5SJed Brown 31923f2090d5SJed Brown Input Parameters: 31933f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 31943f2090d5SJed Brown 31953f2090d5SJed Brown Notes: 31963f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 31973f2090d5SJed Brown so most users would not generally call this routine themselves. 31983f2090d5SJed Brown 31993f2090d5SJed Brown Level: developer 32003f2090d5SJed Brown 3201db781477SPatrick Sanan .seealso: `TSSetPreStep()`, `TSPreStage()`, `TSPostStage()`, `TSPostStep()` 32023f2090d5SJed Brown @*/ 32037087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 32043f2090d5SJed Brown { 32053f2090d5SJed Brown PetscFunctionBegin; 32060700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3207ae60f76fSBarry Smith if (ts->prestep) { 32085efd42a4SStefano Zampini Vec U; 3209ef8d1ce0SJohann Rudi PetscObjectId idprev; 3210ef8d1ce0SJohann Rudi PetscBool sameObject; 32115efd42a4SStefano Zampini PetscObjectState sprev,spost; 32125efd42a4SStefano Zampini 32139566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts,&U)); 32149566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U,&idprev)); 32159566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&sprev)); 3216a74df02fSJacob Faibussowitsch PetscStackCallStandard((*ts->prestep),ts); 32179566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts,&U)); 32189566063dSJacob Faibussowitsch PetscCall(PetscObjectCompareId((PetscObject)U,idprev,&sameObject)); 32199566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&spost)); 32209566063dSJacob Faibussowitsch if (!sameObject || sprev != spost) PetscCall(TSRestartStep(ts)); 3221312ce896SJed Brown } 32223f2090d5SJed Brown PetscFunctionReturn(0); 32233f2090d5SJed Brown } 32243f2090d5SJed Brown 3225b8123daeSJed Brown /*@C 3226b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 3227b8123daeSJed Brown called once at the beginning of each stage. 3228b8123daeSJed Brown 3229b8123daeSJed Brown Logically Collective on TS 3230b8123daeSJed Brown 3231b8123daeSJed Brown Input Parameters: 3232b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 3233b8123daeSJed Brown - func - The function 3234b8123daeSJed Brown 3235b8123daeSJed Brown Calling sequence of func: 323667b8a455SSatish Balay .vb 323767b8a455SSatish Balay PetscErrorCode func(TS ts, PetscReal stagetime); 323867b8a455SSatish Balay .ve 3239b8123daeSJed Brown 3240b8123daeSJed Brown Level: intermediate 3241b8123daeSJed Brown 3242b8123daeSJed Brown Note: 3243b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 324480275a0aSLisandro Dalcin The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being 3245b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 3246b8123daeSJed Brown 3247db781477SPatrick Sanan .seealso: `TSSetPostStage()`, `TSSetPreStep()`, `TSSetPostStep()`, `TSGetApplicationContext()` 3248b8123daeSJed Brown @*/ 3249b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 3250b8123daeSJed Brown { 3251b8123daeSJed Brown PetscFunctionBegin; 3252b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3253ae60f76fSBarry Smith ts->prestage = func; 3254b8123daeSJed Brown PetscFunctionReturn(0); 3255b8123daeSJed Brown } 3256b8123daeSJed Brown 32579be3e283SDebojyoti Ghosh /*@C 32589be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 32599be3e283SDebojyoti Ghosh called once at the end of each stage. 32609be3e283SDebojyoti Ghosh 32619be3e283SDebojyoti Ghosh Logically Collective on TS 32629be3e283SDebojyoti Ghosh 32639be3e283SDebojyoti Ghosh Input Parameters: 32649be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 32659be3e283SDebojyoti Ghosh - func - The function 32669be3e283SDebojyoti Ghosh 32679be3e283SDebojyoti Ghosh Calling sequence of func: 326867b8a455SSatish Balay .vb 326967b8a455SSatish Balay PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 327067b8a455SSatish Balay .ve 32719be3e283SDebojyoti Ghosh 32729be3e283SDebojyoti Ghosh Level: intermediate 32739be3e283SDebojyoti Ghosh 32749be3e283SDebojyoti Ghosh Note: 32759be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 327680275a0aSLisandro Dalcin The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being 32779be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 32789be3e283SDebojyoti Ghosh 3279db781477SPatrick Sanan .seealso: `TSSetPreStage()`, `TSSetPreStep()`, `TSSetPostStep()`, `TSGetApplicationContext()` 32809be3e283SDebojyoti Ghosh @*/ 32819be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 32829be3e283SDebojyoti Ghosh { 32839be3e283SDebojyoti Ghosh PetscFunctionBegin; 32849be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 32859be3e283SDebojyoti Ghosh ts->poststage = func; 32869be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 32879be3e283SDebojyoti Ghosh } 32889be3e283SDebojyoti Ghosh 3289c688d042SShri Abhyankar /*@C 3290c688d042SShri Abhyankar TSSetPostEvaluate - Sets the general-purpose function 3291c688d042SShri Abhyankar called once at the end of each step evaluation. 3292c688d042SShri Abhyankar 3293c688d042SShri Abhyankar Logically Collective on TS 3294c688d042SShri Abhyankar 3295c688d042SShri Abhyankar Input Parameters: 3296c688d042SShri Abhyankar + ts - The TS context obtained from TSCreate() 3297c688d042SShri Abhyankar - func - The function 3298c688d042SShri Abhyankar 3299c688d042SShri Abhyankar Calling sequence of func: 330067b8a455SSatish Balay .vb 330167b8a455SSatish Balay PetscErrorCode func(TS ts); 330267b8a455SSatish Balay .ve 3303c688d042SShri Abhyankar 3304c688d042SShri Abhyankar Level: intermediate 3305c688d042SShri Abhyankar 3306c688d042SShri Abhyankar Note: 33071785ff2aSShri Abhyankar Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling 33081785ff2aSShri Abhyankar thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep() 3309e7e94ed4SShri Abhyankar may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step 3310e7e94ed4SShri Abhyankar solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step 3311e7e94ed4SShri Abhyankar with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime() 3312c688d042SShri Abhyankar 3313db781477SPatrick Sanan .seealso: `TSSetPreStage()`, `TSSetPreStep()`, `TSSetPostStep()`, `TSGetApplicationContext()` 3314c688d042SShri Abhyankar @*/ 3315c688d042SShri Abhyankar PetscErrorCode TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS)) 3316c688d042SShri Abhyankar { 3317c688d042SShri Abhyankar PetscFunctionBegin; 3318c688d042SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3319c688d042SShri Abhyankar ts->postevaluate = func; 3320c688d042SShri Abhyankar PetscFunctionReturn(0); 3321c688d042SShri Abhyankar } 3322c688d042SShri Abhyankar 3323b8123daeSJed Brown /*@ 3324b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 3325b8123daeSJed Brown 3326b8123daeSJed Brown Collective on TS 3327b8123daeSJed Brown 3328b8123daeSJed Brown Input Parameters: 3329b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 33309be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 3331b8123daeSJed Brown 3332b8123daeSJed Brown Notes: 3333b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 3334b8123daeSJed Brown most users would not generally call this routine themselves. 3335b8123daeSJed Brown 3336b8123daeSJed Brown Level: developer 3337b8123daeSJed Brown 3338db781477SPatrick Sanan .seealso: `TSPostStage()`, `TSSetPreStep()`, `TSPreStep()`, `TSPostStep()` 3339b8123daeSJed Brown @*/ 3340b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 3341b8123daeSJed Brown { 3342b8123daeSJed Brown PetscFunctionBegin; 3343b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3344ae60f76fSBarry Smith if (ts->prestage) { 3345a74df02fSJacob Faibussowitsch PetscStackCallStandard((*ts->prestage),ts,stagetime); 3346b8123daeSJed Brown } 3347b8123daeSJed Brown PetscFunctionReturn(0); 3348b8123daeSJed Brown } 3349b8123daeSJed Brown 33509be3e283SDebojyoti Ghosh /*@ 33519be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 33529be3e283SDebojyoti Ghosh 33539be3e283SDebojyoti Ghosh Collective on TS 33549be3e283SDebojyoti Ghosh 33559be3e283SDebojyoti Ghosh Input Parameters: 33569be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 33579be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 33589be3e283SDebojyoti Ghosh stageindex - Stage number 33599be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 33609be3e283SDebojyoti Ghosh of stages) with the stage solutions 33619be3e283SDebojyoti Ghosh 33629be3e283SDebojyoti Ghosh Notes: 33639be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 33649be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 33659be3e283SDebojyoti Ghosh 33669be3e283SDebojyoti Ghosh Level: developer 33679be3e283SDebojyoti Ghosh 3368db781477SPatrick Sanan .seealso: `TSPreStage()`, `TSSetPreStep()`, `TSPreStep()`, `TSPostStep()` 33699be3e283SDebojyoti Ghosh @*/ 33709be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 33719be3e283SDebojyoti Ghosh { 33729be3e283SDebojyoti Ghosh PetscFunctionBegin; 33739be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 33744beae5d8SLisandro Dalcin if (ts->poststage) { 3375a74df02fSJacob Faibussowitsch PetscStackCallStandard((*ts->poststage),ts,stagetime,stageindex,Y); 33769be3e283SDebojyoti Ghosh } 33779be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 33789be3e283SDebojyoti Ghosh } 33799be3e283SDebojyoti Ghosh 3380c688d042SShri Abhyankar /*@ 3381c688d042SShri Abhyankar TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate() 3382c688d042SShri Abhyankar 3383c688d042SShri Abhyankar Collective on TS 3384c688d042SShri Abhyankar 3385c688d042SShri Abhyankar Input Parameters: 3386c688d042SShri Abhyankar . ts - The TS context obtained from TSCreate() 3387c688d042SShri Abhyankar 3388c688d042SShri Abhyankar Notes: 3389c688d042SShri Abhyankar TSPostEvaluate() is typically used within time stepping implementations, 3390c688d042SShri Abhyankar most users would not generally call this routine themselves. 3391c688d042SShri Abhyankar 3392c688d042SShri Abhyankar Level: developer 3393c688d042SShri Abhyankar 3394db781477SPatrick Sanan .seealso: `TSSetPostEvaluate()`, `TSSetPreStep()`, `TSPreStep()`, `TSPostStep()` 3395c688d042SShri Abhyankar @*/ 3396c688d042SShri Abhyankar PetscErrorCode TSPostEvaluate(TS ts) 3397c688d042SShri Abhyankar { 3398c688d042SShri Abhyankar PetscFunctionBegin; 3399c688d042SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3400c688d042SShri Abhyankar if (ts->postevaluate) { 3401dcb233daSLisandro Dalcin Vec U; 3402dcb233daSLisandro Dalcin PetscObjectState sprev,spost; 3403dcb233daSLisandro Dalcin 34049566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts,&U)); 34059566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&sprev)); 3406a74df02fSJacob Faibussowitsch PetscStackCallStandard((*ts->postevaluate),ts); 34079566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&spost)); 34089566063dSJacob Faibussowitsch if (sprev != spost) PetscCall(TSRestartStep(ts)); 3409c688d042SShri Abhyankar } 3410c688d042SShri Abhyankar PetscFunctionReturn(0); 3411c688d042SShri Abhyankar } 3412c688d042SShri Abhyankar 3413ac226902SBarry Smith /*@C 3414000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 34153f2090d5SJed Brown called once at the end of each time step. 3416000e7ae3SMatthew Knepley 34173f9fe445SBarry Smith Logically Collective on TS 3418000e7ae3SMatthew Knepley 3419000e7ae3SMatthew Knepley Input Parameters: 3420000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3421000e7ae3SMatthew Knepley - func - The function 3422000e7ae3SMatthew Knepley 3423000e7ae3SMatthew Knepley Calling sequence of func: 3424b8123daeSJed Brown $ func (TS ts); 3425000e7ae3SMatthew Knepley 34261785ff2aSShri Abhyankar Notes: 34271785ff2aSShri Abhyankar The function set by TSSetPostStep() is called after each successful step. The solution vector X 34281785ff2aSShri Abhyankar obtained by TSGetSolution() may be different than that computed at the step end if the event handler 34291785ff2aSShri Abhyankar locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead. 34301785ff2aSShri Abhyankar 3431000e7ae3SMatthew Knepley Level: intermediate 3432000e7ae3SMatthew Knepley 3433db781477SPatrick Sanan .seealso: `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostEvaluate()`, `TSGetTimeStep()`, `TSGetStepNumber()`, `TSGetTime()`, `TSRestartStep()` 3434000e7ae3SMatthew Knepley @*/ 34357087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 3436000e7ae3SMatthew Knepley { 3437000e7ae3SMatthew Knepley PetscFunctionBegin; 34380700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3439ae60f76fSBarry Smith ts->poststep = func; 3440000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3441000e7ae3SMatthew Knepley } 3442000e7ae3SMatthew Knepley 344309ee8438SJed Brown /*@ 34443f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 34453f2090d5SJed Brown 34463f2090d5SJed Brown Collective on TS 34473f2090d5SJed Brown 34483f2090d5SJed Brown Input Parameters: 34493f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 34503f2090d5SJed Brown 34513f2090d5SJed Brown Notes: 34523f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 34533f2090d5SJed Brown so most users would not generally call this routine themselves. 34543f2090d5SJed Brown 34553f2090d5SJed Brown Level: developer 34563f2090d5SJed Brown 34573f2090d5SJed Brown @*/ 34587087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 34593f2090d5SJed Brown { 34603f2090d5SJed Brown PetscFunctionBegin; 34610700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3462ae60f76fSBarry Smith if (ts->poststep) { 34635efd42a4SStefano Zampini Vec U; 3464ef8d1ce0SJohann Rudi PetscObjectId idprev; 3465ef8d1ce0SJohann Rudi PetscBool sameObject; 34665efd42a4SStefano Zampini PetscObjectState sprev,spost; 34675efd42a4SStefano Zampini 34689566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts,&U)); 34699566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U,&idprev)); 34709566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&sprev)); 3471a74df02fSJacob Faibussowitsch PetscStackCallStandard((*ts->poststep),ts); 34729566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts,&U)); 34739566063dSJacob Faibussowitsch PetscCall(PetscObjectCompareId((PetscObject)U,idprev,&sameObject)); 34749566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&spost)); 34759566063dSJacob Faibussowitsch if (!sameObject || sprev != spost) PetscCall(TSRestartStep(ts)); 347672ac3e02SJed Brown } 34773f2090d5SJed Brown PetscFunctionReturn(0); 34783f2090d5SJed Brown } 34793f2090d5SJed Brown 3480cd652676SJed Brown /*@ 3481cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3482cd652676SJed Brown 3483cd652676SJed Brown Collective on TS 3484cd652676SJed Brown 34854165533cSJose E. Roman Input Parameters: 3486cd652676SJed Brown + ts - time stepping context 3487cd652676SJed Brown - t - time to interpolate to 3488cd652676SJed Brown 34894165533cSJose E. Roman Output Parameter: 34900910c330SBarry Smith . U - state at given time 3491cd652676SJed Brown 3492cd652676SJed Brown Level: intermediate 3493cd652676SJed Brown 3494cd652676SJed Brown Developer Notes: 3495cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3496cd652676SJed Brown 3497db781477SPatrick Sanan .seealso: `TSSetExactFinalTime()`, `TSSolve()` 3498cd652676SJed Brown @*/ 34990910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3500cd652676SJed Brown { 3501cd652676SJed Brown PetscFunctionBegin; 3502cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3503b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 350463a3b9bcSJacob 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); 35053c633725SBarry Smith PetscCheck(ts->ops->interpolate,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 35069566063dSJacob Faibussowitsch PetscCall((*ts->ops->interpolate)(ts,t,U)); 3507cd652676SJed Brown PetscFunctionReturn(0); 3508cd652676SJed Brown } 3509cd652676SJed Brown 3510d763cef2SBarry Smith /*@ 35116d9e5789SSean Farley TSStep - Steps one time step 3512d763cef2SBarry Smith 3513d763cef2SBarry Smith Collective on TS 3514d763cef2SBarry Smith 3515d763cef2SBarry Smith Input Parameter: 3516d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3517d763cef2SBarry Smith 351827829d71SBarry Smith Level: developer 3519d763cef2SBarry Smith 3520b8123daeSJed Brown Notes: 352127829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 352227829d71SBarry Smith 3523b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3524b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3525b8123daeSJed Brown 352619eac22cSLisandro Dalcin This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the 352719eac22cSLisandro Dalcin time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 352825cb2221SBarry Smith 3529db781477SPatrick Sanan .seealso: `TSCreate()`, `TSSetUp()`, `TSDestroy()`, `TSSolve()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostStage()`, `TSInterpolate()` 3530d763cef2SBarry Smith @*/ 3531193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3532d763cef2SBarry Smith { 3533fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3534be5899b3SLisandro Dalcin PetscReal ptime; 3535d763cef2SBarry Smith 3536d763cef2SBarry Smith PetscFunctionBegin; 35370700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3538d0609cedSBarry Smith PetscCall(PetscCitationsRegister("@article{tspaper,\n" 3539fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3540f1d62c27SHong Zhang " author = {Abhyankar, Shrirang and Brown, Jed and Constantinescu, Emil and Ghosh, Debojyoti and Smith, Barry F. and Zhang, Hong},\n" 3541f1d62c27SHong Zhang " journal = {arXiv e-preprints},\n" 3542f1d62c27SHong Zhang " eprint = {1806.01437},\n" 3543f1d62c27SHong Zhang " archivePrefix = {arXiv},\n" 3544d0609cedSBarry Smith " year = {2018}\n}\n",&cite)); 35459566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts)); 35469566063dSJacob Faibussowitsch PetscCall(TSTrajectorySetUp(ts->trajectory,ts)); 3547d405a339SMatthew Knepley 35483c633725SBarry Smith PetscCheck(ts->ops->step,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 35493c633725SBarry 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>"); 35503c633725SBarry 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()"); 35513c633725SBarry 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"); 3552a6772fa2SLisandro Dalcin 3553be5899b3SLisandro Dalcin if (!ts->steps) ts->ptime_prev = ts->ptime; 3554be5899b3SLisandro Dalcin ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev; 35552808aa04SLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3556fc8dbba5SLisandro Dalcin 35579566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_Step,ts,0,0,0)); 35589566063dSJacob Faibussowitsch PetscCall((*ts->ops->step)(ts)); 35599566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_Step,ts,0,0,0)); 3560fc8dbba5SLisandro Dalcin 3561fc8dbba5SLisandro Dalcin if (ts->reason >= 0) { 3562be5899b3SLisandro Dalcin ts->ptime_prev = ptime; 35632808aa04SLisandro Dalcin ts->steps++; 3564be5899b3SLisandro Dalcin ts->steprollback = PETSC_FALSE; 356528d5b5d6SLisandro Dalcin ts->steprestart = PETSC_FALSE; 35664a658b32SHong Zhang if (ts->tspan && PetscIsCloseAtTol(ts->ptime,ts->tspan->span_times[ts->tspan->spanctr],10*PETSC_MACHINE_EPSILON,0) && ts->tspan->spanctr < ts->tspan->num_span_times) PetscCall(VecCopy(ts->vec_sol,ts->tspan->vecs_sol[ts->tspan->spanctr++])); 3567d2daff3dSHong Zhang } 3568fc8dbba5SLisandro Dalcin 3569fc8dbba5SLisandro Dalcin if (!ts->reason) { 357008c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 357108c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 357208c7845fSBarry Smith } 3573fc8dbba5SLisandro Dalcin 35743c633725SBarry 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]); 35753c633725SBarry Smith PetscCheck(ts->reason >= 0 || !ts->errorifstepfailed,PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 357608c7845fSBarry Smith PetscFunctionReturn(0); 357708c7845fSBarry Smith } 357808c7845fSBarry Smith 357908c7845fSBarry Smith /*@ 35807cbde773SLisandro Dalcin TSEvaluateWLTE - Evaluate the weighted local truncation error norm 35817cbde773SLisandro Dalcin at the end of a time step with a given order of accuracy. 35827cbde773SLisandro Dalcin 35837cbde773SLisandro Dalcin Collective on TS 35847cbde773SLisandro Dalcin 35854165533cSJose E. Roman Input Parameters: 35867cbde773SLisandro Dalcin + ts - time stepping context 358797bb3fdcSJose E. Roman - wnormtype - norm type, either NORM_2 or NORM_INFINITY 35887cbde773SLisandro Dalcin 358997bb3fdcSJose E. Roman Input/Output Parameter: 359097bb3fdcSJose E. Roman . order - optional, desired order for the error evaluation or PETSC_DECIDE; 359197bb3fdcSJose E. Roman on output, the actual order of the error evaluation 359297bb3fdcSJose E. Roman 359397bb3fdcSJose E. Roman Output Parameter: 359497bb3fdcSJose E. Roman . wlte - the weighted local truncation error norm 35957cbde773SLisandro Dalcin 35967cbde773SLisandro Dalcin Level: advanced 35977cbde773SLisandro Dalcin 35987cbde773SLisandro Dalcin Notes: 35997cbde773SLisandro Dalcin If the timestepper cannot evaluate the error in a particular step 36007cbde773SLisandro Dalcin (eg. in the first step or restart steps after event handling), 36017cbde773SLisandro Dalcin this routine returns wlte=-1.0 . 36027cbde773SLisandro Dalcin 3603db781477SPatrick Sanan .seealso: `TSStep()`, `TSAdapt`, `TSErrorWeightedNorm()` 36047cbde773SLisandro Dalcin @*/ 36057cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte) 36067cbde773SLisandro Dalcin { 36077cbde773SLisandro Dalcin PetscFunctionBegin; 36087cbde773SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 36097cbde773SLisandro Dalcin PetscValidType(ts,1); 3610064a246eSJacob Faibussowitsch PetscValidLogicalCollectiveEnum(ts,wnormtype,2); 36117cbde773SLisandro Dalcin if (order) PetscValidIntPointer(order,3); 36127cbde773SLisandro Dalcin if (order) PetscValidLogicalCollectiveInt(ts,*order,3); 36137cbde773SLisandro Dalcin PetscValidRealPointer(wlte,4); 36143c633725SBarry Smith PetscCheck(wnormtype == NORM_2 || wnormtype == NORM_INFINITY,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 36153c633725SBarry Smith PetscCheck(ts->ops->evaluatewlte,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name); 36169566063dSJacob Faibussowitsch PetscCall((*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte)); 36177cbde773SLisandro Dalcin PetscFunctionReturn(0); 36187cbde773SLisandro Dalcin } 36197cbde773SLisandro Dalcin 362005175c85SJed Brown /*@ 362105175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 362205175c85SJed Brown 36231c3436cfSJed Brown Collective on TS 362405175c85SJed Brown 36254165533cSJose E. Roman Input Parameters: 36261c3436cfSJed Brown + ts - time stepping context 36271c3436cfSJed Brown . order - desired order of accuracy 36280298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 362905175c85SJed Brown 36304165533cSJose E. Roman Output Parameter: 36310910c330SBarry Smith . U - state at the end of the current step 363205175c85SJed Brown 363305175c85SJed Brown Level: advanced 363405175c85SJed Brown 3635108c343cSJed Brown Notes: 3636108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3637108c343cSJed Brown 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. 3638108c343cSJed Brown 3639db781477SPatrick Sanan .seealso: `TSStep()`, `TSAdapt` 364005175c85SJed Brown @*/ 36410910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 364205175c85SJed Brown { 364305175c85SJed Brown PetscFunctionBegin; 364405175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 364505175c85SJed Brown PetscValidType(ts,1); 36460910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 36473c633725SBarry Smith PetscCheck(ts->ops->evaluatestep,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 36489566063dSJacob Faibussowitsch PetscCall((*ts->ops->evaluatestep)(ts,order,U,done)); 364905175c85SJed Brown PetscFunctionReturn(0); 365005175c85SJed Brown } 365105175c85SJed Brown 3652aad739acSMatthew G. Knepley /*@C 36532e61be88SMatthew G. Knepley TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping. 3654aad739acSMatthew G. Knepley 3655aad739acSMatthew G. Knepley Not collective 3656aad739acSMatthew G. Knepley 36574165533cSJose E. Roman Input Parameter: 3658aad739acSMatthew G. Knepley . ts - time stepping context 3659aad739acSMatthew G. Knepley 36604165533cSJose E. Roman Output Parameter: 36612e61be88SMatthew G. Knepley . initConditions - The function which computes an initial condition 3662aad739acSMatthew G. Knepley 3663aad739acSMatthew G. Knepley Level: advanced 3664aad739acSMatthew G. Knepley 3665aad739acSMatthew G. Knepley Notes: 3666aad739acSMatthew G. Knepley The calling sequence for the function is 36672e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u) 3668aad739acSMatthew G. Knepley $ ts - The timestepping context 36692e61be88SMatthew G. Knepley $ u - The input vector in which the initial condition is stored 3670aad739acSMatthew G. Knepley 3671db781477SPatrick Sanan .seealso: `TSSetComputeInitialCondition()`, `TSComputeInitialCondition()` 3672aad739acSMatthew G. Knepley @*/ 36732e61be88SMatthew G. Knepley PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS, Vec)) 3674aad739acSMatthew G. Knepley { 3675aad739acSMatthew G. Knepley PetscFunctionBegin; 3676aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 36772e61be88SMatthew G. Knepley PetscValidPointer(initCondition, 2); 36782e61be88SMatthew G. Knepley *initCondition = ts->ops->initcondition; 3679aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3680aad739acSMatthew G. Knepley } 3681aad739acSMatthew G. Knepley 3682aad739acSMatthew G. Knepley /*@C 36832e61be88SMatthew G. Knepley TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping. 3684aad739acSMatthew G. Knepley 3685aad739acSMatthew G. Knepley Logically collective on ts 3686aad739acSMatthew G. Knepley 36874165533cSJose E. Roman Input Parameters: 3688aad739acSMatthew G. Knepley + ts - time stepping context 36892e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition 3690aad739acSMatthew G. Knepley 3691aad739acSMatthew G. Knepley Level: advanced 3692aad739acSMatthew G. Knepley 3693a96d6ef6SBarry Smith Calling sequence for initCondition: 3694a96d6ef6SBarry Smith $ PetscErrorCode initCondition(TS ts, Vec u) 3695a96d6ef6SBarry Smith 3696a96d6ef6SBarry Smith + ts - The timestepping context 3697a96d6ef6SBarry Smith - u - The input vector in which the initial condition is to be stored 3698aad739acSMatthew G. Knepley 3699db781477SPatrick Sanan .seealso: `TSGetComputeInitialCondition()`, `TSComputeInitialCondition()` 3700aad739acSMatthew G. Knepley @*/ 37012e61be88SMatthew G. Knepley PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS, Vec)) 3702aad739acSMatthew G. Knepley { 3703aad739acSMatthew G. Knepley PetscFunctionBegin; 3704aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 37052e61be88SMatthew G. Knepley PetscValidFunction(initCondition, 2); 37062e61be88SMatthew G. Knepley ts->ops->initcondition = initCondition; 3707aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3708aad739acSMatthew G. Knepley } 3709aad739acSMatthew G. Knepley 3710aad739acSMatthew G. Knepley /*@ 37112e61be88SMatthew G. Knepley TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set. 3712aad739acSMatthew G. Knepley 3713aad739acSMatthew G. Knepley Collective on ts 3714aad739acSMatthew G. Knepley 37154165533cSJose E. Roman Input Parameters: 3716aad739acSMatthew G. Knepley + ts - time stepping context 37172e61be88SMatthew G. Knepley - u - The Vec to store the condition in which will be used in TSSolve() 3718aad739acSMatthew G. Knepley 3719aad739acSMatthew G. Knepley Level: advanced 3720aad739acSMatthew G. Knepley 3721db781477SPatrick Sanan .seealso: `TSGetComputeInitialCondition()`, `TSSetComputeInitialCondition()`, `TSSolve()` 3722aad739acSMatthew G. Knepley @*/ 37232e61be88SMatthew G. Knepley PetscErrorCode TSComputeInitialCondition(TS ts, Vec u) 3724aad739acSMatthew G. Knepley { 3725aad739acSMatthew G. Knepley PetscFunctionBegin; 3726aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3727aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 37289566063dSJacob Faibussowitsch if (ts->ops->initcondition) PetscCall((*ts->ops->initcondition)(ts, u)); 3729aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3730aad739acSMatthew G. Knepley } 3731aad739acSMatthew G. Knepley 3732aad739acSMatthew G. Knepley /*@C 3733aad739acSMatthew G. Knepley TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping. 3734aad739acSMatthew G. Knepley 3735aad739acSMatthew G. Knepley Not collective 3736aad739acSMatthew G. Knepley 37374165533cSJose E. Roman Input Parameter: 3738aad739acSMatthew G. Knepley . ts - time stepping context 3739aad739acSMatthew G. Knepley 37404165533cSJose E. Roman Output Parameter: 3741aad739acSMatthew G. Knepley . exactError - The function which computes the solution error 3742aad739acSMatthew G. Knepley 3743aad739acSMatthew G. Knepley Level: advanced 3744aad739acSMatthew G. Knepley 3745a96d6ef6SBarry Smith Calling sequence for exactError: 3746a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u) 3747a96d6ef6SBarry Smith 3748a96d6ef6SBarry Smith + ts - The timestepping context 3749a96d6ef6SBarry Smith . u - The approximate solution vector 3750a96d6ef6SBarry Smith - e - The input vector in which the error is stored 3751aad739acSMatthew G. Knepley 3752db781477SPatrick Sanan .seealso: `TSGetComputeExactError()`, `TSComputeExactError()` 3753aad739acSMatthew G. Knepley @*/ 3754aad739acSMatthew G. Knepley PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS, Vec, Vec)) 3755aad739acSMatthew G. Knepley { 3756aad739acSMatthew G. Knepley PetscFunctionBegin; 3757aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3758aad739acSMatthew G. Knepley PetscValidPointer(exactError, 2); 3759aad739acSMatthew G. Knepley *exactError = ts->ops->exacterror; 3760aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3761aad739acSMatthew G. Knepley } 3762aad739acSMatthew G. Knepley 3763aad739acSMatthew G. Knepley /*@C 3764aad739acSMatthew G. Knepley TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping. 3765aad739acSMatthew G. Knepley 3766aad739acSMatthew G. Knepley Logically collective on ts 3767aad739acSMatthew G. Knepley 37684165533cSJose E. Roman Input Parameters: 3769aad739acSMatthew G. Knepley + ts - time stepping context 3770aad739acSMatthew G. Knepley - exactError - The function which computes the solution error 3771aad739acSMatthew G. Knepley 3772aad739acSMatthew G. Knepley Level: advanced 3773aad739acSMatthew G. Knepley 3774a96d6ef6SBarry Smith Calling sequence for exactError: 3775a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u) 3776a96d6ef6SBarry Smith 3777a96d6ef6SBarry Smith + ts - The timestepping context 3778a96d6ef6SBarry Smith . u - The approximate solution vector 3779a96d6ef6SBarry Smith - e - The input vector in which the error is stored 3780aad739acSMatthew G. Knepley 3781db781477SPatrick Sanan .seealso: `TSGetComputeExactError()`, `TSComputeExactError()` 3782aad739acSMatthew G. Knepley @*/ 3783aad739acSMatthew G. Knepley PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS, Vec, Vec)) 3784aad739acSMatthew G. Knepley { 3785aad739acSMatthew G. Knepley PetscFunctionBegin; 3786aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3787f907fdbfSMatthew G. Knepley PetscValidFunction(exactError, 2); 3788aad739acSMatthew G. Knepley ts->ops->exacterror = exactError; 3789aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3790aad739acSMatthew G. Knepley } 3791aad739acSMatthew G. Knepley 3792aad739acSMatthew G. Knepley /*@ 3793aad739acSMatthew G. Knepley TSComputeExactError - Compute the solution error for the timestepping using the function previously set. 3794aad739acSMatthew G. Knepley 3795aad739acSMatthew G. Knepley Collective on ts 3796aad739acSMatthew G. Knepley 37974165533cSJose E. Roman Input Parameters: 3798aad739acSMatthew G. Knepley + ts - time stepping context 3799aad739acSMatthew G. Knepley . u - The approximate solution 3800aad739acSMatthew G. Knepley - e - The Vec used to store the error 3801aad739acSMatthew G. Knepley 3802aad739acSMatthew G. Knepley Level: advanced 3803aad739acSMatthew G. Knepley 3804db781477SPatrick Sanan .seealso: `TSGetComputeInitialCondition()`, `TSSetComputeInitialCondition()`, `TSSolve()` 3805aad739acSMatthew G. Knepley @*/ 3806aad739acSMatthew G. Knepley PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e) 3807aad739acSMatthew G. Knepley { 3808aad739acSMatthew G. Knepley PetscFunctionBegin; 3809aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3810aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3811aad739acSMatthew G. Knepley PetscValidHeaderSpecific(e, VEC_CLASSID, 3); 38129566063dSJacob Faibussowitsch if (ts->ops->exacterror) PetscCall((*ts->ops->exacterror)(ts, u, e)); 3813aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3814aad739acSMatthew G. Knepley } 3815aad739acSMatthew G. Knepley 3816b1cb36f3SHong Zhang /*@ 38176a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 38186a4d4014SLisandro Dalcin 38196a4d4014SLisandro Dalcin Collective on TS 38206a4d4014SLisandro Dalcin 3821d8d19677SJose E. Roman Input Parameters: 38226a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 382363e21af5SBarry Smith - u - the solution vector (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used, 382463e21af5SBarry Smith otherwise must contain the initial conditions and will contain the solution at the final requested time 38255a3a76d0SJed Brown 38266a4d4014SLisandro Dalcin Level: beginner 38276a4d4014SLisandro Dalcin 38285a3a76d0SJed Brown Notes: 38295a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 38305a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 38315a3a76d0SJed Brown stepped over the final time. 38325a3a76d0SJed Brown 3833db781477SPatrick Sanan .seealso: `TSCreate()`, `TSSetSolution()`, `TSStep()`, `TSGetTime()`, `TSGetSolveTime()` 38346a4d4014SLisandro Dalcin @*/ 3835cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 38366a4d4014SLisandro Dalcin { 3837b06615a5SLisandro Dalcin Vec solution; 3838f22f69f0SBarry Smith 38396a4d4014SLisandro Dalcin PetscFunctionBegin; 38400700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3841f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 3842303a5415SBarry Smith 38439566063dSJacob Faibussowitsch PetscCall(TSSetExactFinalTimeDefault(ts)); 3844ee41a567SStefano 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 */ 38450910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 38469566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u,&solution)); 38479566063dSJacob Faibussowitsch PetscCall(TSSetSolution(ts,solution)); 38489566063dSJacob Faibussowitsch PetscCall(VecDestroy(&solution)); /* grant ownership */ 38495a3a76d0SJed Brown } 38509566063dSJacob Faibussowitsch PetscCall(VecCopy(u,ts->vec_sol)); 38513c633725SBarry Smith PetscCheck(!ts->forward_solve,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE"); 3852bbd56ea5SKarl Rupp } else if (u) { 38539566063dSJacob Faibussowitsch PetscCall(TSSetSolution(ts,u)); 38545a3a76d0SJed Brown } 38559566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts)); 38569566063dSJacob Faibussowitsch PetscCall(TSTrajectorySetUp(ts->trajectory,ts)); 3857a6772fa2SLisandro Dalcin 38583c633725SBarry 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>"); 38593c633725SBarry 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()"); 38603c633725SBarry 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"); 38614a658b32SHong 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"); 38624a658b32SHong Zhang 38634a658b32SHong Zhang if (ts->tspan && PetscIsCloseAtTol(ts->ptime,ts->tspan->span_times[0],10*PETSC_MACHINE_EPSILON,0)) { /* starting point in time span */ 38644a658b32SHong Zhang PetscCall(VecCopy(ts->vec_sol,ts->tspan->vecs_sol[0])); 38654a658b32SHong Zhang ts->tspan->spanctr = 1; 38664a658b32SHong Zhang } 3867a6772fa2SLisandro Dalcin 3868715f1b00SHong Zhang if (ts->forward_solve) { 38699566063dSJacob Faibussowitsch PetscCall(TSForwardSetUp(ts)); 3870715f1b00SHong Zhang } 3871715f1b00SHong Zhang 3872e7069c78SShri /* reset number of steps only when the step is not restarted. ARKIMEX 3873715f1b00SHong Zhang restarts the step after an event. Resetting these counters in such case causes 3874e7069c78SShri TSTrajectory to incorrectly save the output files 3875e7069c78SShri */ 3876715f1b00SHong Zhang /* reset time step and iteration counters */ 38772808aa04SLisandro Dalcin if (!ts->steps) { 38785ef26d82SJed Brown ts->ksp_its = 0; 38795ef26d82SJed Brown ts->snes_its = 0; 3880c610991cSLisandro Dalcin ts->num_snes_failures = 0; 3881c610991cSLisandro Dalcin ts->reject = 0; 38822808aa04SLisandro Dalcin ts->steprestart = PETSC_TRUE; 38832808aa04SLisandro Dalcin ts->steprollback = PETSC_FALSE; 38847d51462cSStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 38852808aa04SLisandro Dalcin } 3886e97c63d7SStefano Zampini 38874a658b32SHong Zhang /* make sure initial time step does not overshoot final time or the next point in tspan */ 3888e97c63d7SStefano Zampini if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) { 38894a658b32SHong Zhang PetscReal maxdt; 3890e97c63d7SStefano Zampini PetscReal dt = ts->time_step; 3891e97c63d7SStefano Zampini 38924a658b32SHong Zhang if (ts->tspan) maxdt = ts->tspan->span_times[ts->tspan->spanctr] - ts->ptime; 38934a658b32SHong Zhang else maxdt = ts->max_time - ts->ptime; 3894e97c63d7SStefano Zampini ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt,maxdt,10*PETSC_MACHINE_EPSILON,0) ? maxdt : dt); 3895e97c63d7SStefano Zampini } 3896193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 3897193ac0bcSJed Brown 3898900f6b5bSMatthew G. Knepley { 3899900f6b5bSMatthew G. Knepley PetscViewer viewer; 3900900f6b5bSMatthew G. Knepley PetscViewerFormat format; 3901900f6b5bSMatthew G. Knepley PetscBool flg; 3902900f6b5bSMatthew G. Knepley static PetscBool incall = PETSC_FALSE; 3903900f6b5bSMatthew G. Knepley 3904900f6b5bSMatthew G. Knepley if (!incall) { 3905900f6b5bSMatthew G. Knepley /* Estimate the convergence rate of the time discretization */ 39069566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject) ts),((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg)); 3907900f6b5bSMatthew G. Knepley if (flg) { 3908900f6b5bSMatthew G. Knepley PetscConvEst conv; 3909900f6b5bSMatthew G. Knepley DM dm; 3910900f6b5bSMatthew G. Knepley PetscReal *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */ 3911900f6b5bSMatthew G. Knepley PetscInt Nf; 3912f2ed2dc7SMatthew G. Knepley PetscBool checkTemporal = PETSC_TRUE; 3913900f6b5bSMatthew G. Knepley 3914900f6b5bSMatthew G. Knepley incall = PETSC_TRUE; 39159566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_temporal", &checkTemporal, &flg)); 39169566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 39179566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &Nf)); 39189566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(PetscMax(Nf, 1), &alpha)); 39199566063dSJacob Faibussowitsch PetscCall(PetscConvEstCreate(PetscObjectComm((PetscObject) ts), &conv)); 39209566063dSJacob Faibussowitsch PetscCall(PetscConvEstUseTS(conv, checkTemporal)); 39219566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetSolver(conv, (PetscObject) ts)); 39229566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetFromOptions(conv)); 39239566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetUp(conv)); 39249566063dSJacob Faibussowitsch PetscCall(PetscConvEstGetConvRate(conv, alpha)); 39259566063dSJacob Faibussowitsch PetscCall(PetscViewerPushFormat(viewer, format)); 39269566063dSJacob Faibussowitsch PetscCall(PetscConvEstRateView(conv, alpha, viewer)); 39279566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(viewer)); 39289566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&viewer)); 39299566063dSJacob Faibussowitsch PetscCall(PetscConvEstDestroy(&conv)); 39309566063dSJacob Faibussowitsch PetscCall(PetscFree(alpha)); 3931900f6b5bSMatthew G. Knepley incall = PETSC_FALSE; 3932900f6b5bSMatthew G. Knepley } 3933900f6b5bSMatthew G. Knepley } 3934900f6b5bSMatthew G. Knepley } 3935900f6b5bSMatthew G. Knepley 39369566063dSJacob Faibussowitsch PetscCall(TSViewFromOptions(ts,NULL,"-ts_view_pre")); 3937f05ece33SBarry Smith 3938193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 39399566063dSJacob Faibussowitsch PetscCall((*ts->ops->solve)(ts)); 39409566063dSJacob Faibussowitsch if (u) PetscCall(VecCopy(ts->vec_sol,u)); 3941cc708dedSBarry Smith ts->solvetime = ts->ptime; 3942a6772fa2SLisandro Dalcin solution = ts->vec_sol; 3943be5899b3SLisandro Dalcin } else { /* Step the requested number of timesteps. */ 3944db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3945db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3946e7069c78SShri 39472808aa04SLisandro Dalcin if (!ts->steps) { 39489566063dSJacob Faibussowitsch PetscCall(TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol)); 39499566063dSJacob Faibussowitsch PetscCall(TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol)); 39502808aa04SLisandro Dalcin } 39516427ac75SLisandro Dalcin 3952e1a7a14fSJed Brown while (!ts->reason) { 39539566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol)); 39549687d888SLisandro Dalcin if (!ts->steprollback) { 39559566063dSJacob Faibussowitsch PetscCall(TSPreStep(ts)); 39569687d888SLisandro Dalcin } 39579566063dSJacob Faibussowitsch PetscCall(TSStep(ts)); 3958f3b1f45cSBarry Smith if (ts->testjacobian) { 39599566063dSJacob Faibussowitsch PetscCall(TSRHSJacobianTest(ts,NULL)); 3960f3b1f45cSBarry Smith } 3961f3b1f45cSBarry Smith if (ts->testjacobiantranspose) { 39629566063dSJacob Faibussowitsch PetscCall(TSRHSJacobianTestTranspose(ts,NULL)); 3963f3b1f45cSBarry Smith } 3964cd4cee2dSHong Zhang if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */ 39657b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */ 39669566063dSJacob Faibussowitsch PetscCall(TSForwardCostIntegral(ts)); 39677b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps++; 3968b1cb36f3SHong Zhang } 396958818c2dSLisandro Dalcin if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */ 39707b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */ 39719566063dSJacob Faibussowitsch PetscCall(TSForwardStep(ts)); 39727b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps++; 3973715f1b00SHong Zhang } 39749566063dSJacob Faibussowitsch PetscCall(TSPostEvaluate(ts)); 39759566063dSJacob 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. */ 397658818c2dSLisandro Dalcin if (ts->steprollback) { 39779566063dSJacob Faibussowitsch PetscCall(TSPostEvaluate(ts)); 397858818c2dSLisandro Dalcin } 3979e783b05fSHong Zhang if (!ts->steprollback) { 39809566063dSJacob Faibussowitsch PetscCall(TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol)); 39819566063dSJacob Faibussowitsch PetscCall(TSPostStep(ts)); 3982aeb4809dSShri Abhyankar } 3983193ac0bcSJed Brown } 39849566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol)); 39856427ac75SLisandro Dalcin 398649354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 39879566063dSJacob Faibussowitsch PetscCall(TSInterpolate(ts,ts->max_time,u)); 3988cc708dedSBarry Smith ts->solvetime = ts->max_time; 3989b06615a5SLisandro Dalcin solution = u; 39909566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts,-1,ts->solvetime,solution)); 39910574a7fbSJed Brown } else { 39929566063dSJacob Faibussowitsch if (u) PetscCall(VecCopy(ts->vec_sol,u)); 3993cc708dedSBarry Smith ts->solvetime = ts->ptime; 3994b06615a5SLisandro Dalcin solution = ts->vec_sol; 39950574a7fbSJed Brown } 3996193ac0bcSJed Brown } 3997d2daff3dSHong Zhang 39989566063dSJacob Faibussowitsch PetscCall(TSViewFromOptions(ts,NULL,"-ts_view")); 39999566063dSJacob Faibussowitsch PetscCall(VecViewFromOptions(solution,(PetscObject)ts,"-ts_view_solution")); 40009566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsBlock((PetscObject)ts)); 4001ef222394SBarry Smith if (ts->adjoint_solve) { 40029566063dSJacob Faibussowitsch PetscCall(TSAdjointSolve(ts)); 40032b0a91c0SBarry Smith } 40046a4d4014SLisandro Dalcin PetscFunctionReturn(0); 40056a4d4014SLisandro Dalcin } 40066a4d4014SLisandro Dalcin 4007d763cef2SBarry Smith /*@ 4008b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 4009d763cef2SBarry Smith 4010d763cef2SBarry Smith Not Collective 4011d763cef2SBarry Smith 4012d763cef2SBarry Smith Input Parameter: 4013d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 4014d763cef2SBarry Smith 4015d763cef2SBarry Smith Output Parameter: 401619eac22cSLisandro Dalcin . t - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime(). 4017d763cef2SBarry Smith 4018d763cef2SBarry Smith Level: beginner 4019d763cef2SBarry Smith 4020b8123daeSJed Brown Note: 4021b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 40229be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 4023b8123daeSJed Brown 4024db781477SPatrick Sanan .seealso: `TSGetSolveTime()`, `TSSetTime()`, `TSGetTimeStep()`, `TSGetStepNumber()` 4025d763cef2SBarry Smith 4026d763cef2SBarry Smith @*/ 40277087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 4028d763cef2SBarry Smith { 4029d763cef2SBarry Smith PetscFunctionBegin; 40300700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4031f7cf8827SBarry Smith PetscValidRealPointer(t,2); 4032d763cef2SBarry Smith *t = ts->ptime; 4033d763cef2SBarry Smith PetscFunctionReturn(0); 4034d763cef2SBarry Smith } 4035d763cef2SBarry Smith 4036e5e524a1SHong Zhang /*@ 4037e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 4038e5e524a1SHong Zhang 4039e5e524a1SHong Zhang Not Collective 4040e5e524a1SHong Zhang 4041e5e524a1SHong Zhang Input Parameter: 4042e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 4043e5e524a1SHong Zhang 4044e5e524a1SHong Zhang Output Parameter: 4045e5e524a1SHong Zhang . t - the previous time 4046e5e524a1SHong Zhang 4047e5e524a1SHong Zhang Level: beginner 4048e5e524a1SHong Zhang 4049db781477SPatrick Sanan .seealso: `TSGetTime()`, `TSGetSolveTime()`, `TSGetTimeStep()` 4050e5e524a1SHong Zhang 4051e5e524a1SHong Zhang @*/ 4052e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 4053e5e524a1SHong Zhang { 4054e5e524a1SHong Zhang PetscFunctionBegin; 4055e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4056e5e524a1SHong Zhang PetscValidRealPointer(t,2); 4057e5e524a1SHong Zhang *t = ts->ptime_prev; 4058e5e524a1SHong Zhang PetscFunctionReturn(0); 4059e5e524a1SHong Zhang } 4060e5e524a1SHong Zhang 40616a4d4014SLisandro Dalcin /*@ 40626a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 40636a4d4014SLisandro Dalcin 40643f9fe445SBarry Smith Logically Collective on TS 40656a4d4014SLisandro Dalcin 40666a4d4014SLisandro Dalcin Input Parameters: 40676a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 40686a4d4014SLisandro Dalcin - time - the time 40696a4d4014SLisandro Dalcin 40706a4d4014SLisandro Dalcin Level: intermediate 40716a4d4014SLisandro Dalcin 4072db781477SPatrick Sanan .seealso: `TSGetTime()`, `TSSetMaxSteps()` 40736a4d4014SLisandro Dalcin 40746a4d4014SLisandro Dalcin @*/ 40757087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 40766a4d4014SLisandro Dalcin { 40776a4d4014SLisandro Dalcin PetscFunctionBegin; 40780700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4079c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 40806a4d4014SLisandro Dalcin ts->ptime = t; 40816a4d4014SLisandro Dalcin PetscFunctionReturn(0); 40826a4d4014SLisandro Dalcin } 40836a4d4014SLisandro Dalcin 4084d763cef2SBarry Smith /*@C 4085d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 4086d763cef2SBarry Smith TS options in the database. 4087d763cef2SBarry Smith 40883f9fe445SBarry Smith Logically Collective on TS 4089d763cef2SBarry Smith 4090d8d19677SJose E. Roman Input Parameters: 4091d763cef2SBarry Smith + ts - The TS context 4092d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4093d763cef2SBarry Smith 4094d763cef2SBarry Smith Notes: 4095d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4096d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4097d763cef2SBarry Smith hyphen. 4098d763cef2SBarry Smith 4099d763cef2SBarry Smith Level: advanced 4100d763cef2SBarry Smith 4101db781477SPatrick Sanan .seealso: `TSSetFromOptions()` 4102d763cef2SBarry Smith 4103d763cef2SBarry Smith @*/ 41047087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 4105d763cef2SBarry Smith { 4106089b2837SJed Brown SNES snes; 4107d763cef2SBarry Smith 4108d763cef2SBarry Smith PetscFunctionBegin; 41090700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 41109566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)ts,prefix)); 41119566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 41129566063dSJacob Faibussowitsch PetscCall(SNESSetOptionsPrefix(snes,prefix)); 4113d763cef2SBarry Smith PetscFunctionReturn(0); 4114d763cef2SBarry Smith } 4115d763cef2SBarry Smith 4116d763cef2SBarry Smith /*@C 4117d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 4118d763cef2SBarry Smith TS options in the database. 4119d763cef2SBarry Smith 41203f9fe445SBarry Smith Logically Collective on TS 4121d763cef2SBarry Smith 4122d8d19677SJose E. Roman Input Parameters: 4123d763cef2SBarry Smith + ts - The TS context 4124d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4125d763cef2SBarry Smith 4126d763cef2SBarry Smith Notes: 4127d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4128d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4129d763cef2SBarry Smith hyphen. 4130d763cef2SBarry Smith 4131d763cef2SBarry Smith Level: advanced 4132d763cef2SBarry Smith 4133db781477SPatrick Sanan .seealso: `TSGetOptionsPrefix()` 4134d763cef2SBarry Smith 4135d763cef2SBarry Smith @*/ 41367087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 4137d763cef2SBarry Smith { 4138089b2837SJed Brown SNES snes; 4139d763cef2SBarry Smith 4140d763cef2SBarry Smith PetscFunctionBegin; 41410700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 41429566063dSJacob Faibussowitsch PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix)); 41439566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 41449566063dSJacob Faibussowitsch PetscCall(SNESAppendOptionsPrefix(snes,prefix)); 4145d763cef2SBarry Smith PetscFunctionReturn(0); 4146d763cef2SBarry Smith } 4147d763cef2SBarry Smith 4148d763cef2SBarry Smith /*@C 4149d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 4150d763cef2SBarry Smith TS options in the database. 4151d763cef2SBarry Smith 4152d763cef2SBarry Smith Not Collective 4153d763cef2SBarry Smith 4154d763cef2SBarry Smith Input Parameter: 4155d763cef2SBarry Smith . ts - The TS context 4156d763cef2SBarry Smith 4157d763cef2SBarry Smith Output Parameter: 4158d763cef2SBarry Smith . prefix - A pointer to the prefix string used 4159d763cef2SBarry Smith 416095452b02SPatrick Sanan Notes: 416195452b02SPatrick Sanan On the fortran side, the user should pass in a string 'prifix' of 4162d763cef2SBarry Smith sufficient length to hold the prefix. 4163d763cef2SBarry Smith 4164d763cef2SBarry Smith Level: intermediate 4165d763cef2SBarry Smith 4166db781477SPatrick Sanan .seealso: `TSAppendOptionsPrefix()` 4167d763cef2SBarry Smith @*/ 41687087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 4169d763cef2SBarry Smith { 4170d763cef2SBarry Smith PetscFunctionBegin; 41710700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 41724482741eSBarry Smith PetscValidPointer(prefix,2); 41739566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)ts,prefix)); 4174d763cef2SBarry Smith PetscFunctionReturn(0); 4175d763cef2SBarry Smith } 4176d763cef2SBarry Smith 4177d763cef2SBarry Smith /*@C 4178d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4179d763cef2SBarry Smith 4180d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 4181d763cef2SBarry Smith 4182d763cef2SBarry Smith Input Parameter: 4183d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 4184d763cef2SBarry Smith 4185d763cef2SBarry Smith Output Parameters: 4186e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 4187e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 4188e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 4189e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 4190d763cef2SBarry Smith 419195452b02SPatrick Sanan Notes: 419295452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 4193d763cef2SBarry Smith 4194d763cef2SBarry Smith Level: intermediate 4195d763cef2SBarry Smith 4196db781477SPatrick Sanan .seealso: `TSGetTimeStep()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()` 4197d763cef2SBarry Smith 4198d763cef2SBarry Smith @*/ 4199e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 4200d763cef2SBarry Smith { 420124989b8cSPeter Brune DM dm; 4202089b2837SJed Brown 4203d763cef2SBarry Smith PetscFunctionBegin; 420423a57915SBarry Smith if (Amat || Pmat) { 420523a57915SBarry Smith SNES snes; 42069566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 42079566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 42089566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes,Amat,Pmat,NULL,NULL)); 420923a57915SBarry Smith } 42109566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 42119566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm,func,ctx)); 4212d763cef2SBarry Smith PetscFunctionReturn(0); 4213d763cef2SBarry Smith } 4214d763cef2SBarry Smith 42152eca1d9cSJed Brown /*@C 42162eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 42172eca1d9cSJed Brown 42182eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 42192eca1d9cSJed Brown 42202eca1d9cSJed Brown Input Parameter: 42212eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 42222eca1d9cSJed Brown 42232eca1d9cSJed Brown Output Parameters: 4224e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4225e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 42262eca1d9cSJed Brown . f - The function to compute the matrices 42272eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 42282eca1d9cSJed Brown 422995452b02SPatrick Sanan Notes: 423095452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 42312eca1d9cSJed Brown 42322eca1d9cSJed Brown Level: advanced 42332eca1d9cSJed Brown 4234db781477SPatrick Sanan .seealso: `TSGetTimeStep()`, `TSGetRHSJacobian()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()` 42352eca1d9cSJed Brown 42362eca1d9cSJed Brown @*/ 4237e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 42382eca1d9cSJed Brown { 423924989b8cSPeter Brune DM dm; 42400910c330SBarry Smith 42412eca1d9cSJed Brown PetscFunctionBegin; 4242c0aab802Sstefano_zampini if (Amat || Pmat) { 4243c0aab802Sstefano_zampini SNES snes; 42449566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 42459566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 42469566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes,Amat,Pmat,NULL,NULL)); 4247c0aab802Sstefano_zampini } 42489566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 42499566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm,f,ctx)); 42502eca1d9cSJed Brown PetscFunctionReturn(0); 42512eca1d9cSJed Brown } 42522eca1d9cSJed Brown 4253af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 42546c699258SBarry Smith /*@ 42552a808120SBarry Smith TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS 42566c699258SBarry Smith 4257d083f849SBarry Smith Logically Collective on ts 42586c699258SBarry Smith 42596c699258SBarry Smith Input Parameters: 42602a808120SBarry Smith + ts - the ODE integrator object 42612a808120SBarry Smith - dm - the dm, cannot be NULL 42626c699258SBarry Smith 4263e03a659cSJed Brown Notes: 4264e03a659cSJed Brown A DM can only be used for solving one problem at a time because information about the problem is stored on the DM, 4265e03a659cSJed Brown even when not using interfaces like DMTSSetIFunction(). Use DMClone() to get a distinct DM when solving 4266e03a659cSJed Brown different problems using the same function space. 4267e03a659cSJed Brown 42686c699258SBarry Smith Level: intermediate 42696c699258SBarry Smith 4270db781477SPatrick Sanan .seealso: `TSGetDM()`, `SNESSetDM()`, `SNESGetDM()` 42716c699258SBarry Smith @*/ 42727087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 42736c699258SBarry Smith { 4274089b2837SJed Brown SNES snes; 4275942e3340SBarry Smith DMTS tsdm; 42766c699258SBarry Smith 42776c699258SBarry Smith PetscFunctionBegin; 42780700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 42792a808120SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,2); 42809566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)dm)); 4281942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 42822a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 42839566063dSJacob Faibussowitsch PetscCall(DMCopyDMTS(ts->dm,dm)); 42849566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm,&tsdm)); 428524989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 428624989b8cSPeter Brune tsdm->originaldm = dm; 428724989b8cSPeter Brune } 428824989b8cSPeter Brune } 42899566063dSJacob Faibussowitsch PetscCall(DMDestroy(&ts->dm)); 429024989b8cSPeter Brune } 42916c699258SBarry Smith ts->dm = dm; 4292bbd56ea5SKarl Rupp 42939566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 42949566063dSJacob Faibussowitsch PetscCall(SNESSetDM(snes,dm)); 42956c699258SBarry Smith PetscFunctionReturn(0); 42966c699258SBarry Smith } 42976c699258SBarry Smith 42986c699258SBarry Smith /*@ 42996c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 43006c699258SBarry Smith 43013f9fe445SBarry Smith Not Collective 43026c699258SBarry Smith 43036c699258SBarry Smith Input Parameter: 43046c699258SBarry Smith . ts - the preconditioner context 43056c699258SBarry Smith 43066c699258SBarry Smith Output Parameter: 43076c699258SBarry Smith . dm - the dm 43086c699258SBarry Smith 43096c699258SBarry Smith Level: intermediate 43106c699258SBarry Smith 4311db781477SPatrick Sanan .seealso: `TSSetDM()`, `SNESSetDM()`, `SNESGetDM()` 43126c699258SBarry Smith @*/ 43137087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 43146c699258SBarry Smith { 43156c699258SBarry Smith PetscFunctionBegin; 43160700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4317496e6a7aSJed Brown if (!ts->dm) { 43189566063dSJacob Faibussowitsch PetscCall(DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm)); 43199566063dSJacob Faibussowitsch if (ts->snes) PetscCall(SNESSetDM(ts->snes,ts->dm)); 4320496e6a7aSJed Brown } 43216c699258SBarry Smith *dm = ts->dm; 43226c699258SBarry Smith PetscFunctionReturn(0); 43236c699258SBarry Smith } 43241713a123SBarry Smith 43250f5c6efeSJed Brown /*@ 43260f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 43270f5c6efeSJed Brown 43283f9fe445SBarry Smith Logically Collective on SNES 43290f5c6efeSJed Brown 4330d8d19677SJose E. Roman Input Parameters: 4331d42a1c89SJed Brown + snes - nonlinear solver 43320910c330SBarry Smith . U - the current state at which to evaluate the residual 4333d42a1c89SJed Brown - ctx - user context, must be a TS 43340f5c6efeSJed Brown 43350f5c6efeSJed Brown Output Parameter: 43360f5c6efeSJed Brown . F - the nonlinear residual 43370f5c6efeSJed Brown 43380f5c6efeSJed Brown Notes: 43390f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 43400f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 43410f5c6efeSJed Brown 43420f5c6efeSJed Brown Level: advanced 43430f5c6efeSJed Brown 4344db781477SPatrick Sanan .seealso: `SNESSetFunction()`, `MatFDColoringSetFunction()` 43450f5c6efeSJed Brown @*/ 43460910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 43470f5c6efeSJed Brown { 43480f5c6efeSJed Brown TS ts = (TS)ctx; 43490f5c6efeSJed Brown 43500f5c6efeSJed Brown PetscFunctionBegin; 43510f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 43520910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 43530f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 43540f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 43559566063dSJacob Faibussowitsch PetscCall((ts->ops->snesfunction)(snes,U,F,ts)); 43560f5c6efeSJed Brown PetscFunctionReturn(0); 43570f5c6efeSJed Brown } 43580f5c6efeSJed Brown 43590f5c6efeSJed Brown /*@ 43600f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 43610f5c6efeSJed Brown 43620f5c6efeSJed Brown Collective on SNES 43630f5c6efeSJed Brown 4364d8d19677SJose E. Roman Input Parameters: 43650f5c6efeSJed Brown + snes - nonlinear solver 43660910c330SBarry Smith . U - the current state at which to evaluate the residual 43670f5c6efeSJed Brown - ctx - user context, must be a TS 43680f5c6efeSJed Brown 4369d8d19677SJose E. Roman Output Parameters: 43700f5c6efeSJed Brown + A - the Jacobian 43716b867d5aSJose E. Roman - B - the preconditioning matrix (may be the same as A) 43720f5c6efeSJed Brown 43730f5c6efeSJed Brown Notes: 43740f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 43750f5c6efeSJed Brown 43760f5c6efeSJed Brown Level: developer 43770f5c6efeSJed Brown 4378db781477SPatrick Sanan .seealso: `SNESSetJacobian()` 43790f5c6efeSJed Brown @*/ 4380d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 43810f5c6efeSJed Brown { 43820f5c6efeSJed Brown TS ts = (TS)ctx; 43830f5c6efeSJed Brown 43840f5c6efeSJed Brown PetscFunctionBegin; 43850f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 43860910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 43870f5c6efeSJed Brown PetscValidPointer(A,3); 438894ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 43890f5c6efeSJed Brown PetscValidPointer(B,4); 439094ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 4391064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(ts,TS_CLASSID,5); 43929566063dSJacob Faibussowitsch PetscCall((ts->ops->snesjacobian)(snes,U,A,B,ts)); 43930f5c6efeSJed Brown PetscFunctionReturn(0); 43940f5c6efeSJed Brown } 4395325fc9f4SBarry Smith 43960e4ef248SJed Brown /*@C 43979ae8fd06SBarry Smith TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only 43980e4ef248SJed Brown 43990e4ef248SJed Brown Collective on TS 44000e4ef248SJed Brown 44014165533cSJose E. Roman Input Parameters: 44020e4ef248SJed Brown + ts - time stepping context 44030e4ef248SJed Brown . t - time at which to evaluate 44040910c330SBarry Smith . U - state at which to evaluate 44050e4ef248SJed Brown - ctx - context 44060e4ef248SJed Brown 44074165533cSJose E. Roman Output Parameter: 44080e4ef248SJed Brown . F - right hand side 44090e4ef248SJed Brown 44100e4ef248SJed Brown Level: intermediate 44110e4ef248SJed Brown 44120e4ef248SJed Brown Notes: 44130e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 44140e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 44150e4ef248SJed Brown 4416db781477SPatrick Sanan .seealso: `TSSetRHSFunction()`, `TSSetRHSJacobian()`, `TSComputeRHSJacobianConstant()` 44170e4ef248SJed Brown @*/ 44180910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 44190e4ef248SJed Brown { 44200e4ef248SJed Brown Mat Arhs,Brhs; 44210e4ef248SJed Brown 44220e4ef248SJed Brown PetscFunctionBegin; 44239566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts,&Arhs,&Brhs)); 44242663174eSHong Zhang /* undo the damage caused by shifting */ 44259566063dSJacob Faibussowitsch PetscCall(TSRecoverRHSJacobian(ts,Arhs,Brhs)); 44269566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts,t,U,Arhs,Brhs)); 44279566063dSJacob Faibussowitsch PetscCall(MatMult(Arhs,U,F)); 44280e4ef248SJed Brown PetscFunctionReturn(0); 44290e4ef248SJed Brown } 44300e4ef248SJed Brown 44310e4ef248SJed Brown /*@C 44320e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 44330e4ef248SJed Brown 44340e4ef248SJed Brown Collective on TS 44350e4ef248SJed Brown 44364165533cSJose E. Roman Input Parameters: 44370e4ef248SJed Brown + ts - time stepping context 44380e4ef248SJed Brown . t - time at which to evaluate 44390910c330SBarry Smith . U - state at which to evaluate 44400e4ef248SJed Brown - ctx - context 44410e4ef248SJed Brown 44424165533cSJose E. Roman Output Parameters: 44430e4ef248SJed Brown + A - pointer to operator 444497bb3fdcSJose E. Roman - B - pointer to preconditioning matrix 44450e4ef248SJed Brown 44460e4ef248SJed Brown Level: intermediate 44470e4ef248SJed Brown 44480e4ef248SJed Brown Notes: 44490e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 44500e4ef248SJed Brown 4451db781477SPatrick Sanan .seealso: `TSSetRHSFunction()`, `TSSetRHSJacobian()`, `TSComputeRHSFunctionLinear()` 44520e4ef248SJed Brown @*/ 4453d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 44540e4ef248SJed Brown { 44550e4ef248SJed Brown PetscFunctionBegin; 44560e4ef248SJed Brown PetscFunctionReturn(0); 44570e4ef248SJed Brown } 44580e4ef248SJed Brown 44590026cea9SSean Farley /*@C 44600026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 44610026cea9SSean Farley 44620026cea9SSean Farley Collective on TS 44630026cea9SSean Farley 44644165533cSJose E. Roman Input Parameters: 44650026cea9SSean Farley + ts - time stepping context 44660026cea9SSean Farley . t - time at which to evaluate 44670910c330SBarry Smith . U - state at which to evaluate 44680910c330SBarry Smith . Udot - time derivative of state vector 44690026cea9SSean Farley - ctx - context 44700026cea9SSean Farley 44714165533cSJose E. Roman Output Parameter: 44720026cea9SSean Farley . F - left hand side 44730026cea9SSean Farley 44740026cea9SSean Farley Level: intermediate 44750026cea9SSean Farley 44760026cea9SSean Farley Notes: 44770910c330SBarry 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 44780026cea9SSean Farley user is required to write their own TSComputeIFunction. 44790026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 44800026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 44810026cea9SSean Farley 44829ae8fd06SBarry Smith Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U 44839ae8fd06SBarry Smith 4484db781477SPatrick Sanan .seealso: `TSSetIFunction()`, `TSSetIJacobian()`, `TSComputeIJacobianConstant()`, `TSComputeRHSFunctionLinear()` 44850026cea9SSean Farley @*/ 44860910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 44870026cea9SSean Farley { 44880026cea9SSean Farley Mat A,B; 44890026cea9SSean Farley 44900026cea9SSean Farley PetscFunctionBegin; 44919566063dSJacob Faibussowitsch PetscCall(TSGetIJacobian(ts,&A,&B,NULL,NULL)); 44929566063dSJacob Faibussowitsch PetscCall(TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE)); 44939566063dSJacob Faibussowitsch PetscCall(MatMult(A,Udot,F)); 44940026cea9SSean Farley PetscFunctionReturn(0); 44950026cea9SSean Farley } 44960026cea9SSean Farley 44970026cea9SSean Farley /*@C 4498b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 44990026cea9SSean Farley 45000026cea9SSean Farley Collective on TS 45010026cea9SSean Farley 45024165533cSJose E. Roman Input Parameters: 45030026cea9SSean Farley + ts - time stepping context 45040026cea9SSean Farley . t - time at which to evaluate 45050910c330SBarry Smith . U - state at which to evaluate 45060910c330SBarry Smith . Udot - time derivative of state vector 45070026cea9SSean Farley . shift - shift to apply 45080026cea9SSean Farley - ctx - context 45090026cea9SSean Farley 45104165533cSJose E. Roman Output Parameters: 45110026cea9SSean Farley + A - pointer to operator 451297bb3fdcSJose E. Roman - B - pointer to preconditioning matrix 45130026cea9SSean Farley 4514b41af12eSJed Brown Level: advanced 45150026cea9SSean Farley 45160026cea9SSean Farley Notes: 45170026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 45180026cea9SSean Farley 4519b41af12eSJed Brown It is only appropriate for problems of the form 4520b41af12eSJed Brown 4521b41af12eSJed Brown $ M Udot = F(U,t) 4522b41af12eSJed Brown 4523b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 4524b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 4525b41af12eSJed Brown an implicit operator of the form 4526b41af12eSJed Brown 4527b41af12eSJed Brown $ shift*M + J 4528b41af12eSJed Brown 4529b41af12eSJed 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 4530b41af12eSJed Brown a copy of M or reassemble it when requested. 4531b41af12eSJed Brown 4532db781477SPatrick Sanan .seealso: `TSSetIFunction()`, `TSSetIJacobian()`, `TSComputeIFunctionLinear()` 45330026cea9SSean Farley @*/ 4534d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 45350026cea9SSean Farley { 45360026cea9SSean Farley PetscFunctionBegin; 45379566063dSJacob Faibussowitsch PetscCall(MatScale(A, shift / ts->ijacobian.shift)); 4538b41af12eSJed Brown ts->ijacobian.shift = shift; 45390026cea9SSean Farley PetscFunctionReturn(0); 45400026cea9SSean Farley } 4541b41af12eSJed Brown 4542e817cc15SEmil Constantinescu /*@ 4543e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 4544e817cc15SEmil Constantinescu 4545e817cc15SEmil Constantinescu Not Collective 4546e817cc15SEmil Constantinescu 4547e817cc15SEmil Constantinescu Input Parameter: 4548e817cc15SEmil Constantinescu . ts - the TS context 4549e817cc15SEmil Constantinescu 4550e817cc15SEmil Constantinescu Output Parameter: 45514e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 4552e817cc15SEmil Constantinescu 4553e817cc15SEmil Constantinescu Level: beginner 4554e817cc15SEmil Constantinescu 4555db781477SPatrick Sanan .seealso: `TSSetEquationType()`, `TSEquationType` 4556e817cc15SEmil Constantinescu @*/ 4557e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 4558e817cc15SEmil Constantinescu { 4559e817cc15SEmil Constantinescu PetscFunctionBegin; 4560e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4561e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 4562e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 4563e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4564e817cc15SEmil Constantinescu } 4565e817cc15SEmil Constantinescu 4566e817cc15SEmil Constantinescu /*@ 4567e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 4568e817cc15SEmil Constantinescu 4569e817cc15SEmil Constantinescu Not Collective 4570e817cc15SEmil Constantinescu 4571d8d19677SJose E. Roman Input Parameters: 4572e817cc15SEmil Constantinescu + ts - the TS context 45731297b224SEmil Constantinescu - equation_type - see TSEquationType 4574e817cc15SEmil Constantinescu 4575e817cc15SEmil Constantinescu Level: advanced 4576e817cc15SEmil Constantinescu 4577db781477SPatrick Sanan .seealso: `TSGetEquationType()`, `TSEquationType` 4578e817cc15SEmil Constantinescu @*/ 4579e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 4580e817cc15SEmil Constantinescu { 4581e817cc15SEmil Constantinescu PetscFunctionBegin; 4582e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4583e817cc15SEmil Constantinescu ts->equation_type = equation_type; 4584e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4585e817cc15SEmil Constantinescu } 45860026cea9SSean Farley 45874af1b03aSJed Brown /*@ 45884af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 45894af1b03aSJed Brown 45904af1b03aSJed Brown Not Collective 45914af1b03aSJed Brown 45924af1b03aSJed Brown Input Parameter: 45934af1b03aSJed Brown . ts - the TS context 45944af1b03aSJed Brown 45954af1b03aSJed Brown Output Parameter: 45964af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 45974af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 45984af1b03aSJed Brown 4599487e0bb9SJed Brown Level: beginner 46004af1b03aSJed Brown 4601cd652676SJed Brown Notes: 4602cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 46034af1b03aSJed Brown 4604db781477SPatrick Sanan .seealso: `TSSetConvergenceTest()`, `TSConvergedReason` 46054af1b03aSJed Brown @*/ 46064af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 46074af1b03aSJed Brown { 46084af1b03aSJed Brown PetscFunctionBegin; 46094af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 46104af1b03aSJed Brown PetscValidPointer(reason,2); 46114af1b03aSJed Brown *reason = ts->reason; 46124af1b03aSJed Brown PetscFunctionReturn(0); 46134af1b03aSJed Brown } 46144af1b03aSJed Brown 4615d6ad946cSShri Abhyankar /*@ 4616d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 4617d6ad946cSShri Abhyankar 46186b221cbeSPatrick Sanan Logically Collective; reason must contain common value 4619d6ad946cSShri Abhyankar 46206b221cbeSPatrick Sanan Input Parameters: 4621d6ad946cSShri Abhyankar + ts - the TS context 46226b221cbeSPatrick Sanan - reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 4623d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 4624d6ad946cSShri Abhyankar 4625f5abba47SShri Abhyankar Level: advanced 4626d6ad946cSShri Abhyankar 4627d6ad946cSShri Abhyankar Notes: 46286b221cbeSPatrick Sanan Can only be called while TSSolve() is active. 4629d6ad946cSShri Abhyankar 4630db781477SPatrick Sanan .seealso: `TSConvergedReason` 4631d6ad946cSShri Abhyankar @*/ 4632d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 4633d6ad946cSShri Abhyankar { 4634d6ad946cSShri Abhyankar PetscFunctionBegin; 4635d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4636d6ad946cSShri Abhyankar ts->reason = reason; 4637d6ad946cSShri Abhyankar PetscFunctionReturn(0); 4638d6ad946cSShri Abhyankar } 4639d6ad946cSShri Abhyankar 4640cc708dedSBarry Smith /*@ 4641cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 4642cc708dedSBarry Smith 4643cc708dedSBarry Smith Not Collective 4644cc708dedSBarry Smith 4645cc708dedSBarry Smith Input Parameter: 4646cc708dedSBarry Smith . ts - the TS context 4647cc708dedSBarry Smith 4648cc708dedSBarry Smith Output Parameter: 464919eac22cSLisandro Dalcin . ftime - the final time. This time corresponds to the final time set with TSSetMaxTime() 4650cc708dedSBarry Smith 4651487e0bb9SJed Brown Level: beginner 4652cc708dedSBarry Smith 4653cc708dedSBarry Smith Notes: 4654cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 4655cc708dedSBarry Smith 4656db781477SPatrick Sanan .seealso: `TSSetConvergenceTest()`, `TSConvergedReason` 4657cc708dedSBarry Smith @*/ 4658cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 4659cc708dedSBarry Smith { 4660cc708dedSBarry Smith PetscFunctionBegin; 4661cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4662dadcf809SJacob Faibussowitsch PetscValidRealPointer(ftime,2); 4663cc708dedSBarry Smith *ftime = ts->solvetime; 4664cc708dedSBarry Smith PetscFunctionReturn(0); 4665cc708dedSBarry Smith } 4666cc708dedSBarry Smith 46672c18e0fdSBarry Smith /*@ 46685ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 46699f67acb7SJed Brown used by the time integrator. 46709f67acb7SJed Brown 46719f67acb7SJed Brown Not Collective 46729f67acb7SJed Brown 46739f67acb7SJed Brown Input Parameter: 46749f67acb7SJed Brown . ts - TS context 46759f67acb7SJed Brown 46769f67acb7SJed Brown Output Parameter: 46779f67acb7SJed Brown . nits - number of nonlinear iterations 46789f67acb7SJed Brown 46799f67acb7SJed Brown Notes: 46809f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 46819f67acb7SJed Brown 46829f67acb7SJed Brown Level: intermediate 46839f67acb7SJed Brown 4684db781477SPatrick Sanan .seealso: `TSGetKSPIterations()` 46859f67acb7SJed Brown @*/ 46865ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 46879f67acb7SJed Brown { 46889f67acb7SJed Brown PetscFunctionBegin; 46899f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 46909f67acb7SJed Brown PetscValidIntPointer(nits,2); 46915ef26d82SJed Brown *nits = ts->snes_its; 46929f67acb7SJed Brown PetscFunctionReturn(0); 46939f67acb7SJed Brown } 46949f67acb7SJed Brown 46959f67acb7SJed Brown /*@ 46965ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 46979f67acb7SJed Brown used by the time integrator. 46989f67acb7SJed Brown 46999f67acb7SJed Brown Not Collective 47009f67acb7SJed Brown 47019f67acb7SJed Brown Input Parameter: 47029f67acb7SJed Brown . ts - TS context 47039f67acb7SJed Brown 47049f67acb7SJed Brown Output Parameter: 47059f67acb7SJed Brown . lits - number of linear iterations 47069f67acb7SJed Brown 47079f67acb7SJed Brown Notes: 47089f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 47099f67acb7SJed Brown 47109f67acb7SJed Brown Level: intermediate 47119f67acb7SJed Brown 4712db781477SPatrick Sanan .seealso: `TSGetSNESIterations()`, `SNESGetKSPIterations()` 47139f67acb7SJed Brown @*/ 47145ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 47159f67acb7SJed Brown { 47169f67acb7SJed Brown PetscFunctionBegin; 47179f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 47189f67acb7SJed Brown PetscValidIntPointer(lits,2); 47195ef26d82SJed Brown *lits = ts->ksp_its; 47209f67acb7SJed Brown PetscFunctionReturn(0); 47219f67acb7SJed Brown } 47229f67acb7SJed Brown 4723cef5090cSJed Brown /*@ 4724cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 4725cef5090cSJed Brown 4726cef5090cSJed Brown Not Collective 4727cef5090cSJed Brown 4728cef5090cSJed Brown Input Parameter: 4729cef5090cSJed Brown . ts - TS context 4730cef5090cSJed Brown 4731cef5090cSJed Brown Output Parameter: 4732cef5090cSJed Brown . rejects - number of steps rejected 4733cef5090cSJed Brown 4734cef5090cSJed Brown Notes: 4735cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4736cef5090cSJed Brown 4737cef5090cSJed Brown Level: intermediate 4738cef5090cSJed Brown 4739db781477SPatrick Sanan .seealso: `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetSNESFailures()`, `TSSetMaxSNESFailures()`, `TSSetErrorIfStepFails()` 4740cef5090cSJed Brown @*/ 4741cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 4742cef5090cSJed Brown { 4743cef5090cSJed Brown PetscFunctionBegin; 4744cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4745cef5090cSJed Brown PetscValidIntPointer(rejects,2); 4746cef5090cSJed Brown *rejects = ts->reject; 4747cef5090cSJed Brown PetscFunctionReturn(0); 4748cef5090cSJed Brown } 4749cef5090cSJed Brown 4750cef5090cSJed Brown /*@ 4751cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 4752cef5090cSJed Brown 4753cef5090cSJed Brown Not Collective 4754cef5090cSJed Brown 4755cef5090cSJed Brown Input Parameter: 4756cef5090cSJed Brown . ts - TS context 4757cef5090cSJed Brown 4758cef5090cSJed Brown Output Parameter: 4759cef5090cSJed Brown . fails - number of failed nonlinear solves 4760cef5090cSJed Brown 4761cef5090cSJed Brown Notes: 4762cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4763cef5090cSJed Brown 4764cef5090cSJed Brown Level: intermediate 4765cef5090cSJed Brown 4766db781477SPatrick Sanan .seealso: `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSSetMaxSNESFailures()` 4767cef5090cSJed Brown @*/ 4768cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 4769cef5090cSJed Brown { 4770cef5090cSJed Brown PetscFunctionBegin; 4771cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4772cef5090cSJed Brown PetscValidIntPointer(fails,2); 4773cef5090cSJed Brown *fails = ts->num_snes_failures; 4774cef5090cSJed Brown PetscFunctionReturn(0); 4775cef5090cSJed Brown } 4776cef5090cSJed Brown 4777cef5090cSJed Brown /*@ 4778cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 4779cef5090cSJed Brown 4780cef5090cSJed Brown Not Collective 4781cef5090cSJed Brown 4782d8d19677SJose E. Roman Input Parameters: 4783cef5090cSJed Brown + ts - TS context 4784cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 4785cef5090cSJed Brown 4786cef5090cSJed Brown Notes: 4787cef5090cSJed Brown The counter is reset to zero for each step 4788cef5090cSJed Brown 4789cef5090cSJed Brown Options Database Key: 4790cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 4791cef5090cSJed Brown 4792cef5090cSJed Brown Level: intermediate 4793cef5090cSJed Brown 4794db781477SPatrick Sanan .seealso: `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxSNESFailures()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `TSSetErrorIfStepFails()`, `TSGetConvergedReason()` 4795cef5090cSJed Brown @*/ 4796cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 4797cef5090cSJed Brown { 4798cef5090cSJed Brown PetscFunctionBegin; 4799cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4800cef5090cSJed Brown ts->max_reject = rejects; 4801cef5090cSJed Brown PetscFunctionReturn(0); 4802cef5090cSJed Brown } 4803cef5090cSJed Brown 4804cef5090cSJed Brown /*@ 4805cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 4806cef5090cSJed Brown 4807cef5090cSJed Brown Not Collective 4808cef5090cSJed Brown 4809d8d19677SJose E. Roman Input Parameters: 4810cef5090cSJed Brown + ts - TS context 4811cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 4812cef5090cSJed Brown 4813cef5090cSJed Brown Notes: 4814cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 4815cef5090cSJed Brown 4816cef5090cSJed Brown Options Database Key: 4817cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 4818cef5090cSJed Brown 4819cef5090cSJed Brown Level: intermediate 4820cef5090cSJed Brown 4821db781477SPatrick Sanan .seealso: `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `SNESGetConvergedReason()`, `TSGetConvergedReason()` 4822cef5090cSJed Brown @*/ 4823cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 4824cef5090cSJed Brown { 4825cef5090cSJed Brown PetscFunctionBegin; 4826cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4827cef5090cSJed Brown ts->max_snes_failures = fails; 4828cef5090cSJed Brown PetscFunctionReturn(0); 4829cef5090cSJed Brown } 4830cef5090cSJed Brown 4831cef5090cSJed Brown /*@ 4832cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 4833cef5090cSJed Brown 4834cef5090cSJed Brown Not Collective 4835cef5090cSJed Brown 4836d8d19677SJose E. Roman Input Parameters: 4837cef5090cSJed Brown + ts - TS context 4838cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 4839cef5090cSJed Brown 4840cef5090cSJed Brown Options Database Key: 4841cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 4842cef5090cSJed Brown 4843cef5090cSJed Brown Level: intermediate 4844cef5090cSJed Brown 4845db781477SPatrick Sanan .seealso: `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `TSSetErrorIfStepFails()`, `TSGetConvergedReason()` 4846cef5090cSJed Brown @*/ 4847cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 4848cef5090cSJed Brown { 4849cef5090cSJed Brown PetscFunctionBegin; 4850cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4851cef5090cSJed Brown ts->errorifstepfailed = err; 4852cef5090cSJed Brown PetscFunctionReturn(0); 4853cef5090cSJed Brown } 4854cef5090cSJed Brown 485584df9cb4SJed Brown /*@ 4856552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 485784df9cb4SJed Brown 4858ed81e22dSJed Brown Collective on TS if controller has not been created yet 485984df9cb4SJed Brown 48604165533cSJose E. Roman Input Parameter: 4861ed81e22dSJed Brown . ts - time stepping context 486284df9cb4SJed Brown 48634165533cSJose E. Roman Output Parameter: 4864ed81e22dSJed Brown . adapt - adaptive controller 486584df9cb4SJed Brown 486684df9cb4SJed Brown Level: intermediate 486784df9cb4SJed Brown 4868db781477SPatrick Sanan .seealso: `TSAdapt`, `TSAdaptSetType()`, `TSAdaptChoose()` 486984df9cb4SJed Brown @*/ 4870552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 487184df9cb4SJed Brown { 487284df9cb4SJed Brown PetscFunctionBegin; 487384df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4874bec58848SLisandro Dalcin PetscValidPointer(adapt,2); 487584df9cb4SJed Brown if (!ts->adapt) { 48769566063dSJacob Faibussowitsch PetscCall(TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt)); 48779566063dSJacob Faibussowitsch PetscCall(PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt)); 48789566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1)); 487984df9cb4SJed Brown } 4880bec58848SLisandro Dalcin *adapt = ts->adapt; 488184df9cb4SJed Brown PetscFunctionReturn(0); 488284df9cb4SJed Brown } 4883d6ebe24aSShri Abhyankar 48841c3436cfSJed Brown /*@ 48851c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 48861c3436cfSJed Brown 48871c3436cfSJed Brown Logically Collective 48881c3436cfSJed Brown 48894165533cSJose E. Roman Input Parameters: 48901c3436cfSJed Brown + ts - time integration context 48911c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 48920298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 48931c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 48940298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 48951c3436cfSJed Brown 4896a3cdaa26SBarry Smith Options Database keys: 4897a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 489867b8a455SSatish Balay - -ts_atol <atol> - Absolute tolerance for local truncation error 4899a3cdaa26SBarry Smith 49003ff766beSShri Abhyankar Notes: 49013ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 49023ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 49033ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 49043ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 49053ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 49063ff766beSShri Abhyankar differential variables. 49073ff766beSShri Abhyankar 49081c3436cfSJed Brown Level: beginner 49091c3436cfSJed Brown 4910db781477SPatrick Sanan .seealso: `TS`, `TSAdapt`, `TSErrorWeightedNorm()`, `TSGetTolerances()` 49111c3436cfSJed Brown @*/ 49121c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 49131c3436cfSJed Brown { 49141c3436cfSJed Brown PetscFunctionBegin; 4915c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 49161c3436cfSJed Brown if (vatol) { 49179566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)vatol)); 49189566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vatol)); 49191c3436cfSJed Brown ts->vatol = vatol; 49201c3436cfSJed Brown } 4921c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 49221c3436cfSJed Brown if (vrtol) { 49239566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)vrtol)); 49249566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vrtol)); 49251c3436cfSJed Brown ts->vrtol = vrtol; 49261c3436cfSJed Brown } 49271c3436cfSJed Brown PetscFunctionReturn(0); 49281c3436cfSJed Brown } 49291c3436cfSJed Brown 4930c5033834SJed Brown /*@ 4931c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 4932c5033834SJed Brown 4933c5033834SJed Brown Logically Collective 4934c5033834SJed Brown 49354165533cSJose E. Roman Input Parameter: 4936c5033834SJed Brown . ts - time integration context 4937c5033834SJed Brown 49384165533cSJose E. Roman Output Parameters: 49390298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 49400298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 49410298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 49420298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 4943c5033834SJed Brown 4944c5033834SJed Brown Level: beginner 4945c5033834SJed Brown 4946db781477SPatrick Sanan .seealso: `TS`, `TSAdapt`, `TSErrorWeightedNorm()`, `TSSetTolerances()` 4947c5033834SJed Brown @*/ 4948c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 4949c5033834SJed Brown { 4950c5033834SJed Brown PetscFunctionBegin; 4951c5033834SJed Brown if (atol) *atol = ts->atol; 4952c5033834SJed Brown if (vatol) *vatol = ts->vatol; 4953c5033834SJed Brown if (rtol) *rtol = ts->rtol; 4954c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 4955c5033834SJed Brown PetscFunctionReturn(0); 4956c5033834SJed Brown } 4957c5033834SJed Brown 49589c6b16b5SShri Abhyankar /*@ 4959a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 49609c6b16b5SShri Abhyankar 49619c6b16b5SShri Abhyankar Collective on TS 49629c6b16b5SShri Abhyankar 49634165533cSJose E. Roman Input Parameters: 49649c6b16b5SShri Abhyankar + ts - time stepping context 4965a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 4966a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 49679c6b16b5SShri Abhyankar 49684165533cSJose E. Roman Output Parameters: 4969a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 49707453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 4971a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 49729c6b16b5SShri Abhyankar 49739c6b16b5SShri Abhyankar Level: developer 49749c6b16b5SShri Abhyankar 4975db781477SPatrick Sanan .seealso: `TSErrorWeightedNorm()`, `TSErrorWeightedNormInfinity()` 49769c6b16b5SShri Abhyankar @*/ 49777453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 49789c6b16b5SShri Abhyankar { 49799c6b16b5SShri Abhyankar PetscInt i,n,N,rstart; 49807453f775SEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 49817453f775SEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 49829c6b16b5SShri Abhyankar const PetscScalar *u,*y; 49837453f775SEmil Constantinescu PetscReal sum,suma,sumr,gsum,gsuma,gsumr,diff; 49847453f775SEmil Constantinescu PetscReal tol,tola,tolr; 49857453f775SEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 49869c6b16b5SShri Abhyankar 49879c6b16b5SShri Abhyankar PetscFunctionBegin; 49889c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4989a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 4990a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 4991a4868fbcSLisandro Dalcin PetscValidType(U,2); 4992a4868fbcSLisandro Dalcin PetscValidType(Y,3); 4993a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 4994dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm,4); 4995dadcf809SJacob Faibussowitsch PetscValidRealPointer(norma,5); 4996dadcf809SJacob Faibussowitsch PetscValidRealPointer(normr,6); 49973c633725SBarry Smith PetscCheck(U != Y,PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 49989c6b16b5SShri Abhyankar 49999566063dSJacob Faibussowitsch PetscCall(VecGetSize(U,&N)); 50009566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(U,&n)); 50019566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(U,&rstart,NULL)); 50029566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(U,&u)); 50039566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(Y,&y)); 50047453f775SEmil Constantinescu sum = 0.; n_loc = 0; 50057453f775SEmil Constantinescu suma = 0.; na_loc = 0; 50067453f775SEmil Constantinescu sumr = 0.; nr_loc = 0; 50079c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 50089c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 50099566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 50109566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 50119c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 501276cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 50137453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50147453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 50157453f775SEmil Constantinescu if (tola>0.) { 50167453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 50177453f775SEmil Constantinescu na_loc++; 50187453f775SEmil Constantinescu } 50197453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 50207453f775SEmil Constantinescu if (tolr>0.) { 50217453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 50227453f775SEmil Constantinescu nr_loc++; 50237453f775SEmil Constantinescu } 50247453f775SEmil Constantinescu tol=tola+tolr; 50257453f775SEmil Constantinescu if (tol>0.) { 50267453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 50277453f775SEmil Constantinescu n_loc++; 50287453f775SEmil Constantinescu } 50299c6b16b5SShri Abhyankar } 50309566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 50319566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 50329c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 50339c6b16b5SShri Abhyankar const PetscScalar *atol; 50349566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 50359c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 503676cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 50377453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50387453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 50397453f775SEmil Constantinescu if (tola>0.) { 50407453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 50417453f775SEmil Constantinescu na_loc++; 50427453f775SEmil Constantinescu } 50437453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 50447453f775SEmil Constantinescu if (tolr>0.) { 50457453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 50467453f775SEmil Constantinescu nr_loc++; 50477453f775SEmil Constantinescu } 50487453f775SEmil Constantinescu tol=tola+tolr; 50497453f775SEmil Constantinescu if (tol>0.) { 50507453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 50517453f775SEmil Constantinescu n_loc++; 50527453f775SEmil Constantinescu } 50539c6b16b5SShri Abhyankar } 50549566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 50559c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 50569c6b16b5SShri Abhyankar const PetscScalar *rtol; 50579566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 50589c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 505976cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 50607453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50617453f775SEmil Constantinescu tola = ts->atol; 50627453f775SEmil Constantinescu if (tola>0.) { 50637453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 50647453f775SEmil Constantinescu na_loc++; 50657453f775SEmil Constantinescu } 50667453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 50677453f775SEmil Constantinescu if (tolr>0.) { 50687453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 50697453f775SEmil Constantinescu nr_loc++; 50707453f775SEmil Constantinescu } 50717453f775SEmil Constantinescu tol=tola+tolr; 50727453f775SEmil Constantinescu if (tol>0.) { 50737453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 50747453f775SEmil Constantinescu n_loc++; 50757453f775SEmil Constantinescu } 50769c6b16b5SShri Abhyankar } 50779566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 50789c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 50799c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 508076cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 50817453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50827453f775SEmil Constantinescu tola = ts->atol; 50837453f775SEmil Constantinescu if (tola>0.) { 50847453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 50857453f775SEmil Constantinescu na_loc++; 50867453f775SEmil Constantinescu } 50877453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 50887453f775SEmil Constantinescu if (tolr>0.) { 50897453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 50907453f775SEmil Constantinescu nr_loc++; 50917453f775SEmil Constantinescu } 50927453f775SEmil Constantinescu tol=tola+tolr; 50937453f775SEmil Constantinescu if (tol>0.) { 50947453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 50957453f775SEmil Constantinescu n_loc++; 50967453f775SEmil Constantinescu } 50979c6b16b5SShri Abhyankar } 50989c6b16b5SShri Abhyankar } 50999566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(U,&u)); 51009566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(Y,&y)); 51019c6b16b5SShri Abhyankar 51027453f775SEmil Constantinescu err_loc[0] = sum; 51037453f775SEmil Constantinescu err_loc[1] = suma; 51047453f775SEmil Constantinescu err_loc[2] = sumr; 51057453f775SEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 51067453f775SEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 51077453f775SEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 51087453f775SEmil Constantinescu 51091c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts))); 51107453f775SEmil Constantinescu 51117453f775SEmil Constantinescu gsum = err_glb[0]; 51127453f775SEmil Constantinescu gsuma = err_glb[1]; 51137453f775SEmil Constantinescu gsumr = err_glb[2]; 51147453f775SEmil Constantinescu n_glb = err_glb[3]; 51157453f775SEmil Constantinescu na_glb = err_glb[4]; 51167453f775SEmil Constantinescu nr_glb = err_glb[5]; 51177453f775SEmil Constantinescu 5118b1316ef9SEmil Constantinescu *norm = 0.; 5119b1316ef9SEmil Constantinescu if (n_glb>0.) {*norm = PetscSqrtReal(gsum / n_glb);} 5120b1316ef9SEmil Constantinescu *norma = 0.; 5121b1316ef9SEmil Constantinescu if (na_glb>0.) {*norma = PetscSqrtReal(gsuma / na_glb);} 5122b1316ef9SEmil Constantinescu *normr = 0.; 5123b1316ef9SEmil Constantinescu if (nr_glb>0.) {*normr = PetscSqrtReal(gsumr / nr_glb);} 51249c6b16b5SShri Abhyankar 51253c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 51263c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 51273c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 51289c6b16b5SShri Abhyankar PetscFunctionReturn(0); 51299c6b16b5SShri Abhyankar } 51309c6b16b5SShri Abhyankar 51319c6b16b5SShri Abhyankar /*@ 5132a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 51339c6b16b5SShri Abhyankar 51349c6b16b5SShri Abhyankar Collective on TS 51359c6b16b5SShri Abhyankar 51364165533cSJose E. Roman Input Parameters: 51379c6b16b5SShri Abhyankar + ts - time stepping context 5138a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5139a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 51409c6b16b5SShri Abhyankar 51414165533cSJose E. Roman Output Parameters: 5142a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 51437453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5144a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 51459c6b16b5SShri Abhyankar 51469c6b16b5SShri Abhyankar Level: developer 51479c6b16b5SShri Abhyankar 5148db781477SPatrick Sanan .seealso: `TSErrorWeightedNorm()`, `TSErrorWeightedNorm2()` 51499c6b16b5SShri Abhyankar @*/ 51507453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 51519c6b16b5SShri Abhyankar { 51527453f775SEmil Constantinescu PetscInt i,n,N,rstart; 51539c6b16b5SShri Abhyankar const PetscScalar *u,*y; 51547453f775SEmil Constantinescu PetscReal max,gmax,maxa,gmaxa,maxr,gmaxr; 51557453f775SEmil Constantinescu PetscReal tol,tola,tolr,diff; 51567453f775SEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 51579c6b16b5SShri Abhyankar 51589c6b16b5SShri Abhyankar PetscFunctionBegin; 51599c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5160a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5161a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5162a4868fbcSLisandro Dalcin PetscValidType(U,2); 5163a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5164a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5165dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm,4); 5166dadcf809SJacob Faibussowitsch PetscValidRealPointer(norma,5); 5167dadcf809SJacob Faibussowitsch PetscValidRealPointer(normr,6); 51683c633725SBarry Smith PetscCheck(U != Y,PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 51699c6b16b5SShri Abhyankar 51709566063dSJacob Faibussowitsch PetscCall(VecGetSize(U,&N)); 51719566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(U,&n)); 51729566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(U,&rstart,NULL)); 51739566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(U,&u)); 51749566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(Y,&y)); 51757453f775SEmil Constantinescu 51767453f775SEmil Constantinescu max=0.; 51777453f775SEmil Constantinescu maxa=0.; 51787453f775SEmil Constantinescu maxr=0.; 51797453f775SEmil Constantinescu 51807453f775SEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 51819c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 51829566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 51839566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 51847453f775SEmil Constantinescu 51857453f775SEmil Constantinescu for (i=0; i<n; i++) { 518676cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 51877453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 51887453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 51897453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 51907453f775SEmil Constantinescu tol = tola+tolr; 51917453f775SEmil Constantinescu if (tola>0.) { 51927453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 51937453f775SEmil Constantinescu } 51947453f775SEmil Constantinescu if (tolr>0.) { 51957453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 51967453f775SEmil Constantinescu } 51977453f775SEmil Constantinescu if (tol>0.) { 51987453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 51997453f775SEmil Constantinescu } 52009c6b16b5SShri Abhyankar } 52019566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 52029566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 52039c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 52049c6b16b5SShri Abhyankar const PetscScalar *atol; 52059566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 52067453f775SEmil Constantinescu for (i=0; i<n; i++) { 520776cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 52087453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 52097453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 52107453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52117453f775SEmil Constantinescu tol = tola+tolr; 52127453f775SEmil Constantinescu if (tola>0.) { 52137453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 52147453f775SEmil Constantinescu } 52157453f775SEmil Constantinescu if (tolr>0.) { 52167453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 52177453f775SEmil Constantinescu } 52187453f775SEmil Constantinescu if (tol>0.) { 52197453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 52207453f775SEmil Constantinescu } 52219c6b16b5SShri Abhyankar } 52229566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 52239c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 52249c6b16b5SShri Abhyankar const PetscScalar *rtol; 52259566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 52267453f775SEmil Constantinescu 52277453f775SEmil Constantinescu for (i=0; i<n; i++) { 522876cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 52297453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 52307453f775SEmil Constantinescu tola = ts->atol; 52317453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52327453f775SEmil Constantinescu tol = tola+tolr; 52337453f775SEmil Constantinescu if (tola>0.) { 52347453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 52357453f775SEmil Constantinescu } 52367453f775SEmil Constantinescu if (tolr>0.) { 52377453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 52387453f775SEmil Constantinescu } 52397453f775SEmil Constantinescu if (tol>0.) { 52407453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 52417453f775SEmil Constantinescu } 52429c6b16b5SShri Abhyankar } 52439566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 52449c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 52457453f775SEmil Constantinescu 52467453f775SEmil Constantinescu for (i=0; i<n; i++) { 524776cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 52487453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 52497453f775SEmil Constantinescu tola = ts->atol; 52507453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52517453f775SEmil Constantinescu tol = tola+tolr; 52527453f775SEmil Constantinescu if (tola>0.) { 52537453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 52547453f775SEmil Constantinescu } 52557453f775SEmil Constantinescu if (tolr>0.) { 52567453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 52577453f775SEmil Constantinescu } 52587453f775SEmil Constantinescu if (tol>0.) { 52597453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 52607453f775SEmil Constantinescu } 52619c6b16b5SShri Abhyankar } 52629c6b16b5SShri Abhyankar } 52639566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(U,&u)); 52649566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(Y,&y)); 52657453f775SEmil Constantinescu err_loc[0] = max; 52667453f775SEmil Constantinescu err_loc[1] = maxa; 52677453f775SEmil Constantinescu err_loc[2] = maxr; 52681c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts))); 52697453f775SEmil Constantinescu gmax = err_glb[0]; 52707453f775SEmil Constantinescu gmaxa = err_glb[1]; 52717453f775SEmil Constantinescu gmaxr = err_glb[2]; 52729c6b16b5SShri Abhyankar 52739c6b16b5SShri Abhyankar *norm = gmax; 52747453f775SEmil Constantinescu *norma = gmaxa; 52757453f775SEmil Constantinescu *normr = gmaxr; 52763c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 52773c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 52783c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 52799c6b16b5SShri Abhyankar PetscFunctionReturn(0); 52809c6b16b5SShri Abhyankar } 52819c6b16b5SShri Abhyankar 52821c3436cfSJed Brown /*@ 52838a175baeSEmil Constantinescu TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances 52841c3436cfSJed Brown 52851c3436cfSJed Brown Collective on TS 52861c3436cfSJed Brown 52874165533cSJose E. Roman Input Parameters: 52881c3436cfSJed Brown + ts - time stepping context 5289a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5290a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 5291a4868fbcSLisandro Dalcin - wnormtype - norm type, either NORM_2 or NORM_INFINITY 52927619abb3SShri 52934165533cSJose E. Roman Output Parameters: 5294a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 52958a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 5296a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 5297a4868fbcSLisandro Dalcin 5298a4868fbcSLisandro Dalcin Options Database Keys: 5299a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 5300a4868fbcSLisandro Dalcin 53011c3436cfSJed Brown Level: developer 53021c3436cfSJed Brown 5303db781477SPatrick Sanan .seealso: `TSErrorWeightedNormInfinity()`, `TSErrorWeightedNorm2()`, `TSErrorWeightedENorm` 53041c3436cfSJed Brown @*/ 53057453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 53061c3436cfSJed Brown { 53071c3436cfSJed Brown PetscFunctionBegin; 5308a4868fbcSLisandro Dalcin if (wnormtype == NORM_2) { 53099566063dSJacob Faibussowitsch PetscCall(TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr)); 5310a4868fbcSLisandro Dalcin } else if (wnormtype == NORM_INFINITY) { 53119566063dSJacob Faibussowitsch PetscCall(TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr)); 531298921bdaSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 53131c3436cfSJed Brown PetscFunctionReturn(0); 53141c3436cfSJed Brown } 53151c3436cfSJed Brown 53168a175baeSEmil Constantinescu /*@ 53178a175baeSEmil Constantinescu TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances 53188a175baeSEmil Constantinescu 53198a175baeSEmil Constantinescu Collective on TS 53208a175baeSEmil Constantinescu 53214165533cSJose E. Roman Input Parameters: 53228a175baeSEmil Constantinescu + ts - time stepping context 53238a175baeSEmil Constantinescu . E - error vector 53248a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 53258a175baeSEmil Constantinescu - Y - state vector, previous time step 53268a175baeSEmil Constantinescu 53274165533cSJose E. Roman Output Parameters: 5328a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 53298a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5330a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 53318a175baeSEmil Constantinescu 53328a175baeSEmil Constantinescu Level: developer 53338a175baeSEmil Constantinescu 5334db781477SPatrick Sanan .seealso: `TSErrorWeightedENorm()`, `TSErrorWeightedENormInfinity()` 53358a175baeSEmil Constantinescu @*/ 53368a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 53378a175baeSEmil Constantinescu { 53388a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 53398a175baeSEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 53408a175baeSEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 53418a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 53428a175baeSEmil Constantinescu PetscReal err,sum,suma,sumr,gsum,gsuma,gsumr; 53438a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 53448a175baeSEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 53458a175baeSEmil Constantinescu 53468a175baeSEmil Constantinescu PetscFunctionBegin; 53478a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 53488a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 53498a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 53508a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 53518a175baeSEmil Constantinescu PetscValidType(E,2); 53528a175baeSEmil Constantinescu PetscValidType(U,3); 53538a175baeSEmil Constantinescu PetscValidType(Y,4); 53548a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 5355064a246eSJacob Faibussowitsch PetscCheckSameComm(U,3,Y,4); 5356dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm,5); 5357dadcf809SJacob Faibussowitsch PetscValidRealPointer(norma,6); 5358dadcf809SJacob Faibussowitsch PetscValidRealPointer(normr,7); 53598a175baeSEmil Constantinescu 53609566063dSJacob Faibussowitsch PetscCall(VecGetSize(E,&N)); 53619566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(E,&n)); 53629566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(E,&rstart,NULL)); 53639566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(E,&e)); 53649566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(U,&u)); 53659566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(Y,&y)); 53668a175baeSEmil Constantinescu sum = 0.; n_loc = 0; 53678a175baeSEmil Constantinescu suma = 0.; na_loc = 0; 53688a175baeSEmil Constantinescu sumr = 0.; nr_loc = 0; 53698a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { 53708a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 53719566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 53729566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 53738a175baeSEmil Constantinescu for (i=0; i<n; i++) { 537476cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 53758a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 53768a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 53778a175baeSEmil Constantinescu if (tola>0.) { 53788a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 53798a175baeSEmil Constantinescu na_loc++; 53808a175baeSEmil Constantinescu } 53818a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 53828a175baeSEmil Constantinescu if (tolr>0.) { 53838a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 53848a175baeSEmil Constantinescu nr_loc++; 53858a175baeSEmil Constantinescu } 53868a175baeSEmil Constantinescu tol=tola+tolr; 53878a175baeSEmil Constantinescu if (tol>0.) { 53888a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 53898a175baeSEmil Constantinescu n_loc++; 53908a175baeSEmil Constantinescu } 53918a175baeSEmil Constantinescu } 53929566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 53939566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 53948a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 53958a175baeSEmil Constantinescu const PetscScalar *atol; 53969566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 53978a175baeSEmil Constantinescu for (i=0; i<n; i++) { 539876cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 53998a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 54008a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 54018a175baeSEmil Constantinescu if (tola>0.) { 54028a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 54038a175baeSEmil Constantinescu na_loc++; 54048a175baeSEmil Constantinescu } 54058a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54068a175baeSEmil Constantinescu if (tolr>0.) { 54078a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 54088a175baeSEmil Constantinescu nr_loc++; 54098a175baeSEmil Constantinescu } 54108a175baeSEmil Constantinescu tol=tola+tolr; 54118a175baeSEmil Constantinescu if (tol>0.) { 54128a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 54138a175baeSEmil Constantinescu n_loc++; 54148a175baeSEmil Constantinescu } 54158a175baeSEmil Constantinescu } 54169566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 54178a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 54188a175baeSEmil Constantinescu const PetscScalar *rtol; 54199566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 54208a175baeSEmil Constantinescu for (i=0; i<n; i++) { 542176cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 54228a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 54238a175baeSEmil Constantinescu tola = ts->atol; 54248a175baeSEmil Constantinescu if (tola>0.) { 54258a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 54268a175baeSEmil Constantinescu na_loc++; 54278a175baeSEmil Constantinescu } 54288a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54298a175baeSEmil Constantinescu if (tolr>0.) { 54308a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 54318a175baeSEmil Constantinescu nr_loc++; 54328a175baeSEmil Constantinescu } 54338a175baeSEmil Constantinescu tol=tola+tolr; 54348a175baeSEmil Constantinescu if (tol>0.) { 54358a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 54368a175baeSEmil Constantinescu n_loc++; 54378a175baeSEmil Constantinescu } 54388a175baeSEmil Constantinescu } 54399566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 54408a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 54418a175baeSEmil Constantinescu for (i=0; i<n; i++) { 544276cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 54438a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 54448a175baeSEmil Constantinescu tola = ts->atol; 54458a175baeSEmil Constantinescu if (tola>0.) { 54468a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 54478a175baeSEmil Constantinescu na_loc++; 54488a175baeSEmil Constantinescu } 54498a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54508a175baeSEmil Constantinescu if (tolr>0.) { 54518a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 54528a175baeSEmil Constantinescu nr_loc++; 54538a175baeSEmil Constantinescu } 54548a175baeSEmil Constantinescu tol=tola+tolr; 54558a175baeSEmil Constantinescu if (tol>0.) { 54568a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 54578a175baeSEmil Constantinescu n_loc++; 54588a175baeSEmil Constantinescu } 54598a175baeSEmil Constantinescu } 54608a175baeSEmil Constantinescu } 54619566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(E,&e)); 54629566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(U,&u)); 54639566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(Y,&y)); 54648a175baeSEmil Constantinescu 54658a175baeSEmil Constantinescu err_loc[0] = sum; 54668a175baeSEmil Constantinescu err_loc[1] = suma; 54678a175baeSEmil Constantinescu err_loc[2] = sumr; 54688a175baeSEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 54698a175baeSEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 54708a175baeSEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 54718a175baeSEmil Constantinescu 54721c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts))); 54738a175baeSEmil Constantinescu 54748a175baeSEmil Constantinescu gsum = err_glb[0]; 54758a175baeSEmil Constantinescu gsuma = err_glb[1]; 54768a175baeSEmil Constantinescu gsumr = err_glb[2]; 54778a175baeSEmil Constantinescu n_glb = err_glb[3]; 54788a175baeSEmil Constantinescu na_glb = err_glb[4]; 54798a175baeSEmil Constantinescu nr_glb = err_glb[5]; 54808a175baeSEmil Constantinescu 54818a175baeSEmil Constantinescu *norm = 0.; 54828a175baeSEmil Constantinescu if (n_glb>0.) {*norm = PetscSqrtReal(gsum / n_glb);} 54838a175baeSEmil Constantinescu *norma = 0.; 54848a175baeSEmil Constantinescu if (na_glb>0.) {*norma = PetscSqrtReal(gsuma / na_glb);} 54858a175baeSEmil Constantinescu *normr = 0.; 54868a175baeSEmil Constantinescu if (nr_glb>0.) {*normr = PetscSqrtReal(gsumr / nr_glb);} 54878a175baeSEmil Constantinescu 54883c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 54893c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 54903c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 54918a175baeSEmil Constantinescu PetscFunctionReturn(0); 54928a175baeSEmil Constantinescu } 54938a175baeSEmil Constantinescu 54948a175baeSEmil Constantinescu /*@ 54958a175baeSEmil Constantinescu TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances 54968a175baeSEmil Constantinescu Collective on TS 54978a175baeSEmil Constantinescu 54984165533cSJose E. Roman Input Parameters: 54998a175baeSEmil Constantinescu + ts - time stepping context 55008a175baeSEmil Constantinescu . E - error vector 55018a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 55028a175baeSEmil Constantinescu - Y - state vector, previous time step 55038a175baeSEmil Constantinescu 55044165533cSJose E. Roman Output Parameters: 5505a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 55068a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5507a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 55088a175baeSEmil Constantinescu 55098a175baeSEmil Constantinescu Level: developer 55108a175baeSEmil Constantinescu 5511db781477SPatrick Sanan .seealso: `TSErrorWeightedENorm()`, `TSErrorWeightedENorm2()` 55128a175baeSEmil Constantinescu @*/ 55138a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 55148a175baeSEmil Constantinescu { 55158a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 55168a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 55178a175baeSEmil Constantinescu PetscReal err,max,gmax,maxa,gmaxa,maxr,gmaxr; 55188a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 55198a175baeSEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 55208a175baeSEmil Constantinescu 55218a175baeSEmil Constantinescu PetscFunctionBegin; 55228a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 55238a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 55248a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 55258a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 55268a175baeSEmil Constantinescu PetscValidType(E,2); 55278a175baeSEmil Constantinescu PetscValidType(U,3); 55288a175baeSEmil Constantinescu PetscValidType(Y,4); 55298a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 5530064a246eSJacob Faibussowitsch PetscCheckSameComm(U,3,Y,4); 5531dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm,5); 5532dadcf809SJacob Faibussowitsch PetscValidRealPointer(norma,6); 5533dadcf809SJacob Faibussowitsch PetscValidRealPointer(normr,7); 55348a175baeSEmil Constantinescu 55359566063dSJacob Faibussowitsch PetscCall(VecGetSize(E,&N)); 55369566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(E,&n)); 55379566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(E,&rstart,NULL)); 55389566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(E,&e)); 55399566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(U,&u)); 55409566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(Y,&y)); 55418a175baeSEmil Constantinescu 55428a175baeSEmil Constantinescu max=0.; 55438a175baeSEmil Constantinescu maxa=0.; 55448a175baeSEmil Constantinescu maxr=0.; 55458a175baeSEmil Constantinescu 55468a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 55478a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 55489566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 55499566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 55508a175baeSEmil Constantinescu 55518a175baeSEmil Constantinescu for (i=0; i<n; i++) { 555276cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 55538a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 55548a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 55558a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 55568a175baeSEmil Constantinescu tol = tola+tolr; 55578a175baeSEmil Constantinescu if (tola>0.) { 55588a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 55598a175baeSEmil Constantinescu } 55608a175baeSEmil Constantinescu if (tolr>0.) { 55618a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 55628a175baeSEmil Constantinescu } 55638a175baeSEmil Constantinescu if (tol>0.) { 55648a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 55658a175baeSEmil Constantinescu } 55668a175baeSEmil Constantinescu } 55679566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 55689566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 55698a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 55708a175baeSEmil Constantinescu const PetscScalar *atol; 55719566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 55728a175baeSEmil Constantinescu for (i=0; i<n; i++) { 557376cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 55748a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 55758a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 55768a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 55778a175baeSEmil Constantinescu tol = tola+tolr; 55788a175baeSEmil Constantinescu if (tola>0.) { 55798a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 55808a175baeSEmil Constantinescu } 55818a175baeSEmil Constantinescu if (tolr>0.) { 55828a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 55838a175baeSEmil Constantinescu } 55848a175baeSEmil Constantinescu if (tol>0.) { 55858a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 55868a175baeSEmil Constantinescu } 55878a175baeSEmil Constantinescu } 55889566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 55898a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 55908a175baeSEmil Constantinescu const PetscScalar *rtol; 55919566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 55928a175baeSEmil Constantinescu 55938a175baeSEmil Constantinescu for (i=0; i<n; i++) { 559476cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 55958a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 55968a175baeSEmil Constantinescu tola = ts->atol; 55978a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 55988a175baeSEmil Constantinescu tol = tola+tolr; 55998a175baeSEmil Constantinescu if (tola>0.) { 56008a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 56018a175baeSEmil Constantinescu } 56028a175baeSEmil Constantinescu if (tolr>0.) { 56038a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 56048a175baeSEmil Constantinescu } 56058a175baeSEmil Constantinescu if (tol>0.) { 56068a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 56078a175baeSEmil Constantinescu } 56088a175baeSEmil Constantinescu } 56099566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 56108a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 56118a175baeSEmil Constantinescu 56128a175baeSEmil Constantinescu for (i=0; i<n; i++) { 561376cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 56148a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 56158a175baeSEmil Constantinescu tola = ts->atol; 56168a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 56178a175baeSEmil Constantinescu tol = tola+tolr; 56188a175baeSEmil Constantinescu if (tola>0.) { 56198a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 56208a175baeSEmil Constantinescu } 56218a175baeSEmil Constantinescu if (tolr>0.) { 56228a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 56238a175baeSEmil Constantinescu } 56248a175baeSEmil Constantinescu if (tol>0.) { 56258a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 56268a175baeSEmil Constantinescu } 56278a175baeSEmil Constantinescu } 56288a175baeSEmil Constantinescu } 56299566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(E,&e)); 56309566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(U,&u)); 56319566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(Y,&y)); 56328a175baeSEmil Constantinescu err_loc[0] = max; 56338a175baeSEmil Constantinescu err_loc[1] = maxa; 56348a175baeSEmil Constantinescu err_loc[2] = maxr; 56351c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts))); 56368a175baeSEmil Constantinescu gmax = err_glb[0]; 56378a175baeSEmil Constantinescu gmaxa = err_glb[1]; 56388a175baeSEmil Constantinescu gmaxr = err_glb[2]; 56398a175baeSEmil Constantinescu 56408a175baeSEmil Constantinescu *norm = gmax; 56418a175baeSEmil Constantinescu *norma = gmaxa; 56428a175baeSEmil Constantinescu *normr = gmaxr; 56433c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 56443c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 56453c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 56468a175baeSEmil Constantinescu PetscFunctionReturn(0); 56478a175baeSEmil Constantinescu } 56488a175baeSEmil Constantinescu 56498a175baeSEmil Constantinescu /*@ 56508a175baeSEmil Constantinescu TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances 56518a175baeSEmil Constantinescu 56528a175baeSEmil Constantinescu Collective on TS 56538a175baeSEmil Constantinescu 56544165533cSJose E. Roman Input Parameters: 56558a175baeSEmil Constantinescu + ts - time stepping context 56568a175baeSEmil Constantinescu . E - error vector 56578a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 56588a175baeSEmil Constantinescu . Y - state vector, previous time step 56598a175baeSEmil Constantinescu - wnormtype - norm type, either NORM_2 or NORM_INFINITY 56608a175baeSEmil Constantinescu 56614165533cSJose E. Roman Output Parameters: 5662a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 56638a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 5664a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 56658a175baeSEmil Constantinescu 56668a175baeSEmil Constantinescu Options Database Keys: 56678a175baeSEmil Constantinescu . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 56688a175baeSEmil Constantinescu 56698a175baeSEmil Constantinescu Level: developer 56708a175baeSEmil Constantinescu 5671db781477SPatrick Sanan .seealso: `TSErrorWeightedENormInfinity()`, `TSErrorWeightedENorm2()`, `TSErrorWeightedNormInfinity()`, `TSErrorWeightedNorm2()` 56728a175baeSEmil Constantinescu @*/ 56738a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 56748a175baeSEmil Constantinescu { 56758a175baeSEmil Constantinescu PetscFunctionBegin; 56768a175baeSEmil Constantinescu if (wnormtype == NORM_2) { 56779566063dSJacob Faibussowitsch PetscCall(TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr)); 56788a175baeSEmil Constantinescu } else if (wnormtype == NORM_INFINITY) { 56799566063dSJacob Faibussowitsch PetscCall(TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr)); 568098921bdaSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 56818a175baeSEmil Constantinescu PetscFunctionReturn(0); 56828a175baeSEmil Constantinescu } 56838a175baeSEmil Constantinescu 56848d59e960SJed Brown /*@ 56858d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 56868d59e960SJed Brown 56878d59e960SJed Brown Logically Collective on TS 56888d59e960SJed Brown 56894165533cSJose E. Roman Input Parameters: 56908d59e960SJed Brown + ts - time stepping context 56918d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 56928d59e960SJed Brown 56938d59e960SJed Brown Note: 56948d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 56958d59e960SJed Brown 56968d59e960SJed Brown Level: intermediate 56978d59e960SJed Brown 5698db781477SPatrick Sanan .seealso: `TSGetCFLTime()`, `TSADAPTCFL` 56998d59e960SJed Brown @*/ 57008d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 57018d59e960SJed Brown { 57028d59e960SJed Brown PetscFunctionBegin; 57038d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 57048d59e960SJed Brown ts->cfltime_local = cfltime; 57058d59e960SJed Brown ts->cfltime = -1.; 57068d59e960SJed Brown PetscFunctionReturn(0); 57078d59e960SJed Brown } 57088d59e960SJed Brown 57098d59e960SJed Brown /*@ 57108d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 57118d59e960SJed Brown 57128d59e960SJed Brown Collective on TS 57138d59e960SJed Brown 57144165533cSJose E. Roman Input Parameter: 57158d59e960SJed Brown . ts - time stepping context 57168d59e960SJed Brown 57174165533cSJose E. Roman Output Parameter: 57188d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 57198d59e960SJed Brown 57208d59e960SJed Brown Level: advanced 57218d59e960SJed Brown 5722db781477SPatrick Sanan .seealso: `TSSetCFLTimeLocal()` 57238d59e960SJed Brown @*/ 57248d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 57258d59e960SJed Brown { 57268d59e960SJed Brown PetscFunctionBegin; 57278d59e960SJed Brown if (ts->cfltime < 0) { 57281c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts))); 57298d59e960SJed Brown } 57308d59e960SJed Brown *cfltime = ts->cfltime; 57318d59e960SJed Brown PetscFunctionReturn(0); 57328d59e960SJed Brown } 57338d59e960SJed Brown 5734d6ebe24aSShri Abhyankar /*@ 5735d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 5736d6ebe24aSShri Abhyankar 5737d6ebe24aSShri Abhyankar Input Parameters: 5738a2b725a8SWilliam Gropp + ts - the TS context. 5739d6ebe24aSShri Abhyankar . xl - lower bound. 5740a2b725a8SWilliam Gropp - xu - upper bound. 5741d6ebe24aSShri Abhyankar 5742d6ebe24aSShri Abhyankar Notes: 5743d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 5744e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 5745d6ebe24aSShri Abhyankar 57462bd2b0e6SSatish Balay Level: advanced 57472bd2b0e6SSatish Balay 5748d6ebe24aSShri Abhyankar @*/ 5749d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 5750d6ebe24aSShri Abhyankar { 5751d6ebe24aSShri Abhyankar SNES snes; 5752d6ebe24aSShri Abhyankar 5753d6ebe24aSShri Abhyankar PetscFunctionBegin; 57549566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 57559566063dSJacob Faibussowitsch PetscCall(SNESVISetVariableBounds(snes,xl,xu)); 5756d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 5757d6ebe24aSShri Abhyankar } 5758d6ebe24aSShri Abhyankar 5759f9c1d6abSBarry Smith /*@ 5760f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 5761f9c1d6abSBarry Smith 5762d083f849SBarry Smith Collective on TS 5763f9c1d6abSBarry Smith 5764f9c1d6abSBarry Smith Input Parameters: 5765f9c1d6abSBarry Smith + ts - the TS context 5766f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 5767f9c1d6abSBarry Smith 5768f9c1d6abSBarry Smith Output Parameters: 5769f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 5770f9c1d6abSBarry Smith 5771f9c1d6abSBarry Smith Level: developer 5772f9c1d6abSBarry Smith 5773db781477SPatrick Sanan .seealso: `TSSetRHSFunction()`, `TSComputeIFunction()` 5774f9c1d6abSBarry Smith @*/ 5775f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 5776f9c1d6abSBarry Smith { 5777f9c1d6abSBarry Smith PetscFunctionBegin; 5778f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 57793c633725SBarry Smith PetscCheck(ts->ops->linearstability,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 57809566063dSJacob Faibussowitsch PetscCall((*ts->ops->linearstability)(ts,xr,xi,yr,yi)); 5781f9c1d6abSBarry Smith PetscFunctionReturn(0); 5782f9c1d6abSBarry Smith } 578324655328SShri 578424655328SShri /*@ 5785dcb233daSLisandro Dalcin TSRestartStep - Flags the solver to restart the next step 5786dcb233daSLisandro Dalcin 5787dcb233daSLisandro Dalcin Collective on TS 5788dcb233daSLisandro Dalcin 5789dcb233daSLisandro Dalcin Input Parameter: 5790dcb233daSLisandro Dalcin . ts - the TS context obtained from TSCreate() 5791dcb233daSLisandro Dalcin 5792dcb233daSLisandro Dalcin Level: advanced 5793dcb233daSLisandro Dalcin 5794dcb233daSLisandro Dalcin Notes: 5795dcb233daSLisandro Dalcin Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of 5796dcb233daSLisandro Dalcin discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution 5797dcb233daSLisandro Dalcin vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For 5798dcb233daSLisandro Dalcin the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce 5799dcb233daSLisandro Dalcin discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with 5800dcb233daSLisandro Dalcin discontinuous source terms). 5801dcb233daSLisandro Dalcin 5802db781477SPatrick Sanan .seealso: `TSSolve()`, `TSSetPreStep()`, `TSSetPostStep()` 5803dcb233daSLisandro Dalcin @*/ 5804dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts) 5805dcb233daSLisandro Dalcin { 5806dcb233daSLisandro Dalcin PetscFunctionBegin; 5807dcb233daSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5808dcb233daSLisandro Dalcin ts->steprestart = PETSC_TRUE; 5809dcb233daSLisandro Dalcin PetscFunctionReturn(0); 5810dcb233daSLisandro Dalcin } 5811dcb233daSLisandro Dalcin 5812dcb233daSLisandro Dalcin /*@ 581324655328SShri TSRollBack - Rolls back one time step 581424655328SShri 581524655328SShri Collective on TS 581624655328SShri 581724655328SShri Input Parameter: 581824655328SShri . ts - the TS context obtained from TSCreate() 581924655328SShri 582024655328SShri Level: advanced 582124655328SShri 5822db781477SPatrick Sanan .seealso: `TSCreate()`, `TSSetUp()`, `TSDestroy()`, `TSSolve()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSInterpolate()` 582324655328SShri @*/ 582424655328SShri PetscErrorCode TSRollBack(TS ts) 582524655328SShri { 582624655328SShri PetscFunctionBegin; 582724655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 58283c633725SBarry Smith PetscCheck(!ts->steprollback,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called"); 58293c633725SBarry Smith PetscCheck(ts->ops->rollback,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 58309566063dSJacob Faibussowitsch PetscCall((*ts->ops->rollback)(ts)); 583124655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 583224655328SShri ts->ptime = ts->ptime_prev; 5833be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime_prev_rollback; 58342808aa04SLisandro Dalcin ts->steps--; 5835b3de5cdeSLisandro Dalcin ts->steprollback = PETSC_TRUE; 583624655328SShri PetscFunctionReturn(0); 583724655328SShri } 5838aeb4809dSShri Abhyankar 5839ff22ae23SHong Zhang /*@ 5840ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 5841ff22ae23SHong Zhang 5842ff22ae23SHong Zhang Input Parameter: 5843ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 5844ff22ae23SHong Zhang 58450429704eSStefano Zampini Output Parameters: 58460429704eSStefano Zampini + ns - the number of stages 58470429704eSStefano Zampini - Y - the current stage vectors 58480429704eSStefano Zampini 5849ff22ae23SHong Zhang Level: advanced 5850ff22ae23SHong Zhang 58510429704eSStefano Zampini Notes: Both ns and Y can be NULL. 58520429704eSStefano Zampini 5853db781477SPatrick Sanan .seealso: `TSCreate()` 5854ff22ae23SHong Zhang @*/ 5855ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns,Vec **Y) 5856ff22ae23SHong Zhang { 5857ff22ae23SHong Zhang PetscFunctionBegin; 5858ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 5859dadcf809SJacob Faibussowitsch if (ns) PetscValidIntPointer(ns,2); 58600429704eSStefano Zampini if (Y) PetscValidPointer(Y,3); 58610429704eSStefano Zampini if (!ts->ops->getstages) { 58620429704eSStefano Zampini if (ns) *ns = 0; 58630429704eSStefano Zampini if (Y) *Y = NULL; 58640429704eSStefano Zampini } else { 58659566063dSJacob Faibussowitsch PetscCall((*ts->ops->getstages)(ts,ns,Y)); 5866ff22ae23SHong Zhang } 5867ff22ae23SHong Zhang PetscFunctionReturn(0); 5868ff22ae23SHong Zhang } 5869ff22ae23SHong Zhang 5870847ff0e1SMatthew G. Knepley /*@C 5871847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 5872847ff0e1SMatthew G. Knepley 5873847ff0e1SMatthew G. Knepley Collective on SNES 5874847ff0e1SMatthew G. Knepley 5875847ff0e1SMatthew G. Knepley Input Parameters: 5876847ff0e1SMatthew G. Knepley + ts - the TS context 5877847ff0e1SMatthew G. Knepley . t - current timestep 5878847ff0e1SMatthew G. Knepley . U - state vector 5879847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 5880847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 5881847ff0e1SMatthew G. Knepley - ctx - an optional user context 5882847ff0e1SMatthew G. Knepley 5883847ff0e1SMatthew G. Knepley Output Parameters: 5884847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 5885847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 5886847ff0e1SMatthew G. Knepley 5887847ff0e1SMatthew G. Knepley Level: intermediate 5888847ff0e1SMatthew G. Knepley 5889847ff0e1SMatthew G. Knepley Notes: 5890847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 5891847ff0e1SMatthew G. Knepley 5892847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 5893847ff0e1SMatthew G. Knepley 5894847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 5895847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 5896847ff0e1SMatthew G. Knepley 5897847ff0e1SMatthew G. Knepley This will first try to get the coloring from the DM. If the DM type has no coloring 5898847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 5899847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 5900847ff0e1SMatthew G. Knepley 5901db781477SPatrick Sanan .seealso: `TSSetIJacobian()`, `MatFDColoringCreate()`, `MatFDColoringSetFunction()` 5902847ff0e1SMatthew G. Knepley @*/ 5903847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx) 5904847ff0e1SMatthew G. Knepley { 5905847ff0e1SMatthew G. Knepley SNES snes; 5906847ff0e1SMatthew G. Knepley MatFDColoring color; 5907847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 5908847ff0e1SMatthew G. Knepley 5909847ff0e1SMatthew G. Knepley PetscFunctionBegin; 59109566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL)); 59119566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color)); 5912847ff0e1SMatthew G. Knepley if (!color) { 5913847ff0e1SMatthew G. Knepley DM dm; 5914847ff0e1SMatthew G. Knepley ISColoring iscoloring; 5915847ff0e1SMatthew G. Knepley 59169566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 59179566063dSJacob Faibussowitsch PetscCall(DMHasColoring(dm, &hascolor)); 5918847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 59199566063dSJacob Faibussowitsch PetscCall(DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring)); 59209566063dSJacob Faibussowitsch PetscCall(MatFDColoringCreate(B, iscoloring, &color)); 59219566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts)); 59229566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFromOptions(color)); 59239566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetUp(B, iscoloring, color)); 59249566063dSJacob Faibussowitsch PetscCall(ISColoringDestroy(&iscoloring)); 5925847ff0e1SMatthew G. Knepley } else { 5926847ff0e1SMatthew G. Knepley MatColoring mc; 5927847ff0e1SMatthew G. Knepley 59289566063dSJacob Faibussowitsch PetscCall(MatColoringCreate(B, &mc)); 59299566063dSJacob Faibussowitsch PetscCall(MatColoringSetDistance(mc, 2)); 59309566063dSJacob Faibussowitsch PetscCall(MatColoringSetType(mc, MATCOLORINGSL)); 59319566063dSJacob Faibussowitsch PetscCall(MatColoringSetFromOptions(mc)); 59329566063dSJacob Faibussowitsch PetscCall(MatColoringApply(mc, &iscoloring)); 59339566063dSJacob Faibussowitsch PetscCall(MatColoringDestroy(&mc)); 59349566063dSJacob Faibussowitsch PetscCall(MatFDColoringCreate(B, iscoloring, &color)); 59359566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts)); 59369566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFromOptions(color)); 59379566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetUp(B, iscoloring, color)); 59389566063dSJacob Faibussowitsch PetscCall(ISColoringDestroy(&iscoloring)); 5939847ff0e1SMatthew G. Knepley } 59409566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color)); 59419566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject) color)); 5942847ff0e1SMatthew G. Knepley } 59439566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 59449566063dSJacob Faibussowitsch PetscCall(MatFDColoringApply(B, color, U, snes)); 5945847ff0e1SMatthew G. Knepley if (J != B) { 59469566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 59479566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 5948847ff0e1SMatthew G. Knepley } 5949847ff0e1SMatthew G. Knepley PetscFunctionReturn(0); 5950847ff0e1SMatthew G. Knepley } 595193b34091SDebojyoti Ghosh 5952cb9d8021SPierre Barbier de Reuille /*@ 59536bc98fa9SBarry Smith TSSetFunctionDomainError - Set a function that tests if the current state vector is valid 5954cb9d8021SPierre Barbier de Reuille 5955cb9d8021SPierre Barbier de Reuille Input Parameters: 59566bc98fa9SBarry Smith + ts - the TS context 59576bc98fa9SBarry Smith - func - function called within TSFunctionDomainError 59586bc98fa9SBarry Smith 59596bc98fa9SBarry Smith Calling sequence of func: 59606bc98fa9SBarry Smith $ PetscErrorCode func(TS ts,PetscReal time,Vec state,PetscBool reject) 59616bc98fa9SBarry Smith 59626bc98fa9SBarry Smith + ts - the TS context 59636bc98fa9SBarry Smith . time - the current time (of the stage) 59646bc98fa9SBarry Smith . state - the state to check if it is valid 59656bc98fa9SBarry Smith - reject - (output parameter) PETSC_FALSE if the state is acceptable, PETSC_TRUE if not acceptable 5966cb9d8021SPierre Barbier de Reuille 5967cb9d8021SPierre Barbier de Reuille Level: intermediate 5968cb9d8021SPierre Barbier de Reuille 59696bc98fa9SBarry Smith Notes: 59706bc98fa9SBarry Smith If an implicit ODE solver is being used then, in addition to providing this routine, the 59716bc98fa9SBarry Smith user's code should call SNESSetFunctionDomainError() when domain errors occur during 59726bc98fa9SBarry Smith function evaluations where the functions are provided by TSSetIFunction() or TSSetRHSFunction(). 59736bc98fa9SBarry Smith Use TSGetSNES() to obtain the SNES object 59746bc98fa9SBarry Smith 59756bc98fa9SBarry Smith Developer Notes: 59766bc98fa9SBarry Smith The naming of this function is inconsistent with the SNESSetFunctionDomainError() 59776bc98fa9SBarry Smith since one takes a function pointer and the other does not. 59786bc98fa9SBarry Smith 5979db781477SPatrick Sanan .seealso: `TSAdaptCheckStage()`, `TSFunctionDomainError()`, `SNESSetFunctionDomainError()`, `TSGetSNES()` 5980cb9d8021SPierre Barbier de Reuille @*/ 5981cb9d8021SPierre Barbier de Reuille 5982d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*)) 5983cb9d8021SPierre Barbier de Reuille { 5984cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 5985cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID,1); 5986cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 5987cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 5988cb9d8021SPierre Barbier de Reuille } 5989cb9d8021SPierre Barbier de Reuille 5990cb9d8021SPierre Barbier de Reuille /*@ 59916bc98fa9SBarry Smith TSFunctionDomainError - Checks if the current state is valid 5992cb9d8021SPierre Barbier de Reuille 5993cb9d8021SPierre Barbier de Reuille Input Parameters: 59946bc98fa9SBarry Smith + ts - the TS context 59956bc98fa9SBarry Smith . stagetime - time of the simulation 59966bc98fa9SBarry Smith - Y - state vector to check. 5997cb9d8021SPierre Barbier de Reuille 5998cb9d8021SPierre Barbier de Reuille Output Parameter: 59996bc98fa9SBarry Smith . accept - Set to PETSC_FALSE if the current state vector is valid. 6000cb9d8021SPierre Barbier de Reuille 6001cb9d8021SPierre Barbier de Reuille Note: 60026bc98fa9SBarry Smith This function is called by the TS integration routines and calls the user provided function (set with TSSetFunctionDomainError()) 60036bc98fa9SBarry Smith to check if the current state is valid. 600496a0c994SBarry Smith 60056bc98fa9SBarry Smith Level: developer 60066bc98fa9SBarry Smith 6007db781477SPatrick Sanan .seealso: `TSSetFunctionDomainError()` 6008cb9d8021SPierre Barbier de Reuille @*/ 6009d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept) 6010cb9d8021SPierre Barbier de Reuille { 6011cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 6012cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6013cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 6014cb9d8021SPierre Barbier de Reuille if (ts->functiondomainerror) { 6015a74df02fSJacob Faibussowitsch PetscStackCallStandard((*ts->functiondomainerror),ts,stagetime,Y,accept); 6016cb9d8021SPierre Barbier de Reuille } 6017cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 6018cb9d8021SPierre Barbier de Reuille } 60191ceb14c0SBarry Smith 602093b34091SDebojyoti Ghosh /*@C 6021e5168f73SEmil Constantinescu TSClone - This function clones a time step object. 602293b34091SDebojyoti Ghosh 6023d083f849SBarry Smith Collective 602493b34091SDebojyoti Ghosh 602593b34091SDebojyoti Ghosh Input Parameter: 602693b34091SDebojyoti Ghosh . tsin - The input TS 602793b34091SDebojyoti Ghosh 602893b34091SDebojyoti Ghosh Output Parameter: 6029e5168f73SEmil Constantinescu . tsout - The output TS (cloned) 603093b34091SDebojyoti Ghosh 60315eca1a21SEmil Constantinescu Notes: 60325eca1a21SEmil Constantinescu This function is used to create a clone of a TS object. It is used in ARKIMEX for initializing the slope for first stage explicit methods. It will likely be replaced in the future with a mechanism of switching methods on the fly. 60335eca1a21SEmil Constantinescu 6034928bb9adSStefano Zampini 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); 60355eca1a21SEmil Constantinescu 60365eca1a21SEmil Constantinescu Level: developer 603793b34091SDebojyoti Ghosh 6038db781477SPatrick Sanan .seealso: `TSCreate()`, `TSSetType()`, `TSSetUp()`, `TSDestroy()`, `TSSetProblemType()` 603993b34091SDebojyoti Ghosh @*/ 6040baa10174SEmil Constantinescu PetscErrorCode TSClone(TS tsin, TS *tsout) 604193b34091SDebojyoti Ghosh { 604293b34091SDebojyoti Ghosh TS t; 6043dc846ba4SSatish Balay SNES snes_start; 6044dc846ba4SSatish Balay DM dm; 6045dc846ba4SSatish Balay TSType type; 604693b34091SDebojyoti Ghosh 604793b34091SDebojyoti Ghosh PetscFunctionBegin; 604893b34091SDebojyoti Ghosh PetscValidPointer(tsin,1); 604993b34091SDebojyoti Ghosh *tsout = NULL; 605093b34091SDebojyoti Ghosh 60519566063dSJacob Faibussowitsch PetscCall(PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView)); 605293b34091SDebojyoti Ghosh 605393b34091SDebojyoti Ghosh /* General TS description */ 605493b34091SDebojyoti Ghosh t->numbermonitors = 0; 6055d0c080abSJoseph Pusztay t->monitorFrequency = 1; 605693b34091SDebojyoti Ghosh t->setupcalled = 0; 605793b34091SDebojyoti Ghosh t->ksp_its = 0; 605893b34091SDebojyoti Ghosh t->snes_its = 0; 605993b34091SDebojyoti Ghosh t->nwork = 0; 60607d51462cSStefano Zampini t->rhsjacobian.time = PETSC_MIN_REAL; 606193b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 606293b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 606393b34091SDebojyoti Ghosh 60649566063dSJacob Faibussowitsch PetscCall(TSGetSNES(tsin,&snes_start)); 60659566063dSJacob Faibussowitsch PetscCall(TSSetSNES(t,snes_start)); 6066d15a3a53SEmil Constantinescu 60679566063dSJacob Faibussowitsch PetscCall(TSGetDM(tsin,&dm)); 60689566063dSJacob Faibussowitsch PetscCall(TSSetDM(t,dm)); 606993b34091SDebojyoti Ghosh 607093b34091SDebojyoti Ghosh t->adapt = tsin->adapt; 60719566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)t->adapt)); 607293b34091SDebojyoti Ghosh 6073e7069c78SShri t->trajectory = tsin->trajectory; 60749566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)t->trajectory)); 6075e7069c78SShri 6076e7069c78SShri t->event = tsin->event; 60776b10a48eSSatish Balay if (t->event) t->event->refct++; 6078e7069c78SShri 607993b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 608093b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 6081e7069c78SShri t->ptime_prev = tsin->ptime_prev; 608293b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 608393b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 608493b34091SDebojyoti Ghosh t->steps = tsin->steps; 608593b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 608693b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 608793b34091SDebojyoti Ghosh t->atol = tsin->atol; 608893b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 608993b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 609093b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 609193b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 609293b34091SDebojyoti Ghosh 60939566063dSJacob Faibussowitsch PetscCall(TSGetType(tsin,&type)); 60949566063dSJacob Faibussowitsch PetscCall(TSSetType(t,type)); 609593b34091SDebojyoti Ghosh 609693b34091SDebojyoti Ghosh t->vec_sol = NULL; 609793b34091SDebojyoti Ghosh 609893b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 609993b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 610093b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 610193b34091SDebojyoti Ghosh 61029566063dSJacob Faibussowitsch PetscCall(PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps))); 610393b34091SDebojyoti Ghosh 61040d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 61050d4fed19SBarry Smith PetscInt i; 61069566063dSJacob Faibussowitsch PetscCall(PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers)); 61070d4fed19SBarry Smith for (i=0; i<10; i++) { 61080d4fed19SBarry Smith ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 61090d4fed19SBarry Smith } 61100d4fed19SBarry Smith } 611193b34091SDebojyoti Ghosh *tsout = t; 611293b34091SDebojyoti Ghosh PetscFunctionReturn(0); 611393b34091SDebojyoti Ghosh } 6114f3b1f45cSBarry Smith 6115f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y) 6116f3b1f45cSBarry Smith { 6117f3b1f45cSBarry Smith TS ts = (TS) ctx; 6118f3b1f45cSBarry Smith 6119f3b1f45cSBarry Smith PetscFunctionBegin; 61209566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts,0,x,y)); 6121f3b1f45cSBarry Smith PetscFunctionReturn(0); 6122f3b1f45cSBarry Smith } 6123f3b1f45cSBarry Smith 6124f3b1f45cSBarry Smith /*@ 6125f3b1f45cSBarry Smith TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function. 6126f3b1f45cSBarry Smith 6127d083f849SBarry Smith Logically Collective on TS 6128f3b1f45cSBarry Smith 6129f3b1f45cSBarry Smith Input Parameters: 6130f3b1f45cSBarry Smith TS - the time stepping routine 6131f3b1f45cSBarry Smith 6132f3b1f45cSBarry Smith Output Parameter: 6133f3b1f45cSBarry Smith . flg - PETSC_TRUE if the multiply is likely correct 6134f3b1f45cSBarry Smith 6135f3b1f45cSBarry Smith Options Database: 6136f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator 6137f3b1f45cSBarry Smith 6138f3b1f45cSBarry Smith Level: advanced 6139f3b1f45cSBarry Smith 614095452b02SPatrick Sanan Notes: 614195452b02SPatrick Sanan This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian 6142f3b1f45cSBarry Smith 6143db781477SPatrick Sanan .seealso: `MatCreateShell()`, `MatShellGetContext()`, `MatShellGetOperation()`, `MatShellTestMultTranspose()`, `TSRHSJacobianTestTranspose()` 6144f3b1f45cSBarry Smith @*/ 6145f3b1f45cSBarry Smith PetscErrorCode TSRHSJacobianTest(TS ts,PetscBool *flg) 6146f3b1f45cSBarry Smith { 6147f3b1f45cSBarry Smith Mat J,B; 6148f3b1f45cSBarry Smith TSRHSJacobian func; 6149f3b1f45cSBarry Smith void* ctx; 6150f3b1f45cSBarry Smith 6151f3b1f45cSBarry Smith PetscFunctionBegin; 61529566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts,&J,&B,&func,&ctx)); 61539566063dSJacob Faibussowitsch PetscCall((*func)(ts,0.0,ts->vec_sol,J,B,ctx)); 61549566063dSJacob Faibussowitsch PetscCall(MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg)); 6155f3b1f45cSBarry Smith PetscFunctionReturn(0); 6156f3b1f45cSBarry Smith } 6157f3b1f45cSBarry Smith 6158f3b1f45cSBarry Smith /*@C 6159f3b1f45cSBarry Smith TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function. 6160f3b1f45cSBarry Smith 6161d083f849SBarry Smith Logically Collective on TS 6162f3b1f45cSBarry Smith 6163f3b1f45cSBarry Smith Input Parameters: 6164f3b1f45cSBarry Smith TS - the time stepping routine 6165f3b1f45cSBarry Smith 6166f3b1f45cSBarry Smith Output Parameter: 6167f3b1f45cSBarry Smith . flg - PETSC_TRUE if the multiply is likely correct 6168f3b1f45cSBarry Smith 6169f3b1f45cSBarry Smith Options Database: 6170f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator 6171f3b1f45cSBarry Smith 617295452b02SPatrick Sanan Notes: 617395452b02SPatrick Sanan This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian 6174f3b1f45cSBarry Smith 6175f3b1f45cSBarry Smith Level: advanced 6176f3b1f45cSBarry Smith 6177db781477SPatrick Sanan .seealso: `MatCreateShell()`, `MatShellGetContext()`, `MatShellGetOperation()`, `MatShellTestMultTranspose()`, `TSRHSJacobianTest()` 6178f3b1f45cSBarry Smith @*/ 6179f3b1f45cSBarry Smith PetscErrorCode TSRHSJacobianTestTranspose(TS ts,PetscBool *flg) 6180f3b1f45cSBarry Smith { 6181f3b1f45cSBarry Smith Mat J,B; 6182f3b1f45cSBarry Smith void *ctx; 6183f3b1f45cSBarry Smith TSRHSJacobian func; 6184f3b1f45cSBarry Smith 6185f3b1f45cSBarry Smith PetscFunctionBegin; 61869566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts,&J,&B,&func,&ctx)); 61879566063dSJacob Faibussowitsch PetscCall((*func)(ts,0.0,ts->vec_sol,J,B,ctx)); 61889566063dSJacob Faibussowitsch PetscCall(MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg)); 6189f3b1f45cSBarry Smith PetscFunctionReturn(0); 6190f3b1f45cSBarry Smith } 61910fe4d17eSHong Zhang 61920fe4d17eSHong Zhang /*@ 61930fe4d17eSHong Zhang TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used. 61940fe4d17eSHong Zhang 61950fe4d17eSHong Zhang Logically collective 61960fe4d17eSHong Zhang 6197d8d19677SJose E. Roman Input Parameters: 61980fe4d17eSHong Zhang + ts - timestepping context 61990fe4d17eSHong Zhang - use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used 62000fe4d17eSHong Zhang 62010fe4d17eSHong Zhang Options Database: 62020fe4d17eSHong Zhang . -ts_use_splitrhsfunction - <true,false> 62030fe4d17eSHong Zhang 62040fe4d17eSHong Zhang Notes: 62050fe4d17eSHong Zhang This is only useful for multirate methods 62060fe4d17eSHong Zhang 62070fe4d17eSHong Zhang Level: intermediate 62080fe4d17eSHong Zhang 6209db781477SPatrick Sanan .seealso: `TSGetUseSplitRHSFunction()` 62100fe4d17eSHong Zhang @*/ 62110fe4d17eSHong Zhang PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction) 62120fe4d17eSHong Zhang { 62130fe4d17eSHong Zhang PetscFunctionBegin; 62140fe4d17eSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 62150fe4d17eSHong Zhang ts->use_splitrhsfunction = use_splitrhsfunction; 62160fe4d17eSHong Zhang PetscFunctionReturn(0); 62170fe4d17eSHong Zhang } 62180fe4d17eSHong Zhang 62190fe4d17eSHong Zhang /*@ 62200fe4d17eSHong Zhang TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used. 62210fe4d17eSHong Zhang 62220fe4d17eSHong Zhang Not collective 62230fe4d17eSHong Zhang 62240fe4d17eSHong Zhang Input Parameter: 62250fe4d17eSHong Zhang . ts - timestepping context 62260fe4d17eSHong Zhang 62270fe4d17eSHong Zhang Output Parameter: 62280fe4d17eSHong Zhang . use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used 62290fe4d17eSHong Zhang 62300fe4d17eSHong Zhang Level: intermediate 62310fe4d17eSHong Zhang 6232db781477SPatrick Sanan .seealso: `TSSetUseSplitRHSFunction()` 62330fe4d17eSHong Zhang @*/ 62340fe4d17eSHong Zhang PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction) 62350fe4d17eSHong Zhang { 62360fe4d17eSHong Zhang PetscFunctionBegin; 62370fe4d17eSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 62380fe4d17eSHong Zhang *use_splitrhsfunction = ts->use_splitrhsfunction; 62390fe4d17eSHong Zhang PetscFunctionReturn(0); 62400fe4d17eSHong Zhang } 6241d60b7d5cSBarry Smith 6242d60b7d5cSBarry Smith /*@ 6243d60b7d5cSBarry Smith TSSetMatStructure - sets the relationship between the nonzero structure of the RHS Jacobian matrix to the IJacobian matrix. 6244d60b7d5cSBarry Smith 6245d60b7d5cSBarry Smith Logically Collective on ts 6246d60b7d5cSBarry Smith 6247d60b7d5cSBarry Smith Input Parameters: 6248d60b7d5cSBarry Smith + ts - the time-stepper 6249d60b7d5cSBarry Smith - str - the structure (the default is UNKNOWN_NONZERO_PATTERN) 6250d60b7d5cSBarry Smith 6251d60b7d5cSBarry Smith Level: intermediate 6252d60b7d5cSBarry Smith 6253d60b7d5cSBarry Smith Notes: 6254d60b7d5cSBarry Smith When the relationship between the nonzero structures is known and supplied the solution process can be much faster 6255d60b7d5cSBarry Smith 6256db781477SPatrick Sanan .seealso: `MatAXPY()`, `MatStructure` 6257d60b7d5cSBarry Smith @*/ 6258d60b7d5cSBarry Smith PetscErrorCode TSSetMatStructure(TS ts,MatStructure str) 6259d60b7d5cSBarry Smith { 6260d60b7d5cSBarry Smith PetscFunctionBegin; 6261d60b7d5cSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6262d60b7d5cSBarry Smith ts->axpy_pattern = str; 6263d60b7d5cSBarry Smith PetscFunctionReturn(0); 6264d60b7d5cSBarry Smith } 62654a658b32SHong Zhang 62664a658b32SHong Zhang /*@ 62674a658b32SHong Zhang TSSetTimeSpan - sets the time span. The solution will be computed and stored for each time requested. 62684a658b32SHong Zhang 62694a658b32SHong Zhang Collective on ts 62704a658b32SHong Zhang 62714a658b32SHong Zhang Input Parameters: 62724a658b32SHong Zhang + ts - the time-stepper 62734a658b32SHong Zhang . n - number of the time points (>=2) 62744a658b32SHong Zhang - span_times - array of the time points. The first element and the last element are the initial time and the final time respectively. 62754a658b32SHong Zhang 62764a658b32SHong Zhang Options Database Keys: 62774a658b32SHong Zhang . -ts_time_span <t0,...tf> - Sets the time span 62784a658b32SHong Zhang 62794a658b32SHong Zhang Level: beginner 62804a658b32SHong Zhang 62814a658b32SHong Zhang Notes: 62824a658b32SHong Zhang The elements in tspan must be all increasing. They correspond to the intermediate points for time integration. 62834a658b32SHong Zhang TS_EXACTFINALTIME_MATCHSTEP must be used to make the last time step in each sub-interval match the intermediate points specified. 62844a658b32SHong Zhang The intermediate solutions are saved in a vector array that can be accessed with TSGetSolutions(). Thus using time span may 62854a658b32SHong Zhang pressure the memory system when using a large number of span points. 62864a658b32SHong Zhang 6287*c2e3fba1SPatrick Sanan .seealso: `TSGetTimeSpan()`, `TSGetSolutions()` 62884a658b32SHong Zhang @*/ 62894a658b32SHong Zhang PetscErrorCode TSSetTimeSpan(TS ts,PetscInt n,PetscReal *span_times) 62904a658b32SHong Zhang { 62914a658b32SHong Zhang PetscFunctionBegin; 62924a658b32SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 629363a3b9bcSJacob Faibussowitsch PetscCheck(n >= 2,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Minimum time span size is 2 but %" PetscInt_FMT " is provided",n); 62944a658b32SHong Zhang if (ts->tspan && n != ts->tspan->num_span_times) { 62954a658b32SHong Zhang PetscCall(PetscFree(ts->tspan->span_times)); 62964a658b32SHong Zhang PetscCall(VecDestroyVecs(ts->tspan->num_span_times,&ts->tspan->vecs_sol)); 62974a658b32SHong Zhang PetscCall(PetscMalloc1(n,&ts->tspan->span_times)); 62984a658b32SHong Zhang } 62994a658b32SHong Zhang if (!ts->tspan) { 63004a658b32SHong Zhang TSTimeSpan tspan; 63014a658b32SHong Zhang PetscCall(PetscNew(&tspan)); 63024a658b32SHong Zhang PetscCall(PetscMalloc1(n,&tspan->span_times)); 63034a658b32SHong Zhang ts->tspan = tspan; 63044a658b32SHong Zhang } 63054a658b32SHong Zhang ts->tspan->num_span_times = n; 63064a658b32SHong Zhang PetscCall(PetscArraycpy(ts->tspan->span_times,span_times,n)); 63074a658b32SHong Zhang PetscCall(TSSetTime(ts,ts->tspan->span_times[0])); 63084a658b32SHong Zhang PetscCall(TSSetMaxTime(ts,ts->tspan->span_times[n-1])); 63094a658b32SHong Zhang PetscFunctionReturn(0); 63104a658b32SHong Zhang } 63114a658b32SHong Zhang 63124a658b32SHong Zhang /*@C 63134a658b32SHong Zhang TSGetTimeSpan - gets the time span. 63144a658b32SHong Zhang 63154a658b32SHong Zhang Not Collective 63164a658b32SHong Zhang 63174a658b32SHong Zhang Input Parameter: 63184a658b32SHong Zhang . ts - the time-stepper 63194a658b32SHong Zhang 63204a658b32SHong Zhang Output Parameters: 63214a658b32SHong Zhang + n - number of the time points (>=2) 63224a658b32SHong Zhang - span_times - array of the time points. The first element and the last element are the initial time and the final time respectively. The values are valid until the TS object is destroyed. 63234a658b32SHong Zhang 63244a658b32SHong Zhang Level: beginner 63254a658b32SHong Zhang Notes: Both n and span_times can be NULL. 63264a658b32SHong Zhang 6327*c2e3fba1SPatrick Sanan .seealso: `TSSetTimeSpan()`, `TSGetSolutions()` 63284a658b32SHong Zhang @*/ 63294a658b32SHong Zhang PetscErrorCode TSGetTimeSpan(TS ts,PetscInt *n,const PetscReal **span_times) 63304a658b32SHong Zhang { 63314a658b32SHong Zhang PetscFunctionBegin; 63324a658b32SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 63334a658b32SHong Zhang if (n) PetscValidIntPointer(n,2); 63344a658b32SHong Zhang if (span_times) PetscValidPointer(span_times,3); 63354a658b32SHong Zhang if (!ts->tspan) { 63364a658b32SHong Zhang if (n) *n = 0; 63374a658b32SHong Zhang if (span_times) *span_times = NULL; 63384a658b32SHong Zhang } else { 63394a658b32SHong Zhang if (n) *n = ts->tspan->num_span_times; 63404a658b32SHong Zhang if (span_times) *span_times = ts->tspan->span_times; 63414a658b32SHong Zhang } 63424a658b32SHong Zhang PetscFunctionReturn(0); 63434a658b32SHong Zhang } 63444a658b32SHong Zhang 63454a658b32SHong Zhang /*@ 63464a658b32SHong Zhang TSGetTimeSpanSolutions - Get the number of solutions and the solutions at the time points specified by the time span. 63474a658b32SHong Zhang 63484a658b32SHong Zhang Input Parameter: 63494a658b32SHong Zhang . ts - the TS context obtained from TSCreate() 63504a658b32SHong Zhang 63514a658b32SHong Zhang Output Parameters: 63524a658b32SHong Zhang + nsol - the number of solutions 63534a658b32SHong Zhang - Sols - the solution vectors 63544a658b32SHong Zhang 63554a658b32SHong Zhang Level: beginner 63564a658b32SHong Zhang 63574a658b32SHong Zhang Notes: Both nsol and Sols can be NULL. 63584a658b32SHong Zhang 6359db781477SPatrick Sanan .seealso: `TSSetTimeSpan()` 63604a658b32SHong Zhang @*/ 63614a658b32SHong Zhang PetscErrorCode TSGetTimeSpanSolutions(TS ts,PetscInt *nsol,Vec **Sols) 63624a658b32SHong Zhang { 63634a658b32SHong Zhang PetscFunctionBegin; 63644a658b32SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 63654a658b32SHong Zhang if (nsol) PetscValidIntPointer(nsol,2); 63664a658b32SHong Zhang if (Sols) PetscValidPointer(Sols,3); 63674a658b32SHong Zhang if (!ts->tspan) { 63684a658b32SHong Zhang if (nsol) *nsol = 0; 63694a658b32SHong Zhang if (Sols) *Sols = NULL; 63704a658b32SHong Zhang } else { 63714a658b32SHong Zhang if (nsol) *nsol = ts->tspan->num_span_times; 63724a658b32SHong Zhang if (Sols) *Sols = ts->tspan->vecs_sol; 63734a658b32SHong Zhang } 63744a658b32SHong Zhang PetscFunctionReturn(0); 63754a658b32SHong Zhang } 6376