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); 211e66621cSBarry Smith if (!((PetscObject)adapt)->type_name) PetscCall(TSAdaptSetType(adapt,default_type)); 222ffb9264SLisandro Dalcin PetscFunctionReturn(0); 232ffb9264SLisandro Dalcin } 242ffb9264SLisandro Dalcin 25bdad233fSMatthew Knepley /*@ 26bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 27bdad233fSMatthew Knepley 28bdad233fSMatthew Knepley Collective on TS 29bdad233fSMatthew Knepley 30bdad233fSMatthew Knepley Input Parameter: 31bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 32bdad233fSMatthew Knepley 33bdad233fSMatthew Knepley Options Database Keys: 34d2567f34SHong Zhang + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE, TSBSYMP, TSIRK 35ef222394SBarry Smith . -ts_save_trajectory - checkpoint the solution at each time-step 36ef85077eSLisandro Dalcin . -ts_max_time <time> - maximum time to compute to 374a658b32SHong Zhang . -ts_time_span <t0,...tf> - sets the time span, solutions are computed and stored for each indicated time 38ef85077eSLisandro Dalcin . -ts_max_steps <steps> - maximum number of time-steps to take 39ef85077eSLisandro Dalcin . -ts_init_time <time> - initial time to start computation 404dc72f7fSBarry Smith . -ts_final_time <time> - final time to compute to (deprecated: use -ts_max_time) 413e4cdcaaSBarry Smith . -ts_dt <dt> - initial time step 421628793fSSatish Balay . -ts_exact_final_time <stepover,interpolate,matchstep> - whether to stop at the exact given final time and how to compute the solution at that time 43a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 44a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 45a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 46a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 4767b8a455SSatish Balay . -ts_atol <atol> - Absolute tolerance for local truncation error 48f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function 49f3b1f45cSBarry 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 5067b8a455SSatish Balay . -ts_adjoint_solve <yes,no> - After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory) 51847ff0e1SMatthew G. Knepley . -ts_fd_color - Use finite differences with coloring to compute IJacobian 52bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 53aee7a9fbSMatthew G. Knepley . -ts_monitor_cancel - Cancel all monitors 54de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 55de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 567cf37e64SBarry Smith . -ts_monitor_error - Monitors norm of error 576934998bSLisandro Dalcin . -ts_monitor_lg_timestep - Monitor timestep size graphically 588b668821SLisandro Dalcin . -ts_monitor_lg_timestep_log - Monitor log timestep size graphically 59de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 60de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 61de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 62de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 633e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 643e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 65fde5950dSBarry Smith . -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep 6663a3b9bcSJacob 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) 679e336e28SPatrick Sanan - -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 6853ea634cSHong Zhang 693d5a8a6aSBarry Smith Notes: 703d5a8a6aSBarry Smith See SNESSetFromOptions() and KSPSetFromOptions() for how to control the nonlinear and linear solves used by the time-stepper. 713d5a8a6aSBarry Smith 723d5a8a6aSBarry Smith Certain SNES options get reset for each new nonlinear solver, for example -snes_lag_jacobian <its> and -snes_lag_preconditioner <its>, in order 733d5a8a6aSBarry Smith to retain them over the multiple nonlinear solves that TS uses you mush also provide -snes_lag_jacobian_persists true and 743d5a8a6aSBarry Smith -snes_lag_preconditioner_persists true 753d5a8a6aSBarry Smith 769e336e28SPatrick Sanan Developer Note: 779e336e28SPatrick Sanan We should unify all the -ts_monitor options in the way that -xxx_view has been unified 78bdad233fSMatthew Knepley 79bdad233fSMatthew Knepley Level: beginner 80bdad233fSMatthew Knepley 81db781477SPatrick Sanan .seealso: `TSGetType()` 82bdad233fSMatthew Knepley @*/ 837087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 84bdad233fSMatthew Knepley { 85bc952696SBarry Smith PetscBool opt,flg,tflg; 86eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 874a658b32SHong Zhang PetscReal time_step,tspan[100]; 884a658b32SHong Zhang PetscInt nt = PETSC_STATIC_ARRAY_LENGTH(tspan); 8949354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 90d1212d36SBarry Smith char dir[16]; 91cd11d68dSLisandro Dalcin TSIFunction ifun; 926991f827SBarry Smith const char *defaultType; 936991f827SBarry Smith char typeName[256]; 94bdad233fSMatthew Knepley 95bdad233fSMatthew Knepley PetscFunctionBegin; 960700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 976991f827SBarry Smith 989566063dSJacob Faibussowitsch PetscCall(TSRegisterAll()); 999566063dSJacob Faibussowitsch PetscCall(TSGetIFunction(ts,NULL,&ifun,NULL)); 100cd11d68dSLisandro Dalcin 101d0609cedSBarry Smith PetscObjectOptionsBegin((PetscObject)ts); 1021ef27442SStefano Zampini if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 1031ef27442SStefano Zampini else defaultType = ifun ? TSBEULER : TSEULER; 1049566063dSJacob Faibussowitsch PetscCall(PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt)); 1051e66621cSBarry Smith if (opt) PetscCall(TSSetType(ts,typeName)); 1061e66621cSBarry Smith else PetscCall(TSSetType(ts,defaultType)); 107bdad233fSMatthew Knepley 108bdad233fSMatthew Knepley /* Handle generic TS options */ 1099566063dSJacob Faibussowitsch PetscCall(PetscOptionsDeprecated("-ts_final_time","-ts_max_time","3.10",NULL)); 1109566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL)); 1114a658b32SHong Zhang PetscCall(PetscOptionsRealArray("-ts_time_span","Time span","TSSetTimeSpan",tspan,&nt,&flg)); 1124a658b32SHong Zhang if (flg) PetscCall(TSSetTimeSpan(ts,nt,tspan)); 1139566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL)); 1149566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL)); 1159566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg)); 1169566063dSJacob Faibussowitsch if (flg) PetscCall(TSSetTimeStep(ts,time_step)); 1179566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum("-ts_exact_final_time","Option for handling of final time step","TSSetExactFinalTime",TSExactFinalTimeOptions,(PetscEnum)ts->exact_final_time,(PetscEnum*)&eftopt,&flg)); 1189566063dSJacob Faibussowitsch if (flg) PetscCall(TSSetExactFinalTime(ts,eftopt)); 1199566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,NULL)); 1209566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL)); 1219566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL)); 1229566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL)); 1239566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL)); 124bdad233fSMatthew Knepley 1259566063dSJacob 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)); 1269566063dSJacob 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)); 1279566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_use_splitrhsfunction","Use the split RHS function for multirate solvers ","TSSetUseSplitRHSFunction",ts->use_splitrhsfunction,&ts->use_splitrhsfunction,NULL)); 12856f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 12956f85f32SBarry Smith { 13056f85f32SBarry Smith PetscBool set; 13156f85f32SBarry Smith flg = PETSC_FALSE; 1329566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set)); 1331baa6e33SBarry Smith if (set) PetscCall(PetscObjectSAWsSetBlock((PetscObject)ts,flg)); 13456f85f32SBarry Smith } 13556f85f32SBarry Smith #endif 13656f85f32SBarry Smith 137bdad233fSMatthew Knepley /* Monitor options */ 1389566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_frequency", "Number of time steps between monitor output", "TSMonitorSetFrequency", ts->monitorFrequency, &ts->monitorFrequency, NULL)); 1399566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL)); 1409566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts,"-ts_monitor_extreme","Monitor extreme values of the solution","TSMonitorExtreme",TSMonitorExtreme,NULL)); 1419566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL)); 1429566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts,"-ts_dmswarm_monitor_moments","Monitor moments of particle distribution","TSDMSwarmMonitorMoments",TSDMSwarmMonitorMoments,NULL)); 143fde5950dSBarry Smith 1449566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",NULL,monfilename,sizeof(monfilename),&flg)); 1459566063dSJacob Faibussowitsch if (flg) PetscCall(PetscPythonMonitorSet((PetscObject)ts,monfilename)); 1465180491cSLisandro Dalcin 1479566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt)); 148b3603a34SBarry Smith if (opt) { 1493923b477SBarry Smith PetscInt howoften = 1; 150e669de00SBarry Smith DM dm; 151e669de00SBarry Smith PetscBool net; 152b3603a34SBarry Smith 1539566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL)); 1549566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 1559566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)dm,DMNETWORK,&net)); 156e669de00SBarry Smith if (net) { 157e669de00SBarry Smith TSMonitorLGCtxNetwork ctx; 1589566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxNetworkCreate(ts,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx)); 1599566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorLGCtxNetworkSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxNetworkDestroy)); 1609566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_lg_solution_semilogy","Plot the solution with a semi-log axis","",ctx->semilogy,&ctx->semilogy,NULL)); 161e669de00SBarry Smith } else { 162e669de00SBarry Smith TSMonitorLGCtx ctx; 1639566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx)); 1649566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy)); 165bdad233fSMatthew Knepley } 166e669de00SBarry Smith } 1676ba87a44SLisandro Dalcin 1689566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt)); 169ef20d060SBarry Smith if (opt) { 1700b039ecaSBarry Smith TSMonitorLGCtx ctx; 1713923b477SBarry Smith PetscInt howoften = 1; 172ef20d060SBarry Smith 1739566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL)); 1749566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx)); 1759566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy)); 176ef20d060SBarry Smith } 1779566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts,"-ts_monitor_error","View the error at each timestep","TSMonitorError",TSMonitorError,NULL)); 1787cf37e64SBarry Smith 1799566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt)); 1806934998bSLisandro Dalcin if (opt) { 1816934998bSLisandro Dalcin TSMonitorLGCtx ctx; 1826934998bSLisandro Dalcin PetscInt howoften = 1; 1836934998bSLisandro Dalcin 1849566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL)); 1859566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx)); 1869566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy)); 1876934998bSLisandro Dalcin } 1889566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt)); 1898b668821SLisandro Dalcin if (opt) { 1908b668821SLisandro Dalcin TSMonitorLGCtx ctx; 1918b668821SLisandro Dalcin PetscInt howoften = 1; 1928b668821SLisandro Dalcin 1939566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL)); 1949566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx)); 1959566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy)); 1968b668821SLisandro Dalcin ctx->semilogy = PETSC_TRUE; 1978b668821SLisandro Dalcin } 1988b668821SLisandro Dalcin 1999566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt)); 200201da799SBarry Smith if (opt) { 201201da799SBarry Smith TSMonitorLGCtx ctx; 202201da799SBarry Smith PetscInt howoften = 1; 203201da799SBarry Smith 2049566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL)); 2059566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx)); 2069566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy)); 207201da799SBarry Smith } 2089566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt)); 209201da799SBarry Smith if (opt) { 210201da799SBarry Smith TSMonitorLGCtx ctx; 211201da799SBarry Smith PetscInt howoften = 1; 212201da799SBarry Smith 2139566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL)); 2149566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx)); 2159566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy)); 216201da799SBarry Smith } 2179566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt)); 2188189c53fSBarry Smith if (opt) { 2198189c53fSBarry Smith TSMonitorSPEigCtx ctx; 2208189c53fSBarry Smith PetscInt howoften = 1; 2218189c53fSBarry Smith 2229566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL)); 2239566063dSJacob Faibussowitsch PetscCall(TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx)); 2249566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy)); 2258189c53fSBarry Smith } 2269566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_sp_swarm","Display particle phase from the DMSwarm","TSMonitorSPSwarm",&opt)); 2271b575b74SJoseph Pusztay if (opt) { 2281b575b74SJoseph Pusztay TSMonitorSPCtx ctx; 229d7462660SMatthew Knepley PetscInt howoften = 1, retain = 0; 2306a5217c0SMatthew G. Knepley PetscBool phase = PETSC_TRUE, create = PETSC_TRUE; 231d7462660SMatthew Knepley 2326a5217c0SMatthew G. Knepley for (PetscInt i = 0; i < ts->numbermonitors; ++i) if (ts->monitor[i] == TSMonitorSPSwarmSolution) {create = PETSC_FALSE;break;} 2336a5217c0SMatthew G. Knepley if (create) { 2349566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm","Display particles phase from the DMSwarm", "TSMonitorSPSwarm", howoften, &howoften, NULL)); 2359566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm_retain", "Retain n points plotted to show trajectory, -1 for all points", "TSMonitorSPSwarm", retain, &retain, NULL)); 2369566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_sp_swarm_phase", "Plot in phase space rather than coordinate space", "TSMonitorSPSwarm", phase, &phase, NULL)); 2379566063dSJacob Faibussowitsch PetscCall(TSMonitorSPCtxCreate(PetscObjectComm((PetscObject) ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, retain, phase, &ctx)); 2389566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode (*)(void**))TSMonitorSPCtxDestroy)); 2391b575b74SJoseph Pusztay } 2406a5217c0SMatthew G. Knepley } 241ef20d060SBarry Smith opt = PETSC_FALSE; 2429566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt)); 243a7cc72afSBarry Smith if (opt) { 24483a4ac43SBarry Smith TSMonitorDrawCtx ctx; 24583a4ac43SBarry Smith PetscInt howoften = 1; 246a80ad3e0SBarry Smith 2479566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL)); 2489566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx)); 2499566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy)); 250bdad233fSMatthew Knepley } 251fb1732b5SBarry Smith opt = PETSC_FALSE; 2529566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt)); 2532d5ee99bSBarry Smith if (opt) { 2542d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2552d5ee99bSBarry Smith PetscReal bounds[4]; 2562d5ee99bSBarry Smith PetscInt n = 4; 2572d5ee99bSBarry Smith PetscDraw draw; 2586934998bSLisandro Dalcin PetscDrawAxis axis; 2592d5ee99bSBarry Smith 2609566063dSJacob Faibussowitsch PetscCall(PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL)); 2613c633725SBarry Smith PetscCheck(n == 4,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 2629566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx)); 2639566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(ctx->viewer,0,&draw)); 2649566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis)); 2659566063dSJacob Faibussowitsch PetscCall(PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3])); 2669566063dSJacob Faibussowitsch PetscCall(PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2")); 2679566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy)); 2682d5ee99bSBarry Smith } 2692d5ee99bSBarry Smith opt = PETSC_FALSE; 2709566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt)); 2713a471f94SBarry Smith if (opt) { 27283a4ac43SBarry Smith TSMonitorDrawCtx ctx; 27383a4ac43SBarry Smith PetscInt howoften = 1; 2743a471f94SBarry Smith 2759566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL)); 2769566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx)); 2779566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy)); 2783a471f94SBarry Smith } 2790ed3bfb6SBarry Smith opt = PETSC_FALSE; 2809566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt)); 2810ed3bfb6SBarry Smith if (opt) { 2820ed3bfb6SBarry Smith TSMonitorDrawCtx ctx; 2830ed3bfb6SBarry Smith PetscInt howoften = 1; 2840ed3bfb6SBarry Smith 2859566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL)); 2869566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx)); 2879566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy)); 2880ed3bfb6SBarry Smith } 289fde5950dSBarry Smith 290ed81e22dSJed Brown opt = PETSC_FALSE; 29163a3b9bcSJacob 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)); 292ed81e22dSJed Brown if (flg) { 293ed81e22dSJed Brown const char *ptr,*ptr2; 294ed81e22dSJed Brown char *filetemplate; 29563a3b9bcSJacob 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"); 296ed81e22dSJed Brown /* Do some cursory validation of the input. */ 2979566063dSJacob Faibussowitsch PetscCall(PetscStrstr(monfilename,"%",(char**)&ptr)); 29863a3b9bcSJacob Faibussowitsch PetscCheck(ptr,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03" PetscInt_FMT ".vts"); 299ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 3009566063dSJacob Faibussowitsch PetscCall(PetscStrchr("DdiouxX",*ptr,(char**)&ptr2)); 30163a3b9bcSJacob 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"); 302ed81e22dSJed Brown if (ptr2) break; 303ed81e22dSJed Brown } 3049566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(monfilename,&filetemplate)); 3059566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy)); 306ed81e22dSJed Brown } 307bdad233fSMatthew Knepley 3089566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,sizeof(dir),&flg)); 309d1212d36SBarry Smith if (flg) { 310d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 311d1212d36SBarry Smith int ray = 0; 3123ee9839eSMatthew G. Knepley DMDirection ddir; 313d1212d36SBarry Smith DM da; 314d1212d36SBarry Smith PetscMPIInt rank; 315d1212d36SBarry Smith 3163c633725SBarry Smith PetscCheck(dir[1] == '=',PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 3173ee9839eSMatthew G. Knepley if (dir[0] == 'x') ddir = DM_X; 3183ee9839eSMatthew G. Knepley else if (dir[0] == 'y') ddir = DM_Y; 31998921bdaSJacob Faibussowitsch else SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 320d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 321d1212d36SBarry Smith 3229566063dSJacob Faibussowitsch PetscCall(PetscInfo(((PetscObject)ts),"Displaying DMDA ray %c = %d\n",dir[0],ray)); 3239566063dSJacob Faibussowitsch PetscCall(PetscNew(&rayctx)); 3249566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&da)); 3259566063dSJacob Faibussowitsch PetscCall(DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter)); 3269566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank)); 3271e66621cSBarry Smith if (rank == 0) PetscCall(PetscViewerDrawOpen(PETSC_COMM_SELF,NULL,NULL,0,0,600,300,&rayctx->viewer)); 32851b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 3299566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy)); 330d1212d36SBarry Smith } 3319566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,sizeof(dir),&flg)); 33251b4a12fSMatthew G. Knepley if (flg) { 33351b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 33451b4a12fSMatthew G. Knepley int ray = 0; 3353ee9839eSMatthew G. Knepley DMDirection ddir; 33651b4a12fSMatthew G. Knepley DM da; 33751b4a12fSMatthew G. Knepley PetscInt howoften = 1; 338d1212d36SBarry Smith 3393c633725SBarry Smith PetscCheck(dir[1] == '=',PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 3403ee9839eSMatthew G. Knepley if (dir[0] == 'x') ddir = DM_X; 3413ee9839eSMatthew G. Knepley else if (dir[0] == 'y') ddir = DM_Y; 34298921bdaSJacob Faibussowitsch else SETERRQ(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 34351b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 3441c3436cfSJed Brown 3459566063dSJacob Faibussowitsch PetscCall(PetscInfo(((PetscObject) ts),"Displaying LG DMDA ray %c = %d\n", dir[0], ray)); 3469566063dSJacob Faibussowitsch PetscCall(PetscNew(&rayctx)); 3479566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &da)); 3489566063dSJacob Faibussowitsch PetscCall(DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter)); 3499566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx)); 3509566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy)); 35151b4a12fSMatthew G. Knepley } 352a7a1495cSBarry Smith 3539566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt)); 354b3d3934dSBarry Smith if (opt) { 355b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 356b3d3934dSBarry Smith 3579566063dSJacob Faibussowitsch PetscCall(TSMonitorEnvelopeCtxCreate(ts,&ctx)); 3589566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy)); 359b3d3934dSBarry Smith } 360aee7a9fbSMatthew G. Knepley flg = PETSC_FALSE; 3619566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_cancel","Remove all monitors","TSMonitorCancel",flg,&flg,&opt)); 3629566063dSJacob Faibussowitsch if (opt && flg) PetscCall(TSMonitorCancel(ts)); 363b3d3934dSBarry Smith 364847ff0e1SMatthew G. Knepley flg = PETSC_FALSE; 3659566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL)); 366847ff0e1SMatthew G. Knepley if (flg) { 367847ff0e1SMatthew G. Knepley DM dm; 368847ff0e1SMatthew G. Knepley 369*800f99ffSJeremy L Thompson PetscCall(TSGetDM(ts, &dm)); PetscCall(DMTSUnsetIJacobianContext_Internal(dm)); 3709566063dSJacob Faibussowitsch PetscCall(TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, NULL)); 3719566063dSJacob Faibussowitsch PetscCall(PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n")); 372847ff0e1SMatthew G. Knepley } 373847ff0e1SMatthew G. Knepley 374d763cef2SBarry Smith /* Handle specific TS options */ 3751baa6e33SBarry Smith if (ts->ops->setfromoptions) PetscCall((*ts->ops->setfromoptions)(PetscOptionsObject,ts)); 376fbc52257SHong Zhang 377a7bdc993SLisandro Dalcin /* Handle TSAdapt options */ 3789566063dSJacob Faibussowitsch PetscCall(TSGetAdapt(ts,&ts->adapt)); 3799566063dSJacob Faibussowitsch PetscCall(TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type)); 3809566063dSJacob Faibussowitsch PetscCall(TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt)); 381a7bdc993SLisandro Dalcin 38268bece0bSHong Zhang /* TS trajectory must be set after TS, since it may use some TS options above */ 3834f122a70SLisandro Dalcin tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE; 3849566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL)); 3851baa6e33SBarry Smith if (tflg) PetscCall(TSSetSaveTrajectory(ts)); 386a05bf03eSHong Zhang 3879566063dSJacob Faibussowitsch PetscCall(TSAdjointSetFromOptions(PetscOptionsObject,ts)); 388d763cef2SBarry Smith 389d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 3909566063dSJacob Faibussowitsch PetscCall(PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts)); 391d0609cedSBarry Smith PetscOptionsEnd(); 392d763cef2SBarry Smith 3931baa6e33SBarry Smith if (ts->trajectory) PetscCall(TSTrajectorySetFromOptions(ts->trajectory,ts)); 39468bece0bSHong Zhang 3951ef27442SStefano Zampini /* why do we have to do this here and not during TSSetUp? */ 3969566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&ts->snes)); 3971ef27442SStefano Zampini if (ts->problem_type == TS_LINEAR) { 3989566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompareAny((PetscObject)ts->snes,&flg,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"")); 3999566063dSJacob Faibussowitsch if (!flg) PetscCall(SNESSetType(ts->snes,SNESKSPONLY)); 4001ef27442SStefano Zampini } 4019566063dSJacob Faibussowitsch PetscCall(SNESSetFromOptions(ts->snes)); 402d763cef2SBarry Smith PetscFunctionReturn(0); 403d763cef2SBarry Smith } 404d763cef2SBarry Smith 405d2daff3dSHong Zhang /*@ 40678fbdcc8SBarry Smith TSGetTrajectory - Gets the trajectory from a TS if it exists 40778fbdcc8SBarry Smith 40878fbdcc8SBarry Smith Collective on TS 40978fbdcc8SBarry Smith 41078fbdcc8SBarry Smith Input Parameters: 41178fbdcc8SBarry Smith . ts - the TS context obtained from TSCreate() 41278fbdcc8SBarry Smith 4137a7aea1fSJed Brown Output Parameters: 41478fbdcc8SBarry Smith . tr - the TSTrajectory object, if it exists 41578fbdcc8SBarry Smith 41678fbdcc8SBarry Smith Note: This routine should be called after all TS options have been set 41778fbdcc8SBarry Smith 41878fbdcc8SBarry Smith Level: advanced 41978fbdcc8SBarry Smith 420db781477SPatrick Sanan .seealso: `TSGetTrajectory()`, `TSAdjointSolve()`, `TSTrajectory`, `TSTrajectoryCreate()` 42178fbdcc8SBarry Smith 42278fbdcc8SBarry Smith @*/ 42378fbdcc8SBarry Smith PetscErrorCode TSGetTrajectory(TS ts,TSTrajectory *tr) 42478fbdcc8SBarry Smith { 42578fbdcc8SBarry Smith PetscFunctionBegin; 42678fbdcc8SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 42778fbdcc8SBarry Smith *tr = ts->trajectory; 42878fbdcc8SBarry Smith PetscFunctionReturn(0); 42978fbdcc8SBarry Smith } 43078fbdcc8SBarry Smith 43178fbdcc8SBarry Smith /*@ 432bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 433d2daff3dSHong Zhang 434d2daff3dSHong Zhang Collective on TS 435d2daff3dSHong Zhang 436f899ff85SJose E. Roman Input Parameter: 437bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 438bc952696SBarry Smith 43978fbdcc8SBarry Smith Options Database: 44078fbdcc8SBarry Smith + -ts_save_trajectory - saves the trajectory to a file 44167b8a455SSatish Balay - -ts_trajectory_type type - set trajectory type 44278fbdcc8SBarry Smith 44368bece0bSHong Zhang Note: This routine should be called after all TS options have been set 444d2daff3dSHong Zhang 445c3a89c15SBarry Smith The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and 44678fbdcc8SBarry Smith MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m 44778fbdcc8SBarry Smith 448d2daff3dSHong Zhang Level: intermediate 449d2daff3dSHong Zhang 450db781477SPatrick Sanan .seealso: `TSGetTrajectory()`, `TSAdjointSolve()` 451d2daff3dSHong Zhang 452d2daff3dSHong Zhang @*/ 453bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 454d2daff3dSHong Zhang { 455d2daff3dSHong Zhang PetscFunctionBegin; 456d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 45763a3b9bcSJacob Faibussowitsch if (!ts->trajectory) PetscCall(TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory)); 458d2daff3dSHong Zhang PetscFunctionReturn(0); 459d2daff3dSHong Zhang } 460d2daff3dSHong Zhang 461a7a1495cSBarry Smith /*@ 4622d29f1f2SStefano Zampini TSResetTrajectory - Destroys and recreates the internal TSTrajectory object 4632d29f1f2SStefano Zampini 4642d29f1f2SStefano Zampini Collective on TS 4652d29f1f2SStefano Zampini 4662d29f1f2SStefano Zampini Input Parameters: 4672d29f1f2SStefano Zampini . ts - the TS context obtained from TSCreate() 4682d29f1f2SStefano Zampini 4692d29f1f2SStefano Zampini Level: intermediate 4702d29f1f2SStefano Zampini 471db781477SPatrick Sanan .seealso: `TSGetTrajectory()`, `TSAdjointSolve()`, `TSRemoveTrajectory()` 4722d29f1f2SStefano Zampini 4732d29f1f2SStefano Zampini @*/ 4742d29f1f2SStefano Zampini PetscErrorCode TSResetTrajectory(TS ts) 4752d29f1f2SStefano Zampini { 4762d29f1f2SStefano Zampini PetscFunctionBegin; 4772d29f1f2SStefano Zampini PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4782d29f1f2SStefano Zampini if (ts->trajectory) { 4799566063dSJacob Faibussowitsch PetscCall(TSTrajectoryDestroy(&ts->trajectory)); 4809566063dSJacob Faibussowitsch PetscCall(TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory)); 4812d29f1f2SStefano Zampini } 4822d29f1f2SStefano Zampini PetscFunctionReturn(0); 4832d29f1f2SStefano Zampini } 4842d29f1f2SStefano Zampini 4852d29f1f2SStefano Zampini /*@ 48667a3cfb0SHong Zhang TSRemoveTrajectory - Destroys and removes the internal TSTrajectory object from TS 48767a3cfb0SHong Zhang 48867a3cfb0SHong Zhang Collective on TS 48967a3cfb0SHong Zhang 49067a3cfb0SHong Zhang Input Parameters: 49167a3cfb0SHong Zhang . ts - the TS context obtained from TSCreate() 49267a3cfb0SHong Zhang 49367a3cfb0SHong Zhang Level: intermediate 49467a3cfb0SHong Zhang 495db781477SPatrick Sanan .seealso: `TSResetTrajectory()`, `TSAdjointSolve()` 49667a3cfb0SHong Zhang 49767a3cfb0SHong Zhang @*/ 49867a3cfb0SHong Zhang PetscErrorCode TSRemoveTrajectory(TS ts) 49967a3cfb0SHong Zhang { 50067a3cfb0SHong Zhang PetscFunctionBegin; 50167a3cfb0SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5021e66621cSBarry Smith if (ts->trajectory) PetscCall(TSTrajectoryDestroy(&ts->trajectory)); 50367a3cfb0SHong Zhang PetscFunctionReturn(0); 50467a3cfb0SHong Zhang } 50567a3cfb0SHong Zhang 50667a3cfb0SHong Zhang /*@ 507a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 508a7a1495cSBarry Smith set with TSSetRHSJacobian(). 509a7a1495cSBarry Smith 510d083f849SBarry Smith Collective on TS 511a7a1495cSBarry Smith 512a7a1495cSBarry Smith Input Parameters: 513316643e7SJed Brown + ts - the TS context 514a7a1495cSBarry Smith . t - current timestep 5150910c330SBarry Smith - U - input vector 516a7a1495cSBarry Smith 517a7a1495cSBarry Smith Output Parameters: 518a7a1495cSBarry Smith + A - Jacobian matrix 5196b867d5aSJose E. Roman - B - optional preconditioning matrix 520a7a1495cSBarry Smith 521a7a1495cSBarry Smith Notes: 522a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 523a7a1495cSBarry Smith is used internally within the nonlinear solvers. 524a7a1495cSBarry Smith 525a7a1495cSBarry Smith Level: developer 526a7a1495cSBarry Smith 527db781477SPatrick Sanan .seealso: `TSSetRHSJacobian()`, `KSPSetOperators()` 528a7a1495cSBarry Smith @*/ 529d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 530a7a1495cSBarry Smith { 531270bf2e7SJed Brown PetscObjectState Ustate; 5326c1e1eecSBarry Smith PetscObjectId Uid; 53324989b8cSPeter Brune DM dm; 534942e3340SBarry Smith DMTS tsdm; 53524989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 53624989b8cSPeter Brune void *ctx; 537b2df71adSDebojyoti Ghosh TSRHSFunction rhsfunction; 538a7a1495cSBarry Smith 539a7a1495cSBarry Smith PetscFunctionBegin; 5400700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5410910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5420910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 5439566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 5449566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm,&tsdm)); 5459566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,NULL)); 5469566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx)); 5479566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&Ustate)); 5489566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U,&Uid)); 549971015bcSStefano Zampini 5502663174eSHong Zhang if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) PetscFunctionReturn(0); 551d90be118SSean Farley 55263a3b9bcSJacob 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); 55324989b8cSPeter Brune if (rhsjacobianfunc) { 5549566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_JacobianEval,ts,U,A,B)); 555792fecdfSBarry Smith PetscCallBack("TS callback Jacobian",(*rhsjacobianfunc)(ts,t,U,A,B,ctx)); 556a6ab3590SBarry Smith ts->rhsjacs++; 5579566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_JacobianEval,ts,U,A,B)); 558ef66eb69SBarry Smith } else { 5599566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(A)); 5609566063dSJacob Faibussowitsch if (B && A != B) PetscCall(MatZeroEntries(B)); 561ef66eb69SBarry Smith } 5620e4ef248SJed Brown ts->rhsjacobian.time = t; 563971015bcSStefano Zampini ts->rhsjacobian.shift = 0; 564971015bcSStefano Zampini ts->rhsjacobian.scale = 1.; 5659566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid)); 5669566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate)); 567a7a1495cSBarry Smith PetscFunctionReturn(0); 568a7a1495cSBarry Smith } 569a7a1495cSBarry Smith 570316643e7SJed Brown /*@ 571d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 572d763cef2SBarry Smith 573d083f849SBarry Smith Collective on TS 574316643e7SJed Brown 575316643e7SJed Brown Input Parameters: 576316643e7SJed Brown + ts - the TS context 577316643e7SJed Brown . t - current time 5780910c330SBarry Smith - U - state vector 579316643e7SJed Brown 580316643e7SJed Brown Output Parameter: 581316643e7SJed Brown . y - right hand side 582316643e7SJed Brown 583316643e7SJed Brown Note: 584316643e7SJed Brown Most users should not need to explicitly call this routine, as it 585316643e7SJed Brown is used internally within the nonlinear solvers. 586316643e7SJed Brown 587316643e7SJed Brown Level: developer 588316643e7SJed Brown 589db781477SPatrick Sanan .seealso: `TSSetRHSFunction()`, `TSComputeIFunction()` 590316643e7SJed Brown @*/ 5910910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 592d763cef2SBarry Smith { 59324989b8cSPeter Brune TSRHSFunction rhsfunction; 59424989b8cSPeter Brune TSIFunction ifunction; 59524989b8cSPeter Brune void *ctx; 59624989b8cSPeter Brune DM dm; 59724989b8cSPeter Brune 598d763cef2SBarry Smith PetscFunctionBegin; 5990700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6000910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6010700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 6029566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 6039566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,&ctx)); 6049566063dSJacob Faibussowitsch PetscCall(DMTSGetIFunction(dm,&ifunction,NULL)); 605d763cef2SBarry Smith 6063c633725SBarry Smith PetscCheck(rhsfunction || ifunction,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 607d763cef2SBarry Smith 60824989b8cSPeter Brune if (rhsfunction) { 6099566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_FunctionEval,ts,U,y,0)); 6109566063dSJacob Faibussowitsch PetscCall(VecLockReadPush(U)); 611792fecdfSBarry Smith PetscCallBack("TS callback right-hand-side",(*rhsfunction)(ts,t,U,y,ctx)); 6129566063dSJacob Faibussowitsch PetscCall(VecLockReadPop(U)); 613a6ab3590SBarry Smith ts->rhsfuncs++; 6149566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_FunctionEval,ts,U,y,0)); 6151e66621cSBarry Smith } else PetscCall(VecZeroEntries(y)); 616d763cef2SBarry Smith PetscFunctionReturn(0); 617d763cef2SBarry Smith } 618d763cef2SBarry Smith 619ef20d060SBarry Smith /*@ 620ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 621ef20d060SBarry Smith 622d083f849SBarry Smith Collective on TS 623ef20d060SBarry Smith 624ef20d060SBarry Smith Input Parameters: 625ef20d060SBarry Smith + ts - the TS context 626ef20d060SBarry Smith - t - current time 627ef20d060SBarry Smith 628ef20d060SBarry Smith Output Parameter: 6290910c330SBarry Smith . U - the solution 630ef20d060SBarry Smith 631ef20d060SBarry Smith Note: 632ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 633ef20d060SBarry Smith is used internally within the nonlinear solvers. 634ef20d060SBarry Smith 635ef20d060SBarry Smith Level: developer 636ef20d060SBarry Smith 637db781477SPatrick Sanan .seealso: `TSSetSolutionFunction()`, `TSSetRHSFunction()`, `TSComputeIFunction()` 638ef20d060SBarry Smith @*/ 6390910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 640ef20d060SBarry Smith { 641ef20d060SBarry Smith TSSolutionFunction solutionfunction; 642ef20d060SBarry Smith void *ctx; 643ef20d060SBarry Smith DM dm; 644ef20d060SBarry Smith 645ef20d060SBarry Smith PetscFunctionBegin; 646ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6470910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6489566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 6499566063dSJacob Faibussowitsch PetscCall(DMTSGetSolutionFunction(dm,&solutionfunction,&ctx)); 650ef20d060SBarry Smith 651792fecdfSBarry Smith if (solutionfunction) PetscCallBack("TS callback solution",(*solutionfunction)(ts,t,U,ctx)); 652ef20d060SBarry Smith PetscFunctionReturn(0); 653ef20d060SBarry Smith } 6549b7cd975SBarry Smith /*@ 6559b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 6569b7cd975SBarry Smith 657d083f849SBarry Smith Collective on TS 6589b7cd975SBarry Smith 6599b7cd975SBarry Smith Input Parameters: 6609b7cd975SBarry Smith + ts - the TS context 6619b7cd975SBarry Smith - t - current time 6629b7cd975SBarry Smith 6639b7cd975SBarry Smith Output Parameter: 6649b7cd975SBarry Smith . U - the function value 6659b7cd975SBarry Smith 6669b7cd975SBarry Smith Note: 6679b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 6689b7cd975SBarry Smith is used internally within the nonlinear solvers. 6699b7cd975SBarry Smith 6709b7cd975SBarry Smith Level: developer 6719b7cd975SBarry Smith 672db781477SPatrick Sanan .seealso: `TSSetSolutionFunction()`, `TSSetRHSFunction()`, `TSComputeIFunction()` 6739b7cd975SBarry Smith @*/ 6749b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 6759b7cd975SBarry Smith { 6769b7cd975SBarry Smith void *ctx; 6779b7cd975SBarry Smith DM dm; 6785f80ce2aSJacob Faibussowitsch TSForcingFunction forcing; 6799b7cd975SBarry Smith 6809b7cd975SBarry Smith PetscFunctionBegin; 6819b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6829b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6839566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 6849566063dSJacob Faibussowitsch PetscCall(DMTSGetForcingFunction(dm,&forcing,&ctx)); 6859b7cd975SBarry Smith 686792fecdfSBarry Smith if (forcing) PetscCallBack("TS callback forcing function",(*forcing)(ts,t,U,ctx)); 6879b7cd975SBarry Smith PetscFunctionReturn(0); 6889b7cd975SBarry Smith } 689ef20d060SBarry Smith 690214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 691214bc6a2SJed Brown { 6922dd45cf8SJed Brown Vec F; 693214bc6a2SJed Brown 694214bc6a2SJed Brown PetscFunctionBegin; 6950298fd71SBarry Smith *Frhs = NULL; 6969566063dSJacob Faibussowitsch PetscCall(TSGetIFunction(ts,&F,NULL,NULL)); 6971e66621cSBarry Smith if (!ts->Frhs) PetscCall(VecDuplicate(F,&ts->Frhs)); 698214bc6a2SJed Brown *Frhs = ts->Frhs; 699214bc6a2SJed Brown PetscFunctionReturn(0); 700214bc6a2SJed Brown } 701214bc6a2SJed Brown 702971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 703214bc6a2SJed Brown { 704214bc6a2SJed Brown Mat A,B; 70541a1d4d2SBarry Smith TSIJacobian ijacobian; 706214bc6a2SJed Brown 707214bc6a2SJed Brown PetscFunctionBegin; 708c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 709c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 7109566063dSJacob Faibussowitsch PetscCall(TSGetIJacobian(ts,&A,&B,&ijacobian,NULL)); 711214bc6a2SJed Brown if (Arhs) { 712214bc6a2SJed Brown if (!ts->Arhs) { 71341a1d4d2SBarry Smith if (ijacobian) { 7149566063dSJacob Faibussowitsch PetscCall(MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs)); 7159566063dSJacob Faibussowitsch PetscCall(TSSetMatStructure(ts,SAME_NONZERO_PATTERN)); 71641a1d4d2SBarry Smith } else { 71741a1d4d2SBarry Smith ts->Arhs = A; 7189566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)A)); 71941a1d4d2SBarry Smith } 7203565c898SBarry Smith } else { 7213565c898SBarry Smith PetscBool flg; 7229566063dSJacob Faibussowitsch PetscCall(SNESGetUseMatrixFree(ts->snes,NULL,&flg)); 7233565c898SBarry Smith /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */ 7243565c898SBarry Smith if (flg && !ijacobian && ts->Arhs == ts->Brhs) { 7259566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)ts->Arhs)); 7263565c898SBarry Smith ts->Arhs = A; 7279566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)A)); 7283565c898SBarry Smith } 729214bc6a2SJed Brown } 730214bc6a2SJed Brown *Arhs = ts->Arhs; 731214bc6a2SJed Brown } 732214bc6a2SJed Brown if (Brhs) { 733214bc6a2SJed Brown if (!ts->Brhs) { 734bdb70873SJed Brown if (A != B) { 73541a1d4d2SBarry Smith if (ijacobian) { 7369566063dSJacob Faibussowitsch PetscCall(MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs)); 737bdb70873SJed Brown } else { 73841a1d4d2SBarry Smith ts->Brhs = B; 7399566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)B)); 74041a1d4d2SBarry Smith } 74141a1d4d2SBarry Smith } else { 7429566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)ts->Arhs)); 74351699248SLisandro Dalcin ts->Brhs = ts->Arhs; 744bdb70873SJed Brown } 745214bc6a2SJed Brown } 746214bc6a2SJed Brown *Brhs = ts->Brhs; 747214bc6a2SJed Brown } 748214bc6a2SJed Brown PetscFunctionReturn(0); 749214bc6a2SJed Brown } 750214bc6a2SJed Brown 751316643e7SJed Brown /*@ 7520910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 753316643e7SJed Brown 754d083f849SBarry Smith Collective on TS 755316643e7SJed Brown 756316643e7SJed Brown Input Parameters: 757316643e7SJed Brown + ts - the TS context 758316643e7SJed Brown . t - current time 7590910c330SBarry Smith . U - state vector 7600910c330SBarry Smith . Udot - time derivative of state vector 761214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 762316643e7SJed Brown 763316643e7SJed Brown Output Parameter: 764316643e7SJed Brown . Y - right hand side 765316643e7SJed Brown 766316643e7SJed Brown Note: 767316643e7SJed Brown Most users should not need to explicitly call this routine, as it 768316643e7SJed Brown is used internally within the nonlinear solvers. 769316643e7SJed Brown 770316643e7SJed Brown If the user did did not write their equations in implicit form, this 771316643e7SJed Brown function recasts them in implicit form. 772316643e7SJed Brown 773316643e7SJed Brown Level: developer 774316643e7SJed Brown 775db781477SPatrick Sanan .seealso: `TSSetIFunction()`, `TSComputeRHSFunction()` 776316643e7SJed Brown @*/ 7770910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 778316643e7SJed Brown { 77924989b8cSPeter Brune TSIFunction ifunction; 78024989b8cSPeter Brune TSRHSFunction rhsfunction; 78124989b8cSPeter Brune void *ctx; 78224989b8cSPeter Brune DM dm; 783316643e7SJed Brown 784316643e7SJed Brown PetscFunctionBegin; 7850700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7860910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7870910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 7880700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 789316643e7SJed Brown 7909566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 7919566063dSJacob Faibussowitsch PetscCall(DMTSGetIFunction(dm,&ifunction,&ctx)); 7929566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,NULL)); 79324989b8cSPeter Brune 7943c633725SBarry Smith PetscCheck(rhsfunction || ifunction,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 795d90be118SSean Farley 7969566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y)); 79724989b8cSPeter Brune if (ifunction) { 798792fecdfSBarry Smith PetscCallBack("TS callback implicit function",(*ifunction)(ts,t,U,Udot,Y,ctx)); 799a6ab3590SBarry Smith ts->ifuncs++; 800214bc6a2SJed Brown } 801214bc6a2SJed Brown if (imex) { 8021e66621cSBarry Smith if (!ifunction) PetscCall(VecCopy(Udot,Y)); 80324989b8cSPeter Brune } else if (rhsfunction) { 80424989b8cSPeter Brune if (ifunction) { 805214bc6a2SJed Brown Vec Frhs; 8069566063dSJacob Faibussowitsch PetscCall(TSGetRHSVec_Private(ts,&Frhs)); 8079566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts,t,U,Frhs)); 8089566063dSJacob Faibussowitsch PetscCall(VecAXPY(Y,-1,Frhs)); 8092dd45cf8SJed Brown } else { 8109566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts,t,U,Y)); 8119566063dSJacob Faibussowitsch PetscCall(VecAYPX(Y,-1,Udot)); 812316643e7SJed Brown } 8134a6899ffSJed Brown } 8149566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y)); 815316643e7SJed Brown PetscFunctionReturn(0); 816316643e7SJed Brown } 817316643e7SJed Brown 818cfa8a9a2SHong Zhang /* 819cfa8a9a2SHong Zhang TSRecoverRHSJacobian - Recover the Jacobian matrix so that one can call TSComputeRHSJacobian() on it. 820cfa8a9a2SHong Zhang 821cfa8a9a2SHong Zhang Note: 822cfa8a9a2SHong Zhang This routine is needed when one switches from TSComputeIJacobian() to TSComputeRHSJacobian() because the Jacobian matrix may be shifted or scaled in TSComputeIJacobian(). 823cfa8a9a2SHong Zhang 824cfa8a9a2SHong Zhang */ 825cfa8a9a2SHong Zhang static PetscErrorCode TSRecoverRHSJacobian(TS ts,Mat A,Mat B) 826cfa8a9a2SHong Zhang { 827cfa8a9a2SHong Zhang PetscFunctionBegin; 828cfa8a9a2SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8293c633725SBarry Smith PetscCheck(A == ts->Arhs,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Invalid Amat"); 8303c633725SBarry Smith PetscCheck(B == ts->Brhs,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Invalid Bmat"); 831cfa8a9a2SHong Zhang 8321baa6e33SBarry Smith if (ts->rhsjacobian.shift) PetscCall(MatShift(A,-ts->rhsjacobian.shift)); 833cfa8a9a2SHong Zhang if (ts->rhsjacobian.scale == -1.) { 8349566063dSJacob Faibussowitsch PetscCall(MatScale(A,-1)); 835cfa8a9a2SHong Zhang } 836cfa8a9a2SHong Zhang if (B && B == ts->Brhs && A != B) { 8371baa6e33SBarry Smith if (ts->rhsjacobian.shift) PetscCall(MatShift(B,-ts->rhsjacobian.shift)); 8381e66621cSBarry Smith if (ts->rhsjacobian.scale == -1.) PetscCall(MatScale(B,-1)); 839cfa8a9a2SHong Zhang } 840cfa8a9a2SHong Zhang ts->rhsjacobian.shift = 0; 841cfa8a9a2SHong Zhang ts->rhsjacobian.scale = 1.; 842cfa8a9a2SHong Zhang PetscFunctionReturn(0); 843cfa8a9a2SHong Zhang } 844cfa8a9a2SHong Zhang 845316643e7SJed Brown /*@ 846316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 847316643e7SJed Brown 848d083f849SBarry Smith Collective on TS 849316643e7SJed Brown 850316643e7SJed Brown Input 851316643e7SJed Brown Input Parameters: 852316643e7SJed Brown + ts - the TS context 853316643e7SJed Brown . t - current timestep 8540910c330SBarry Smith . U - state vector 8550910c330SBarry Smith . Udot - time derivative of state vector 856214bc6a2SJed Brown . shift - shift to apply, see note below 857214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 858316643e7SJed Brown 859316643e7SJed Brown Output Parameters: 860316643e7SJed Brown + A - Jacobian matrix 8613565c898SBarry Smith - B - matrix from which the preconditioner is constructed; often the same as A 862316643e7SJed Brown 863316643e7SJed Brown Notes: 8640910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 865316643e7SJed Brown 8660910c330SBarry Smith dF/dU + shift*dF/dUdot 867316643e7SJed Brown 868316643e7SJed Brown Most users should not need to explicitly call this routine, as it 869316643e7SJed Brown is used internally within the nonlinear solvers. 870316643e7SJed Brown 871316643e7SJed Brown Level: developer 872316643e7SJed Brown 873db781477SPatrick Sanan .seealso: `TSSetIJacobian()` 87463495f91SJed Brown @*/ 875d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 876316643e7SJed Brown { 87724989b8cSPeter Brune TSIJacobian ijacobian; 87824989b8cSPeter Brune TSRHSJacobian rhsjacobian; 87924989b8cSPeter Brune DM dm; 88024989b8cSPeter Brune void *ctx; 881316643e7SJed Brown 882316643e7SJed Brown PetscFunctionBegin; 8830700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8840910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8850910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 886316643e7SJed Brown PetscValidPointer(A,6); 88794ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 888316643e7SJed Brown PetscValidPointer(B,7); 88994ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 89024989b8cSPeter Brune 8919566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 8929566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm,&ijacobian,&ctx)); 8939566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm,&rhsjacobian,NULL)); 89424989b8cSPeter Brune 8953c633725SBarry Smith PetscCheck(rhsjacobian || ijacobian,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 896316643e7SJed Brown 8979566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_JacobianEval,ts,U,A,B)); 89824989b8cSPeter Brune if (ijacobian) { 899792fecdfSBarry Smith PetscCallBack("TS callback implicit Jacobian",(*ijacobian)(ts,t,U,Udot,shift,A,B,ctx)); 900a6ab3590SBarry Smith ts->ijacs++; 9014a6899ffSJed Brown } 902214bc6a2SJed Brown if (imex) { 903b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 9044c26be97Sstefano_zampini PetscBool assembled; 905971015bcSStefano Zampini if (rhsjacobian) { 906971015bcSStefano Zampini Mat Arhs = NULL; 9079566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts,&Arhs,NULL)); 908971015bcSStefano Zampini if (A == Arhs) { 9093c633725SBarry 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 */ 910971015bcSStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 911971015bcSStefano Zampini } 912971015bcSStefano Zampini } 9139566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(A)); 9149566063dSJacob Faibussowitsch PetscCall(MatAssembled(A,&assembled)); 9154c26be97Sstefano_zampini if (!assembled) { 9169566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY)); 9179566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY)); 9184c26be97Sstefano_zampini } 9199566063dSJacob Faibussowitsch PetscCall(MatShift(A,shift)); 92094ab13aaSBarry Smith if (A != B) { 9219566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(B)); 9229566063dSJacob Faibussowitsch PetscCall(MatAssembled(B,&assembled)); 9234c26be97Sstefano_zampini if (!assembled) { 9249566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY)); 9259566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY)); 9264c26be97Sstefano_zampini } 9279566063dSJacob Faibussowitsch PetscCall(MatShift(B,shift)); 928214bc6a2SJed Brown } 929214bc6a2SJed Brown } 930214bc6a2SJed Brown } else { 931e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 9321e66621cSBarry Smith 9331e66621cSBarry Smith /* RHSJacobian needs to be converted to part of IJacobian if exists */ 9341e66621cSBarry Smith if (rhsjacobian) PetscCall(TSGetRHSMats_Private(ts,&Arhs,&Brhs)); 935e8b1e424SHong Zhang if (Arhs == A) { /* No IJacobian matrix, so we only have the RHS matrix */ 936e8b1e424SHong Zhang PetscObjectState Ustate; 937e8b1e424SHong Zhang PetscObjectId Uid; 938e8b1e424SHong Zhang TSRHSFunction rhsfunction; 939e8b1e424SHong Zhang 9409566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,NULL)); 9419566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&Ustate)); 9429566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U,&Uid)); 943097a96a2SHong 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 */ 9449566063dSJacob Faibussowitsch PetscCall(MatShift(A,shift-ts->rhsjacobian.shift)); /* revert the old shift and add the new shift with a single call to MatShift */ 9451e66621cSBarry Smith if (A != B) PetscCall(MatShift(B,shift-ts->rhsjacobian.shift)); 946e8b1e424SHong Zhang } else { 9473565c898SBarry Smith PetscBool flg; 948e8b1e424SHong Zhang 949e8b1e424SHong Zhang if (ts->rhsjacobian.reuse) { /* Undo the damage */ 950e8b1e424SHong Zhang /* MatScale has a short path for this case. 951e8b1e424SHong Zhang However, this code path is taken the first time TSComputeRHSJacobian is called 952e8b1e424SHong Zhang and the matrices have not been assembled yet */ 9539566063dSJacob Faibussowitsch PetscCall(TSRecoverRHSJacobian(ts,A,B)); 954e8b1e424SHong Zhang } 9559566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts,t,U,A,B)); 9569566063dSJacob Faibussowitsch PetscCall(SNESGetUseMatrixFree(ts->snes,NULL,&flg)); 9573565c898SBarry Smith /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */ 9583565c898SBarry Smith if (!flg) { 9599566063dSJacob Faibussowitsch PetscCall(MatScale(A,-1)); 9609566063dSJacob Faibussowitsch PetscCall(MatShift(A,shift)); 9613565c898SBarry Smith } 96294ab13aaSBarry Smith if (A != B) { 9639566063dSJacob Faibussowitsch PetscCall(MatScale(B,-1)); 9649566063dSJacob Faibussowitsch PetscCall(MatShift(B,shift)); 965316643e7SJed Brown } 966e8b1e424SHong Zhang } 967e8b1e424SHong Zhang ts->rhsjacobian.scale = -1; 968e8b1e424SHong Zhang ts->rhsjacobian.shift = shift; 969d60b7d5cSBarry Smith } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 970e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 9719566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(A)); 9729566063dSJacob Faibussowitsch PetscCall(MatShift(A,shift)); 97394ab13aaSBarry Smith if (A != B) { 9749566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(B)); 9759566063dSJacob Faibussowitsch PetscCall(MatShift(B,shift)); 976214bc6a2SJed Brown } 977316643e7SJed Brown } 9789566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts,t,U,Arhs,Brhs)); 9799566063dSJacob Faibussowitsch PetscCall(MatAXPY(A,-1,Arhs,ts->axpy_pattern)); 9801e66621cSBarry Smith if (A != B) PetscCall(MatAXPY(B,-1,Brhs,ts->axpy_pattern)); 981316643e7SJed Brown } 982316643e7SJed Brown } 9839566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_JacobianEval,ts,U,A,B)); 984316643e7SJed Brown PetscFunctionReturn(0); 985316643e7SJed Brown } 986316643e7SJed Brown 987d763cef2SBarry Smith /*@C 988d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 989b5abc632SBarry Smith where U_t = G(t,u). 990d763cef2SBarry Smith 9913f9fe445SBarry Smith Logically Collective on TS 992d763cef2SBarry Smith 993d763cef2SBarry Smith Input Parameters: 994d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 9950298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 996d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 997d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 9980298fd71SBarry Smith function evaluation routine (may be NULL) 999d763cef2SBarry Smith 1000a96d6ef6SBarry Smith Calling sequence of f: 1001a96d6ef6SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec F,void *ctx); 1002d763cef2SBarry Smith 1003a96d6ef6SBarry Smith + ts - timestep context 1004a96d6ef6SBarry Smith . t - current timestep 1005d763cef2SBarry Smith . u - input vector 1006d763cef2SBarry Smith . F - function vector 1007d763cef2SBarry Smith - ctx - [optional] user-defined function context 1008d763cef2SBarry Smith 1009d763cef2SBarry Smith Level: beginner 1010d763cef2SBarry Smith 101195452b02SPatrick Sanan Notes: 101295452b02SPatrick Sanan You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 10132bbac0d3SBarry Smith 1014db781477SPatrick Sanan .seealso: `TSSetRHSJacobian()`, `TSSetIJacobian()`, `TSSetIFunction()` 1015d763cef2SBarry Smith @*/ 1016089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 1017d763cef2SBarry Smith { 1018089b2837SJed Brown SNES snes; 10190298fd71SBarry Smith Vec ralloc = NULL; 102024989b8cSPeter Brune DM dm; 1021d763cef2SBarry Smith 1022089b2837SJed Brown PetscFunctionBegin; 10230700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1024ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 102524989b8cSPeter Brune 10269566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 10279566063dSJacob Faibussowitsch PetscCall(DMTSSetRHSFunction(dm,f,ctx)); 10289566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 1029e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 10309566063dSJacob Faibussowitsch PetscCall(VecDuplicate(ts->vec_sol,&ralloc)); 1031e856ceecSJed Brown r = ralloc; 1032e856ceecSJed Brown } 10339566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(snes,r,SNESTSFormFunction,ts)); 10349566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ralloc)); 1035d763cef2SBarry Smith PetscFunctionReturn(0); 1036d763cef2SBarry Smith } 1037d763cef2SBarry Smith 1038ef20d060SBarry Smith /*@C 1039abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1040ef20d060SBarry Smith 1041ef20d060SBarry Smith Logically Collective on TS 1042ef20d060SBarry Smith 1043ef20d060SBarry Smith Input Parameters: 1044ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 1045ef20d060SBarry Smith . f - routine for evaluating the solution 1046ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 10470298fd71SBarry Smith function evaluation routine (may be NULL) 1048ef20d060SBarry Smith 1049a96d6ef6SBarry Smith Calling sequence of f: 1050a96d6ef6SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,void *ctx); 1051ef20d060SBarry Smith 1052ef20d060SBarry Smith + t - current timestep 1053ef20d060SBarry Smith . u - output vector 1054ef20d060SBarry Smith - ctx - [optional] user-defined function context 1055ef20d060SBarry Smith 10560ed3bfb6SBarry Smith Options Database: 10570ed3bfb6SBarry Smith + -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction() 10580ed3bfb6SBarry Smith - -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction() 10590ed3bfb6SBarry Smith 1060abd5a294SJed Brown Notes: 1061abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 1062abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 1063abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 1064abd5a294SJed Brown 10654f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 1066abd5a294SJed Brown 1067ef20d060SBarry Smith Level: beginner 1068ef20d060SBarry Smith 1069db781477SPatrick Sanan .seealso: `TSSetRHSJacobian()`, `TSSetIJacobian()`, `TSComputeSolutionFunction()`, `TSSetForcingFunction()`, `TSSetSolution()`, `TSGetSolution()`, `TSMonitorLGError()`, `TSMonitorDrawError()` 1070ef20d060SBarry Smith @*/ 1071ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 1072ef20d060SBarry Smith { 1073ef20d060SBarry Smith DM dm; 1074ef20d060SBarry Smith 1075ef20d060SBarry Smith PetscFunctionBegin; 1076ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10779566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 10789566063dSJacob Faibussowitsch PetscCall(DMTSSetSolutionFunction(dm,f,ctx)); 1079ef20d060SBarry Smith PetscFunctionReturn(0); 1080ef20d060SBarry Smith } 1081ef20d060SBarry Smith 10829b7cd975SBarry Smith /*@C 10839b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 10849b7cd975SBarry Smith 10859b7cd975SBarry Smith Logically Collective on TS 10869b7cd975SBarry Smith 10879b7cd975SBarry Smith Input Parameters: 10889b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 1089e162b725SBarry Smith . func - routine for evaluating the forcing function 10909b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 10910298fd71SBarry Smith function evaluation routine (may be NULL) 10929b7cd975SBarry Smith 10939b7cd975SBarry Smith Calling sequence of func: 10946bc98fa9SBarry Smith $ PetscErrorCode func (TS ts,PetscReal t,Vec f,void *ctx); 10959b7cd975SBarry Smith 10969b7cd975SBarry Smith + t - current timestep 1097e162b725SBarry Smith . f - output vector 10989b7cd975SBarry Smith - ctx - [optional] user-defined function context 10999b7cd975SBarry Smith 11009b7cd975SBarry Smith Notes: 11019b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 1102e162b725SBarry 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 1103e162b725SBarry Smith definition of the problem you are solving and hence possibly introducing bugs. 1104e162b725SBarry Smith 1105e162b725SBarry Smith This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0 1106e162b725SBarry Smith 1107e162b725SBarry 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 1108e162b725SBarry Smith parameters can be passed in the ctx variable. 11099b7cd975SBarry Smith 11109b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 11119b7cd975SBarry Smith 11129b7cd975SBarry Smith Level: beginner 11139b7cd975SBarry Smith 1114db781477SPatrick Sanan .seealso: `TSSetRHSJacobian()`, `TSSetIJacobian()`, `TSComputeSolutionFunction()`, `TSSetSolutionFunction()` 11159b7cd975SBarry Smith @*/ 1116e162b725SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx) 11179b7cd975SBarry Smith { 11189b7cd975SBarry Smith DM dm; 11199b7cd975SBarry Smith 11209b7cd975SBarry Smith PetscFunctionBegin; 11219b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11229566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 11239566063dSJacob Faibussowitsch PetscCall(DMTSSetForcingFunction(dm,func,ctx)); 11249b7cd975SBarry Smith PetscFunctionReturn(0); 11259b7cd975SBarry Smith } 11269b7cd975SBarry Smith 1127d763cef2SBarry Smith /*@C 1128f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1129b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1130d763cef2SBarry Smith 11313f9fe445SBarry Smith Logically Collective on TS 1132d763cef2SBarry Smith 1133d763cef2SBarry Smith Input Parameters: 1134d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1135e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1136e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1137d763cef2SBarry Smith . f - the Jacobian evaluation routine 1138d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 11390298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1140d763cef2SBarry Smith 1141f7ab8db6SBarry Smith Calling sequence of f: 1142a96d6ef6SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1143d763cef2SBarry Smith 1144d763cef2SBarry Smith + t - current timestep 1145d763cef2SBarry Smith . u - input vector 1146e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1147e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1148d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1149d763cef2SBarry Smith 11506cd88445SBarry Smith Notes: 11516cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 11526cd88445SBarry Smith 11536cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1154ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1155d763cef2SBarry Smith 1156d763cef2SBarry Smith Level: beginner 1157d763cef2SBarry Smith 1158db781477SPatrick Sanan .seealso: `SNESComputeJacobianDefaultColor()`, `TSSetRHSFunction()`, `TSRHSJacobianSetReuse()`, `TSSetIJacobian()` 1159d763cef2SBarry Smith 1160d763cef2SBarry Smith @*/ 1161e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1162d763cef2SBarry Smith { 1163089b2837SJed Brown SNES snes; 116424989b8cSPeter Brune DM dm; 116524989b8cSPeter Brune TSIJacobian ijacobian; 1166277b19d0SLisandro Dalcin 1167d763cef2SBarry Smith PetscFunctionBegin; 11680700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1169e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1170e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1171e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1172e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1173d763cef2SBarry Smith 11749566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 11759566063dSJacob Faibussowitsch PetscCall(DMTSSetRHSJacobian(dm,f,ctx)); 11769566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm,&ijacobian,NULL)); 11779566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 11781e66621cSBarry Smith if (!ijacobian) PetscCall(SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts)); 1179e5d3d808SBarry Smith if (Amat) { 11809566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)Amat)); 11819566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Arhs)); 1182e5d3d808SBarry Smith ts->Arhs = Amat; 11830e4ef248SJed Brown } 1184e5d3d808SBarry Smith if (Pmat) { 11859566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)Pmat)); 11869566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Brhs)); 1187e5d3d808SBarry Smith ts->Brhs = Pmat; 11880e4ef248SJed Brown } 1189d763cef2SBarry Smith PetscFunctionReturn(0); 1190d763cef2SBarry Smith } 1191d763cef2SBarry Smith 1192316643e7SJed Brown /*@C 1193b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1194316643e7SJed Brown 11953f9fe445SBarry Smith Logically Collective on TS 1196316643e7SJed Brown 1197316643e7SJed Brown Input Parameters: 1198316643e7SJed Brown + ts - the TS context obtained from TSCreate() 11990298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1200316643e7SJed Brown . f - the function evaluation routine 12010298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1202316643e7SJed Brown 1203316643e7SJed Brown Calling sequence of f: 12046bc98fa9SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1205316643e7SJed Brown 1206316643e7SJed Brown + t - time at step/stage being solved 1207316643e7SJed Brown . u - state vector 1208316643e7SJed Brown . u_t - time derivative of state vector 1209316643e7SJed Brown . F - function vector 1210316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1211316643e7SJed Brown 1212316643e7SJed Brown Important: 12132bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1214316643e7SJed Brown 1215316643e7SJed Brown Level: beginner 1216316643e7SJed Brown 1217db781477SPatrick Sanan .seealso: `TSSetRHSJacobian()`, `TSSetRHSFunction()`, `TSSetIJacobian()` 1218316643e7SJed Brown @*/ 121951699248SLisandro Dalcin PetscErrorCode TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx) 1220316643e7SJed Brown { 1221089b2837SJed Brown SNES snes; 122251699248SLisandro Dalcin Vec ralloc = NULL; 122324989b8cSPeter Brune DM dm; 1224316643e7SJed Brown 1225316643e7SJed Brown PetscFunctionBegin; 12260700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 122751699248SLisandro Dalcin if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 122824989b8cSPeter Brune 12299566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 12309566063dSJacob Faibussowitsch PetscCall(DMTSSetIFunction(dm,f,ctx)); 123124989b8cSPeter Brune 12329566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 123351699248SLisandro Dalcin if (!r && !ts->dm && ts->vec_sol) { 12349566063dSJacob Faibussowitsch PetscCall(VecDuplicate(ts->vec_sol,&ralloc)); 123551699248SLisandro Dalcin r = ralloc; 1236e856ceecSJed Brown } 12379566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(snes,r,SNESTSFormFunction,ts)); 12389566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ralloc)); 1239089b2837SJed Brown PetscFunctionReturn(0); 1240089b2837SJed Brown } 1241089b2837SJed Brown 1242089b2837SJed Brown /*@C 1243a5b23f4aSJose E. Roman TSGetIFunction - Returns the vector where the implicit residual is stored and the function/context to compute it. 1244089b2837SJed Brown 1245089b2837SJed Brown Not Collective 1246089b2837SJed Brown 1247089b2837SJed Brown Input Parameter: 1248089b2837SJed Brown . ts - the TS context 1249089b2837SJed Brown 1250d8d19677SJose E. Roman Output Parameters: 12510298fd71SBarry Smith + r - vector to hold residual (or NULL) 12520298fd71SBarry Smith . func - the function to compute residual (or NULL) 12530298fd71SBarry Smith - ctx - the function context (or NULL) 1254089b2837SJed Brown 1255089b2837SJed Brown Level: advanced 1256089b2837SJed Brown 1257db781477SPatrick Sanan .seealso: `TSSetIFunction()`, `SNESGetFunction()` 1258089b2837SJed Brown @*/ 1259089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1260089b2837SJed Brown { 1261089b2837SJed Brown SNES snes; 126224989b8cSPeter Brune DM dm; 1263089b2837SJed Brown 1264089b2837SJed Brown PetscFunctionBegin; 1265089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 12669566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 12679566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes,r,NULL,NULL)); 12689566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 12699566063dSJacob Faibussowitsch PetscCall(DMTSGetIFunction(dm,func,ctx)); 1270089b2837SJed Brown PetscFunctionReturn(0); 1271089b2837SJed Brown } 1272089b2837SJed Brown 1273089b2837SJed Brown /*@C 1274089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1275089b2837SJed Brown 1276089b2837SJed Brown Not Collective 1277089b2837SJed Brown 1278089b2837SJed Brown Input Parameter: 1279089b2837SJed Brown . ts - the TS context 1280089b2837SJed Brown 1281d8d19677SJose E. Roman Output Parameters: 12820298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 12830298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 12840298fd71SBarry Smith - ctx - the function context (or NULL) 1285089b2837SJed Brown 1286089b2837SJed Brown Level: advanced 1287089b2837SJed Brown 1288db781477SPatrick Sanan .seealso: `TSSetRHSFunction()`, `SNESGetFunction()` 1289089b2837SJed Brown @*/ 1290089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1291089b2837SJed Brown { 1292089b2837SJed Brown SNES snes; 129324989b8cSPeter Brune DM dm; 1294089b2837SJed Brown 1295089b2837SJed Brown PetscFunctionBegin; 1296089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 12979566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 12989566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes,r,NULL,NULL)); 12999566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 13009566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm,func,ctx)); 1301316643e7SJed Brown PetscFunctionReturn(0); 1302316643e7SJed Brown } 1303316643e7SJed Brown 1304316643e7SJed Brown /*@C 1305a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1306ae8867d6SBarry Smith provided with TSSetIFunction(). 1307316643e7SJed Brown 13083f9fe445SBarry Smith Logically Collective on TS 1309316643e7SJed Brown 1310316643e7SJed Brown Input Parameters: 1311316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1312e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1313e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1314316643e7SJed Brown . f - the Jacobian evaluation routine 13150298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1316316643e7SJed Brown 1317316643e7SJed Brown Calling sequence of f: 13186bc98fa9SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1319316643e7SJed Brown 1320316643e7SJed Brown + t - time at step/stage being solved 13211b4a444bSJed Brown . U - state vector 13221b4a444bSJed Brown . U_t - time derivative of state vector 1323316643e7SJed Brown . a - shift 1324e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1325e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1326316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1327316643e7SJed Brown 1328316643e7SJed Brown Notes: 1329e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1330316643e7SJed Brown 1331895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 1332895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 1333895c21f2SBarry Smith 1334a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1335b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1336a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1337a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1338a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1339a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1340a4f0a591SBarry Smith 13416cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 13426cd88445SBarry Smith 13436cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1344ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1345ca5f011dSBarry Smith 1346316643e7SJed Brown Level: beginner 1347316643e7SJed Brown 1348db781477SPatrick Sanan .seealso: `TSSetIFunction()`, `TSSetRHSJacobian()`, `SNESComputeJacobianDefaultColor()`, `SNESComputeJacobianDefault()`, `TSSetRHSFunction()` 1349316643e7SJed Brown 1350316643e7SJed Brown @*/ 1351e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1352316643e7SJed Brown { 1353089b2837SJed Brown SNES snes; 135424989b8cSPeter Brune DM dm; 1355316643e7SJed Brown 1356316643e7SJed Brown PetscFunctionBegin; 13570700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1358e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1359e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1360e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1361e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 136224989b8cSPeter Brune 13639566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 13649566063dSJacob Faibussowitsch PetscCall(DMTSSetIJacobian(dm,f,ctx)); 136524989b8cSPeter Brune 13669566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 13679566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts)); 1368316643e7SJed Brown PetscFunctionReturn(0); 1369316643e7SJed Brown } 1370316643e7SJed Brown 1371e1244c69SJed Brown /*@ 1372e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1373e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1374e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1375e1244c69SJed Brown not been changed by the TS. 1376e1244c69SJed Brown 1377e1244c69SJed Brown Logically Collective 1378e1244c69SJed Brown 13794165533cSJose E. Roman Input Parameters: 1380e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1381e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1382e1244c69SJed Brown 1383e1244c69SJed Brown Level: intermediate 1384e1244c69SJed Brown 1385db781477SPatrick Sanan .seealso: `TSSetRHSJacobian()`, `TSComputeRHSJacobianConstant()` 1386e1244c69SJed Brown @*/ 1387e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1388e1244c69SJed Brown { 1389e1244c69SJed Brown PetscFunctionBegin; 1390e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1391e1244c69SJed Brown PetscFunctionReturn(0); 1392e1244c69SJed Brown } 1393e1244c69SJed Brown 1394efe9872eSLisandro Dalcin /*@C 1395efe9872eSLisandro Dalcin TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved. 1396efe9872eSLisandro Dalcin 1397efe9872eSLisandro Dalcin Logically Collective on TS 1398efe9872eSLisandro Dalcin 1399efe9872eSLisandro Dalcin Input Parameters: 1400efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1401efe9872eSLisandro Dalcin . F - vector to hold the residual (or NULL to have it created internally) 1402efe9872eSLisandro Dalcin . fun - the function evaluation routine 1403efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1404efe9872eSLisandro Dalcin 1405efe9872eSLisandro Dalcin Calling sequence of fun: 14066bc98fa9SBarry Smith $ PetscErrorCode fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx); 1407efe9872eSLisandro Dalcin 1408efe9872eSLisandro Dalcin + t - time at step/stage being solved 1409efe9872eSLisandro Dalcin . U - state vector 1410efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1411efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1412efe9872eSLisandro Dalcin . F - function vector 1413efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine (may be NULL) 1414efe9872eSLisandro Dalcin 1415efe9872eSLisandro Dalcin Level: beginner 1416efe9872eSLisandro Dalcin 1417db781477SPatrick Sanan .seealso: `TSSetI2Jacobian()`, `TSSetIFunction()`, `TSCreate()`, `TSSetRHSFunction()` 1418efe9872eSLisandro Dalcin @*/ 1419efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx) 1420efe9872eSLisandro Dalcin { 1421efe9872eSLisandro Dalcin DM dm; 1422efe9872eSLisandro Dalcin 1423efe9872eSLisandro Dalcin PetscFunctionBegin; 1424efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1425efe9872eSLisandro Dalcin if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2); 14269566063dSJacob Faibussowitsch PetscCall(TSSetIFunction(ts,F,NULL,NULL)); 14279566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 14289566063dSJacob Faibussowitsch PetscCall(DMTSSetI2Function(dm,fun,ctx)); 1429efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1430efe9872eSLisandro Dalcin } 1431efe9872eSLisandro Dalcin 1432efe9872eSLisandro Dalcin /*@C 1433a5b23f4aSJose E. Roman TSGetI2Function - Returns the vector where the implicit residual is stored and the function/context to compute it. 1434efe9872eSLisandro Dalcin 1435efe9872eSLisandro Dalcin Not Collective 1436efe9872eSLisandro Dalcin 1437efe9872eSLisandro Dalcin Input Parameter: 1438efe9872eSLisandro Dalcin . ts - the TS context 1439efe9872eSLisandro Dalcin 1440d8d19677SJose E. Roman Output Parameters: 1441efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL) 1442efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL) 1443efe9872eSLisandro Dalcin - ctx - the function context (or NULL) 1444efe9872eSLisandro Dalcin 1445efe9872eSLisandro Dalcin Level: advanced 1446efe9872eSLisandro Dalcin 1447db781477SPatrick Sanan .seealso: `TSSetIFunction()`, `SNESGetFunction()`, `TSCreate()` 1448efe9872eSLisandro Dalcin @*/ 1449efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx) 1450efe9872eSLisandro Dalcin { 1451efe9872eSLisandro Dalcin SNES snes; 1452efe9872eSLisandro Dalcin DM dm; 1453efe9872eSLisandro Dalcin 1454efe9872eSLisandro Dalcin PetscFunctionBegin; 1455efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 14569566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 14579566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes,r,NULL,NULL)); 14589566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 14599566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Function(dm,fun,ctx)); 1460efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1461efe9872eSLisandro Dalcin } 1462efe9872eSLisandro Dalcin 1463efe9872eSLisandro Dalcin /*@C 1464bc77d74cSLisandro Dalcin TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t + a*dF/dU_tt 1465efe9872eSLisandro Dalcin where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function(). 1466efe9872eSLisandro Dalcin 1467efe9872eSLisandro Dalcin Logically Collective on TS 1468efe9872eSLisandro Dalcin 1469efe9872eSLisandro Dalcin Input Parameters: 1470efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1471efe9872eSLisandro Dalcin . J - Jacobian matrix 1472efe9872eSLisandro Dalcin . P - preconditioning matrix for J (may be same as J) 1473efe9872eSLisandro Dalcin . jac - the Jacobian evaluation routine 1474efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1475efe9872eSLisandro Dalcin 1476efe9872eSLisandro Dalcin Calling sequence of jac: 14776bc98fa9SBarry 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); 1478efe9872eSLisandro Dalcin 1479efe9872eSLisandro Dalcin + t - time at step/stage being solved 1480efe9872eSLisandro Dalcin . U - state vector 1481efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1482efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1483efe9872eSLisandro Dalcin . v - shift for U_t 1484efe9872eSLisandro Dalcin . a - shift for U_tt 1485efe9872eSLisandro 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 1486efe9872eSLisandro Dalcin . P - preconditioning matrix for J, may be same as J 1487efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine 1488efe9872eSLisandro Dalcin 1489efe9872eSLisandro Dalcin Notes: 1490efe9872eSLisandro Dalcin The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve. 1491efe9872eSLisandro Dalcin 1492efe9872eSLisandro Dalcin The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be 1493efe9872eSLisandro 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. 1494efe9872eSLisandro Dalcin The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U where the positive "shift" 1495bc77d74cSLisandro Dalcin parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states. 1496efe9872eSLisandro Dalcin 1497efe9872eSLisandro Dalcin Level: beginner 1498efe9872eSLisandro Dalcin 1499db781477SPatrick Sanan .seealso: `TSSetI2Function()`, `TSGetI2Jacobian()` 1500efe9872eSLisandro Dalcin @*/ 1501efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx) 1502efe9872eSLisandro Dalcin { 1503efe9872eSLisandro Dalcin DM dm; 1504efe9872eSLisandro Dalcin 1505efe9872eSLisandro Dalcin PetscFunctionBegin; 1506efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1507efe9872eSLisandro Dalcin if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2); 1508efe9872eSLisandro Dalcin if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3); 15099566063dSJacob Faibussowitsch PetscCall(TSSetIJacobian(ts,J,P,NULL,NULL)); 15109566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 15119566063dSJacob Faibussowitsch PetscCall(DMTSSetI2Jacobian(dm,jac,ctx)); 1512efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1513efe9872eSLisandro Dalcin } 1514efe9872eSLisandro Dalcin 1515efe9872eSLisandro Dalcin /*@C 1516efe9872eSLisandro Dalcin TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep. 1517efe9872eSLisandro Dalcin 1518efe9872eSLisandro Dalcin Not Collective, but parallel objects are returned if TS is parallel 1519efe9872eSLisandro Dalcin 1520efe9872eSLisandro Dalcin Input Parameter: 1521efe9872eSLisandro Dalcin . ts - The TS context obtained from TSCreate() 1522efe9872eSLisandro Dalcin 1523efe9872eSLisandro Dalcin Output Parameters: 1524efe9872eSLisandro Dalcin + J - The (approximate) Jacobian of F(t,U,U_t,U_tt) 1525efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J 1526efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices 1527efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine 1528efe9872eSLisandro Dalcin 152995452b02SPatrick Sanan Notes: 153095452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 1531efe9872eSLisandro Dalcin 1532efe9872eSLisandro Dalcin Level: advanced 1533efe9872eSLisandro Dalcin 1534db781477SPatrick Sanan .seealso: `TSGetTimeStep()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()`, `TSSetI2Jacobian()`, `TSGetI2Function()`, `TSCreate()` 1535efe9872eSLisandro Dalcin 1536efe9872eSLisandro Dalcin @*/ 1537efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx) 1538efe9872eSLisandro Dalcin { 1539efe9872eSLisandro Dalcin SNES snes; 1540efe9872eSLisandro Dalcin DM dm; 1541efe9872eSLisandro Dalcin 1542efe9872eSLisandro Dalcin PetscFunctionBegin; 15439566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 15449566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 15459566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes,J,P,NULL,NULL)); 15469566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 15479566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm,jac,ctx)); 1548efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1549efe9872eSLisandro Dalcin } 1550efe9872eSLisandro Dalcin 1551efe9872eSLisandro Dalcin /*@ 1552efe9872eSLisandro Dalcin TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0 1553efe9872eSLisandro Dalcin 1554d083f849SBarry Smith Collective on TS 1555efe9872eSLisandro Dalcin 1556efe9872eSLisandro Dalcin Input Parameters: 1557efe9872eSLisandro Dalcin + ts - the TS context 1558efe9872eSLisandro Dalcin . t - current time 1559efe9872eSLisandro Dalcin . U - state vector 1560efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t) 1561efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt) 1562efe9872eSLisandro Dalcin 1563efe9872eSLisandro Dalcin Output Parameter: 1564efe9872eSLisandro Dalcin . F - the residual vector 1565efe9872eSLisandro Dalcin 1566efe9872eSLisandro Dalcin Note: 1567efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1568efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1569efe9872eSLisandro Dalcin 1570efe9872eSLisandro Dalcin Level: developer 1571efe9872eSLisandro Dalcin 1572db781477SPatrick Sanan .seealso: `TSSetI2Function()`, `TSGetI2Function()` 1573efe9872eSLisandro Dalcin @*/ 1574efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F) 1575efe9872eSLisandro Dalcin { 1576efe9872eSLisandro Dalcin DM dm; 1577efe9872eSLisandro Dalcin TSI2Function I2Function; 1578efe9872eSLisandro Dalcin void *ctx; 1579efe9872eSLisandro Dalcin TSRHSFunction rhsfunction; 1580efe9872eSLisandro Dalcin 1581efe9872eSLisandro Dalcin PetscFunctionBegin; 1582efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1583efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1584efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1585efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1586efe9872eSLisandro Dalcin PetscValidHeaderSpecific(F,VEC_CLASSID,6); 1587efe9872eSLisandro Dalcin 15889566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 15899566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Function(dm,&I2Function,&ctx)); 15909566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,NULL)); 1591efe9872eSLisandro Dalcin 1592efe9872eSLisandro Dalcin if (!I2Function) { 15939566063dSJacob Faibussowitsch PetscCall(TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE)); 1594efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1595efe9872eSLisandro Dalcin } 1596efe9872eSLisandro Dalcin 15979566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_FunctionEval,ts,U,V,F)); 1598efe9872eSLisandro Dalcin 1599792fecdfSBarry Smith PetscCallBack("TS callback implicit function",I2Function(ts,t,U,V,A,F,ctx)); 1600efe9872eSLisandro Dalcin 1601efe9872eSLisandro Dalcin if (rhsfunction) { 1602efe9872eSLisandro Dalcin Vec Frhs; 16039566063dSJacob Faibussowitsch PetscCall(TSGetRHSVec_Private(ts,&Frhs)); 16049566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts,t,U,Frhs)); 16059566063dSJacob Faibussowitsch PetscCall(VecAXPY(F,-1,Frhs)); 1606efe9872eSLisandro Dalcin } 1607efe9872eSLisandro Dalcin 16089566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_FunctionEval,ts,U,V,F)); 1609efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1610efe9872eSLisandro Dalcin } 1611efe9872eSLisandro Dalcin 1612efe9872eSLisandro Dalcin /*@ 1613efe9872eSLisandro Dalcin TSComputeI2Jacobian - Evaluates the Jacobian of the DAE 1614efe9872eSLisandro Dalcin 1615d083f849SBarry Smith Collective on TS 1616efe9872eSLisandro Dalcin 1617efe9872eSLisandro Dalcin Input Parameters: 1618efe9872eSLisandro Dalcin + ts - the TS context 1619efe9872eSLisandro Dalcin . t - current timestep 1620efe9872eSLisandro Dalcin . U - state vector 1621efe9872eSLisandro Dalcin . V - time derivative of state vector 1622efe9872eSLisandro Dalcin . A - second time derivative of state vector 1623efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below 1624efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below 1625efe9872eSLisandro Dalcin 1626efe9872eSLisandro Dalcin Output Parameters: 1627efe9872eSLisandro Dalcin + J - Jacobian matrix 1628efe9872eSLisandro Dalcin - P - optional preconditioning matrix 1629efe9872eSLisandro Dalcin 1630efe9872eSLisandro Dalcin Notes: 1631efe9872eSLisandro Dalcin If F(t,U,V,A)=0 is the DAE, the required Jacobian is 1632efe9872eSLisandro Dalcin 1633efe9872eSLisandro Dalcin dF/dU + shiftV*dF/dV + shiftA*dF/dA 1634efe9872eSLisandro Dalcin 1635efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1636efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1637efe9872eSLisandro Dalcin 1638efe9872eSLisandro Dalcin Level: developer 1639efe9872eSLisandro Dalcin 1640db781477SPatrick Sanan .seealso: `TSSetI2Jacobian()` 1641efe9872eSLisandro Dalcin @*/ 1642efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P) 1643efe9872eSLisandro Dalcin { 1644efe9872eSLisandro Dalcin DM dm; 1645efe9872eSLisandro Dalcin TSI2Jacobian I2Jacobian; 1646efe9872eSLisandro Dalcin void *ctx; 1647efe9872eSLisandro Dalcin TSRHSJacobian rhsjacobian; 1648efe9872eSLisandro Dalcin 1649efe9872eSLisandro Dalcin PetscFunctionBegin; 1650efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1651efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1652efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1653efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1654efe9872eSLisandro Dalcin PetscValidHeaderSpecific(J,MAT_CLASSID,8); 1655efe9872eSLisandro Dalcin PetscValidHeaderSpecific(P,MAT_CLASSID,9); 1656efe9872eSLisandro Dalcin 16579566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 16589566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx)); 16599566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm,&rhsjacobian,NULL)); 1660efe9872eSLisandro Dalcin 1661efe9872eSLisandro Dalcin if (!I2Jacobian) { 16629566063dSJacob Faibussowitsch PetscCall(TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE)); 1663efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1664efe9872eSLisandro Dalcin } 1665efe9872eSLisandro Dalcin 16669566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_JacobianEval,ts,U,J,P)); 1667792fecdfSBarry Smith PetscCallBack("TS callback implicit Jacobian",I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx)); 1668efe9872eSLisandro Dalcin if (rhsjacobian) { 1669d60b7d5cSBarry Smith Mat Jrhs,Prhs; 16709566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts,&Jrhs,&Prhs)); 16719566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs)); 16729566063dSJacob Faibussowitsch PetscCall(MatAXPY(J,-1,Jrhs,ts->axpy_pattern)); 16739566063dSJacob Faibussowitsch if (P != J) PetscCall(MatAXPY(P,-1,Prhs,ts->axpy_pattern)); 1674efe9872eSLisandro Dalcin } 1675efe9872eSLisandro Dalcin 16769566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_JacobianEval,ts,U,J,P)); 1677efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1678efe9872eSLisandro Dalcin } 1679efe9872eSLisandro Dalcin 1680438f35afSJed Brown /*@C 1681438f35afSJed Brown TSSetTransientVariable - sets function to transform from state to transient variables 1682438f35afSJed Brown 1683438f35afSJed Brown Logically Collective 1684438f35afSJed Brown 16854165533cSJose E. Roman Input Parameters: 1686438f35afSJed Brown + ts - time stepping context on which to change the transient variable 1687a96d6ef6SBarry Smith . tvar - a function that transforms to transient variables 1688438f35afSJed Brown - ctx - a context for tvar 1689438f35afSJed Brown 1690a96d6ef6SBarry Smith Calling sequence of tvar: 1691a96d6ef6SBarry Smith $ PetscErrorCode tvar(TS ts,Vec p,Vec c,void *ctx); 1692a96d6ef6SBarry Smith 1693a96d6ef6SBarry Smith + ts - timestep context 16946aad120cSJose E. Roman . p - input vector (primitive form) 1695a96d6ef6SBarry Smith . c - output vector, transient variables (conservative form) 1696a96d6ef6SBarry Smith - ctx - [optional] user-defined function context 1697a96d6ef6SBarry Smith 1698438f35afSJed Brown Level: advanced 1699438f35afSJed Brown 1700438f35afSJed Brown Notes: 1701438f35afSJed Brown This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., TSBDF) 1702438f35afSJed Brown can be conservative. In this context, primitive variables P are used to model the state (e.g., because they lead to 1703438f35afSJed Brown well-conditioned formulations even in limiting cases such as low-Mach or zero porosity). The transient variable is 1704438f35afSJed Brown C(P), specified by calling this function. An IFunction thus receives arguments (P, Cdot) and the IJacobian must be 1705438f35afSJed Brown evaluated via the chain rule, as in 1706438f35afSJed Brown 1707438f35afSJed Brown dF/dP + shift * dF/dCdot dC/dP. 1708438f35afSJed Brown 1709db781477SPatrick Sanan .seealso: `DMTSSetTransientVariable()`, `DMTSGetTransientVariable()`, `TSSetIFunction()`, `TSSetIJacobian()` 1710438f35afSJed Brown @*/ 1711438f35afSJed Brown PetscErrorCode TSSetTransientVariable(TS ts,TSTransientVariable tvar,void *ctx) 1712438f35afSJed Brown { 1713438f35afSJed Brown DM dm; 1714438f35afSJed Brown 1715438f35afSJed Brown PetscFunctionBegin; 1716438f35afSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 17179566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 17189566063dSJacob Faibussowitsch PetscCall(DMTSSetTransientVariable(dm,tvar,ctx)); 1719438f35afSJed Brown PetscFunctionReturn(0); 1720438f35afSJed Brown } 1721438f35afSJed Brown 1722efe9872eSLisandro Dalcin /*@ 1723e3c11fc1SJed Brown TSComputeTransientVariable - transforms state (primitive) variables to transient (conservative) variables 1724e3c11fc1SJed Brown 1725e3c11fc1SJed Brown Logically Collective 1726e3c11fc1SJed Brown 1727e3c11fc1SJed Brown Input Parameters: 1728e3c11fc1SJed Brown + ts - TS on which to compute 1729e3c11fc1SJed Brown - U - state vector to be transformed to transient variables 1730e3c11fc1SJed Brown 1731e3c11fc1SJed Brown Output Parameters: 1732e3c11fc1SJed Brown . C - transient (conservative) variable 1733e3c11fc1SJed Brown 1734e3c11fc1SJed Brown Developer Notes: 1735e3c11fc1SJed Brown If DMTSSetTransientVariable() has not been called, then C is not modified in this routine and C=NULL is allowed. 1736e3c11fc1SJed Brown This makes it safe to call without a guard. One can use TSHasTransientVariable() to check if transient variables are 1737e3c11fc1SJed Brown being used. 1738e3c11fc1SJed Brown 1739e3c11fc1SJed Brown Level: developer 1740e3c11fc1SJed Brown 1741db781477SPatrick Sanan .seealso: `DMTSSetTransientVariable()`, `TSComputeIFunction()`, `TSComputeIJacobian()` 1742e3c11fc1SJed Brown @*/ 1743e3c11fc1SJed Brown PetscErrorCode TSComputeTransientVariable(TS ts,Vec U,Vec C) 1744e3c11fc1SJed Brown { 1745e3c11fc1SJed Brown DM dm; 1746e3c11fc1SJed Brown DMTS dmts; 1747e3c11fc1SJed Brown 1748e3c11fc1SJed Brown PetscFunctionBegin; 1749e3c11fc1SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1750e3c11fc1SJed Brown PetscValidHeaderSpecific(U,VEC_CLASSID,2); 17519566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 17529566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm,&dmts)); 1753e3c11fc1SJed Brown if (dmts->ops->transientvar) { 1754e3c11fc1SJed Brown PetscValidHeaderSpecific(C,VEC_CLASSID,3); 17559566063dSJacob Faibussowitsch PetscCall((*dmts->ops->transientvar)(ts,U,C,dmts->transientvarctx)); 1756e3c11fc1SJed Brown } 1757e3c11fc1SJed Brown PetscFunctionReturn(0); 1758e3c11fc1SJed Brown } 1759e3c11fc1SJed Brown 1760e3c11fc1SJed Brown /*@ 1761e3c11fc1SJed Brown TSHasTransientVariable - determine whether transient variables have been set 1762e3c11fc1SJed Brown 1763e3c11fc1SJed Brown Logically Collective 1764e3c11fc1SJed Brown 1765e3c11fc1SJed Brown Input Parameters: 1766e3c11fc1SJed Brown . ts - TS on which to compute 1767e3c11fc1SJed Brown 1768e3c11fc1SJed Brown Output Parameters: 1769e3c11fc1SJed Brown . has - PETSC_TRUE if transient variables have been set 1770e3c11fc1SJed Brown 1771e3c11fc1SJed Brown Level: developer 1772e3c11fc1SJed Brown 1773db781477SPatrick Sanan .seealso: `DMTSSetTransientVariable()`, `TSComputeTransientVariable()` 1774e3c11fc1SJed Brown @*/ 1775e3c11fc1SJed Brown PetscErrorCode TSHasTransientVariable(TS ts,PetscBool *has) 1776e3c11fc1SJed Brown { 1777e3c11fc1SJed Brown DM dm; 1778e3c11fc1SJed Brown DMTS dmts; 1779e3c11fc1SJed Brown 1780e3c11fc1SJed Brown PetscFunctionBegin; 1781e3c11fc1SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 17829566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 17839566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm,&dmts)); 1784e3c11fc1SJed Brown *has = dmts->ops->transientvar ? PETSC_TRUE : PETSC_FALSE; 1785e3c11fc1SJed Brown PetscFunctionReturn(0); 1786e3c11fc1SJed Brown } 1787e3c11fc1SJed Brown 1788e3c11fc1SJed Brown /*@ 1789efe9872eSLisandro Dalcin TS2SetSolution - Sets the initial solution and time derivative vectors 1790efe9872eSLisandro Dalcin for use by the TS routines handling second order equations. 1791efe9872eSLisandro Dalcin 1792d083f849SBarry Smith Logically Collective on TS 1793efe9872eSLisandro Dalcin 1794efe9872eSLisandro Dalcin Input Parameters: 1795efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1796efe9872eSLisandro Dalcin . u - the solution vector 1797efe9872eSLisandro Dalcin - v - the time derivative vector 1798efe9872eSLisandro Dalcin 1799efe9872eSLisandro Dalcin Level: beginner 1800efe9872eSLisandro Dalcin 1801efe9872eSLisandro Dalcin @*/ 1802efe9872eSLisandro Dalcin PetscErrorCode TS2SetSolution(TS ts,Vec u,Vec v) 1803efe9872eSLisandro Dalcin { 1804efe9872eSLisandro Dalcin PetscFunctionBegin; 1805efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1806efe9872eSLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 1807efe9872eSLisandro Dalcin PetscValidHeaderSpecific(v,VEC_CLASSID,3); 18089566063dSJacob Faibussowitsch PetscCall(TSSetSolution(ts,u)); 18099566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)v)); 18109566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_dot)); 1811efe9872eSLisandro Dalcin ts->vec_dot = v; 1812efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1813efe9872eSLisandro Dalcin } 1814efe9872eSLisandro Dalcin 1815efe9872eSLisandro Dalcin /*@ 1816efe9872eSLisandro Dalcin TS2GetSolution - Returns the solution and time derivative at the present timestep 1817efe9872eSLisandro Dalcin for second order equations. It is valid to call this routine inside the function 1818efe9872eSLisandro Dalcin that you are evaluating in order to move to the new timestep. This vector not 1819efe9872eSLisandro Dalcin changed until the solution at the next timestep has been calculated. 1820efe9872eSLisandro Dalcin 1821efe9872eSLisandro Dalcin Not Collective, but Vec returned is parallel if TS is parallel 1822efe9872eSLisandro Dalcin 1823efe9872eSLisandro Dalcin Input Parameter: 1824efe9872eSLisandro Dalcin . ts - the TS context obtained from TSCreate() 1825efe9872eSLisandro Dalcin 1826d8d19677SJose E. Roman Output Parameters: 1827efe9872eSLisandro Dalcin + u - the vector containing the solution 1828efe9872eSLisandro Dalcin - v - the vector containing the time derivative 1829efe9872eSLisandro Dalcin 1830efe9872eSLisandro Dalcin Level: intermediate 1831efe9872eSLisandro Dalcin 1832db781477SPatrick Sanan .seealso: `TS2SetSolution()`, `TSGetTimeStep()`, `TSGetTime()` 1833efe9872eSLisandro Dalcin 1834efe9872eSLisandro Dalcin @*/ 1835efe9872eSLisandro Dalcin PetscErrorCode TS2GetSolution(TS ts,Vec *u,Vec *v) 1836efe9872eSLisandro Dalcin { 1837efe9872eSLisandro Dalcin PetscFunctionBegin; 1838efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1839efe9872eSLisandro Dalcin if (u) PetscValidPointer(u,2); 1840efe9872eSLisandro Dalcin if (v) PetscValidPointer(v,3); 1841efe9872eSLisandro Dalcin if (u) *u = ts->vec_sol; 1842efe9872eSLisandro Dalcin if (v) *v = ts->vec_dot; 1843efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1844efe9872eSLisandro Dalcin } 1845efe9872eSLisandro Dalcin 184655849f57SBarry Smith /*@C 184755849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 184855849f57SBarry Smith 184955849f57SBarry Smith Collective on PetscViewer 185055849f57SBarry Smith 185155849f57SBarry Smith Input Parameters: 185255849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 185355849f57SBarry Smith some related function before a call to TSLoad(). 185455849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 185555849f57SBarry Smith 185655849f57SBarry Smith Level: intermediate 185755849f57SBarry Smith 185855849f57SBarry Smith Notes: 185955849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 186055849f57SBarry Smith 186155849f57SBarry Smith Notes for advanced users: 186255849f57SBarry Smith Most users should not need to know the details of the binary storage 186355849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 186455849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 186555849f57SBarry Smith format is 186655849f57SBarry Smith .vb 186755849f57SBarry Smith has not yet been determined 186855849f57SBarry Smith .ve 186955849f57SBarry Smith 1870db781477SPatrick Sanan .seealso: `PetscViewerBinaryOpen()`, `TSView()`, `MatLoad()`, `VecLoad()` 187155849f57SBarry Smith @*/ 1872f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 187355849f57SBarry Smith { 187455849f57SBarry Smith PetscBool isbinary; 187555849f57SBarry Smith PetscInt classid; 187655849f57SBarry Smith char type[256]; 18772d53ad75SBarry Smith DMTS sdm; 1878ad6bc421SBarry Smith DM dm; 187955849f57SBarry Smith 188055849f57SBarry Smith PetscFunctionBegin; 1881f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 188255849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 18839566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary)); 18843c633725SBarry Smith PetscCheck(isbinary,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 188555849f57SBarry Smith 18869566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT)); 18873c633725SBarry Smith PetscCheck(classid == TS_FILE_CLASSID,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 18889566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR)); 18899566063dSJacob Faibussowitsch PetscCall(TSSetType(ts, type)); 18901baa6e33SBarry Smith if (ts->ops->load) PetscCall((*ts->ops->load)(ts,viewer)); 18919566063dSJacob Faibussowitsch PetscCall(DMCreate(PetscObjectComm((PetscObject)ts),&dm)); 18929566063dSJacob Faibussowitsch PetscCall(DMLoad(dm,viewer)); 18939566063dSJacob Faibussowitsch PetscCall(TSSetDM(ts,dm)); 18949566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(ts->dm,&ts->vec_sol)); 18959566063dSJacob Faibussowitsch PetscCall(VecLoad(ts->vec_sol,viewer)); 18969566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm,&sdm)); 18979566063dSJacob Faibussowitsch PetscCall(DMTSLoad(sdm,viewer)); 189855849f57SBarry Smith PetscFunctionReturn(0); 189955849f57SBarry Smith } 190055849f57SBarry Smith 19019804daf3SBarry Smith #include <petscdraw.h> 1902e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1903e04113cfSBarry Smith #include <petscviewersaws.h> 1904f05ece33SBarry Smith #endif 1905fe2efc57SMark 1906fe2efc57SMark /*@C 1907fe2efc57SMark TSViewFromOptions - View from Options 1908fe2efc57SMark 1909fe2efc57SMark Collective on TS 1910fe2efc57SMark 1911fe2efc57SMark Input Parameters: 1912fe2efc57SMark + A - the application ordering context 1913736c3998SJose E. Roman . obj - Optional object 1914736c3998SJose E. Roman - name - command line option 1915fe2efc57SMark 1916fe2efc57SMark Level: intermediate 1917db781477SPatrick Sanan .seealso: `TS`, `TSView`, `PetscObjectViewFromOptions()`, `TSCreate()` 1918fe2efc57SMark @*/ 1919fe2efc57SMark PetscErrorCode TSViewFromOptions(TS A,PetscObject obj,const char name[]) 1920fe2efc57SMark { 1921fe2efc57SMark PetscFunctionBegin; 1922fe2efc57SMark PetscValidHeaderSpecific(A,TS_CLASSID,1); 19239566063dSJacob Faibussowitsch PetscCall(PetscObjectViewFromOptions((PetscObject)A,obj,name)); 1924fe2efc57SMark PetscFunctionReturn(0); 1925fe2efc57SMark } 1926fe2efc57SMark 19277e2c5f70SBarry Smith /*@C 1928d763cef2SBarry Smith TSView - Prints the TS data structure. 1929d763cef2SBarry Smith 19304c49b128SBarry Smith Collective on TS 1931d763cef2SBarry Smith 1932d763cef2SBarry Smith Input Parameters: 1933d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1934d763cef2SBarry Smith - viewer - visualization context 1935d763cef2SBarry Smith 1936d763cef2SBarry Smith Options Database Key: 1937d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1938d763cef2SBarry Smith 1939d763cef2SBarry Smith Notes: 1940d763cef2SBarry Smith The available visualization contexts include 1941b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1942b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1943d763cef2SBarry Smith output where only the first processor opens 1944d763cef2SBarry Smith the file. All other processors send their 1945d763cef2SBarry Smith data to the first processor to print. 1946d763cef2SBarry Smith 1947d763cef2SBarry Smith The user can open an alternative visualization context with 1948b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1949d763cef2SBarry Smith 1950595c91d4SBarry Smith In the debugger you can do "call TSView(ts,0)" to display the TS solver. (The same holds for any PETSc object viewer). 1951595c91d4SBarry Smith 1952d763cef2SBarry Smith Level: beginner 1953d763cef2SBarry Smith 1954db781477SPatrick Sanan .seealso: `PetscViewerASCIIOpen()` 1955d763cef2SBarry Smith @*/ 19567087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1957d763cef2SBarry Smith { 195819fd82e9SBarry Smith TSType type; 19592b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 19602d53ad75SBarry Smith DMTS sdm; 1961e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1962536b137fSBarry Smith PetscBool issaws; 1963f05ece33SBarry Smith #endif 1964d763cef2SBarry Smith 1965d763cef2SBarry Smith PetscFunctionBegin; 19660700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 19671e66621cSBarry Smith if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer)); 19680700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1969c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1970fd16b177SBarry Smith 19719566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii)); 19729566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring)); 19739566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary)); 19749566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw)); 1975e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 19769566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws)); 1977f05ece33SBarry Smith #endif 197832077d6dSBarry Smith if (iascii) { 19799566063dSJacob Faibussowitsch PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer)); 1980efd4aadfSBarry Smith if (ts->ops->view) { 19819566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 19829566063dSJacob Faibussowitsch PetscCall((*ts->ops->view)(ts,viewer)); 19839566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 1984efd4aadfSBarry Smith } 19851e66621cSBarry Smith if (ts->max_steps < PETSC_MAX_INT) PetscCall(PetscViewerASCIIPrintf(viewer," maximum steps=%" PetscInt_FMT "\n",ts->max_steps)); 19861e66621cSBarry Smith if (ts->max_time < PETSC_MAX_REAL) PetscCall(PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time)); 19871e66621cSBarry Smith if (ts->ifuncs) PetscCall(PetscViewerASCIIPrintf(viewer," total number of I function evaluations=%" PetscInt_FMT "\n",ts->ifuncs)); 19881e66621cSBarry Smith if (ts->ijacs) PetscCall(PetscViewerASCIIPrintf(viewer," total number of I Jacobian evaluations=%" PetscInt_FMT "\n",ts->ijacs)); 19891e66621cSBarry Smith if (ts->rhsfuncs) PetscCall(PetscViewerASCIIPrintf(viewer," total number of RHS function evaluations=%" PetscInt_FMT "\n",ts->rhsfuncs)); 19901e66621cSBarry Smith if (ts->rhsjacs) PetscCall(PetscViewerASCIIPrintf(viewer," total number of RHS Jacobian evaluations=%" PetscInt_FMT "\n",ts->rhsjacs)); 1991efd4aadfSBarry Smith if (ts->usessnes) { 1992efd4aadfSBarry Smith PetscBool lin; 19931e66621cSBarry Smith if (ts->problem_type == TS_NONLINEAR) PetscCall(PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%" PetscInt_FMT "\n",ts->snes_its)); 199463a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%" PetscInt_FMT "\n",ts->ksp_its)); 19959566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"")); 199663a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of %slinear solve failures=%" PetscInt_FMT "\n",lin ? "" : "non",ts->num_snes_failures)); 1997efd4aadfSBarry Smith } 199863a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of rejected steps=%" PetscInt_FMT "\n",ts->reject)); 19991e66621cSBarry Smith if (ts->vrtol) PetscCall(PetscViewerASCIIPrintf(viewer," using vector of relative error tolerances, ")); 20001e66621cSBarry Smith else PetscCall(PetscViewerASCIIPrintf(viewer," using relative error tolerance of %g, ",(double)ts->rtol)); 20011e66621cSBarry Smith if (ts->vatol) PetscCall(PetscViewerASCIIPrintf(viewer," using vector of absolute error tolerances\n")); 20021e66621cSBarry Smith else PetscCall(PetscViewerASCIIPrintf(viewer," using absolute error tolerance of %g\n",(double)ts->atol)); 20039566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 20049566063dSJacob Faibussowitsch PetscCall(TSAdaptView(ts->adapt,viewer)); 20059566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 20060f5bd95cSBarry Smith } else if (isstring) { 20079566063dSJacob Faibussowitsch PetscCall(TSGetType(ts,&type)); 20089566063dSJacob Faibussowitsch PetscCall(PetscViewerStringSPrintf(viewer," TSType: %-7.7s",type)); 20099566063dSJacob Faibussowitsch if (ts->ops->view) PetscCall((*ts->ops->view)(ts,viewer)); 201055849f57SBarry Smith } else if (isbinary) { 201155849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 201255849f57SBarry Smith MPI_Comm comm; 201355849f57SBarry Smith PetscMPIInt rank; 201455849f57SBarry Smith char type[256]; 201555849f57SBarry Smith 20169566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)ts,&comm)); 20179566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm,&rank)); 2018dd400576SPatrick Sanan if (rank == 0) { 20199566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT)); 20209566063dSJacob Faibussowitsch PetscCall(PetscStrncpy(type,((PetscObject)ts)->type_name,256)); 20219566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR)); 202255849f57SBarry Smith } 20231baa6e33SBarry Smith if (ts->ops->view) PetscCall((*ts->ops->view)(ts,viewer)); 20249566063dSJacob Faibussowitsch if (ts->adapt) PetscCall(TSAdaptView(ts->adapt,viewer)); 20259566063dSJacob Faibussowitsch PetscCall(DMView(ts->dm,viewer)); 20269566063dSJacob Faibussowitsch PetscCall(VecView(ts->vec_sol,viewer)); 20279566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm,&sdm)); 20289566063dSJacob Faibussowitsch PetscCall(DMTSView(sdm,viewer)); 20292b0a91c0SBarry Smith } else if (isdraw) { 20302b0a91c0SBarry Smith PetscDraw draw; 20312b0a91c0SBarry Smith char str[36]; 203289fd9fafSBarry Smith PetscReal x,y,bottom,h; 20332b0a91c0SBarry Smith 20349566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(viewer,0,&draw)); 20359566063dSJacob Faibussowitsch PetscCall(PetscDrawGetCurrentPoint(draw,&x,&y)); 20369566063dSJacob Faibussowitsch PetscCall(PetscStrcpy(str,"TS: ")); 20379566063dSJacob Faibussowitsch PetscCall(PetscStrcat(str,((PetscObject)ts)->type_name)); 20389566063dSJacob Faibussowitsch PetscCall(PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h)); 203989fd9fafSBarry Smith bottom = y - h; 20409566063dSJacob Faibussowitsch PetscCall(PetscDrawPushCurrentPoint(draw,x,bottom)); 20411baa6e33SBarry Smith if (ts->ops->view) PetscCall((*ts->ops->view)(ts,viewer)); 20429566063dSJacob Faibussowitsch if (ts->adapt) PetscCall(TSAdaptView(ts->adapt,viewer)); 20439566063dSJacob Faibussowitsch if (ts->snes) PetscCall(SNESView(ts->snes,viewer)); 20449566063dSJacob Faibussowitsch PetscCall(PetscDrawPopCurrentPoint(draw)); 2045e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 2046536b137fSBarry Smith } else if (issaws) { 2047d45a07a7SBarry Smith PetscMPIInt rank; 20482657e9d9SBarry Smith const char *name; 20492657e9d9SBarry Smith 20509566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)ts,&name)); 20519566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); 2052dd400576SPatrick Sanan if (!((PetscObject)ts)->amsmem && rank == 0) { 2053d45a07a7SBarry Smith char dir[1024]; 2054d45a07a7SBarry Smith 20559566063dSJacob Faibussowitsch PetscCall(PetscObjectViewSAWs((PetscObject)ts,viewer)); 20569566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name)); 2057792fecdfSBarry Smith PetscCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 20589566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name)); 2059792fecdfSBarry Smith PetscCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 2060d763cef2SBarry Smith } 20611baa6e33SBarry Smith if (ts->ops->view) PetscCall((*ts->ops->view)(ts,viewer)); 2062f05ece33SBarry Smith #endif 2063f05ece33SBarry Smith } 206436a9e3b9SBarry Smith if (ts->snes && ts->usessnes) { 20659566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 20669566063dSJacob Faibussowitsch PetscCall(SNESView(ts->snes,viewer)); 20679566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 206836a9e3b9SBarry Smith } 20699566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm,&sdm)); 20709566063dSJacob Faibussowitsch PetscCall(DMTSView(sdm,viewer)); 2071f05ece33SBarry Smith 20729566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 20739566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials)); 20749566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 2075d763cef2SBarry Smith PetscFunctionReturn(0); 2076d763cef2SBarry Smith } 2077d763cef2SBarry Smith 2078b07ff414SBarry Smith /*@ 2079d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 2080d763cef2SBarry Smith the timesteppers. 2081d763cef2SBarry Smith 20823f9fe445SBarry Smith Logically Collective on TS 2083d763cef2SBarry Smith 2084d763cef2SBarry Smith Input Parameters: 2085d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2086d763cef2SBarry Smith - usrP - optional user context 2087d763cef2SBarry Smith 208895452b02SPatrick Sanan Fortran Notes: 208995452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 2090daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2091daf670e6SBarry Smith 2092d763cef2SBarry Smith Level: intermediate 2093d763cef2SBarry Smith 2094db781477SPatrick Sanan .seealso: `TSGetApplicationContext()` 2095d763cef2SBarry Smith @*/ 20967087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 2097d763cef2SBarry Smith { 2098d763cef2SBarry Smith PetscFunctionBegin; 20990700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2100d763cef2SBarry Smith ts->user = usrP; 2101d763cef2SBarry Smith PetscFunctionReturn(0); 2102d763cef2SBarry Smith } 2103d763cef2SBarry Smith 2104b07ff414SBarry Smith /*@ 2105d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 2106d763cef2SBarry Smith timestepper. 2107d763cef2SBarry Smith 2108d763cef2SBarry Smith Not Collective 2109d763cef2SBarry Smith 2110d763cef2SBarry Smith Input Parameter: 2111d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2112d763cef2SBarry Smith 2113d763cef2SBarry Smith Output Parameter: 2114d763cef2SBarry Smith . usrP - user context 2115d763cef2SBarry Smith 211695452b02SPatrick Sanan Fortran Notes: 211795452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 2118daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2119daf670e6SBarry Smith 2120d763cef2SBarry Smith Level: intermediate 2121d763cef2SBarry Smith 2122db781477SPatrick Sanan .seealso: `TSSetApplicationContext()` 2123d763cef2SBarry Smith @*/ 2124e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 2125d763cef2SBarry Smith { 2126d763cef2SBarry Smith PetscFunctionBegin; 21270700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2128e71120c6SJed Brown *(void**)usrP = ts->user; 2129d763cef2SBarry Smith PetscFunctionReturn(0); 2130d763cef2SBarry Smith } 2131d763cef2SBarry Smith 2132d763cef2SBarry Smith /*@ 213380275a0aSLisandro Dalcin TSGetStepNumber - Gets the number of steps completed. 2134d763cef2SBarry Smith 2135d763cef2SBarry Smith Not Collective 2136d763cef2SBarry Smith 2137d763cef2SBarry Smith Input Parameter: 2138d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2139d763cef2SBarry Smith 2140d763cef2SBarry Smith Output Parameter: 214180275a0aSLisandro Dalcin . steps - number of steps completed so far 2142d763cef2SBarry Smith 2143d763cef2SBarry Smith Level: intermediate 2144d763cef2SBarry Smith 2145db781477SPatrick Sanan .seealso: `TSGetTime()`, `TSGetTimeStep()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostStage()`, `TSSetPostStep()` 2146d763cef2SBarry Smith @*/ 214780275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps) 2148d763cef2SBarry Smith { 2149d763cef2SBarry Smith PetscFunctionBegin; 21500700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 215180275a0aSLisandro Dalcin PetscValidIntPointer(steps,2); 215280275a0aSLisandro Dalcin *steps = ts->steps; 215380275a0aSLisandro Dalcin PetscFunctionReturn(0); 215480275a0aSLisandro Dalcin } 215580275a0aSLisandro Dalcin 215680275a0aSLisandro Dalcin /*@ 215780275a0aSLisandro Dalcin TSSetStepNumber - Sets the number of steps completed. 215880275a0aSLisandro Dalcin 215980275a0aSLisandro Dalcin Logically Collective on TS 216080275a0aSLisandro Dalcin 216180275a0aSLisandro Dalcin Input Parameters: 216280275a0aSLisandro Dalcin + ts - the TS context 216380275a0aSLisandro Dalcin - steps - number of steps completed so far 216480275a0aSLisandro Dalcin 216580275a0aSLisandro Dalcin Notes: 216680275a0aSLisandro Dalcin For most uses of the TS solvers the user need not explicitly call 216780275a0aSLisandro Dalcin TSSetStepNumber(), as the step counter is appropriately updated in 216880275a0aSLisandro Dalcin TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to 216980275a0aSLisandro Dalcin reinitialize timestepping by setting the step counter to zero (and time 217080275a0aSLisandro Dalcin to the initial time) to solve a similar problem with different initial 217180275a0aSLisandro Dalcin conditions or parameters. Other possible use case is to continue 217280275a0aSLisandro Dalcin timestepping from a previously interrupted run in such a way that TS 217380275a0aSLisandro Dalcin monitors will be called with a initial nonzero step counter. 217480275a0aSLisandro Dalcin 217580275a0aSLisandro Dalcin Level: advanced 217680275a0aSLisandro Dalcin 2177db781477SPatrick Sanan .seealso: `TSGetStepNumber()`, `TSSetTime()`, `TSSetTimeStep()`, `TSSetSolution()` 217880275a0aSLisandro Dalcin @*/ 217980275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps) 218080275a0aSLisandro Dalcin { 218180275a0aSLisandro Dalcin PetscFunctionBegin; 218280275a0aSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 218380275a0aSLisandro Dalcin PetscValidLogicalCollectiveInt(ts,steps,2); 21843c633725SBarry Smith PetscCheck(steps >= 0,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative"); 218580275a0aSLisandro Dalcin ts->steps = steps; 2186d763cef2SBarry Smith PetscFunctionReturn(0); 2187d763cef2SBarry Smith } 2188d763cef2SBarry Smith 2189d763cef2SBarry Smith /*@ 2190d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 2191d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 2192d763cef2SBarry Smith 21933f9fe445SBarry Smith Logically Collective on TS 2194d763cef2SBarry Smith 2195d763cef2SBarry Smith Input Parameters: 2196d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2197d763cef2SBarry Smith - time_step - the size of the timestep 2198d763cef2SBarry Smith 2199d763cef2SBarry Smith Level: intermediate 2200d763cef2SBarry Smith 2201db781477SPatrick Sanan .seealso: `TSGetTimeStep()`, `TSSetTime()` 2202d763cef2SBarry Smith 2203d763cef2SBarry Smith @*/ 22047087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 2205d763cef2SBarry Smith { 2206d763cef2SBarry Smith PetscFunctionBegin; 22070700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2208c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 2209d763cef2SBarry Smith ts->time_step = time_step; 2210d763cef2SBarry Smith PetscFunctionReturn(0); 2211d763cef2SBarry Smith } 2212d763cef2SBarry Smith 2213a43b19c4SJed Brown /*@ 221449354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 221549354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 221649354f04SShri Abhyankar or just return at the final time TS computed. 2217a43b19c4SJed Brown 2218a43b19c4SJed Brown Logically Collective on TS 2219a43b19c4SJed Brown 2220d8d19677SJose E. Roman Input Parameters: 2221a43b19c4SJed Brown + ts - the time-step context 222249354f04SShri Abhyankar - eftopt - exact final time option 2223a43b19c4SJed Brown 2224feed9e9dSBarry Smith $ TS_EXACTFINALTIME_STEPOVER - Don't do anything if final time is exceeded 2225feed9e9dSBarry Smith $ TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time 2226feed9e9dSBarry Smith $ TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time 2227feed9e9dSBarry Smith 2228feed9e9dSBarry Smith Options Database: 2229feed9e9dSBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime 2230feed9e9dSBarry Smith 2231ee346746SBarry Smith Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time 2232ee346746SBarry Smith then the final time you selected. 2233ee346746SBarry Smith 2234a43b19c4SJed Brown Level: beginner 2235a43b19c4SJed Brown 2236db781477SPatrick Sanan .seealso: `TSExactFinalTimeOption`, `TSGetExactFinalTime()` 2237a43b19c4SJed Brown @*/ 223849354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 2239a43b19c4SJed Brown { 2240a43b19c4SJed Brown PetscFunctionBegin; 2241a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 224249354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 224349354f04SShri Abhyankar ts->exact_final_time = eftopt; 2244a43b19c4SJed Brown PetscFunctionReturn(0); 2245a43b19c4SJed Brown } 2246a43b19c4SJed Brown 2247d763cef2SBarry Smith /*@ 2248f6953c82SLisandro Dalcin TSGetExactFinalTime - Gets the exact final time option. 2249f6953c82SLisandro Dalcin 2250f6953c82SLisandro Dalcin Not Collective 2251f6953c82SLisandro Dalcin 2252f6953c82SLisandro Dalcin Input Parameter: 2253f6953c82SLisandro Dalcin . ts - the TS context 2254f6953c82SLisandro Dalcin 2255f6953c82SLisandro Dalcin Output Parameter: 2256f6953c82SLisandro Dalcin . eftopt - exact final time option 2257f6953c82SLisandro Dalcin 2258f6953c82SLisandro Dalcin Level: beginner 2259f6953c82SLisandro Dalcin 2260db781477SPatrick Sanan .seealso: `TSExactFinalTimeOption`, `TSSetExactFinalTime()` 2261f6953c82SLisandro Dalcin @*/ 2262f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt) 2263f6953c82SLisandro Dalcin { 2264f6953c82SLisandro Dalcin PetscFunctionBegin; 2265f6953c82SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2266f6953c82SLisandro Dalcin PetscValidPointer(eftopt,2); 2267f6953c82SLisandro Dalcin *eftopt = ts->exact_final_time; 2268f6953c82SLisandro Dalcin PetscFunctionReturn(0); 2269f6953c82SLisandro Dalcin } 2270f6953c82SLisandro Dalcin 2271f6953c82SLisandro Dalcin /*@ 2272d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 2273d763cef2SBarry Smith 2274d763cef2SBarry Smith Not Collective 2275d763cef2SBarry Smith 2276d763cef2SBarry Smith Input Parameter: 2277d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2278d763cef2SBarry Smith 2279d763cef2SBarry Smith Output Parameter: 2280d763cef2SBarry Smith . dt - the current timestep size 2281d763cef2SBarry Smith 2282d763cef2SBarry Smith Level: intermediate 2283d763cef2SBarry Smith 2284db781477SPatrick Sanan .seealso: `TSSetTimeStep()`, `TSGetTime()` 2285d763cef2SBarry Smith 2286d763cef2SBarry Smith @*/ 22877087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 2288d763cef2SBarry Smith { 2289d763cef2SBarry Smith PetscFunctionBegin; 22900700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2291f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 2292d763cef2SBarry Smith *dt = ts->time_step; 2293d763cef2SBarry Smith PetscFunctionReturn(0); 2294d763cef2SBarry Smith } 2295d763cef2SBarry Smith 2296d8e5e3e6SSatish Balay /*@ 2297d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 2298d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 2299d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 2300d763cef2SBarry Smith the solution at the next timestep has been calculated. 2301d763cef2SBarry Smith 2302d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 2303d763cef2SBarry Smith 2304d763cef2SBarry Smith Input Parameter: 2305d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2306d763cef2SBarry Smith 2307d763cef2SBarry Smith Output Parameter: 2308d763cef2SBarry Smith . v - the vector containing the solution 2309d763cef2SBarry Smith 231063e21af5SBarry Smith Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested 231163e21af5SBarry Smith final time. It returns the solution at the next timestep. 231263e21af5SBarry Smith 2313d763cef2SBarry Smith Level: intermediate 2314d763cef2SBarry Smith 2315db781477SPatrick Sanan .seealso: `TSGetTimeStep()`, `TSGetTime()`, `TSGetSolveTime()`, `TSGetSolutionComponents()`, `TSSetSolutionFunction()` 2316d763cef2SBarry Smith 2317d763cef2SBarry Smith @*/ 23187087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 2319d763cef2SBarry Smith { 2320d763cef2SBarry Smith PetscFunctionBegin; 23210700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 23224482741eSBarry Smith PetscValidPointer(v,2); 23238737fe31SLisandro Dalcin *v = ts->vec_sol; 2324d763cef2SBarry Smith PetscFunctionReturn(0); 2325d763cef2SBarry Smith } 2326d763cef2SBarry Smith 232703fe5f5eSDebojyoti Ghosh /*@ 2328b2bf4f3aSDebojyoti Ghosh TSGetSolutionComponents - Returns any solution components at the present 232903fe5f5eSDebojyoti Ghosh timestep, if available for the time integration method being used. 2330b2bf4f3aSDebojyoti Ghosh Solution components are quantities that share the same size and 233103fe5f5eSDebojyoti Ghosh structure as the solution vector. 233203fe5f5eSDebojyoti Ghosh 233303fe5f5eSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 233403fe5f5eSDebojyoti Ghosh 233503fe5f5eSDebojyoti Ghosh Parameters : 2336a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2337b2bf4f3aSDebojyoti Ghosh . n - If v is PETSC_NULL, then the number of solution components is 2338b2bf4f3aSDebojyoti Ghosh returned through n, else the n-th solution component is 233903fe5f5eSDebojyoti Ghosh returned in v. 2340a2b725a8SWilliam Gropp - v - the vector containing the n-th solution component 234103fe5f5eSDebojyoti Ghosh (may be PETSC_NULL to use this function to find out 2342b2bf4f3aSDebojyoti Ghosh the number of solutions components). 234303fe5f5eSDebojyoti Ghosh 23444cdd57e5SDebojyoti Ghosh Level: advanced 234503fe5f5eSDebojyoti Ghosh 2346db781477SPatrick Sanan .seealso: `TSGetSolution()` 234703fe5f5eSDebojyoti Ghosh 234803fe5f5eSDebojyoti Ghosh @*/ 2349b2bf4f3aSDebojyoti Ghosh PetscErrorCode TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v) 235003fe5f5eSDebojyoti Ghosh { 235103fe5f5eSDebojyoti Ghosh PetscFunctionBegin; 235203fe5f5eSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2353b2bf4f3aSDebojyoti Ghosh if (!ts->ops->getsolutioncomponents) *n = 0; 23541e66621cSBarry Smith else PetscCall((*ts->ops->getsolutioncomponents)(ts,n,v)); 235503fe5f5eSDebojyoti Ghosh PetscFunctionReturn(0); 235603fe5f5eSDebojyoti Ghosh } 235703fe5f5eSDebojyoti Ghosh 23584cdd57e5SDebojyoti Ghosh /*@ 23594cdd57e5SDebojyoti Ghosh TSGetAuxSolution - Returns an auxiliary solution at the present 23604cdd57e5SDebojyoti Ghosh timestep, if available for the time integration method being used. 23614cdd57e5SDebojyoti Ghosh 23624cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 23634cdd57e5SDebojyoti Ghosh 23644cdd57e5SDebojyoti Ghosh Parameters : 2365a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2366a2b725a8SWilliam Gropp - v - the vector containing the auxiliary solution 23674cdd57e5SDebojyoti Ghosh 23684cdd57e5SDebojyoti Ghosh Level: intermediate 23694cdd57e5SDebojyoti Ghosh 2370db781477SPatrick Sanan .seealso: `TSGetSolution()` 23714cdd57e5SDebojyoti Ghosh 23724cdd57e5SDebojyoti Ghosh @*/ 23734cdd57e5SDebojyoti Ghosh PetscErrorCode TSGetAuxSolution(TS ts,Vec *v) 23744cdd57e5SDebojyoti Ghosh { 23754cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 23764cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 23771e66621cSBarry Smith if (ts->ops->getauxsolution) PetscCall((*ts->ops->getauxsolution)(ts,v)); 23781e66621cSBarry Smith else PetscCall(VecZeroEntries(*v)); 23794cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 23804cdd57e5SDebojyoti Ghosh } 23814cdd57e5SDebojyoti Ghosh 23824cdd57e5SDebojyoti Ghosh /*@ 23834cdd57e5SDebojyoti Ghosh TSGetTimeError - Returns the estimated error vector, if the chosen 23844cdd57e5SDebojyoti Ghosh TSType has an error estimation functionality. 23854cdd57e5SDebojyoti Ghosh 23864cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 23874cdd57e5SDebojyoti Ghosh 23889657682dSDebojyoti Ghosh Note: MUST call after TSSetUp() 23899657682dSDebojyoti Ghosh 23904cdd57e5SDebojyoti Ghosh Parameters : 2391a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2392657c1e31SEmil Constantinescu . n - current estimate (n=0) or previous one (n=-1) 2393a2b725a8SWilliam Gropp - v - the vector containing the error (same size as the solution). 23944cdd57e5SDebojyoti Ghosh 23954cdd57e5SDebojyoti Ghosh Level: intermediate 23964cdd57e5SDebojyoti Ghosh 2397db781477SPatrick Sanan .seealso: `TSGetSolution()`, `TSSetTimeError()` 23984cdd57e5SDebojyoti Ghosh 23994cdd57e5SDebojyoti Ghosh @*/ 24000a01e1b2SEmil Constantinescu PetscErrorCode TSGetTimeError(TS ts,PetscInt n,Vec *v) 24014cdd57e5SDebojyoti Ghosh { 24024cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 24034cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 24041e66621cSBarry Smith if (ts->ops->gettimeerror) PetscCall((*ts->ops->gettimeerror)(ts,n,v)); 24051e66621cSBarry Smith else PetscCall(VecZeroEntries(*v)); 24064cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 24074cdd57e5SDebojyoti Ghosh } 24084cdd57e5SDebojyoti Ghosh 240957df6a1bSDebojyoti Ghosh /*@ 241057df6a1bSDebojyoti Ghosh TSSetTimeError - Sets the estimated error vector, if the chosen 241157df6a1bSDebojyoti Ghosh TSType has an error estimation functionality. This can be used 241257df6a1bSDebojyoti Ghosh to restart such a time integrator with a given error vector. 241357df6a1bSDebojyoti Ghosh 241457df6a1bSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 241557df6a1bSDebojyoti Ghosh 241657df6a1bSDebojyoti Ghosh Parameters : 2417a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2418a2b725a8SWilliam Gropp - v - the vector containing the error (same size as the solution). 241957df6a1bSDebojyoti Ghosh 242057df6a1bSDebojyoti Ghosh Level: intermediate 242157df6a1bSDebojyoti Ghosh 2422db781477SPatrick Sanan .seealso: `TSSetSolution()`, `TSGetTimeError)` 242357df6a1bSDebojyoti Ghosh 242457df6a1bSDebojyoti Ghosh @*/ 242557df6a1bSDebojyoti Ghosh PetscErrorCode TSSetTimeError(TS ts,Vec v) 242657df6a1bSDebojyoti Ghosh { 242757df6a1bSDebojyoti Ghosh PetscFunctionBegin; 242857df6a1bSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 24293c633725SBarry Smith PetscCheck(ts->setupcalled,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first"); 24301baa6e33SBarry Smith if (ts->ops->settimeerror) PetscCall((*ts->ops->settimeerror)(ts,v)); 243157df6a1bSDebojyoti Ghosh PetscFunctionReturn(0); 243257df6a1bSDebojyoti Ghosh } 243357df6a1bSDebojyoti Ghosh 2434bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 2435d8e5e3e6SSatish Balay /*@ 2436bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 2437d763cef2SBarry Smith 2438bdad233fSMatthew Knepley Not collective 2439d763cef2SBarry Smith 2440bdad233fSMatthew Knepley Input Parameters: 2441bdad233fSMatthew Knepley + ts - The TS 2442bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2443d763cef2SBarry Smith .vb 24440910c330SBarry Smith U_t - A U = 0 (linear) 24450910c330SBarry Smith U_t - A(t) U = 0 (linear) 24460910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 2447d763cef2SBarry Smith .ve 2448d763cef2SBarry Smith 2449d763cef2SBarry Smith Level: beginner 2450d763cef2SBarry Smith 2451db781477SPatrick Sanan .seealso: `TSSetUp()`, `TSProblemType`, `TS` 2452d763cef2SBarry Smith @*/ 24537087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 2454a7cc72afSBarry Smith { 2455d763cef2SBarry Smith PetscFunctionBegin; 24560700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2457bdad233fSMatthew Knepley ts->problem_type = type; 24589e2a6581SJed Brown if (type == TS_LINEAR) { 24599e2a6581SJed Brown SNES snes; 24609566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 24619566063dSJacob Faibussowitsch PetscCall(SNESSetType(snes,SNESKSPONLY)); 24629e2a6581SJed Brown } 2463d763cef2SBarry Smith PetscFunctionReturn(0); 2464d763cef2SBarry Smith } 2465d763cef2SBarry Smith 2466bdad233fSMatthew Knepley /*@C 2467bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 2468bdad233fSMatthew Knepley 2469bdad233fSMatthew Knepley Not collective 2470bdad233fSMatthew Knepley 2471bdad233fSMatthew Knepley Input Parameter: 2472bdad233fSMatthew Knepley . ts - The TS 2473bdad233fSMatthew Knepley 2474bdad233fSMatthew Knepley Output Parameter: 2475bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2476bdad233fSMatthew Knepley .vb 2477089b2837SJed Brown M U_t = A U 2478089b2837SJed Brown M(t) U_t = A(t) U 2479b5abc632SBarry Smith F(t,U,U_t) 2480bdad233fSMatthew Knepley .ve 2481bdad233fSMatthew Knepley 2482bdad233fSMatthew Knepley Level: beginner 2483bdad233fSMatthew Knepley 2484db781477SPatrick Sanan .seealso: `TSSetUp()`, `TSProblemType`, `TS` 2485bdad233fSMatthew Knepley @*/ 24867087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 2487a7cc72afSBarry Smith { 2488bdad233fSMatthew Knepley PetscFunctionBegin; 24890700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 24904482741eSBarry Smith PetscValidIntPointer(type,2); 2491bdad233fSMatthew Knepley *type = ts->problem_type; 2492bdad233fSMatthew Knepley PetscFunctionReturn(0); 2493bdad233fSMatthew Knepley } 2494d763cef2SBarry Smith 2495303a5415SBarry Smith /* 2496303a5415SBarry 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() 2497303a5415SBarry Smith */ 2498303a5415SBarry Smith static PetscErrorCode TSSetExactFinalTimeDefault(TS ts) 2499303a5415SBarry Smith { 2500303a5415SBarry Smith PetscBool isnone; 2501303a5415SBarry Smith 2502303a5415SBarry Smith PetscFunctionBegin; 25039566063dSJacob Faibussowitsch PetscCall(TSGetAdapt(ts,&ts->adapt)); 25049566063dSJacob Faibussowitsch PetscCall(TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type)); 2505303a5415SBarry Smith 25069566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone)); 25071e66621cSBarry Smith if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP; 25081e66621cSBarry Smith else if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) ts->exact_final_time = TS_EXACTFINALTIME_INTERPOLATE; 2509303a5415SBarry Smith PetscFunctionReturn(0); 2510303a5415SBarry Smith } 2511303a5415SBarry Smith 2512d763cef2SBarry Smith /*@ 2513303a5415SBarry Smith TSSetUp - Sets up the internal data structures for the later use of a timestepper. 2514d763cef2SBarry Smith 2515d763cef2SBarry Smith Collective on TS 2516d763cef2SBarry Smith 2517d763cef2SBarry Smith Input Parameter: 2518d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2519d763cef2SBarry Smith 2520d763cef2SBarry Smith Notes: 2521d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 2522d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 2523141bd67dSStefano Zampini the call to TSStep() or TSSolve(). However, if one wishes to control this 2524d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 2525141bd67dSStefano Zampini and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve(). 2526d763cef2SBarry Smith 2527d763cef2SBarry Smith Level: advanced 2528d763cef2SBarry Smith 2529db781477SPatrick Sanan .seealso: `TSCreate()`, `TSStep()`, `TSDestroy()`, `TSSolve()` 2530d763cef2SBarry Smith @*/ 25317087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 2532d763cef2SBarry Smith { 25336c6b9e74SPeter Brune DM dm; 25346c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 2535d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 2536cd11d68dSLisandro Dalcin TSIFunction ifun; 25376c6b9e74SPeter Brune TSIJacobian ijac; 2538efe9872eSLisandro Dalcin TSI2Jacobian i2jac; 25396c6b9e74SPeter Brune TSRHSJacobian rhsjac; 2540d763cef2SBarry Smith 2541d763cef2SBarry Smith PetscFunctionBegin; 25420700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2543277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 2544277b19d0SLisandro Dalcin 25457adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 25469566063dSJacob Faibussowitsch PetscCall(TSGetIFunction(ts,NULL,&ifun,NULL)); 25479566063dSJacob Faibussowitsch PetscCall(TSSetType(ts,ifun ? TSBEULER : TSEULER)); 2548d763cef2SBarry Smith } 2549277b19d0SLisandro Dalcin 25501a638600SStefano Zampini if (!ts->vec_sol) { 25511e66621cSBarry Smith PetscCheck(ts->dm,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 25529566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(ts->dm,&ts->vec_sol)); 25531a638600SStefano Zampini } 2554277b19d0SLisandro Dalcin 25554a658b32SHong Zhang if (ts->tspan) { 25561e66621cSBarry Smith if (!ts->tspan->vecs_sol) PetscCall(VecDuplicateVecs(ts->vec_sol,ts->tspan->num_span_times,&ts->tspan->vecs_sol)); 25574a658b32SHong Zhang } 2558298bade4SHong Zhang if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */ 25599566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)ts->Jacprhs)); 2560298bade4SHong Zhang ts->Jacp = ts->Jacprhs; 2561298bade4SHong Zhang } 2562298bade4SHong Zhang 2563cd4cee2dSHong Zhang if (ts->quadraturets) { 25649566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts->quadraturets)); 25659566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_costintegrand)); 25669566063dSJacob Faibussowitsch PetscCall(VecDuplicate(ts->quadraturets->vec_sol,&ts->vec_costintegrand)); 2567cd4cee2dSHong Zhang } 2568cd4cee2dSHong Zhang 25699566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL)); 2570f23ba4b3SHong Zhang if (rhsjac == TSComputeRHSJacobianConstant) { 2571e1244c69SJed Brown Mat Amat,Pmat; 2572e1244c69SJed Brown SNES snes; 25739566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 25749566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL)); 2575e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 2576e1244c69SJed Brown * have displaced the RHS matrix */ 2577971015bcSStefano Zampini if (Amat && Amat == ts->Arhs) { 2578abc0d4abSBarry 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 */ 25799566063dSJacob Faibussowitsch PetscCall(MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat)); 25809566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes,Amat,NULL,NULL,NULL)); 25819566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Amat)); 2582e1244c69SJed Brown } 2583971015bcSStefano Zampini if (Pmat && Pmat == ts->Brhs) { 25849566063dSJacob Faibussowitsch PetscCall(MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat)); 25859566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes,NULL,Pmat,NULL,NULL)); 25869566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Pmat)); 2587e1244c69SJed Brown } 2588e1244c69SJed Brown } 25892ffb9264SLisandro Dalcin 25909566063dSJacob Faibussowitsch PetscCall(TSGetAdapt(ts,&ts->adapt)); 25919566063dSJacob Faibussowitsch PetscCall(TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type)); 25922ffb9264SLisandro Dalcin 25931baa6e33SBarry Smith if (ts->ops->setup) PetscCall((*ts->ops->setup)(ts)); 2594277b19d0SLisandro Dalcin 25959566063dSJacob Faibussowitsch PetscCall(TSSetExactFinalTimeDefault(ts)); 25962ffb9264SLisandro Dalcin 2597a6772fa2SLisandro Dalcin /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 25986c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 25996c6b9e74SPeter Brune */ 26009566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 26019566063dSJacob Faibussowitsch PetscCall(DMSNESGetFunction(dm,&func,NULL)); 26021e66621cSBarry Smith if (!func) PetscCall(DMSNESSetFunction(dm,SNESTSFormFunction,ts)); 26031e66621cSBarry Smith 2604a6772fa2SLisandro 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. 26056c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 26066c6b9e74SPeter Brune */ 26079566063dSJacob Faibussowitsch PetscCall(DMSNESGetJacobian(dm,&jac,NULL)); 26089566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm,&ijac,NULL)); 26099566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm,&i2jac,NULL)); 26109566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm,&rhsjac,NULL)); 26111e66621cSBarry Smith if (!jac && (ijac || i2jac || rhsjac)) PetscCall(DMSNESSetJacobian(dm,SNESTSFormJacobian,ts)); 2612c0517034SDebojyoti Ghosh 2613c0517034SDebojyoti Ghosh /* if time integration scheme has a starting method, call it */ 26141baa6e33SBarry Smith if (ts->ops->startingmethod) PetscCall((*ts->ops->startingmethod)(ts)); 2615c0517034SDebojyoti Ghosh 2616277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 2617277b19d0SLisandro Dalcin PetscFunctionReturn(0); 2618277b19d0SLisandro Dalcin } 2619277b19d0SLisandro Dalcin 2620f6a906c0SBarry Smith /*@ 2621277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 2622277b19d0SLisandro Dalcin 2623277b19d0SLisandro Dalcin Collective on TS 2624277b19d0SLisandro Dalcin 2625277b19d0SLisandro Dalcin Input Parameter: 2626277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 2627277b19d0SLisandro Dalcin 2628277b19d0SLisandro Dalcin Level: beginner 2629277b19d0SLisandro Dalcin 2630db781477SPatrick Sanan .seealso: `TSCreate()`, `TSSetup()`, `TSDestroy()` 2631277b19d0SLisandro Dalcin @*/ 2632277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 2633277b19d0SLisandro Dalcin { 26341d06f6b3SHong Zhang TS_RHSSplitLink ilink = ts->tsrhssplit,next; 2635277b19d0SLisandro Dalcin 2636277b19d0SLisandro Dalcin PetscFunctionBegin; 2637277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2638b18ea86cSHong Zhang 26391baa6e33SBarry Smith if (ts->ops->reset) PetscCall((*ts->ops->reset)(ts)); 26409566063dSJacob Faibussowitsch if (ts->snes) PetscCall(SNESReset(ts->snes)); 26419566063dSJacob Faibussowitsch if (ts->adapt) PetscCall(TSAdaptReset(ts->adapt)); 2642bbd56ea5SKarl Rupp 26439566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Arhs)); 26449566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Brhs)); 26459566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->Frhs)); 26469566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_sol)); 26479566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_dot)); 26489566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vatol)); 26499566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vrtol)); 26509566063dSJacob Faibussowitsch PetscCall(VecDestroyVecs(ts->nwork,&ts->work)); 2651bbd56ea5SKarl Rupp 26529566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Jacprhs)); 26539566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Jacp)); 26541baa6e33SBarry Smith if (ts->forward_solve) PetscCall(TSForwardReset(ts)); 2655cd4cee2dSHong Zhang if (ts->quadraturets) { 26569566063dSJacob Faibussowitsch PetscCall(TSReset(ts->quadraturets)); 26579566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_costintegrand)); 2658cd4cee2dSHong Zhang } 26591d06f6b3SHong Zhang while (ilink) { 26601d06f6b3SHong Zhang next = ilink->next; 26619566063dSJacob Faibussowitsch PetscCall(TSDestroy(&ilink->ts)); 26629566063dSJacob Faibussowitsch PetscCall(PetscFree(ilink->splitname)); 26639566063dSJacob Faibussowitsch PetscCall(ISDestroy(&ilink->is)); 26649566063dSJacob Faibussowitsch PetscCall(PetscFree(ilink)); 26651d06f6b3SHong Zhang ilink = next; 266687f4e208SHong Zhang } 26676bf673ebSJoe Pusztay ts->tsrhssplit = NULL; 2668545aaa6fSHong Zhang ts->num_rhs_splits = 0; 26694a658b32SHong Zhang if (ts->tspan) { 26704a658b32SHong Zhang PetscCall(PetscFree(ts->tspan->span_times)); 26714a658b32SHong Zhang PetscCall(VecDestroyVecs(ts->tspan->num_span_times,&ts->tspan->vecs_sol)); 26724a658b32SHong Zhang PetscCall(PetscFree(ts->tspan)); 26734a658b32SHong Zhang } 2674277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 2675d763cef2SBarry Smith PetscFunctionReturn(0); 2676d763cef2SBarry Smith } 2677d763cef2SBarry Smith 26781fb7b255SJunchao Zhang /*@C 2679d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2680d763cef2SBarry Smith with TSCreate(). 2681d763cef2SBarry Smith 2682d763cef2SBarry Smith Collective on TS 2683d763cef2SBarry Smith 2684d763cef2SBarry Smith Input Parameter: 2685d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2686d763cef2SBarry Smith 2687d763cef2SBarry Smith Level: beginner 2688d763cef2SBarry Smith 2689db781477SPatrick Sanan .seealso: `TSCreate()`, `TSSetUp()`, `TSSolve()` 2690d763cef2SBarry Smith @*/ 26916bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2692d763cef2SBarry Smith { 2693d763cef2SBarry Smith PetscFunctionBegin; 26946bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 2695ecf68647SHong Zhang PetscValidHeaderSpecific(*ts,TS_CLASSID,1); 2696c793f718SLisandro Dalcin if (--((PetscObject)(*ts))->refct > 0) {*ts = NULL; PetscFunctionReturn(0);} 2697d763cef2SBarry Smith 26989566063dSJacob Faibussowitsch PetscCall(TSReset(*ts)); 26999566063dSJacob Faibussowitsch PetscCall(TSAdjointReset(*ts)); 27001e66621cSBarry Smith if ((*ts)->forward_solve) PetscCall(TSForwardReset(*ts)); 27011e66621cSBarry Smith 2702e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 27039566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsViewOff((PetscObject)*ts)); 27049566063dSJacob Faibussowitsch if ((*ts)->ops->destroy) PetscCall((*(*ts)->ops->destroy)((*ts))); 27056d4c513bSLisandro Dalcin 27069566063dSJacob Faibussowitsch PetscCall(TSTrajectoryDestroy(&(*ts)->trajectory)); 2707bc952696SBarry Smith 27089566063dSJacob Faibussowitsch PetscCall(TSAdaptDestroy(&(*ts)->adapt)); 27099566063dSJacob Faibussowitsch PetscCall(TSEventDestroy(&(*ts)->event)); 27106427ac75SLisandro Dalcin 27119566063dSJacob Faibussowitsch PetscCall(SNESDestroy(&(*ts)->snes)); 27129566063dSJacob Faibussowitsch PetscCall(DMDestroy(&(*ts)->dm)); 27139566063dSJacob Faibussowitsch PetscCall(TSMonitorCancel((*ts))); 27149566063dSJacob Faibussowitsch PetscCall(TSAdjointMonitorCancel((*ts))); 27156d4c513bSLisandro Dalcin 27169566063dSJacob Faibussowitsch PetscCall(TSDestroy(&(*ts)->quadraturets)); 27179566063dSJacob Faibussowitsch PetscCall(PetscHeaderDestroy(ts)); 2718d763cef2SBarry Smith PetscFunctionReturn(0); 2719d763cef2SBarry Smith } 2720d763cef2SBarry Smith 2721d8e5e3e6SSatish Balay /*@ 2722d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2723d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2724d763cef2SBarry Smith 2725d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2726d763cef2SBarry Smith 2727d763cef2SBarry Smith Input Parameter: 2728d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2729d763cef2SBarry Smith 2730d763cef2SBarry Smith Output Parameter: 2731d763cef2SBarry Smith . snes - the nonlinear solver context 2732d763cef2SBarry Smith 2733d763cef2SBarry Smith Notes: 2734d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2735d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 273694b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2737d763cef2SBarry Smith 2738d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 27390298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2740d763cef2SBarry Smith 2741d763cef2SBarry Smith Level: beginner 2742d763cef2SBarry Smith 2743d763cef2SBarry Smith @*/ 27447087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2745d763cef2SBarry Smith { 2746d763cef2SBarry Smith PetscFunctionBegin; 27470700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 27484482741eSBarry Smith PetscValidPointer(snes,2); 2749d372ba47SLisandro Dalcin if (!ts->snes) { 27509566063dSJacob Faibussowitsch PetscCall(SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes)); 27519566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options)); 27529566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts)); 27539566063dSJacob Faibussowitsch PetscCall(PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes)); 27549566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1)); 27559566063dSJacob Faibussowitsch if (ts->dm) PetscCall(SNESSetDM(ts->snes,ts->dm)); 27561e66621cSBarry Smith if (ts->problem_type == TS_LINEAR) PetscCall(SNESSetType(ts->snes,SNESKSPONLY)); 2757d372ba47SLisandro Dalcin } 2758d763cef2SBarry Smith *snes = ts->snes; 2759d763cef2SBarry Smith PetscFunctionReturn(0); 2760d763cef2SBarry Smith } 2761d763cef2SBarry Smith 2762deb2cd25SJed Brown /*@ 2763deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2764deb2cd25SJed Brown 2765deb2cd25SJed Brown Collective 2766deb2cd25SJed Brown 2767d8d19677SJose E. Roman Input Parameters: 2768deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2769deb2cd25SJed Brown - snes - the nonlinear solver context 2770deb2cd25SJed Brown 2771deb2cd25SJed Brown Notes: 2772deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2773deb2cd25SJed Brown 2774deb2cd25SJed Brown Level: developer 2775deb2cd25SJed Brown 2776deb2cd25SJed Brown @*/ 2777deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2778deb2cd25SJed Brown { 2779d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2780deb2cd25SJed Brown 2781deb2cd25SJed Brown PetscFunctionBegin; 2782deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2783deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 27849566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)snes)); 27859566063dSJacob Faibussowitsch PetscCall(SNESDestroy(&ts->snes)); 2786bbd56ea5SKarl Rupp 2787deb2cd25SJed Brown ts->snes = snes; 2788bbd56ea5SKarl Rupp 27899566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts)); 27909566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL)); 27911e66621cSBarry Smith if (func == SNESTSFormJacobian) PetscCall(SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts)); 2792deb2cd25SJed Brown PetscFunctionReturn(0); 2793deb2cd25SJed Brown } 2794deb2cd25SJed Brown 2795d8e5e3e6SSatish Balay /*@ 279694b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2797d763cef2SBarry Smith a TS (timestepper) context. 2798d763cef2SBarry Smith 279994b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2800d763cef2SBarry Smith 2801d763cef2SBarry Smith Input Parameter: 2802d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2803d763cef2SBarry Smith 2804d763cef2SBarry Smith Output Parameter: 280594b7f48cSBarry Smith . ksp - the nonlinear solver context 2806d763cef2SBarry Smith 2807d763cef2SBarry Smith Notes: 280894b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2809d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2810d763cef2SBarry Smith KSP and PC contexts as well. 2811d763cef2SBarry Smith 281294b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 28130298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2814d763cef2SBarry Smith 2815d763cef2SBarry Smith Level: beginner 2816d763cef2SBarry Smith 2817d763cef2SBarry Smith @*/ 28187087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2819d763cef2SBarry Smith { 2820089b2837SJed Brown SNES snes; 2821d372ba47SLisandro Dalcin 2822d763cef2SBarry Smith PetscFunctionBegin; 28230700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 28244482741eSBarry Smith PetscValidPointer(ksp,2); 28253c633725SBarry Smith PetscCheck(((PetscObject)ts)->type_name,PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 28263c633725SBarry Smith PetscCheck(ts->problem_type == TS_LINEAR,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 28279566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 28289566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes,ksp)); 2829d763cef2SBarry Smith PetscFunctionReturn(0); 2830d763cef2SBarry Smith } 2831d763cef2SBarry Smith 2832d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2833d763cef2SBarry Smith 2834adb62b0dSMatthew Knepley /*@ 2835618ce8baSLisandro Dalcin TSSetMaxSteps - Sets the maximum number of steps to use. 2836618ce8baSLisandro Dalcin 2837618ce8baSLisandro Dalcin Logically Collective on TS 2838618ce8baSLisandro Dalcin 2839618ce8baSLisandro Dalcin Input Parameters: 2840618ce8baSLisandro Dalcin + ts - the TS context obtained from TSCreate() 2841618ce8baSLisandro Dalcin - maxsteps - maximum number of steps to use 2842618ce8baSLisandro Dalcin 2843618ce8baSLisandro Dalcin Options Database Keys: 2844618ce8baSLisandro Dalcin . -ts_max_steps <maxsteps> - Sets maxsteps 2845618ce8baSLisandro Dalcin 2846618ce8baSLisandro Dalcin Notes: 2847618ce8baSLisandro Dalcin The default maximum number of steps is 5000 2848618ce8baSLisandro Dalcin 2849618ce8baSLisandro Dalcin Level: intermediate 2850618ce8baSLisandro Dalcin 2851db781477SPatrick Sanan .seealso: `TSGetMaxSteps()`, `TSSetMaxTime()`, `TSSetExactFinalTime()` 2852618ce8baSLisandro Dalcin @*/ 2853618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps) 2854618ce8baSLisandro Dalcin { 2855618ce8baSLisandro Dalcin PetscFunctionBegin; 2856618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2857618ce8baSLisandro Dalcin PetscValidLogicalCollectiveInt(ts,maxsteps,2); 28583c633725SBarry Smith PetscCheck(maxsteps >= 0,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative"); 2859618ce8baSLisandro Dalcin ts->max_steps = maxsteps; 2860618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2861618ce8baSLisandro Dalcin } 2862618ce8baSLisandro Dalcin 2863618ce8baSLisandro Dalcin /*@ 2864618ce8baSLisandro Dalcin TSGetMaxSteps - Gets the maximum number of steps to use. 2865618ce8baSLisandro Dalcin 2866618ce8baSLisandro Dalcin Not Collective 2867618ce8baSLisandro Dalcin 2868618ce8baSLisandro Dalcin Input Parameters: 2869618ce8baSLisandro Dalcin . ts - the TS context obtained from TSCreate() 2870618ce8baSLisandro Dalcin 2871618ce8baSLisandro Dalcin Output Parameter: 2872618ce8baSLisandro Dalcin . maxsteps - maximum number of steps to use 2873618ce8baSLisandro Dalcin 2874618ce8baSLisandro Dalcin Level: advanced 2875618ce8baSLisandro Dalcin 2876db781477SPatrick Sanan .seealso: `TSSetMaxSteps()`, `TSGetMaxTime()`, `TSSetMaxTime()` 2877618ce8baSLisandro Dalcin @*/ 2878618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps) 2879618ce8baSLisandro Dalcin { 2880618ce8baSLisandro Dalcin PetscFunctionBegin; 2881618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2882618ce8baSLisandro Dalcin PetscValidIntPointer(maxsteps,2); 2883618ce8baSLisandro Dalcin *maxsteps = ts->max_steps; 2884618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2885618ce8baSLisandro Dalcin } 2886618ce8baSLisandro Dalcin 2887618ce8baSLisandro Dalcin /*@ 2888618ce8baSLisandro Dalcin TSSetMaxTime - Sets the maximum (or final) time for timestepping. 2889618ce8baSLisandro Dalcin 2890618ce8baSLisandro Dalcin Logically Collective on TS 2891618ce8baSLisandro Dalcin 2892618ce8baSLisandro Dalcin Input Parameters: 2893618ce8baSLisandro Dalcin + ts - the TS context obtained from TSCreate() 2894618ce8baSLisandro Dalcin - maxtime - final time to step to 2895618ce8baSLisandro Dalcin 2896618ce8baSLisandro Dalcin Options Database Keys: 2897ef85077eSLisandro Dalcin . -ts_max_time <maxtime> - Sets maxtime 2898618ce8baSLisandro Dalcin 2899618ce8baSLisandro Dalcin Notes: 2900618ce8baSLisandro Dalcin The default maximum time is 5.0 2901618ce8baSLisandro Dalcin 2902618ce8baSLisandro Dalcin Level: intermediate 2903618ce8baSLisandro Dalcin 2904db781477SPatrick Sanan .seealso: `TSGetMaxTime()`, `TSSetMaxSteps()`, `TSSetExactFinalTime()` 2905618ce8baSLisandro Dalcin @*/ 2906618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime) 2907618ce8baSLisandro Dalcin { 2908618ce8baSLisandro Dalcin PetscFunctionBegin; 2909618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2910618ce8baSLisandro Dalcin PetscValidLogicalCollectiveReal(ts,maxtime,2); 2911618ce8baSLisandro Dalcin ts->max_time = maxtime; 2912618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2913618ce8baSLisandro Dalcin } 2914618ce8baSLisandro Dalcin 2915618ce8baSLisandro Dalcin /*@ 2916618ce8baSLisandro Dalcin TSGetMaxTime - Gets the maximum (or final) time for timestepping. 2917618ce8baSLisandro Dalcin 2918618ce8baSLisandro Dalcin Not Collective 2919618ce8baSLisandro Dalcin 2920618ce8baSLisandro Dalcin Input Parameters: 2921618ce8baSLisandro Dalcin . ts - the TS context obtained from TSCreate() 2922618ce8baSLisandro Dalcin 2923618ce8baSLisandro Dalcin Output Parameter: 2924618ce8baSLisandro Dalcin . maxtime - final time to step to 2925618ce8baSLisandro Dalcin 2926618ce8baSLisandro Dalcin Level: advanced 2927618ce8baSLisandro Dalcin 2928db781477SPatrick Sanan .seealso: `TSSetMaxTime()`, `TSGetMaxSteps()`, `TSSetMaxSteps()` 2929618ce8baSLisandro Dalcin @*/ 2930618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime) 2931618ce8baSLisandro Dalcin { 2932618ce8baSLisandro Dalcin PetscFunctionBegin; 2933618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2934618ce8baSLisandro Dalcin PetscValidRealPointer(maxtime,2); 2935618ce8baSLisandro Dalcin *maxtime = ts->max_time; 2936618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2937618ce8baSLisandro Dalcin } 2938618ce8baSLisandro Dalcin 2939618ce8baSLisandro Dalcin /*@ 2940aaa6c58dSLisandro Dalcin TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep(). 2941edc382c3SSatish Balay 2942edc382c3SSatish Balay Level: deprecated 2943edc382c3SSatish Balay 2944aaa6c58dSLisandro Dalcin @*/ 2945aaa6c58dSLisandro Dalcin PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 2946aaa6c58dSLisandro Dalcin { 2947aaa6c58dSLisandro Dalcin PetscFunctionBegin; 2948aaa6c58dSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 29499566063dSJacob Faibussowitsch PetscCall(TSSetTime(ts,initial_time)); 29509566063dSJacob Faibussowitsch PetscCall(TSSetTimeStep(ts,time_step)); 2951aaa6c58dSLisandro Dalcin PetscFunctionReturn(0); 2952aaa6c58dSLisandro Dalcin } 2953aaa6c58dSLisandro Dalcin 2954aaa6c58dSLisandro Dalcin /*@ 295519eac22cSLisandro Dalcin TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime(). 2956edc382c3SSatish Balay 2957edc382c3SSatish Balay Level: deprecated 2958edc382c3SSatish Balay 2959adb62b0dSMatthew Knepley @*/ 29607087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2961adb62b0dSMatthew Knepley { 2962adb62b0dSMatthew Knepley PetscFunctionBegin; 29630700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2964abc0a331SBarry Smith if (maxsteps) { 29654482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 2966adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2967adb62b0dSMatthew Knepley } 2968abc0a331SBarry Smith if (maxtime) { 2969064a246eSJacob Faibussowitsch PetscValidRealPointer(maxtime,3); 2970adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2971adb62b0dSMatthew Knepley } 2972adb62b0dSMatthew Knepley PetscFunctionReturn(0); 2973adb62b0dSMatthew Knepley } 2974adb62b0dSMatthew Knepley 2975d763cef2SBarry Smith /*@ 297619eac22cSLisandro Dalcin TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime(). 2977edc382c3SSatish Balay 2978edc382c3SSatish Balay Level: deprecated 2979edc382c3SSatish Balay 2980d763cef2SBarry Smith @*/ 29817087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2982d763cef2SBarry Smith { 2983d763cef2SBarry Smith PetscFunctionBegin; 29840700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2985c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2986064a246eSJacob Faibussowitsch PetscValidLogicalCollectiveReal(ts,maxtime,3); 298739b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 298839b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2989d763cef2SBarry Smith PetscFunctionReturn(0); 2990d763cef2SBarry Smith } 2991d763cef2SBarry Smith 2992d763cef2SBarry Smith /*@ 29935c5f5948SLisandro Dalcin TSGetTimeStepNumber - Deprecated, use TSGetStepNumber(). 2994edc382c3SSatish Balay 2995edc382c3SSatish Balay Level: deprecated 2996edc382c3SSatish Balay 29975c5f5948SLisandro Dalcin @*/ 2998e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); } 29995c5f5948SLisandro Dalcin 300019eac22cSLisandro Dalcin /*@ 30014f4e0956SLisandro Dalcin TSGetTotalSteps - Deprecated, use TSGetStepNumber(). 3002edc382c3SSatish Balay 3003edc382c3SSatish Balay Level: deprecated 3004edc382c3SSatish Balay 30054f4e0956SLisandro Dalcin @*/ 30064f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); } 30074f4e0956SLisandro Dalcin 30084f4e0956SLisandro Dalcin /*@ 3009d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 3010d763cef2SBarry Smith for use by the TS routines. 3011d763cef2SBarry Smith 3012d083f849SBarry Smith Logically Collective on TS 3013d763cef2SBarry Smith 3014d763cef2SBarry Smith Input Parameters: 3015d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 30160910c330SBarry Smith - u - the solution vector 3017d763cef2SBarry Smith 3018d763cef2SBarry Smith Level: beginner 3019d763cef2SBarry Smith 3020db781477SPatrick Sanan .seealso: `TSSetSolutionFunction()`, `TSGetSolution()`, `TSCreate()` 3021d763cef2SBarry Smith @*/ 30220910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 3023d763cef2SBarry Smith { 3024496e6a7aSJed Brown DM dm; 30258737fe31SLisandro Dalcin 3026d763cef2SBarry Smith PetscFunctionBegin; 30270700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 30280910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 30299566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)u)); 30309566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_sol)); 30310910c330SBarry Smith ts->vec_sol = u; 3032bbd56ea5SKarl Rupp 30339566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 30349566063dSJacob Faibussowitsch PetscCall(DMShellSetGlobalVector(dm,u)); 3035d763cef2SBarry Smith PetscFunctionReturn(0); 3036d763cef2SBarry Smith } 3037d763cef2SBarry Smith 3038ac226902SBarry Smith /*@C 3039000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 30403f2090d5SJed Brown called once at the beginning of each time step. 3041000e7ae3SMatthew Knepley 30423f9fe445SBarry Smith Logically Collective on TS 3043000e7ae3SMatthew Knepley 3044000e7ae3SMatthew Knepley Input Parameters: 3045000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3046000e7ae3SMatthew Knepley - func - The function 3047000e7ae3SMatthew Knepley 3048000e7ae3SMatthew Knepley Calling sequence of func: 304967b8a455SSatish Balay .vb 305067b8a455SSatish Balay PetscErrorCode func (TS ts); 305167b8a455SSatish Balay .ve 3052000e7ae3SMatthew Knepley 3053000e7ae3SMatthew Knepley Level: intermediate 3054000e7ae3SMatthew Knepley 3055db781477SPatrick Sanan .seealso: `TSSetPreStage()`, `TSSetPostStage()`, `TSSetPostStep()`, `TSStep()`, `TSRestartStep()` 3056000e7ae3SMatthew Knepley @*/ 30577087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 3058000e7ae3SMatthew Knepley { 3059000e7ae3SMatthew Knepley PetscFunctionBegin; 30600700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3061ae60f76fSBarry Smith ts->prestep = func; 3062000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3063000e7ae3SMatthew Knepley } 3064000e7ae3SMatthew Knepley 306509ee8438SJed Brown /*@ 30663f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 30673f2090d5SJed Brown 30683f2090d5SJed Brown Collective on TS 30693f2090d5SJed Brown 30703f2090d5SJed Brown Input Parameters: 30713f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 30723f2090d5SJed Brown 30733f2090d5SJed Brown Notes: 30743f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 30753f2090d5SJed Brown so most users would not generally call this routine themselves. 30763f2090d5SJed Brown 30773f2090d5SJed Brown Level: developer 30783f2090d5SJed Brown 3079db781477SPatrick Sanan .seealso: `TSSetPreStep()`, `TSPreStage()`, `TSPostStage()`, `TSPostStep()` 30803f2090d5SJed Brown @*/ 30817087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 30823f2090d5SJed Brown { 30833f2090d5SJed Brown PetscFunctionBegin; 30840700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3085ae60f76fSBarry Smith if (ts->prestep) { 30865efd42a4SStefano Zampini Vec U; 3087ef8d1ce0SJohann Rudi PetscObjectId idprev; 3088ef8d1ce0SJohann Rudi PetscBool sameObject; 30895efd42a4SStefano Zampini PetscObjectState sprev,spost; 30905efd42a4SStefano Zampini 30919566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts,&U)); 30929566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U,&idprev)); 30939566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&sprev)); 309425e27a38SBarry Smith PetscCallBack("TS callback preset",(*ts->prestep)(ts)); 30959566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts,&U)); 30969566063dSJacob Faibussowitsch PetscCall(PetscObjectCompareId((PetscObject)U,idprev,&sameObject)); 30979566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&spost)); 30989566063dSJacob Faibussowitsch if (!sameObject || sprev != spost) PetscCall(TSRestartStep(ts)); 3099312ce896SJed Brown } 31003f2090d5SJed Brown PetscFunctionReturn(0); 31013f2090d5SJed Brown } 31023f2090d5SJed Brown 3103b8123daeSJed Brown /*@C 3104b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 3105b8123daeSJed Brown called once at the beginning of each stage. 3106b8123daeSJed Brown 3107b8123daeSJed Brown Logically Collective on TS 3108b8123daeSJed Brown 3109b8123daeSJed Brown Input Parameters: 3110b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 3111b8123daeSJed Brown - func - The function 3112b8123daeSJed Brown 3113b8123daeSJed Brown Calling sequence of func: 311467b8a455SSatish Balay .vb 311567b8a455SSatish Balay PetscErrorCode func(TS ts, PetscReal stagetime); 311667b8a455SSatish Balay .ve 3117b8123daeSJed Brown 3118b8123daeSJed Brown Level: intermediate 3119b8123daeSJed Brown 3120b8123daeSJed Brown Note: 3121b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 312280275a0aSLisandro Dalcin The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being 3123b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 3124b8123daeSJed Brown 3125db781477SPatrick Sanan .seealso: `TSSetPostStage()`, `TSSetPreStep()`, `TSSetPostStep()`, `TSGetApplicationContext()` 3126b8123daeSJed Brown @*/ 3127b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 3128b8123daeSJed Brown { 3129b8123daeSJed Brown PetscFunctionBegin; 3130b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3131ae60f76fSBarry Smith ts->prestage = func; 3132b8123daeSJed Brown PetscFunctionReturn(0); 3133b8123daeSJed Brown } 3134b8123daeSJed Brown 31359be3e283SDebojyoti Ghosh /*@C 31369be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 31379be3e283SDebojyoti Ghosh called once at the end of each stage. 31389be3e283SDebojyoti Ghosh 31399be3e283SDebojyoti Ghosh Logically Collective on TS 31409be3e283SDebojyoti Ghosh 31419be3e283SDebojyoti Ghosh Input Parameters: 31429be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 31439be3e283SDebojyoti Ghosh - func - The function 31449be3e283SDebojyoti Ghosh 31459be3e283SDebojyoti Ghosh Calling sequence of func: 314667b8a455SSatish Balay .vb 314767b8a455SSatish Balay PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 314867b8a455SSatish Balay .ve 31499be3e283SDebojyoti Ghosh 31509be3e283SDebojyoti Ghosh Level: intermediate 31519be3e283SDebojyoti Ghosh 31529be3e283SDebojyoti Ghosh Note: 31539be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 315480275a0aSLisandro Dalcin The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being 31559be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 31569be3e283SDebojyoti Ghosh 3157db781477SPatrick Sanan .seealso: `TSSetPreStage()`, `TSSetPreStep()`, `TSSetPostStep()`, `TSGetApplicationContext()` 31589be3e283SDebojyoti Ghosh @*/ 31599be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 31609be3e283SDebojyoti Ghosh { 31619be3e283SDebojyoti Ghosh PetscFunctionBegin; 31629be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 31639be3e283SDebojyoti Ghosh ts->poststage = func; 31649be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 31659be3e283SDebojyoti Ghosh } 31669be3e283SDebojyoti Ghosh 3167c688d042SShri Abhyankar /*@C 3168c688d042SShri Abhyankar TSSetPostEvaluate - Sets the general-purpose function 3169c688d042SShri Abhyankar called once at the end of each step evaluation. 3170c688d042SShri Abhyankar 3171c688d042SShri Abhyankar Logically Collective on TS 3172c688d042SShri Abhyankar 3173c688d042SShri Abhyankar Input Parameters: 3174c688d042SShri Abhyankar + ts - The TS context obtained from TSCreate() 3175c688d042SShri Abhyankar - func - The function 3176c688d042SShri Abhyankar 3177c688d042SShri Abhyankar Calling sequence of func: 317867b8a455SSatish Balay .vb 317967b8a455SSatish Balay PetscErrorCode func(TS ts); 318067b8a455SSatish Balay .ve 3181c688d042SShri Abhyankar 3182c688d042SShri Abhyankar Level: intermediate 3183c688d042SShri Abhyankar 3184c688d042SShri Abhyankar Note: 31851785ff2aSShri Abhyankar Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling 31861785ff2aSShri Abhyankar thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep() 3187e7e94ed4SShri Abhyankar may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step 3188e7e94ed4SShri Abhyankar solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step 3189e7e94ed4SShri Abhyankar with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime() 3190c688d042SShri Abhyankar 3191db781477SPatrick Sanan .seealso: `TSSetPreStage()`, `TSSetPreStep()`, `TSSetPostStep()`, `TSGetApplicationContext()` 3192c688d042SShri Abhyankar @*/ 3193c688d042SShri Abhyankar PetscErrorCode TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS)) 3194c688d042SShri Abhyankar { 3195c688d042SShri Abhyankar PetscFunctionBegin; 3196c688d042SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3197c688d042SShri Abhyankar ts->postevaluate = func; 3198c688d042SShri Abhyankar PetscFunctionReturn(0); 3199c688d042SShri Abhyankar } 3200c688d042SShri Abhyankar 3201b8123daeSJed Brown /*@ 3202b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 3203b8123daeSJed Brown 3204b8123daeSJed Brown Collective on TS 3205b8123daeSJed Brown 3206b8123daeSJed Brown Input Parameters: 3207b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 32089be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 3209b8123daeSJed Brown 3210b8123daeSJed Brown Notes: 3211b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 3212b8123daeSJed Brown most users would not generally call this routine themselves. 3213b8123daeSJed Brown 3214b8123daeSJed Brown Level: developer 3215b8123daeSJed Brown 3216db781477SPatrick Sanan .seealso: `TSPostStage()`, `TSSetPreStep()`, `TSPreStep()`, `TSPostStep()` 3217b8123daeSJed Brown @*/ 3218b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 3219b8123daeSJed Brown { 3220b8123daeSJed Brown PetscFunctionBegin; 3221b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 32221e66621cSBarry Smith if (ts->prestage) PetscCallBack("TS callback prestage",(*ts->prestage)(ts,stagetime)); 3223b8123daeSJed Brown PetscFunctionReturn(0); 3224b8123daeSJed Brown } 3225b8123daeSJed Brown 32269be3e283SDebojyoti Ghosh /*@ 32279be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 32289be3e283SDebojyoti Ghosh 32299be3e283SDebojyoti Ghosh Collective on TS 32309be3e283SDebojyoti Ghosh 32319be3e283SDebojyoti Ghosh Input Parameters: 32329be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 32339be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 32349be3e283SDebojyoti Ghosh stageindex - Stage number 32359be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 32369be3e283SDebojyoti Ghosh of stages) with the stage solutions 32379be3e283SDebojyoti Ghosh 32389be3e283SDebojyoti Ghosh Notes: 32399be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 32409be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 32419be3e283SDebojyoti Ghosh 32429be3e283SDebojyoti Ghosh Level: developer 32439be3e283SDebojyoti Ghosh 3244db781477SPatrick Sanan .seealso: `TSPreStage()`, `TSSetPreStep()`, `TSPreStep()`, `TSPostStep()` 32459be3e283SDebojyoti Ghosh @*/ 32469be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 32479be3e283SDebojyoti Ghosh { 32489be3e283SDebojyoti Ghosh PetscFunctionBegin; 32499be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 32501e66621cSBarry Smith if (ts->poststage) PetscCallBack("TS callback poststage",(*ts->poststage)(ts,stagetime,stageindex,Y)); 32519be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 32529be3e283SDebojyoti Ghosh } 32539be3e283SDebojyoti Ghosh 3254c688d042SShri Abhyankar /*@ 3255c688d042SShri Abhyankar TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate() 3256c688d042SShri Abhyankar 3257c688d042SShri Abhyankar Collective on TS 3258c688d042SShri Abhyankar 3259c688d042SShri Abhyankar Input Parameters: 3260c688d042SShri Abhyankar . ts - The TS context obtained from TSCreate() 3261c688d042SShri Abhyankar 3262c688d042SShri Abhyankar Notes: 3263c688d042SShri Abhyankar TSPostEvaluate() is typically used within time stepping implementations, 3264c688d042SShri Abhyankar most users would not generally call this routine themselves. 3265c688d042SShri Abhyankar 3266c688d042SShri Abhyankar Level: developer 3267c688d042SShri Abhyankar 3268db781477SPatrick Sanan .seealso: `TSSetPostEvaluate()`, `TSSetPreStep()`, `TSPreStep()`, `TSPostStep()` 3269c688d042SShri Abhyankar @*/ 3270c688d042SShri Abhyankar PetscErrorCode TSPostEvaluate(TS ts) 3271c688d042SShri Abhyankar { 3272c688d042SShri Abhyankar PetscFunctionBegin; 3273c688d042SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3274c688d042SShri Abhyankar if (ts->postevaluate) { 3275dcb233daSLisandro Dalcin Vec U; 3276dcb233daSLisandro Dalcin PetscObjectState sprev,spost; 3277dcb233daSLisandro Dalcin 32789566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts,&U)); 32799566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&sprev)); 328025e27a38SBarry Smith PetscCallBack("TS callback postevaluate",(*ts->postevaluate)(ts)); 32819566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&spost)); 32829566063dSJacob Faibussowitsch if (sprev != spost) PetscCall(TSRestartStep(ts)); 3283c688d042SShri Abhyankar } 3284c688d042SShri Abhyankar PetscFunctionReturn(0); 3285c688d042SShri Abhyankar } 3286c688d042SShri Abhyankar 3287ac226902SBarry Smith /*@C 3288000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 32893f2090d5SJed Brown called once at the end of each time step. 3290000e7ae3SMatthew Knepley 32913f9fe445SBarry Smith Logically Collective on TS 3292000e7ae3SMatthew Knepley 3293000e7ae3SMatthew Knepley Input Parameters: 3294000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3295000e7ae3SMatthew Knepley - func - The function 3296000e7ae3SMatthew Knepley 3297000e7ae3SMatthew Knepley Calling sequence of func: 3298b8123daeSJed Brown $ func (TS ts); 3299000e7ae3SMatthew Knepley 33001785ff2aSShri Abhyankar Notes: 33011785ff2aSShri Abhyankar The function set by TSSetPostStep() is called after each successful step. The solution vector X 33021785ff2aSShri Abhyankar obtained by TSGetSolution() may be different than that computed at the step end if the event handler 33031785ff2aSShri Abhyankar locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead. 33041785ff2aSShri Abhyankar 3305000e7ae3SMatthew Knepley Level: intermediate 3306000e7ae3SMatthew Knepley 3307db781477SPatrick Sanan .seealso: `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostEvaluate()`, `TSGetTimeStep()`, `TSGetStepNumber()`, `TSGetTime()`, `TSRestartStep()` 3308000e7ae3SMatthew Knepley @*/ 33097087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 3310000e7ae3SMatthew Knepley { 3311000e7ae3SMatthew Knepley PetscFunctionBegin; 33120700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3313ae60f76fSBarry Smith ts->poststep = func; 3314000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3315000e7ae3SMatthew Knepley } 3316000e7ae3SMatthew Knepley 331709ee8438SJed Brown /*@ 33183f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 33193f2090d5SJed Brown 33203f2090d5SJed Brown Collective on TS 33213f2090d5SJed Brown 33223f2090d5SJed Brown Input Parameters: 33233f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 33243f2090d5SJed Brown 33253f2090d5SJed Brown Notes: 33263f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 33273f2090d5SJed Brown so most users would not generally call this routine themselves. 33283f2090d5SJed Brown 33293f2090d5SJed Brown Level: developer 33303f2090d5SJed Brown 33313f2090d5SJed Brown @*/ 33327087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 33333f2090d5SJed Brown { 33343f2090d5SJed Brown PetscFunctionBegin; 33350700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3336ae60f76fSBarry Smith if (ts->poststep) { 33375efd42a4SStefano Zampini Vec U; 3338ef8d1ce0SJohann Rudi PetscObjectId idprev; 3339ef8d1ce0SJohann Rudi PetscBool sameObject; 33405efd42a4SStefano Zampini PetscObjectState sprev,spost; 33415efd42a4SStefano Zampini 33429566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts,&U)); 33439566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U,&idprev)); 33449566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&sprev)); 334525e27a38SBarry Smith PetscCallBack("TS callback poststep",(*ts->poststep)(ts)); 33469566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts,&U)); 33479566063dSJacob Faibussowitsch PetscCall(PetscObjectCompareId((PetscObject)U,idprev,&sameObject)); 33489566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&spost)); 33499566063dSJacob Faibussowitsch if (!sameObject || sprev != spost) PetscCall(TSRestartStep(ts)); 335072ac3e02SJed Brown } 33513f2090d5SJed Brown PetscFunctionReturn(0); 33523f2090d5SJed Brown } 33533f2090d5SJed Brown 3354cd652676SJed Brown /*@ 3355cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3356cd652676SJed Brown 3357cd652676SJed Brown Collective on TS 3358cd652676SJed Brown 33594165533cSJose E. Roman Input Parameters: 3360cd652676SJed Brown + ts - time stepping context 3361cd652676SJed Brown - t - time to interpolate to 3362cd652676SJed Brown 33634165533cSJose E. Roman Output Parameter: 33640910c330SBarry Smith . U - state at given time 3365cd652676SJed Brown 3366cd652676SJed Brown Level: intermediate 3367cd652676SJed Brown 3368cd652676SJed Brown Developer Notes: 3369cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3370cd652676SJed Brown 3371db781477SPatrick Sanan .seealso: `TSSetExactFinalTime()`, `TSSolve()` 3372cd652676SJed Brown @*/ 33730910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3374cd652676SJed Brown { 3375cd652676SJed Brown PetscFunctionBegin; 3376cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3377b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 337863a3b9bcSJacob 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); 33793c633725SBarry Smith PetscCheck(ts->ops->interpolate,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 33809566063dSJacob Faibussowitsch PetscCall((*ts->ops->interpolate)(ts,t,U)); 3381cd652676SJed Brown PetscFunctionReturn(0); 3382cd652676SJed Brown } 3383cd652676SJed Brown 3384d763cef2SBarry Smith /*@ 33856d9e5789SSean Farley TSStep - Steps one time step 3386d763cef2SBarry Smith 3387d763cef2SBarry Smith Collective on TS 3388d763cef2SBarry Smith 3389d763cef2SBarry Smith Input Parameter: 3390d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3391d763cef2SBarry Smith 339227829d71SBarry Smith Level: developer 3393d763cef2SBarry Smith 3394b8123daeSJed Brown Notes: 339527829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 339627829d71SBarry Smith 3397b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3398b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3399b8123daeSJed Brown 340019eac22cSLisandro Dalcin This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the 340119eac22cSLisandro Dalcin time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 340225cb2221SBarry Smith 3403db781477SPatrick Sanan .seealso: `TSCreate()`, `TSSetUp()`, `TSDestroy()`, `TSSolve()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostStage()`, `TSInterpolate()` 3404d763cef2SBarry Smith @*/ 3405193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3406d763cef2SBarry Smith { 3407fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3408be5899b3SLisandro Dalcin PetscReal ptime; 3409d763cef2SBarry Smith 3410d763cef2SBarry Smith PetscFunctionBegin; 34110700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3412d0609cedSBarry Smith PetscCall(PetscCitationsRegister("@article{tspaper,\n" 3413fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3414f1d62c27SHong Zhang " author = {Abhyankar, Shrirang and Brown, Jed and Constantinescu, Emil and Ghosh, Debojyoti and Smith, Barry F. and Zhang, Hong},\n" 3415f1d62c27SHong Zhang " journal = {arXiv e-preprints},\n" 3416f1d62c27SHong Zhang " eprint = {1806.01437},\n" 3417f1d62c27SHong Zhang " archivePrefix = {arXiv},\n" 3418d0609cedSBarry Smith " year = {2018}\n}\n",&cite)); 34199566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts)); 34209566063dSJacob Faibussowitsch PetscCall(TSTrajectorySetUp(ts->trajectory,ts)); 3421d405a339SMatthew Knepley 34223c633725SBarry Smith PetscCheck(ts->ops->step,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 34233c633725SBarry 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>"); 34243c633725SBarry 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()"); 34253c633725SBarry 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"); 3426a6772fa2SLisandro Dalcin 3427be5899b3SLisandro Dalcin if (!ts->steps) ts->ptime_prev = ts->ptime; 3428be5899b3SLisandro Dalcin ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev; 34292808aa04SLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3430fc8dbba5SLisandro Dalcin 34319566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_Step,ts,0,0,0)); 34329566063dSJacob Faibussowitsch PetscCall((*ts->ops->step)(ts)); 34339566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_Step,ts,0,0,0)); 3434fc8dbba5SLisandro Dalcin 3435fc8dbba5SLisandro Dalcin if (ts->reason >= 0) { 3436be5899b3SLisandro Dalcin ts->ptime_prev = ptime; 34372808aa04SLisandro Dalcin ts->steps++; 3438be5899b3SLisandro Dalcin ts->steprollback = PETSC_FALSE; 343928d5b5d6SLisandro Dalcin ts->steprestart = PETSC_FALSE; 3440e1db57b0SHong Zhang if (ts->tspan && PetscIsCloseAtTol(ts->ptime,ts->tspan->span_times[ts->tspan->spanctr],ts->tspan->reltol*ts->time_step+ts->tspan->abstol,0) && ts->tspan->spanctr < ts->tspan->num_span_times) PetscCall(VecCopy(ts->vec_sol,ts->tspan->vecs_sol[ts->tspan->spanctr++])); 3441d2daff3dSHong Zhang } 3442fc8dbba5SLisandro Dalcin 3443fc8dbba5SLisandro Dalcin if (!ts->reason) { 344408c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 344508c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 344608c7845fSBarry Smith } 3447fc8dbba5SLisandro Dalcin 34483c633725SBarry 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]); 34493c633725SBarry Smith PetscCheck(ts->reason >= 0 || !ts->errorifstepfailed,PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 345008c7845fSBarry Smith PetscFunctionReturn(0); 345108c7845fSBarry Smith } 345208c7845fSBarry Smith 345308c7845fSBarry Smith /*@ 34547cbde773SLisandro Dalcin TSEvaluateWLTE - Evaluate the weighted local truncation error norm 34557cbde773SLisandro Dalcin at the end of a time step with a given order of accuracy. 34567cbde773SLisandro Dalcin 34577cbde773SLisandro Dalcin Collective on TS 34587cbde773SLisandro Dalcin 34594165533cSJose E. Roman Input Parameters: 34607cbde773SLisandro Dalcin + ts - time stepping context 346197bb3fdcSJose E. Roman - wnormtype - norm type, either NORM_2 or NORM_INFINITY 34627cbde773SLisandro Dalcin 346397bb3fdcSJose E. Roman Input/Output Parameter: 346497bb3fdcSJose E. Roman . order - optional, desired order for the error evaluation or PETSC_DECIDE; 346597bb3fdcSJose E. Roman on output, the actual order of the error evaluation 346697bb3fdcSJose E. Roman 346797bb3fdcSJose E. Roman Output Parameter: 346897bb3fdcSJose E. Roman . wlte - the weighted local truncation error norm 34697cbde773SLisandro Dalcin 34707cbde773SLisandro Dalcin Level: advanced 34717cbde773SLisandro Dalcin 34727cbde773SLisandro Dalcin Notes: 34737cbde773SLisandro Dalcin If the timestepper cannot evaluate the error in a particular step 34747cbde773SLisandro Dalcin (eg. in the first step or restart steps after event handling), 34757cbde773SLisandro Dalcin this routine returns wlte=-1.0 . 34767cbde773SLisandro Dalcin 3477db781477SPatrick Sanan .seealso: `TSStep()`, `TSAdapt`, `TSErrorWeightedNorm()` 34787cbde773SLisandro Dalcin @*/ 34797cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte) 34807cbde773SLisandro Dalcin { 34817cbde773SLisandro Dalcin PetscFunctionBegin; 34827cbde773SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 34837cbde773SLisandro Dalcin PetscValidType(ts,1); 3484064a246eSJacob Faibussowitsch PetscValidLogicalCollectiveEnum(ts,wnormtype,2); 34857cbde773SLisandro Dalcin if (order) PetscValidIntPointer(order,3); 34867cbde773SLisandro Dalcin if (order) PetscValidLogicalCollectiveInt(ts,*order,3); 34877cbde773SLisandro Dalcin PetscValidRealPointer(wlte,4); 34883c633725SBarry Smith PetscCheck(wnormtype == NORM_2 || wnormtype == NORM_INFINITY,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 34893c633725SBarry Smith PetscCheck(ts->ops->evaluatewlte,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name); 34909566063dSJacob Faibussowitsch PetscCall((*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte)); 34917cbde773SLisandro Dalcin PetscFunctionReturn(0); 34927cbde773SLisandro Dalcin } 34937cbde773SLisandro Dalcin 349405175c85SJed Brown /*@ 349505175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 349605175c85SJed Brown 34971c3436cfSJed Brown Collective on TS 349805175c85SJed Brown 34994165533cSJose E. Roman Input Parameters: 35001c3436cfSJed Brown + ts - time stepping context 35011c3436cfSJed Brown . order - desired order of accuracy 35020298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 350305175c85SJed Brown 35044165533cSJose E. Roman Output Parameter: 35050910c330SBarry Smith . U - state at the end of the current step 350605175c85SJed Brown 350705175c85SJed Brown Level: advanced 350805175c85SJed Brown 3509108c343cSJed Brown Notes: 3510108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3511108c343cSJed 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. 3512108c343cSJed Brown 3513db781477SPatrick Sanan .seealso: `TSStep()`, `TSAdapt` 351405175c85SJed Brown @*/ 35150910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 351605175c85SJed Brown { 351705175c85SJed Brown PetscFunctionBegin; 351805175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 351905175c85SJed Brown PetscValidType(ts,1); 35200910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 35213c633725SBarry Smith PetscCheck(ts->ops->evaluatestep,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 35229566063dSJacob Faibussowitsch PetscCall((*ts->ops->evaluatestep)(ts,order,U,done)); 352305175c85SJed Brown PetscFunctionReturn(0); 352405175c85SJed Brown } 352505175c85SJed Brown 3526aad739acSMatthew G. Knepley /*@C 35272e61be88SMatthew G. Knepley TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping. 3528aad739acSMatthew G. Knepley 3529aad739acSMatthew G. Knepley Not collective 3530aad739acSMatthew G. Knepley 35314165533cSJose E. Roman Input Parameter: 3532aad739acSMatthew G. Knepley . ts - time stepping context 3533aad739acSMatthew G. Knepley 35344165533cSJose E. Roman Output Parameter: 35352e61be88SMatthew G. Knepley . initConditions - The function which computes an initial condition 3536aad739acSMatthew G. Knepley 3537aad739acSMatthew G. Knepley Level: advanced 3538aad739acSMatthew G. Knepley 3539aad739acSMatthew G. Knepley Notes: 3540aad739acSMatthew G. Knepley The calling sequence for the function is 35412e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u) 3542aad739acSMatthew G. Knepley $ ts - The timestepping context 35432e61be88SMatthew G. Knepley $ u - The input vector in which the initial condition is stored 3544aad739acSMatthew G. Knepley 3545db781477SPatrick Sanan .seealso: `TSSetComputeInitialCondition()`, `TSComputeInitialCondition()` 3546aad739acSMatthew G. Knepley @*/ 35472e61be88SMatthew G. Knepley PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS, Vec)) 3548aad739acSMatthew G. Knepley { 3549aad739acSMatthew G. Knepley PetscFunctionBegin; 3550aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 35512e61be88SMatthew G. Knepley PetscValidPointer(initCondition, 2); 35522e61be88SMatthew G. Knepley *initCondition = ts->ops->initcondition; 3553aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3554aad739acSMatthew G. Knepley } 3555aad739acSMatthew G. Knepley 3556aad739acSMatthew G. Knepley /*@C 35572e61be88SMatthew G. Knepley TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping. 3558aad739acSMatthew G. Knepley 3559aad739acSMatthew G. Knepley Logically collective on ts 3560aad739acSMatthew G. Knepley 35614165533cSJose E. Roman Input Parameters: 3562aad739acSMatthew G. Knepley + ts - time stepping context 35632e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition 3564aad739acSMatthew G. Knepley 3565aad739acSMatthew G. Knepley Level: advanced 3566aad739acSMatthew G. Knepley 3567a96d6ef6SBarry Smith Calling sequence for initCondition: 3568a96d6ef6SBarry Smith $ PetscErrorCode initCondition(TS ts, Vec u) 3569a96d6ef6SBarry Smith 3570a96d6ef6SBarry Smith + ts - The timestepping context 3571a96d6ef6SBarry Smith - u - The input vector in which the initial condition is to be stored 3572aad739acSMatthew G. Knepley 3573db781477SPatrick Sanan .seealso: `TSGetComputeInitialCondition()`, `TSComputeInitialCondition()` 3574aad739acSMatthew G. Knepley @*/ 35752e61be88SMatthew G. Knepley PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS, Vec)) 3576aad739acSMatthew G. Knepley { 3577aad739acSMatthew G. Knepley PetscFunctionBegin; 3578aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 35792e61be88SMatthew G. Knepley PetscValidFunction(initCondition, 2); 35802e61be88SMatthew G. Knepley ts->ops->initcondition = initCondition; 3581aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3582aad739acSMatthew G. Knepley } 3583aad739acSMatthew G. Knepley 3584aad739acSMatthew G. Knepley /*@ 35852e61be88SMatthew G. Knepley TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set. 3586aad739acSMatthew G. Knepley 3587aad739acSMatthew G. Knepley Collective on ts 3588aad739acSMatthew G. Knepley 35894165533cSJose E. Roman Input Parameters: 3590aad739acSMatthew G. Knepley + ts - time stepping context 35912e61be88SMatthew G. Knepley - u - The Vec to store the condition in which will be used in TSSolve() 3592aad739acSMatthew G. Knepley 3593aad739acSMatthew G. Knepley Level: advanced 3594aad739acSMatthew G. Knepley 3595db781477SPatrick Sanan .seealso: `TSGetComputeInitialCondition()`, `TSSetComputeInitialCondition()`, `TSSolve()` 3596aad739acSMatthew G. Knepley @*/ 35972e61be88SMatthew G. Knepley PetscErrorCode TSComputeInitialCondition(TS ts, Vec u) 3598aad739acSMatthew G. Knepley { 3599aad739acSMatthew G. Knepley PetscFunctionBegin; 3600aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3601aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 36029566063dSJacob Faibussowitsch if (ts->ops->initcondition) PetscCall((*ts->ops->initcondition)(ts, u)); 3603aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3604aad739acSMatthew G. Knepley } 3605aad739acSMatthew G. Knepley 3606aad739acSMatthew G. Knepley /*@C 3607aad739acSMatthew G. Knepley TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping. 3608aad739acSMatthew G. Knepley 3609aad739acSMatthew G. Knepley Not collective 3610aad739acSMatthew G. Knepley 36114165533cSJose E. Roman Input Parameter: 3612aad739acSMatthew G. Knepley . ts - time stepping context 3613aad739acSMatthew G. Knepley 36144165533cSJose E. Roman Output Parameter: 3615aad739acSMatthew G. Knepley . exactError - The function which computes the solution error 3616aad739acSMatthew G. Knepley 3617aad739acSMatthew G. Knepley Level: advanced 3618aad739acSMatthew G. Knepley 3619a96d6ef6SBarry Smith Calling sequence for exactError: 3620a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u) 3621a96d6ef6SBarry Smith 3622a96d6ef6SBarry Smith + ts - The timestepping context 3623a96d6ef6SBarry Smith . u - The approximate solution vector 3624a96d6ef6SBarry Smith - e - The input vector in which the error is stored 3625aad739acSMatthew G. Knepley 3626db781477SPatrick Sanan .seealso: `TSGetComputeExactError()`, `TSComputeExactError()` 3627aad739acSMatthew G. Knepley @*/ 3628aad739acSMatthew G. Knepley PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS, Vec, Vec)) 3629aad739acSMatthew G. Knepley { 3630aad739acSMatthew G. Knepley PetscFunctionBegin; 3631aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3632aad739acSMatthew G. Knepley PetscValidPointer(exactError, 2); 3633aad739acSMatthew G. Knepley *exactError = ts->ops->exacterror; 3634aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3635aad739acSMatthew G. Knepley } 3636aad739acSMatthew G. Knepley 3637aad739acSMatthew G. Knepley /*@C 3638aad739acSMatthew G. Knepley TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping. 3639aad739acSMatthew G. Knepley 3640aad739acSMatthew G. Knepley Logically collective on ts 3641aad739acSMatthew G. Knepley 36424165533cSJose E. Roman Input Parameters: 3643aad739acSMatthew G. Knepley + ts - time stepping context 3644aad739acSMatthew G. Knepley - exactError - The function which computes the solution error 3645aad739acSMatthew G. Knepley 3646aad739acSMatthew G. Knepley Level: advanced 3647aad739acSMatthew G. Knepley 3648a96d6ef6SBarry Smith Calling sequence for exactError: 3649a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u) 3650a96d6ef6SBarry Smith 3651a96d6ef6SBarry Smith + ts - The timestepping context 3652a96d6ef6SBarry Smith . u - The approximate solution vector 3653a96d6ef6SBarry Smith - e - The input vector in which the error is stored 3654aad739acSMatthew G. Knepley 3655db781477SPatrick Sanan .seealso: `TSGetComputeExactError()`, `TSComputeExactError()` 3656aad739acSMatthew G. Knepley @*/ 3657aad739acSMatthew G. Knepley PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS, Vec, Vec)) 3658aad739acSMatthew G. Knepley { 3659aad739acSMatthew G. Knepley PetscFunctionBegin; 3660aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3661f907fdbfSMatthew G. Knepley PetscValidFunction(exactError, 2); 3662aad739acSMatthew G. Knepley ts->ops->exacterror = exactError; 3663aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3664aad739acSMatthew G. Knepley } 3665aad739acSMatthew G. Knepley 3666aad739acSMatthew G. Knepley /*@ 3667aad739acSMatthew G. Knepley TSComputeExactError - Compute the solution error for the timestepping using the function previously set. 3668aad739acSMatthew G. Knepley 3669aad739acSMatthew G. Knepley Collective on ts 3670aad739acSMatthew G. Knepley 36714165533cSJose E. Roman Input Parameters: 3672aad739acSMatthew G. Knepley + ts - time stepping context 3673aad739acSMatthew G. Knepley . u - The approximate solution 3674aad739acSMatthew G. Knepley - e - The Vec used to store the error 3675aad739acSMatthew G. Knepley 3676aad739acSMatthew G. Knepley Level: advanced 3677aad739acSMatthew G. Knepley 3678db781477SPatrick Sanan .seealso: `TSGetComputeInitialCondition()`, `TSSetComputeInitialCondition()`, `TSSolve()` 3679aad739acSMatthew G. Knepley @*/ 3680aad739acSMatthew G. Knepley PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e) 3681aad739acSMatthew G. Knepley { 3682aad739acSMatthew G. Knepley PetscFunctionBegin; 3683aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3684aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3685aad739acSMatthew G. Knepley PetscValidHeaderSpecific(e, VEC_CLASSID, 3); 36869566063dSJacob Faibussowitsch if (ts->ops->exacterror) PetscCall((*ts->ops->exacterror)(ts, u, e)); 3687aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3688aad739acSMatthew G. Knepley } 3689aad739acSMatthew G. Knepley 3690b1cb36f3SHong Zhang /*@ 36916a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 36926a4d4014SLisandro Dalcin 36936a4d4014SLisandro Dalcin Collective on TS 36946a4d4014SLisandro Dalcin 3695d8d19677SJose E. Roman Input Parameters: 36966a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 369763e21af5SBarry Smith - u - the solution vector (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used, 369863e21af5SBarry Smith otherwise must contain the initial conditions and will contain the solution at the final requested time 36995a3a76d0SJed Brown 37006a4d4014SLisandro Dalcin Level: beginner 37016a4d4014SLisandro Dalcin 37025a3a76d0SJed Brown Notes: 37035a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 37045a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 37055a3a76d0SJed Brown stepped over the final time. 37065a3a76d0SJed Brown 3707db781477SPatrick Sanan .seealso: `TSCreate()`, `TSSetSolution()`, `TSStep()`, `TSGetTime()`, `TSGetSolveTime()` 37086a4d4014SLisandro Dalcin @*/ 3709cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 37106a4d4014SLisandro Dalcin { 3711b06615a5SLisandro Dalcin Vec solution; 3712f22f69f0SBarry Smith 37136a4d4014SLisandro Dalcin PetscFunctionBegin; 37140700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3715f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 3716303a5415SBarry Smith 37179566063dSJacob Faibussowitsch PetscCall(TSSetExactFinalTimeDefault(ts)); 3718ee41a567SStefano 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 */ 37190910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 37209566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u,&solution)); 37219566063dSJacob Faibussowitsch PetscCall(TSSetSolution(ts,solution)); 37229566063dSJacob Faibussowitsch PetscCall(VecDestroy(&solution)); /* grant ownership */ 37235a3a76d0SJed Brown } 37249566063dSJacob Faibussowitsch PetscCall(VecCopy(u,ts->vec_sol)); 37253c633725SBarry Smith PetscCheck(!ts->forward_solve,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE"); 37261baa6e33SBarry Smith } else if (u) PetscCall(TSSetSolution(ts,u)); 37279566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts)); 37289566063dSJacob Faibussowitsch PetscCall(TSTrajectorySetUp(ts->trajectory,ts)); 3729a6772fa2SLisandro Dalcin 37303c633725SBarry 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>"); 37313c633725SBarry 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()"); 37323c633725SBarry 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"); 37334a658b32SHong 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"); 37344a658b32SHong Zhang 3735e1db57b0SHong Zhang if (ts->tspan && PetscIsCloseAtTol(ts->ptime,ts->tspan->span_times[0],ts->tspan->reltol*ts->time_step+ts->tspan->abstol,0)) { /* starting point in time span */ 37364a658b32SHong Zhang PetscCall(VecCopy(ts->vec_sol,ts->tspan->vecs_sol[0])); 37374a658b32SHong Zhang ts->tspan->spanctr = 1; 37384a658b32SHong Zhang } 3739a6772fa2SLisandro Dalcin 37401baa6e33SBarry Smith if (ts->forward_solve) PetscCall(TSForwardSetUp(ts)); 3741715f1b00SHong Zhang 3742e7069c78SShri /* reset number of steps only when the step is not restarted. ARKIMEX 3743715f1b00SHong Zhang restarts the step after an event. Resetting these counters in such case causes 3744e7069c78SShri TSTrajectory to incorrectly save the output files 3745e7069c78SShri */ 3746715f1b00SHong Zhang /* reset time step and iteration counters */ 37472808aa04SLisandro Dalcin if (!ts->steps) { 37485ef26d82SJed Brown ts->ksp_its = 0; 37495ef26d82SJed Brown ts->snes_its = 0; 3750c610991cSLisandro Dalcin ts->num_snes_failures = 0; 3751c610991cSLisandro Dalcin ts->reject = 0; 37522808aa04SLisandro Dalcin ts->steprestart = PETSC_TRUE; 37532808aa04SLisandro Dalcin ts->steprollback = PETSC_FALSE; 37547d51462cSStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 37552808aa04SLisandro Dalcin } 3756e97c63d7SStefano Zampini 37574a658b32SHong Zhang /* make sure initial time step does not overshoot final time or the next point in tspan */ 3758e97c63d7SStefano Zampini if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) { 37594a658b32SHong Zhang PetscReal maxdt; 3760e97c63d7SStefano Zampini PetscReal dt = ts->time_step; 3761e97c63d7SStefano Zampini 37624a658b32SHong Zhang if (ts->tspan) maxdt = ts->tspan->span_times[ts->tspan->spanctr] - ts->ptime; 37634a658b32SHong Zhang else maxdt = ts->max_time - ts->ptime; 3764e97c63d7SStefano Zampini ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt,maxdt,10*PETSC_MACHINE_EPSILON,0) ? maxdt : dt); 3765e97c63d7SStefano Zampini } 3766193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 3767193ac0bcSJed Brown 3768900f6b5bSMatthew G. Knepley { 3769900f6b5bSMatthew G. Knepley PetscViewer viewer; 3770900f6b5bSMatthew G. Knepley PetscViewerFormat format; 3771900f6b5bSMatthew G. Knepley PetscBool flg; 3772900f6b5bSMatthew G. Knepley static PetscBool incall = PETSC_FALSE; 3773900f6b5bSMatthew G. Knepley 3774900f6b5bSMatthew G. Knepley if (!incall) { 3775900f6b5bSMatthew G. Knepley /* Estimate the convergence rate of the time discretization */ 37769566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject) ts),((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg)); 3777900f6b5bSMatthew G. Knepley if (flg) { 3778900f6b5bSMatthew G. Knepley PetscConvEst conv; 3779900f6b5bSMatthew G. Knepley DM dm; 3780900f6b5bSMatthew G. Knepley PetscReal *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */ 3781900f6b5bSMatthew G. Knepley PetscInt Nf; 3782f2ed2dc7SMatthew G. Knepley PetscBool checkTemporal = PETSC_TRUE; 3783900f6b5bSMatthew G. Knepley 3784900f6b5bSMatthew G. Knepley incall = PETSC_TRUE; 37859566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_temporal", &checkTemporal, &flg)); 37869566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 37879566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &Nf)); 37889566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(PetscMax(Nf, 1), &alpha)); 37899566063dSJacob Faibussowitsch PetscCall(PetscConvEstCreate(PetscObjectComm((PetscObject) ts), &conv)); 37909566063dSJacob Faibussowitsch PetscCall(PetscConvEstUseTS(conv, checkTemporal)); 37919566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetSolver(conv, (PetscObject) ts)); 37929566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetFromOptions(conv)); 37939566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetUp(conv)); 37949566063dSJacob Faibussowitsch PetscCall(PetscConvEstGetConvRate(conv, alpha)); 37959566063dSJacob Faibussowitsch PetscCall(PetscViewerPushFormat(viewer, format)); 37969566063dSJacob Faibussowitsch PetscCall(PetscConvEstRateView(conv, alpha, viewer)); 37979566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(viewer)); 37989566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&viewer)); 37999566063dSJacob Faibussowitsch PetscCall(PetscConvEstDestroy(&conv)); 38009566063dSJacob Faibussowitsch PetscCall(PetscFree(alpha)); 3801900f6b5bSMatthew G. Knepley incall = PETSC_FALSE; 3802900f6b5bSMatthew G. Knepley } 3803900f6b5bSMatthew G. Knepley } 3804900f6b5bSMatthew G. Knepley } 3805900f6b5bSMatthew G. Knepley 38069566063dSJacob Faibussowitsch PetscCall(TSViewFromOptions(ts,NULL,"-ts_view_pre")); 3807f05ece33SBarry Smith 3808193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 38099566063dSJacob Faibussowitsch PetscCall((*ts->ops->solve)(ts)); 38109566063dSJacob Faibussowitsch if (u) PetscCall(VecCopy(ts->vec_sol,u)); 3811cc708dedSBarry Smith ts->solvetime = ts->ptime; 3812a6772fa2SLisandro Dalcin solution = ts->vec_sol; 3813be5899b3SLisandro Dalcin } else { /* Step the requested number of timesteps. */ 3814db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3815db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3816e7069c78SShri 38172808aa04SLisandro Dalcin if (!ts->steps) { 38189566063dSJacob Faibussowitsch PetscCall(TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol)); 38199566063dSJacob Faibussowitsch PetscCall(TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol)); 38202808aa04SLisandro Dalcin } 38216427ac75SLisandro Dalcin 3822e1a7a14fSJed Brown while (!ts->reason) { 38239566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol)); 38241e66621cSBarry Smith if (!ts->steprollback) PetscCall(TSPreStep(ts)); 38259566063dSJacob Faibussowitsch PetscCall(TSStep(ts)); 38261baa6e33SBarry Smith if (ts->testjacobian) PetscCall(TSRHSJacobianTest(ts,NULL)); 38271baa6e33SBarry Smith if (ts->testjacobiantranspose) PetscCall(TSRHSJacobianTestTranspose(ts,NULL)); 3828cd4cee2dSHong Zhang if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */ 38297b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */ 38309566063dSJacob Faibussowitsch PetscCall(TSForwardCostIntegral(ts)); 38317b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps++; 3832b1cb36f3SHong Zhang } 383358818c2dSLisandro Dalcin if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */ 38347b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */ 38359566063dSJacob Faibussowitsch PetscCall(TSForwardStep(ts)); 38367b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps++; 3837715f1b00SHong Zhang } 38389566063dSJacob Faibussowitsch PetscCall(TSPostEvaluate(ts)); 38399566063dSJacob 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. */ 38401baa6e33SBarry Smith if (ts->steprollback) PetscCall(TSPostEvaluate(ts)); 3841e783b05fSHong Zhang if (!ts->steprollback) { 38429566063dSJacob Faibussowitsch PetscCall(TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol)); 38439566063dSJacob Faibussowitsch PetscCall(TSPostStep(ts)); 3844aeb4809dSShri Abhyankar } 3845193ac0bcSJed Brown } 38469566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol)); 38476427ac75SLisandro Dalcin 384849354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 38499566063dSJacob Faibussowitsch PetscCall(TSInterpolate(ts,ts->max_time,u)); 3850cc708dedSBarry Smith ts->solvetime = ts->max_time; 3851b06615a5SLisandro Dalcin solution = u; 38529566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts,-1,ts->solvetime,solution)); 38530574a7fbSJed Brown } else { 38549566063dSJacob Faibussowitsch if (u) PetscCall(VecCopy(ts->vec_sol,u)); 3855cc708dedSBarry Smith ts->solvetime = ts->ptime; 3856b06615a5SLisandro Dalcin solution = ts->vec_sol; 38570574a7fbSJed Brown } 3858193ac0bcSJed Brown } 3859d2daff3dSHong Zhang 38609566063dSJacob Faibussowitsch PetscCall(TSViewFromOptions(ts,NULL,"-ts_view")); 38619566063dSJacob Faibussowitsch PetscCall(VecViewFromOptions(solution,(PetscObject)ts,"-ts_view_solution")); 38629566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsBlock((PetscObject)ts)); 38631baa6e33SBarry Smith if (ts->adjoint_solve) PetscCall(TSAdjointSolve(ts)); 38646a4d4014SLisandro Dalcin PetscFunctionReturn(0); 38656a4d4014SLisandro Dalcin } 38666a4d4014SLisandro Dalcin 3867d763cef2SBarry Smith /*@ 3868b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 3869d763cef2SBarry Smith 3870d763cef2SBarry Smith Not Collective 3871d763cef2SBarry Smith 3872d763cef2SBarry Smith Input Parameter: 3873d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3874d763cef2SBarry Smith 3875d763cef2SBarry Smith Output Parameter: 387619eac22cSLisandro Dalcin . t - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime(). 3877d763cef2SBarry Smith 3878d763cef2SBarry Smith Level: beginner 3879d763cef2SBarry Smith 3880b8123daeSJed Brown Note: 3881b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 38829be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 3883b8123daeSJed Brown 3884db781477SPatrick Sanan .seealso: `TSGetSolveTime()`, `TSSetTime()`, `TSGetTimeStep()`, `TSGetStepNumber()` 3885d763cef2SBarry Smith 3886d763cef2SBarry Smith @*/ 38877087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 3888d763cef2SBarry Smith { 3889d763cef2SBarry Smith PetscFunctionBegin; 38900700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3891f7cf8827SBarry Smith PetscValidRealPointer(t,2); 3892d763cef2SBarry Smith *t = ts->ptime; 3893d763cef2SBarry Smith PetscFunctionReturn(0); 3894d763cef2SBarry Smith } 3895d763cef2SBarry Smith 3896e5e524a1SHong Zhang /*@ 3897e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 3898e5e524a1SHong Zhang 3899e5e524a1SHong Zhang Not Collective 3900e5e524a1SHong Zhang 3901e5e524a1SHong Zhang Input Parameter: 3902e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 3903e5e524a1SHong Zhang 3904e5e524a1SHong Zhang Output Parameter: 3905e5e524a1SHong Zhang . t - the previous time 3906e5e524a1SHong Zhang 3907e5e524a1SHong Zhang Level: beginner 3908e5e524a1SHong Zhang 3909db781477SPatrick Sanan .seealso: `TSGetTime()`, `TSGetSolveTime()`, `TSGetTimeStep()` 3910e5e524a1SHong Zhang 3911e5e524a1SHong Zhang @*/ 3912e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 3913e5e524a1SHong Zhang { 3914e5e524a1SHong Zhang PetscFunctionBegin; 3915e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3916e5e524a1SHong Zhang PetscValidRealPointer(t,2); 3917e5e524a1SHong Zhang *t = ts->ptime_prev; 3918e5e524a1SHong Zhang PetscFunctionReturn(0); 3919e5e524a1SHong Zhang } 3920e5e524a1SHong Zhang 39216a4d4014SLisandro Dalcin /*@ 39226a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 39236a4d4014SLisandro Dalcin 39243f9fe445SBarry Smith Logically Collective on TS 39256a4d4014SLisandro Dalcin 39266a4d4014SLisandro Dalcin Input Parameters: 39276a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 39286a4d4014SLisandro Dalcin - time - the time 39296a4d4014SLisandro Dalcin 39306a4d4014SLisandro Dalcin Level: intermediate 39316a4d4014SLisandro Dalcin 3932db781477SPatrick Sanan .seealso: `TSGetTime()`, `TSSetMaxSteps()` 39336a4d4014SLisandro Dalcin 39346a4d4014SLisandro Dalcin @*/ 39357087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 39366a4d4014SLisandro Dalcin { 39376a4d4014SLisandro Dalcin PetscFunctionBegin; 39380700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3939c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 39406a4d4014SLisandro Dalcin ts->ptime = t; 39416a4d4014SLisandro Dalcin PetscFunctionReturn(0); 39426a4d4014SLisandro Dalcin } 39436a4d4014SLisandro Dalcin 3944d763cef2SBarry Smith /*@C 3945d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 3946d763cef2SBarry Smith TS options in the database. 3947d763cef2SBarry Smith 39483f9fe445SBarry Smith Logically Collective on TS 3949d763cef2SBarry Smith 3950d8d19677SJose E. Roman Input Parameters: 3951d763cef2SBarry Smith + ts - The TS context 3952d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3953d763cef2SBarry Smith 3954d763cef2SBarry Smith Notes: 3955d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3956d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3957d763cef2SBarry Smith hyphen. 3958d763cef2SBarry Smith 3959d763cef2SBarry Smith Level: advanced 3960d763cef2SBarry Smith 3961db781477SPatrick Sanan .seealso: `TSSetFromOptions()` 3962d763cef2SBarry Smith 3963d763cef2SBarry Smith @*/ 39647087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 3965d763cef2SBarry Smith { 3966089b2837SJed Brown SNES snes; 3967d763cef2SBarry Smith 3968d763cef2SBarry Smith PetscFunctionBegin; 39690700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 39709566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)ts,prefix)); 39719566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 39729566063dSJacob Faibussowitsch PetscCall(SNESSetOptionsPrefix(snes,prefix)); 3973d763cef2SBarry Smith PetscFunctionReturn(0); 3974d763cef2SBarry Smith } 3975d763cef2SBarry Smith 3976d763cef2SBarry Smith /*@C 3977d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 3978d763cef2SBarry Smith TS options in the database. 3979d763cef2SBarry Smith 39803f9fe445SBarry Smith Logically Collective on TS 3981d763cef2SBarry Smith 3982d8d19677SJose E. Roman Input Parameters: 3983d763cef2SBarry Smith + ts - The TS context 3984d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3985d763cef2SBarry Smith 3986d763cef2SBarry Smith Notes: 3987d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3988d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3989d763cef2SBarry Smith hyphen. 3990d763cef2SBarry Smith 3991d763cef2SBarry Smith Level: advanced 3992d763cef2SBarry Smith 3993db781477SPatrick Sanan .seealso: `TSGetOptionsPrefix()` 3994d763cef2SBarry Smith 3995d763cef2SBarry Smith @*/ 39967087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 3997d763cef2SBarry Smith { 3998089b2837SJed Brown SNES snes; 3999d763cef2SBarry Smith 4000d763cef2SBarry Smith PetscFunctionBegin; 40010700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 40029566063dSJacob Faibussowitsch PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix)); 40039566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 40049566063dSJacob Faibussowitsch PetscCall(SNESAppendOptionsPrefix(snes,prefix)); 4005d763cef2SBarry Smith PetscFunctionReturn(0); 4006d763cef2SBarry Smith } 4007d763cef2SBarry Smith 4008d763cef2SBarry Smith /*@C 4009d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 4010d763cef2SBarry Smith TS options in the database. 4011d763cef2SBarry Smith 4012d763cef2SBarry Smith Not Collective 4013d763cef2SBarry Smith 4014d763cef2SBarry Smith Input Parameter: 4015d763cef2SBarry Smith . ts - The TS context 4016d763cef2SBarry Smith 4017d763cef2SBarry Smith Output Parameter: 4018d763cef2SBarry Smith . prefix - A pointer to the prefix string used 4019d763cef2SBarry Smith 402095452b02SPatrick Sanan Notes: 402195452b02SPatrick Sanan On the fortran side, the user should pass in a string 'prifix' of 4022d763cef2SBarry Smith sufficient length to hold the prefix. 4023d763cef2SBarry Smith 4024d763cef2SBarry Smith Level: intermediate 4025d763cef2SBarry Smith 4026db781477SPatrick Sanan .seealso: `TSAppendOptionsPrefix()` 4027d763cef2SBarry Smith @*/ 40287087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 4029d763cef2SBarry Smith { 4030d763cef2SBarry Smith PetscFunctionBegin; 40310700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 40324482741eSBarry Smith PetscValidPointer(prefix,2); 40339566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)ts,prefix)); 4034d763cef2SBarry Smith PetscFunctionReturn(0); 4035d763cef2SBarry Smith } 4036d763cef2SBarry Smith 4037d763cef2SBarry Smith /*@C 4038d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4039d763cef2SBarry Smith 4040d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 4041d763cef2SBarry Smith 4042d763cef2SBarry Smith Input Parameter: 4043d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 4044d763cef2SBarry Smith 4045d763cef2SBarry Smith Output Parameters: 4046e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 4047e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 4048e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 4049e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 4050d763cef2SBarry Smith 405195452b02SPatrick Sanan Notes: 405295452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 4053d763cef2SBarry Smith 4054d763cef2SBarry Smith Level: intermediate 4055d763cef2SBarry Smith 4056db781477SPatrick Sanan .seealso: `TSGetTimeStep()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()` 4057d763cef2SBarry Smith 4058d763cef2SBarry Smith @*/ 4059e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 4060d763cef2SBarry Smith { 406124989b8cSPeter Brune DM dm; 4062089b2837SJed Brown 4063d763cef2SBarry Smith PetscFunctionBegin; 406423a57915SBarry Smith if (Amat || Pmat) { 406523a57915SBarry Smith SNES snes; 40669566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 40679566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 40689566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes,Amat,Pmat,NULL,NULL)); 406923a57915SBarry Smith } 40709566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 40719566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm,func,ctx)); 4072d763cef2SBarry Smith PetscFunctionReturn(0); 4073d763cef2SBarry Smith } 4074d763cef2SBarry Smith 40752eca1d9cSJed Brown /*@C 40762eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 40772eca1d9cSJed Brown 40782eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 40792eca1d9cSJed Brown 40802eca1d9cSJed Brown Input Parameter: 40812eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 40822eca1d9cSJed Brown 40832eca1d9cSJed Brown Output Parameters: 4084e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4085e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 40862eca1d9cSJed Brown . f - The function to compute the matrices 40872eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 40882eca1d9cSJed Brown 408995452b02SPatrick Sanan Notes: 409095452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 40912eca1d9cSJed Brown 40922eca1d9cSJed Brown Level: advanced 40932eca1d9cSJed Brown 4094db781477SPatrick Sanan .seealso: `TSGetTimeStep()`, `TSGetRHSJacobian()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()` 40952eca1d9cSJed Brown 40962eca1d9cSJed Brown @*/ 4097e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 40982eca1d9cSJed Brown { 409924989b8cSPeter Brune DM dm; 41000910c330SBarry Smith 41012eca1d9cSJed Brown PetscFunctionBegin; 4102c0aab802Sstefano_zampini if (Amat || Pmat) { 4103c0aab802Sstefano_zampini SNES snes; 41049566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 41059566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 41069566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes,Amat,Pmat,NULL,NULL)); 4107c0aab802Sstefano_zampini } 41089566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 41099566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm,f,ctx)); 41102eca1d9cSJed Brown PetscFunctionReturn(0); 41112eca1d9cSJed Brown } 41122eca1d9cSJed Brown 4113af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 41146c699258SBarry Smith /*@ 41152a808120SBarry Smith TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS 41166c699258SBarry Smith 4117d083f849SBarry Smith Logically Collective on ts 41186c699258SBarry Smith 41196c699258SBarry Smith Input Parameters: 41202a808120SBarry Smith + ts - the ODE integrator object 41212a808120SBarry Smith - dm - the dm, cannot be NULL 41226c699258SBarry Smith 4123e03a659cSJed Brown Notes: 4124e03a659cSJed Brown A DM can only be used for solving one problem at a time because information about the problem is stored on the DM, 4125e03a659cSJed Brown even when not using interfaces like DMTSSetIFunction(). Use DMClone() to get a distinct DM when solving 4126e03a659cSJed Brown different problems using the same function space. 4127e03a659cSJed Brown 41286c699258SBarry Smith Level: intermediate 41296c699258SBarry Smith 4130db781477SPatrick Sanan .seealso: `TSGetDM()`, `SNESSetDM()`, `SNESGetDM()` 41316c699258SBarry Smith @*/ 41327087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 41336c699258SBarry Smith { 4134089b2837SJed Brown SNES snes; 4135942e3340SBarry Smith DMTS tsdm; 41366c699258SBarry Smith 41376c699258SBarry Smith PetscFunctionBegin; 41380700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 41392a808120SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,2); 41409566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)dm)); 4141942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 41422a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 41439566063dSJacob Faibussowitsch PetscCall(DMCopyDMTS(ts->dm,dm)); 41449566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm,&tsdm)); 41451e66621cSBarry Smith /* Grant write privileges to the replacement DM */ 41461e66621cSBarry Smith if (tsdm->originaldm == ts->dm) tsdm->originaldm = dm; 414724989b8cSPeter Brune } 41489566063dSJacob Faibussowitsch PetscCall(DMDestroy(&ts->dm)); 414924989b8cSPeter Brune } 41506c699258SBarry Smith ts->dm = dm; 4151bbd56ea5SKarl Rupp 41529566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 41539566063dSJacob Faibussowitsch PetscCall(SNESSetDM(snes,dm)); 41546c699258SBarry Smith PetscFunctionReturn(0); 41556c699258SBarry Smith } 41566c699258SBarry Smith 41576c699258SBarry Smith /*@ 41586c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 41596c699258SBarry Smith 41603f9fe445SBarry Smith Not Collective 41616c699258SBarry Smith 41626c699258SBarry Smith Input Parameter: 41636c699258SBarry Smith . ts - the preconditioner context 41646c699258SBarry Smith 41656c699258SBarry Smith Output Parameter: 41666c699258SBarry Smith . dm - the dm 41676c699258SBarry Smith 41686c699258SBarry Smith Level: intermediate 41696c699258SBarry Smith 4170db781477SPatrick Sanan .seealso: `TSSetDM()`, `SNESSetDM()`, `SNESGetDM()` 41716c699258SBarry Smith @*/ 41727087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 41736c699258SBarry Smith { 41746c699258SBarry Smith PetscFunctionBegin; 41750700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4176496e6a7aSJed Brown if (!ts->dm) { 41779566063dSJacob Faibussowitsch PetscCall(DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm)); 41789566063dSJacob Faibussowitsch if (ts->snes) PetscCall(SNESSetDM(ts->snes,ts->dm)); 4179496e6a7aSJed Brown } 41806c699258SBarry Smith *dm = ts->dm; 41816c699258SBarry Smith PetscFunctionReturn(0); 41826c699258SBarry Smith } 41831713a123SBarry Smith 41840f5c6efeSJed Brown /*@ 41850f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 41860f5c6efeSJed Brown 41873f9fe445SBarry Smith Logically Collective on SNES 41880f5c6efeSJed Brown 4189d8d19677SJose E. Roman Input Parameters: 4190d42a1c89SJed Brown + snes - nonlinear solver 41910910c330SBarry Smith . U - the current state at which to evaluate the residual 4192d42a1c89SJed Brown - ctx - user context, must be a TS 41930f5c6efeSJed Brown 41940f5c6efeSJed Brown Output Parameter: 41950f5c6efeSJed Brown . F - the nonlinear residual 41960f5c6efeSJed Brown 41970f5c6efeSJed Brown Notes: 41980f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 41990f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 42000f5c6efeSJed Brown 42010f5c6efeSJed Brown Level: advanced 42020f5c6efeSJed Brown 4203db781477SPatrick Sanan .seealso: `SNESSetFunction()`, `MatFDColoringSetFunction()` 42040f5c6efeSJed Brown @*/ 42050910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 42060f5c6efeSJed Brown { 42070f5c6efeSJed Brown TS ts = (TS)ctx; 42080f5c6efeSJed Brown 42090f5c6efeSJed Brown PetscFunctionBegin; 42100f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 42110910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 42120f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 42130f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 42149566063dSJacob Faibussowitsch PetscCall((ts->ops->snesfunction)(snes,U,F,ts)); 42150f5c6efeSJed Brown PetscFunctionReturn(0); 42160f5c6efeSJed Brown } 42170f5c6efeSJed Brown 42180f5c6efeSJed Brown /*@ 42190f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 42200f5c6efeSJed Brown 42210f5c6efeSJed Brown Collective on SNES 42220f5c6efeSJed Brown 4223d8d19677SJose E. Roman Input Parameters: 42240f5c6efeSJed Brown + snes - nonlinear solver 42250910c330SBarry Smith . U - the current state at which to evaluate the residual 42260f5c6efeSJed Brown - ctx - user context, must be a TS 42270f5c6efeSJed Brown 4228d8d19677SJose E. Roman Output Parameters: 42290f5c6efeSJed Brown + A - the Jacobian 42306b867d5aSJose E. Roman - B - the preconditioning matrix (may be the same as A) 42310f5c6efeSJed Brown 42320f5c6efeSJed Brown Notes: 42330f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 42340f5c6efeSJed Brown 42350f5c6efeSJed Brown Level: developer 42360f5c6efeSJed Brown 4237db781477SPatrick Sanan .seealso: `SNESSetJacobian()` 42380f5c6efeSJed Brown @*/ 4239d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 42400f5c6efeSJed Brown { 42410f5c6efeSJed Brown TS ts = (TS)ctx; 42420f5c6efeSJed Brown 42430f5c6efeSJed Brown PetscFunctionBegin; 42440f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 42450910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 42460f5c6efeSJed Brown PetscValidPointer(A,3); 424794ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 42480f5c6efeSJed Brown PetscValidPointer(B,4); 424994ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 4250064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(ts,TS_CLASSID,5); 42519566063dSJacob Faibussowitsch PetscCall((ts->ops->snesjacobian)(snes,U,A,B,ts)); 42520f5c6efeSJed Brown PetscFunctionReturn(0); 42530f5c6efeSJed Brown } 4254325fc9f4SBarry Smith 42550e4ef248SJed Brown /*@C 42569ae8fd06SBarry Smith TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only 42570e4ef248SJed Brown 42580e4ef248SJed Brown Collective on TS 42590e4ef248SJed Brown 42604165533cSJose E. Roman Input Parameters: 42610e4ef248SJed Brown + ts - time stepping context 42620e4ef248SJed Brown . t - time at which to evaluate 42630910c330SBarry Smith . U - state at which to evaluate 42640e4ef248SJed Brown - ctx - context 42650e4ef248SJed Brown 42664165533cSJose E. Roman Output Parameter: 42670e4ef248SJed Brown . F - right hand side 42680e4ef248SJed Brown 42690e4ef248SJed Brown Level: intermediate 42700e4ef248SJed Brown 42710e4ef248SJed Brown Notes: 42720e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 42730e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 42740e4ef248SJed Brown 4275db781477SPatrick Sanan .seealso: `TSSetRHSFunction()`, `TSSetRHSJacobian()`, `TSComputeRHSJacobianConstant()` 42760e4ef248SJed Brown @*/ 42770910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 42780e4ef248SJed Brown { 42790e4ef248SJed Brown Mat Arhs,Brhs; 42800e4ef248SJed Brown 42810e4ef248SJed Brown PetscFunctionBegin; 42829566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts,&Arhs,&Brhs)); 42832663174eSHong Zhang /* undo the damage caused by shifting */ 42849566063dSJacob Faibussowitsch PetscCall(TSRecoverRHSJacobian(ts,Arhs,Brhs)); 42859566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts,t,U,Arhs,Brhs)); 42869566063dSJacob Faibussowitsch PetscCall(MatMult(Arhs,U,F)); 42870e4ef248SJed Brown PetscFunctionReturn(0); 42880e4ef248SJed Brown } 42890e4ef248SJed Brown 42900e4ef248SJed Brown /*@C 42910e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 42920e4ef248SJed Brown 42930e4ef248SJed Brown Collective on TS 42940e4ef248SJed Brown 42954165533cSJose E. Roman Input Parameters: 42960e4ef248SJed Brown + ts - time stepping context 42970e4ef248SJed Brown . t - time at which to evaluate 42980910c330SBarry Smith . U - state at which to evaluate 42990e4ef248SJed Brown - ctx - context 43000e4ef248SJed Brown 43014165533cSJose E. Roman Output Parameters: 43020e4ef248SJed Brown + A - pointer to operator 430397bb3fdcSJose E. Roman - B - pointer to preconditioning matrix 43040e4ef248SJed Brown 43050e4ef248SJed Brown Level: intermediate 43060e4ef248SJed Brown 43070e4ef248SJed Brown Notes: 43080e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 43090e4ef248SJed Brown 4310db781477SPatrick Sanan .seealso: `TSSetRHSFunction()`, `TSSetRHSJacobian()`, `TSComputeRHSFunctionLinear()` 43110e4ef248SJed Brown @*/ 4312d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 43130e4ef248SJed Brown { 43140e4ef248SJed Brown PetscFunctionBegin; 43150e4ef248SJed Brown PetscFunctionReturn(0); 43160e4ef248SJed Brown } 43170e4ef248SJed Brown 43180026cea9SSean Farley /*@C 43190026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 43200026cea9SSean Farley 43210026cea9SSean Farley Collective on TS 43220026cea9SSean Farley 43234165533cSJose E. Roman Input Parameters: 43240026cea9SSean Farley + ts - time stepping context 43250026cea9SSean Farley . t - time at which to evaluate 43260910c330SBarry Smith . U - state at which to evaluate 43270910c330SBarry Smith . Udot - time derivative of state vector 43280026cea9SSean Farley - ctx - context 43290026cea9SSean Farley 43304165533cSJose E. Roman Output Parameter: 43310026cea9SSean Farley . F - left hand side 43320026cea9SSean Farley 43330026cea9SSean Farley Level: intermediate 43340026cea9SSean Farley 43350026cea9SSean Farley Notes: 43360910c330SBarry 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 43370026cea9SSean Farley user is required to write their own TSComputeIFunction. 43380026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 43390026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 43400026cea9SSean Farley 43419ae8fd06SBarry Smith Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U 43429ae8fd06SBarry Smith 4343db781477SPatrick Sanan .seealso: `TSSetIFunction()`, `TSSetIJacobian()`, `TSComputeIJacobianConstant()`, `TSComputeRHSFunctionLinear()` 43440026cea9SSean Farley @*/ 43450910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 43460026cea9SSean Farley { 43470026cea9SSean Farley Mat A,B; 43480026cea9SSean Farley 43490026cea9SSean Farley PetscFunctionBegin; 43509566063dSJacob Faibussowitsch PetscCall(TSGetIJacobian(ts,&A,&B,NULL,NULL)); 43519566063dSJacob Faibussowitsch PetscCall(TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE)); 43529566063dSJacob Faibussowitsch PetscCall(MatMult(A,Udot,F)); 43530026cea9SSean Farley PetscFunctionReturn(0); 43540026cea9SSean Farley } 43550026cea9SSean Farley 43560026cea9SSean Farley /*@C 4357b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 43580026cea9SSean Farley 43590026cea9SSean Farley Collective on TS 43600026cea9SSean Farley 43614165533cSJose E. Roman Input Parameters: 43620026cea9SSean Farley + ts - time stepping context 43630026cea9SSean Farley . t - time at which to evaluate 43640910c330SBarry Smith . U - state at which to evaluate 43650910c330SBarry Smith . Udot - time derivative of state vector 43660026cea9SSean Farley . shift - shift to apply 43670026cea9SSean Farley - ctx - context 43680026cea9SSean Farley 43694165533cSJose E. Roman Output Parameters: 43700026cea9SSean Farley + A - pointer to operator 437197bb3fdcSJose E. Roman - B - pointer to preconditioning matrix 43720026cea9SSean Farley 4373b41af12eSJed Brown Level: advanced 43740026cea9SSean Farley 43750026cea9SSean Farley Notes: 43760026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 43770026cea9SSean Farley 4378b41af12eSJed Brown It is only appropriate for problems of the form 4379b41af12eSJed Brown 4380b41af12eSJed Brown $ M Udot = F(U,t) 4381b41af12eSJed Brown 4382b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 4383b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 4384b41af12eSJed Brown an implicit operator of the form 4385b41af12eSJed Brown 4386b41af12eSJed Brown $ shift*M + J 4387b41af12eSJed Brown 4388b41af12eSJed 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 4389b41af12eSJed Brown a copy of M or reassemble it when requested. 4390b41af12eSJed Brown 4391db781477SPatrick Sanan .seealso: `TSSetIFunction()`, `TSSetIJacobian()`, `TSComputeIFunctionLinear()` 43920026cea9SSean Farley @*/ 4393d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 43940026cea9SSean Farley { 43950026cea9SSean Farley PetscFunctionBegin; 43969566063dSJacob Faibussowitsch PetscCall(MatScale(A, shift / ts->ijacobian.shift)); 4397b41af12eSJed Brown ts->ijacobian.shift = shift; 43980026cea9SSean Farley PetscFunctionReturn(0); 43990026cea9SSean Farley } 4400b41af12eSJed Brown 4401e817cc15SEmil Constantinescu /*@ 4402e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 4403e817cc15SEmil Constantinescu 4404e817cc15SEmil Constantinescu Not Collective 4405e817cc15SEmil Constantinescu 4406e817cc15SEmil Constantinescu Input Parameter: 4407e817cc15SEmil Constantinescu . ts - the TS context 4408e817cc15SEmil Constantinescu 4409e817cc15SEmil Constantinescu Output Parameter: 44104e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 4411e817cc15SEmil Constantinescu 4412e817cc15SEmil Constantinescu Level: beginner 4413e817cc15SEmil Constantinescu 4414db781477SPatrick Sanan .seealso: `TSSetEquationType()`, `TSEquationType` 4415e817cc15SEmil Constantinescu @*/ 4416e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 4417e817cc15SEmil Constantinescu { 4418e817cc15SEmil Constantinescu PetscFunctionBegin; 4419e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4420e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 4421e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 4422e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4423e817cc15SEmil Constantinescu } 4424e817cc15SEmil Constantinescu 4425e817cc15SEmil Constantinescu /*@ 4426e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 4427e817cc15SEmil Constantinescu 4428e817cc15SEmil Constantinescu Not Collective 4429e817cc15SEmil Constantinescu 4430d8d19677SJose E. Roman Input Parameters: 4431e817cc15SEmil Constantinescu + ts - the TS context 44321297b224SEmil Constantinescu - equation_type - see TSEquationType 4433e817cc15SEmil Constantinescu 4434e817cc15SEmil Constantinescu Level: advanced 4435e817cc15SEmil Constantinescu 4436db781477SPatrick Sanan .seealso: `TSGetEquationType()`, `TSEquationType` 4437e817cc15SEmil Constantinescu @*/ 4438e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 4439e817cc15SEmil Constantinescu { 4440e817cc15SEmil Constantinescu PetscFunctionBegin; 4441e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4442e817cc15SEmil Constantinescu ts->equation_type = equation_type; 4443e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4444e817cc15SEmil Constantinescu } 44450026cea9SSean Farley 44464af1b03aSJed Brown /*@ 44474af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 44484af1b03aSJed Brown 44494af1b03aSJed Brown Not Collective 44504af1b03aSJed Brown 44514af1b03aSJed Brown Input Parameter: 44524af1b03aSJed Brown . ts - the TS context 44534af1b03aSJed Brown 44544af1b03aSJed Brown Output Parameter: 44554af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 44564af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 44574af1b03aSJed Brown 4458487e0bb9SJed Brown Level: beginner 44594af1b03aSJed Brown 4460cd652676SJed Brown Notes: 4461cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 44624af1b03aSJed Brown 4463db781477SPatrick Sanan .seealso: `TSSetConvergenceTest()`, `TSConvergedReason` 44644af1b03aSJed Brown @*/ 44654af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 44664af1b03aSJed Brown { 44674af1b03aSJed Brown PetscFunctionBegin; 44684af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 44694af1b03aSJed Brown PetscValidPointer(reason,2); 44704af1b03aSJed Brown *reason = ts->reason; 44714af1b03aSJed Brown PetscFunctionReturn(0); 44724af1b03aSJed Brown } 44734af1b03aSJed Brown 4474d6ad946cSShri Abhyankar /*@ 4475d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 4476d6ad946cSShri Abhyankar 44776b221cbeSPatrick Sanan Logically Collective; reason must contain common value 4478d6ad946cSShri Abhyankar 44796b221cbeSPatrick Sanan Input Parameters: 4480d6ad946cSShri Abhyankar + ts - the TS context 44816b221cbeSPatrick Sanan - reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 4482d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 4483d6ad946cSShri Abhyankar 4484f5abba47SShri Abhyankar Level: advanced 4485d6ad946cSShri Abhyankar 4486d6ad946cSShri Abhyankar Notes: 44876b221cbeSPatrick Sanan Can only be called while TSSolve() is active. 4488d6ad946cSShri Abhyankar 4489db781477SPatrick Sanan .seealso: `TSConvergedReason` 4490d6ad946cSShri Abhyankar @*/ 4491d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 4492d6ad946cSShri Abhyankar { 4493d6ad946cSShri Abhyankar PetscFunctionBegin; 4494d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4495d6ad946cSShri Abhyankar ts->reason = reason; 4496d6ad946cSShri Abhyankar PetscFunctionReturn(0); 4497d6ad946cSShri Abhyankar } 4498d6ad946cSShri Abhyankar 4499cc708dedSBarry Smith /*@ 4500cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 4501cc708dedSBarry Smith 4502cc708dedSBarry Smith Not Collective 4503cc708dedSBarry Smith 4504cc708dedSBarry Smith Input Parameter: 4505cc708dedSBarry Smith . ts - the TS context 4506cc708dedSBarry Smith 4507cc708dedSBarry Smith Output Parameter: 450819eac22cSLisandro Dalcin . ftime - the final time. This time corresponds to the final time set with TSSetMaxTime() 4509cc708dedSBarry Smith 4510487e0bb9SJed Brown Level: beginner 4511cc708dedSBarry Smith 4512cc708dedSBarry Smith Notes: 4513cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 4514cc708dedSBarry Smith 4515db781477SPatrick Sanan .seealso: `TSSetConvergenceTest()`, `TSConvergedReason` 4516cc708dedSBarry Smith @*/ 4517cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 4518cc708dedSBarry Smith { 4519cc708dedSBarry Smith PetscFunctionBegin; 4520cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4521dadcf809SJacob Faibussowitsch PetscValidRealPointer(ftime,2); 4522cc708dedSBarry Smith *ftime = ts->solvetime; 4523cc708dedSBarry Smith PetscFunctionReturn(0); 4524cc708dedSBarry Smith } 4525cc708dedSBarry Smith 45262c18e0fdSBarry Smith /*@ 45275ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 45289f67acb7SJed Brown used by the time integrator. 45299f67acb7SJed Brown 45309f67acb7SJed Brown Not Collective 45319f67acb7SJed Brown 45329f67acb7SJed Brown Input Parameter: 45339f67acb7SJed Brown . ts - TS context 45349f67acb7SJed Brown 45359f67acb7SJed Brown Output Parameter: 45369f67acb7SJed Brown . nits - number of nonlinear iterations 45379f67acb7SJed Brown 45389f67acb7SJed Brown Notes: 45399f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 45409f67acb7SJed Brown 45419f67acb7SJed Brown Level: intermediate 45429f67acb7SJed Brown 4543db781477SPatrick Sanan .seealso: `TSGetKSPIterations()` 45449f67acb7SJed Brown @*/ 45455ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 45469f67acb7SJed Brown { 45479f67acb7SJed Brown PetscFunctionBegin; 45489f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 45499f67acb7SJed Brown PetscValidIntPointer(nits,2); 45505ef26d82SJed Brown *nits = ts->snes_its; 45519f67acb7SJed Brown PetscFunctionReturn(0); 45529f67acb7SJed Brown } 45539f67acb7SJed Brown 45549f67acb7SJed Brown /*@ 45555ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 45569f67acb7SJed Brown used by the time integrator. 45579f67acb7SJed Brown 45589f67acb7SJed Brown Not Collective 45599f67acb7SJed Brown 45609f67acb7SJed Brown Input Parameter: 45619f67acb7SJed Brown . ts - TS context 45629f67acb7SJed Brown 45639f67acb7SJed Brown Output Parameter: 45649f67acb7SJed Brown . lits - number of linear iterations 45659f67acb7SJed Brown 45669f67acb7SJed Brown Notes: 45679f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 45689f67acb7SJed Brown 45699f67acb7SJed Brown Level: intermediate 45709f67acb7SJed Brown 4571db781477SPatrick Sanan .seealso: `TSGetSNESIterations()`, `SNESGetKSPIterations()` 45729f67acb7SJed Brown @*/ 45735ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 45749f67acb7SJed Brown { 45759f67acb7SJed Brown PetscFunctionBegin; 45769f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 45779f67acb7SJed Brown PetscValidIntPointer(lits,2); 45785ef26d82SJed Brown *lits = ts->ksp_its; 45799f67acb7SJed Brown PetscFunctionReturn(0); 45809f67acb7SJed Brown } 45819f67acb7SJed Brown 4582cef5090cSJed Brown /*@ 4583cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 4584cef5090cSJed Brown 4585cef5090cSJed Brown Not Collective 4586cef5090cSJed Brown 4587cef5090cSJed Brown Input Parameter: 4588cef5090cSJed Brown . ts - TS context 4589cef5090cSJed Brown 4590cef5090cSJed Brown Output Parameter: 4591cef5090cSJed Brown . rejects - number of steps rejected 4592cef5090cSJed Brown 4593cef5090cSJed Brown Notes: 4594cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4595cef5090cSJed Brown 4596cef5090cSJed Brown Level: intermediate 4597cef5090cSJed Brown 4598db781477SPatrick Sanan .seealso: `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetSNESFailures()`, `TSSetMaxSNESFailures()`, `TSSetErrorIfStepFails()` 4599cef5090cSJed Brown @*/ 4600cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 4601cef5090cSJed Brown { 4602cef5090cSJed Brown PetscFunctionBegin; 4603cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4604cef5090cSJed Brown PetscValidIntPointer(rejects,2); 4605cef5090cSJed Brown *rejects = ts->reject; 4606cef5090cSJed Brown PetscFunctionReturn(0); 4607cef5090cSJed Brown } 4608cef5090cSJed Brown 4609cef5090cSJed Brown /*@ 4610cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 4611cef5090cSJed Brown 4612cef5090cSJed Brown Not Collective 4613cef5090cSJed Brown 4614cef5090cSJed Brown Input Parameter: 4615cef5090cSJed Brown . ts - TS context 4616cef5090cSJed Brown 4617cef5090cSJed Brown Output Parameter: 4618cef5090cSJed Brown . fails - number of failed nonlinear solves 4619cef5090cSJed Brown 4620cef5090cSJed Brown Notes: 4621cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4622cef5090cSJed Brown 4623cef5090cSJed Brown Level: intermediate 4624cef5090cSJed Brown 4625db781477SPatrick Sanan .seealso: `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSSetMaxSNESFailures()` 4626cef5090cSJed Brown @*/ 4627cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 4628cef5090cSJed Brown { 4629cef5090cSJed Brown PetscFunctionBegin; 4630cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4631cef5090cSJed Brown PetscValidIntPointer(fails,2); 4632cef5090cSJed Brown *fails = ts->num_snes_failures; 4633cef5090cSJed Brown PetscFunctionReturn(0); 4634cef5090cSJed Brown } 4635cef5090cSJed Brown 4636cef5090cSJed Brown /*@ 4637cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 4638cef5090cSJed Brown 4639cef5090cSJed Brown Not Collective 4640cef5090cSJed Brown 4641d8d19677SJose E. Roman Input Parameters: 4642cef5090cSJed Brown + ts - TS context 4643cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 4644cef5090cSJed Brown 4645cef5090cSJed Brown Notes: 4646cef5090cSJed Brown The counter is reset to zero for each step 4647cef5090cSJed Brown 4648cef5090cSJed Brown Options Database Key: 4649cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 4650cef5090cSJed Brown 4651cef5090cSJed Brown Level: intermediate 4652cef5090cSJed Brown 4653db781477SPatrick Sanan .seealso: `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxSNESFailures()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `TSSetErrorIfStepFails()`, `TSGetConvergedReason()` 4654cef5090cSJed Brown @*/ 4655cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 4656cef5090cSJed Brown { 4657cef5090cSJed Brown PetscFunctionBegin; 4658cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4659cef5090cSJed Brown ts->max_reject = rejects; 4660cef5090cSJed Brown PetscFunctionReturn(0); 4661cef5090cSJed Brown } 4662cef5090cSJed Brown 4663cef5090cSJed Brown /*@ 4664cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 4665cef5090cSJed Brown 4666cef5090cSJed Brown Not Collective 4667cef5090cSJed Brown 4668d8d19677SJose E. Roman Input Parameters: 4669cef5090cSJed Brown + ts - TS context 4670cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 4671cef5090cSJed Brown 4672cef5090cSJed Brown Notes: 4673cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 4674cef5090cSJed Brown 4675cef5090cSJed Brown Options Database Key: 4676cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 4677cef5090cSJed Brown 4678cef5090cSJed Brown Level: intermediate 4679cef5090cSJed Brown 4680db781477SPatrick Sanan .seealso: `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `SNESGetConvergedReason()`, `TSGetConvergedReason()` 4681cef5090cSJed Brown @*/ 4682cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 4683cef5090cSJed Brown { 4684cef5090cSJed Brown PetscFunctionBegin; 4685cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4686cef5090cSJed Brown ts->max_snes_failures = fails; 4687cef5090cSJed Brown PetscFunctionReturn(0); 4688cef5090cSJed Brown } 4689cef5090cSJed Brown 4690cef5090cSJed Brown /*@ 4691cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 4692cef5090cSJed Brown 4693cef5090cSJed Brown Not Collective 4694cef5090cSJed Brown 4695d8d19677SJose E. Roman Input Parameters: 4696cef5090cSJed Brown + ts - TS context 4697cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 4698cef5090cSJed Brown 4699cef5090cSJed Brown Options Database Key: 4700cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 4701cef5090cSJed Brown 4702cef5090cSJed Brown Level: intermediate 4703cef5090cSJed Brown 4704db781477SPatrick Sanan .seealso: `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `TSSetErrorIfStepFails()`, `TSGetConvergedReason()` 4705cef5090cSJed Brown @*/ 4706cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 4707cef5090cSJed Brown { 4708cef5090cSJed Brown PetscFunctionBegin; 4709cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4710cef5090cSJed Brown ts->errorifstepfailed = err; 4711cef5090cSJed Brown PetscFunctionReturn(0); 4712cef5090cSJed Brown } 4713cef5090cSJed Brown 471484df9cb4SJed Brown /*@ 4715552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 471684df9cb4SJed Brown 4717ed81e22dSJed Brown Collective on TS if controller has not been created yet 471884df9cb4SJed Brown 47194165533cSJose E. Roman Input Parameter: 4720ed81e22dSJed Brown . ts - time stepping context 472184df9cb4SJed Brown 47224165533cSJose E. Roman Output Parameter: 4723ed81e22dSJed Brown . adapt - adaptive controller 472484df9cb4SJed Brown 472584df9cb4SJed Brown Level: intermediate 472684df9cb4SJed Brown 4727db781477SPatrick Sanan .seealso: `TSAdapt`, `TSAdaptSetType()`, `TSAdaptChoose()` 472884df9cb4SJed Brown @*/ 4729552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 473084df9cb4SJed Brown { 473184df9cb4SJed Brown PetscFunctionBegin; 473284df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4733bec58848SLisandro Dalcin PetscValidPointer(adapt,2); 473484df9cb4SJed Brown if (!ts->adapt) { 47359566063dSJacob Faibussowitsch PetscCall(TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt)); 47369566063dSJacob Faibussowitsch PetscCall(PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt)); 47379566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1)); 473884df9cb4SJed Brown } 4739bec58848SLisandro Dalcin *adapt = ts->adapt; 474084df9cb4SJed Brown PetscFunctionReturn(0); 474184df9cb4SJed Brown } 4742d6ebe24aSShri Abhyankar 47431c3436cfSJed Brown /*@ 47441c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 47451c3436cfSJed Brown 47461c3436cfSJed Brown Logically Collective 47471c3436cfSJed Brown 47484165533cSJose E. Roman Input Parameters: 47491c3436cfSJed Brown + ts - time integration context 47501c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 47510298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 47521c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 47530298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 47541c3436cfSJed Brown 4755a3cdaa26SBarry Smith Options Database keys: 4756a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 475767b8a455SSatish Balay - -ts_atol <atol> - Absolute tolerance for local truncation error 4758a3cdaa26SBarry Smith 47593ff766beSShri Abhyankar Notes: 47603ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 47613ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 47623ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 47633ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 47643ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 47653ff766beSShri Abhyankar differential variables. 47663ff766beSShri Abhyankar 47671c3436cfSJed Brown Level: beginner 47681c3436cfSJed Brown 4769db781477SPatrick Sanan .seealso: `TS`, `TSAdapt`, `TSErrorWeightedNorm()`, `TSGetTolerances()` 47701c3436cfSJed Brown @*/ 47711c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 47721c3436cfSJed Brown { 47731c3436cfSJed Brown PetscFunctionBegin; 4774c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 47751c3436cfSJed Brown if (vatol) { 47769566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)vatol)); 47779566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vatol)); 47781c3436cfSJed Brown ts->vatol = vatol; 47791c3436cfSJed Brown } 4780c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 47811c3436cfSJed Brown if (vrtol) { 47829566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)vrtol)); 47839566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vrtol)); 47841c3436cfSJed Brown ts->vrtol = vrtol; 47851c3436cfSJed Brown } 47861c3436cfSJed Brown PetscFunctionReturn(0); 47871c3436cfSJed Brown } 47881c3436cfSJed Brown 4789c5033834SJed Brown /*@ 4790c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 4791c5033834SJed Brown 4792c5033834SJed Brown Logically Collective 4793c5033834SJed Brown 47944165533cSJose E. Roman Input Parameter: 4795c5033834SJed Brown . ts - time integration context 4796c5033834SJed Brown 47974165533cSJose E. Roman Output Parameters: 47980298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 47990298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 48000298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 48010298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 4802c5033834SJed Brown 4803c5033834SJed Brown Level: beginner 4804c5033834SJed Brown 4805db781477SPatrick Sanan .seealso: `TS`, `TSAdapt`, `TSErrorWeightedNorm()`, `TSSetTolerances()` 4806c5033834SJed Brown @*/ 4807c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 4808c5033834SJed Brown { 4809c5033834SJed Brown PetscFunctionBegin; 4810c5033834SJed Brown if (atol) *atol = ts->atol; 4811c5033834SJed Brown if (vatol) *vatol = ts->vatol; 4812c5033834SJed Brown if (rtol) *rtol = ts->rtol; 4813c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 4814c5033834SJed Brown PetscFunctionReturn(0); 4815c5033834SJed Brown } 4816c5033834SJed Brown 48179c6b16b5SShri Abhyankar /*@ 4818a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 48199c6b16b5SShri Abhyankar 48209c6b16b5SShri Abhyankar Collective on TS 48219c6b16b5SShri Abhyankar 48224165533cSJose E. Roman Input Parameters: 48239c6b16b5SShri Abhyankar + ts - time stepping context 4824a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 4825a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 48269c6b16b5SShri Abhyankar 48274165533cSJose E. Roman Output Parameters: 4828a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 48297453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 4830a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 48319c6b16b5SShri Abhyankar 48329c6b16b5SShri Abhyankar Level: developer 48339c6b16b5SShri Abhyankar 4834db781477SPatrick Sanan .seealso: `TSErrorWeightedNorm()`, `TSErrorWeightedNormInfinity()` 48359c6b16b5SShri Abhyankar @*/ 48367453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 48379c6b16b5SShri Abhyankar { 48389c6b16b5SShri Abhyankar PetscInt i,n,N,rstart; 48397453f775SEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 48407453f775SEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 48419c6b16b5SShri Abhyankar const PetscScalar *u,*y; 48427453f775SEmil Constantinescu PetscReal sum,suma,sumr,gsum,gsuma,gsumr,diff; 48437453f775SEmil Constantinescu PetscReal tol,tola,tolr; 48447453f775SEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 48459c6b16b5SShri Abhyankar 48469c6b16b5SShri Abhyankar PetscFunctionBegin; 48479c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4848a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 4849a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 4850a4868fbcSLisandro Dalcin PetscValidType(U,2); 4851a4868fbcSLisandro Dalcin PetscValidType(Y,3); 4852a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 4853dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm,4); 4854dadcf809SJacob Faibussowitsch PetscValidRealPointer(norma,5); 4855dadcf809SJacob Faibussowitsch PetscValidRealPointer(normr,6); 48563c633725SBarry Smith PetscCheck(U != Y,PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 48579c6b16b5SShri Abhyankar 48589566063dSJacob Faibussowitsch PetscCall(VecGetSize(U,&N)); 48599566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(U,&n)); 48609566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(U,&rstart,NULL)); 48619566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(U,&u)); 48629566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(Y,&y)); 48637453f775SEmil Constantinescu sum = 0.; n_loc = 0; 48647453f775SEmil Constantinescu suma = 0.; na_loc = 0; 48657453f775SEmil Constantinescu sumr = 0.; nr_loc = 0; 48669c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 48679c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 48689566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 48699566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 48709c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 487176cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 48727453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 48737453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 48747453f775SEmil Constantinescu if (tola>0.) { 48757453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 48767453f775SEmil Constantinescu na_loc++; 48777453f775SEmil Constantinescu } 48787453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 48797453f775SEmil Constantinescu if (tolr>0.) { 48807453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 48817453f775SEmil Constantinescu nr_loc++; 48827453f775SEmil Constantinescu } 48837453f775SEmil Constantinescu tol=tola+tolr; 48847453f775SEmil Constantinescu if (tol>0.) { 48857453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 48867453f775SEmil Constantinescu n_loc++; 48877453f775SEmil Constantinescu } 48889c6b16b5SShri Abhyankar } 48899566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 48909566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 48919c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 48929c6b16b5SShri Abhyankar const PetscScalar *atol; 48939566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 48949c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 489576cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 48967453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 48977453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 48987453f775SEmil Constantinescu if (tola>0.) { 48997453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 49007453f775SEmil Constantinescu na_loc++; 49017453f775SEmil Constantinescu } 49027453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 49037453f775SEmil Constantinescu if (tolr>0.) { 49047453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 49057453f775SEmil Constantinescu nr_loc++; 49067453f775SEmil Constantinescu } 49077453f775SEmil Constantinescu tol=tola+tolr; 49087453f775SEmil Constantinescu if (tol>0.) { 49097453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 49107453f775SEmil Constantinescu n_loc++; 49117453f775SEmil Constantinescu } 49129c6b16b5SShri Abhyankar } 49139566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 49149c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 49159c6b16b5SShri Abhyankar const PetscScalar *rtol; 49169566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 49179c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 491876cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 49197453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 49207453f775SEmil Constantinescu tola = ts->atol; 49217453f775SEmil Constantinescu if (tola>0.) { 49227453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 49237453f775SEmil Constantinescu na_loc++; 49247453f775SEmil Constantinescu } 49257453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 49267453f775SEmil Constantinescu if (tolr>0.) { 49277453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 49287453f775SEmil Constantinescu nr_loc++; 49297453f775SEmil Constantinescu } 49307453f775SEmil Constantinescu tol=tola+tolr; 49317453f775SEmil Constantinescu if (tol>0.) { 49327453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 49337453f775SEmil Constantinescu n_loc++; 49347453f775SEmil Constantinescu } 49359c6b16b5SShri Abhyankar } 49369566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 49379c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 49389c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 493976cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 49407453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 49417453f775SEmil Constantinescu tola = ts->atol; 49427453f775SEmil Constantinescu if (tola>0.) { 49437453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 49447453f775SEmil Constantinescu na_loc++; 49457453f775SEmil Constantinescu } 49467453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 49477453f775SEmil Constantinescu if (tolr>0.) { 49487453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 49497453f775SEmil Constantinescu nr_loc++; 49507453f775SEmil Constantinescu } 49517453f775SEmil Constantinescu tol=tola+tolr; 49527453f775SEmil Constantinescu if (tol>0.) { 49537453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 49547453f775SEmil Constantinescu n_loc++; 49557453f775SEmil Constantinescu } 49569c6b16b5SShri Abhyankar } 49579c6b16b5SShri Abhyankar } 49589566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(U,&u)); 49599566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(Y,&y)); 49609c6b16b5SShri Abhyankar 49617453f775SEmil Constantinescu err_loc[0] = sum; 49627453f775SEmil Constantinescu err_loc[1] = suma; 49637453f775SEmil Constantinescu err_loc[2] = sumr; 49647453f775SEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 49657453f775SEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 49667453f775SEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 49677453f775SEmil Constantinescu 49681c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts))); 49697453f775SEmil Constantinescu 49707453f775SEmil Constantinescu gsum = err_glb[0]; 49717453f775SEmil Constantinescu gsuma = err_glb[1]; 49727453f775SEmil Constantinescu gsumr = err_glb[2]; 49737453f775SEmil Constantinescu n_glb = err_glb[3]; 49747453f775SEmil Constantinescu na_glb = err_glb[4]; 49757453f775SEmil Constantinescu nr_glb = err_glb[5]; 49767453f775SEmil Constantinescu 4977b1316ef9SEmil Constantinescu *norm = 0.; 49781e66621cSBarry Smith if (n_glb>0.) *norm = PetscSqrtReal(gsum / n_glb); 4979b1316ef9SEmil Constantinescu *norma = 0.; 49801e66621cSBarry Smith if (na_glb>0.) *norma = PetscSqrtReal(gsuma / na_glb); 4981b1316ef9SEmil Constantinescu *normr = 0.; 49821e66621cSBarry Smith if (nr_glb>0.) *normr = PetscSqrtReal(gsumr / nr_glb); 49839c6b16b5SShri Abhyankar 49843c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 49853c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 49863c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 49879c6b16b5SShri Abhyankar PetscFunctionReturn(0); 49889c6b16b5SShri Abhyankar } 49899c6b16b5SShri Abhyankar 49909c6b16b5SShri Abhyankar /*@ 4991a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 49929c6b16b5SShri Abhyankar 49939c6b16b5SShri Abhyankar Collective on TS 49949c6b16b5SShri Abhyankar 49954165533cSJose E. Roman Input Parameters: 49969c6b16b5SShri Abhyankar + ts - time stepping context 4997a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 4998a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 49999c6b16b5SShri Abhyankar 50004165533cSJose E. Roman Output Parameters: 5001a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 50027453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5003a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 50049c6b16b5SShri Abhyankar 50059c6b16b5SShri Abhyankar Level: developer 50069c6b16b5SShri Abhyankar 5007db781477SPatrick Sanan .seealso: `TSErrorWeightedNorm()`, `TSErrorWeightedNorm2()` 50089c6b16b5SShri Abhyankar @*/ 50097453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 50109c6b16b5SShri Abhyankar { 50117453f775SEmil Constantinescu PetscInt i,n,N,rstart; 50129c6b16b5SShri Abhyankar const PetscScalar *u,*y; 50137453f775SEmil Constantinescu PetscReal max,gmax,maxa,gmaxa,maxr,gmaxr; 50147453f775SEmil Constantinescu PetscReal tol,tola,tolr,diff; 50157453f775SEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 50169c6b16b5SShri Abhyankar 50179c6b16b5SShri Abhyankar PetscFunctionBegin; 50189c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5019a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5020a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5021a4868fbcSLisandro Dalcin PetscValidType(U,2); 5022a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5023a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5024dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm,4); 5025dadcf809SJacob Faibussowitsch PetscValidRealPointer(norma,5); 5026dadcf809SJacob Faibussowitsch PetscValidRealPointer(normr,6); 50273c633725SBarry Smith PetscCheck(U != Y,PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 50289c6b16b5SShri Abhyankar 50299566063dSJacob Faibussowitsch PetscCall(VecGetSize(U,&N)); 50309566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(U,&n)); 50319566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(U,&rstart,NULL)); 50329566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(U,&u)); 50339566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(Y,&y)); 50347453f775SEmil Constantinescu 50357453f775SEmil Constantinescu max=0.; 50367453f775SEmil Constantinescu maxa=0.; 50377453f775SEmil Constantinescu maxr=0.; 50387453f775SEmil Constantinescu 50397453f775SEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 50409c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 50419566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 50429566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 50437453f775SEmil Constantinescu 50447453f775SEmil Constantinescu for (i=0; i<n; i++) { 504576cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 50467453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50477453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 50487453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 50497453f775SEmil Constantinescu tol = tola+tolr; 50501e66621cSBarry Smith if (tola>0.) maxa = PetscMax(maxa,diff / tola); 50511e66621cSBarry Smith if (tolr>0.) maxr = PetscMax(maxr,diff / tolr); 50521e66621cSBarry Smith if (tol>0.) max = PetscMax(max,diff / tol); 50539c6b16b5SShri Abhyankar } 50549566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 50559566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 50569c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 50579c6b16b5SShri Abhyankar const PetscScalar *atol; 50589566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 50597453f775SEmil Constantinescu for (i=0; i<n; i++) { 506076cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 50617453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50627453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 50637453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 50647453f775SEmil Constantinescu tol = tola+tolr; 50651e66621cSBarry Smith if (tola>0.) maxa = PetscMax(maxa,diff / tola); 50661e66621cSBarry Smith if (tolr>0.) maxr = PetscMax(maxr,diff / tolr); 50671e66621cSBarry Smith if (tol>0.) max = PetscMax(max,diff / tol); 50689c6b16b5SShri Abhyankar } 50699566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 50709c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 50719c6b16b5SShri Abhyankar const PetscScalar *rtol; 50729566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 50737453f775SEmil Constantinescu 50747453f775SEmil Constantinescu for (i=0; i<n; i++) { 507576cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 50767453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50777453f775SEmil Constantinescu tola = ts->atol; 50787453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 50797453f775SEmil Constantinescu tol = tola+tolr; 50801e66621cSBarry Smith if (tola>0.) maxa = PetscMax(maxa,diff / tola); 50811e66621cSBarry Smith if (tolr>0.) maxr = PetscMax(maxr,diff / tolr); 50821e66621cSBarry Smith if (tol>0.) max = PetscMax(max,diff / tol); 50839c6b16b5SShri Abhyankar } 50849566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 50859c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 50867453f775SEmil Constantinescu 50877453f775SEmil Constantinescu for (i=0; i<n; i++) { 508876cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 50897453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50907453f775SEmil Constantinescu tola = ts->atol; 50917453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 50927453f775SEmil Constantinescu tol = tola+tolr; 50931e66621cSBarry Smith if (tola>0.) maxa = PetscMax(maxa,diff / tola); 50941e66621cSBarry Smith if (tolr>0.) maxr = PetscMax(maxr,diff / tolr); 50951e66621cSBarry Smith if (tol>0.) max = PetscMax(max,diff / tol); 50969c6b16b5SShri Abhyankar } 50979c6b16b5SShri Abhyankar } 50989566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(U,&u)); 50999566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(Y,&y)); 51007453f775SEmil Constantinescu err_loc[0] = max; 51017453f775SEmil Constantinescu err_loc[1] = maxa; 51027453f775SEmil Constantinescu err_loc[2] = maxr; 51031c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts))); 51047453f775SEmil Constantinescu gmax = err_glb[0]; 51057453f775SEmil Constantinescu gmaxa = err_glb[1]; 51067453f775SEmil Constantinescu gmaxr = err_glb[2]; 51079c6b16b5SShri Abhyankar 51089c6b16b5SShri Abhyankar *norm = gmax; 51097453f775SEmil Constantinescu *norma = gmaxa; 51107453f775SEmil Constantinescu *normr = gmaxr; 51113c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 51123c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 51133c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 51149c6b16b5SShri Abhyankar PetscFunctionReturn(0); 51159c6b16b5SShri Abhyankar } 51169c6b16b5SShri Abhyankar 51171c3436cfSJed Brown /*@ 51188a175baeSEmil Constantinescu TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances 51191c3436cfSJed Brown 51201c3436cfSJed Brown Collective on TS 51211c3436cfSJed Brown 51224165533cSJose E. Roman Input Parameters: 51231c3436cfSJed Brown + ts - time stepping context 5124a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5125a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 5126a4868fbcSLisandro Dalcin - wnormtype - norm type, either NORM_2 or NORM_INFINITY 51277619abb3SShri 51284165533cSJose E. Roman Output Parameters: 5129a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 51308a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 5131a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 5132a4868fbcSLisandro Dalcin 5133a4868fbcSLisandro Dalcin Options Database Keys: 5134a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 5135a4868fbcSLisandro Dalcin 51361c3436cfSJed Brown Level: developer 51371c3436cfSJed Brown 5138db781477SPatrick Sanan .seealso: `TSErrorWeightedNormInfinity()`, `TSErrorWeightedNorm2()`, `TSErrorWeightedENorm` 51391c3436cfSJed Brown @*/ 51407453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 51411c3436cfSJed Brown { 51421c3436cfSJed Brown PetscFunctionBegin; 51431e66621cSBarry Smith if (wnormtype == NORM_2) PetscCall(TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr)); 51441e66621cSBarry Smith else if (wnormtype == NORM_INFINITY) PetscCall(TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr)); 51451e66621cSBarry Smith else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 51461c3436cfSJed Brown PetscFunctionReturn(0); 51471c3436cfSJed Brown } 51481c3436cfSJed Brown 51498a175baeSEmil Constantinescu /*@ 51508a175baeSEmil Constantinescu TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances 51518a175baeSEmil Constantinescu 51528a175baeSEmil Constantinescu Collective on TS 51538a175baeSEmil Constantinescu 51544165533cSJose E. Roman Input Parameters: 51558a175baeSEmil Constantinescu + ts - time stepping context 51568a175baeSEmil Constantinescu . E - error vector 51578a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 51588a175baeSEmil Constantinescu - Y - state vector, previous time step 51598a175baeSEmil Constantinescu 51604165533cSJose E. Roman Output Parameters: 5161a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 51628a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5163a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 51648a175baeSEmil Constantinescu 51658a175baeSEmil Constantinescu Level: developer 51668a175baeSEmil Constantinescu 5167db781477SPatrick Sanan .seealso: `TSErrorWeightedENorm()`, `TSErrorWeightedENormInfinity()` 51688a175baeSEmil Constantinescu @*/ 51698a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 51708a175baeSEmil Constantinescu { 51718a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 51728a175baeSEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 51738a175baeSEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 51748a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 51758a175baeSEmil Constantinescu PetscReal err,sum,suma,sumr,gsum,gsuma,gsumr; 51768a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 51778a175baeSEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 51788a175baeSEmil Constantinescu 51798a175baeSEmil Constantinescu PetscFunctionBegin; 51808a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 51818a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 51828a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 51838a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 51848a175baeSEmil Constantinescu PetscValidType(E,2); 51858a175baeSEmil Constantinescu PetscValidType(U,3); 51868a175baeSEmil Constantinescu PetscValidType(Y,4); 51878a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 5188064a246eSJacob Faibussowitsch PetscCheckSameComm(U,3,Y,4); 5189dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm,5); 5190dadcf809SJacob Faibussowitsch PetscValidRealPointer(norma,6); 5191dadcf809SJacob Faibussowitsch PetscValidRealPointer(normr,7); 51928a175baeSEmil Constantinescu 51939566063dSJacob Faibussowitsch PetscCall(VecGetSize(E,&N)); 51949566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(E,&n)); 51959566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(E,&rstart,NULL)); 51969566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(E,&e)); 51979566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(U,&u)); 51989566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(Y,&y)); 51998a175baeSEmil Constantinescu sum = 0.; n_loc = 0; 52008a175baeSEmil Constantinescu suma = 0.; na_loc = 0; 52018a175baeSEmil Constantinescu sumr = 0.; nr_loc = 0; 52028a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { 52038a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 52049566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 52059566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 52068a175baeSEmil Constantinescu for (i=0; i<n; i++) { 520776cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 52088a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 52098a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 52108a175baeSEmil Constantinescu if (tola>0.) { 52118a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 52128a175baeSEmil Constantinescu na_loc++; 52138a175baeSEmil Constantinescu } 52148a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52158a175baeSEmil Constantinescu if (tolr>0.) { 52168a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 52178a175baeSEmil Constantinescu nr_loc++; 52188a175baeSEmil Constantinescu } 52198a175baeSEmil Constantinescu tol=tola+tolr; 52208a175baeSEmil Constantinescu if (tol>0.) { 52218a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 52228a175baeSEmil Constantinescu n_loc++; 52238a175baeSEmil Constantinescu } 52248a175baeSEmil Constantinescu } 52259566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 52269566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 52278a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 52288a175baeSEmil Constantinescu const PetscScalar *atol; 52299566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 52308a175baeSEmil Constantinescu for (i=0; i<n; i++) { 523176cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 52328a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 52338a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 52348a175baeSEmil Constantinescu if (tola>0.) { 52358a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 52368a175baeSEmil Constantinescu na_loc++; 52378a175baeSEmil Constantinescu } 52388a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52398a175baeSEmil Constantinescu if (tolr>0.) { 52408a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 52418a175baeSEmil Constantinescu nr_loc++; 52428a175baeSEmil Constantinescu } 52438a175baeSEmil Constantinescu tol=tola+tolr; 52448a175baeSEmil Constantinescu if (tol>0.) { 52458a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 52468a175baeSEmil Constantinescu n_loc++; 52478a175baeSEmil Constantinescu } 52488a175baeSEmil Constantinescu } 52499566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 52508a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 52518a175baeSEmil Constantinescu const PetscScalar *rtol; 52529566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 52538a175baeSEmil Constantinescu for (i=0; i<n; i++) { 525476cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 52558a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 52568a175baeSEmil Constantinescu tola = ts->atol; 52578a175baeSEmil Constantinescu if (tola>0.) { 52588a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 52598a175baeSEmil Constantinescu na_loc++; 52608a175baeSEmil Constantinescu } 52618a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52628a175baeSEmil Constantinescu if (tolr>0.) { 52638a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 52648a175baeSEmil Constantinescu nr_loc++; 52658a175baeSEmil Constantinescu } 52668a175baeSEmil Constantinescu tol=tola+tolr; 52678a175baeSEmil Constantinescu if (tol>0.) { 52688a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 52698a175baeSEmil Constantinescu n_loc++; 52708a175baeSEmil Constantinescu } 52718a175baeSEmil Constantinescu } 52729566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 52738a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 52748a175baeSEmil Constantinescu for (i=0; i<n; i++) { 527576cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 52768a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 52778a175baeSEmil Constantinescu tola = ts->atol; 52788a175baeSEmil Constantinescu if (tola>0.) { 52798a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 52808a175baeSEmil Constantinescu na_loc++; 52818a175baeSEmil Constantinescu } 52828a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52838a175baeSEmil Constantinescu if (tolr>0.) { 52848a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 52858a175baeSEmil Constantinescu nr_loc++; 52868a175baeSEmil Constantinescu } 52878a175baeSEmil Constantinescu tol=tola+tolr; 52888a175baeSEmil Constantinescu if (tol>0.) { 52898a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 52908a175baeSEmil Constantinescu n_loc++; 52918a175baeSEmil Constantinescu } 52928a175baeSEmil Constantinescu } 52938a175baeSEmil Constantinescu } 52949566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(E,&e)); 52959566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(U,&u)); 52969566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(Y,&y)); 52978a175baeSEmil Constantinescu 52988a175baeSEmil Constantinescu err_loc[0] = sum; 52998a175baeSEmil Constantinescu err_loc[1] = suma; 53008a175baeSEmil Constantinescu err_loc[2] = sumr; 53018a175baeSEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 53028a175baeSEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 53038a175baeSEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 53048a175baeSEmil Constantinescu 53051c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts))); 53068a175baeSEmil Constantinescu 53078a175baeSEmil Constantinescu gsum = err_glb[0]; 53088a175baeSEmil Constantinescu gsuma = err_glb[1]; 53098a175baeSEmil Constantinescu gsumr = err_glb[2]; 53108a175baeSEmil Constantinescu n_glb = err_glb[3]; 53118a175baeSEmil Constantinescu na_glb = err_glb[4]; 53128a175baeSEmil Constantinescu nr_glb = err_glb[5]; 53138a175baeSEmil Constantinescu 53148a175baeSEmil Constantinescu *norm = 0.; 53151e66621cSBarry Smith if (n_glb>0.) *norm = PetscSqrtReal(gsum / n_glb); 53168a175baeSEmil Constantinescu *norma = 0.; 53171e66621cSBarry Smith if (na_glb>0.) *norma = PetscSqrtReal(gsuma / na_glb); 53188a175baeSEmil Constantinescu *normr = 0.; 53191e66621cSBarry Smith if (nr_glb>0.) *normr = PetscSqrtReal(gsumr / nr_glb); 53208a175baeSEmil Constantinescu 53213c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 53223c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 53233c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 53248a175baeSEmil Constantinescu PetscFunctionReturn(0); 53258a175baeSEmil Constantinescu } 53268a175baeSEmil Constantinescu 53278a175baeSEmil Constantinescu /*@ 53288a175baeSEmil Constantinescu TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances 53298a175baeSEmil Constantinescu Collective on TS 53308a175baeSEmil Constantinescu 53314165533cSJose E. Roman Input Parameters: 53328a175baeSEmil Constantinescu + ts - time stepping context 53338a175baeSEmil Constantinescu . E - error vector 53348a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 53358a175baeSEmil Constantinescu - Y - state vector, previous time step 53368a175baeSEmil Constantinescu 53374165533cSJose E. Roman Output Parameters: 5338a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 53398a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5340a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 53418a175baeSEmil Constantinescu 53428a175baeSEmil Constantinescu Level: developer 53438a175baeSEmil Constantinescu 5344db781477SPatrick Sanan .seealso: `TSErrorWeightedENorm()`, `TSErrorWeightedENorm2()` 53458a175baeSEmil Constantinescu @*/ 53468a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 53478a175baeSEmil Constantinescu { 53488a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 53498a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 53508a175baeSEmil Constantinescu PetscReal err,max,gmax,maxa,gmaxa,maxr,gmaxr; 53518a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 53528a175baeSEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 53538a175baeSEmil Constantinescu 53548a175baeSEmil Constantinescu PetscFunctionBegin; 53558a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 53568a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 53578a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 53588a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 53598a175baeSEmil Constantinescu PetscValidType(E,2); 53608a175baeSEmil Constantinescu PetscValidType(U,3); 53618a175baeSEmil Constantinescu PetscValidType(Y,4); 53628a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 5363064a246eSJacob Faibussowitsch PetscCheckSameComm(U,3,Y,4); 5364dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm,5); 5365dadcf809SJacob Faibussowitsch PetscValidRealPointer(norma,6); 5366dadcf809SJacob Faibussowitsch PetscValidRealPointer(normr,7); 53678a175baeSEmil Constantinescu 53689566063dSJacob Faibussowitsch PetscCall(VecGetSize(E,&N)); 53699566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(E,&n)); 53709566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(E,&rstart,NULL)); 53719566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(E,&e)); 53729566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(U,&u)); 53739566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(Y,&y)); 53748a175baeSEmil Constantinescu 53758a175baeSEmil Constantinescu max=0.; 53768a175baeSEmil Constantinescu maxa=0.; 53778a175baeSEmil Constantinescu maxr=0.; 53788a175baeSEmil Constantinescu 53798a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 53808a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 53819566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 53829566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 53838a175baeSEmil Constantinescu 53848a175baeSEmil Constantinescu for (i=0; i<n; i++) { 538576cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 53868a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 53878a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 53888a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 53898a175baeSEmil Constantinescu tol = tola+tolr; 53901e66621cSBarry Smith if (tola>0.) maxa = PetscMax(maxa,err / tola); 53911e66621cSBarry Smith if (tolr>0.) maxr = PetscMax(maxr,err / tolr); 53921e66621cSBarry Smith if (tol>0.) max = PetscMax(max,err / tol); 53938a175baeSEmil Constantinescu } 53949566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 53959566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 53968a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 53978a175baeSEmil Constantinescu const PetscScalar *atol; 53989566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 53998a175baeSEmil Constantinescu for (i=0; i<n; i++) { 540076cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 54018a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 54028a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 54038a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54048a175baeSEmil Constantinescu tol = tola+tolr; 54051e66621cSBarry Smith if (tola>0.) maxa = PetscMax(maxa,err / tola); 54061e66621cSBarry Smith if (tolr>0.) maxr = PetscMax(maxr,err / tolr); 54071e66621cSBarry Smith if (tol>0.) max = PetscMax(max,err / tol); 54088a175baeSEmil Constantinescu } 54099566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 54108a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 54118a175baeSEmil Constantinescu const PetscScalar *rtol; 54129566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 54138a175baeSEmil Constantinescu 54148a175baeSEmil Constantinescu for (i=0; i<n; i++) { 541576cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 54168a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 54178a175baeSEmil Constantinescu tola = ts->atol; 54188a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54198a175baeSEmil Constantinescu tol = tola+tolr; 54201e66621cSBarry Smith if (tola>0.) maxa = PetscMax(maxa,err / tola); 54211e66621cSBarry Smith if (tolr>0.) maxr = PetscMax(maxr,err / tolr); 54221e66621cSBarry Smith if (tol>0.) max = PetscMax(max,err / tol); 54238a175baeSEmil Constantinescu } 54249566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 54258a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 54268a175baeSEmil Constantinescu 54278a175baeSEmil Constantinescu for (i=0; i<n; i++) { 542876cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 54298a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 54308a175baeSEmil Constantinescu tola = ts->atol; 54318a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54328a175baeSEmil Constantinescu tol = tola+tolr; 54331e66621cSBarry Smith if (tola>0.) maxa = PetscMax(maxa,err / tola); 54341e66621cSBarry Smith if (tolr>0.) maxr = PetscMax(maxr,err / tolr); 54351e66621cSBarry Smith if (tol>0.) max = PetscMax(max,err / tol); 54368a175baeSEmil Constantinescu } 54378a175baeSEmil Constantinescu } 54389566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(E,&e)); 54399566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(U,&u)); 54409566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(Y,&y)); 54418a175baeSEmil Constantinescu err_loc[0] = max; 54428a175baeSEmil Constantinescu err_loc[1] = maxa; 54438a175baeSEmil Constantinescu err_loc[2] = maxr; 54441c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts))); 54458a175baeSEmil Constantinescu gmax = err_glb[0]; 54468a175baeSEmil Constantinescu gmaxa = err_glb[1]; 54478a175baeSEmil Constantinescu gmaxr = err_glb[2]; 54488a175baeSEmil Constantinescu 54498a175baeSEmil Constantinescu *norm = gmax; 54508a175baeSEmil Constantinescu *norma = gmaxa; 54518a175baeSEmil Constantinescu *normr = gmaxr; 54523c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 54533c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 54543c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 54558a175baeSEmil Constantinescu PetscFunctionReturn(0); 54568a175baeSEmil Constantinescu } 54578a175baeSEmil Constantinescu 54588a175baeSEmil Constantinescu /*@ 54598a175baeSEmil Constantinescu TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances 54608a175baeSEmil Constantinescu 54618a175baeSEmil Constantinescu Collective on TS 54628a175baeSEmil Constantinescu 54634165533cSJose E. Roman Input Parameters: 54648a175baeSEmil Constantinescu + ts - time stepping context 54658a175baeSEmil Constantinescu . E - error vector 54668a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 54678a175baeSEmil Constantinescu . Y - state vector, previous time step 54688a175baeSEmil Constantinescu - wnormtype - norm type, either NORM_2 or NORM_INFINITY 54698a175baeSEmil Constantinescu 54704165533cSJose E. Roman Output Parameters: 5471a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 54728a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 5473a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 54748a175baeSEmil Constantinescu 54758a175baeSEmil Constantinescu Options Database Keys: 54768a175baeSEmil Constantinescu . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 54778a175baeSEmil Constantinescu 54788a175baeSEmil Constantinescu Level: developer 54798a175baeSEmil Constantinescu 5480db781477SPatrick Sanan .seealso: `TSErrorWeightedENormInfinity()`, `TSErrorWeightedENorm2()`, `TSErrorWeightedNormInfinity()`, `TSErrorWeightedNorm2()` 54818a175baeSEmil Constantinescu @*/ 54828a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 54838a175baeSEmil Constantinescu { 54848a175baeSEmil Constantinescu PetscFunctionBegin; 54851e66621cSBarry Smith if (wnormtype == NORM_2)PetscCall(TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr)); 54861e66621cSBarry Smith else if (wnormtype == NORM_INFINITY) PetscCall(TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr)); 54871e66621cSBarry Smith else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 54888a175baeSEmil Constantinescu PetscFunctionReturn(0); 54898a175baeSEmil Constantinescu } 54908a175baeSEmil Constantinescu 54918d59e960SJed Brown /*@ 54928d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 54938d59e960SJed Brown 54948d59e960SJed Brown Logically Collective on TS 54958d59e960SJed Brown 54964165533cSJose E. Roman Input Parameters: 54978d59e960SJed Brown + ts - time stepping context 54988d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 54998d59e960SJed Brown 55008d59e960SJed Brown Note: 55018d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 55028d59e960SJed Brown 55038d59e960SJed Brown Level: intermediate 55048d59e960SJed Brown 5505db781477SPatrick Sanan .seealso: `TSGetCFLTime()`, `TSADAPTCFL` 55068d59e960SJed Brown @*/ 55078d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 55088d59e960SJed Brown { 55098d59e960SJed Brown PetscFunctionBegin; 55108d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 55118d59e960SJed Brown ts->cfltime_local = cfltime; 55128d59e960SJed Brown ts->cfltime = -1.; 55138d59e960SJed Brown PetscFunctionReturn(0); 55148d59e960SJed Brown } 55158d59e960SJed Brown 55168d59e960SJed Brown /*@ 55178d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 55188d59e960SJed Brown 55198d59e960SJed Brown Collective on TS 55208d59e960SJed Brown 55214165533cSJose E. Roman Input Parameter: 55228d59e960SJed Brown . ts - time stepping context 55238d59e960SJed Brown 55244165533cSJose E. Roman Output Parameter: 55258d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 55268d59e960SJed Brown 55278d59e960SJed Brown Level: advanced 55288d59e960SJed Brown 5529db781477SPatrick Sanan .seealso: `TSSetCFLTimeLocal()` 55308d59e960SJed Brown @*/ 55318d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 55328d59e960SJed Brown { 55338d59e960SJed Brown PetscFunctionBegin; 55341e66621cSBarry Smith if (ts->cfltime < 0) PetscCall(MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts))); 55358d59e960SJed Brown *cfltime = ts->cfltime; 55368d59e960SJed Brown PetscFunctionReturn(0); 55378d59e960SJed Brown } 55388d59e960SJed Brown 5539d6ebe24aSShri Abhyankar /*@ 5540d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 5541d6ebe24aSShri Abhyankar 5542d6ebe24aSShri Abhyankar Input Parameters: 5543a2b725a8SWilliam Gropp + ts - the TS context. 5544d6ebe24aSShri Abhyankar . xl - lower bound. 5545a2b725a8SWilliam Gropp - xu - upper bound. 5546d6ebe24aSShri Abhyankar 5547d6ebe24aSShri Abhyankar Notes: 5548d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 5549e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 5550d6ebe24aSShri Abhyankar 55512bd2b0e6SSatish Balay Level: advanced 55522bd2b0e6SSatish Balay 5553d6ebe24aSShri Abhyankar @*/ 5554d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 5555d6ebe24aSShri Abhyankar { 5556d6ebe24aSShri Abhyankar SNES snes; 5557d6ebe24aSShri Abhyankar 5558d6ebe24aSShri Abhyankar PetscFunctionBegin; 55599566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 55609566063dSJacob Faibussowitsch PetscCall(SNESVISetVariableBounds(snes,xl,xu)); 5561d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 5562d6ebe24aSShri Abhyankar } 5563d6ebe24aSShri Abhyankar 5564f9c1d6abSBarry Smith /*@ 5565f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 5566f9c1d6abSBarry Smith 5567d083f849SBarry Smith Collective on TS 5568f9c1d6abSBarry Smith 5569f9c1d6abSBarry Smith Input Parameters: 5570f9c1d6abSBarry Smith + ts - the TS context 5571f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 5572f9c1d6abSBarry Smith 5573f9c1d6abSBarry Smith Output Parameters: 5574f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 5575f9c1d6abSBarry Smith 5576f9c1d6abSBarry Smith Level: developer 5577f9c1d6abSBarry Smith 5578db781477SPatrick Sanan .seealso: `TSSetRHSFunction()`, `TSComputeIFunction()` 5579f9c1d6abSBarry Smith @*/ 5580f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 5581f9c1d6abSBarry Smith { 5582f9c1d6abSBarry Smith PetscFunctionBegin; 5583f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 55843c633725SBarry Smith PetscCheck(ts->ops->linearstability,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 55859566063dSJacob Faibussowitsch PetscCall((*ts->ops->linearstability)(ts,xr,xi,yr,yi)); 5586f9c1d6abSBarry Smith PetscFunctionReturn(0); 5587f9c1d6abSBarry Smith } 558824655328SShri 558924655328SShri /*@ 5590dcb233daSLisandro Dalcin TSRestartStep - Flags the solver to restart the next step 5591dcb233daSLisandro Dalcin 5592dcb233daSLisandro Dalcin Collective on TS 5593dcb233daSLisandro Dalcin 5594dcb233daSLisandro Dalcin Input Parameter: 5595dcb233daSLisandro Dalcin . ts - the TS context obtained from TSCreate() 5596dcb233daSLisandro Dalcin 5597dcb233daSLisandro Dalcin Level: advanced 5598dcb233daSLisandro Dalcin 5599dcb233daSLisandro Dalcin Notes: 5600dcb233daSLisandro Dalcin Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of 5601dcb233daSLisandro Dalcin discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution 5602dcb233daSLisandro Dalcin vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For 5603dcb233daSLisandro Dalcin the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce 5604dcb233daSLisandro Dalcin discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with 5605dcb233daSLisandro Dalcin discontinuous source terms). 5606dcb233daSLisandro Dalcin 5607db781477SPatrick Sanan .seealso: `TSSolve()`, `TSSetPreStep()`, `TSSetPostStep()` 5608dcb233daSLisandro Dalcin @*/ 5609dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts) 5610dcb233daSLisandro Dalcin { 5611dcb233daSLisandro Dalcin PetscFunctionBegin; 5612dcb233daSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5613dcb233daSLisandro Dalcin ts->steprestart = PETSC_TRUE; 5614dcb233daSLisandro Dalcin PetscFunctionReturn(0); 5615dcb233daSLisandro Dalcin } 5616dcb233daSLisandro Dalcin 5617dcb233daSLisandro Dalcin /*@ 561824655328SShri TSRollBack - Rolls back one time step 561924655328SShri 562024655328SShri Collective on TS 562124655328SShri 562224655328SShri Input Parameter: 562324655328SShri . ts - the TS context obtained from TSCreate() 562424655328SShri 562524655328SShri Level: advanced 562624655328SShri 5627db781477SPatrick Sanan .seealso: `TSCreate()`, `TSSetUp()`, `TSDestroy()`, `TSSolve()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSInterpolate()` 562824655328SShri @*/ 562924655328SShri PetscErrorCode TSRollBack(TS ts) 563024655328SShri { 563124655328SShri PetscFunctionBegin; 563224655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 56333c633725SBarry Smith PetscCheck(!ts->steprollback,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called"); 56343c633725SBarry Smith PetscCheck(ts->ops->rollback,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 56359566063dSJacob Faibussowitsch PetscCall((*ts->ops->rollback)(ts)); 563624655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 563724655328SShri ts->ptime = ts->ptime_prev; 5638be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime_prev_rollback; 56392808aa04SLisandro Dalcin ts->steps--; 5640b3de5cdeSLisandro Dalcin ts->steprollback = PETSC_TRUE; 564124655328SShri PetscFunctionReturn(0); 564224655328SShri } 5643aeb4809dSShri Abhyankar 5644ff22ae23SHong Zhang /*@ 5645ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 5646ff22ae23SHong Zhang 5647ff22ae23SHong Zhang Input Parameter: 5648ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 5649ff22ae23SHong Zhang 56500429704eSStefano Zampini Output Parameters: 56510429704eSStefano Zampini + ns - the number of stages 56520429704eSStefano Zampini - Y - the current stage vectors 56530429704eSStefano Zampini 5654ff22ae23SHong Zhang Level: advanced 5655ff22ae23SHong Zhang 56560429704eSStefano Zampini Notes: Both ns and Y can be NULL. 56570429704eSStefano Zampini 5658db781477SPatrick Sanan .seealso: `TSCreate()` 5659ff22ae23SHong Zhang @*/ 5660ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns,Vec **Y) 5661ff22ae23SHong Zhang { 5662ff22ae23SHong Zhang PetscFunctionBegin; 5663ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 5664dadcf809SJacob Faibussowitsch if (ns) PetscValidIntPointer(ns,2); 56650429704eSStefano Zampini if (Y) PetscValidPointer(Y,3); 56660429704eSStefano Zampini if (!ts->ops->getstages) { 56670429704eSStefano Zampini if (ns) *ns = 0; 56680429704eSStefano Zampini if (Y) *Y = NULL; 56691e66621cSBarry Smith } else PetscCall((*ts->ops->getstages)(ts,ns,Y)); 5670ff22ae23SHong Zhang PetscFunctionReturn(0); 5671ff22ae23SHong Zhang } 5672ff22ae23SHong Zhang 5673847ff0e1SMatthew G. Knepley /*@C 5674847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 5675847ff0e1SMatthew G. Knepley 5676847ff0e1SMatthew G. Knepley Collective on SNES 5677847ff0e1SMatthew G. Knepley 5678847ff0e1SMatthew G. Knepley Input Parameters: 5679847ff0e1SMatthew G. Knepley + ts - the TS context 5680847ff0e1SMatthew G. Knepley . t - current timestep 5681847ff0e1SMatthew G. Knepley . U - state vector 5682847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 5683847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 5684847ff0e1SMatthew G. Knepley - ctx - an optional user context 5685847ff0e1SMatthew G. Knepley 5686847ff0e1SMatthew G. Knepley Output Parameters: 5687847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 5688847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 5689847ff0e1SMatthew G. Knepley 5690847ff0e1SMatthew G. Knepley Level: intermediate 5691847ff0e1SMatthew G. Knepley 5692847ff0e1SMatthew G. Knepley Notes: 5693847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 5694847ff0e1SMatthew G. Knepley 5695847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 5696847ff0e1SMatthew G. Knepley 5697847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 5698847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 5699847ff0e1SMatthew G. Knepley 5700847ff0e1SMatthew G. Knepley This will first try to get the coloring from the DM. If the DM type has no coloring 5701847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 5702847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 5703847ff0e1SMatthew G. Knepley 5704db781477SPatrick Sanan .seealso: `TSSetIJacobian()`, `MatFDColoringCreate()`, `MatFDColoringSetFunction()` 5705847ff0e1SMatthew G. Knepley @*/ 5706847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx) 5707847ff0e1SMatthew G. Knepley { 5708847ff0e1SMatthew G. Knepley SNES snes; 5709847ff0e1SMatthew G. Knepley MatFDColoring color; 5710847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 5711847ff0e1SMatthew G. Knepley 5712847ff0e1SMatthew G. Knepley PetscFunctionBegin; 57139566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL)); 57149566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color)); 5715847ff0e1SMatthew G. Knepley if (!color) { 5716847ff0e1SMatthew G. Knepley DM dm; 5717847ff0e1SMatthew G. Knepley ISColoring iscoloring; 5718847ff0e1SMatthew G. Knepley 57199566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 57209566063dSJacob Faibussowitsch PetscCall(DMHasColoring(dm, &hascolor)); 5721847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 57229566063dSJacob Faibussowitsch PetscCall(DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring)); 57239566063dSJacob Faibussowitsch PetscCall(MatFDColoringCreate(B, iscoloring, &color)); 57249566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts)); 57259566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFromOptions(color)); 57269566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetUp(B, iscoloring, color)); 57279566063dSJacob Faibussowitsch PetscCall(ISColoringDestroy(&iscoloring)); 5728847ff0e1SMatthew G. Knepley } else { 5729847ff0e1SMatthew G. Knepley MatColoring mc; 5730847ff0e1SMatthew G. Knepley 57319566063dSJacob Faibussowitsch PetscCall(MatColoringCreate(B, &mc)); 57329566063dSJacob Faibussowitsch PetscCall(MatColoringSetDistance(mc, 2)); 57339566063dSJacob Faibussowitsch PetscCall(MatColoringSetType(mc, MATCOLORINGSL)); 57349566063dSJacob Faibussowitsch PetscCall(MatColoringSetFromOptions(mc)); 57359566063dSJacob Faibussowitsch PetscCall(MatColoringApply(mc, &iscoloring)); 57369566063dSJacob Faibussowitsch PetscCall(MatColoringDestroy(&mc)); 57379566063dSJacob Faibussowitsch PetscCall(MatFDColoringCreate(B, iscoloring, &color)); 57389566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts)); 57399566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFromOptions(color)); 57409566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetUp(B, iscoloring, color)); 57419566063dSJacob Faibussowitsch PetscCall(ISColoringDestroy(&iscoloring)); 5742847ff0e1SMatthew G. Knepley } 57439566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color)); 57449566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject) color)); 5745847ff0e1SMatthew G. Knepley } 57469566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 57479566063dSJacob Faibussowitsch PetscCall(MatFDColoringApply(B, color, U, snes)); 5748847ff0e1SMatthew G. Knepley if (J != B) { 57499566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 57509566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 5751847ff0e1SMatthew G. Knepley } 5752847ff0e1SMatthew G. Knepley PetscFunctionReturn(0); 5753847ff0e1SMatthew G. Knepley } 575493b34091SDebojyoti Ghosh 5755cb9d8021SPierre Barbier de Reuille /*@ 57566bc98fa9SBarry Smith TSSetFunctionDomainError - Set a function that tests if the current state vector is valid 5757cb9d8021SPierre Barbier de Reuille 5758cb9d8021SPierre Barbier de Reuille Input Parameters: 57596bc98fa9SBarry Smith + ts - the TS context 57606bc98fa9SBarry Smith - func - function called within TSFunctionDomainError 57616bc98fa9SBarry Smith 57626bc98fa9SBarry Smith Calling sequence of func: 57636bc98fa9SBarry Smith $ PetscErrorCode func(TS ts,PetscReal time,Vec state,PetscBool reject) 57646bc98fa9SBarry Smith 57656bc98fa9SBarry Smith + ts - the TS context 57666bc98fa9SBarry Smith . time - the current time (of the stage) 57676bc98fa9SBarry Smith . state - the state to check if it is valid 57686bc98fa9SBarry Smith - reject - (output parameter) PETSC_FALSE if the state is acceptable, PETSC_TRUE if not acceptable 5769cb9d8021SPierre Barbier de Reuille 5770cb9d8021SPierre Barbier de Reuille Level: intermediate 5771cb9d8021SPierre Barbier de Reuille 57726bc98fa9SBarry Smith Notes: 57736bc98fa9SBarry Smith If an implicit ODE solver is being used then, in addition to providing this routine, the 57746bc98fa9SBarry Smith user's code should call SNESSetFunctionDomainError() when domain errors occur during 57756bc98fa9SBarry Smith function evaluations where the functions are provided by TSSetIFunction() or TSSetRHSFunction(). 57766bc98fa9SBarry Smith Use TSGetSNES() to obtain the SNES object 57776bc98fa9SBarry Smith 57786bc98fa9SBarry Smith Developer Notes: 57796bc98fa9SBarry Smith The naming of this function is inconsistent with the SNESSetFunctionDomainError() 57806bc98fa9SBarry Smith since one takes a function pointer and the other does not. 57816bc98fa9SBarry Smith 5782db781477SPatrick Sanan .seealso: `TSAdaptCheckStage()`, `TSFunctionDomainError()`, `SNESSetFunctionDomainError()`, `TSGetSNES()` 5783cb9d8021SPierre Barbier de Reuille @*/ 5784cb9d8021SPierre Barbier de Reuille 5785d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*)) 5786cb9d8021SPierre Barbier de Reuille { 5787cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 5788cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID,1); 5789cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 5790cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 5791cb9d8021SPierre Barbier de Reuille } 5792cb9d8021SPierre Barbier de Reuille 5793cb9d8021SPierre Barbier de Reuille /*@ 57946bc98fa9SBarry Smith TSFunctionDomainError - Checks if the current state is valid 5795cb9d8021SPierre Barbier de Reuille 5796cb9d8021SPierre Barbier de Reuille Input Parameters: 57976bc98fa9SBarry Smith + ts - the TS context 57986bc98fa9SBarry Smith . stagetime - time of the simulation 57996bc98fa9SBarry Smith - Y - state vector to check. 5800cb9d8021SPierre Barbier de Reuille 5801cb9d8021SPierre Barbier de Reuille Output Parameter: 58026bc98fa9SBarry Smith . accept - Set to PETSC_FALSE if the current state vector is valid. 5803cb9d8021SPierre Barbier de Reuille 5804cb9d8021SPierre Barbier de Reuille Note: 58056bc98fa9SBarry Smith This function is called by the TS integration routines and calls the user provided function (set with TSSetFunctionDomainError()) 58066bc98fa9SBarry Smith to check if the current state is valid. 580796a0c994SBarry Smith 58086bc98fa9SBarry Smith Level: developer 58096bc98fa9SBarry Smith 5810db781477SPatrick Sanan .seealso: `TSSetFunctionDomainError()` 5811cb9d8021SPierre Barbier de Reuille @*/ 5812d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept) 5813cb9d8021SPierre Barbier de Reuille { 5814cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 5815cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5816cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 58171e66621cSBarry Smith if (ts->functiondomainerror) PetscCall((*ts->functiondomainerror)(ts,stagetime,Y,accept)); 5818cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 5819cb9d8021SPierre Barbier de Reuille } 58201ceb14c0SBarry Smith 582193b34091SDebojyoti Ghosh /*@C 5822e5168f73SEmil Constantinescu TSClone - This function clones a time step object. 582393b34091SDebojyoti Ghosh 5824d083f849SBarry Smith Collective 582593b34091SDebojyoti Ghosh 582693b34091SDebojyoti Ghosh Input Parameter: 582793b34091SDebojyoti Ghosh . tsin - The input TS 582893b34091SDebojyoti Ghosh 582993b34091SDebojyoti Ghosh Output Parameter: 5830e5168f73SEmil Constantinescu . tsout - The output TS (cloned) 583193b34091SDebojyoti Ghosh 58325eca1a21SEmil Constantinescu Notes: 58335eca1a21SEmil 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. 58345eca1a21SEmil Constantinescu 5835928bb9adSStefano 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); 58365eca1a21SEmil Constantinescu 58375eca1a21SEmil Constantinescu Level: developer 583893b34091SDebojyoti Ghosh 5839db781477SPatrick Sanan .seealso: `TSCreate()`, `TSSetType()`, `TSSetUp()`, `TSDestroy()`, `TSSetProblemType()` 584093b34091SDebojyoti Ghosh @*/ 5841baa10174SEmil Constantinescu PetscErrorCode TSClone(TS tsin, TS *tsout) 584293b34091SDebojyoti Ghosh { 584393b34091SDebojyoti Ghosh TS t; 5844dc846ba4SSatish Balay SNES snes_start; 5845dc846ba4SSatish Balay DM dm; 5846dc846ba4SSatish Balay TSType type; 584793b34091SDebojyoti Ghosh 584893b34091SDebojyoti Ghosh PetscFunctionBegin; 584993b34091SDebojyoti Ghosh PetscValidPointer(tsin,1); 585093b34091SDebojyoti Ghosh *tsout = NULL; 585193b34091SDebojyoti Ghosh 58529566063dSJacob Faibussowitsch PetscCall(PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView)); 585393b34091SDebojyoti Ghosh 585493b34091SDebojyoti Ghosh /* General TS description */ 585593b34091SDebojyoti Ghosh t->numbermonitors = 0; 5856d0c080abSJoseph Pusztay t->monitorFrequency = 1; 585793b34091SDebojyoti Ghosh t->setupcalled = 0; 585893b34091SDebojyoti Ghosh t->ksp_its = 0; 585993b34091SDebojyoti Ghosh t->snes_its = 0; 586093b34091SDebojyoti Ghosh t->nwork = 0; 58617d51462cSStefano Zampini t->rhsjacobian.time = PETSC_MIN_REAL; 586293b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 586393b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 586493b34091SDebojyoti Ghosh 58659566063dSJacob Faibussowitsch PetscCall(TSGetSNES(tsin,&snes_start)); 58669566063dSJacob Faibussowitsch PetscCall(TSSetSNES(t,snes_start)); 5867d15a3a53SEmil Constantinescu 58689566063dSJacob Faibussowitsch PetscCall(TSGetDM(tsin,&dm)); 58699566063dSJacob Faibussowitsch PetscCall(TSSetDM(t,dm)); 587093b34091SDebojyoti Ghosh 587193b34091SDebojyoti Ghosh t->adapt = tsin->adapt; 58729566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)t->adapt)); 587393b34091SDebojyoti Ghosh 5874e7069c78SShri t->trajectory = tsin->trajectory; 58759566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)t->trajectory)); 5876e7069c78SShri 5877e7069c78SShri t->event = tsin->event; 58786b10a48eSSatish Balay if (t->event) t->event->refct++; 5879e7069c78SShri 588093b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 588193b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 5882e7069c78SShri t->ptime_prev = tsin->ptime_prev; 588393b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 588493b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 588593b34091SDebojyoti Ghosh t->steps = tsin->steps; 588693b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 588793b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 588893b34091SDebojyoti Ghosh t->atol = tsin->atol; 588993b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 589093b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 589193b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 589293b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 589393b34091SDebojyoti Ghosh 58949566063dSJacob Faibussowitsch PetscCall(TSGetType(tsin,&type)); 58959566063dSJacob Faibussowitsch PetscCall(TSSetType(t,type)); 589693b34091SDebojyoti Ghosh 589793b34091SDebojyoti Ghosh t->vec_sol = NULL; 589893b34091SDebojyoti Ghosh 589993b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 590093b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 590193b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 590293b34091SDebojyoti Ghosh 59039566063dSJacob Faibussowitsch PetscCall(PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps))); 590493b34091SDebojyoti Ghosh 59050d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 59060d4fed19SBarry Smith PetscInt i; 59079566063dSJacob Faibussowitsch PetscCall(PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers)); 59080d4fed19SBarry Smith for (i=0; i<10; i++) { 59090d4fed19SBarry Smith ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 59100d4fed19SBarry Smith } 59110d4fed19SBarry Smith } 591293b34091SDebojyoti Ghosh *tsout = t; 591393b34091SDebojyoti Ghosh PetscFunctionReturn(0); 591493b34091SDebojyoti Ghosh } 5915f3b1f45cSBarry Smith 5916f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y) 5917f3b1f45cSBarry Smith { 5918f3b1f45cSBarry Smith TS ts = (TS) ctx; 5919f3b1f45cSBarry Smith 5920f3b1f45cSBarry Smith PetscFunctionBegin; 59219566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts,0,x,y)); 5922f3b1f45cSBarry Smith PetscFunctionReturn(0); 5923f3b1f45cSBarry Smith } 5924f3b1f45cSBarry Smith 5925f3b1f45cSBarry Smith /*@ 5926f3b1f45cSBarry Smith TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function. 5927f3b1f45cSBarry Smith 5928d083f849SBarry Smith Logically Collective on TS 5929f3b1f45cSBarry Smith 5930f3b1f45cSBarry Smith Input Parameters: 5931f3b1f45cSBarry Smith TS - the time stepping routine 5932f3b1f45cSBarry Smith 5933f3b1f45cSBarry Smith Output Parameter: 5934f3b1f45cSBarry Smith . flg - PETSC_TRUE if the multiply is likely correct 5935f3b1f45cSBarry Smith 5936f3b1f45cSBarry Smith Options Database: 5937f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator 5938f3b1f45cSBarry Smith 5939f3b1f45cSBarry Smith Level: advanced 5940f3b1f45cSBarry Smith 594195452b02SPatrick Sanan Notes: 594295452b02SPatrick Sanan This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian 5943f3b1f45cSBarry Smith 5944db781477SPatrick Sanan .seealso: `MatCreateShell()`, `MatShellGetContext()`, `MatShellGetOperation()`, `MatShellTestMultTranspose()`, `TSRHSJacobianTestTranspose()` 5945f3b1f45cSBarry Smith @*/ 5946f3b1f45cSBarry Smith PetscErrorCode TSRHSJacobianTest(TS ts,PetscBool *flg) 5947f3b1f45cSBarry Smith { 5948f3b1f45cSBarry Smith Mat J,B; 5949f3b1f45cSBarry Smith TSRHSJacobian func; 5950f3b1f45cSBarry Smith void* ctx; 5951f3b1f45cSBarry Smith 5952f3b1f45cSBarry Smith PetscFunctionBegin; 59539566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts,&J,&B,&func,&ctx)); 59549566063dSJacob Faibussowitsch PetscCall((*func)(ts,0.0,ts->vec_sol,J,B,ctx)); 59559566063dSJacob Faibussowitsch PetscCall(MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg)); 5956f3b1f45cSBarry Smith PetscFunctionReturn(0); 5957f3b1f45cSBarry Smith } 5958f3b1f45cSBarry Smith 5959f3b1f45cSBarry Smith /*@C 5960f3b1f45cSBarry Smith TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function. 5961f3b1f45cSBarry Smith 5962d083f849SBarry Smith Logically Collective on TS 5963f3b1f45cSBarry Smith 5964f3b1f45cSBarry Smith Input Parameters: 5965f3b1f45cSBarry Smith TS - the time stepping routine 5966f3b1f45cSBarry Smith 5967f3b1f45cSBarry Smith Output Parameter: 5968f3b1f45cSBarry Smith . flg - PETSC_TRUE if the multiply is likely correct 5969f3b1f45cSBarry Smith 5970f3b1f45cSBarry Smith Options Database: 5971f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator 5972f3b1f45cSBarry Smith 597395452b02SPatrick Sanan Notes: 597495452b02SPatrick Sanan This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian 5975f3b1f45cSBarry Smith 5976f3b1f45cSBarry Smith Level: advanced 5977f3b1f45cSBarry Smith 5978db781477SPatrick Sanan .seealso: `MatCreateShell()`, `MatShellGetContext()`, `MatShellGetOperation()`, `MatShellTestMultTranspose()`, `TSRHSJacobianTest()` 5979f3b1f45cSBarry Smith @*/ 5980f3b1f45cSBarry Smith PetscErrorCode TSRHSJacobianTestTranspose(TS ts,PetscBool *flg) 5981f3b1f45cSBarry Smith { 5982f3b1f45cSBarry Smith Mat J,B; 5983f3b1f45cSBarry Smith void *ctx; 5984f3b1f45cSBarry Smith TSRHSJacobian func; 5985f3b1f45cSBarry Smith 5986f3b1f45cSBarry Smith PetscFunctionBegin; 59879566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts,&J,&B,&func,&ctx)); 59889566063dSJacob Faibussowitsch PetscCall((*func)(ts,0.0,ts->vec_sol,J,B,ctx)); 59899566063dSJacob Faibussowitsch PetscCall(MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg)); 5990f3b1f45cSBarry Smith PetscFunctionReturn(0); 5991f3b1f45cSBarry Smith } 59920fe4d17eSHong Zhang 59930fe4d17eSHong Zhang /*@ 59940fe4d17eSHong Zhang TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used. 59950fe4d17eSHong Zhang 59960fe4d17eSHong Zhang Logically collective 59970fe4d17eSHong Zhang 5998d8d19677SJose E. Roman Input Parameters: 59990fe4d17eSHong Zhang + ts - timestepping context 60000fe4d17eSHong Zhang - use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used 60010fe4d17eSHong Zhang 60020fe4d17eSHong Zhang Options Database: 60030fe4d17eSHong Zhang . -ts_use_splitrhsfunction - <true,false> 60040fe4d17eSHong Zhang 60050fe4d17eSHong Zhang Notes: 60060fe4d17eSHong Zhang This is only useful for multirate methods 60070fe4d17eSHong Zhang 60080fe4d17eSHong Zhang Level: intermediate 60090fe4d17eSHong Zhang 6010db781477SPatrick Sanan .seealso: `TSGetUseSplitRHSFunction()` 60110fe4d17eSHong Zhang @*/ 60120fe4d17eSHong Zhang PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction) 60130fe4d17eSHong Zhang { 60140fe4d17eSHong Zhang PetscFunctionBegin; 60150fe4d17eSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 60160fe4d17eSHong Zhang ts->use_splitrhsfunction = use_splitrhsfunction; 60170fe4d17eSHong Zhang PetscFunctionReturn(0); 60180fe4d17eSHong Zhang } 60190fe4d17eSHong Zhang 60200fe4d17eSHong Zhang /*@ 60210fe4d17eSHong Zhang TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used. 60220fe4d17eSHong Zhang 60230fe4d17eSHong Zhang Not collective 60240fe4d17eSHong Zhang 60250fe4d17eSHong Zhang Input Parameter: 60260fe4d17eSHong Zhang . ts - timestepping context 60270fe4d17eSHong Zhang 60280fe4d17eSHong Zhang Output Parameter: 60290fe4d17eSHong Zhang . use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used 60300fe4d17eSHong Zhang 60310fe4d17eSHong Zhang Level: intermediate 60320fe4d17eSHong Zhang 6033db781477SPatrick Sanan .seealso: `TSSetUseSplitRHSFunction()` 60340fe4d17eSHong Zhang @*/ 60350fe4d17eSHong Zhang PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction) 60360fe4d17eSHong Zhang { 60370fe4d17eSHong Zhang PetscFunctionBegin; 60380fe4d17eSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 60390fe4d17eSHong Zhang *use_splitrhsfunction = ts->use_splitrhsfunction; 60400fe4d17eSHong Zhang PetscFunctionReturn(0); 60410fe4d17eSHong Zhang } 6042d60b7d5cSBarry Smith 6043d60b7d5cSBarry Smith /*@ 6044d60b7d5cSBarry Smith TSSetMatStructure - sets the relationship between the nonzero structure of the RHS Jacobian matrix to the IJacobian matrix. 6045d60b7d5cSBarry Smith 6046d60b7d5cSBarry Smith Logically Collective on ts 6047d60b7d5cSBarry Smith 6048d60b7d5cSBarry Smith Input Parameters: 6049d60b7d5cSBarry Smith + ts - the time-stepper 6050d60b7d5cSBarry Smith - str - the structure (the default is UNKNOWN_NONZERO_PATTERN) 6051d60b7d5cSBarry Smith 6052d60b7d5cSBarry Smith Level: intermediate 6053d60b7d5cSBarry Smith 6054d60b7d5cSBarry Smith Notes: 6055d60b7d5cSBarry Smith When the relationship between the nonzero structures is known and supplied the solution process can be much faster 6056d60b7d5cSBarry Smith 6057db781477SPatrick Sanan .seealso: `MatAXPY()`, `MatStructure` 6058d60b7d5cSBarry Smith @*/ 6059d60b7d5cSBarry Smith PetscErrorCode TSSetMatStructure(TS ts,MatStructure str) 6060d60b7d5cSBarry Smith { 6061d60b7d5cSBarry Smith PetscFunctionBegin; 6062d60b7d5cSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6063d60b7d5cSBarry Smith ts->axpy_pattern = str; 6064d60b7d5cSBarry Smith PetscFunctionReturn(0); 6065d60b7d5cSBarry Smith } 60664a658b32SHong Zhang 60674a658b32SHong Zhang /*@ 60684a658b32SHong Zhang TSSetTimeSpan - sets the time span. The solution will be computed and stored for each time requested. 60694a658b32SHong Zhang 60704a658b32SHong Zhang Collective on ts 60714a658b32SHong Zhang 60724a658b32SHong Zhang Input Parameters: 60734a658b32SHong Zhang + ts - the time-stepper 60744a658b32SHong Zhang . n - number of the time points (>=2) 60754a658b32SHong Zhang - span_times - array of the time points. The first element and the last element are the initial time and the final time respectively. 60764a658b32SHong Zhang 60774a658b32SHong Zhang Options Database Keys: 60784a658b32SHong Zhang . -ts_time_span <t0,...tf> - Sets the time span 60794a658b32SHong Zhang 60804a658b32SHong Zhang Level: beginner 60814a658b32SHong Zhang 60824a658b32SHong Zhang Notes: 60834a658b32SHong Zhang The elements in tspan must be all increasing. They correspond to the intermediate points for time integration. 60844a658b32SHong Zhang TS_EXACTFINALTIME_MATCHSTEP must be used to make the last time step in each sub-interval match the intermediate points specified. 60854a658b32SHong Zhang The intermediate solutions are saved in a vector array that can be accessed with TSGetSolutions(). Thus using time span may 60864a658b32SHong Zhang pressure the memory system when using a large number of span points. 60874a658b32SHong Zhang 6088c2e3fba1SPatrick Sanan .seealso: `TSGetTimeSpan()`, `TSGetSolutions()` 60894a658b32SHong Zhang @*/ 60904a658b32SHong Zhang PetscErrorCode TSSetTimeSpan(TS ts,PetscInt n,PetscReal *span_times) 60914a658b32SHong Zhang { 60924a658b32SHong Zhang PetscFunctionBegin; 60934a658b32SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 609463a3b9bcSJacob Faibussowitsch PetscCheck(n >= 2,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Minimum time span size is 2 but %" PetscInt_FMT " is provided",n); 60954a658b32SHong Zhang if (ts->tspan && n != ts->tspan->num_span_times) { 60964a658b32SHong Zhang PetscCall(PetscFree(ts->tspan->span_times)); 60974a658b32SHong Zhang PetscCall(VecDestroyVecs(ts->tspan->num_span_times,&ts->tspan->vecs_sol)); 60984a658b32SHong Zhang PetscCall(PetscMalloc1(n,&ts->tspan->span_times)); 60994a658b32SHong Zhang } 61004a658b32SHong Zhang if (!ts->tspan) { 61014a658b32SHong Zhang TSTimeSpan tspan; 61024a658b32SHong Zhang PetscCall(PetscNew(&tspan)); 61034a658b32SHong Zhang PetscCall(PetscMalloc1(n,&tspan->span_times)); 6104e1db57b0SHong Zhang tspan->reltol = 1e-6; 6105e1db57b0SHong Zhang tspan->abstol = 10*PETSC_MACHINE_EPSILON; 61064a658b32SHong Zhang ts->tspan = tspan; 61074a658b32SHong Zhang } 61084a658b32SHong Zhang ts->tspan->num_span_times = n; 61094a658b32SHong Zhang PetscCall(PetscArraycpy(ts->tspan->span_times,span_times,n)); 61104a658b32SHong Zhang PetscCall(TSSetTime(ts,ts->tspan->span_times[0])); 61114a658b32SHong Zhang PetscCall(TSSetMaxTime(ts,ts->tspan->span_times[n-1])); 61124a658b32SHong Zhang PetscFunctionReturn(0); 61134a658b32SHong Zhang } 61144a658b32SHong Zhang 61154a658b32SHong Zhang /*@C 61164a658b32SHong Zhang TSGetTimeSpan - gets the time span. 61174a658b32SHong Zhang 61184a658b32SHong Zhang Not Collective 61194a658b32SHong Zhang 61204a658b32SHong Zhang Input Parameter: 61214a658b32SHong Zhang . ts - the time-stepper 61224a658b32SHong Zhang 61234a658b32SHong Zhang Output Parameters: 61244a658b32SHong Zhang + n - number of the time points (>=2) 61254a658b32SHong 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. 61264a658b32SHong Zhang 61274a658b32SHong Zhang Level: beginner 61284a658b32SHong Zhang Notes: Both n and span_times can be NULL. 61294a658b32SHong Zhang 6130c2e3fba1SPatrick Sanan .seealso: `TSSetTimeSpan()`, `TSGetSolutions()` 61314a658b32SHong Zhang @*/ 61324a658b32SHong Zhang PetscErrorCode TSGetTimeSpan(TS ts,PetscInt *n,const PetscReal **span_times) 61334a658b32SHong Zhang { 61344a658b32SHong Zhang PetscFunctionBegin; 61354a658b32SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 61364a658b32SHong Zhang if (n) PetscValidIntPointer(n,2); 61374a658b32SHong Zhang if (span_times) PetscValidPointer(span_times,3); 61384a658b32SHong Zhang if (!ts->tspan) { 61394a658b32SHong Zhang if (n) *n = 0; 61404a658b32SHong Zhang if (span_times) *span_times = NULL; 61414a658b32SHong Zhang } else { 61424a658b32SHong Zhang if (n) *n = ts->tspan->num_span_times; 61434a658b32SHong Zhang if (span_times) *span_times = ts->tspan->span_times; 61444a658b32SHong Zhang } 61454a658b32SHong Zhang PetscFunctionReturn(0); 61464a658b32SHong Zhang } 61474a658b32SHong Zhang 61484a658b32SHong Zhang /*@ 61494a658b32SHong Zhang TSGetTimeSpanSolutions - Get the number of solutions and the solutions at the time points specified by the time span. 61504a658b32SHong Zhang 61514a658b32SHong Zhang Input Parameter: 61524a658b32SHong Zhang . ts - the TS context obtained from TSCreate() 61534a658b32SHong Zhang 61544a658b32SHong Zhang Output Parameters: 61554a658b32SHong Zhang + nsol - the number of solutions 61564a658b32SHong Zhang - Sols - the solution vectors 61574a658b32SHong Zhang 61584a658b32SHong Zhang Level: beginner 61594a658b32SHong Zhang 616040bd4cedSHong Zhang Notes: 616140bd4cedSHong Zhang Both nsol and Sols can be NULL. 616240bd4cedSHong Zhang Some time points in the time span may be skipped by TS so that nsol is less than the number of points specified by TSSetTimeSpan(). For example, manipulating the step size, especially with a reduced precision, may cause TS to step over certain points in the span. 61634a658b32SHong Zhang 6164db781477SPatrick Sanan .seealso: `TSSetTimeSpan()` 61654a658b32SHong Zhang @*/ 61664a658b32SHong Zhang PetscErrorCode TSGetTimeSpanSolutions(TS ts,PetscInt *nsol,Vec **Sols) 61674a658b32SHong Zhang { 61684a658b32SHong Zhang PetscFunctionBegin; 61694a658b32SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 61704a658b32SHong Zhang if (nsol) PetscValidIntPointer(nsol,2); 61714a658b32SHong Zhang if (Sols) PetscValidPointer(Sols,3); 61724a658b32SHong Zhang if (!ts->tspan) { 61734a658b32SHong Zhang if (nsol) *nsol = 0; 61744a658b32SHong Zhang if (Sols) *Sols = NULL; 61754a658b32SHong Zhang } else { 617640bd4cedSHong Zhang if (nsol) *nsol = ts->tspan->spanctr; 61774a658b32SHong Zhang if (Sols) *Sols = ts->tspan->vecs_sol; 61784a658b32SHong Zhang } 61794a658b32SHong Zhang PetscFunctionReturn(0); 61804a658b32SHong Zhang } 6181