1af0996ceSBarry Smith #include <petsc/private/tsimpl.h> /*I "petscts.h" I*/ 2496e6a7aSJed Brown #include <petscdmshell.h> 31e25c274SJed Brown #include <petscdmda.h> 42d5ee99bSBarry Smith #include <petscviewer.h> 52d5ee99bSBarry Smith #include <petscdraw.h> 6900f6b5bSMatthew G. Knepley #include <petscconvest.h> 7d763cef2SBarry Smith 81c167fc2SEmil Constantinescu #define SkipSmallValue(a,b,tol) if (PetscAbsScalar(a)< tol || PetscAbsScalar(b)< tol) continue; 91c167fc2SEmil Constantinescu 10d5ba7fb7SMatthew Knepley /* Logging support */ 11d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 12a05bf03eSHong Zhang PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 13d405a339SMatthew Knepley 14c793f718SLisandro Dalcin const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",NULL}; 1549354f04SShri Abhyankar 162ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type) 172ffb9264SLisandro Dalcin { 182ffb9264SLisandro Dalcin PetscFunctionBegin; 19b92453a8SLisandro Dalcin PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1); 20b92453a8SLisandro Dalcin PetscValidCharPointer(default_type,2); 212ffb9264SLisandro Dalcin if (!((PetscObject)adapt)->type_name) { 229566063dSJacob Faibussowitsch PetscCall(TSAdaptSetType(adapt,default_type)); 232ffb9264SLisandro Dalcin } 242ffb9264SLisandro Dalcin PetscFunctionReturn(0); 252ffb9264SLisandro Dalcin } 262ffb9264SLisandro Dalcin 27bdad233fSMatthew Knepley /*@ 28bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 29bdad233fSMatthew Knepley 30bdad233fSMatthew Knepley Collective on TS 31bdad233fSMatthew Knepley 32bdad233fSMatthew Knepley Input Parameter: 33bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 34bdad233fSMatthew Knepley 35bdad233fSMatthew Knepley Options Database Keys: 36d2567f34SHong Zhang + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE, TSBSYMP, TSIRK 37ef222394SBarry Smith . -ts_save_trajectory - checkpoint the solution at each time-step 38ef85077eSLisandro Dalcin . -ts_max_time <time> - maximum time to compute to 394a658b32SHong Zhang . -ts_time_span <t0,...tf> - sets the time span, solutions are computed and stored for each indicated time 40ef85077eSLisandro Dalcin . -ts_max_steps <steps> - maximum number of time-steps to take 41ef85077eSLisandro Dalcin . -ts_init_time <time> - initial time to start computation 424dc72f7fSBarry Smith . -ts_final_time <time> - final time to compute to (deprecated: use -ts_max_time) 433e4cdcaaSBarry Smith . -ts_dt <dt> - initial time step 441628793fSSatish Balay . -ts_exact_final_time <stepover,interpolate,matchstep> - whether to stop at the exact given final time and how to compute the solution at that time 45a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 46a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 47a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 48a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 4967b8a455SSatish Balay . -ts_atol <atol> - Absolute tolerance for local truncation error 50f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function 51f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - test the Jacobian at each iteration against finite difference with RHS function 5267b8a455SSatish Balay . -ts_adjoint_solve <yes,no> - After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory) 53847ff0e1SMatthew G. Knepley . -ts_fd_color - Use finite differences with coloring to compute IJacobian 54bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 55aee7a9fbSMatthew G. Knepley . -ts_monitor_cancel - Cancel all monitors 56de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 57de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 587cf37e64SBarry Smith . -ts_monitor_error - Monitors norm of error 596934998bSLisandro Dalcin . -ts_monitor_lg_timestep - Monitor timestep size graphically 608b668821SLisandro Dalcin . -ts_monitor_lg_timestep_log - Monitor log timestep size graphically 61de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 62de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 63de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 64de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 653e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 663e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 67fde5950dSBarry Smith . -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep 68e4160dc7SJulian Andrej . -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu) 699e336e28SPatrick Sanan - -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 7053ea634cSHong Zhang 713d5a8a6aSBarry Smith Notes: 723d5a8a6aSBarry Smith See SNESSetFromOptions() and KSPSetFromOptions() for how to control the nonlinear and linear solves used by the time-stepper. 733d5a8a6aSBarry Smith 743d5a8a6aSBarry Smith Certain SNES options get reset for each new nonlinear solver, for example -snes_lag_jacobian <its> and -snes_lag_preconditioner <its>, in order 753d5a8a6aSBarry Smith to retain them over the multiple nonlinear solves that TS uses you mush also provide -snes_lag_jacobian_persists true and 763d5a8a6aSBarry Smith -snes_lag_preconditioner_persists true 773d5a8a6aSBarry Smith 789e336e28SPatrick Sanan Developer Note: 799e336e28SPatrick Sanan We should unify all the -ts_monitor options in the way that -xxx_view has been unified 80bdad233fSMatthew Knepley 81bdad233fSMatthew Knepley Level: beginner 82bdad233fSMatthew Knepley 83a313700dSBarry Smith .seealso: TSGetType() 84bdad233fSMatthew Knepley @*/ 857087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 86bdad233fSMatthew Knepley { 87bc952696SBarry Smith PetscBool opt,flg,tflg; 88eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 894a658b32SHong Zhang PetscReal time_step,tspan[100]; 904a658b32SHong Zhang PetscInt nt = PETSC_STATIC_ARRAY_LENGTH(tspan); 9149354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 92d1212d36SBarry Smith char dir[16]; 93cd11d68dSLisandro Dalcin TSIFunction ifun; 946991f827SBarry Smith const char *defaultType; 956991f827SBarry Smith char typeName[256]; 96bdad233fSMatthew Knepley 97bdad233fSMatthew Knepley PetscFunctionBegin; 980700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 996991f827SBarry Smith 1009566063dSJacob Faibussowitsch PetscCall(TSRegisterAll()); 1019566063dSJacob Faibussowitsch PetscCall(TSGetIFunction(ts,NULL,&ifun,NULL)); 102cd11d68dSLisandro Dalcin 103*d0609cedSBarry Smith PetscObjectOptionsBegin((PetscObject)ts); 1041ef27442SStefano Zampini if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 1051ef27442SStefano Zampini else defaultType = ifun ? TSBEULER : TSEULER; 1069566063dSJacob Faibussowitsch PetscCall(PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt)); 1076991f827SBarry Smith if (opt) { 1089566063dSJacob Faibussowitsch PetscCall(TSSetType(ts,typeName)); 1096991f827SBarry Smith } else { 1109566063dSJacob Faibussowitsch PetscCall(TSSetType(ts,defaultType)); 1116991f827SBarry Smith } 112bdad233fSMatthew Knepley 113bdad233fSMatthew Knepley /* Handle generic TS options */ 1149566063dSJacob Faibussowitsch PetscCall(PetscOptionsDeprecated("-ts_final_time","-ts_max_time","3.10",NULL)); 1159566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL)); 1164a658b32SHong Zhang PetscCall(PetscOptionsRealArray("-ts_time_span","Time span","TSSetTimeSpan",tspan,&nt,&flg)); 1174a658b32SHong Zhang if (flg) PetscCall(TSSetTimeSpan(ts,nt,tspan)); 1189566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL)); 1199566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL)); 1209566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg)); 1219566063dSJacob Faibussowitsch if (flg) PetscCall(TSSetTimeStep(ts,time_step)); 1229566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum("-ts_exact_final_time","Option for handling of final time step","TSSetExactFinalTime",TSExactFinalTimeOptions,(PetscEnum)ts->exact_final_time,(PetscEnum*)&eftopt,&flg)); 1239566063dSJacob Faibussowitsch if (flg) PetscCall(TSSetExactFinalTime(ts,eftopt)); 1249566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,NULL)); 1259566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL)); 1269566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL)); 1279566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL)); 1289566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL)); 129bdad233fSMatthew Knepley 1309566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_rhs_jacobian_test_mult","Test the RHS Jacobian for consistency with RHS at each solve ","None",ts->testjacobian,&ts->testjacobian,NULL)); 1319566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_rhs_jacobian_test_mult_transpose","Test the RHS Jacobian transpose for consistency with RHS at each solve ","None",ts->testjacobiantranspose,&ts->testjacobiantranspose,NULL)); 1329566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_use_splitrhsfunction","Use the split RHS function for multirate solvers ","TSSetUseSplitRHSFunction",ts->use_splitrhsfunction,&ts->use_splitrhsfunction,NULL)); 13356f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 13456f85f32SBarry Smith { 13556f85f32SBarry Smith PetscBool set; 13656f85f32SBarry Smith flg = PETSC_FALSE; 1379566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set)); 13856f85f32SBarry Smith if (set) { 1399566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsSetBlock((PetscObject)ts,flg)); 14056f85f32SBarry Smith } 14156f85f32SBarry Smith } 14256f85f32SBarry Smith #endif 14356f85f32SBarry Smith 144bdad233fSMatthew Knepley /* Monitor options */ 1459566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_frequency", "Number of time steps between monitor output", "TSMonitorSetFrequency", ts->monitorFrequency, &ts->monitorFrequency, NULL)); 1469566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL)); 1479566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts,"-ts_monitor_extreme","Monitor extreme values of the solution","TSMonitorExtreme",TSMonitorExtreme,NULL)); 1489566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL)); 1499566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts,"-ts_dmswarm_monitor_moments","Monitor moments of particle distribution","TSDMSwarmMonitorMoments",TSDMSwarmMonitorMoments,NULL)); 150fde5950dSBarry Smith 1519566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",NULL,monfilename,sizeof(monfilename),&flg)); 1529566063dSJacob Faibussowitsch if (flg) PetscCall(PetscPythonMonitorSet((PetscObject)ts,monfilename)); 1535180491cSLisandro Dalcin 1549566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt)); 155b3603a34SBarry Smith if (opt) { 1563923b477SBarry Smith PetscInt howoften = 1; 157e669de00SBarry Smith DM dm; 158e669de00SBarry Smith PetscBool net; 159b3603a34SBarry Smith 1609566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL)); 1619566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 1629566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)dm,DMNETWORK,&net)); 163e669de00SBarry Smith if (net) { 164e669de00SBarry Smith TSMonitorLGCtxNetwork ctx; 1659566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxNetworkCreate(ts,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx)); 1669566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorLGCtxNetworkSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxNetworkDestroy)); 1679566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_lg_solution_semilogy","Plot the solution with a semi-log axis","",ctx->semilogy,&ctx->semilogy,NULL)); 168e669de00SBarry Smith } else { 169e669de00SBarry Smith TSMonitorLGCtx ctx; 1709566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx)); 1719566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy)); 172bdad233fSMatthew Knepley } 173e669de00SBarry Smith } 1746ba87a44SLisandro Dalcin 1759566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt)); 176ef20d060SBarry Smith if (opt) { 1770b039ecaSBarry Smith TSMonitorLGCtx ctx; 1783923b477SBarry Smith PetscInt howoften = 1; 179ef20d060SBarry Smith 1809566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL)); 1819566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx)); 1829566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy)); 183ef20d060SBarry Smith } 1849566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts,"-ts_monitor_error","View the error at each timestep","TSMonitorError",TSMonitorError,NULL)); 1857cf37e64SBarry Smith 1869566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt)); 1876934998bSLisandro Dalcin if (opt) { 1886934998bSLisandro Dalcin TSMonitorLGCtx ctx; 1896934998bSLisandro Dalcin PetscInt howoften = 1; 1906934998bSLisandro Dalcin 1919566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL)); 1929566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx)); 1939566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy)); 1946934998bSLisandro Dalcin } 1959566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt)); 1968b668821SLisandro Dalcin if (opt) { 1978b668821SLisandro Dalcin TSMonitorLGCtx ctx; 1988b668821SLisandro Dalcin PetscInt howoften = 1; 1998b668821SLisandro Dalcin 2009566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL)); 2019566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx)); 2029566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy)); 2038b668821SLisandro Dalcin ctx->semilogy = PETSC_TRUE; 2048b668821SLisandro Dalcin } 2058b668821SLisandro Dalcin 2069566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt)); 207201da799SBarry Smith if (opt) { 208201da799SBarry Smith TSMonitorLGCtx ctx; 209201da799SBarry Smith PetscInt howoften = 1; 210201da799SBarry Smith 2119566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL)); 2129566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx)); 2139566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy)); 214201da799SBarry Smith } 2159566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt)); 216201da799SBarry Smith if (opt) { 217201da799SBarry Smith TSMonitorLGCtx ctx; 218201da799SBarry Smith PetscInt howoften = 1; 219201da799SBarry Smith 2209566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL)); 2219566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx)); 2229566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy)); 223201da799SBarry Smith } 2249566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt)); 2258189c53fSBarry Smith if (opt) { 2268189c53fSBarry Smith TSMonitorSPEigCtx ctx; 2278189c53fSBarry Smith PetscInt howoften = 1; 2288189c53fSBarry Smith 2299566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL)); 2309566063dSJacob Faibussowitsch PetscCall(TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx)); 2319566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy)); 2328189c53fSBarry Smith } 2339566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_sp_swarm","Display particle phase from the DMSwarm","TSMonitorSPSwarm",&opt)); 2341b575b74SJoseph Pusztay if (opt) { 2351b575b74SJoseph Pusztay TSMonitorSPCtx ctx; 236d7462660SMatthew Knepley PetscInt howoften = 1, retain = 0; 2376a5217c0SMatthew G. Knepley PetscBool phase = PETSC_TRUE, create = PETSC_TRUE; 238d7462660SMatthew Knepley 2396a5217c0SMatthew G. Knepley for (PetscInt i = 0; i < ts->numbermonitors; ++i) if (ts->monitor[i] == TSMonitorSPSwarmSolution) {create = PETSC_FALSE;break;} 2406a5217c0SMatthew G. Knepley if (create) { 2419566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm","Display particles phase from the DMSwarm", "TSMonitorSPSwarm", howoften, &howoften, NULL)); 2429566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm_retain", "Retain n points plotted to show trajectory, -1 for all points", "TSMonitorSPSwarm", retain, &retain, NULL)); 2439566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_sp_swarm_phase", "Plot in phase space rather than coordinate space", "TSMonitorSPSwarm", phase, &phase, NULL)); 2449566063dSJacob Faibussowitsch PetscCall(TSMonitorSPCtxCreate(PetscObjectComm((PetscObject) ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, retain, phase, &ctx)); 2459566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode (*)(void**))TSMonitorSPCtxDestroy)); 2461b575b74SJoseph Pusztay } 2476a5217c0SMatthew G. Knepley } 248ef20d060SBarry Smith opt = PETSC_FALSE; 2499566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt)); 250a7cc72afSBarry Smith if (opt) { 25183a4ac43SBarry Smith TSMonitorDrawCtx ctx; 25283a4ac43SBarry Smith PetscInt howoften = 1; 253a80ad3e0SBarry Smith 2549566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL)); 2559566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx)); 2569566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy)); 257bdad233fSMatthew Knepley } 258fb1732b5SBarry Smith opt = PETSC_FALSE; 2599566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt)); 2602d5ee99bSBarry Smith if (opt) { 2612d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2622d5ee99bSBarry Smith PetscReal bounds[4]; 2632d5ee99bSBarry Smith PetscInt n = 4; 2642d5ee99bSBarry Smith PetscDraw draw; 2656934998bSLisandro Dalcin PetscDrawAxis axis; 2662d5ee99bSBarry Smith 2679566063dSJacob Faibussowitsch PetscCall(PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL)); 2683c633725SBarry Smith PetscCheck(n == 4,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 2699566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx)); 2709566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(ctx->viewer,0,&draw)); 2719566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis)); 2729566063dSJacob Faibussowitsch PetscCall(PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3])); 2739566063dSJacob Faibussowitsch PetscCall(PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2")); 2749566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy)); 2752d5ee99bSBarry Smith } 2762d5ee99bSBarry Smith opt = PETSC_FALSE; 2779566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt)); 2783a471f94SBarry Smith if (opt) { 27983a4ac43SBarry Smith TSMonitorDrawCtx ctx; 28083a4ac43SBarry Smith PetscInt howoften = 1; 2813a471f94SBarry Smith 2829566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL)); 2839566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx)); 2849566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy)); 2853a471f94SBarry Smith } 2860ed3bfb6SBarry Smith opt = PETSC_FALSE; 2879566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt)); 2880ed3bfb6SBarry Smith if (opt) { 2890ed3bfb6SBarry Smith TSMonitorDrawCtx ctx; 2900ed3bfb6SBarry Smith PetscInt howoften = 1; 2910ed3bfb6SBarry Smith 2929566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL)); 2939566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx)); 2949566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy)); 2950ed3bfb6SBarry Smith } 296fde5950dSBarry Smith 297ed81e22dSJed Brown opt = PETSC_FALSE; 2989566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",NULL,monfilename,sizeof(monfilename),&flg)); 299ed81e22dSJed Brown if (flg) { 300ed81e22dSJed Brown const char *ptr,*ptr2; 301ed81e22dSJed Brown char *filetemplate; 3023c633725SBarry Smith PetscCheck(monfilename[0],PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 303ed81e22dSJed Brown /* Do some cursory validation of the input. */ 3049566063dSJacob Faibussowitsch PetscCall(PetscStrstr(monfilename,"%",(char**)&ptr)); 3053c633725SBarry Smith PetscCheck(ptr,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 306ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 3079566063dSJacob Faibussowitsch PetscCall(PetscStrchr("DdiouxX",*ptr,(char**)&ptr2)); 3083c633725SBarry Smith 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-%%03D.vts"); 309ed81e22dSJed Brown if (ptr2) break; 310ed81e22dSJed Brown } 3119566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(monfilename,&filetemplate)); 3129566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy)); 313ed81e22dSJed Brown } 314bdad233fSMatthew Knepley 3159566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,sizeof(dir),&flg)); 316d1212d36SBarry Smith if (flg) { 317d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 318d1212d36SBarry Smith int ray = 0; 3193ee9839eSMatthew G. Knepley DMDirection ddir; 320d1212d36SBarry Smith DM da; 321d1212d36SBarry Smith PetscMPIInt rank; 322d1212d36SBarry Smith 3233c633725SBarry Smith PetscCheck(dir[1] == '=',PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 3243ee9839eSMatthew G. Knepley if (dir[0] == 'x') ddir = DM_X; 3253ee9839eSMatthew G. Knepley else if (dir[0] == 'y') ddir = DM_Y; 32698921bdaSJacob Faibussowitsch else SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 327d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 328d1212d36SBarry Smith 3299566063dSJacob Faibussowitsch PetscCall(PetscInfo(((PetscObject)ts),"Displaying DMDA ray %c = %d\n",dir[0],ray)); 3309566063dSJacob Faibussowitsch PetscCall(PetscNew(&rayctx)); 3319566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&da)); 3329566063dSJacob Faibussowitsch PetscCall(DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter)); 3339566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank)); 334dd400576SPatrick Sanan if (rank == 0) { 3359566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawOpen(PETSC_COMM_SELF,NULL,NULL,0,0,600,300,&rayctx->viewer)); 336d1212d36SBarry Smith } 33751b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 3389566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy)); 339d1212d36SBarry Smith } 3409566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,sizeof(dir),&flg)); 34151b4a12fSMatthew G. Knepley if (flg) { 34251b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 34351b4a12fSMatthew G. Knepley int ray = 0; 3443ee9839eSMatthew G. Knepley DMDirection ddir; 34551b4a12fSMatthew G. Knepley DM da; 34651b4a12fSMatthew G. Knepley PetscInt howoften = 1; 347d1212d36SBarry Smith 3483c633725SBarry Smith PetscCheck(dir[1] == '=',PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 3493ee9839eSMatthew G. Knepley if (dir[0] == 'x') ddir = DM_X; 3503ee9839eSMatthew G. Knepley else if (dir[0] == 'y') ddir = DM_Y; 35198921bdaSJacob Faibussowitsch else SETERRQ(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 35251b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 3531c3436cfSJed Brown 3549566063dSJacob Faibussowitsch PetscCall(PetscInfo(((PetscObject) ts),"Displaying LG DMDA ray %c = %d\n", dir[0], ray)); 3559566063dSJacob Faibussowitsch PetscCall(PetscNew(&rayctx)); 3569566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &da)); 3579566063dSJacob Faibussowitsch PetscCall(DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter)); 3589566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx)); 3599566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy)); 36051b4a12fSMatthew G. Knepley } 361a7a1495cSBarry Smith 3629566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt)); 363b3d3934dSBarry Smith if (opt) { 364b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 365b3d3934dSBarry Smith 3669566063dSJacob Faibussowitsch PetscCall(TSMonitorEnvelopeCtxCreate(ts,&ctx)); 3679566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy)); 368b3d3934dSBarry Smith } 369aee7a9fbSMatthew G. Knepley flg = PETSC_FALSE; 3709566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_cancel","Remove all monitors","TSMonitorCancel",flg,&flg,&opt)); 3719566063dSJacob Faibussowitsch if (opt && flg) PetscCall(TSMonitorCancel(ts)); 372b3d3934dSBarry Smith 373847ff0e1SMatthew G. Knepley flg = PETSC_FALSE; 3749566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL)); 375847ff0e1SMatthew G. Knepley if (flg) { 376847ff0e1SMatthew G. Knepley DM dm; 377847ff0e1SMatthew G. Knepley DMTS tdm; 378847ff0e1SMatthew G. Knepley 3799566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 3809566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm, &tdm)); 381847ff0e1SMatthew G. Knepley tdm->ijacobianctx = NULL; 3829566063dSJacob Faibussowitsch PetscCall(TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, NULL)); 3839566063dSJacob Faibussowitsch PetscCall(PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n")); 384847ff0e1SMatthew G. Knepley } 385847ff0e1SMatthew G. Knepley 386d763cef2SBarry Smith /* Handle specific TS options */ 387d763cef2SBarry Smith if (ts->ops->setfromoptions) { 3889566063dSJacob Faibussowitsch PetscCall((*ts->ops->setfromoptions)(PetscOptionsObject,ts)); 389d763cef2SBarry Smith } 390fbc52257SHong Zhang 391a7bdc993SLisandro Dalcin /* Handle TSAdapt options */ 3929566063dSJacob Faibussowitsch PetscCall(TSGetAdapt(ts,&ts->adapt)); 3939566063dSJacob Faibussowitsch PetscCall(TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type)); 3949566063dSJacob Faibussowitsch PetscCall(TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt)); 395a7bdc993SLisandro Dalcin 39668bece0bSHong Zhang /* TS trajectory must be set after TS, since it may use some TS options above */ 3974f122a70SLisandro Dalcin tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE; 3989566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL)); 39968bece0bSHong Zhang if (tflg) { 4009566063dSJacob Faibussowitsch PetscCall(TSSetSaveTrajectory(ts)); 40168bece0bSHong Zhang } 402a05bf03eSHong Zhang 4039566063dSJacob Faibussowitsch PetscCall(TSAdjointSetFromOptions(PetscOptionsObject,ts)); 404d763cef2SBarry Smith 405d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 4069566063dSJacob Faibussowitsch PetscCall(PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts)); 407*d0609cedSBarry Smith PetscOptionsEnd(); 408d763cef2SBarry Smith 4094f122a70SLisandro Dalcin if (ts->trajectory) { 4109566063dSJacob Faibussowitsch PetscCall(TSTrajectorySetFromOptions(ts->trajectory,ts)); 411d763cef2SBarry Smith } 41268bece0bSHong Zhang 4131ef27442SStefano Zampini /* why do we have to do this here and not during TSSetUp? */ 4149566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&ts->snes)); 4151ef27442SStefano Zampini if (ts->problem_type == TS_LINEAR) { 4169566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompareAny((PetscObject)ts->snes,&flg,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"")); 4179566063dSJacob Faibussowitsch if (!flg) PetscCall(SNESSetType(ts->snes,SNESKSPONLY)); 4181ef27442SStefano Zampini } 4199566063dSJacob Faibussowitsch PetscCall(SNESSetFromOptions(ts->snes)); 420d763cef2SBarry Smith PetscFunctionReturn(0); 421d763cef2SBarry Smith } 422d763cef2SBarry Smith 423d2daff3dSHong Zhang /*@ 42478fbdcc8SBarry Smith TSGetTrajectory - Gets the trajectory from a TS if it exists 42578fbdcc8SBarry Smith 42678fbdcc8SBarry Smith Collective on TS 42778fbdcc8SBarry Smith 42878fbdcc8SBarry Smith Input Parameters: 42978fbdcc8SBarry Smith . ts - the TS context obtained from TSCreate() 43078fbdcc8SBarry Smith 4317a7aea1fSJed Brown Output Parameters: 43278fbdcc8SBarry Smith . tr - the TSTrajectory object, if it exists 43378fbdcc8SBarry Smith 43478fbdcc8SBarry Smith Note: This routine should be called after all TS options have been set 43578fbdcc8SBarry Smith 43678fbdcc8SBarry Smith Level: advanced 43778fbdcc8SBarry Smith 43878fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate() 43978fbdcc8SBarry Smith 44078fbdcc8SBarry Smith @*/ 44178fbdcc8SBarry Smith PetscErrorCode TSGetTrajectory(TS ts,TSTrajectory *tr) 44278fbdcc8SBarry Smith { 44378fbdcc8SBarry Smith PetscFunctionBegin; 44478fbdcc8SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 44578fbdcc8SBarry Smith *tr = ts->trajectory; 44678fbdcc8SBarry Smith PetscFunctionReturn(0); 44778fbdcc8SBarry Smith } 44878fbdcc8SBarry Smith 44978fbdcc8SBarry Smith /*@ 450bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 451d2daff3dSHong Zhang 452d2daff3dSHong Zhang Collective on TS 453d2daff3dSHong Zhang 454f899ff85SJose E. Roman Input Parameter: 455bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 456bc952696SBarry Smith 45778fbdcc8SBarry Smith Options Database: 45878fbdcc8SBarry Smith + -ts_save_trajectory - saves the trajectory to a file 45967b8a455SSatish Balay - -ts_trajectory_type type - set trajectory type 46078fbdcc8SBarry Smith 46168bece0bSHong Zhang Note: This routine should be called after all TS options have been set 462d2daff3dSHong Zhang 463c3a89c15SBarry Smith The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and 46478fbdcc8SBarry Smith MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m 46578fbdcc8SBarry Smith 466d2daff3dSHong Zhang Level: intermediate 467d2daff3dSHong Zhang 4682d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve() 469d2daff3dSHong Zhang 470d2daff3dSHong Zhang @*/ 471bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 472d2daff3dSHong Zhang { 473d2daff3dSHong Zhang PetscFunctionBegin; 474d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 475bc952696SBarry Smith if (!ts->trajectory) { 4769566063dSJacob Faibussowitsch PetscCall(TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory)); 477bc952696SBarry Smith } 478d2daff3dSHong Zhang PetscFunctionReturn(0); 479d2daff3dSHong Zhang } 480d2daff3dSHong Zhang 481a7a1495cSBarry Smith /*@ 4822d29f1f2SStefano Zampini TSResetTrajectory - Destroys and recreates the internal TSTrajectory object 4832d29f1f2SStefano Zampini 4842d29f1f2SStefano Zampini Collective on TS 4852d29f1f2SStefano Zampini 4862d29f1f2SStefano Zampini Input Parameters: 4872d29f1f2SStefano Zampini . ts - the TS context obtained from TSCreate() 4882d29f1f2SStefano Zampini 4892d29f1f2SStefano Zampini Level: intermediate 4902d29f1f2SStefano Zampini 49167a3cfb0SHong Zhang .seealso: TSGetTrajectory(), TSAdjointSolve(), TSRemoveTrajectory() 4922d29f1f2SStefano Zampini 4932d29f1f2SStefano Zampini @*/ 4942d29f1f2SStefano Zampini PetscErrorCode TSResetTrajectory(TS ts) 4952d29f1f2SStefano Zampini { 4962d29f1f2SStefano Zampini PetscFunctionBegin; 4972d29f1f2SStefano Zampini PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4982d29f1f2SStefano Zampini if (ts->trajectory) { 4999566063dSJacob Faibussowitsch PetscCall(TSTrajectoryDestroy(&ts->trajectory)); 5009566063dSJacob Faibussowitsch PetscCall(TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory)); 5012d29f1f2SStefano Zampini } 5022d29f1f2SStefano Zampini PetscFunctionReturn(0); 5032d29f1f2SStefano Zampini } 5042d29f1f2SStefano Zampini 5052d29f1f2SStefano Zampini /*@ 50667a3cfb0SHong Zhang TSRemoveTrajectory - Destroys and removes the internal TSTrajectory object from TS 50767a3cfb0SHong Zhang 50867a3cfb0SHong Zhang Collective on TS 50967a3cfb0SHong Zhang 51067a3cfb0SHong Zhang Input Parameters: 51167a3cfb0SHong Zhang . ts - the TS context obtained from TSCreate() 51267a3cfb0SHong Zhang 51367a3cfb0SHong Zhang Level: intermediate 51467a3cfb0SHong Zhang 51567a3cfb0SHong Zhang .seealso: TSResetTrajectory(), TSAdjointSolve() 51667a3cfb0SHong Zhang 51767a3cfb0SHong Zhang @*/ 51867a3cfb0SHong Zhang PetscErrorCode TSRemoveTrajectory(TS ts) 51967a3cfb0SHong Zhang { 52067a3cfb0SHong Zhang PetscFunctionBegin; 52167a3cfb0SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 52267a3cfb0SHong Zhang if (ts->trajectory) { 5239566063dSJacob Faibussowitsch PetscCall(TSTrajectoryDestroy(&ts->trajectory)); 52467a3cfb0SHong Zhang } 52567a3cfb0SHong Zhang PetscFunctionReturn(0); 52667a3cfb0SHong Zhang } 52767a3cfb0SHong Zhang 52867a3cfb0SHong Zhang /*@ 529a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 530a7a1495cSBarry Smith set with TSSetRHSJacobian(). 531a7a1495cSBarry Smith 532d083f849SBarry Smith Collective on TS 533a7a1495cSBarry Smith 534a7a1495cSBarry Smith Input Parameters: 535316643e7SJed Brown + ts - the TS context 536a7a1495cSBarry Smith . t - current timestep 5370910c330SBarry Smith - U - input vector 538a7a1495cSBarry Smith 539a7a1495cSBarry Smith Output Parameters: 540a7a1495cSBarry Smith + A - Jacobian matrix 5416b867d5aSJose E. Roman - B - optional preconditioning matrix 542a7a1495cSBarry Smith 543a7a1495cSBarry Smith Notes: 544a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 545a7a1495cSBarry Smith is used internally within the nonlinear solvers. 546a7a1495cSBarry Smith 547a7a1495cSBarry Smith Level: developer 548a7a1495cSBarry Smith 54994b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 550a7a1495cSBarry Smith @*/ 551d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 552a7a1495cSBarry Smith { 553270bf2e7SJed Brown PetscObjectState Ustate; 5546c1e1eecSBarry Smith PetscObjectId Uid; 55524989b8cSPeter Brune DM dm; 556942e3340SBarry Smith DMTS tsdm; 55724989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 55824989b8cSPeter Brune void *ctx; 559b2df71adSDebojyoti Ghosh TSRHSFunction rhsfunction; 560a7a1495cSBarry Smith 561a7a1495cSBarry Smith PetscFunctionBegin; 5620700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5630910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5640910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 5659566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 5669566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm,&tsdm)); 5679566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,NULL)); 5689566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx)); 5699566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&Ustate)); 5709566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U,&Uid)); 571971015bcSStefano Zampini 5722663174eSHong Zhang if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) PetscFunctionReturn(0); 573d90be118SSean Farley 5743c633725SBarry Smith 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.",ts->rhsjacobian.shift); 57524989b8cSPeter Brune if (rhsjacobianfunc) { 5769566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_JacobianEval,ts,U,A,B)); 577a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 5789566063dSJacob Faibussowitsch PetscCall((*rhsjacobianfunc)(ts,t,U,A,B,ctx)); 579a7a1495cSBarry Smith PetscStackPop; 580a6ab3590SBarry Smith ts->rhsjacs++; 5819566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_JacobianEval,ts,U,A,B)); 582ef66eb69SBarry Smith } else { 5839566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(A)); 5849566063dSJacob Faibussowitsch if (B && A != B) PetscCall(MatZeroEntries(B)); 585ef66eb69SBarry Smith } 5860e4ef248SJed Brown ts->rhsjacobian.time = t; 587971015bcSStefano Zampini ts->rhsjacobian.shift = 0; 588971015bcSStefano Zampini ts->rhsjacobian.scale = 1.; 5899566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid)); 5909566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate)); 591a7a1495cSBarry Smith PetscFunctionReturn(0); 592a7a1495cSBarry Smith } 593a7a1495cSBarry Smith 594316643e7SJed Brown /*@ 595d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 596d763cef2SBarry Smith 597d083f849SBarry Smith Collective on TS 598316643e7SJed Brown 599316643e7SJed Brown Input Parameters: 600316643e7SJed Brown + ts - the TS context 601316643e7SJed Brown . t - current time 6020910c330SBarry Smith - U - state vector 603316643e7SJed Brown 604316643e7SJed Brown Output Parameter: 605316643e7SJed Brown . y - right hand side 606316643e7SJed Brown 607316643e7SJed Brown Note: 608316643e7SJed Brown Most users should not need to explicitly call this routine, as it 609316643e7SJed Brown is used internally within the nonlinear solvers. 610316643e7SJed Brown 611316643e7SJed Brown Level: developer 612316643e7SJed Brown 613316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 614316643e7SJed Brown @*/ 6150910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 616d763cef2SBarry Smith { 61724989b8cSPeter Brune TSRHSFunction rhsfunction; 61824989b8cSPeter Brune TSIFunction ifunction; 61924989b8cSPeter Brune void *ctx; 62024989b8cSPeter Brune DM dm; 62124989b8cSPeter Brune 622d763cef2SBarry Smith PetscFunctionBegin; 6230700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6240910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6250700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 6269566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 6279566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,&ctx)); 6289566063dSJacob Faibussowitsch PetscCall(DMTSGetIFunction(dm,&ifunction,NULL)); 629d763cef2SBarry Smith 6303c633725SBarry Smith PetscCheck(rhsfunction || ifunction,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 631d763cef2SBarry Smith 63224989b8cSPeter Brune if (rhsfunction) { 6339566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_FunctionEval,ts,U,y,0)); 6349566063dSJacob Faibussowitsch PetscCall(VecLockReadPush(U)); 635d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 6369566063dSJacob Faibussowitsch PetscCall((*rhsfunction)(ts,t,U,y,ctx)); 637d763cef2SBarry Smith PetscStackPop; 6389566063dSJacob Faibussowitsch PetscCall(VecLockReadPop(U)); 639a6ab3590SBarry Smith ts->rhsfuncs++; 6409566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_FunctionEval,ts,U,y,0)); 641214bc6a2SJed Brown } else { 6429566063dSJacob Faibussowitsch PetscCall(VecZeroEntries(y)); 643b2cd27e8SJed Brown } 644d763cef2SBarry Smith PetscFunctionReturn(0); 645d763cef2SBarry Smith } 646d763cef2SBarry Smith 647ef20d060SBarry Smith /*@ 648ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 649ef20d060SBarry Smith 650d083f849SBarry Smith Collective on TS 651ef20d060SBarry Smith 652ef20d060SBarry Smith Input Parameters: 653ef20d060SBarry Smith + ts - the TS context 654ef20d060SBarry Smith - t - current time 655ef20d060SBarry Smith 656ef20d060SBarry Smith Output Parameter: 6570910c330SBarry Smith . U - the solution 658ef20d060SBarry Smith 659ef20d060SBarry Smith Note: 660ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 661ef20d060SBarry Smith is used internally within the nonlinear solvers. 662ef20d060SBarry Smith 663ef20d060SBarry Smith Level: developer 664ef20d060SBarry Smith 665abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 666ef20d060SBarry Smith @*/ 6670910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 668ef20d060SBarry Smith { 669ef20d060SBarry Smith TSSolutionFunction solutionfunction; 670ef20d060SBarry Smith void *ctx; 671ef20d060SBarry Smith DM dm; 672ef20d060SBarry Smith 673ef20d060SBarry Smith PetscFunctionBegin; 674ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6750910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6769566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 6779566063dSJacob Faibussowitsch PetscCall(DMTSGetSolutionFunction(dm,&solutionfunction,&ctx)); 678ef20d060SBarry Smith 679ef20d060SBarry Smith if (solutionfunction) { 6809b7cd975SBarry Smith PetscStackPush("TS user solution function"); 6819566063dSJacob Faibussowitsch PetscCall((*solutionfunction)(ts,t,U,ctx)); 682ef20d060SBarry Smith PetscStackPop; 683ef20d060SBarry Smith } 684ef20d060SBarry Smith PetscFunctionReturn(0); 685ef20d060SBarry Smith } 6869b7cd975SBarry Smith /*@ 6879b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 6889b7cd975SBarry Smith 689d083f849SBarry Smith Collective on TS 6909b7cd975SBarry Smith 6919b7cd975SBarry Smith Input Parameters: 6929b7cd975SBarry Smith + ts - the TS context 6939b7cd975SBarry Smith - t - current time 6949b7cd975SBarry Smith 6959b7cd975SBarry Smith Output Parameter: 6969b7cd975SBarry Smith . U - the function value 6979b7cd975SBarry Smith 6989b7cd975SBarry Smith Note: 6999b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 7009b7cd975SBarry Smith is used internally within the nonlinear solvers. 7019b7cd975SBarry Smith 7029b7cd975SBarry Smith Level: developer 7039b7cd975SBarry Smith 7049b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 7059b7cd975SBarry Smith @*/ 7069b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 7079b7cd975SBarry Smith { 7089b7cd975SBarry Smith void *ctx; 7099b7cd975SBarry Smith DM dm; 7105f80ce2aSJacob Faibussowitsch TSForcingFunction forcing; 7119b7cd975SBarry Smith 7129b7cd975SBarry Smith PetscFunctionBegin; 7139b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7149b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7159566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 7169566063dSJacob Faibussowitsch PetscCall(DMTSGetForcingFunction(dm,&forcing,&ctx)); 7179b7cd975SBarry Smith 7189b7cd975SBarry Smith if (forcing) { 7199b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 7209566063dSJacob Faibussowitsch PetscCall((*forcing)(ts,t,U,ctx)); 7219b7cd975SBarry Smith PetscStackPop; 7229b7cd975SBarry Smith } 7239b7cd975SBarry Smith PetscFunctionReturn(0); 7249b7cd975SBarry Smith } 725ef20d060SBarry Smith 726214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 727214bc6a2SJed Brown { 7282dd45cf8SJed Brown Vec F; 729214bc6a2SJed Brown 730214bc6a2SJed Brown PetscFunctionBegin; 7310298fd71SBarry Smith *Frhs = NULL; 7329566063dSJacob Faibussowitsch PetscCall(TSGetIFunction(ts,&F,NULL,NULL)); 733214bc6a2SJed Brown if (!ts->Frhs) { 7349566063dSJacob Faibussowitsch PetscCall(VecDuplicate(F,&ts->Frhs)); 735214bc6a2SJed Brown } 736214bc6a2SJed Brown *Frhs = ts->Frhs; 737214bc6a2SJed Brown PetscFunctionReturn(0); 738214bc6a2SJed Brown } 739214bc6a2SJed Brown 740971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 741214bc6a2SJed Brown { 742214bc6a2SJed Brown Mat A,B; 74341a1d4d2SBarry Smith TSIJacobian ijacobian; 744214bc6a2SJed Brown 745214bc6a2SJed Brown PetscFunctionBegin; 746c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 747c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 7489566063dSJacob Faibussowitsch PetscCall(TSGetIJacobian(ts,&A,&B,&ijacobian,NULL)); 749214bc6a2SJed Brown if (Arhs) { 750214bc6a2SJed Brown if (!ts->Arhs) { 75141a1d4d2SBarry Smith if (ijacobian) { 7529566063dSJacob Faibussowitsch PetscCall(MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs)); 7539566063dSJacob Faibussowitsch PetscCall(TSSetMatStructure(ts,SAME_NONZERO_PATTERN)); 75441a1d4d2SBarry Smith } else { 75541a1d4d2SBarry Smith ts->Arhs = A; 7569566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)A)); 75741a1d4d2SBarry Smith } 7583565c898SBarry Smith } else { 7593565c898SBarry Smith PetscBool flg; 7609566063dSJacob Faibussowitsch PetscCall(SNESGetUseMatrixFree(ts->snes,NULL,&flg)); 7613565c898SBarry Smith /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */ 7623565c898SBarry Smith if (flg && !ijacobian && ts->Arhs == ts->Brhs) { 7639566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)ts->Arhs)); 7643565c898SBarry Smith ts->Arhs = A; 7659566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)A)); 7663565c898SBarry Smith } 767214bc6a2SJed Brown } 768214bc6a2SJed Brown *Arhs = ts->Arhs; 769214bc6a2SJed Brown } 770214bc6a2SJed Brown if (Brhs) { 771214bc6a2SJed Brown if (!ts->Brhs) { 772bdb70873SJed Brown if (A != B) { 77341a1d4d2SBarry Smith if (ijacobian) { 7749566063dSJacob Faibussowitsch PetscCall(MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs)); 775bdb70873SJed Brown } else { 77641a1d4d2SBarry Smith ts->Brhs = B; 7779566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)B)); 77841a1d4d2SBarry Smith } 77941a1d4d2SBarry Smith } else { 7809566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)ts->Arhs)); 78151699248SLisandro Dalcin ts->Brhs = ts->Arhs; 782bdb70873SJed Brown } 783214bc6a2SJed Brown } 784214bc6a2SJed Brown *Brhs = ts->Brhs; 785214bc6a2SJed Brown } 786214bc6a2SJed Brown PetscFunctionReturn(0); 787214bc6a2SJed Brown } 788214bc6a2SJed Brown 789316643e7SJed Brown /*@ 7900910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 791316643e7SJed Brown 792d083f849SBarry Smith Collective on TS 793316643e7SJed Brown 794316643e7SJed Brown Input Parameters: 795316643e7SJed Brown + ts - the TS context 796316643e7SJed Brown . t - current time 7970910c330SBarry Smith . U - state vector 7980910c330SBarry Smith . Udot - time derivative of state vector 799214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 800316643e7SJed Brown 801316643e7SJed Brown Output Parameter: 802316643e7SJed Brown . Y - right hand side 803316643e7SJed Brown 804316643e7SJed Brown Note: 805316643e7SJed Brown Most users should not need to explicitly call this routine, as it 806316643e7SJed Brown is used internally within the nonlinear solvers. 807316643e7SJed Brown 808316643e7SJed Brown If the user did did not write their equations in implicit form, this 809316643e7SJed Brown function recasts them in implicit form. 810316643e7SJed Brown 811316643e7SJed Brown Level: developer 812316643e7SJed Brown 813316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 814316643e7SJed Brown @*/ 8150910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 816316643e7SJed Brown { 81724989b8cSPeter Brune TSIFunction ifunction; 81824989b8cSPeter Brune TSRHSFunction rhsfunction; 81924989b8cSPeter Brune void *ctx; 82024989b8cSPeter Brune DM dm; 821316643e7SJed Brown 822316643e7SJed Brown PetscFunctionBegin; 8230700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8240910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8250910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 8260700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 827316643e7SJed Brown 8289566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 8299566063dSJacob Faibussowitsch PetscCall(DMTSGetIFunction(dm,&ifunction,&ctx)); 8309566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,NULL)); 83124989b8cSPeter Brune 8323c633725SBarry Smith PetscCheck(rhsfunction || ifunction,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 833d90be118SSean Farley 8349566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y)); 83524989b8cSPeter Brune if (ifunction) { 836316643e7SJed Brown PetscStackPush("TS user implicit function"); 8379566063dSJacob Faibussowitsch PetscCall((*ifunction)(ts,t,U,Udot,Y,ctx)); 838316643e7SJed Brown PetscStackPop; 839a6ab3590SBarry Smith ts->ifuncs++; 840214bc6a2SJed Brown } 841214bc6a2SJed Brown if (imex) { 84224989b8cSPeter Brune if (!ifunction) { 8439566063dSJacob Faibussowitsch PetscCall(VecCopy(Udot,Y)); 8442dd45cf8SJed Brown } 84524989b8cSPeter Brune } else if (rhsfunction) { 84624989b8cSPeter Brune if (ifunction) { 847214bc6a2SJed Brown Vec Frhs; 8489566063dSJacob Faibussowitsch PetscCall(TSGetRHSVec_Private(ts,&Frhs)); 8499566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts,t,U,Frhs)); 8509566063dSJacob Faibussowitsch PetscCall(VecAXPY(Y,-1,Frhs)); 8512dd45cf8SJed Brown } else { 8529566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts,t,U,Y)); 8539566063dSJacob Faibussowitsch PetscCall(VecAYPX(Y,-1,Udot)); 854316643e7SJed Brown } 8554a6899ffSJed Brown } 8569566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y)); 857316643e7SJed Brown PetscFunctionReturn(0); 858316643e7SJed Brown } 859316643e7SJed Brown 860cfa8a9a2SHong Zhang /* 861cfa8a9a2SHong Zhang TSRecoverRHSJacobian - Recover the Jacobian matrix so that one can call TSComputeRHSJacobian() on it. 862cfa8a9a2SHong Zhang 863cfa8a9a2SHong Zhang Note: 864cfa8a9a2SHong Zhang This routine is needed when one switches from TSComputeIJacobian() to TSComputeRHSJacobian() because the Jacobian matrix may be shifted or scaled in TSComputeIJacobian(). 865cfa8a9a2SHong Zhang 866cfa8a9a2SHong Zhang */ 867cfa8a9a2SHong Zhang static PetscErrorCode TSRecoverRHSJacobian(TS ts,Mat A,Mat B) 868cfa8a9a2SHong Zhang { 869cfa8a9a2SHong Zhang PetscFunctionBegin; 870cfa8a9a2SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8713c633725SBarry Smith PetscCheck(A == ts->Arhs,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Invalid Amat"); 8723c633725SBarry Smith PetscCheck(B == ts->Brhs,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Invalid Bmat"); 873cfa8a9a2SHong Zhang 874cfa8a9a2SHong Zhang if (ts->rhsjacobian.shift) { 8759566063dSJacob Faibussowitsch PetscCall(MatShift(A,-ts->rhsjacobian.shift)); 876cfa8a9a2SHong Zhang } 877cfa8a9a2SHong Zhang if (ts->rhsjacobian.scale == -1.) { 8789566063dSJacob Faibussowitsch PetscCall(MatScale(A,-1)); 879cfa8a9a2SHong Zhang } 880cfa8a9a2SHong Zhang if (B && B == ts->Brhs && A != B) { 881cfa8a9a2SHong Zhang if (ts->rhsjacobian.shift) { 8829566063dSJacob Faibussowitsch PetscCall(MatShift(B,-ts->rhsjacobian.shift)); 883cfa8a9a2SHong Zhang } 884cfa8a9a2SHong Zhang if (ts->rhsjacobian.scale == -1.) { 8859566063dSJacob Faibussowitsch PetscCall(MatScale(B,-1)); 886cfa8a9a2SHong Zhang } 887cfa8a9a2SHong Zhang } 888cfa8a9a2SHong Zhang ts->rhsjacobian.shift = 0; 889cfa8a9a2SHong Zhang ts->rhsjacobian.scale = 1.; 890cfa8a9a2SHong Zhang PetscFunctionReturn(0); 891cfa8a9a2SHong Zhang } 892cfa8a9a2SHong Zhang 893316643e7SJed Brown /*@ 894316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 895316643e7SJed Brown 896d083f849SBarry Smith Collective on TS 897316643e7SJed Brown 898316643e7SJed Brown Input 899316643e7SJed Brown Input Parameters: 900316643e7SJed Brown + ts - the TS context 901316643e7SJed Brown . t - current timestep 9020910c330SBarry Smith . U - state vector 9030910c330SBarry Smith . Udot - time derivative of state vector 904214bc6a2SJed Brown . shift - shift to apply, see note below 905214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 906316643e7SJed Brown 907316643e7SJed Brown Output Parameters: 908316643e7SJed Brown + A - Jacobian matrix 9093565c898SBarry Smith - B - matrix from which the preconditioner is constructed; often the same as A 910316643e7SJed Brown 911316643e7SJed Brown Notes: 9120910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 913316643e7SJed Brown 9140910c330SBarry Smith dF/dU + shift*dF/dUdot 915316643e7SJed Brown 916316643e7SJed Brown Most users should not need to explicitly call this routine, as it 917316643e7SJed Brown is used internally within the nonlinear solvers. 918316643e7SJed Brown 919316643e7SJed Brown Level: developer 920316643e7SJed Brown 921316643e7SJed Brown .seealso: TSSetIJacobian() 92263495f91SJed Brown @*/ 923d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 924316643e7SJed Brown { 92524989b8cSPeter Brune TSIJacobian ijacobian; 92624989b8cSPeter Brune TSRHSJacobian rhsjacobian; 92724989b8cSPeter Brune DM dm; 92824989b8cSPeter Brune void *ctx; 929316643e7SJed Brown 930316643e7SJed Brown PetscFunctionBegin; 9310700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9320910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 9330910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 934316643e7SJed Brown PetscValidPointer(A,6); 93594ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 936316643e7SJed Brown PetscValidPointer(B,7); 93794ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 93824989b8cSPeter Brune 9399566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 9409566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm,&ijacobian,&ctx)); 9419566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm,&rhsjacobian,NULL)); 94224989b8cSPeter Brune 9433c633725SBarry Smith PetscCheck(rhsjacobian || ijacobian,PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 944316643e7SJed Brown 9459566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_JacobianEval,ts,U,A,B)); 94624989b8cSPeter Brune if (ijacobian) { 947316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 9489566063dSJacob Faibussowitsch PetscCall((*ijacobian)(ts,t,U,Udot,shift,A,B,ctx)); 949a6ab3590SBarry Smith ts->ijacs++; 950316643e7SJed Brown PetscStackPop; 9514a6899ffSJed Brown } 952214bc6a2SJed Brown if (imex) { 953b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 9544c26be97Sstefano_zampini PetscBool assembled; 955971015bcSStefano Zampini if (rhsjacobian) { 956971015bcSStefano Zampini Mat Arhs = NULL; 9579566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts,&Arhs,NULL)); 958971015bcSStefano Zampini if (A == Arhs) { 9593c633725SBarry 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 */ 960971015bcSStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 961971015bcSStefano Zampini } 962971015bcSStefano Zampini } 9639566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(A)); 9649566063dSJacob Faibussowitsch PetscCall(MatAssembled(A,&assembled)); 9654c26be97Sstefano_zampini if (!assembled) { 9669566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY)); 9679566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY)); 9684c26be97Sstefano_zampini } 9699566063dSJacob Faibussowitsch PetscCall(MatShift(A,shift)); 97094ab13aaSBarry Smith if (A != B) { 9719566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(B)); 9729566063dSJacob Faibussowitsch PetscCall(MatAssembled(B,&assembled)); 9734c26be97Sstefano_zampini if (!assembled) { 9749566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY)); 9759566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY)); 9764c26be97Sstefano_zampini } 9779566063dSJacob Faibussowitsch PetscCall(MatShift(B,shift)); 978214bc6a2SJed Brown } 979214bc6a2SJed Brown } 980214bc6a2SJed Brown } else { 981e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 982e8b1e424SHong Zhang if (rhsjacobian) { /* RHSJacobian needs to be converted to part of IJacobian if exists */ 9839566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts,&Arhs,&Brhs)); 984e1244c69SJed Brown } 985e8b1e424SHong Zhang if (Arhs == A) { /* No IJacobian matrix, so we only have the RHS matrix */ 986e8b1e424SHong Zhang PetscObjectState Ustate; 987e8b1e424SHong Zhang PetscObjectId Uid; 988e8b1e424SHong Zhang TSRHSFunction rhsfunction; 989e8b1e424SHong Zhang 9909566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,NULL)); 9919566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&Ustate)); 9929566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U,&Uid)); 993097a96a2SHong 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 */ 9949566063dSJacob Faibussowitsch PetscCall(MatShift(A,shift-ts->rhsjacobian.shift)); /* revert the old shift and add the new shift with a single call to MatShift */ 99534d322a1SHong Zhang if (A != B) { 9969566063dSJacob Faibussowitsch PetscCall(MatShift(B,shift-ts->rhsjacobian.shift)); 99734d322a1SHong Zhang } 998e8b1e424SHong Zhang } else { 9993565c898SBarry Smith PetscBool flg; 1000e8b1e424SHong Zhang 1001e8b1e424SHong Zhang if (ts->rhsjacobian.reuse) { /* Undo the damage */ 1002e8b1e424SHong Zhang /* MatScale has a short path for this case. 1003e8b1e424SHong Zhang However, this code path is taken the first time TSComputeRHSJacobian is called 1004e8b1e424SHong Zhang and the matrices have not been assembled yet */ 10059566063dSJacob Faibussowitsch PetscCall(TSRecoverRHSJacobian(ts,A,B)); 1006e8b1e424SHong Zhang } 10079566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts,t,U,A,B)); 10089566063dSJacob Faibussowitsch PetscCall(SNESGetUseMatrixFree(ts->snes,NULL,&flg)); 10093565c898SBarry Smith /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */ 10103565c898SBarry Smith if (!flg) { 10119566063dSJacob Faibussowitsch PetscCall(MatScale(A,-1)); 10129566063dSJacob Faibussowitsch PetscCall(MatShift(A,shift)); 10133565c898SBarry Smith } 101494ab13aaSBarry Smith if (A != B) { 10159566063dSJacob Faibussowitsch PetscCall(MatScale(B,-1)); 10169566063dSJacob Faibussowitsch PetscCall(MatShift(B,shift)); 1017316643e7SJed Brown } 1018e8b1e424SHong Zhang } 1019e8b1e424SHong Zhang ts->rhsjacobian.scale = -1; 1020e8b1e424SHong Zhang ts->rhsjacobian.shift = shift; 1021d60b7d5cSBarry Smith } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 1022e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 10239566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(A)); 10249566063dSJacob Faibussowitsch PetscCall(MatShift(A,shift)); 102594ab13aaSBarry Smith if (A != B) { 10269566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(B)); 10279566063dSJacob Faibussowitsch PetscCall(MatShift(B,shift)); 1028214bc6a2SJed Brown } 1029316643e7SJed Brown } 10309566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts,t,U,Arhs,Brhs)); 10319566063dSJacob Faibussowitsch PetscCall(MatAXPY(A,-1,Arhs,ts->axpy_pattern)); 103294ab13aaSBarry Smith if (A != B) { 10339566063dSJacob Faibussowitsch PetscCall(MatAXPY(B,-1,Brhs,ts->axpy_pattern)); 1034316643e7SJed Brown } 1035316643e7SJed Brown } 1036316643e7SJed Brown } 10379566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_JacobianEval,ts,U,A,B)); 1038316643e7SJed Brown PetscFunctionReturn(0); 1039316643e7SJed Brown } 1040316643e7SJed Brown 1041d763cef2SBarry Smith /*@C 1042d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 1043b5abc632SBarry Smith where U_t = G(t,u). 1044d763cef2SBarry Smith 10453f9fe445SBarry Smith Logically Collective on TS 1046d763cef2SBarry Smith 1047d763cef2SBarry Smith Input Parameters: 1048d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 10490298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 1050d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 1051d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 10520298fd71SBarry Smith function evaluation routine (may be NULL) 1053d763cef2SBarry Smith 1054a96d6ef6SBarry Smith Calling sequence of f: 1055a96d6ef6SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec F,void *ctx); 1056d763cef2SBarry Smith 1057a96d6ef6SBarry Smith + ts - timestep context 1058a96d6ef6SBarry Smith . t - current timestep 1059d763cef2SBarry Smith . u - input vector 1060d763cef2SBarry Smith . F - function vector 1061d763cef2SBarry Smith - ctx - [optional] user-defined function context 1062d763cef2SBarry Smith 1063d763cef2SBarry Smith Level: beginner 1064d763cef2SBarry Smith 106595452b02SPatrick Sanan Notes: 106695452b02SPatrick Sanan You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 10672bbac0d3SBarry Smith 1068ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 1069d763cef2SBarry Smith @*/ 1070089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 1071d763cef2SBarry Smith { 1072089b2837SJed Brown SNES snes; 10730298fd71SBarry Smith Vec ralloc = NULL; 107424989b8cSPeter Brune DM dm; 1075d763cef2SBarry Smith 1076089b2837SJed Brown PetscFunctionBegin; 10770700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1078ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 107924989b8cSPeter Brune 10809566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 10819566063dSJacob Faibussowitsch PetscCall(DMTSSetRHSFunction(dm,f,ctx)); 10829566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 1083e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 10849566063dSJacob Faibussowitsch PetscCall(VecDuplicate(ts->vec_sol,&ralloc)); 1085e856ceecSJed Brown r = ralloc; 1086e856ceecSJed Brown } 10879566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(snes,r,SNESTSFormFunction,ts)); 10889566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ralloc)); 1089d763cef2SBarry Smith PetscFunctionReturn(0); 1090d763cef2SBarry Smith } 1091d763cef2SBarry Smith 1092ef20d060SBarry Smith /*@C 1093abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1094ef20d060SBarry Smith 1095ef20d060SBarry Smith Logically Collective on TS 1096ef20d060SBarry Smith 1097ef20d060SBarry Smith Input Parameters: 1098ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 1099ef20d060SBarry Smith . f - routine for evaluating the solution 1100ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 11010298fd71SBarry Smith function evaluation routine (may be NULL) 1102ef20d060SBarry Smith 1103a96d6ef6SBarry Smith Calling sequence of f: 1104a96d6ef6SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,void *ctx); 1105ef20d060SBarry Smith 1106ef20d060SBarry Smith + t - current timestep 1107ef20d060SBarry Smith . u - output vector 1108ef20d060SBarry Smith - ctx - [optional] user-defined function context 1109ef20d060SBarry Smith 11100ed3bfb6SBarry Smith Options Database: 11110ed3bfb6SBarry Smith + -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction() 11120ed3bfb6SBarry Smith - -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction() 11130ed3bfb6SBarry Smith 1114abd5a294SJed Brown Notes: 1115abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 1116abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 1117abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 1118abd5a294SJed Brown 11194f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 1120abd5a294SJed Brown 1121ef20d060SBarry Smith Level: beginner 1122ef20d060SBarry Smith 11230ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError() 1124ef20d060SBarry Smith @*/ 1125ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 1126ef20d060SBarry Smith { 1127ef20d060SBarry Smith DM dm; 1128ef20d060SBarry Smith 1129ef20d060SBarry Smith PetscFunctionBegin; 1130ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11319566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 11329566063dSJacob Faibussowitsch PetscCall(DMTSSetSolutionFunction(dm,f,ctx)); 1133ef20d060SBarry Smith PetscFunctionReturn(0); 1134ef20d060SBarry Smith } 1135ef20d060SBarry Smith 11369b7cd975SBarry Smith /*@C 11379b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 11389b7cd975SBarry Smith 11399b7cd975SBarry Smith Logically Collective on TS 11409b7cd975SBarry Smith 11419b7cd975SBarry Smith Input Parameters: 11429b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 1143e162b725SBarry Smith . func - routine for evaluating the forcing function 11449b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 11450298fd71SBarry Smith function evaluation routine (may be NULL) 11469b7cd975SBarry Smith 11479b7cd975SBarry Smith Calling sequence of func: 11486bc98fa9SBarry Smith $ PetscErrorCode func (TS ts,PetscReal t,Vec f,void *ctx); 11499b7cd975SBarry Smith 11509b7cd975SBarry Smith + t - current timestep 1151e162b725SBarry Smith . f - output vector 11529b7cd975SBarry Smith - ctx - [optional] user-defined function context 11539b7cd975SBarry Smith 11549b7cd975SBarry Smith Notes: 11559b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 1156e162b725SBarry 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 1157e162b725SBarry Smith definition of the problem you are solving and hence possibly introducing bugs. 1158e162b725SBarry Smith 1159e162b725SBarry Smith This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0 1160e162b725SBarry Smith 1161e162b725SBarry 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 1162e162b725SBarry Smith parameters can be passed in the ctx variable. 11639b7cd975SBarry Smith 11649b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 11659b7cd975SBarry Smith 11669b7cd975SBarry Smith Level: beginner 11679b7cd975SBarry Smith 11689b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 11699b7cd975SBarry Smith @*/ 1170e162b725SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx) 11719b7cd975SBarry Smith { 11729b7cd975SBarry Smith DM dm; 11739b7cd975SBarry Smith 11749b7cd975SBarry Smith PetscFunctionBegin; 11759b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11769566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 11779566063dSJacob Faibussowitsch PetscCall(DMTSSetForcingFunction(dm,func,ctx)); 11789b7cd975SBarry Smith PetscFunctionReturn(0); 11799b7cd975SBarry Smith } 11809b7cd975SBarry Smith 1181d763cef2SBarry Smith /*@C 1182f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1183b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1184d763cef2SBarry Smith 11853f9fe445SBarry Smith Logically Collective on TS 1186d763cef2SBarry Smith 1187d763cef2SBarry Smith Input Parameters: 1188d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1189e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1190e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1191d763cef2SBarry Smith . f - the Jacobian evaluation routine 1192d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 11930298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1194d763cef2SBarry Smith 1195f7ab8db6SBarry Smith Calling sequence of f: 1196a96d6ef6SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1197d763cef2SBarry Smith 1198d763cef2SBarry Smith + t - current timestep 1199d763cef2SBarry Smith . u - input vector 1200e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1201e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1202d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1203d763cef2SBarry Smith 12046cd88445SBarry Smith Notes: 12056cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 12066cd88445SBarry Smith 12076cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1208ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1209d763cef2SBarry Smith 1210d763cef2SBarry Smith Level: beginner 1211d763cef2SBarry Smith 1212ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1213d763cef2SBarry Smith 1214d763cef2SBarry Smith @*/ 1215e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1216d763cef2SBarry Smith { 1217089b2837SJed Brown SNES snes; 121824989b8cSPeter Brune DM dm; 121924989b8cSPeter Brune TSIJacobian ijacobian; 1220277b19d0SLisandro Dalcin 1221d763cef2SBarry Smith PetscFunctionBegin; 12220700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1223e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1224e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1225e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1226e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1227d763cef2SBarry Smith 12289566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 12299566063dSJacob Faibussowitsch PetscCall(DMTSSetRHSJacobian(dm,f,ctx)); 12309566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm,&ijacobian,NULL)); 12319566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 12325f659677SPeter Brune if (!ijacobian) { 12339566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts)); 12340e4ef248SJed Brown } 1235e5d3d808SBarry Smith if (Amat) { 12369566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)Amat)); 12379566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Arhs)); 1238e5d3d808SBarry Smith ts->Arhs = Amat; 12390e4ef248SJed Brown } 1240e5d3d808SBarry Smith if (Pmat) { 12419566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)Pmat)); 12429566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Brhs)); 1243e5d3d808SBarry Smith ts->Brhs = Pmat; 12440e4ef248SJed Brown } 1245d763cef2SBarry Smith PetscFunctionReturn(0); 1246d763cef2SBarry Smith } 1247d763cef2SBarry Smith 1248316643e7SJed Brown /*@C 1249b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1250316643e7SJed Brown 12513f9fe445SBarry Smith Logically Collective on TS 1252316643e7SJed Brown 1253316643e7SJed Brown Input Parameters: 1254316643e7SJed Brown + ts - the TS context obtained from TSCreate() 12550298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1256316643e7SJed Brown . f - the function evaluation routine 12570298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1258316643e7SJed Brown 1259316643e7SJed Brown Calling sequence of f: 12606bc98fa9SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1261316643e7SJed Brown 1262316643e7SJed Brown + t - time at step/stage being solved 1263316643e7SJed Brown . u - state vector 1264316643e7SJed Brown . u_t - time derivative of state vector 1265316643e7SJed Brown . F - function vector 1266316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1267316643e7SJed Brown 1268316643e7SJed Brown Important: 12692bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1270316643e7SJed Brown 1271316643e7SJed Brown Level: beginner 1272316643e7SJed Brown 1273d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1274316643e7SJed Brown @*/ 127551699248SLisandro Dalcin PetscErrorCode TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx) 1276316643e7SJed Brown { 1277089b2837SJed Brown SNES snes; 127851699248SLisandro Dalcin Vec ralloc = NULL; 127924989b8cSPeter Brune DM dm; 1280316643e7SJed Brown 1281316643e7SJed Brown PetscFunctionBegin; 12820700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 128351699248SLisandro Dalcin if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 128424989b8cSPeter Brune 12859566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 12869566063dSJacob Faibussowitsch PetscCall(DMTSSetIFunction(dm,f,ctx)); 128724989b8cSPeter Brune 12889566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 128951699248SLisandro Dalcin if (!r && !ts->dm && ts->vec_sol) { 12909566063dSJacob Faibussowitsch PetscCall(VecDuplicate(ts->vec_sol,&ralloc)); 129151699248SLisandro Dalcin r = ralloc; 1292e856ceecSJed Brown } 12939566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(snes,r,SNESTSFormFunction,ts)); 12949566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ralloc)); 1295089b2837SJed Brown PetscFunctionReturn(0); 1296089b2837SJed Brown } 1297089b2837SJed Brown 1298089b2837SJed Brown /*@C 1299a5b23f4aSJose E. Roman TSGetIFunction - Returns the vector where the implicit residual is stored and the function/context to compute it. 1300089b2837SJed Brown 1301089b2837SJed Brown Not Collective 1302089b2837SJed Brown 1303089b2837SJed Brown Input Parameter: 1304089b2837SJed Brown . ts - the TS context 1305089b2837SJed Brown 1306d8d19677SJose E. Roman Output Parameters: 13070298fd71SBarry Smith + r - vector to hold residual (or NULL) 13080298fd71SBarry Smith . func - the function to compute residual (or NULL) 13090298fd71SBarry Smith - ctx - the function context (or NULL) 1310089b2837SJed Brown 1311089b2837SJed Brown Level: advanced 1312089b2837SJed Brown 1313089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1314089b2837SJed Brown @*/ 1315089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1316089b2837SJed Brown { 1317089b2837SJed Brown SNES snes; 131824989b8cSPeter Brune DM dm; 1319089b2837SJed Brown 1320089b2837SJed Brown PetscFunctionBegin; 1321089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13229566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 13239566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes,r,NULL,NULL)); 13249566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 13259566063dSJacob Faibussowitsch PetscCall(DMTSGetIFunction(dm,func,ctx)); 1326089b2837SJed Brown PetscFunctionReturn(0); 1327089b2837SJed Brown } 1328089b2837SJed Brown 1329089b2837SJed Brown /*@C 1330089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1331089b2837SJed Brown 1332089b2837SJed Brown Not Collective 1333089b2837SJed Brown 1334089b2837SJed Brown Input Parameter: 1335089b2837SJed Brown . ts - the TS context 1336089b2837SJed Brown 1337d8d19677SJose E. Roman Output Parameters: 13380298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 13390298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 13400298fd71SBarry Smith - ctx - the function context (or NULL) 1341089b2837SJed Brown 1342089b2837SJed Brown Level: advanced 1343089b2837SJed Brown 13442bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction() 1345089b2837SJed Brown @*/ 1346089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1347089b2837SJed Brown { 1348089b2837SJed Brown SNES snes; 134924989b8cSPeter Brune DM dm; 1350089b2837SJed Brown 1351089b2837SJed Brown PetscFunctionBegin; 1352089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 13539566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 13549566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes,r,NULL,NULL)); 13559566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 13569566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm,func,ctx)); 1357316643e7SJed Brown PetscFunctionReturn(0); 1358316643e7SJed Brown } 1359316643e7SJed Brown 1360316643e7SJed Brown /*@C 1361a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1362ae8867d6SBarry Smith provided with TSSetIFunction(). 1363316643e7SJed Brown 13643f9fe445SBarry Smith Logically Collective on TS 1365316643e7SJed Brown 1366316643e7SJed Brown Input Parameters: 1367316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1368e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1369e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1370316643e7SJed Brown . f - the Jacobian evaluation routine 13710298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1372316643e7SJed Brown 1373316643e7SJed Brown Calling sequence of f: 13746bc98fa9SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1375316643e7SJed Brown 1376316643e7SJed Brown + t - time at step/stage being solved 13771b4a444bSJed Brown . U - state vector 13781b4a444bSJed Brown . U_t - time derivative of state vector 1379316643e7SJed Brown . a - shift 1380e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1381e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1382316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1383316643e7SJed Brown 1384316643e7SJed Brown Notes: 1385e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1386316643e7SJed Brown 1387895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 1388895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 1389895c21f2SBarry Smith 1390a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1391b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1392a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1393a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1394a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1395a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1396a4f0a591SBarry Smith 13976cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 13986cd88445SBarry Smith 13996cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1400ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1401ca5f011dSBarry Smith 1402316643e7SJed Brown Level: beginner 1403316643e7SJed Brown 1404ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1405316643e7SJed Brown 1406316643e7SJed Brown @*/ 1407e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1408316643e7SJed Brown { 1409089b2837SJed Brown SNES snes; 141024989b8cSPeter Brune DM dm; 1411316643e7SJed Brown 1412316643e7SJed Brown PetscFunctionBegin; 14130700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1414e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1415e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1416e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1417e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 141824989b8cSPeter Brune 14199566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 14209566063dSJacob Faibussowitsch PetscCall(DMTSSetIJacobian(dm,f,ctx)); 142124989b8cSPeter Brune 14229566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 14239566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts)); 1424316643e7SJed Brown PetscFunctionReturn(0); 1425316643e7SJed Brown } 1426316643e7SJed Brown 1427e1244c69SJed Brown /*@ 1428e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1429e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1430e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1431e1244c69SJed Brown not been changed by the TS. 1432e1244c69SJed Brown 1433e1244c69SJed Brown Logically Collective 1434e1244c69SJed Brown 14354165533cSJose E. Roman Input Parameters: 1436e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1437e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1438e1244c69SJed Brown 1439e1244c69SJed Brown Level: intermediate 1440e1244c69SJed Brown 1441e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1442e1244c69SJed Brown @*/ 1443e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1444e1244c69SJed Brown { 1445e1244c69SJed Brown PetscFunctionBegin; 1446e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1447e1244c69SJed Brown PetscFunctionReturn(0); 1448e1244c69SJed Brown } 1449e1244c69SJed Brown 1450efe9872eSLisandro Dalcin /*@C 1451efe9872eSLisandro Dalcin TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved. 1452efe9872eSLisandro Dalcin 1453efe9872eSLisandro Dalcin Logically Collective on TS 1454efe9872eSLisandro Dalcin 1455efe9872eSLisandro Dalcin Input Parameters: 1456efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1457efe9872eSLisandro Dalcin . F - vector to hold the residual (or NULL to have it created internally) 1458efe9872eSLisandro Dalcin . fun - the function evaluation routine 1459efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1460efe9872eSLisandro Dalcin 1461efe9872eSLisandro Dalcin Calling sequence of fun: 14626bc98fa9SBarry Smith $ PetscErrorCode fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx); 1463efe9872eSLisandro Dalcin 1464efe9872eSLisandro Dalcin + t - time at step/stage being solved 1465efe9872eSLisandro Dalcin . U - state vector 1466efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1467efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1468efe9872eSLisandro Dalcin . F - function vector 1469efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine (may be NULL) 1470efe9872eSLisandro Dalcin 1471efe9872eSLisandro Dalcin Level: beginner 1472efe9872eSLisandro Dalcin 1473a96d6ef6SBarry Smith .seealso: TSSetI2Jacobian(), TSSetIFunction(), TSCreate(), TSSetRHSFunction() 1474efe9872eSLisandro Dalcin @*/ 1475efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx) 1476efe9872eSLisandro Dalcin { 1477efe9872eSLisandro Dalcin DM dm; 1478efe9872eSLisandro Dalcin 1479efe9872eSLisandro Dalcin PetscFunctionBegin; 1480efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1481efe9872eSLisandro Dalcin if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2); 14829566063dSJacob Faibussowitsch PetscCall(TSSetIFunction(ts,F,NULL,NULL)); 14839566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 14849566063dSJacob Faibussowitsch PetscCall(DMTSSetI2Function(dm,fun,ctx)); 1485efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1486efe9872eSLisandro Dalcin } 1487efe9872eSLisandro Dalcin 1488efe9872eSLisandro Dalcin /*@C 1489a5b23f4aSJose E. Roman TSGetI2Function - Returns the vector where the implicit residual is stored and the function/context to compute it. 1490efe9872eSLisandro Dalcin 1491efe9872eSLisandro Dalcin Not Collective 1492efe9872eSLisandro Dalcin 1493efe9872eSLisandro Dalcin Input Parameter: 1494efe9872eSLisandro Dalcin . ts - the TS context 1495efe9872eSLisandro Dalcin 1496d8d19677SJose E. Roman Output Parameters: 1497efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL) 1498efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL) 1499efe9872eSLisandro Dalcin - ctx - the function context (or NULL) 1500efe9872eSLisandro Dalcin 1501efe9872eSLisandro Dalcin Level: advanced 1502efe9872eSLisandro Dalcin 1503a96d6ef6SBarry Smith .seealso: TSSetIFunction(), SNESGetFunction(), TSCreate() 1504efe9872eSLisandro Dalcin @*/ 1505efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx) 1506efe9872eSLisandro Dalcin { 1507efe9872eSLisandro Dalcin SNES snes; 1508efe9872eSLisandro Dalcin DM dm; 1509efe9872eSLisandro Dalcin 1510efe9872eSLisandro Dalcin PetscFunctionBegin; 1511efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 15129566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 15139566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes,r,NULL,NULL)); 15149566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 15159566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Function(dm,fun,ctx)); 1516efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1517efe9872eSLisandro Dalcin } 1518efe9872eSLisandro Dalcin 1519efe9872eSLisandro Dalcin /*@C 1520bc77d74cSLisandro Dalcin TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t + a*dF/dU_tt 1521efe9872eSLisandro Dalcin where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function(). 1522efe9872eSLisandro Dalcin 1523efe9872eSLisandro Dalcin Logically Collective on TS 1524efe9872eSLisandro Dalcin 1525efe9872eSLisandro Dalcin Input Parameters: 1526efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1527efe9872eSLisandro Dalcin . J - Jacobian matrix 1528efe9872eSLisandro Dalcin . P - preconditioning matrix for J (may be same as J) 1529efe9872eSLisandro Dalcin . jac - the Jacobian evaluation routine 1530efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1531efe9872eSLisandro Dalcin 1532efe9872eSLisandro Dalcin Calling sequence of jac: 15336bc98fa9SBarry 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); 1534efe9872eSLisandro Dalcin 1535efe9872eSLisandro Dalcin + t - time at step/stage being solved 1536efe9872eSLisandro Dalcin . U - state vector 1537efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1538efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1539efe9872eSLisandro Dalcin . v - shift for U_t 1540efe9872eSLisandro Dalcin . a - shift for U_tt 1541efe9872eSLisandro 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 1542efe9872eSLisandro Dalcin . P - preconditioning matrix for J, may be same as J 1543efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine 1544efe9872eSLisandro Dalcin 1545efe9872eSLisandro Dalcin Notes: 1546efe9872eSLisandro Dalcin The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve. 1547efe9872eSLisandro Dalcin 1548efe9872eSLisandro Dalcin The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be 1549efe9872eSLisandro 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. 1550efe9872eSLisandro Dalcin The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U where the positive "shift" 1551bc77d74cSLisandro Dalcin parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states. 1552efe9872eSLisandro Dalcin 1553efe9872eSLisandro Dalcin Level: beginner 1554efe9872eSLisandro Dalcin 1555a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Jacobian() 1556efe9872eSLisandro Dalcin @*/ 1557efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx) 1558efe9872eSLisandro Dalcin { 1559efe9872eSLisandro Dalcin DM dm; 1560efe9872eSLisandro Dalcin 1561efe9872eSLisandro Dalcin PetscFunctionBegin; 1562efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1563efe9872eSLisandro Dalcin if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2); 1564efe9872eSLisandro Dalcin if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3); 15659566063dSJacob Faibussowitsch PetscCall(TSSetIJacobian(ts,J,P,NULL,NULL)); 15669566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 15679566063dSJacob Faibussowitsch PetscCall(DMTSSetI2Jacobian(dm,jac,ctx)); 1568efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1569efe9872eSLisandro Dalcin } 1570efe9872eSLisandro Dalcin 1571efe9872eSLisandro Dalcin /*@C 1572efe9872eSLisandro Dalcin TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep. 1573efe9872eSLisandro Dalcin 1574efe9872eSLisandro Dalcin Not Collective, but parallel objects are returned if TS is parallel 1575efe9872eSLisandro Dalcin 1576efe9872eSLisandro Dalcin Input Parameter: 1577efe9872eSLisandro Dalcin . ts - The TS context obtained from TSCreate() 1578efe9872eSLisandro Dalcin 1579efe9872eSLisandro Dalcin Output Parameters: 1580efe9872eSLisandro Dalcin + J - The (approximate) Jacobian of F(t,U,U_t,U_tt) 1581efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J 1582efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices 1583efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine 1584efe9872eSLisandro Dalcin 158595452b02SPatrick Sanan Notes: 158695452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 1587efe9872eSLisandro Dalcin 1588efe9872eSLisandro Dalcin Level: advanced 1589efe9872eSLisandro Dalcin 1590a96d6ef6SBarry Smith .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber(), TSSetI2Jacobian(), TSGetI2Function(), TSCreate() 1591efe9872eSLisandro Dalcin 1592efe9872eSLisandro Dalcin @*/ 1593efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx) 1594efe9872eSLisandro Dalcin { 1595efe9872eSLisandro Dalcin SNES snes; 1596efe9872eSLisandro Dalcin DM dm; 1597efe9872eSLisandro Dalcin 1598efe9872eSLisandro Dalcin PetscFunctionBegin; 15999566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 16009566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 16019566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes,J,P,NULL,NULL)); 16029566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 16039566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm,jac,ctx)); 1604efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1605efe9872eSLisandro Dalcin } 1606efe9872eSLisandro Dalcin 1607efe9872eSLisandro Dalcin /*@ 1608efe9872eSLisandro Dalcin TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0 1609efe9872eSLisandro Dalcin 1610d083f849SBarry Smith Collective on TS 1611efe9872eSLisandro Dalcin 1612efe9872eSLisandro Dalcin Input Parameters: 1613efe9872eSLisandro Dalcin + ts - the TS context 1614efe9872eSLisandro Dalcin . t - current time 1615efe9872eSLisandro Dalcin . U - state vector 1616efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t) 1617efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt) 1618efe9872eSLisandro Dalcin 1619efe9872eSLisandro Dalcin Output Parameter: 1620efe9872eSLisandro Dalcin . F - the residual vector 1621efe9872eSLisandro Dalcin 1622efe9872eSLisandro Dalcin Note: 1623efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1624efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1625efe9872eSLisandro Dalcin 1626efe9872eSLisandro Dalcin Level: developer 1627efe9872eSLisandro Dalcin 1628a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Function() 1629efe9872eSLisandro Dalcin @*/ 1630efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F) 1631efe9872eSLisandro Dalcin { 1632efe9872eSLisandro Dalcin DM dm; 1633efe9872eSLisandro Dalcin TSI2Function I2Function; 1634efe9872eSLisandro Dalcin void *ctx; 1635efe9872eSLisandro Dalcin TSRHSFunction rhsfunction; 1636efe9872eSLisandro Dalcin 1637efe9872eSLisandro Dalcin PetscFunctionBegin; 1638efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1639efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1640efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1641efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1642efe9872eSLisandro Dalcin PetscValidHeaderSpecific(F,VEC_CLASSID,6); 1643efe9872eSLisandro Dalcin 16449566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 16459566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Function(dm,&I2Function,&ctx)); 16469566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm,&rhsfunction,NULL)); 1647efe9872eSLisandro Dalcin 1648efe9872eSLisandro Dalcin if (!I2Function) { 16499566063dSJacob Faibussowitsch PetscCall(TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE)); 1650efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1651efe9872eSLisandro Dalcin } 1652efe9872eSLisandro Dalcin 16539566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_FunctionEval,ts,U,V,F)); 1654efe9872eSLisandro Dalcin 1655efe9872eSLisandro Dalcin PetscStackPush("TS user implicit function"); 16569566063dSJacob Faibussowitsch PetscCall(I2Function(ts,t,U,V,A,F,ctx)); 1657efe9872eSLisandro Dalcin PetscStackPop; 1658efe9872eSLisandro Dalcin 1659efe9872eSLisandro Dalcin if (rhsfunction) { 1660efe9872eSLisandro Dalcin Vec Frhs; 16619566063dSJacob Faibussowitsch PetscCall(TSGetRHSVec_Private(ts,&Frhs)); 16629566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts,t,U,Frhs)); 16639566063dSJacob Faibussowitsch PetscCall(VecAXPY(F,-1,Frhs)); 1664efe9872eSLisandro Dalcin } 1665efe9872eSLisandro Dalcin 16669566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_FunctionEval,ts,U,V,F)); 1667efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1668efe9872eSLisandro Dalcin } 1669efe9872eSLisandro Dalcin 1670efe9872eSLisandro Dalcin /*@ 1671efe9872eSLisandro Dalcin TSComputeI2Jacobian - Evaluates the Jacobian of the DAE 1672efe9872eSLisandro Dalcin 1673d083f849SBarry Smith Collective on TS 1674efe9872eSLisandro Dalcin 1675efe9872eSLisandro Dalcin Input Parameters: 1676efe9872eSLisandro Dalcin + ts - the TS context 1677efe9872eSLisandro Dalcin . t - current timestep 1678efe9872eSLisandro Dalcin . U - state vector 1679efe9872eSLisandro Dalcin . V - time derivative of state vector 1680efe9872eSLisandro Dalcin . A - second time derivative of state vector 1681efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below 1682efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below 1683efe9872eSLisandro Dalcin 1684efe9872eSLisandro Dalcin Output Parameters: 1685efe9872eSLisandro Dalcin + J - Jacobian matrix 1686efe9872eSLisandro Dalcin - P - optional preconditioning matrix 1687efe9872eSLisandro Dalcin 1688efe9872eSLisandro Dalcin Notes: 1689efe9872eSLisandro Dalcin If F(t,U,V,A)=0 is the DAE, the required Jacobian is 1690efe9872eSLisandro Dalcin 1691efe9872eSLisandro Dalcin dF/dU + shiftV*dF/dV + shiftA*dF/dA 1692efe9872eSLisandro Dalcin 1693efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1694efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1695efe9872eSLisandro Dalcin 1696efe9872eSLisandro Dalcin Level: developer 1697efe9872eSLisandro Dalcin 1698efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1699efe9872eSLisandro Dalcin @*/ 1700efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P) 1701efe9872eSLisandro Dalcin { 1702efe9872eSLisandro Dalcin DM dm; 1703efe9872eSLisandro Dalcin TSI2Jacobian I2Jacobian; 1704efe9872eSLisandro Dalcin void *ctx; 1705efe9872eSLisandro Dalcin TSRHSJacobian rhsjacobian; 1706efe9872eSLisandro Dalcin 1707efe9872eSLisandro Dalcin PetscFunctionBegin; 1708efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1709efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1710efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1711efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1712efe9872eSLisandro Dalcin PetscValidHeaderSpecific(J,MAT_CLASSID,8); 1713efe9872eSLisandro Dalcin PetscValidHeaderSpecific(P,MAT_CLASSID,9); 1714efe9872eSLisandro Dalcin 17159566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 17169566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx)); 17179566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm,&rhsjacobian,NULL)); 1718efe9872eSLisandro Dalcin 1719efe9872eSLisandro Dalcin if (!I2Jacobian) { 17209566063dSJacob Faibussowitsch PetscCall(TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE)); 1721efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1722efe9872eSLisandro Dalcin } 1723efe9872eSLisandro Dalcin 17249566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_JacobianEval,ts,U,J,P)); 1725efe9872eSLisandro Dalcin 1726efe9872eSLisandro Dalcin PetscStackPush("TS user implicit Jacobian"); 17279566063dSJacob Faibussowitsch PetscCall(I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx)); 1728efe9872eSLisandro Dalcin PetscStackPop; 1729efe9872eSLisandro Dalcin 1730efe9872eSLisandro Dalcin if (rhsjacobian) { 1731d60b7d5cSBarry Smith Mat Jrhs,Prhs; 17329566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts,&Jrhs,&Prhs)); 17339566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs)); 17349566063dSJacob Faibussowitsch PetscCall(MatAXPY(J,-1,Jrhs,ts->axpy_pattern)); 17359566063dSJacob Faibussowitsch if (P != J) PetscCall(MatAXPY(P,-1,Prhs,ts->axpy_pattern)); 1736efe9872eSLisandro Dalcin } 1737efe9872eSLisandro Dalcin 17389566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_JacobianEval,ts,U,J,P)); 1739efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1740efe9872eSLisandro Dalcin } 1741efe9872eSLisandro Dalcin 1742438f35afSJed Brown /*@C 1743438f35afSJed Brown TSSetTransientVariable - sets function to transform from state to transient variables 1744438f35afSJed Brown 1745438f35afSJed Brown Logically Collective 1746438f35afSJed Brown 17474165533cSJose E. Roman Input Parameters: 1748438f35afSJed Brown + ts - time stepping context on which to change the transient variable 1749a96d6ef6SBarry Smith . tvar - a function that transforms to transient variables 1750438f35afSJed Brown - ctx - a context for tvar 1751438f35afSJed Brown 1752a96d6ef6SBarry Smith Calling sequence of tvar: 1753a96d6ef6SBarry Smith $ PetscErrorCode tvar(TS ts,Vec p,Vec c,void *ctx); 1754a96d6ef6SBarry Smith 1755a96d6ef6SBarry Smith + ts - timestep context 1756a96d6ef6SBarry Smith . p - input vector (primative form) 1757a96d6ef6SBarry Smith . c - output vector, transient variables (conservative form) 1758a96d6ef6SBarry Smith - ctx - [optional] user-defined function context 1759a96d6ef6SBarry Smith 1760438f35afSJed Brown Level: advanced 1761438f35afSJed Brown 1762438f35afSJed Brown Notes: 1763438f35afSJed Brown This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., TSBDF) 1764438f35afSJed Brown can be conservative. In this context, primitive variables P are used to model the state (e.g., because they lead to 1765438f35afSJed Brown well-conditioned formulations even in limiting cases such as low-Mach or zero porosity). The transient variable is 1766438f35afSJed Brown C(P), specified by calling this function. An IFunction thus receives arguments (P, Cdot) and the IJacobian must be 1767438f35afSJed Brown evaluated via the chain rule, as in 1768438f35afSJed Brown 1769438f35afSJed Brown dF/dP + shift * dF/dCdot dC/dP. 1770438f35afSJed Brown 1771438f35afSJed Brown .seealso: DMTSSetTransientVariable(), DMTSGetTransientVariable(), TSSetIFunction(), TSSetIJacobian() 1772438f35afSJed Brown @*/ 1773438f35afSJed Brown PetscErrorCode TSSetTransientVariable(TS ts,TSTransientVariable tvar,void *ctx) 1774438f35afSJed Brown { 1775438f35afSJed Brown DM dm; 1776438f35afSJed Brown 1777438f35afSJed Brown PetscFunctionBegin; 1778438f35afSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 17799566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 17809566063dSJacob Faibussowitsch PetscCall(DMTSSetTransientVariable(dm,tvar,ctx)); 1781438f35afSJed Brown PetscFunctionReturn(0); 1782438f35afSJed Brown } 1783438f35afSJed Brown 1784efe9872eSLisandro Dalcin /*@ 1785e3c11fc1SJed Brown TSComputeTransientVariable - transforms state (primitive) variables to transient (conservative) variables 1786e3c11fc1SJed Brown 1787e3c11fc1SJed Brown Logically Collective 1788e3c11fc1SJed Brown 1789e3c11fc1SJed Brown Input Parameters: 1790e3c11fc1SJed Brown + ts - TS on which to compute 1791e3c11fc1SJed Brown - U - state vector to be transformed to transient variables 1792e3c11fc1SJed Brown 1793e3c11fc1SJed Brown Output Parameters: 1794e3c11fc1SJed Brown . C - transient (conservative) variable 1795e3c11fc1SJed Brown 1796e3c11fc1SJed Brown Developer Notes: 1797e3c11fc1SJed Brown If DMTSSetTransientVariable() has not been called, then C is not modified in this routine and C=NULL is allowed. 1798e3c11fc1SJed Brown This makes it safe to call without a guard. One can use TSHasTransientVariable() to check if transient variables are 1799e3c11fc1SJed Brown being used. 1800e3c11fc1SJed Brown 1801e3c11fc1SJed Brown Level: developer 1802e3c11fc1SJed Brown 1803e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeIFunction(), TSComputeIJacobian() 1804e3c11fc1SJed Brown @*/ 1805e3c11fc1SJed Brown PetscErrorCode TSComputeTransientVariable(TS ts,Vec U,Vec C) 1806e3c11fc1SJed Brown { 1807e3c11fc1SJed Brown DM dm; 1808e3c11fc1SJed Brown DMTS dmts; 1809e3c11fc1SJed Brown 1810e3c11fc1SJed Brown PetscFunctionBegin; 1811e3c11fc1SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1812e3c11fc1SJed Brown PetscValidHeaderSpecific(U,VEC_CLASSID,2); 18139566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 18149566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm,&dmts)); 1815e3c11fc1SJed Brown if (dmts->ops->transientvar) { 1816e3c11fc1SJed Brown PetscValidHeaderSpecific(C,VEC_CLASSID,3); 18179566063dSJacob Faibussowitsch PetscCall((*dmts->ops->transientvar)(ts,U,C,dmts->transientvarctx)); 1818e3c11fc1SJed Brown } 1819e3c11fc1SJed Brown PetscFunctionReturn(0); 1820e3c11fc1SJed Brown } 1821e3c11fc1SJed Brown 1822e3c11fc1SJed Brown /*@ 1823e3c11fc1SJed Brown TSHasTransientVariable - determine whether transient variables have been set 1824e3c11fc1SJed Brown 1825e3c11fc1SJed Brown Logically Collective 1826e3c11fc1SJed Brown 1827e3c11fc1SJed Brown Input Parameters: 1828e3c11fc1SJed Brown . ts - TS on which to compute 1829e3c11fc1SJed Brown 1830e3c11fc1SJed Brown Output Parameters: 1831e3c11fc1SJed Brown . has - PETSC_TRUE if transient variables have been set 1832e3c11fc1SJed Brown 1833e3c11fc1SJed Brown Level: developer 1834e3c11fc1SJed Brown 1835e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeTransientVariable() 1836e3c11fc1SJed Brown @*/ 1837e3c11fc1SJed Brown PetscErrorCode TSHasTransientVariable(TS ts,PetscBool *has) 1838e3c11fc1SJed Brown { 1839e3c11fc1SJed Brown DM dm; 1840e3c11fc1SJed Brown DMTS dmts; 1841e3c11fc1SJed Brown 1842e3c11fc1SJed Brown PetscFunctionBegin; 1843e3c11fc1SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 18449566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 18459566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm,&dmts)); 1846e3c11fc1SJed Brown *has = dmts->ops->transientvar ? PETSC_TRUE : PETSC_FALSE; 1847e3c11fc1SJed Brown PetscFunctionReturn(0); 1848e3c11fc1SJed Brown } 1849e3c11fc1SJed Brown 1850e3c11fc1SJed Brown /*@ 1851efe9872eSLisandro Dalcin TS2SetSolution - Sets the initial solution and time derivative vectors 1852efe9872eSLisandro Dalcin for use by the TS routines handling second order equations. 1853efe9872eSLisandro Dalcin 1854d083f849SBarry Smith Logically Collective on TS 1855efe9872eSLisandro Dalcin 1856efe9872eSLisandro Dalcin Input Parameters: 1857efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1858efe9872eSLisandro Dalcin . u - the solution vector 1859efe9872eSLisandro Dalcin - v - the time derivative vector 1860efe9872eSLisandro Dalcin 1861efe9872eSLisandro Dalcin Level: beginner 1862efe9872eSLisandro Dalcin 1863efe9872eSLisandro Dalcin @*/ 1864efe9872eSLisandro Dalcin PetscErrorCode TS2SetSolution(TS ts,Vec u,Vec v) 1865efe9872eSLisandro Dalcin { 1866efe9872eSLisandro Dalcin PetscFunctionBegin; 1867efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1868efe9872eSLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 1869efe9872eSLisandro Dalcin PetscValidHeaderSpecific(v,VEC_CLASSID,3); 18709566063dSJacob Faibussowitsch PetscCall(TSSetSolution(ts,u)); 18719566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)v)); 18729566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_dot)); 1873efe9872eSLisandro Dalcin ts->vec_dot = v; 1874efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1875efe9872eSLisandro Dalcin } 1876efe9872eSLisandro Dalcin 1877efe9872eSLisandro Dalcin /*@ 1878efe9872eSLisandro Dalcin TS2GetSolution - Returns the solution and time derivative at the present timestep 1879efe9872eSLisandro Dalcin for second order equations. It is valid to call this routine inside the function 1880efe9872eSLisandro Dalcin that you are evaluating in order to move to the new timestep. This vector not 1881efe9872eSLisandro Dalcin changed until the solution at the next timestep has been calculated. 1882efe9872eSLisandro Dalcin 1883efe9872eSLisandro Dalcin Not Collective, but Vec returned is parallel if TS is parallel 1884efe9872eSLisandro Dalcin 1885efe9872eSLisandro Dalcin Input Parameter: 1886efe9872eSLisandro Dalcin . ts - the TS context obtained from TSCreate() 1887efe9872eSLisandro Dalcin 1888d8d19677SJose E. Roman Output Parameters: 1889efe9872eSLisandro Dalcin + u - the vector containing the solution 1890efe9872eSLisandro Dalcin - v - the vector containing the time derivative 1891efe9872eSLisandro Dalcin 1892efe9872eSLisandro Dalcin Level: intermediate 1893efe9872eSLisandro Dalcin 1894efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime() 1895efe9872eSLisandro Dalcin 1896efe9872eSLisandro Dalcin @*/ 1897efe9872eSLisandro Dalcin PetscErrorCode TS2GetSolution(TS ts,Vec *u,Vec *v) 1898efe9872eSLisandro Dalcin { 1899efe9872eSLisandro Dalcin PetscFunctionBegin; 1900efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1901efe9872eSLisandro Dalcin if (u) PetscValidPointer(u,2); 1902efe9872eSLisandro Dalcin if (v) PetscValidPointer(v,3); 1903efe9872eSLisandro Dalcin if (u) *u = ts->vec_sol; 1904efe9872eSLisandro Dalcin if (v) *v = ts->vec_dot; 1905efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1906efe9872eSLisandro Dalcin } 1907efe9872eSLisandro Dalcin 190855849f57SBarry Smith /*@C 190955849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 191055849f57SBarry Smith 191155849f57SBarry Smith Collective on PetscViewer 191255849f57SBarry Smith 191355849f57SBarry Smith Input Parameters: 191455849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 191555849f57SBarry Smith some related function before a call to TSLoad(). 191655849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 191755849f57SBarry Smith 191855849f57SBarry Smith Level: intermediate 191955849f57SBarry Smith 192055849f57SBarry Smith Notes: 192155849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 192255849f57SBarry Smith 192355849f57SBarry Smith Notes for advanced users: 192455849f57SBarry Smith Most users should not need to know the details of the binary storage 192555849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 192655849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 192755849f57SBarry Smith format is 192855849f57SBarry Smith .vb 192955849f57SBarry Smith has not yet been determined 193055849f57SBarry Smith .ve 193155849f57SBarry Smith 193255849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 193355849f57SBarry Smith @*/ 1934f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 193555849f57SBarry Smith { 193655849f57SBarry Smith PetscBool isbinary; 193755849f57SBarry Smith PetscInt classid; 193855849f57SBarry Smith char type[256]; 19392d53ad75SBarry Smith DMTS sdm; 1940ad6bc421SBarry Smith DM dm; 194155849f57SBarry Smith 194255849f57SBarry Smith PetscFunctionBegin; 1943f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 194455849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 19459566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary)); 19463c633725SBarry Smith PetscCheck(isbinary,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 194755849f57SBarry Smith 19489566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT)); 19493c633725SBarry Smith PetscCheck(classid == TS_FILE_CLASSID,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 19509566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR)); 19519566063dSJacob Faibussowitsch PetscCall(TSSetType(ts, type)); 1952f2c2a1b9SBarry Smith if (ts->ops->load) { 19539566063dSJacob Faibussowitsch PetscCall((*ts->ops->load)(ts,viewer)); 1954f2c2a1b9SBarry Smith } 19559566063dSJacob Faibussowitsch PetscCall(DMCreate(PetscObjectComm((PetscObject)ts),&dm)); 19569566063dSJacob Faibussowitsch PetscCall(DMLoad(dm,viewer)); 19579566063dSJacob Faibussowitsch PetscCall(TSSetDM(ts,dm)); 19589566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(ts->dm,&ts->vec_sol)); 19599566063dSJacob Faibussowitsch PetscCall(VecLoad(ts->vec_sol,viewer)); 19609566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm,&sdm)); 19619566063dSJacob Faibussowitsch PetscCall(DMTSLoad(sdm,viewer)); 196255849f57SBarry Smith PetscFunctionReturn(0); 196355849f57SBarry Smith } 196455849f57SBarry Smith 19659804daf3SBarry Smith #include <petscdraw.h> 1966e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1967e04113cfSBarry Smith #include <petscviewersaws.h> 1968f05ece33SBarry Smith #endif 1969fe2efc57SMark 1970fe2efc57SMark /*@C 1971fe2efc57SMark TSViewFromOptions - View from Options 1972fe2efc57SMark 1973fe2efc57SMark Collective on TS 1974fe2efc57SMark 1975fe2efc57SMark Input Parameters: 1976fe2efc57SMark + A - the application ordering context 1977736c3998SJose E. Roman . obj - Optional object 1978736c3998SJose E. Roman - name - command line option 1979fe2efc57SMark 1980fe2efc57SMark Level: intermediate 1981fe2efc57SMark .seealso: TS, TSView, PetscObjectViewFromOptions(), TSCreate() 1982fe2efc57SMark @*/ 1983fe2efc57SMark PetscErrorCode TSViewFromOptions(TS A,PetscObject obj,const char name[]) 1984fe2efc57SMark { 1985fe2efc57SMark PetscFunctionBegin; 1986fe2efc57SMark PetscValidHeaderSpecific(A,TS_CLASSID,1); 19879566063dSJacob Faibussowitsch PetscCall(PetscObjectViewFromOptions((PetscObject)A,obj,name)); 1988fe2efc57SMark PetscFunctionReturn(0); 1989fe2efc57SMark } 1990fe2efc57SMark 19917e2c5f70SBarry Smith /*@C 1992d763cef2SBarry Smith TSView - Prints the TS data structure. 1993d763cef2SBarry Smith 19944c49b128SBarry Smith Collective on TS 1995d763cef2SBarry Smith 1996d763cef2SBarry Smith Input Parameters: 1997d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1998d763cef2SBarry Smith - viewer - visualization context 1999d763cef2SBarry Smith 2000d763cef2SBarry Smith Options Database Key: 2001d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 2002d763cef2SBarry Smith 2003d763cef2SBarry Smith Notes: 2004d763cef2SBarry Smith The available visualization contexts include 2005b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 2006b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 2007d763cef2SBarry Smith output where only the first processor opens 2008d763cef2SBarry Smith the file. All other processors send their 2009d763cef2SBarry Smith data to the first processor to print. 2010d763cef2SBarry Smith 2011d763cef2SBarry Smith The user can open an alternative visualization context with 2012b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 2013d763cef2SBarry Smith 2014595c91d4SBarry Smith In the debugger you can do "call TSView(ts,0)" to display the TS solver. (The same holds for any PETSc object viewer). 2015595c91d4SBarry Smith 2016d763cef2SBarry Smith Level: beginner 2017d763cef2SBarry Smith 2018b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 2019d763cef2SBarry Smith @*/ 20207087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 2021d763cef2SBarry Smith { 202219fd82e9SBarry Smith TSType type; 20232b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 20242d53ad75SBarry Smith DMTS sdm; 2025e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 2026536b137fSBarry Smith PetscBool issaws; 2027f05ece33SBarry Smith #endif 2028d763cef2SBarry Smith 2029d763cef2SBarry Smith PetscFunctionBegin; 20300700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20313050cee2SBarry Smith if (!viewer) { 20329566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer)); 20333050cee2SBarry Smith } 20340700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 2035c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 2036fd16b177SBarry Smith 20379566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii)); 20389566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring)); 20399566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary)); 20409566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw)); 2041e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 20429566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws)); 2043f05ece33SBarry Smith #endif 204432077d6dSBarry Smith if (iascii) { 20459566063dSJacob Faibussowitsch PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer)); 2046efd4aadfSBarry Smith if (ts->ops->view) { 20479566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 20489566063dSJacob Faibussowitsch PetscCall((*ts->ops->view)(ts,viewer)); 20499566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 2050efd4aadfSBarry Smith } 2051ef85077eSLisandro Dalcin if (ts->max_steps < PETSC_MAX_INT) { 20529566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps)); 2053ef85077eSLisandro Dalcin } 2054ef85077eSLisandro Dalcin if (ts->max_time < PETSC_MAX_REAL) { 20559566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time)); 2056ef85077eSLisandro Dalcin } 2057a6ab3590SBarry Smith if (ts->ifuncs) { 20589566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of I function evaluations=%D\n",ts->ifuncs)); 2059a6ab3590SBarry Smith } 2060a6ab3590SBarry Smith if (ts->ijacs) { 20619566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of I Jacobian evaluations=%D\n",ts->ijacs)); 2062a6ab3590SBarry Smith } 2063a6ab3590SBarry Smith if (ts->rhsfuncs) { 20649566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of RHS function evaluations=%D\n",ts->rhsfuncs)); 2065a6ab3590SBarry Smith } 2066a6ab3590SBarry Smith if (ts->rhsjacs) { 20679566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of RHS Jacobian evaluations=%D\n",ts->rhsjacs)); 2068a6ab3590SBarry Smith } 2069efd4aadfSBarry Smith if (ts->usessnes) { 2070efd4aadfSBarry Smith PetscBool lin; 2071d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 20729566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its)); 2073d763cef2SBarry Smith } 20749566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its)); 20759566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"")); 20769566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures)); 2077efd4aadfSBarry Smith } 20789566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject)); 2079a0af407cSBarry Smith if (ts->vrtol) { 20809566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," using vector of relative error tolerances, ")); 2081a0af407cSBarry Smith } else { 20829566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," using relative error tolerance of %g, ",(double)ts->rtol)); 2083a0af407cSBarry Smith } 2084a0af407cSBarry Smith if (ts->vatol) { 20859566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," using vector of absolute error tolerances\n")); 2086a0af407cSBarry Smith } else { 20879566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," using absolute error tolerance of %g\n",(double)ts->atol)); 2088a0af407cSBarry Smith } 20899566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 20909566063dSJacob Faibussowitsch PetscCall(TSAdaptView(ts->adapt,viewer)); 20919566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 20920f5bd95cSBarry Smith } else if (isstring) { 20939566063dSJacob Faibussowitsch PetscCall(TSGetType(ts,&type)); 20949566063dSJacob Faibussowitsch PetscCall(PetscViewerStringSPrintf(viewer," TSType: %-7.7s",type)); 20959566063dSJacob Faibussowitsch if (ts->ops->view) PetscCall((*ts->ops->view)(ts,viewer)); 209655849f57SBarry Smith } else if (isbinary) { 209755849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 209855849f57SBarry Smith MPI_Comm comm; 209955849f57SBarry Smith PetscMPIInt rank; 210055849f57SBarry Smith char type[256]; 210155849f57SBarry Smith 21029566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)ts,&comm)); 21039566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm,&rank)); 2104dd400576SPatrick Sanan if (rank == 0) { 21059566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT)); 21069566063dSJacob Faibussowitsch PetscCall(PetscStrncpy(type,((PetscObject)ts)->type_name,256)); 21079566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR)); 210855849f57SBarry Smith } 210955849f57SBarry Smith if (ts->ops->view) { 21109566063dSJacob Faibussowitsch PetscCall((*ts->ops->view)(ts,viewer)); 211155849f57SBarry Smith } 21129566063dSJacob Faibussowitsch if (ts->adapt) PetscCall(TSAdaptView(ts->adapt,viewer)); 21139566063dSJacob Faibussowitsch PetscCall(DMView(ts->dm,viewer)); 21149566063dSJacob Faibussowitsch PetscCall(VecView(ts->vec_sol,viewer)); 21159566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm,&sdm)); 21169566063dSJacob Faibussowitsch PetscCall(DMTSView(sdm,viewer)); 21172b0a91c0SBarry Smith } else if (isdraw) { 21182b0a91c0SBarry Smith PetscDraw draw; 21192b0a91c0SBarry Smith char str[36]; 212089fd9fafSBarry Smith PetscReal x,y,bottom,h; 21212b0a91c0SBarry Smith 21229566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(viewer,0,&draw)); 21239566063dSJacob Faibussowitsch PetscCall(PetscDrawGetCurrentPoint(draw,&x,&y)); 21249566063dSJacob Faibussowitsch PetscCall(PetscStrcpy(str,"TS: ")); 21259566063dSJacob Faibussowitsch PetscCall(PetscStrcat(str,((PetscObject)ts)->type_name)); 21269566063dSJacob Faibussowitsch PetscCall(PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h)); 212789fd9fafSBarry Smith bottom = y - h; 21289566063dSJacob Faibussowitsch PetscCall(PetscDrawPushCurrentPoint(draw,x,bottom)); 21292b0a91c0SBarry Smith if (ts->ops->view) { 21309566063dSJacob Faibussowitsch PetscCall((*ts->ops->view)(ts,viewer)); 21312b0a91c0SBarry Smith } 21329566063dSJacob Faibussowitsch if (ts->adapt) PetscCall(TSAdaptView(ts->adapt,viewer)); 21339566063dSJacob Faibussowitsch if (ts->snes) PetscCall(SNESView(ts->snes,viewer)); 21349566063dSJacob Faibussowitsch PetscCall(PetscDrawPopCurrentPoint(draw)); 2135e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 2136536b137fSBarry Smith } else if (issaws) { 2137d45a07a7SBarry Smith PetscMPIInt rank; 21382657e9d9SBarry Smith const char *name; 21392657e9d9SBarry Smith 21409566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)ts,&name)); 21419566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); 2142dd400576SPatrick Sanan if (!((PetscObject)ts)->amsmem && rank == 0) { 2143d45a07a7SBarry Smith char dir[1024]; 2144d45a07a7SBarry Smith 21459566063dSJacob Faibussowitsch PetscCall(PetscObjectViewSAWs((PetscObject)ts,viewer)); 21469566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name)); 21472657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 21489566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name)); 21492657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 2150d763cef2SBarry Smith } 21510acecf5bSBarry Smith if (ts->ops->view) { 21529566063dSJacob Faibussowitsch PetscCall((*ts->ops->view)(ts,viewer)); 21530acecf5bSBarry Smith } 2154f05ece33SBarry Smith #endif 2155f05ece33SBarry Smith } 215636a9e3b9SBarry Smith if (ts->snes && ts->usessnes) { 21579566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 21589566063dSJacob Faibussowitsch PetscCall(SNESView(ts->snes,viewer)); 21599566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 216036a9e3b9SBarry Smith } 21619566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm,&sdm)); 21629566063dSJacob Faibussowitsch PetscCall(DMTSView(sdm,viewer)); 2163f05ece33SBarry Smith 21649566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 21659566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials)); 21669566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 2167d763cef2SBarry Smith PetscFunctionReturn(0); 2168d763cef2SBarry Smith } 2169d763cef2SBarry Smith 2170b07ff414SBarry Smith /*@ 2171d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 2172d763cef2SBarry Smith the timesteppers. 2173d763cef2SBarry Smith 21743f9fe445SBarry Smith Logically Collective on TS 2175d763cef2SBarry Smith 2176d763cef2SBarry Smith Input Parameters: 2177d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2178d763cef2SBarry Smith - usrP - optional user context 2179d763cef2SBarry Smith 218095452b02SPatrick Sanan Fortran Notes: 218195452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 2182daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2183daf670e6SBarry Smith 2184d763cef2SBarry Smith Level: intermediate 2185d763cef2SBarry Smith 2186d763cef2SBarry Smith .seealso: TSGetApplicationContext() 2187d763cef2SBarry Smith @*/ 21887087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 2189d763cef2SBarry Smith { 2190d763cef2SBarry Smith PetscFunctionBegin; 21910700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2192d763cef2SBarry Smith ts->user = usrP; 2193d763cef2SBarry Smith PetscFunctionReturn(0); 2194d763cef2SBarry Smith } 2195d763cef2SBarry Smith 2196b07ff414SBarry Smith /*@ 2197d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 2198d763cef2SBarry Smith timestepper. 2199d763cef2SBarry Smith 2200d763cef2SBarry Smith Not Collective 2201d763cef2SBarry Smith 2202d763cef2SBarry Smith Input Parameter: 2203d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2204d763cef2SBarry Smith 2205d763cef2SBarry Smith Output Parameter: 2206d763cef2SBarry Smith . usrP - user context 2207d763cef2SBarry Smith 220895452b02SPatrick Sanan Fortran Notes: 220995452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 2210daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2211daf670e6SBarry Smith 2212d763cef2SBarry Smith Level: intermediate 2213d763cef2SBarry Smith 2214d763cef2SBarry Smith .seealso: TSSetApplicationContext() 2215d763cef2SBarry Smith @*/ 2216e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 2217d763cef2SBarry Smith { 2218d763cef2SBarry Smith PetscFunctionBegin; 22190700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2220e71120c6SJed Brown *(void**)usrP = ts->user; 2221d763cef2SBarry Smith PetscFunctionReturn(0); 2222d763cef2SBarry Smith } 2223d763cef2SBarry Smith 2224d763cef2SBarry Smith /*@ 222580275a0aSLisandro Dalcin TSGetStepNumber - Gets the number of steps completed. 2226d763cef2SBarry Smith 2227d763cef2SBarry Smith Not Collective 2228d763cef2SBarry Smith 2229d763cef2SBarry Smith Input Parameter: 2230d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2231d763cef2SBarry Smith 2232d763cef2SBarry Smith Output Parameter: 223380275a0aSLisandro Dalcin . steps - number of steps completed so far 2234d763cef2SBarry Smith 2235d763cef2SBarry Smith Level: intermediate 2236d763cef2SBarry Smith 22379be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 2238d763cef2SBarry Smith @*/ 223980275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps) 2240d763cef2SBarry Smith { 2241d763cef2SBarry Smith PetscFunctionBegin; 22420700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 224380275a0aSLisandro Dalcin PetscValidIntPointer(steps,2); 224480275a0aSLisandro Dalcin *steps = ts->steps; 224580275a0aSLisandro Dalcin PetscFunctionReturn(0); 224680275a0aSLisandro Dalcin } 224780275a0aSLisandro Dalcin 224880275a0aSLisandro Dalcin /*@ 224980275a0aSLisandro Dalcin TSSetStepNumber - Sets the number of steps completed. 225080275a0aSLisandro Dalcin 225180275a0aSLisandro Dalcin Logically Collective on TS 225280275a0aSLisandro Dalcin 225380275a0aSLisandro Dalcin Input Parameters: 225480275a0aSLisandro Dalcin + ts - the TS context 225580275a0aSLisandro Dalcin - steps - number of steps completed so far 225680275a0aSLisandro Dalcin 225780275a0aSLisandro Dalcin Notes: 225880275a0aSLisandro Dalcin For most uses of the TS solvers the user need not explicitly call 225980275a0aSLisandro Dalcin TSSetStepNumber(), as the step counter is appropriately updated in 226080275a0aSLisandro Dalcin TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to 226180275a0aSLisandro Dalcin reinitialize timestepping by setting the step counter to zero (and time 226280275a0aSLisandro Dalcin to the initial time) to solve a similar problem with different initial 226380275a0aSLisandro Dalcin conditions or parameters. Other possible use case is to continue 226480275a0aSLisandro Dalcin timestepping from a previously interrupted run in such a way that TS 226580275a0aSLisandro Dalcin monitors will be called with a initial nonzero step counter. 226680275a0aSLisandro Dalcin 226780275a0aSLisandro Dalcin Level: advanced 226880275a0aSLisandro Dalcin 226980275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution() 227080275a0aSLisandro Dalcin @*/ 227180275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps) 227280275a0aSLisandro Dalcin { 227380275a0aSLisandro Dalcin PetscFunctionBegin; 227480275a0aSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 227580275a0aSLisandro Dalcin PetscValidLogicalCollectiveInt(ts,steps,2); 22763c633725SBarry Smith PetscCheck(steps >= 0,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative"); 227780275a0aSLisandro Dalcin ts->steps = steps; 2278d763cef2SBarry Smith PetscFunctionReturn(0); 2279d763cef2SBarry Smith } 2280d763cef2SBarry Smith 2281d763cef2SBarry Smith /*@ 2282d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 2283d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 2284d763cef2SBarry Smith 22853f9fe445SBarry Smith Logically Collective on TS 2286d763cef2SBarry Smith 2287d763cef2SBarry Smith Input Parameters: 2288d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2289d763cef2SBarry Smith - time_step - the size of the timestep 2290d763cef2SBarry Smith 2291d763cef2SBarry Smith Level: intermediate 2292d763cef2SBarry Smith 2293aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime() 2294d763cef2SBarry Smith 2295d763cef2SBarry Smith @*/ 22967087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 2297d763cef2SBarry Smith { 2298d763cef2SBarry Smith PetscFunctionBegin; 22990700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2300c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 2301d763cef2SBarry Smith ts->time_step = time_step; 2302d763cef2SBarry Smith PetscFunctionReturn(0); 2303d763cef2SBarry Smith } 2304d763cef2SBarry Smith 2305a43b19c4SJed Brown /*@ 230649354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 230749354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 230849354f04SShri Abhyankar or just return at the final time TS computed. 2309a43b19c4SJed Brown 2310a43b19c4SJed Brown Logically Collective on TS 2311a43b19c4SJed Brown 2312d8d19677SJose E. Roman Input Parameters: 2313a43b19c4SJed Brown + ts - the time-step context 231449354f04SShri Abhyankar - eftopt - exact final time option 2315a43b19c4SJed Brown 2316feed9e9dSBarry Smith $ TS_EXACTFINALTIME_STEPOVER - Don't do anything if final time is exceeded 2317feed9e9dSBarry Smith $ TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time 2318feed9e9dSBarry Smith $ TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time 2319feed9e9dSBarry Smith 2320feed9e9dSBarry Smith Options Database: 2321feed9e9dSBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime 2322feed9e9dSBarry Smith 2323ee346746SBarry Smith Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time 2324ee346746SBarry Smith then the final time you selected. 2325ee346746SBarry Smith 2326a43b19c4SJed Brown Level: beginner 2327a43b19c4SJed Brown 2328f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime() 2329a43b19c4SJed Brown @*/ 233049354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 2331a43b19c4SJed Brown { 2332a43b19c4SJed Brown PetscFunctionBegin; 2333a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 233449354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 233549354f04SShri Abhyankar ts->exact_final_time = eftopt; 2336a43b19c4SJed Brown PetscFunctionReturn(0); 2337a43b19c4SJed Brown } 2338a43b19c4SJed Brown 2339d763cef2SBarry Smith /*@ 2340f6953c82SLisandro Dalcin TSGetExactFinalTime - Gets the exact final time option. 2341f6953c82SLisandro Dalcin 2342f6953c82SLisandro Dalcin Not Collective 2343f6953c82SLisandro Dalcin 2344f6953c82SLisandro Dalcin Input Parameter: 2345f6953c82SLisandro Dalcin . ts - the TS context 2346f6953c82SLisandro Dalcin 2347f6953c82SLisandro Dalcin Output Parameter: 2348f6953c82SLisandro Dalcin . eftopt - exact final time option 2349f6953c82SLisandro Dalcin 2350f6953c82SLisandro Dalcin Level: beginner 2351f6953c82SLisandro Dalcin 2352f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime() 2353f6953c82SLisandro Dalcin @*/ 2354f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt) 2355f6953c82SLisandro Dalcin { 2356f6953c82SLisandro Dalcin PetscFunctionBegin; 2357f6953c82SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2358f6953c82SLisandro Dalcin PetscValidPointer(eftopt,2); 2359f6953c82SLisandro Dalcin *eftopt = ts->exact_final_time; 2360f6953c82SLisandro Dalcin PetscFunctionReturn(0); 2361f6953c82SLisandro Dalcin } 2362f6953c82SLisandro Dalcin 2363f6953c82SLisandro Dalcin /*@ 2364d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 2365d763cef2SBarry Smith 2366d763cef2SBarry Smith Not Collective 2367d763cef2SBarry Smith 2368d763cef2SBarry Smith Input Parameter: 2369d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2370d763cef2SBarry Smith 2371d763cef2SBarry Smith Output Parameter: 2372d763cef2SBarry Smith . dt - the current timestep size 2373d763cef2SBarry Smith 2374d763cef2SBarry Smith Level: intermediate 2375d763cef2SBarry Smith 2376aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime() 2377d763cef2SBarry Smith 2378d763cef2SBarry Smith @*/ 23797087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 2380d763cef2SBarry Smith { 2381d763cef2SBarry Smith PetscFunctionBegin; 23820700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2383f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 2384d763cef2SBarry Smith *dt = ts->time_step; 2385d763cef2SBarry Smith PetscFunctionReturn(0); 2386d763cef2SBarry Smith } 2387d763cef2SBarry Smith 2388d8e5e3e6SSatish Balay /*@ 2389d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 2390d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 2391d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 2392d763cef2SBarry Smith the solution at the next timestep has been calculated. 2393d763cef2SBarry Smith 2394d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 2395d763cef2SBarry Smith 2396d763cef2SBarry Smith Input Parameter: 2397d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2398d763cef2SBarry Smith 2399d763cef2SBarry Smith Output Parameter: 2400d763cef2SBarry Smith . v - the vector containing the solution 2401d763cef2SBarry Smith 240263e21af5SBarry Smith Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested 240363e21af5SBarry Smith final time. It returns the solution at the next timestep. 240463e21af5SBarry Smith 2405d763cef2SBarry Smith Level: intermediate 2406d763cef2SBarry Smith 24070ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction() 2408d763cef2SBarry Smith 2409d763cef2SBarry Smith @*/ 24107087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 2411d763cef2SBarry Smith { 2412d763cef2SBarry Smith PetscFunctionBegin; 24130700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 24144482741eSBarry Smith PetscValidPointer(v,2); 24158737fe31SLisandro Dalcin *v = ts->vec_sol; 2416d763cef2SBarry Smith PetscFunctionReturn(0); 2417d763cef2SBarry Smith } 2418d763cef2SBarry Smith 241903fe5f5eSDebojyoti Ghosh /*@ 2420b2bf4f3aSDebojyoti Ghosh TSGetSolutionComponents - Returns any solution components at the present 242103fe5f5eSDebojyoti Ghosh timestep, if available for the time integration method being used. 2422b2bf4f3aSDebojyoti Ghosh Solution components are quantities that share the same size and 242303fe5f5eSDebojyoti Ghosh structure as the solution vector. 242403fe5f5eSDebojyoti Ghosh 242503fe5f5eSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 242603fe5f5eSDebojyoti Ghosh 242703fe5f5eSDebojyoti Ghosh Parameters : 2428a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2429b2bf4f3aSDebojyoti Ghosh . n - If v is PETSC_NULL, then the number of solution components is 2430b2bf4f3aSDebojyoti Ghosh returned through n, else the n-th solution component is 243103fe5f5eSDebojyoti Ghosh returned in v. 2432a2b725a8SWilliam Gropp - v - the vector containing the n-th solution component 243303fe5f5eSDebojyoti Ghosh (may be PETSC_NULL to use this function to find out 2434b2bf4f3aSDebojyoti Ghosh the number of solutions components). 243503fe5f5eSDebojyoti Ghosh 24364cdd57e5SDebojyoti Ghosh Level: advanced 243703fe5f5eSDebojyoti Ghosh 243803fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution() 243903fe5f5eSDebojyoti Ghosh 244003fe5f5eSDebojyoti Ghosh @*/ 2441b2bf4f3aSDebojyoti Ghosh PetscErrorCode TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v) 244203fe5f5eSDebojyoti Ghosh { 244303fe5f5eSDebojyoti Ghosh PetscFunctionBegin; 244403fe5f5eSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2445b2bf4f3aSDebojyoti Ghosh if (!ts->ops->getsolutioncomponents) *n = 0; 244603fe5f5eSDebojyoti Ghosh else { 24479566063dSJacob Faibussowitsch PetscCall((*ts->ops->getsolutioncomponents)(ts,n,v)); 244803fe5f5eSDebojyoti Ghosh } 244903fe5f5eSDebojyoti Ghosh PetscFunctionReturn(0); 245003fe5f5eSDebojyoti Ghosh } 245103fe5f5eSDebojyoti Ghosh 24524cdd57e5SDebojyoti Ghosh /*@ 24534cdd57e5SDebojyoti Ghosh TSGetAuxSolution - Returns an auxiliary solution at the present 24544cdd57e5SDebojyoti Ghosh timestep, if available for the time integration method being used. 24554cdd57e5SDebojyoti Ghosh 24564cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 24574cdd57e5SDebojyoti Ghosh 24584cdd57e5SDebojyoti Ghosh Parameters : 2459a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2460a2b725a8SWilliam Gropp - v - the vector containing the auxiliary solution 24614cdd57e5SDebojyoti Ghosh 24624cdd57e5SDebojyoti Ghosh Level: intermediate 24634cdd57e5SDebojyoti Ghosh 24644cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution() 24654cdd57e5SDebojyoti Ghosh 24664cdd57e5SDebojyoti Ghosh @*/ 24674cdd57e5SDebojyoti Ghosh PetscErrorCode TSGetAuxSolution(TS ts,Vec *v) 24684cdd57e5SDebojyoti Ghosh { 24694cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 24704cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2471f6356ec7SDebojyoti Ghosh if (ts->ops->getauxsolution) { 24729566063dSJacob Faibussowitsch PetscCall((*ts->ops->getauxsolution)(ts,v)); 2473f6356ec7SDebojyoti Ghosh } else { 24749566063dSJacob Faibussowitsch PetscCall(VecZeroEntries(*v)); 2475f6356ec7SDebojyoti Ghosh } 24764cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 24774cdd57e5SDebojyoti Ghosh } 24784cdd57e5SDebojyoti Ghosh 24794cdd57e5SDebojyoti Ghosh /*@ 24804cdd57e5SDebojyoti Ghosh TSGetTimeError - Returns the estimated error vector, if the chosen 24814cdd57e5SDebojyoti Ghosh TSType has an error estimation functionality. 24824cdd57e5SDebojyoti Ghosh 24834cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 24844cdd57e5SDebojyoti Ghosh 24859657682dSDebojyoti Ghosh Note: MUST call after TSSetUp() 24869657682dSDebojyoti Ghosh 24874cdd57e5SDebojyoti Ghosh Parameters : 2488a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2489657c1e31SEmil Constantinescu . n - current estimate (n=0) or previous one (n=-1) 2490a2b725a8SWilliam Gropp - v - the vector containing the error (same size as the solution). 24914cdd57e5SDebojyoti Ghosh 24924cdd57e5SDebojyoti Ghosh Level: intermediate 24934cdd57e5SDebojyoti Ghosh 249457df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError() 24954cdd57e5SDebojyoti Ghosh 24964cdd57e5SDebojyoti Ghosh @*/ 24970a01e1b2SEmil Constantinescu PetscErrorCode TSGetTimeError(TS ts,PetscInt n,Vec *v) 24984cdd57e5SDebojyoti Ghosh { 24994cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 25004cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2501f6356ec7SDebojyoti Ghosh if (ts->ops->gettimeerror) { 25029566063dSJacob Faibussowitsch PetscCall((*ts->ops->gettimeerror)(ts,n,v)); 2503f6356ec7SDebojyoti Ghosh } else { 25049566063dSJacob Faibussowitsch PetscCall(VecZeroEntries(*v)); 2505f6356ec7SDebojyoti Ghosh } 25064cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 25074cdd57e5SDebojyoti Ghosh } 25084cdd57e5SDebojyoti Ghosh 250957df6a1bSDebojyoti Ghosh /*@ 251057df6a1bSDebojyoti Ghosh TSSetTimeError - Sets the estimated error vector, if the chosen 251157df6a1bSDebojyoti Ghosh TSType has an error estimation functionality. This can be used 251257df6a1bSDebojyoti Ghosh to restart such a time integrator with a given error vector. 251357df6a1bSDebojyoti Ghosh 251457df6a1bSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 251557df6a1bSDebojyoti Ghosh 251657df6a1bSDebojyoti Ghosh Parameters : 2517a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2518a2b725a8SWilliam Gropp - v - the vector containing the error (same size as the solution). 251957df6a1bSDebojyoti Ghosh 252057df6a1bSDebojyoti Ghosh Level: intermediate 252157df6a1bSDebojyoti Ghosh 252257df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError) 252357df6a1bSDebojyoti Ghosh 252457df6a1bSDebojyoti Ghosh @*/ 252557df6a1bSDebojyoti Ghosh PetscErrorCode TSSetTimeError(TS ts,Vec v) 252657df6a1bSDebojyoti Ghosh { 252757df6a1bSDebojyoti Ghosh PetscFunctionBegin; 252857df6a1bSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 25293c633725SBarry Smith PetscCheck(ts->setupcalled,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first"); 253057df6a1bSDebojyoti Ghosh if (ts->ops->settimeerror) { 25319566063dSJacob Faibussowitsch PetscCall((*ts->ops->settimeerror)(ts,v)); 253257df6a1bSDebojyoti Ghosh } 253357df6a1bSDebojyoti Ghosh PetscFunctionReturn(0); 253457df6a1bSDebojyoti Ghosh } 253557df6a1bSDebojyoti Ghosh 2536bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 2537d8e5e3e6SSatish Balay /*@ 2538bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 2539d763cef2SBarry Smith 2540bdad233fSMatthew Knepley Not collective 2541d763cef2SBarry Smith 2542bdad233fSMatthew Knepley Input Parameters: 2543bdad233fSMatthew Knepley + ts - The TS 2544bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2545d763cef2SBarry Smith .vb 25460910c330SBarry Smith U_t - A U = 0 (linear) 25470910c330SBarry Smith U_t - A(t) U = 0 (linear) 25480910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 2549d763cef2SBarry Smith .ve 2550d763cef2SBarry Smith 2551d763cef2SBarry Smith Level: beginner 2552d763cef2SBarry Smith 2553bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2554d763cef2SBarry Smith @*/ 25557087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 2556a7cc72afSBarry Smith { 2557d763cef2SBarry Smith PetscFunctionBegin; 25580700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2559bdad233fSMatthew Knepley ts->problem_type = type; 25609e2a6581SJed Brown if (type == TS_LINEAR) { 25619e2a6581SJed Brown SNES snes; 25629566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 25639566063dSJacob Faibussowitsch PetscCall(SNESSetType(snes,SNESKSPONLY)); 25649e2a6581SJed Brown } 2565d763cef2SBarry Smith PetscFunctionReturn(0); 2566d763cef2SBarry Smith } 2567d763cef2SBarry Smith 2568bdad233fSMatthew Knepley /*@C 2569bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 2570bdad233fSMatthew Knepley 2571bdad233fSMatthew Knepley Not collective 2572bdad233fSMatthew Knepley 2573bdad233fSMatthew Knepley Input Parameter: 2574bdad233fSMatthew Knepley . ts - The TS 2575bdad233fSMatthew Knepley 2576bdad233fSMatthew Knepley Output Parameter: 2577bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2578bdad233fSMatthew Knepley .vb 2579089b2837SJed Brown M U_t = A U 2580089b2837SJed Brown M(t) U_t = A(t) U 2581b5abc632SBarry Smith F(t,U,U_t) 2582bdad233fSMatthew Knepley .ve 2583bdad233fSMatthew Knepley 2584bdad233fSMatthew Knepley Level: beginner 2585bdad233fSMatthew Knepley 2586bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2587bdad233fSMatthew Knepley @*/ 25887087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 2589a7cc72afSBarry Smith { 2590bdad233fSMatthew Knepley PetscFunctionBegin; 25910700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 25924482741eSBarry Smith PetscValidIntPointer(type,2); 2593bdad233fSMatthew Knepley *type = ts->problem_type; 2594bdad233fSMatthew Knepley PetscFunctionReturn(0); 2595bdad233fSMatthew Knepley } 2596d763cef2SBarry Smith 2597303a5415SBarry Smith /* 2598303a5415SBarry 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() 2599303a5415SBarry Smith */ 2600303a5415SBarry Smith static PetscErrorCode TSSetExactFinalTimeDefault(TS ts) 2601303a5415SBarry Smith { 2602303a5415SBarry Smith PetscBool isnone; 2603303a5415SBarry Smith 2604303a5415SBarry Smith PetscFunctionBegin; 26059566063dSJacob Faibussowitsch PetscCall(TSGetAdapt(ts,&ts->adapt)); 26069566063dSJacob Faibussowitsch PetscCall(TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type)); 2607303a5415SBarry Smith 26089566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone)); 2609303a5415SBarry Smith if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) { 2610303a5415SBarry Smith ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP; 2611303a5415SBarry Smith } else if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) { 2612303a5415SBarry Smith ts->exact_final_time = TS_EXACTFINALTIME_INTERPOLATE; 2613303a5415SBarry Smith } 2614303a5415SBarry Smith PetscFunctionReturn(0); 2615303a5415SBarry Smith } 2616303a5415SBarry Smith 2617d763cef2SBarry Smith /*@ 2618303a5415SBarry Smith TSSetUp - Sets up the internal data structures for the later use of a timestepper. 2619d763cef2SBarry Smith 2620d763cef2SBarry Smith Collective on TS 2621d763cef2SBarry Smith 2622d763cef2SBarry Smith Input Parameter: 2623d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2624d763cef2SBarry Smith 2625d763cef2SBarry Smith Notes: 2626d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 2627d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 2628141bd67dSStefano Zampini the call to TSStep() or TSSolve(). However, if one wishes to control this 2629d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 2630141bd67dSStefano Zampini and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve(). 2631d763cef2SBarry Smith 2632d763cef2SBarry Smith Level: advanced 2633d763cef2SBarry Smith 2634141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve() 2635d763cef2SBarry Smith @*/ 26367087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 2637d763cef2SBarry Smith { 26386c6b9e74SPeter Brune DM dm; 26396c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 2640d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 2641cd11d68dSLisandro Dalcin TSIFunction ifun; 26426c6b9e74SPeter Brune TSIJacobian ijac; 2643efe9872eSLisandro Dalcin TSI2Jacobian i2jac; 26446c6b9e74SPeter Brune TSRHSJacobian rhsjac; 2645d763cef2SBarry Smith 2646d763cef2SBarry Smith PetscFunctionBegin; 26470700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2648277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 2649277b19d0SLisandro Dalcin 26507adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 26519566063dSJacob Faibussowitsch PetscCall(TSGetIFunction(ts,NULL,&ifun,NULL)); 26529566063dSJacob Faibussowitsch PetscCall(TSSetType(ts,ifun ? TSBEULER : TSEULER)); 2653d763cef2SBarry Smith } 2654277b19d0SLisandro Dalcin 26551a638600SStefano Zampini if (!ts->vec_sol) { 26561a638600SStefano Zampini if (ts->dm) { 26579566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(ts->dm,&ts->vec_sol)); 26581a638600SStefano Zampini } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 26591a638600SStefano Zampini } 2660277b19d0SLisandro Dalcin 26614a658b32SHong Zhang if (ts->tspan) { 26624a658b32SHong Zhang if (!ts->tspan->vecs_sol) { 26634a658b32SHong Zhang PetscCall(VecDuplicateVecs(ts->vec_sol,ts->tspan->num_span_times,&ts->tspan->vecs_sol)); 26644a658b32SHong Zhang } 26654a658b32SHong Zhang } 2666298bade4SHong Zhang if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */ 26679566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)ts->Jacprhs)); 2668298bade4SHong Zhang ts->Jacp = ts->Jacprhs; 2669298bade4SHong Zhang } 2670298bade4SHong Zhang 2671cd4cee2dSHong Zhang if (ts->quadraturets) { 26729566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts->quadraturets)); 26739566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_costintegrand)); 26749566063dSJacob Faibussowitsch PetscCall(VecDuplicate(ts->quadraturets->vec_sol,&ts->vec_costintegrand)); 2675cd4cee2dSHong Zhang } 2676cd4cee2dSHong Zhang 26779566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL)); 2678f23ba4b3SHong Zhang if (rhsjac == TSComputeRHSJacobianConstant) { 2679e1244c69SJed Brown Mat Amat,Pmat; 2680e1244c69SJed Brown SNES snes; 26819566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 26829566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL)); 2683e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 2684e1244c69SJed Brown * have displaced the RHS matrix */ 2685971015bcSStefano Zampini if (Amat && Amat == ts->Arhs) { 2686abc0d4abSBarry 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 */ 26879566063dSJacob Faibussowitsch PetscCall(MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat)); 26889566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes,Amat,NULL,NULL,NULL)); 26899566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Amat)); 2690e1244c69SJed Brown } 2691971015bcSStefano Zampini if (Pmat && Pmat == ts->Brhs) { 26929566063dSJacob Faibussowitsch PetscCall(MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat)); 26939566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes,NULL,Pmat,NULL,NULL)); 26949566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Pmat)); 2695e1244c69SJed Brown } 2696e1244c69SJed Brown } 26972ffb9264SLisandro Dalcin 26989566063dSJacob Faibussowitsch PetscCall(TSGetAdapt(ts,&ts->adapt)); 26999566063dSJacob Faibussowitsch PetscCall(TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type)); 27002ffb9264SLisandro Dalcin 2701277b19d0SLisandro Dalcin if (ts->ops->setup) { 27029566063dSJacob Faibussowitsch PetscCall((*ts->ops->setup)(ts)); 2703277b19d0SLisandro Dalcin } 2704277b19d0SLisandro Dalcin 27059566063dSJacob Faibussowitsch PetscCall(TSSetExactFinalTimeDefault(ts)); 27062ffb9264SLisandro Dalcin 2707a6772fa2SLisandro Dalcin /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 27086c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 27096c6b9e74SPeter Brune */ 27109566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 27119566063dSJacob Faibussowitsch PetscCall(DMSNESGetFunction(dm,&func,NULL)); 27126c6b9e74SPeter Brune if (!func) { 27139566063dSJacob Faibussowitsch PetscCall(DMSNESSetFunction(dm,SNESTSFormFunction,ts)); 27146c6b9e74SPeter Brune } 2715a6772fa2SLisandro 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. 27166c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 27176c6b9e74SPeter Brune */ 27189566063dSJacob Faibussowitsch PetscCall(DMSNESGetJacobian(dm,&jac,NULL)); 27199566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm,&ijac,NULL)); 27209566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm,&i2jac,NULL)); 27219566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm,&rhsjac,NULL)); 2722efe9872eSLisandro Dalcin if (!jac && (ijac || i2jac || rhsjac)) { 27239566063dSJacob Faibussowitsch PetscCall(DMSNESSetJacobian(dm,SNESTSFormJacobian,ts)); 27246c6b9e74SPeter Brune } 2725c0517034SDebojyoti Ghosh 2726c0517034SDebojyoti Ghosh /* if time integration scheme has a starting method, call it */ 2727c0517034SDebojyoti Ghosh if (ts->ops->startingmethod) { 27289566063dSJacob Faibussowitsch PetscCall((*ts->ops->startingmethod)(ts)); 2729c0517034SDebojyoti Ghosh } 2730c0517034SDebojyoti Ghosh 2731277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 2732277b19d0SLisandro Dalcin PetscFunctionReturn(0); 2733277b19d0SLisandro Dalcin } 2734277b19d0SLisandro Dalcin 2735f6a906c0SBarry Smith /*@ 2736277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 2737277b19d0SLisandro Dalcin 2738277b19d0SLisandro Dalcin Collective on TS 2739277b19d0SLisandro Dalcin 2740277b19d0SLisandro Dalcin Input Parameter: 2741277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 2742277b19d0SLisandro Dalcin 2743277b19d0SLisandro Dalcin Level: beginner 2744277b19d0SLisandro Dalcin 2745277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 2746277b19d0SLisandro Dalcin @*/ 2747277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 2748277b19d0SLisandro Dalcin { 27491d06f6b3SHong Zhang TS_RHSSplitLink ilink = ts->tsrhssplit,next; 2750277b19d0SLisandro Dalcin 2751277b19d0SLisandro Dalcin PetscFunctionBegin; 2752277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2753b18ea86cSHong Zhang 2754277b19d0SLisandro Dalcin if (ts->ops->reset) { 27559566063dSJacob Faibussowitsch PetscCall((*ts->ops->reset)(ts)); 2756277b19d0SLisandro Dalcin } 27579566063dSJacob Faibussowitsch if (ts->snes) PetscCall(SNESReset(ts->snes)); 27589566063dSJacob Faibussowitsch if (ts->adapt) PetscCall(TSAdaptReset(ts->adapt)); 2759bbd56ea5SKarl Rupp 27609566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Arhs)); 27619566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Brhs)); 27629566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->Frhs)); 27639566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_sol)); 27649566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_dot)); 27659566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vatol)); 27669566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vrtol)); 27679566063dSJacob Faibussowitsch PetscCall(VecDestroyVecs(ts->nwork,&ts->work)); 2768bbd56ea5SKarl Rupp 27699566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Jacprhs)); 27709566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Jacp)); 2771ecf68647SHong Zhang if (ts->forward_solve) { 27729566063dSJacob Faibussowitsch PetscCall(TSForwardReset(ts)); 2773ecf68647SHong Zhang } 2774cd4cee2dSHong Zhang if (ts->quadraturets) { 27759566063dSJacob Faibussowitsch PetscCall(TSReset(ts->quadraturets)); 27769566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_costintegrand)); 2777cd4cee2dSHong Zhang } 27781d06f6b3SHong Zhang while (ilink) { 27791d06f6b3SHong Zhang next = ilink->next; 27809566063dSJacob Faibussowitsch PetscCall(TSDestroy(&ilink->ts)); 27819566063dSJacob Faibussowitsch PetscCall(PetscFree(ilink->splitname)); 27829566063dSJacob Faibussowitsch PetscCall(ISDestroy(&ilink->is)); 27839566063dSJacob Faibussowitsch PetscCall(PetscFree(ilink)); 27841d06f6b3SHong Zhang ilink = next; 278587f4e208SHong Zhang } 27866bf673ebSJoe Pusztay ts->tsrhssplit = NULL; 2787545aaa6fSHong Zhang ts->num_rhs_splits = 0; 27884a658b32SHong Zhang if (ts->tspan) { 27894a658b32SHong Zhang PetscCall(PetscFree(ts->tspan->span_times)); 27904a658b32SHong Zhang PetscCall(VecDestroyVecs(ts->tspan->num_span_times,&ts->tspan->vecs_sol)); 27914a658b32SHong Zhang PetscCall(PetscFree(ts->tspan)); 27924a658b32SHong Zhang } 2793277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 2794d763cef2SBarry Smith PetscFunctionReturn(0); 2795d763cef2SBarry Smith } 2796d763cef2SBarry Smith 27971fb7b255SJunchao Zhang /*@C 2798d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2799d763cef2SBarry Smith with TSCreate(). 2800d763cef2SBarry Smith 2801d763cef2SBarry Smith Collective on TS 2802d763cef2SBarry Smith 2803d763cef2SBarry Smith Input Parameter: 2804d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2805d763cef2SBarry Smith 2806d763cef2SBarry Smith Level: beginner 2807d763cef2SBarry Smith 2808d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 2809d763cef2SBarry Smith @*/ 28106bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2811d763cef2SBarry Smith { 2812d763cef2SBarry Smith PetscFunctionBegin; 28136bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 2814ecf68647SHong Zhang PetscValidHeaderSpecific(*ts,TS_CLASSID,1); 2815c793f718SLisandro Dalcin if (--((PetscObject)(*ts))->refct > 0) {*ts = NULL; PetscFunctionReturn(0);} 2816d763cef2SBarry Smith 28179566063dSJacob Faibussowitsch PetscCall(TSReset(*ts)); 28189566063dSJacob Faibussowitsch PetscCall(TSAdjointReset(*ts)); 2819ecf68647SHong Zhang if ((*ts)->forward_solve) { 28209566063dSJacob Faibussowitsch PetscCall(TSForwardReset(*ts)); 2821ecf68647SHong Zhang } 2822e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 28239566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsViewOff((PetscObject)*ts)); 28249566063dSJacob Faibussowitsch if ((*ts)->ops->destroy) PetscCall((*(*ts)->ops->destroy)((*ts))); 28256d4c513bSLisandro Dalcin 28269566063dSJacob Faibussowitsch PetscCall(TSTrajectoryDestroy(&(*ts)->trajectory)); 2827bc952696SBarry Smith 28289566063dSJacob Faibussowitsch PetscCall(TSAdaptDestroy(&(*ts)->adapt)); 28299566063dSJacob Faibussowitsch PetscCall(TSEventDestroy(&(*ts)->event)); 28306427ac75SLisandro Dalcin 28319566063dSJacob Faibussowitsch PetscCall(SNESDestroy(&(*ts)->snes)); 28329566063dSJacob Faibussowitsch PetscCall(DMDestroy(&(*ts)->dm)); 28339566063dSJacob Faibussowitsch PetscCall(TSMonitorCancel((*ts))); 28349566063dSJacob Faibussowitsch PetscCall(TSAdjointMonitorCancel((*ts))); 28356d4c513bSLisandro Dalcin 28369566063dSJacob Faibussowitsch PetscCall(TSDestroy(&(*ts)->quadraturets)); 28379566063dSJacob Faibussowitsch PetscCall(PetscHeaderDestroy(ts)); 2838d763cef2SBarry Smith PetscFunctionReturn(0); 2839d763cef2SBarry Smith } 2840d763cef2SBarry Smith 2841d8e5e3e6SSatish Balay /*@ 2842d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2843d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2844d763cef2SBarry Smith 2845d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2846d763cef2SBarry Smith 2847d763cef2SBarry Smith Input Parameter: 2848d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2849d763cef2SBarry Smith 2850d763cef2SBarry Smith Output Parameter: 2851d763cef2SBarry Smith . snes - the nonlinear solver context 2852d763cef2SBarry Smith 2853d763cef2SBarry Smith Notes: 2854d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2855d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 285694b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2857d763cef2SBarry Smith 2858d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 28590298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2860d763cef2SBarry Smith 2861d763cef2SBarry Smith Level: beginner 2862d763cef2SBarry Smith 2863d763cef2SBarry Smith @*/ 28647087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2865d763cef2SBarry Smith { 2866d763cef2SBarry Smith PetscFunctionBegin; 28670700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 28684482741eSBarry Smith PetscValidPointer(snes,2); 2869d372ba47SLisandro Dalcin if (!ts->snes) { 28709566063dSJacob Faibussowitsch PetscCall(SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes)); 28719566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options)); 28729566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts)); 28739566063dSJacob Faibussowitsch PetscCall(PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes)); 28749566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1)); 28759566063dSJacob Faibussowitsch if (ts->dm) PetscCall(SNESSetDM(ts->snes,ts->dm)); 28769e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 28779566063dSJacob Faibussowitsch PetscCall(SNESSetType(ts->snes,SNESKSPONLY)); 28789e2a6581SJed Brown } 2879d372ba47SLisandro Dalcin } 2880d763cef2SBarry Smith *snes = ts->snes; 2881d763cef2SBarry Smith PetscFunctionReturn(0); 2882d763cef2SBarry Smith } 2883d763cef2SBarry Smith 2884deb2cd25SJed Brown /*@ 2885deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2886deb2cd25SJed Brown 2887deb2cd25SJed Brown Collective 2888deb2cd25SJed Brown 2889d8d19677SJose E. Roman Input Parameters: 2890deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2891deb2cd25SJed Brown - snes - the nonlinear solver context 2892deb2cd25SJed Brown 2893deb2cd25SJed Brown Notes: 2894deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2895deb2cd25SJed Brown 2896deb2cd25SJed Brown Level: developer 2897deb2cd25SJed Brown 2898deb2cd25SJed Brown @*/ 2899deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2900deb2cd25SJed Brown { 2901d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2902deb2cd25SJed Brown 2903deb2cd25SJed Brown PetscFunctionBegin; 2904deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2905deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 29069566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)snes)); 29079566063dSJacob Faibussowitsch PetscCall(SNESDestroy(&ts->snes)); 2908bbd56ea5SKarl Rupp 2909deb2cd25SJed Brown ts->snes = snes; 2910bbd56ea5SKarl Rupp 29119566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts)); 29129566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL)); 2913740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 29149566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts)); 2915740132f1SEmil Constantinescu } 2916deb2cd25SJed Brown PetscFunctionReturn(0); 2917deb2cd25SJed Brown } 2918deb2cd25SJed Brown 2919d8e5e3e6SSatish Balay /*@ 292094b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2921d763cef2SBarry Smith a TS (timestepper) context. 2922d763cef2SBarry Smith 292394b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2924d763cef2SBarry Smith 2925d763cef2SBarry Smith Input Parameter: 2926d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2927d763cef2SBarry Smith 2928d763cef2SBarry Smith Output Parameter: 292994b7f48cSBarry Smith . ksp - the nonlinear solver context 2930d763cef2SBarry Smith 2931d763cef2SBarry Smith Notes: 293294b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2933d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2934d763cef2SBarry Smith KSP and PC contexts as well. 2935d763cef2SBarry Smith 293694b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 29370298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2938d763cef2SBarry Smith 2939d763cef2SBarry Smith Level: beginner 2940d763cef2SBarry Smith 2941d763cef2SBarry Smith @*/ 29427087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2943d763cef2SBarry Smith { 2944089b2837SJed Brown SNES snes; 2945d372ba47SLisandro Dalcin 2946d763cef2SBarry Smith PetscFunctionBegin; 29470700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 29484482741eSBarry Smith PetscValidPointer(ksp,2); 29493c633725SBarry Smith PetscCheck(((PetscObject)ts)->type_name,PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 29503c633725SBarry Smith PetscCheck(ts->problem_type == TS_LINEAR,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 29519566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 29529566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes,ksp)); 2953d763cef2SBarry Smith PetscFunctionReturn(0); 2954d763cef2SBarry Smith } 2955d763cef2SBarry Smith 2956d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2957d763cef2SBarry Smith 2958adb62b0dSMatthew Knepley /*@ 2959618ce8baSLisandro Dalcin TSSetMaxSteps - Sets the maximum number of steps to use. 2960618ce8baSLisandro Dalcin 2961618ce8baSLisandro Dalcin Logically Collective on TS 2962618ce8baSLisandro Dalcin 2963618ce8baSLisandro Dalcin Input Parameters: 2964618ce8baSLisandro Dalcin + ts - the TS context obtained from TSCreate() 2965618ce8baSLisandro Dalcin - maxsteps - maximum number of steps to use 2966618ce8baSLisandro Dalcin 2967618ce8baSLisandro Dalcin Options Database Keys: 2968618ce8baSLisandro Dalcin . -ts_max_steps <maxsteps> - Sets maxsteps 2969618ce8baSLisandro Dalcin 2970618ce8baSLisandro Dalcin Notes: 2971618ce8baSLisandro Dalcin The default maximum number of steps is 5000 2972618ce8baSLisandro Dalcin 2973618ce8baSLisandro Dalcin Level: intermediate 2974618ce8baSLisandro Dalcin 2975618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime() 2976618ce8baSLisandro Dalcin @*/ 2977618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps) 2978618ce8baSLisandro Dalcin { 2979618ce8baSLisandro Dalcin PetscFunctionBegin; 2980618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2981618ce8baSLisandro Dalcin PetscValidLogicalCollectiveInt(ts,maxsteps,2); 29823c633725SBarry Smith PetscCheck(maxsteps >= 0,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative"); 2983618ce8baSLisandro Dalcin ts->max_steps = maxsteps; 2984618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2985618ce8baSLisandro Dalcin } 2986618ce8baSLisandro Dalcin 2987618ce8baSLisandro Dalcin /*@ 2988618ce8baSLisandro Dalcin TSGetMaxSteps - Gets the maximum number of steps to use. 2989618ce8baSLisandro Dalcin 2990618ce8baSLisandro Dalcin Not Collective 2991618ce8baSLisandro Dalcin 2992618ce8baSLisandro Dalcin Input Parameters: 2993618ce8baSLisandro Dalcin . ts - the TS context obtained from TSCreate() 2994618ce8baSLisandro Dalcin 2995618ce8baSLisandro Dalcin Output Parameter: 2996618ce8baSLisandro Dalcin . maxsteps - maximum number of steps to use 2997618ce8baSLisandro Dalcin 2998618ce8baSLisandro Dalcin Level: advanced 2999618ce8baSLisandro Dalcin 3000618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime() 3001618ce8baSLisandro Dalcin @*/ 3002618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps) 3003618ce8baSLisandro Dalcin { 3004618ce8baSLisandro Dalcin PetscFunctionBegin; 3005618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3006618ce8baSLisandro Dalcin PetscValidIntPointer(maxsteps,2); 3007618ce8baSLisandro Dalcin *maxsteps = ts->max_steps; 3008618ce8baSLisandro Dalcin PetscFunctionReturn(0); 3009618ce8baSLisandro Dalcin } 3010618ce8baSLisandro Dalcin 3011618ce8baSLisandro Dalcin /*@ 3012618ce8baSLisandro Dalcin TSSetMaxTime - Sets the maximum (or final) time for timestepping. 3013618ce8baSLisandro Dalcin 3014618ce8baSLisandro Dalcin Logically Collective on TS 3015618ce8baSLisandro Dalcin 3016618ce8baSLisandro Dalcin Input Parameters: 3017618ce8baSLisandro Dalcin + ts - the TS context obtained from TSCreate() 3018618ce8baSLisandro Dalcin - maxtime - final time to step to 3019618ce8baSLisandro Dalcin 3020618ce8baSLisandro Dalcin Options Database Keys: 3021ef85077eSLisandro Dalcin . -ts_max_time <maxtime> - Sets maxtime 3022618ce8baSLisandro Dalcin 3023618ce8baSLisandro Dalcin Notes: 3024618ce8baSLisandro Dalcin The default maximum time is 5.0 3025618ce8baSLisandro Dalcin 3026618ce8baSLisandro Dalcin Level: intermediate 3027618ce8baSLisandro Dalcin 3028618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime() 3029618ce8baSLisandro Dalcin @*/ 3030618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime) 3031618ce8baSLisandro Dalcin { 3032618ce8baSLisandro Dalcin PetscFunctionBegin; 3033618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3034618ce8baSLisandro Dalcin PetscValidLogicalCollectiveReal(ts,maxtime,2); 3035618ce8baSLisandro Dalcin ts->max_time = maxtime; 3036618ce8baSLisandro Dalcin PetscFunctionReturn(0); 3037618ce8baSLisandro Dalcin } 3038618ce8baSLisandro Dalcin 3039618ce8baSLisandro Dalcin /*@ 3040618ce8baSLisandro Dalcin TSGetMaxTime - Gets the maximum (or final) time for timestepping. 3041618ce8baSLisandro Dalcin 3042618ce8baSLisandro Dalcin Not Collective 3043618ce8baSLisandro Dalcin 3044618ce8baSLisandro Dalcin Input Parameters: 3045618ce8baSLisandro Dalcin . ts - the TS context obtained from TSCreate() 3046618ce8baSLisandro Dalcin 3047618ce8baSLisandro Dalcin Output Parameter: 3048618ce8baSLisandro Dalcin . maxtime - final time to step to 3049618ce8baSLisandro Dalcin 3050618ce8baSLisandro Dalcin Level: advanced 3051618ce8baSLisandro Dalcin 3052618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps() 3053618ce8baSLisandro Dalcin @*/ 3054618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime) 3055618ce8baSLisandro Dalcin { 3056618ce8baSLisandro Dalcin PetscFunctionBegin; 3057618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3058618ce8baSLisandro Dalcin PetscValidRealPointer(maxtime,2); 3059618ce8baSLisandro Dalcin *maxtime = ts->max_time; 3060618ce8baSLisandro Dalcin PetscFunctionReturn(0); 3061618ce8baSLisandro Dalcin } 3062618ce8baSLisandro Dalcin 3063618ce8baSLisandro Dalcin /*@ 3064aaa6c58dSLisandro Dalcin TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep(). 3065edc382c3SSatish Balay 3066edc382c3SSatish Balay Level: deprecated 3067edc382c3SSatish Balay 3068aaa6c58dSLisandro Dalcin @*/ 3069aaa6c58dSLisandro Dalcin PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 3070aaa6c58dSLisandro Dalcin { 3071aaa6c58dSLisandro Dalcin PetscFunctionBegin; 3072aaa6c58dSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 30739566063dSJacob Faibussowitsch PetscCall(TSSetTime(ts,initial_time)); 30749566063dSJacob Faibussowitsch PetscCall(TSSetTimeStep(ts,time_step)); 3075aaa6c58dSLisandro Dalcin PetscFunctionReturn(0); 3076aaa6c58dSLisandro Dalcin } 3077aaa6c58dSLisandro Dalcin 3078aaa6c58dSLisandro Dalcin /*@ 307919eac22cSLisandro Dalcin TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime(). 3080edc382c3SSatish Balay 3081edc382c3SSatish Balay Level: deprecated 3082edc382c3SSatish Balay 3083adb62b0dSMatthew Knepley @*/ 30847087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 3085adb62b0dSMatthew Knepley { 3086adb62b0dSMatthew Knepley PetscFunctionBegin; 30870700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3088abc0a331SBarry Smith if (maxsteps) { 30894482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 3090adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 3091adb62b0dSMatthew Knepley } 3092abc0a331SBarry Smith if (maxtime) { 3093064a246eSJacob Faibussowitsch PetscValidRealPointer(maxtime,3); 3094adb62b0dSMatthew Knepley *maxtime = ts->max_time; 3095adb62b0dSMatthew Knepley } 3096adb62b0dSMatthew Knepley PetscFunctionReturn(0); 3097adb62b0dSMatthew Knepley } 3098adb62b0dSMatthew Knepley 3099d763cef2SBarry Smith /*@ 310019eac22cSLisandro Dalcin TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime(). 3101edc382c3SSatish Balay 3102edc382c3SSatish Balay Level: deprecated 3103edc382c3SSatish Balay 3104d763cef2SBarry Smith @*/ 31057087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 3106d763cef2SBarry Smith { 3107d763cef2SBarry Smith PetscFunctionBegin; 31080700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3109c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 3110064a246eSJacob Faibussowitsch PetscValidLogicalCollectiveReal(ts,maxtime,3); 311139b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 311239b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 3113d763cef2SBarry Smith PetscFunctionReturn(0); 3114d763cef2SBarry Smith } 3115d763cef2SBarry Smith 3116d763cef2SBarry Smith /*@ 31175c5f5948SLisandro Dalcin TSGetTimeStepNumber - Deprecated, use TSGetStepNumber(). 3118edc382c3SSatish Balay 3119edc382c3SSatish Balay Level: deprecated 3120edc382c3SSatish Balay 31215c5f5948SLisandro Dalcin @*/ 3122e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); } 31235c5f5948SLisandro Dalcin 312419eac22cSLisandro Dalcin /*@ 31254f4e0956SLisandro Dalcin TSGetTotalSteps - Deprecated, use TSGetStepNumber(). 3126edc382c3SSatish Balay 3127edc382c3SSatish Balay Level: deprecated 3128edc382c3SSatish Balay 31294f4e0956SLisandro Dalcin @*/ 31304f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); } 31314f4e0956SLisandro Dalcin 31324f4e0956SLisandro Dalcin /*@ 3133d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 3134d763cef2SBarry Smith for use by the TS routines. 3135d763cef2SBarry Smith 3136d083f849SBarry Smith Logically Collective on TS 3137d763cef2SBarry Smith 3138d763cef2SBarry Smith Input Parameters: 3139d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 31400910c330SBarry Smith - u - the solution vector 3141d763cef2SBarry Smith 3142d763cef2SBarry Smith Level: beginner 3143d763cef2SBarry Smith 31440ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate() 3145d763cef2SBarry Smith @*/ 31460910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 3147d763cef2SBarry Smith { 3148496e6a7aSJed Brown DM dm; 31498737fe31SLisandro Dalcin 3150d763cef2SBarry Smith PetscFunctionBegin; 31510700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31520910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 31539566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)u)); 31549566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_sol)); 31550910c330SBarry Smith ts->vec_sol = u; 3156bbd56ea5SKarl Rupp 31579566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 31589566063dSJacob Faibussowitsch PetscCall(DMShellSetGlobalVector(dm,u)); 3159d763cef2SBarry Smith PetscFunctionReturn(0); 3160d763cef2SBarry Smith } 3161d763cef2SBarry Smith 3162ac226902SBarry Smith /*@C 3163000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 31643f2090d5SJed Brown called once at the beginning of each time step. 3165000e7ae3SMatthew Knepley 31663f9fe445SBarry Smith Logically Collective on TS 3167000e7ae3SMatthew Knepley 3168000e7ae3SMatthew Knepley Input Parameters: 3169000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3170000e7ae3SMatthew Knepley - func - The function 3171000e7ae3SMatthew Knepley 3172000e7ae3SMatthew Knepley Calling sequence of func: 317367b8a455SSatish Balay .vb 317467b8a455SSatish Balay PetscErrorCode func (TS ts); 317567b8a455SSatish Balay .ve 3176000e7ae3SMatthew Knepley 3177000e7ae3SMatthew Knepley Level: intermediate 3178000e7ae3SMatthew Knepley 3179dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep() 3180000e7ae3SMatthew Knepley @*/ 31817087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 3182000e7ae3SMatthew Knepley { 3183000e7ae3SMatthew Knepley PetscFunctionBegin; 31840700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3185ae60f76fSBarry Smith ts->prestep = func; 3186000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3187000e7ae3SMatthew Knepley } 3188000e7ae3SMatthew Knepley 318909ee8438SJed Brown /*@ 31903f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 31913f2090d5SJed Brown 31923f2090d5SJed Brown Collective on TS 31933f2090d5SJed Brown 31943f2090d5SJed Brown Input Parameters: 31953f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 31963f2090d5SJed Brown 31973f2090d5SJed Brown Notes: 31983f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 31993f2090d5SJed Brown so most users would not generally call this routine themselves. 32003f2090d5SJed Brown 32013f2090d5SJed Brown Level: developer 32023f2090d5SJed Brown 32039be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 32043f2090d5SJed Brown @*/ 32057087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 32063f2090d5SJed Brown { 32073f2090d5SJed Brown PetscFunctionBegin; 32080700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3209ae60f76fSBarry Smith if (ts->prestep) { 32105efd42a4SStefano Zampini Vec U; 3211ef8d1ce0SJohann Rudi PetscObjectId idprev; 3212ef8d1ce0SJohann Rudi PetscBool sameObject; 32135efd42a4SStefano Zampini PetscObjectState sprev,spost; 32145efd42a4SStefano Zampini 32159566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts,&U)); 32169566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U,&idprev)); 32179566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&sprev)); 3218a74df02fSJacob Faibussowitsch PetscStackCallStandard((*ts->prestep),ts); 32199566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts,&U)); 32209566063dSJacob Faibussowitsch PetscCall(PetscObjectCompareId((PetscObject)U,idprev,&sameObject)); 32219566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&spost)); 32229566063dSJacob Faibussowitsch if (!sameObject || sprev != spost) PetscCall(TSRestartStep(ts)); 3223312ce896SJed Brown } 32243f2090d5SJed Brown PetscFunctionReturn(0); 32253f2090d5SJed Brown } 32263f2090d5SJed Brown 3227b8123daeSJed Brown /*@C 3228b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 3229b8123daeSJed Brown called once at the beginning of each stage. 3230b8123daeSJed Brown 3231b8123daeSJed Brown Logically Collective on TS 3232b8123daeSJed Brown 3233b8123daeSJed Brown Input Parameters: 3234b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 3235b8123daeSJed Brown - func - The function 3236b8123daeSJed Brown 3237b8123daeSJed Brown Calling sequence of func: 323867b8a455SSatish Balay .vb 323967b8a455SSatish Balay PetscErrorCode func(TS ts, PetscReal stagetime); 324067b8a455SSatish Balay .ve 3241b8123daeSJed Brown 3242b8123daeSJed Brown Level: intermediate 3243b8123daeSJed Brown 3244b8123daeSJed Brown Note: 3245b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 324680275a0aSLisandro Dalcin The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being 3247b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 3248b8123daeSJed Brown 32499be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3250b8123daeSJed Brown @*/ 3251b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 3252b8123daeSJed Brown { 3253b8123daeSJed Brown PetscFunctionBegin; 3254b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3255ae60f76fSBarry Smith ts->prestage = func; 3256b8123daeSJed Brown PetscFunctionReturn(0); 3257b8123daeSJed Brown } 3258b8123daeSJed Brown 32599be3e283SDebojyoti Ghosh /*@C 32609be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 32619be3e283SDebojyoti Ghosh called once at the end of each stage. 32629be3e283SDebojyoti Ghosh 32639be3e283SDebojyoti Ghosh Logically Collective on TS 32649be3e283SDebojyoti Ghosh 32659be3e283SDebojyoti Ghosh Input Parameters: 32669be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 32679be3e283SDebojyoti Ghosh - func - The function 32689be3e283SDebojyoti Ghosh 32699be3e283SDebojyoti Ghosh Calling sequence of func: 327067b8a455SSatish Balay .vb 327167b8a455SSatish Balay PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 327267b8a455SSatish Balay .ve 32739be3e283SDebojyoti Ghosh 32749be3e283SDebojyoti Ghosh Level: intermediate 32759be3e283SDebojyoti Ghosh 32769be3e283SDebojyoti Ghosh Note: 32779be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 327880275a0aSLisandro Dalcin The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being 32799be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 32809be3e283SDebojyoti Ghosh 32819be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 32829be3e283SDebojyoti Ghosh @*/ 32839be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 32849be3e283SDebojyoti Ghosh { 32859be3e283SDebojyoti Ghosh PetscFunctionBegin; 32869be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 32879be3e283SDebojyoti Ghosh ts->poststage = func; 32889be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 32899be3e283SDebojyoti Ghosh } 32909be3e283SDebojyoti Ghosh 3291c688d042SShri Abhyankar /*@C 3292c688d042SShri Abhyankar TSSetPostEvaluate - Sets the general-purpose function 3293c688d042SShri Abhyankar called once at the end of each step evaluation. 3294c688d042SShri Abhyankar 3295c688d042SShri Abhyankar Logically Collective on TS 3296c688d042SShri Abhyankar 3297c688d042SShri Abhyankar Input Parameters: 3298c688d042SShri Abhyankar + ts - The TS context obtained from TSCreate() 3299c688d042SShri Abhyankar - func - The function 3300c688d042SShri Abhyankar 3301c688d042SShri Abhyankar Calling sequence of func: 330267b8a455SSatish Balay .vb 330367b8a455SSatish Balay PetscErrorCode func(TS ts); 330467b8a455SSatish Balay .ve 3305c688d042SShri Abhyankar 3306c688d042SShri Abhyankar Level: intermediate 3307c688d042SShri Abhyankar 3308c688d042SShri Abhyankar Note: 33091785ff2aSShri Abhyankar Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling 33101785ff2aSShri Abhyankar thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep() 3311e7e94ed4SShri Abhyankar may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step 3312e7e94ed4SShri Abhyankar solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step 3313e7e94ed4SShri Abhyankar with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime() 3314c688d042SShri Abhyankar 3315c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3316c688d042SShri Abhyankar @*/ 3317c688d042SShri Abhyankar PetscErrorCode TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS)) 3318c688d042SShri Abhyankar { 3319c688d042SShri Abhyankar PetscFunctionBegin; 3320c688d042SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3321c688d042SShri Abhyankar ts->postevaluate = func; 3322c688d042SShri Abhyankar PetscFunctionReturn(0); 3323c688d042SShri Abhyankar } 3324c688d042SShri Abhyankar 3325b8123daeSJed Brown /*@ 3326b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 3327b8123daeSJed Brown 3328b8123daeSJed Brown Collective on TS 3329b8123daeSJed Brown 3330b8123daeSJed Brown Input Parameters: 3331b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 33329be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 3333b8123daeSJed Brown 3334b8123daeSJed Brown Notes: 3335b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 3336b8123daeSJed Brown most users would not generally call this routine themselves. 3337b8123daeSJed Brown 3338b8123daeSJed Brown Level: developer 3339b8123daeSJed Brown 33409be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 3341b8123daeSJed Brown @*/ 3342b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 3343b8123daeSJed Brown { 3344b8123daeSJed Brown PetscFunctionBegin; 3345b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3346ae60f76fSBarry Smith if (ts->prestage) { 3347a74df02fSJacob Faibussowitsch PetscStackCallStandard((*ts->prestage),ts,stagetime); 3348b8123daeSJed Brown } 3349b8123daeSJed Brown PetscFunctionReturn(0); 3350b8123daeSJed Brown } 3351b8123daeSJed Brown 33529be3e283SDebojyoti Ghosh /*@ 33539be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 33549be3e283SDebojyoti Ghosh 33559be3e283SDebojyoti Ghosh Collective on TS 33569be3e283SDebojyoti Ghosh 33579be3e283SDebojyoti Ghosh Input Parameters: 33589be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 33599be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 33609be3e283SDebojyoti Ghosh stageindex - Stage number 33619be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 33629be3e283SDebojyoti Ghosh of stages) with the stage solutions 33639be3e283SDebojyoti Ghosh 33649be3e283SDebojyoti Ghosh Notes: 33659be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 33669be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 33679be3e283SDebojyoti Ghosh 33689be3e283SDebojyoti Ghosh Level: developer 33699be3e283SDebojyoti Ghosh 33709be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 33719be3e283SDebojyoti Ghosh @*/ 33729be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 33739be3e283SDebojyoti Ghosh { 33749be3e283SDebojyoti Ghosh PetscFunctionBegin; 33759be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 33764beae5d8SLisandro Dalcin if (ts->poststage) { 3377a74df02fSJacob Faibussowitsch PetscStackCallStandard((*ts->poststage),ts,stagetime,stageindex,Y); 33789be3e283SDebojyoti Ghosh } 33799be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 33809be3e283SDebojyoti Ghosh } 33819be3e283SDebojyoti Ghosh 3382c688d042SShri Abhyankar /*@ 3383c688d042SShri Abhyankar TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate() 3384c688d042SShri Abhyankar 3385c688d042SShri Abhyankar Collective on TS 3386c688d042SShri Abhyankar 3387c688d042SShri Abhyankar Input Parameters: 3388c688d042SShri Abhyankar . ts - The TS context obtained from TSCreate() 3389c688d042SShri Abhyankar 3390c688d042SShri Abhyankar Notes: 3391c688d042SShri Abhyankar TSPostEvaluate() is typically used within time stepping implementations, 3392c688d042SShri Abhyankar most users would not generally call this routine themselves. 3393c688d042SShri Abhyankar 3394c688d042SShri Abhyankar Level: developer 3395c688d042SShri Abhyankar 3396c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep() 3397c688d042SShri Abhyankar @*/ 3398c688d042SShri Abhyankar PetscErrorCode TSPostEvaluate(TS ts) 3399c688d042SShri Abhyankar { 3400c688d042SShri Abhyankar PetscFunctionBegin; 3401c688d042SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3402c688d042SShri Abhyankar if (ts->postevaluate) { 3403dcb233daSLisandro Dalcin Vec U; 3404dcb233daSLisandro Dalcin PetscObjectState sprev,spost; 3405dcb233daSLisandro Dalcin 34069566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts,&U)); 34079566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&sprev)); 3408a74df02fSJacob Faibussowitsch PetscStackCallStandard((*ts->postevaluate),ts); 34099566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&spost)); 34109566063dSJacob Faibussowitsch if (sprev != spost) PetscCall(TSRestartStep(ts)); 3411c688d042SShri Abhyankar } 3412c688d042SShri Abhyankar PetscFunctionReturn(0); 3413c688d042SShri Abhyankar } 3414c688d042SShri Abhyankar 3415ac226902SBarry Smith /*@C 3416000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 34173f2090d5SJed Brown called once at the end of each time step. 3418000e7ae3SMatthew Knepley 34193f9fe445SBarry Smith Logically Collective on TS 3420000e7ae3SMatthew Knepley 3421000e7ae3SMatthew Knepley Input Parameters: 3422000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3423000e7ae3SMatthew Knepley - func - The function 3424000e7ae3SMatthew Knepley 3425000e7ae3SMatthew Knepley Calling sequence of func: 3426b8123daeSJed Brown $ func (TS ts); 3427000e7ae3SMatthew Knepley 34281785ff2aSShri Abhyankar Notes: 34291785ff2aSShri Abhyankar The function set by TSSetPostStep() is called after each successful step. The solution vector X 34301785ff2aSShri Abhyankar obtained by TSGetSolution() may be different than that computed at the step end if the event handler 34311785ff2aSShri Abhyankar locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead. 34321785ff2aSShri Abhyankar 3433000e7ae3SMatthew Knepley Level: intermediate 3434000e7ae3SMatthew Knepley 3435dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep() 3436000e7ae3SMatthew Knepley @*/ 34377087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 3438000e7ae3SMatthew Knepley { 3439000e7ae3SMatthew Knepley PetscFunctionBegin; 34400700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3441ae60f76fSBarry Smith ts->poststep = func; 3442000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3443000e7ae3SMatthew Knepley } 3444000e7ae3SMatthew Knepley 344509ee8438SJed Brown /*@ 34463f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 34473f2090d5SJed Brown 34483f2090d5SJed Brown Collective on TS 34493f2090d5SJed Brown 34503f2090d5SJed Brown Input Parameters: 34513f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 34523f2090d5SJed Brown 34533f2090d5SJed Brown Notes: 34543f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 34553f2090d5SJed Brown so most users would not generally call this routine themselves. 34563f2090d5SJed Brown 34573f2090d5SJed Brown Level: developer 34583f2090d5SJed Brown 34593f2090d5SJed Brown @*/ 34607087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 34613f2090d5SJed Brown { 34623f2090d5SJed Brown PetscFunctionBegin; 34630700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3464ae60f76fSBarry Smith if (ts->poststep) { 34655efd42a4SStefano Zampini Vec U; 3466ef8d1ce0SJohann Rudi PetscObjectId idprev; 3467ef8d1ce0SJohann Rudi PetscBool sameObject; 34685efd42a4SStefano Zampini PetscObjectState sprev,spost; 34695efd42a4SStefano Zampini 34709566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts,&U)); 34719566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U,&idprev)); 34729566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&sprev)); 3473a74df02fSJacob Faibussowitsch PetscStackCallStandard((*ts->poststep),ts); 34749566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts,&U)); 34759566063dSJacob Faibussowitsch PetscCall(PetscObjectCompareId((PetscObject)U,idprev,&sameObject)); 34769566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U,&spost)); 34779566063dSJacob Faibussowitsch if (!sameObject || sprev != spost) PetscCall(TSRestartStep(ts)); 347872ac3e02SJed Brown } 34793f2090d5SJed Brown PetscFunctionReturn(0); 34803f2090d5SJed Brown } 34813f2090d5SJed Brown 3482cd652676SJed Brown /*@ 3483cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3484cd652676SJed Brown 3485cd652676SJed Brown Collective on TS 3486cd652676SJed Brown 34874165533cSJose E. Roman Input Parameters: 3488cd652676SJed Brown + ts - time stepping context 3489cd652676SJed Brown - t - time to interpolate to 3490cd652676SJed Brown 34914165533cSJose E. Roman Output Parameter: 34920910c330SBarry Smith . U - state at given time 3493cd652676SJed Brown 3494cd652676SJed Brown Level: intermediate 3495cd652676SJed Brown 3496cd652676SJed Brown Developer Notes: 3497cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3498cd652676SJed Brown 3499874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve() 3500cd652676SJed Brown @*/ 35010910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3502cd652676SJed Brown { 3503cd652676SJed Brown PetscFunctionBegin; 3504cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3505b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 35063c633725SBarry Smith 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]",t,(double)ts->ptime_prev,(double)ts->ptime); 35073c633725SBarry Smith PetscCheck(ts->ops->interpolate,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 35089566063dSJacob Faibussowitsch PetscCall((*ts->ops->interpolate)(ts,t,U)); 3509cd652676SJed Brown PetscFunctionReturn(0); 3510cd652676SJed Brown } 3511cd652676SJed Brown 3512d763cef2SBarry Smith /*@ 35136d9e5789SSean Farley TSStep - Steps one time step 3514d763cef2SBarry Smith 3515d763cef2SBarry Smith Collective on TS 3516d763cef2SBarry Smith 3517d763cef2SBarry Smith Input Parameter: 3518d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3519d763cef2SBarry Smith 352027829d71SBarry Smith Level: developer 3521d763cef2SBarry Smith 3522b8123daeSJed Brown Notes: 352327829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 352427829d71SBarry Smith 3525b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3526b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3527b8123daeSJed Brown 352819eac22cSLisandro Dalcin This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the 352919eac22cSLisandro Dalcin time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 353025cb2221SBarry Smith 35319be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 3532d763cef2SBarry Smith @*/ 3533193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3534d763cef2SBarry Smith { 3535fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3536be5899b3SLisandro Dalcin PetscReal ptime; 3537d763cef2SBarry Smith 3538d763cef2SBarry Smith PetscFunctionBegin; 35390700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3540*d0609cedSBarry Smith PetscCall(PetscCitationsRegister("@article{tspaper,\n" 3541fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3542f1d62c27SHong Zhang " author = {Abhyankar, Shrirang and Brown, Jed and Constantinescu, Emil and Ghosh, Debojyoti and Smith, Barry F. and Zhang, Hong},\n" 3543f1d62c27SHong Zhang " journal = {arXiv e-preprints},\n" 3544f1d62c27SHong Zhang " eprint = {1806.01437},\n" 3545f1d62c27SHong Zhang " archivePrefix = {arXiv},\n" 3546*d0609cedSBarry Smith " year = {2018}\n}\n",&cite)); 35479566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts)); 35489566063dSJacob Faibussowitsch PetscCall(TSTrajectorySetUp(ts->trajectory,ts)); 3549d405a339SMatthew Knepley 35503c633725SBarry Smith PetscCheck(ts->ops->step,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 35513c633725SBarry 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>"); 35523c633725SBarry 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()"); 35533c633725SBarry 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"); 3554a6772fa2SLisandro Dalcin 3555be5899b3SLisandro Dalcin if (!ts->steps) ts->ptime_prev = ts->ptime; 3556be5899b3SLisandro Dalcin ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev; 35572808aa04SLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3558fc8dbba5SLisandro Dalcin 35599566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_Step,ts,0,0,0)); 35609566063dSJacob Faibussowitsch PetscCall((*ts->ops->step)(ts)); 35619566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_Step,ts,0,0,0)); 3562fc8dbba5SLisandro Dalcin 3563fc8dbba5SLisandro Dalcin if (ts->reason >= 0) { 3564be5899b3SLisandro Dalcin ts->ptime_prev = ptime; 35652808aa04SLisandro Dalcin ts->steps++; 3566be5899b3SLisandro Dalcin ts->steprollback = PETSC_FALSE; 356728d5b5d6SLisandro Dalcin ts->steprestart = PETSC_FALSE; 35684a658b32SHong Zhang if (ts->tspan && PetscIsCloseAtTol(ts->ptime,ts->tspan->span_times[ts->tspan->spanctr],10*PETSC_MACHINE_EPSILON,0) && ts->tspan->spanctr < ts->tspan->num_span_times) PetscCall(VecCopy(ts->vec_sol,ts->tspan->vecs_sol[ts->tspan->spanctr++])); 3569d2daff3dSHong Zhang } 3570fc8dbba5SLisandro Dalcin 3571fc8dbba5SLisandro Dalcin if (!ts->reason) { 357208c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 357308c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 357408c7845fSBarry Smith } 3575fc8dbba5SLisandro Dalcin 35763c633725SBarry 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]); 35773c633725SBarry Smith PetscCheck(ts->reason >= 0 || !ts->errorifstepfailed,PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 357808c7845fSBarry Smith PetscFunctionReturn(0); 357908c7845fSBarry Smith } 358008c7845fSBarry Smith 358108c7845fSBarry Smith /*@ 35827cbde773SLisandro Dalcin TSEvaluateWLTE - Evaluate the weighted local truncation error norm 35837cbde773SLisandro Dalcin at the end of a time step with a given order of accuracy. 35847cbde773SLisandro Dalcin 35857cbde773SLisandro Dalcin Collective on TS 35867cbde773SLisandro Dalcin 35874165533cSJose E. Roman Input Parameters: 35887cbde773SLisandro Dalcin + ts - time stepping context 358997bb3fdcSJose E. Roman - wnormtype - norm type, either NORM_2 or NORM_INFINITY 35907cbde773SLisandro Dalcin 359197bb3fdcSJose E. Roman Input/Output Parameter: 359297bb3fdcSJose E. Roman . order - optional, desired order for the error evaluation or PETSC_DECIDE; 359397bb3fdcSJose E. Roman on output, the actual order of the error evaluation 359497bb3fdcSJose E. Roman 359597bb3fdcSJose E. Roman Output Parameter: 359697bb3fdcSJose E. Roman . wlte - the weighted local truncation error norm 35977cbde773SLisandro Dalcin 35987cbde773SLisandro Dalcin Level: advanced 35997cbde773SLisandro Dalcin 36007cbde773SLisandro Dalcin Notes: 36017cbde773SLisandro Dalcin If the timestepper cannot evaluate the error in a particular step 36027cbde773SLisandro Dalcin (eg. in the first step or restart steps after event handling), 36037cbde773SLisandro Dalcin this routine returns wlte=-1.0 . 36047cbde773SLisandro Dalcin 36057cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm() 36067cbde773SLisandro Dalcin @*/ 36077cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte) 36087cbde773SLisandro Dalcin { 36097cbde773SLisandro Dalcin PetscFunctionBegin; 36107cbde773SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 36117cbde773SLisandro Dalcin PetscValidType(ts,1); 3612064a246eSJacob Faibussowitsch PetscValidLogicalCollectiveEnum(ts,wnormtype,2); 36137cbde773SLisandro Dalcin if (order) PetscValidIntPointer(order,3); 36147cbde773SLisandro Dalcin if (order) PetscValidLogicalCollectiveInt(ts,*order,3); 36157cbde773SLisandro Dalcin PetscValidRealPointer(wlte,4); 36163c633725SBarry Smith PetscCheck(wnormtype == NORM_2 || wnormtype == NORM_INFINITY,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 36173c633725SBarry Smith PetscCheck(ts->ops->evaluatewlte,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name); 36189566063dSJacob Faibussowitsch PetscCall((*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte)); 36197cbde773SLisandro Dalcin PetscFunctionReturn(0); 36207cbde773SLisandro Dalcin } 36217cbde773SLisandro Dalcin 362205175c85SJed Brown /*@ 362305175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 362405175c85SJed Brown 36251c3436cfSJed Brown Collective on TS 362605175c85SJed Brown 36274165533cSJose E. Roman Input Parameters: 36281c3436cfSJed Brown + ts - time stepping context 36291c3436cfSJed Brown . order - desired order of accuracy 36300298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 363105175c85SJed Brown 36324165533cSJose E. Roman Output Parameter: 36330910c330SBarry Smith . U - state at the end of the current step 363405175c85SJed Brown 363505175c85SJed Brown Level: advanced 363605175c85SJed Brown 3637108c343cSJed Brown Notes: 3638108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3639108c343cSJed 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. 3640108c343cSJed Brown 36411c3436cfSJed Brown .seealso: TSStep(), TSAdapt 364205175c85SJed Brown @*/ 36430910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 364405175c85SJed Brown { 364505175c85SJed Brown PetscFunctionBegin; 364605175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 364705175c85SJed Brown PetscValidType(ts,1); 36480910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 36493c633725SBarry Smith PetscCheck(ts->ops->evaluatestep,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 36509566063dSJacob Faibussowitsch PetscCall((*ts->ops->evaluatestep)(ts,order,U,done)); 365105175c85SJed Brown PetscFunctionReturn(0); 365205175c85SJed Brown } 365305175c85SJed Brown 3654aad739acSMatthew G. Knepley /*@C 36552e61be88SMatthew G. Knepley TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping. 3656aad739acSMatthew G. Knepley 3657aad739acSMatthew G. Knepley Not collective 3658aad739acSMatthew G. Knepley 36594165533cSJose E. Roman Input Parameter: 3660aad739acSMatthew G. Knepley . ts - time stepping context 3661aad739acSMatthew G. Knepley 36624165533cSJose E. Roman Output Parameter: 36632e61be88SMatthew G. Knepley . initConditions - The function which computes an initial condition 3664aad739acSMatthew G. Knepley 3665aad739acSMatthew G. Knepley Level: advanced 3666aad739acSMatthew G. Knepley 3667aad739acSMatthew G. Knepley Notes: 3668aad739acSMatthew G. Knepley The calling sequence for the function is 36692e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u) 3670aad739acSMatthew G. Knepley $ ts - The timestepping context 36712e61be88SMatthew G. Knepley $ u - The input vector in which the initial condition is stored 3672aad739acSMatthew G. Knepley 36732e61be88SMatthew G. Knepley .seealso: TSSetComputeInitialCondition(), TSComputeInitialCondition() 3674aad739acSMatthew G. Knepley @*/ 36752e61be88SMatthew G. Knepley PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS, Vec)) 3676aad739acSMatthew G. Knepley { 3677aad739acSMatthew G. Knepley PetscFunctionBegin; 3678aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 36792e61be88SMatthew G. Knepley PetscValidPointer(initCondition, 2); 36802e61be88SMatthew G. Knepley *initCondition = ts->ops->initcondition; 3681aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3682aad739acSMatthew G. Knepley } 3683aad739acSMatthew G. Knepley 3684aad739acSMatthew G. Knepley /*@C 36852e61be88SMatthew G. Knepley TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping. 3686aad739acSMatthew G. Knepley 3687aad739acSMatthew G. Knepley Logically collective on ts 3688aad739acSMatthew G. Knepley 36894165533cSJose E. Roman Input Parameters: 3690aad739acSMatthew G. Knepley + ts - time stepping context 36912e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition 3692aad739acSMatthew G. Knepley 3693aad739acSMatthew G. Knepley Level: advanced 3694aad739acSMatthew G. Knepley 3695a96d6ef6SBarry Smith Calling sequence for initCondition: 3696a96d6ef6SBarry Smith $ PetscErrorCode initCondition(TS ts, Vec u) 3697a96d6ef6SBarry Smith 3698a96d6ef6SBarry Smith + ts - The timestepping context 3699a96d6ef6SBarry Smith - u - The input vector in which the initial condition is to be stored 3700aad739acSMatthew G. Knepley 37012e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSComputeInitialCondition() 3702aad739acSMatthew G. Knepley @*/ 37032e61be88SMatthew G. Knepley PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS, Vec)) 3704aad739acSMatthew G. Knepley { 3705aad739acSMatthew G. Knepley PetscFunctionBegin; 3706aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 37072e61be88SMatthew G. Knepley PetscValidFunction(initCondition, 2); 37082e61be88SMatthew G. Knepley ts->ops->initcondition = initCondition; 3709aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3710aad739acSMatthew G. Knepley } 3711aad739acSMatthew G. Knepley 3712aad739acSMatthew G. Knepley /*@ 37132e61be88SMatthew G. Knepley TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set. 3714aad739acSMatthew G. Knepley 3715aad739acSMatthew G. Knepley Collective on ts 3716aad739acSMatthew G. Knepley 37174165533cSJose E. Roman Input Parameters: 3718aad739acSMatthew G. Knepley + ts - time stepping context 37192e61be88SMatthew G. Knepley - u - The Vec to store the condition in which will be used in TSSolve() 3720aad739acSMatthew G. Knepley 3721aad739acSMatthew G. Knepley Level: advanced 3722aad739acSMatthew G. Knepley 37232e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve() 3724aad739acSMatthew G. Knepley @*/ 37252e61be88SMatthew G. Knepley PetscErrorCode TSComputeInitialCondition(TS ts, Vec u) 3726aad739acSMatthew G. Knepley { 3727aad739acSMatthew G. Knepley PetscFunctionBegin; 3728aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3729aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 37309566063dSJacob Faibussowitsch if (ts->ops->initcondition) PetscCall((*ts->ops->initcondition)(ts, u)); 3731aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3732aad739acSMatthew G. Knepley } 3733aad739acSMatthew G. Knepley 3734aad739acSMatthew G. Knepley /*@C 3735aad739acSMatthew G. Knepley TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping. 3736aad739acSMatthew G. Knepley 3737aad739acSMatthew G. Knepley Not collective 3738aad739acSMatthew G. Knepley 37394165533cSJose E. Roman Input Parameter: 3740aad739acSMatthew G. Knepley . ts - time stepping context 3741aad739acSMatthew G. Knepley 37424165533cSJose E. Roman Output Parameter: 3743aad739acSMatthew G. Knepley . exactError - The function which computes the solution error 3744aad739acSMatthew G. Knepley 3745aad739acSMatthew G. Knepley Level: advanced 3746aad739acSMatthew G. Knepley 3747a96d6ef6SBarry Smith Calling sequence for exactError: 3748a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u) 3749a96d6ef6SBarry Smith 3750a96d6ef6SBarry Smith + ts - The timestepping context 3751a96d6ef6SBarry Smith . u - The approximate solution vector 3752a96d6ef6SBarry Smith - e - The input vector in which the error is stored 3753aad739acSMatthew G. Knepley 3754aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError() 3755aad739acSMatthew G. Knepley @*/ 3756aad739acSMatthew G. Knepley PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS, Vec, Vec)) 3757aad739acSMatthew G. Knepley { 3758aad739acSMatthew G. Knepley PetscFunctionBegin; 3759aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3760aad739acSMatthew G. Knepley PetscValidPointer(exactError, 2); 3761aad739acSMatthew G. Knepley *exactError = ts->ops->exacterror; 3762aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3763aad739acSMatthew G. Knepley } 3764aad739acSMatthew G. Knepley 3765aad739acSMatthew G. Knepley /*@C 3766aad739acSMatthew G. Knepley TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping. 3767aad739acSMatthew G. Knepley 3768aad739acSMatthew G. Knepley Logically collective on ts 3769aad739acSMatthew G. Knepley 37704165533cSJose E. Roman Input Parameters: 3771aad739acSMatthew G. Knepley + ts - time stepping context 3772aad739acSMatthew G. Knepley - exactError - The function which computes the solution error 3773aad739acSMatthew G. Knepley 3774aad739acSMatthew G. Knepley Level: advanced 3775aad739acSMatthew G. Knepley 3776a96d6ef6SBarry Smith Calling sequence for exactError: 3777a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u) 3778a96d6ef6SBarry Smith 3779a96d6ef6SBarry Smith + ts - The timestepping context 3780a96d6ef6SBarry Smith . u - The approximate solution vector 3781a96d6ef6SBarry Smith - e - The input vector in which the error is stored 3782aad739acSMatthew G. Knepley 3783aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError() 3784aad739acSMatthew G. Knepley @*/ 3785aad739acSMatthew G. Knepley PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS, Vec, Vec)) 3786aad739acSMatthew G. Knepley { 3787aad739acSMatthew G. Knepley PetscFunctionBegin; 3788aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3789f907fdbfSMatthew G. Knepley PetscValidFunction(exactError, 2); 3790aad739acSMatthew G. Knepley ts->ops->exacterror = exactError; 3791aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3792aad739acSMatthew G. Knepley } 3793aad739acSMatthew G. Knepley 3794aad739acSMatthew G. Knepley /*@ 3795aad739acSMatthew G. Knepley TSComputeExactError - Compute the solution error for the timestepping using the function previously set. 3796aad739acSMatthew G. Knepley 3797aad739acSMatthew G. Knepley Collective on ts 3798aad739acSMatthew G. Knepley 37994165533cSJose E. Roman Input Parameters: 3800aad739acSMatthew G. Knepley + ts - time stepping context 3801aad739acSMatthew G. Knepley . u - The approximate solution 3802aad739acSMatthew G. Knepley - e - The Vec used to store the error 3803aad739acSMatthew G. Knepley 3804aad739acSMatthew G. Knepley Level: advanced 3805aad739acSMatthew G. Knepley 38062e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve() 3807aad739acSMatthew G. Knepley @*/ 3808aad739acSMatthew G. Knepley PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e) 3809aad739acSMatthew G. Knepley { 3810aad739acSMatthew G. Knepley PetscFunctionBegin; 3811aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3812aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3813aad739acSMatthew G. Knepley PetscValidHeaderSpecific(e, VEC_CLASSID, 3); 38149566063dSJacob Faibussowitsch if (ts->ops->exacterror) PetscCall((*ts->ops->exacterror)(ts, u, e)); 3815aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3816aad739acSMatthew G. Knepley } 3817aad739acSMatthew G. Knepley 3818b1cb36f3SHong Zhang /*@ 38196a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 38206a4d4014SLisandro Dalcin 38216a4d4014SLisandro Dalcin Collective on TS 38226a4d4014SLisandro Dalcin 3823d8d19677SJose E. Roman Input Parameters: 38246a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 382563e21af5SBarry Smith - u - the solution vector (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used, 382663e21af5SBarry Smith otherwise must contain the initial conditions and will contain the solution at the final requested time 38275a3a76d0SJed Brown 38286a4d4014SLisandro Dalcin Level: beginner 38296a4d4014SLisandro Dalcin 38305a3a76d0SJed Brown Notes: 38315a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 38325a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 38335a3a76d0SJed Brown stepped over the final time. 38345a3a76d0SJed Brown 383563e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime() 38366a4d4014SLisandro Dalcin @*/ 3837cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 38386a4d4014SLisandro Dalcin { 3839b06615a5SLisandro Dalcin Vec solution; 3840f22f69f0SBarry Smith 38416a4d4014SLisandro Dalcin PetscFunctionBegin; 38420700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3843f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 3844303a5415SBarry Smith 38459566063dSJacob Faibussowitsch PetscCall(TSSetExactFinalTimeDefault(ts)); 3846ee41a567SStefano 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 */ 38470910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 38489566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u,&solution)); 38499566063dSJacob Faibussowitsch PetscCall(TSSetSolution(ts,solution)); 38509566063dSJacob Faibussowitsch PetscCall(VecDestroy(&solution)); /* grant ownership */ 38515a3a76d0SJed Brown } 38529566063dSJacob Faibussowitsch PetscCall(VecCopy(u,ts->vec_sol)); 38533c633725SBarry Smith PetscCheck(!ts->forward_solve,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE"); 3854bbd56ea5SKarl Rupp } else if (u) { 38559566063dSJacob Faibussowitsch PetscCall(TSSetSolution(ts,u)); 38565a3a76d0SJed Brown } 38579566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts)); 38589566063dSJacob Faibussowitsch PetscCall(TSTrajectorySetUp(ts->trajectory,ts)); 3859a6772fa2SLisandro Dalcin 38603c633725SBarry 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>"); 38613c633725SBarry 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()"); 38623c633725SBarry 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"); 38634a658b32SHong 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"); 38644a658b32SHong Zhang 38654a658b32SHong Zhang if (ts->tspan && PetscIsCloseAtTol(ts->ptime,ts->tspan->span_times[0],10*PETSC_MACHINE_EPSILON,0)) { /* starting point in time span */ 38664a658b32SHong Zhang PetscCall(VecCopy(ts->vec_sol,ts->tspan->vecs_sol[0])); 38674a658b32SHong Zhang ts->tspan->spanctr = 1; 38684a658b32SHong Zhang } 3869a6772fa2SLisandro Dalcin 3870715f1b00SHong Zhang if (ts->forward_solve) { 38719566063dSJacob Faibussowitsch PetscCall(TSForwardSetUp(ts)); 3872715f1b00SHong Zhang } 3873715f1b00SHong Zhang 3874e7069c78SShri /* reset number of steps only when the step is not restarted. ARKIMEX 3875715f1b00SHong Zhang restarts the step after an event. Resetting these counters in such case causes 3876e7069c78SShri TSTrajectory to incorrectly save the output files 3877e7069c78SShri */ 3878715f1b00SHong Zhang /* reset time step and iteration counters */ 38792808aa04SLisandro Dalcin if (!ts->steps) { 38805ef26d82SJed Brown ts->ksp_its = 0; 38815ef26d82SJed Brown ts->snes_its = 0; 3882c610991cSLisandro Dalcin ts->num_snes_failures = 0; 3883c610991cSLisandro Dalcin ts->reject = 0; 38842808aa04SLisandro Dalcin ts->steprestart = PETSC_TRUE; 38852808aa04SLisandro Dalcin ts->steprollback = PETSC_FALSE; 38867d51462cSStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 38872808aa04SLisandro Dalcin } 3888e97c63d7SStefano Zampini 38894a658b32SHong Zhang /* make sure initial time step does not overshoot final time or the next point in tspan */ 3890e97c63d7SStefano Zampini if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) { 38914a658b32SHong Zhang PetscReal maxdt; 3892e97c63d7SStefano Zampini PetscReal dt = ts->time_step; 3893e97c63d7SStefano Zampini 38944a658b32SHong Zhang if (ts->tspan) maxdt = ts->tspan->span_times[ts->tspan->spanctr] - ts->ptime; 38954a658b32SHong Zhang else maxdt = ts->max_time - ts->ptime; 3896e97c63d7SStefano Zampini ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt,maxdt,10*PETSC_MACHINE_EPSILON,0) ? maxdt : dt); 3897e97c63d7SStefano Zampini } 3898193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 3899193ac0bcSJed Brown 3900900f6b5bSMatthew G. Knepley { 3901900f6b5bSMatthew G. Knepley PetscViewer viewer; 3902900f6b5bSMatthew G. Knepley PetscViewerFormat format; 3903900f6b5bSMatthew G. Knepley PetscBool flg; 3904900f6b5bSMatthew G. Knepley static PetscBool incall = PETSC_FALSE; 3905900f6b5bSMatthew G. Knepley 3906900f6b5bSMatthew G. Knepley if (!incall) { 3907900f6b5bSMatthew G. Knepley /* Estimate the convergence rate of the time discretization */ 39089566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject) ts),((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg)); 3909900f6b5bSMatthew G. Knepley if (flg) { 3910900f6b5bSMatthew G. Knepley PetscConvEst conv; 3911900f6b5bSMatthew G. Knepley DM dm; 3912900f6b5bSMatthew G. Knepley PetscReal *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */ 3913900f6b5bSMatthew G. Knepley PetscInt Nf; 3914f2ed2dc7SMatthew G. Knepley PetscBool checkTemporal = PETSC_TRUE; 3915900f6b5bSMatthew G. Knepley 3916900f6b5bSMatthew G. Knepley incall = PETSC_TRUE; 39179566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_temporal", &checkTemporal, &flg)); 39189566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 39199566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &Nf)); 39209566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(PetscMax(Nf, 1), &alpha)); 39219566063dSJacob Faibussowitsch PetscCall(PetscConvEstCreate(PetscObjectComm((PetscObject) ts), &conv)); 39229566063dSJacob Faibussowitsch PetscCall(PetscConvEstUseTS(conv, checkTemporal)); 39239566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetSolver(conv, (PetscObject) ts)); 39249566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetFromOptions(conv)); 39259566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetUp(conv)); 39269566063dSJacob Faibussowitsch PetscCall(PetscConvEstGetConvRate(conv, alpha)); 39279566063dSJacob Faibussowitsch PetscCall(PetscViewerPushFormat(viewer, format)); 39289566063dSJacob Faibussowitsch PetscCall(PetscConvEstRateView(conv, alpha, viewer)); 39299566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(viewer)); 39309566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&viewer)); 39319566063dSJacob Faibussowitsch PetscCall(PetscConvEstDestroy(&conv)); 39329566063dSJacob Faibussowitsch PetscCall(PetscFree(alpha)); 3933900f6b5bSMatthew G. Knepley incall = PETSC_FALSE; 3934900f6b5bSMatthew G. Knepley } 3935900f6b5bSMatthew G. Knepley } 3936900f6b5bSMatthew G. Knepley } 3937900f6b5bSMatthew G. Knepley 39389566063dSJacob Faibussowitsch PetscCall(TSViewFromOptions(ts,NULL,"-ts_view_pre")); 3939f05ece33SBarry Smith 3940193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 39419566063dSJacob Faibussowitsch PetscCall((*ts->ops->solve)(ts)); 39429566063dSJacob Faibussowitsch if (u) PetscCall(VecCopy(ts->vec_sol,u)); 3943cc708dedSBarry Smith ts->solvetime = ts->ptime; 3944a6772fa2SLisandro Dalcin solution = ts->vec_sol; 3945be5899b3SLisandro Dalcin } else { /* Step the requested number of timesteps. */ 3946db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3947db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3948e7069c78SShri 39492808aa04SLisandro Dalcin if (!ts->steps) { 39509566063dSJacob Faibussowitsch PetscCall(TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol)); 39519566063dSJacob Faibussowitsch PetscCall(TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol)); 39522808aa04SLisandro Dalcin } 39536427ac75SLisandro Dalcin 3954e1a7a14fSJed Brown while (!ts->reason) { 39559566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol)); 39569687d888SLisandro Dalcin if (!ts->steprollback) { 39579566063dSJacob Faibussowitsch PetscCall(TSPreStep(ts)); 39589687d888SLisandro Dalcin } 39599566063dSJacob Faibussowitsch PetscCall(TSStep(ts)); 3960f3b1f45cSBarry Smith if (ts->testjacobian) { 39619566063dSJacob Faibussowitsch PetscCall(TSRHSJacobianTest(ts,NULL)); 3962f3b1f45cSBarry Smith } 3963f3b1f45cSBarry Smith if (ts->testjacobiantranspose) { 39649566063dSJacob Faibussowitsch PetscCall(TSRHSJacobianTestTranspose(ts,NULL)); 3965f3b1f45cSBarry Smith } 3966cd4cee2dSHong Zhang if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */ 39677b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */ 39689566063dSJacob Faibussowitsch PetscCall(TSForwardCostIntegral(ts)); 39697b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps++; 3970b1cb36f3SHong Zhang } 397158818c2dSLisandro Dalcin if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */ 39727b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */ 39739566063dSJacob Faibussowitsch PetscCall(TSForwardStep(ts)); 39747b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps++; 3975715f1b00SHong Zhang } 39769566063dSJacob Faibussowitsch PetscCall(TSPostEvaluate(ts)); 39779566063dSJacob 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. */ 397858818c2dSLisandro Dalcin if (ts->steprollback) { 39799566063dSJacob Faibussowitsch PetscCall(TSPostEvaluate(ts)); 398058818c2dSLisandro Dalcin } 3981e783b05fSHong Zhang if (!ts->steprollback) { 39829566063dSJacob Faibussowitsch PetscCall(TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol)); 39839566063dSJacob Faibussowitsch PetscCall(TSPostStep(ts)); 3984aeb4809dSShri Abhyankar } 3985193ac0bcSJed Brown } 39869566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol)); 39876427ac75SLisandro Dalcin 398849354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 39899566063dSJacob Faibussowitsch PetscCall(TSInterpolate(ts,ts->max_time,u)); 3990cc708dedSBarry Smith ts->solvetime = ts->max_time; 3991b06615a5SLisandro Dalcin solution = u; 39929566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts,-1,ts->solvetime,solution)); 39930574a7fbSJed Brown } else { 39949566063dSJacob Faibussowitsch if (u) PetscCall(VecCopy(ts->vec_sol,u)); 3995cc708dedSBarry Smith ts->solvetime = ts->ptime; 3996b06615a5SLisandro Dalcin solution = ts->vec_sol; 39970574a7fbSJed Brown } 3998193ac0bcSJed Brown } 3999d2daff3dSHong Zhang 40009566063dSJacob Faibussowitsch PetscCall(TSViewFromOptions(ts,NULL,"-ts_view")); 40019566063dSJacob Faibussowitsch PetscCall(VecViewFromOptions(solution,(PetscObject)ts,"-ts_view_solution")); 40029566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsBlock((PetscObject)ts)); 4003ef222394SBarry Smith if (ts->adjoint_solve) { 40049566063dSJacob Faibussowitsch PetscCall(TSAdjointSolve(ts)); 40052b0a91c0SBarry Smith } 40066a4d4014SLisandro Dalcin PetscFunctionReturn(0); 40076a4d4014SLisandro Dalcin } 40086a4d4014SLisandro Dalcin 4009d763cef2SBarry Smith /*@ 4010b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 4011d763cef2SBarry Smith 4012d763cef2SBarry Smith Not Collective 4013d763cef2SBarry Smith 4014d763cef2SBarry Smith Input Parameter: 4015d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 4016d763cef2SBarry Smith 4017d763cef2SBarry Smith Output Parameter: 401819eac22cSLisandro Dalcin . t - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime(). 4019d763cef2SBarry Smith 4020d763cef2SBarry Smith Level: beginner 4021d763cef2SBarry Smith 4022b8123daeSJed Brown Note: 4023b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 40249be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 4025b8123daeSJed Brown 40268f199f4dSPatrick Sanan .seealso: TSGetSolveTime(), TSSetTime(), TSGetTimeStep(), TSGetStepNumber() 4027d763cef2SBarry Smith 4028d763cef2SBarry Smith @*/ 40297087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 4030d763cef2SBarry Smith { 4031d763cef2SBarry Smith PetscFunctionBegin; 40320700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4033f7cf8827SBarry Smith PetscValidRealPointer(t,2); 4034d763cef2SBarry Smith *t = ts->ptime; 4035d763cef2SBarry Smith PetscFunctionReturn(0); 4036d763cef2SBarry Smith } 4037d763cef2SBarry Smith 4038e5e524a1SHong Zhang /*@ 4039e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 4040e5e524a1SHong Zhang 4041e5e524a1SHong Zhang Not Collective 4042e5e524a1SHong Zhang 4043e5e524a1SHong Zhang Input Parameter: 4044e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 4045e5e524a1SHong Zhang 4046e5e524a1SHong Zhang Output Parameter: 4047e5e524a1SHong Zhang . t - the previous time 4048e5e524a1SHong Zhang 4049e5e524a1SHong Zhang Level: beginner 4050e5e524a1SHong Zhang 4051aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep() 4052e5e524a1SHong Zhang 4053e5e524a1SHong Zhang @*/ 4054e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 4055e5e524a1SHong Zhang { 4056e5e524a1SHong Zhang PetscFunctionBegin; 4057e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4058e5e524a1SHong Zhang PetscValidRealPointer(t,2); 4059e5e524a1SHong Zhang *t = ts->ptime_prev; 4060e5e524a1SHong Zhang PetscFunctionReturn(0); 4061e5e524a1SHong Zhang } 4062e5e524a1SHong Zhang 40636a4d4014SLisandro Dalcin /*@ 40646a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 40656a4d4014SLisandro Dalcin 40663f9fe445SBarry Smith Logically Collective on TS 40676a4d4014SLisandro Dalcin 40686a4d4014SLisandro Dalcin Input Parameters: 40696a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 40706a4d4014SLisandro Dalcin - time - the time 40716a4d4014SLisandro Dalcin 40726a4d4014SLisandro Dalcin Level: intermediate 40736a4d4014SLisandro Dalcin 407419eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps() 40756a4d4014SLisandro Dalcin 40766a4d4014SLisandro Dalcin @*/ 40777087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 40786a4d4014SLisandro Dalcin { 40796a4d4014SLisandro Dalcin PetscFunctionBegin; 40800700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4081c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 40826a4d4014SLisandro Dalcin ts->ptime = t; 40836a4d4014SLisandro Dalcin PetscFunctionReturn(0); 40846a4d4014SLisandro Dalcin } 40856a4d4014SLisandro Dalcin 4086d763cef2SBarry Smith /*@C 4087d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 4088d763cef2SBarry Smith TS options in the database. 4089d763cef2SBarry Smith 40903f9fe445SBarry Smith Logically Collective on TS 4091d763cef2SBarry Smith 4092d8d19677SJose E. Roman Input Parameters: 4093d763cef2SBarry Smith + ts - The TS context 4094d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4095d763cef2SBarry Smith 4096d763cef2SBarry Smith Notes: 4097d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4098d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4099d763cef2SBarry Smith hyphen. 4100d763cef2SBarry Smith 4101d763cef2SBarry Smith Level: advanced 4102d763cef2SBarry Smith 4103d763cef2SBarry Smith .seealso: TSSetFromOptions() 4104d763cef2SBarry Smith 4105d763cef2SBarry Smith @*/ 41067087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 4107d763cef2SBarry Smith { 4108089b2837SJed Brown SNES snes; 4109d763cef2SBarry Smith 4110d763cef2SBarry Smith PetscFunctionBegin; 41110700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 41129566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)ts,prefix)); 41139566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 41149566063dSJacob Faibussowitsch PetscCall(SNESSetOptionsPrefix(snes,prefix)); 4115d763cef2SBarry Smith PetscFunctionReturn(0); 4116d763cef2SBarry Smith } 4117d763cef2SBarry Smith 4118d763cef2SBarry Smith /*@C 4119d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 4120d763cef2SBarry Smith TS options in the database. 4121d763cef2SBarry Smith 41223f9fe445SBarry Smith Logically Collective on TS 4123d763cef2SBarry Smith 4124d8d19677SJose E. Roman Input Parameters: 4125d763cef2SBarry Smith + ts - The TS context 4126d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4127d763cef2SBarry Smith 4128d763cef2SBarry Smith Notes: 4129d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4130d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4131d763cef2SBarry Smith hyphen. 4132d763cef2SBarry Smith 4133d763cef2SBarry Smith Level: advanced 4134d763cef2SBarry Smith 4135d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 4136d763cef2SBarry Smith 4137d763cef2SBarry Smith @*/ 41387087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 4139d763cef2SBarry Smith { 4140089b2837SJed Brown SNES snes; 4141d763cef2SBarry Smith 4142d763cef2SBarry Smith PetscFunctionBegin; 41430700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 41449566063dSJacob Faibussowitsch PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix)); 41459566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 41469566063dSJacob Faibussowitsch PetscCall(SNESAppendOptionsPrefix(snes,prefix)); 4147d763cef2SBarry Smith PetscFunctionReturn(0); 4148d763cef2SBarry Smith } 4149d763cef2SBarry Smith 4150d763cef2SBarry Smith /*@C 4151d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 4152d763cef2SBarry Smith TS options in the database. 4153d763cef2SBarry Smith 4154d763cef2SBarry Smith Not Collective 4155d763cef2SBarry Smith 4156d763cef2SBarry Smith Input Parameter: 4157d763cef2SBarry Smith . ts - The TS context 4158d763cef2SBarry Smith 4159d763cef2SBarry Smith Output Parameter: 4160d763cef2SBarry Smith . prefix - A pointer to the prefix string used 4161d763cef2SBarry Smith 416295452b02SPatrick Sanan Notes: 416395452b02SPatrick Sanan On the fortran side, the user should pass in a string 'prifix' of 4164d763cef2SBarry Smith sufficient length to hold the prefix. 4165d763cef2SBarry Smith 4166d763cef2SBarry Smith Level: intermediate 4167d763cef2SBarry Smith 4168d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 4169d763cef2SBarry Smith @*/ 41707087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 4171d763cef2SBarry Smith { 4172d763cef2SBarry Smith PetscFunctionBegin; 41730700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 41744482741eSBarry Smith PetscValidPointer(prefix,2); 41759566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)ts,prefix)); 4176d763cef2SBarry Smith PetscFunctionReturn(0); 4177d763cef2SBarry Smith } 4178d763cef2SBarry Smith 4179d763cef2SBarry Smith /*@C 4180d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4181d763cef2SBarry Smith 4182d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 4183d763cef2SBarry Smith 4184d763cef2SBarry Smith Input Parameter: 4185d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 4186d763cef2SBarry Smith 4187d763cef2SBarry Smith Output Parameters: 4188e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 4189e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 4190e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 4191e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 4192d763cef2SBarry Smith 419395452b02SPatrick Sanan Notes: 419495452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 4195d763cef2SBarry Smith 4196d763cef2SBarry Smith Level: intermediate 4197d763cef2SBarry Smith 419880275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber() 4199d763cef2SBarry Smith 4200d763cef2SBarry Smith @*/ 4201e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 4202d763cef2SBarry Smith { 420324989b8cSPeter Brune DM dm; 4204089b2837SJed Brown 4205d763cef2SBarry Smith PetscFunctionBegin; 420623a57915SBarry Smith if (Amat || Pmat) { 420723a57915SBarry Smith SNES snes; 42089566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 42099566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 42109566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes,Amat,Pmat,NULL,NULL)); 421123a57915SBarry Smith } 42129566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 42139566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm,func,ctx)); 4214d763cef2SBarry Smith PetscFunctionReturn(0); 4215d763cef2SBarry Smith } 4216d763cef2SBarry Smith 42172eca1d9cSJed Brown /*@C 42182eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 42192eca1d9cSJed Brown 42202eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 42212eca1d9cSJed Brown 42222eca1d9cSJed Brown Input Parameter: 42232eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 42242eca1d9cSJed Brown 42252eca1d9cSJed Brown Output Parameters: 4226e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4227e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 42282eca1d9cSJed Brown . f - The function to compute the matrices 42292eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 42302eca1d9cSJed Brown 423195452b02SPatrick Sanan Notes: 423295452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 42332eca1d9cSJed Brown 42342eca1d9cSJed Brown Level: advanced 42352eca1d9cSJed Brown 423680275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber() 42372eca1d9cSJed Brown 42382eca1d9cSJed Brown @*/ 4239e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 42402eca1d9cSJed Brown { 424124989b8cSPeter Brune DM dm; 42420910c330SBarry Smith 42432eca1d9cSJed Brown PetscFunctionBegin; 4244c0aab802Sstefano_zampini if (Amat || Pmat) { 4245c0aab802Sstefano_zampini SNES snes; 42469566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 42479566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 42489566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes,Amat,Pmat,NULL,NULL)); 4249c0aab802Sstefano_zampini } 42509566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts,&dm)); 42519566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm,f,ctx)); 42522eca1d9cSJed Brown PetscFunctionReturn(0); 42532eca1d9cSJed Brown } 42542eca1d9cSJed Brown 4255af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 42566c699258SBarry Smith /*@ 42572a808120SBarry Smith TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS 42586c699258SBarry Smith 4259d083f849SBarry Smith Logically Collective on ts 42606c699258SBarry Smith 42616c699258SBarry Smith Input Parameters: 42622a808120SBarry Smith + ts - the ODE integrator object 42632a808120SBarry Smith - dm - the dm, cannot be NULL 42646c699258SBarry Smith 4265e03a659cSJed Brown Notes: 4266e03a659cSJed Brown A DM can only be used for solving one problem at a time because information about the problem is stored on the DM, 4267e03a659cSJed Brown even when not using interfaces like DMTSSetIFunction(). Use DMClone() to get a distinct DM when solving 4268e03a659cSJed Brown different problems using the same function space. 4269e03a659cSJed Brown 42706c699258SBarry Smith Level: intermediate 42716c699258SBarry Smith 42726c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 42736c699258SBarry Smith @*/ 42747087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 42756c699258SBarry Smith { 4276089b2837SJed Brown SNES snes; 4277942e3340SBarry Smith DMTS tsdm; 42786c699258SBarry Smith 42796c699258SBarry Smith PetscFunctionBegin; 42800700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 42812a808120SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,2); 42829566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)dm)); 4283942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 42842a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 42859566063dSJacob Faibussowitsch PetscCall(DMCopyDMTS(ts->dm,dm)); 42869566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm,&tsdm)); 428724989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 428824989b8cSPeter Brune tsdm->originaldm = dm; 428924989b8cSPeter Brune } 429024989b8cSPeter Brune } 42919566063dSJacob Faibussowitsch PetscCall(DMDestroy(&ts->dm)); 429224989b8cSPeter Brune } 42936c699258SBarry Smith ts->dm = dm; 4294bbd56ea5SKarl Rupp 42959566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 42969566063dSJacob Faibussowitsch PetscCall(SNESSetDM(snes,dm)); 42976c699258SBarry Smith PetscFunctionReturn(0); 42986c699258SBarry Smith } 42996c699258SBarry Smith 43006c699258SBarry Smith /*@ 43016c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 43026c699258SBarry Smith 43033f9fe445SBarry Smith Not Collective 43046c699258SBarry Smith 43056c699258SBarry Smith Input Parameter: 43066c699258SBarry Smith . ts - the preconditioner context 43076c699258SBarry Smith 43086c699258SBarry Smith Output Parameter: 43096c699258SBarry Smith . dm - the dm 43106c699258SBarry Smith 43116c699258SBarry Smith Level: intermediate 43126c699258SBarry Smith 43136c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 43146c699258SBarry Smith @*/ 43157087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 43166c699258SBarry Smith { 43176c699258SBarry Smith PetscFunctionBegin; 43180700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4319496e6a7aSJed Brown if (!ts->dm) { 43209566063dSJacob Faibussowitsch PetscCall(DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm)); 43219566063dSJacob Faibussowitsch if (ts->snes) PetscCall(SNESSetDM(ts->snes,ts->dm)); 4322496e6a7aSJed Brown } 43236c699258SBarry Smith *dm = ts->dm; 43246c699258SBarry Smith PetscFunctionReturn(0); 43256c699258SBarry Smith } 43261713a123SBarry Smith 43270f5c6efeSJed Brown /*@ 43280f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 43290f5c6efeSJed Brown 43303f9fe445SBarry Smith Logically Collective on SNES 43310f5c6efeSJed Brown 4332d8d19677SJose E. Roman Input Parameters: 4333d42a1c89SJed Brown + snes - nonlinear solver 43340910c330SBarry Smith . U - the current state at which to evaluate the residual 4335d42a1c89SJed Brown - ctx - user context, must be a TS 43360f5c6efeSJed Brown 43370f5c6efeSJed Brown Output Parameter: 43380f5c6efeSJed Brown . F - the nonlinear residual 43390f5c6efeSJed Brown 43400f5c6efeSJed Brown Notes: 43410f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 43420f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 43430f5c6efeSJed Brown 43440f5c6efeSJed Brown Level: advanced 43450f5c6efeSJed Brown 43460f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 43470f5c6efeSJed Brown @*/ 43480910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 43490f5c6efeSJed Brown { 43500f5c6efeSJed Brown TS ts = (TS)ctx; 43510f5c6efeSJed Brown 43520f5c6efeSJed Brown PetscFunctionBegin; 43530f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 43540910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 43550f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 43560f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 43579566063dSJacob Faibussowitsch PetscCall((ts->ops->snesfunction)(snes,U,F,ts)); 43580f5c6efeSJed Brown PetscFunctionReturn(0); 43590f5c6efeSJed Brown } 43600f5c6efeSJed Brown 43610f5c6efeSJed Brown /*@ 43620f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 43630f5c6efeSJed Brown 43640f5c6efeSJed Brown Collective on SNES 43650f5c6efeSJed Brown 4366d8d19677SJose E. Roman Input Parameters: 43670f5c6efeSJed Brown + snes - nonlinear solver 43680910c330SBarry Smith . U - the current state at which to evaluate the residual 43690f5c6efeSJed Brown - ctx - user context, must be a TS 43700f5c6efeSJed Brown 4371d8d19677SJose E. Roman Output Parameters: 43720f5c6efeSJed Brown + A - the Jacobian 43736b867d5aSJose E. Roman - B - the preconditioning matrix (may be the same as A) 43740f5c6efeSJed Brown 43750f5c6efeSJed Brown Notes: 43760f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 43770f5c6efeSJed Brown 43780f5c6efeSJed Brown Level: developer 43790f5c6efeSJed Brown 43800f5c6efeSJed Brown .seealso: SNESSetJacobian() 43810f5c6efeSJed Brown @*/ 4382d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 43830f5c6efeSJed Brown { 43840f5c6efeSJed Brown TS ts = (TS)ctx; 43850f5c6efeSJed Brown 43860f5c6efeSJed Brown PetscFunctionBegin; 43870f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 43880910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 43890f5c6efeSJed Brown PetscValidPointer(A,3); 439094ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 43910f5c6efeSJed Brown PetscValidPointer(B,4); 439294ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 4393064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(ts,TS_CLASSID,5); 43949566063dSJacob Faibussowitsch PetscCall((ts->ops->snesjacobian)(snes,U,A,B,ts)); 43950f5c6efeSJed Brown PetscFunctionReturn(0); 43960f5c6efeSJed Brown } 4397325fc9f4SBarry Smith 43980e4ef248SJed Brown /*@C 43999ae8fd06SBarry Smith TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only 44000e4ef248SJed Brown 44010e4ef248SJed Brown Collective on TS 44020e4ef248SJed Brown 44034165533cSJose E. Roman Input Parameters: 44040e4ef248SJed Brown + ts - time stepping context 44050e4ef248SJed Brown . t - time at which to evaluate 44060910c330SBarry Smith . U - state at which to evaluate 44070e4ef248SJed Brown - ctx - context 44080e4ef248SJed Brown 44094165533cSJose E. Roman Output Parameter: 44100e4ef248SJed Brown . F - right hand side 44110e4ef248SJed Brown 44120e4ef248SJed Brown Level: intermediate 44130e4ef248SJed Brown 44140e4ef248SJed Brown Notes: 44150e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 44160e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 44170e4ef248SJed Brown 44180e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 44190e4ef248SJed Brown @*/ 44200910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 44210e4ef248SJed Brown { 44220e4ef248SJed Brown Mat Arhs,Brhs; 44230e4ef248SJed Brown 44240e4ef248SJed Brown PetscFunctionBegin; 44259566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts,&Arhs,&Brhs)); 44262663174eSHong Zhang /* undo the damage caused by shifting */ 44279566063dSJacob Faibussowitsch PetscCall(TSRecoverRHSJacobian(ts,Arhs,Brhs)); 44289566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts,t,U,Arhs,Brhs)); 44299566063dSJacob Faibussowitsch PetscCall(MatMult(Arhs,U,F)); 44300e4ef248SJed Brown PetscFunctionReturn(0); 44310e4ef248SJed Brown } 44320e4ef248SJed Brown 44330e4ef248SJed Brown /*@C 44340e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 44350e4ef248SJed Brown 44360e4ef248SJed Brown Collective on TS 44370e4ef248SJed Brown 44384165533cSJose E. Roman Input Parameters: 44390e4ef248SJed Brown + ts - time stepping context 44400e4ef248SJed Brown . t - time at which to evaluate 44410910c330SBarry Smith . U - state at which to evaluate 44420e4ef248SJed Brown - ctx - context 44430e4ef248SJed Brown 44444165533cSJose E. Roman Output Parameters: 44450e4ef248SJed Brown + A - pointer to operator 444697bb3fdcSJose E. Roman - B - pointer to preconditioning matrix 44470e4ef248SJed Brown 44480e4ef248SJed Brown Level: intermediate 44490e4ef248SJed Brown 44500e4ef248SJed Brown Notes: 44510e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 44520e4ef248SJed Brown 44530e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 44540e4ef248SJed Brown @*/ 4455d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 44560e4ef248SJed Brown { 44570e4ef248SJed Brown PetscFunctionBegin; 44580e4ef248SJed Brown PetscFunctionReturn(0); 44590e4ef248SJed Brown } 44600e4ef248SJed Brown 44610026cea9SSean Farley /*@C 44620026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 44630026cea9SSean Farley 44640026cea9SSean Farley Collective on TS 44650026cea9SSean Farley 44664165533cSJose E. Roman Input Parameters: 44670026cea9SSean Farley + ts - time stepping context 44680026cea9SSean Farley . t - time at which to evaluate 44690910c330SBarry Smith . U - state at which to evaluate 44700910c330SBarry Smith . Udot - time derivative of state vector 44710026cea9SSean Farley - ctx - context 44720026cea9SSean Farley 44734165533cSJose E. Roman Output Parameter: 44740026cea9SSean Farley . F - left hand side 44750026cea9SSean Farley 44760026cea9SSean Farley Level: intermediate 44770026cea9SSean Farley 44780026cea9SSean Farley Notes: 44790910c330SBarry 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 44800026cea9SSean Farley user is required to write their own TSComputeIFunction. 44810026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 44820026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 44830026cea9SSean Farley 44849ae8fd06SBarry Smith Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U 44859ae8fd06SBarry Smith 44869ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear() 44870026cea9SSean Farley @*/ 44880910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 44890026cea9SSean Farley { 44900026cea9SSean Farley Mat A,B; 44910026cea9SSean Farley 44920026cea9SSean Farley PetscFunctionBegin; 44939566063dSJacob Faibussowitsch PetscCall(TSGetIJacobian(ts,&A,&B,NULL,NULL)); 44949566063dSJacob Faibussowitsch PetscCall(TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE)); 44959566063dSJacob Faibussowitsch PetscCall(MatMult(A,Udot,F)); 44960026cea9SSean Farley PetscFunctionReturn(0); 44970026cea9SSean Farley } 44980026cea9SSean Farley 44990026cea9SSean Farley /*@C 4500b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 45010026cea9SSean Farley 45020026cea9SSean Farley Collective on TS 45030026cea9SSean Farley 45044165533cSJose E. Roman Input Parameters: 45050026cea9SSean Farley + ts - time stepping context 45060026cea9SSean Farley . t - time at which to evaluate 45070910c330SBarry Smith . U - state at which to evaluate 45080910c330SBarry Smith . Udot - time derivative of state vector 45090026cea9SSean Farley . shift - shift to apply 45100026cea9SSean Farley - ctx - context 45110026cea9SSean Farley 45124165533cSJose E. Roman Output Parameters: 45130026cea9SSean Farley + A - pointer to operator 451497bb3fdcSJose E. Roman - B - pointer to preconditioning matrix 45150026cea9SSean Farley 4516b41af12eSJed Brown Level: advanced 45170026cea9SSean Farley 45180026cea9SSean Farley Notes: 45190026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 45200026cea9SSean Farley 4521b41af12eSJed Brown It is only appropriate for problems of the form 4522b41af12eSJed Brown 4523b41af12eSJed Brown $ M Udot = F(U,t) 4524b41af12eSJed Brown 4525b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 4526b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 4527b41af12eSJed Brown an implicit operator of the form 4528b41af12eSJed Brown 4529b41af12eSJed Brown $ shift*M + J 4530b41af12eSJed Brown 4531b41af12eSJed 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 4532b41af12eSJed Brown a copy of M or reassemble it when requested. 4533b41af12eSJed Brown 45340026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 45350026cea9SSean Farley @*/ 4536d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 45370026cea9SSean Farley { 45380026cea9SSean Farley PetscFunctionBegin; 45399566063dSJacob Faibussowitsch PetscCall(MatScale(A, shift / ts->ijacobian.shift)); 4540b41af12eSJed Brown ts->ijacobian.shift = shift; 45410026cea9SSean Farley PetscFunctionReturn(0); 45420026cea9SSean Farley } 4543b41af12eSJed Brown 4544e817cc15SEmil Constantinescu /*@ 4545e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 4546e817cc15SEmil Constantinescu 4547e817cc15SEmil Constantinescu Not Collective 4548e817cc15SEmil Constantinescu 4549e817cc15SEmil Constantinescu Input Parameter: 4550e817cc15SEmil Constantinescu . ts - the TS context 4551e817cc15SEmil Constantinescu 4552e817cc15SEmil Constantinescu Output Parameter: 45534e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 4554e817cc15SEmil Constantinescu 4555e817cc15SEmil Constantinescu Level: beginner 4556e817cc15SEmil Constantinescu 4557e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 4558e817cc15SEmil Constantinescu @*/ 4559e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 4560e817cc15SEmil Constantinescu { 4561e817cc15SEmil Constantinescu PetscFunctionBegin; 4562e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4563e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 4564e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 4565e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4566e817cc15SEmil Constantinescu } 4567e817cc15SEmil Constantinescu 4568e817cc15SEmil Constantinescu /*@ 4569e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 4570e817cc15SEmil Constantinescu 4571e817cc15SEmil Constantinescu Not Collective 4572e817cc15SEmil Constantinescu 4573d8d19677SJose E. Roman Input Parameters: 4574e817cc15SEmil Constantinescu + ts - the TS context 45751297b224SEmil Constantinescu - equation_type - see TSEquationType 4576e817cc15SEmil Constantinescu 4577e817cc15SEmil Constantinescu Level: advanced 4578e817cc15SEmil Constantinescu 4579e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 4580e817cc15SEmil Constantinescu @*/ 4581e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 4582e817cc15SEmil Constantinescu { 4583e817cc15SEmil Constantinescu PetscFunctionBegin; 4584e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4585e817cc15SEmil Constantinescu ts->equation_type = equation_type; 4586e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4587e817cc15SEmil Constantinescu } 45880026cea9SSean Farley 45894af1b03aSJed Brown /*@ 45904af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 45914af1b03aSJed Brown 45924af1b03aSJed Brown Not Collective 45934af1b03aSJed Brown 45944af1b03aSJed Brown Input Parameter: 45954af1b03aSJed Brown . ts - the TS context 45964af1b03aSJed Brown 45974af1b03aSJed Brown Output Parameter: 45984af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 45994af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 46004af1b03aSJed Brown 4601487e0bb9SJed Brown Level: beginner 46024af1b03aSJed Brown 4603cd652676SJed Brown Notes: 4604cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 46054af1b03aSJed Brown 46064af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 46074af1b03aSJed Brown @*/ 46084af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 46094af1b03aSJed Brown { 46104af1b03aSJed Brown PetscFunctionBegin; 46114af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 46124af1b03aSJed Brown PetscValidPointer(reason,2); 46134af1b03aSJed Brown *reason = ts->reason; 46144af1b03aSJed Brown PetscFunctionReturn(0); 46154af1b03aSJed Brown } 46164af1b03aSJed Brown 4617d6ad946cSShri Abhyankar /*@ 4618d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 4619d6ad946cSShri Abhyankar 46206b221cbeSPatrick Sanan Logically Collective; reason must contain common value 4621d6ad946cSShri Abhyankar 46226b221cbeSPatrick Sanan Input Parameters: 4623d6ad946cSShri Abhyankar + ts - the TS context 46246b221cbeSPatrick Sanan - reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 4625d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 4626d6ad946cSShri Abhyankar 4627f5abba47SShri Abhyankar Level: advanced 4628d6ad946cSShri Abhyankar 4629d6ad946cSShri Abhyankar Notes: 46306b221cbeSPatrick Sanan Can only be called while TSSolve() is active. 4631d6ad946cSShri Abhyankar 4632d6ad946cSShri Abhyankar .seealso: TSConvergedReason 4633d6ad946cSShri Abhyankar @*/ 4634d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 4635d6ad946cSShri Abhyankar { 4636d6ad946cSShri Abhyankar PetscFunctionBegin; 4637d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4638d6ad946cSShri Abhyankar ts->reason = reason; 4639d6ad946cSShri Abhyankar PetscFunctionReturn(0); 4640d6ad946cSShri Abhyankar } 4641d6ad946cSShri Abhyankar 4642cc708dedSBarry Smith /*@ 4643cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 4644cc708dedSBarry Smith 4645cc708dedSBarry Smith Not Collective 4646cc708dedSBarry Smith 4647cc708dedSBarry Smith Input Parameter: 4648cc708dedSBarry Smith . ts - the TS context 4649cc708dedSBarry Smith 4650cc708dedSBarry Smith Output Parameter: 465119eac22cSLisandro Dalcin . ftime - the final time. This time corresponds to the final time set with TSSetMaxTime() 4652cc708dedSBarry Smith 4653487e0bb9SJed Brown Level: beginner 4654cc708dedSBarry Smith 4655cc708dedSBarry Smith Notes: 4656cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 4657cc708dedSBarry Smith 4658cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 4659cc708dedSBarry Smith @*/ 4660cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 4661cc708dedSBarry Smith { 4662cc708dedSBarry Smith PetscFunctionBegin; 4663cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4664dadcf809SJacob Faibussowitsch PetscValidRealPointer(ftime,2); 4665cc708dedSBarry Smith *ftime = ts->solvetime; 4666cc708dedSBarry Smith PetscFunctionReturn(0); 4667cc708dedSBarry Smith } 4668cc708dedSBarry Smith 46692c18e0fdSBarry Smith /*@ 46705ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 46719f67acb7SJed Brown used by the time integrator. 46729f67acb7SJed Brown 46739f67acb7SJed Brown Not Collective 46749f67acb7SJed Brown 46759f67acb7SJed Brown Input Parameter: 46769f67acb7SJed Brown . ts - TS context 46779f67acb7SJed Brown 46789f67acb7SJed Brown Output Parameter: 46799f67acb7SJed Brown . nits - number of nonlinear iterations 46809f67acb7SJed Brown 46819f67acb7SJed Brown Notes: 46829f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 46839f67acb7SJed Brown 46849f67acb7SJed Brown Level: intermediate 46859f67acb7SJed Brown 46865ef26d82SJed Brown .seealso: TSGetKSPIterations() 46879f67acb7SJed Brown @*/ 46885ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 46899f67acb7SJed Brown { 46909f67acb7SJed Brown PetscFunctionBegin; 46919f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 46929f67acb7SJed Brown PetscValidIntPointer(nits,2); 46935ef26d82SJed Brown *nits = ts->snes_its; 46949f67acb7SJed Brown PetscFunctionReturn(0); 46959f67acb7SJed Brown } 46969f67acb7SJed Brown 46979f67acb7SJed Brown /*@ 46985ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 46999f67acb7SJed Brown used by the time integrator. 47009f67acb7SJed Brown 47019f67acb7SJed Brown Not Collective 47029f67acb7SJed Brown 47039f67acb7SJed Brown Input Parameter: 47049f67acb7SJed Brown . ts - TS context 47059f67acb7SJed Brown 47069f67acb7SJed Brown Output Parameter: 47079f67acb7SJed Brown . lits - number of linear iterations 47089f67acb7SJed Brown 47099f67acb7SJed Brown Notes: 47109f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 47119f67acb7SJed Brown 47129f67acb7SJed Brown Level: intermediate 47139f67acb7SJed Brown 47145ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 47159f67acb7SJed Brown @*/ 47165ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 47179f67acb7SJed Brown { 47189f67acb7SJed Brown PetscFunctionBegin; 47199f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 47209f67acb7SJed Brown PetscValidIntPointer(lits,2); 47215ef26d82SJed Brown *lits = ts->ksp_its; 47229f67acb7SJed Brown PetscFunctionReturn(0); 47239f67acb7SJed Brown } 47249f67acb7SJed Brown 4725cef5090cSJed Brown /*@ 4726cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 4727cef5090cSJed Brown 4728cef5090cSJed Brown Not Collective 4729cef5090cSJed Brown 4730cef5090cSJed Brown Input Parameter: 4731cef5090cSJed Brown . ts - TS context 4732cef5090cSJed Brown 4733cef5090cSJed Brown Output Parameter: 4734cef5090cSJed Brown . rejects - number of steps rejected 4735cef5090cSJed Brown 4736cef5090cSJed Brown Notes: 4737cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4738cef5090cSJed Brown 4739cef5090cSJed Brown Level: intermediate 4740cef5090cSJed Brown 47415ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 4742cef5090cSJed Brown @*/ 4743cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 4744cef5090cSJed Brown { 4745cef5090cSJed Brown PetscFunctionBegin; 4746cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4747cef5090cSJed Brown PetscValidIntPointer(rejects,2); 4748cef5090cSJed Brown *rejects = ts->reject; 4749cef5090cSJed Brown PetscFunctionReturn(0); 4750cef5090cSJed Brown } 4751cef5090cSJed Brown 4752cef5090cSJed Brown /*@ 4753cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 4754cef5090cSJed Brown 4755cef5090cSJed Brown Not Collective 4756cef5090cSJed Brown 4757cef5090cSJed Brown Input Parameter: 4758cef5090cSJed Brown . ts - TS context 4759cef5090cSJed Brown 4760cef5090cSJed Brown Output Parameter: 4761cef5090cSJed Brown . fails - number of failed nonlinear solves 4762cef5090cSJed Brown 4763cef5090cSJed Brown Notes: 4764cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4765cef5090cSJed Brown 4766cef5090cSJed Brown Level: intermediate 4767cef5090cSJed Brown 47685ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 4769cef5090cSJed Brown @*/ 4770cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 4771cef5090cSJed Brown { 4772cef5090cSJed Brown PetscFunctionBegin; 4773cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4774cef5090cSJed Brown PetscValidIntPointer(fails,2); 4775cef5090cSJed Brown *fails = ts->num_snes_failures; 4776cef5090cSJed Brown PetscFunctionReturn(0); 4777cef5090cSJed Brown } 4778cef5090cSJed Brown 4779cef5090cSJed Brown /*@ 4780cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 4781cef5090cSJed Brown 4782cef5090cSJed Brown Not Collective 4783cef5090cSJed Brown 4784d8d19677SJose E. Roman Input Parameters: 4785cef5090cSJed Brown + ts - TS context 4786cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 4787cef5090cSJed Brown 4788cef5090cSJed Brown Notes: 4789cef5090cSJed Brown The counter is reset to zero for each step 4790cef5090cSJed Brown 4791cef5090cSJed Brown Options Database Key: 4792cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 4793cef5090cSJed Brown 4794cef5090cSJed Brown Level: intermediate 4795cef5090cSJed Brown 47965ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4797cef5090cSJed Brown @*/ 4798cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 4799cef5090cSJed Brown { 4800cef5090cSJed Brown PetscFunctionBegin; 4801cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4802cef5090cSJed Brown ts->max_reject = rejects; 4803cef5090cSJed Brown PetscFunctionReturn(0); 4804cef5090cSJed Brown } 4805cef5090cSJed Brown 4806cef5090cSJed Brown /*@ 4807cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 4808cef5090cSJed Brown 4809cef5090cSJed Brown Not Collective 4810cef5090cSJed Brown 4811d8d19677SJose E. Roman Input Parameters: 4812cef5090cSJed Brown + ts - TS context 4813cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 4814cef5090cSJed Brown 4815cef5090cSJed Brown Notes: 4816cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 4817cef5090cSJed Brown 4818cef5090cSJed Brown Options Database Key: 4819cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 4820cef5090cSJed Brown 4821cef5090cSJed Brown Level: intermediate 4822cef5090cSJed Brown 48235ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 4824cef5090cSJed Brown @*/ 4825cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 4826cef5090cSJed Brown { 4827cef5090cSJed Brown PetscFunctionBegin; 4828cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4829cef5090cSJed Brown ts->max_snes_failures = fails; 4830cef5090cSJed Brown PetscFunctionReturn(0); 4831cef5090cSJed Brown } 4832cef5090cSJed Brown 4833cef5090cSJed Brown /*@ 4834cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 4835cef5090cSJed Brown 4836cef5090cSJed Brown Not Collective 4837cef5090cSJed Brown 4838d8d19677SJose E. Roman Input Parameters: 4839cef5090cSJed Brown + ts - TS context 4840cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 4841cef5090cSJed Brown 4842cef5090cSJed Brown Options Database Key: 4843cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 4844cef5090cSJed Brown 4845cef5090cSJed Brown Level: intermediate 4846cef5090cSJed Brown 48475ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4848cef5090cSJed Brown @*/ 4849cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 4850cef5090cSJed Brown { 4851cef5090cSJed Brown PetscFunctionBegin; 4852cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4853cef5090cSJed Brown ts->errorifstepfailed = err; 4854cef5090cSJed Brown PetscFunctionReturn(0); 4855cef5090cSJed Brown } 4856cef5090cSJed Brown 485784df9cb4SJed Brown /*@ 4858552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 485984df9cb4SJed Brown 4860ed81e22dSJed Brown Collective on TS if controller has not been created yet 486184df9cb4SJed Brown 48624165533cSJose E. Roman Input Parameter: 4863ed81e22dSJed Brown . ts - time stepping context 486484df9cb4SJed Brown 48654165533cSJose E. Roman Output Parameter: 4866ed81e22dSJed Brown . adapt - adaptive controller 486784df9cb4SJed Brown 486884df9cb4SJed Brown Level: intermediate 486984df9cb4SJed Brown 4870ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 487184df9cb4SJed Brown @*/ 4872552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 487384df9cb4SJed Brown { 487484df9cb4SJed Brown PetscFunctionBegin; 487584df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4876bec58848SLisandro Dalcin PetscValidPointer(adapt,2); 487784df9cb4SJed Brown if (!ts->adapt) { 48789566063dSJacob Faibussowitsch PetscCall(TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt)); 48799566063dSJacob Faibussowitsch PetscCall(PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt)); 48809566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1)); 488184df9cb4SJed Brown } 4882bec58848SLisandro Dalcin *adapt = ts->adapt; 488384df9cb4SJed Brown PetscFunctionReturn(0); 488484df9cb4SJed Brown } 4885d6ebe24aSShri Abhyankar 48861c3436cfSJed Brown /*@ 48871c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 48881c3436cfSJed Brown 48891c3436cfSJed Brown Logically Collective 48901c3436cfSJed Brown 48914165533cSJose E. Roman Input Parameters: 48921c3436cfSJed Brown + ts - time integration context 48931c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 48940298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 48951c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 48960298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 48971c3436cfSJed Brown 4898a3cdaa26SBarry Smith Options Database keys: 4899a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 490067b8a455SSatish Balay - -ts_atol <atol> - Absolute tolerance for local truncation error 4901a3cdaa26SBarry Smith 49023ff766beSShri Abhyankar Notes: 49033ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 49043ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 49053ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 49063ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 49073ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 49083ff766beSShri Abhyankar differential variables. 49093ff766beSShri Abhyankar 49101c3436cfSJed Brown Level: beginner 49111c3436cfSJed Brown 49125c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSGetTolerances() 49131c3436cfSJed Brown @*/ 49141c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 49151c3436cfSJed Brown { 49161c3436cfSJed Brown PetscFunctionBegin; 4917c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 49181c3436cfSJed Brown if (vatol) { 49199566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)vatol)); 49209566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vatol)); 49211c3436cfSJed Brown ts->vatol = vatol; 49221c3436cfSJed Brown } 4923c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 49241c3436cfSJed Brown if (vrtol) { 49259566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)vrtol)); 49269566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vrtol)); 49271c3436cfSJed Brown ts->vrtol = vrtol; 49281c3436cfSJed Brown } 49291c3436cfSJed Brown PetscFunctionReturn(0); 49301c3436cfSJed Brown } 49311c3436cfSJed Brown 4932c5033834SJed Brown /*@ 4933c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 4934c5033834SJed Brown 4935c5033834SJed Brown Logically Collective 4936c5033834SJed Brown 49374165533cSJose E. Roman Input Parameter: 4938c5033834SJed Brown . ts - time integration context 4939c5033834SJed Brown 49404165533cSJose E. Roman Output Parameters: 49410298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 49420298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 49430298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 49440298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 4945c5033834SJed Brown 4946c5033834SJed Brown Level: beginner 4947c5033834SJed Brown 49485c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSSetTolerances() 4949c5033834SJed Brown @*/ 4950c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 4951c5033834SJed Brown { 4952c5033834SJed Brown PetscFunctionBegin; 4953c5033834SJed Brown if (atol) *atol = ts->atol; 4954c5033834SJed Brown if (vatol) *vatol = ts->vatol; 4955c5033834SJed Brown if (rtol) *rtol = ts->rtol; 4956c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 4957c5033834SJed Brown PetscFunctionReturn(0); 4958c5033834SJed Brown } 4959c5033834SJed Brown 49609c6b16b5SShri Abhyankar /*@ 4961a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 49629c6b16b5SShri Abhyankar 49639c6b16b5SShri Abhyankar Collective on TS 49649c6b16b5SShri Abhyankar 49654165533cSJose E. Roman Input Parameters: 49669c6b16b5SShri Abhyankar + ts - time stepping context 4967a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 4968a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 49699c6b16b5SShri Abhyankar 49704165533cSJose E. Roman Output Parameters: 4971a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 49727453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 4973a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 49749c6b16b5SShri Abhyankar 49759c6b16b5SShri Abhyankar Level: developer 49769c6b16b5SShri Abhyankar 4977deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity() 49789c6b16b5SShri Abhyankar @*/ 49797453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 49809c6b16b5SShri Abhyankar { 49819c6b16b5SShri Abhyankar PetscInt i,n,N,rstart; 49827453f775SEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 49837453f775SEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 49849c6b16b5SShri Abhyankar const PetscScalar *u,*y; 49857453f775SEmil Constantinescu PetscReal sum,suma,sumr,gsum,gsuma,gsumr,diff; 49867453f775SEmil Constantinescu PetscReal tol,tola,tolr; 49877453f775SEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 49889c6b16b5SShri Abhyankar 49899c6b16b5SShri Abhyankar PetscFunctionBegin; 49909c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4991a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 4992a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 4993a4868fbcSLisandro Dalcin PetscValidType(U,2); 4994a4868fbcSLisandro Dalcin PetscValidType(Y,3); 4995a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 4996dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm,4); 4997dadcf809SJacob Faibussowitsch PetscValidRealPointer(norma,5); 4998dadcf809SJacob Faibussowitsch PetscValidRealPointer(normr,6); 49993c633725SBarry Smith PetscCheck(U != Y,PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 50009c6b16b5SShri Abhyankar 50019566063dSJacob Faibussowitsch PetscCall(VecGetSize(U,&N)); 50029566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(U,&n)); 50039566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(U,&rstart,NULL)); 50049566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(U,&u)); 50059566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(Y,&y)); 50067453f775SEmil Constantinescu sum = 0.; n_loc = 0; 50077453f775SEmil Constantinescu suma = 0.; na_loc = 0; 50087453f775SEmil Constantinescu sumr = 0.; nr_loc = 0; 50099c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 50109c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 50119566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 50129566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 50139c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 501476cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 50157453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50167453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 50177453f775SEmil Constantinescu if (tola>0.) { 50187453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 50197453f775SEmil Constantinescu na_loc++; 50207453f775SEmil Constantinescu } 50217453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 50227453f775SEmil Constantinescu if (tolr>0.) { 50237453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 50247453f775SEmil Constantinescu nr_loc++; 50257453f775SEmil Constantinescu } 50267453f775SEmil Constantinescu tol=tola+tolr; 50277453f775SEmil Constantinescu if (tol>0.) { 50287453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 50297453f775SEmil Constantinescu n_loc++; 50307453f775SEmil Constantinescu } 50319c6b16b5SShri Abhyankar } 50329566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 50339566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 50349c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 50359c6b16b5SShri Abhyankar const PetscScalar *atol; 50369566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 50379c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 503876cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 50397453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50407453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 50417453f775SEmil Constantinescu if (tola>0.) { 50427453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 50437453f775SEmil Constantinescu na_loc++; 50447453f775SEmil Constantinescu } 50457453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 50467453f775SEmil Constantinescu if (tolr>0.) { 50477453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 50487453f775SEmil Constantinescu nr_loc++; 50497453f775SEmil Constantinescu } 50507453f775SEmil Constantinescu tol=tola+tolr; 50517453f775SEmil Constantinescu if (tol>0.) { 50527453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 50537453f775SEmil Constantinescu n_loc++; 50547453f775SEmil Constantinescu } 50559c6b16b5SShri Abhyankar } 50569566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 50579c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 50589c6b16b5SShri Abhyankar const PetscScalar *rtol; 50599566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 50609c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 506176cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 50627453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50637453f775SEmil Constantinescu tola = ts->atol; 50647453f775SEmil Constantinescu if (tola>0.) { 50657453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 50667453f775SEmil Constantinescu na_loc++; 50677453f775SEmil Constantinescu } 50687453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 50697453f775SEmil Constantinescu if (tolr>0.) { 50707453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 50717453f775SEmil Constantinescu nr_loc++; 50727453f775SEmil Constantinescu } 50737453f775SEmil Constantinescu tol=tola+tolr; 50747453f775SEmil Constantinescu if (tol>0.) { 50757453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 50767453f775SEmil Constantinescu n_loc++; 50777453f775SEmil Constantinescu } 50789c6b16b5SShri Abhyankar } 50799566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 50809c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 50819c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 508276cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 50837453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50847453f775SEmil Constantinescu tola = ts->atol; 50857453f775SEmil Constantinescu if (tola>0.) { 50867453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 50877453f775SEmil Constantinescu na_loc++; 50887453f775SEmil Constantinescu } 50897453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 50907453f775SEmil Constantinescu if (tolr>0.) { 50917453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 50927453f775SEmil Constantinescu nr_loc++; 50937453f775SEmil Constantinescu } 50947453f775SEmil Constantinescu tol=tola+tolr; 50957453f775SEmil Constantinescu if (tol>0.) { 50967453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 50977453f775SEmil Constantinescu n_loc++; 50987453f775SEmil Constantinescu } 50999c6b16b5SShri Abhyankar } 51009c6b16b5SShri Abhyankar } 51019566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(U,&u)); 51029566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(Y,&y)); 51039c6b16b5SShri Abhyankar 51047453f775SEmil Constantinescu err_loc[0] = sum; 51057453f775SEmil Constantinescu err_loc[1] = suma; 51067453f775SEmil Constantinescu err_loc[2] = sumr; 51077453f775SEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 51087453f775SEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 51097453f775SEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 51107453f775SEmil Constantinescu 51111c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts))); 51127453f775SEmil Constantinescu 51137453f775SEmil Constantinescu gsum = err_glb[0]; 51147453f775SEmil Constantinescu gsuma = err_glb[1]; 51157453f775SEmil Constantinescu gsumr = err_glb[2]; 51167453f775SEmil Constantinescu n_glb = err_glb[3]; 51177453f775SEmil Constantinescu na_glb = err_glb[4]; 51187453f775SEmil Constantinescu nr_glb = err_glb[5]; 51197453f775SEmil Constantinescu 5120b1316ef9SEmil Constantinescu *norm = 0.; 5121b1316ef9SEmil Constantinescu if (n_glb>0.) {*norm = PetscSqrtReal(gsum / n_glb);} 5122b1316ef9SEmil Constantinescu *norma = 0.; 5123b1316ef9SEmil Constantinescu if (na_glb>0.) {*norma = PetscSqrtReal(gsuma / na_glb);} 5124b1316ef9SEmil Constantinescu *normr = 0.; 5125b1316ef9SEmil Constantinescu if (nr_glb>0.) {*normr = PetscSqrtReal(gsumr / nr_glb);} 51269c6b16b5SShri Abhyankar 51273c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 51283c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 51293c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 51309c6b16b5SShri Abhyankar PetscFunctionReturn(0); 51319c6b16b5SShri Abhyankar } 51329c6b16b5SShri Abhyankar 51339c6b16b5SShri Abhyankar /*@ 5134a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 51359c6b16b5SShri Abhyankar 51369c6b16b5SShri Abhyankar Collective on TS 51379c6b16b5SShri Abhyankar 51384165533cSJose E. Roman Input Parameters: 51399c6b16b5SShri Abhyankar + ts - time stepping context 5140a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5141a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 51429c6b16b5SShri Abhyankar 51434165533cSJose E. Roman Output Parameters: 5144a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 51457453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5146a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 51479c6b16b5SShri Abhyankar 51489c6b16b5SShri Abhyankar Level: developer 51499c6b16b5SShri Abhyankar 5150deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2() 51519c6b16b5SShri Abhyankar @*/ 51527453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 51539c6b16b5SShri Abhyankar { 51547453f775SEmil Constantinescu PetscInt i,n,N,rstart; 51559c6b16b5SShri Abhyankar const PetscScalar *u,*y; 51567453f775SEmil Constantinescu PetscReal max,gmax,maxa,gmaxa,maxr,gmaxr; 51577453f775SEmil Constantinescu PetscReal tol,tola,tolr,diff; 51587453f775SEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 51599c6b16b5SShri Abhyankar 51609c6b16b5SShri Abhyankar PetscFunctionBegin; 51619c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5162a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5163a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5164a4868fbcSLisandro Dalcin PetscValidType(U,2); 5165a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5166a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5167dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm,4); 5168dadcf809SJacob Faibussowitsch PetscValidRealPointer(norma,5); 5169dadcf809SJacob Faibussowitsch PetscValidRealPointer(normr,6); 51703c633725SBarry Smith PetscCheck(U != Y,PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 51719c6b16b5SShri Abhyankar 51729566063dSJacob Faibussowitsch PetscCall(VecGetSize(U,&N)); 51739566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(U,&n)); 51749566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(U,&rstart,NULL)); 51759566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(U,&u)); 51769566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(Y,&y)); 51777453f775SEmil Constantinescu 51787453f775SEmil Constantinescu max=0.; 51797453f775SEmil Constantinescu maxa=0.; 51807453f775SEmil Constantinescu maxr=0.; 51817453f775SEmil Constantinescu 51827453f775SEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 51839c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 51849566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 51859566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 51867453f775SEmil Constantinescu 51877453f775SEmil Constantinescu for (i=0; i<n; i++) { 518876cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 51897453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 51907453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 51917453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 51927453f775SEmil Constantinescu tol = tola+tolr; 51937453f775SEmil Constantinescu if (tola>0.) { 51947453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 51957453f775SEmil Constantinescu } 51967453f775SEmil Constantinescu if (tolr>0.) { 51977453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 51987453f775SEmil Constantinescu } 51997453f775SEmil Constantinescu if (tol>0.) { 52007453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 52017453f775SEmil Constantinescu } 52029c6b16b5SShri Abhyankar } 52039566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 52049566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 52059c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 52069c6b16b5SShri Abhyankar const PetscScalar *atol; 52079566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 52087453f775SEmil Constantinescu for (i=0; i<n; i++) { 520976cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 52107453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 52117453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 52127453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52137453f775SEmil Constantinescu tol = tola+tolr; 52147453f775SEmil Constantinescu if (tola>0.) { 52157453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 52167453f775SEmil Constantinescu } 52177453f775SEmil Constantinescu if (tolr>0.) { 52187453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 52197453f775SEmil Constantinescu } 52207453f775SEmil Constantinescu if (tol>0.) { 52217453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 52227453f775SEmil Constantinescu } 52239c6b16b5SShri Abhyankar } 52249566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 52259c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 52269c6b16b5SShri Abhyankar const PetscScalar *rtol; 52279566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 52287453f775SEmil Constantinescu 52297453f775SEmil Constantinescu for (i=0; i<n; i++) { 523076cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 52317453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 52327453f775SEmil Constantinescu tola = ts->atol; 52337453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52347453f775SEmil Constantinescu tol = tola+tolr; 52357453f775SEmil Constantinescu if (tola>0.) { 52367453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 52377453f775SEmil Constantinescu } 52387453f775SEmil Constantinescu if (tolr>0.) { 52397453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 52407453f775SEmil Constantinescu } 52417453f775SEmil Constantinescu if (tol>0.) { 52427453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 52437453f775SEmil Constantinescu } 52449c6b16b5SShri Abhyankar } 52459566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 52469c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 52477453f775SEmil Constantinescu 52487453f775SEmil Constantinescu for (i=0; i<n; i++) { 524976cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 52507453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 52517453f775SEmil Constantinescu tola = ts->atol; 52527453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52537453f775SEmil Constantinescu tol = tola+tolr; 52547453f775SEmil Constantinescu if (tola>0.) { 52557453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 52567453f775SEmil Constantinescu } 52577453f775SEmil Constantinescu if (tolr>0.) { 52587453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 52597453f775SEmil Constantinescu } 52607453f775SEmil Constantinescu if (tol>0.) { 52617453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 52627453f775SEmil Constantinescu } 52639c6b16b5SShri Abhyankar } 52649c6b16b5SShri Abhyankar } 52659566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(U,&u)); 52669566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(Y,&y)); 52677453f775SEmil Constantinescu err_loc[0] = max; 52687453f775SEmil Constantinescu err_loc[1] = maxa; 52697453f775SEmil Constantinescu err_loc[2] = maxr; 52701c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts))); 52717453f775SEmil Constantinescu gmax = err_glb[0]; 52727453f775SEmil Constantinescu gmaxa = err_glb[1]; 52737453f775SEmil Constantinescu gmaxr = err_glb[2]; 52749c6b16b5SShri Abhyankar 52759c6b16b5SShri Abhyankar *norm = gmax; 52767453f775SEmil Constantinescu *norma = gmaxa; 52777453f775SEmil Constantinescu *normr = gmaxr; 52783c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 52793c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 52803c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 52819c6b16b5SShri Abhyankar PetscFunctionReturn(0); 52829c6b16b5SShri Abhyankar } 52839c6b16b5SShri Abhyankar 52841c3436cfSJed Brown /*@ 52858a175baeSEmil Constantinescu TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances 52861c3436cfSJed Brown 52871c3436cfSJed Brown Collective on TS 52881c3436cfSJed Brown 52894165533cSJose E. Roman Input Parameters: 52901c3436cfSJed Brown + ts - time stepping context 5291a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5292a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 5293a4868fbcSLisandro Dalcin - wnormtype - norm type, either NORM_2 or NORM_INFINITY 52947619abb3SShri 52954165533cSJose E. Roman Output Parameters: 5296a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 52978a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 5298a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 5299a4868fbcSLisandro Dalcin 5300a4868fbcSLisandro Dalcin Options Database Keys: 5301a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 5302a4868fbcSLisandro Dalcin 53031c3436cfSJed Brown Level: developer 53041c3436cfSJed Brown 53058a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm 53061c3436cfSJed Brown @*/ 53077453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 53081c3436cfSJed Brown { 53091c3436cfSJed Brown PetscFunctionBegin; 5310a4868fbcSLisandro Dalcin if (wnormtype == NORM_2) { 53119566063dSJacob Faibussowitsch PetscCall(TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr)); 5312a4868fbcSLisandro Dalcin } else if (wnormtype == NORM_INFINITY) { 53139566063dSJacob Faibussowitsch PetscCall(TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr)); 531498921bdaSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 53151c3436cfSJed Brown PetscFunctionReturn(0); 53161c3436cfSJed Brown } 53171c3436cfSJed Brown 53188a175baeSEmil Constantinescu /*@ 53198a175baeSEmil Constantinescu TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances 53208a175baeSEmil Constantinescu 53218a175baeSEmil Constantinescu Collective on TS 53228a175baeSEmil Constantinescu 53234165533cSJose E. Roman Input Parameters: 53248a175baeSEmil Constantinescu + ts - time stepping context 53258a175baeSEmil Constantinescu . E - error vector 53268a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 53278a175baeSEmil Constantinescu - Y - state vector, previous time step 53288a175baeSEmil Constantinescu 53294165533cSJose E. Roman Output Parameters: 5330a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 53318a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5332a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 53338a175baeSEmil Constantinescu 53348a175baeSEmil Constantinescu Level: developer 53358a175baeSEmil Constantinescu 53368a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity() 53378a175baeSEmil Constantinescu @*/ 53388a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 53398a175baeSEmil Constantinescu { 53408a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 53418a175baeSEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 53428a175baeSEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 53438a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 53448a175baeSEmil Constantinescu PetscReal err,sum,suma,sumr,gsum,gsuma,gsumr; 53458a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 53468a175baeSEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 53478a175baeSEmil Constantinescu 53488a175baeSEmil Constantinescu PetscFunctionBegin; 53498a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 53508a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 53518a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 53528a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 53538a175baeSEmil Constantinescu PetscValidType(E,2); 53548a175baeSEmil Constantinescu PetscValidType(U,3); 53558a175baeSEmil Constantinescu PetscValidType(Y,4); 53568a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 5357064a246eSJacob Faibussowitsch PetscCheckSameComm(U,3,Y,4); 5358dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm,5); 5359dadcf809SJacob Faibussowitsch PetscValidRealPointer(norma,6); 5360dadcf809SJacob Faibussowitsch PetscValidRealPointer(normr,7); 53618a175baeSEmil Constantinescu 53629566063dSJacob Faibussowitsch PetscCall(VecGetSize(E,&N)); 53639566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(E,&n)); 53649566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(E,&rstart,NULL)); 53659566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(E,&e)); 53669566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(U,&u)); 53679566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(Y,&y)); 53688a175baeSEmil Constantinescu sum = 0.; n_loc = 0; 53698a175baeSEmil Constantinescu suma = 0.; na_loc = 0; 53708a175baeSEmil Constantinescu sumr = 0.; nr_loc = 0; 53718a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { 53728a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 53739566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 53749566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 53758a175baeSEmil Constantinescu for (i=0; i<n; i++) { 537676cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 53778a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 53788a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 53798a175baeSEmil Constantinescu if (tola>0.) { 53808a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 53818a175baeSEmil Constantinescu na_loc++; 53828a175baeSEmil Constantinescu } 53838a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 53848a175baeSEmil Constantinescu if (tolr>0.) { 53858a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 53868a175baeSEmil Constantinescu nr_loc++; 53878a175baeSEmil Constantinescu } 53888a175baeSEmil Constantinescu tol=tola+tolr; 53898a175baeSEmil Constantinescu if (tol>0.) { 53908a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 53918a175baeSEmil Constantinescu n_loc++; 53928a175baeSEmil Constantinescu } 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 if (tola>0.) { 54048a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 54058a175baeSEmil Constantinescu na_loc++; 54068a175baeSEmil Constantinescu } 54078a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54088a175baeSEmil Constantinescu if (tolr>0.) { 54098a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 54108a175baeSEmil Constantinescu nr_loc++; 54118a175baeSEmil Constantinescu } 54128a175baeSEmil Constantinescu tol=tola+tolr; 54138a175baeSEmil Constantinescu if (tol>0.) { 54148a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 54158a175baeSEmil Constantinescu n_loc++; 54168a175baeSEmil Constantinescu } 54178a175baeSEmil Constantinescu } 54189566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 54198a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 54208a175baeSEmil Constantinescu const PetscScalar *rtol; 54219566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 54228a175baeSEmil Constantinescu for (i=0; i<n; i++) { 542376cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 54248a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 54258a175baeSEmil Constantinescu tola = ts->atol; 54268a175baeSEmil Constantinescu if (tola>0.) { 54278a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 54288a175baeSEmil Constantinescu na_loc++; 54298a175baeSEmil Constantinescu } 54308a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54318a175baeSEmil Constantinescu if (tolr>0.) { 54328a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 54338a175baeSEmil Constantinescu nr_loc++; 54348a175baeSEmil Constantinescu } 54358a175baeSEmil Constantinescu tol=tola+tolr; 54368a175baeSEmil Constantinescu if (tol>0.) { 54378a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 54388a175baeSEmil Constantinescu n_loc++; 54398a175baeSEmil Constantinescu } 54408a175baeSEmil Constantinescu } 54419566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 54428a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 54438a175baeSEmil Constantinescu for (i=0; i<n; i++) { 544476cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 54458a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 54468a175baeSEmil Constantinescu tola = ts->atol; 54478a175baeSEmil Constantinescu if (tola>0.) { 54488a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 54498a175baeSEmil Constantinescu na_loc++; 54508a175baeSEmil Constantinescu } 54518a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54528a175baeSEmil Constantinescu if (tolr>0.) { 54538a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 54548a175baeSEmil Constantinescu nr_loc++; 54558a175baeSEmil Constantinescu } 54568a175baeSEmil Constantinescu tol=tola+tolr; 54578a175baeSEmil Constantinescu if (tol>0.) { 54588a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 54598a175baeSEmil Constantinescu n_loc++; 54608a175baeSEmil Constantinescu } 54618a175baeSEmil Constantinescu } 54628a175baeSEmil Constantinescu } 54639566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(E,&e)); 54649566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(U,&u)); 54659566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(Y,&y)); 54668a175baeSEmil Constantinescu 54678a175baeSEmil Constantinescu err_loc[0] = sum; 54688a175baeSEmil Constantinescu err_loc[1] = suma; 54698a175baeSEmil Constantinescu err_loc[2] = sumr; 54708a175baeSEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 54718a175baeSEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 54728a175baeSEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 54738a175baeSEmil Constantinescu 54741c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts))); 54758a175baeSEmil Constantinescu 54768a175baeSEmil Constantinescu gsum = err_glb[0]; 54778a175baeSEmil Constantinescu gsuma = err_glb[1]; 54788a175baeSEmil Constantinescu gsumr = err_glb[2]; 54798a175baeSEmil Constantinescu n_glb = err_glb[3]; 54808a175baeSEmil Constantinescu na_glb = err_glb[4]; 54818a175baeSEmil Constantinescu nr_glb = err_glb[5]; 54828a175baeSEmil Constantinescu 54838a175baeSEmil Constantinescu *norm = 0.; 54848a175baeSEmil Constantinescu if (n_glb>0.) {*norm = PetscSqrtReal(gsum / n_glb);} 54858a175baeSEmil Constantinescu *norma = 0.; 54868a175baeSEmil Constantinescu if (na_glb>0.) {*norma = PetscSqrtReal(gsuma / na_glb);} 54878a175baeSEmil Constantinescu *normr = 0.; 54888a175baeSEmil Constantinescu if (nr_glb>0.) {*normr = PetscSqrtReal(gsumr / nr_glb);} 54898a175baeSEmil Constantinescu 54903c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 54913c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 54923c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 54938a175baeSEmil Constantinescu PetscFunctionReturn(0); 54948a175baeSEmil Constantinescu } 54958a175baeSEmil Constantinescu 54968a175baeSEmil Constantinescu /*@ 54978a175baeSEmil Constantinescu TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances 54988a175baeSEmil Constantinescu Collective on TS 54998a175baeSEmil Constantinescu 55004165533cSJose E. Roman Input Parameters: 55018a175baeSEmil Constantinescu + ts - time stepping context 55028a175baeSEmil Constantinescu . E - error vector 55038a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 55048a175baeSEmil Constantinescu - Y - state vector, previous time step 55058a175baeSEmil Constantinescu 55064165533cSJose E. Roman Output Parameters: 5507a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 55088a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5509a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 55108a175baeSEmil Constantinescu 55118a175baeSEmil Constantinescu Level: developer 55128a175baeSEmil Constantinescu 55138a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2() 55148a175baeSEmil Constantinescu @*/ 55158a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 55168a175baeSEmil Constantinescu { 55178a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 55188a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 55198a175baeSEmil Constantinescu PetscReal err,max,gmax,maxa,gmaxa,maxr,gmaxr; 55208a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 55218a175baeSEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 55228a175baeSEmil Constantinescu 55238a175baeSEmil Constantinescu PetscFunctionBegin; 55248a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 55258a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 55268a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 55278a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 55288a175baeSEmil Constantinescu PetscValidType(E,2); 55298a175baeSEmil Constantinescu PetscValidType(U,3); 55308a175baeSEmil Constantinescu PetscValidType(Y,4); 55318a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 5532064a246eSJacob Faibussowitsch PetscCheckSameComm(U,3,Y,4); 5533dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm,5); 5534dadcf809SJacob Faibussowitsch PetscValidRealPointer(norma,6); 5535dadcf809SJacob Faibussowitsch PetscValidRealPointer(normr,7); 55368a175baeSEmil Constantinescu 55379566063dSJacob Faibussowitsch PetscCall(VecGetSize(E,&N)); 55389566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(E,&n)); 55399566063dSJacob Faibussowitsch PetscCall(VecGetOwnershipRange(E,&rstart,NULL)); 55409566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(E,&e)); 55419566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(U,&u)); 55429566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(Y,&y)); 55438a175baeSEmil Constantinescu 55448a175baeSEmil Constantinescu max=0.; 55458a175baeSEmil Constantinescu maxa=0.; 55468a175baeSEmil Constantinescu maxr=0.; 55478a175baeSEmil Constantinescu 55488a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 55498a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 55509566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 55519566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 55528a175baeSEmil Constantinescu 55538a175baeSEmil Constantinescu for (i=0; i<n; i++) { 555476cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 55558a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 55568a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 55578a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 55588a175baeSEmil Constantinescu tol = tola+tolr; 55598a175baeSEmil Constantinescu if (tola>0.) { 55608a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 55618a175baeSEmil Constantinescu } 55628a175baeSEmil Constantinescu if (tolr>0.) { 55638a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 55648a175baeSEmil Constantinescu } 55658a175baeSEmil Constantinescu if (tol>0.) { 55668a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 55678a175baeSEmil Constantinescu } 55688a175baeSEmil Constantinescu } 55699566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 55709566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 55718a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 55728a175baeSEmil Constantinescu const PetscScalar *atol; 55739566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vatol,&atol)); 55748a175baeSEmil Constantinescu for (i=0; i<n; i++) { 557576cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 55768a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 55778a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 55788a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 55798a175baeSEmil Constantinescu tol = tola+tolr; 55808a175baeSEmil Constantinescu if (tola>0.) { 55818a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 55828a175baeSEmil Constantinescu } 55838a175baeSEmil Constantinescu if (tolr>0.) { 55848a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 55858a175baeSEmil Constantinescu } 55868a175baeSEmil Constantinescu if (tol>0.) { 55878a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 55888a175baeSEmil Constantinescu } 55898a175baeSEmil Constantinescu } 55909566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vatol,&atol)); 55918a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 55928a175baeSEmil Constantinescu const PetscScalar *rtol; 55939566063dSJacob Faibussowitsch PetscCall(VecGetArrayRead(ts->vrtol,&rtol)); 55948a175baeSEmil Constantinescu 55958a175baeSEmil Constantinescu for (i=0; i<n; i++) { 559676cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 55978a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 55988a175baeSEmil Constantinescu tola = ts->atol; 55998a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 56008a175baeSEmil Constantinescu tol = tola+tolr; 56018a175baeSEmil Constantinescu if (tola>0.) { 56028a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 56038a175baeSEmil Constantinescu } 56048a175baeSEmil Constantinescu if (tolr>0.) { 56058a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 56068a175baeSEmil Constantinescu } 56078a175baeSEmil Constantinescu if (tol>0.) { 56088a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 56098a175baeSEmil Constantinescu } 56108a175baeSEmil Constantinescu } 56119566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(ts->vrtol,&rtol)); 56128a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 56138a175baeSEmil Constantinescu 56148a175baeSEmil Constantinescu for (i=0; i<n; i++) { 561576cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 56168a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 56178a175baeSEmil Constantinescu tola = ts->atol; 56188a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 56198a175baeSEmil Constantinescu tol = tola+tolr; 56208a175baeSEmil Constantinescu if (tola>0.) { 56218a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 56228a175baeSEmil Constantinescu } 56238a175baeSEmil Constantinescu if (tolr>0.) { 56248a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 56258a175baeSEmil Constantinescu } 56268a175baeSEmil Constantinescu if (tol>0.) { 56278a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 56288a175baeSEmil Constantinescu } 56298a175baeSEmil Constantinescu } 56308a175baeSEmil Constantinescu } 56319566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(E,&e)); 56329566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(U,&u)); 56339566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayRead(Y,&y)); 56348a175baeSEmil Constantinescu err_loc[0] = max; 56358a175baeSEmil Constantinescu err_loc[1] = maxa; 56368a175baeSEmil Constantinescu err_loc[2] = maxr; 56371c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts))); 56388a175baeSEmil Constantinescu gmax = err_glb[0]; 56398a175baeSEmil Constantinescu gmaxa = err_glb[1]; 56408a175baeSEmil Constantinescu gmaxr = err_glb[2]; 56418a175baeSEmil Constantinescu 56428a175baeSEmil Constantinescu *norm = gmax; 56438a175baeSEmil Constantinescu *norma = gmaxa; 56448a175baeSEmil Constantinescu *normr = gmaxr; 56453c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 56463c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 56473c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr),PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 56488a175baeSEmil Constantinescu PetscFunctionReturn(0); 56498a175baeSEmil Constantinescu } 56508a175baeSEmil Constantinescu 56518a175baeSEmil Constantinescu /*@ 56528a175baeSEmil Constantinescu TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances 56538a175baeSEmil Constantinescu 56548a175baeSEmil Constantinescu Collective on TS 56558a175baeSEmil Constantinescu 56564165533cSJose E. Roman Input Parameters: 56578a175baeSEmil Constantinescu + ts - time stepping context 56588a175baeSEmil Constantinescu . E - error vector 56598a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 56608a175baeSEmil Constantinescu . Y - state vector, previous time step 56618a175baeSEmil Constantinescu - wnormtype - norm type, either NORM_2 or NORM_INFINITY 56628a175baeSEmil Constantinescu 56634165533cSJose E. Roman Output Parameters: 5664a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 56658a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 5666a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 56678a175baeSEmil Constantinescu 56688a175baeSEmil Constantinescu Options Database Keys: 56698a175baeSEmil Constantinescu . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 56708a175baeSEmil Constantinescu 56718a175baeSEmil Constantinescu Level: developer 56728a175baeSEmil Constantinescu 56738a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2() 56748a175baeSEmil Constantinescu @*/ 56758a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 56768a175baeSEmil Constantinescu { 56778a175baeSEmil Constantinescu PetscFunctionBegin; 56788a175baeSEmil Constantinescu if (wnormtype == NORM_2) { 56799566063dSJacob Faibussowitsch PetscCall(TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr)); 56808a175baeSEmil Constantinescu } else if (wnormtype == NORM_INFINITY) { 56819566063dSJacob Faibussowitsch PetscCall(TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr)); 568298921bdaSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 56838a175baeSEmil Constantinescu PetscFunctionReturn(0); 56848a175baeSEmil Constantinescu } 56858a175baeSEmil Constantinescu 56868d59e960SJed Brown /*@ 56878d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 56888d59e960SJed Brown 56898d59e960SJed Brown Logically Collective on TS 56908d59e960SJed Brown 56914165533cSJose E. Roman Input Parameters: 56928d59e960SJed Brown + ts - time stepping context 56938d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 56948d59e960SJed Brown 56958d59e960SJed Brown Note: 56968d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 56978d59e960SJed Brown 56988d59e960SJed Brown Level: intermediate 56998d59e960SJed Brown 57008d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 57018d59e960SJed Brown @*/ 57028d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 57038d59e960SJed Brown { 57048d59e960SJed Brown PetscFunctionBegin; 57058d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 57068d59e960SJed Brown ts->cfltime_local = cfltime; 57078d59e960SJed Brown ts->cfltime = -1.; 57088d59e960SJed Brown PetscFunctionReturn(0); 57098d59e960SJed Brown } 57108d59e960SJed Brown 57118d59e960SJed Brown /*@ 57128d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 57138d59e960SJed Brown 57148d59e960SJed Brown Collective on TS 57158d59e960SJed Brown 57164165533cSJose E. Roman Input Parameter: 57178d59e960SJed Brown . ts - time stepping context 57188d59e960SJed Brown 57194165533cSJose E. Roman Output Parameter: 57208d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 57218d59e960SJed Brown 57228d59e960SJed Brown Level: advanced 57238d59e960SJed Brown 57248d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 57258d59e960SJed Brown @*/ 57268d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 57278d59e960SJed Brown { 57288d59e960SJed Brown PetscFunctionBegin; 57298d59e960SJed Brown if (ts->cfltime < 0) { 57301c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts))); 57318d59e960SJed Brown } 57328d59e960SJed Brown *cfltime = ts->cfltime; 57338d59e960SJed Brown PetscFunctionReturn(0); 57348d59e960SJed Brown } 57358d59e960SJed Brown 5736d6ebe24aSShri Abhyankar /*@ 5737d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 5738d6ebe24aSShri Abhyankar 5739d6ebe24aSShri Abhyankar Input Parameters: 5740a2b725a8SWilliam Gropp + ts - the TS context. 5741d6ebe24aSShri Abhyankar . xl - lower bound. 5742a2b725a8SWilliam Gropp - xu - upper bound. 5743d6ebe24aSShri Abhyankar 5744d6ebe24aSShri Abhyankar Notes: 5745d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 5746e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 5747d6ebe24aSShri Abhyankar 57482bd2b0e6SSatish Balay Level: advanced 57492bd2b0e6SSatish Balay 5750d6ebe24aSShri Abhyankar @*/ 5751d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 5752d6ebe24aSShri Abhyankar { 5753d6ebe24aSShri Abhyankar SNES snes; 5754d6ebe24aSShri Abhyankar 5755d6ebe24aSShri Abhyankar PetscFunctionBegin; 57569566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts,&snes)); 57579566063dSJacob Faibussowitsch PetscCall(SNESVISetVariableBounds(snes,xl,xu)); 5758d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 5759d6ebe24aSShri Abhyankar } 5760d6ebe24aSShri Abhyankar 5761f9c1d6abSBarry Smith /*@ 5762f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 5763f9c1d6abSBarry Smith 5764d083f849SBarry Smith Collective on TS 5765f9c1d6abSBarry Smith 5766f9c1d6abSBarry Smith Input Parameters: 5767f9c1d6abSBarry Smith + ts - the TS context 5768f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 5769f9c1d6abSBarry Smith 5770f9c1d6abSBarry Smith Output Parameters: 5771f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 5772f9c1d6abSBarry Smith 5773f9c1d6abSBarry Smith Level: developer 5774f9c1d6abSBarry Smith 5775f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 5776f9c1d6abSBarry Smith @*/ 5777f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 5778f9c1d6abSBarry Smith { 5779f9c1d6abSBarry Smith PetscFunctionBegin; 5780f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 57813c633725SBarry Smith PetscCheck(ts->ops->linearstability,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 57829566063dSJacob Faibussowitsch PetscCall((*ts->ops->linearstability)(ts,xr,xi,yr,yi)); 5783f9c1d6abSBarry Smith PetscFunctionReturn(0); 5784f9c1d6abSBarry Smith } 578524655328SShri 578624655328SShri /*@ 5787dcb233daSLisandro Dalcin TSRestartStep - Flags the solver to restart the next step 5788dcb233daSLisandro Dalcin 5789dcb233daSLisandro Dalcin Collective on TS 5790dcb233daSLisandro Dalcin 5791dcb233daSLisandro Dalcin Input Parameter: 5792dcb233daSLisandro Dalcin . ts - the TS context obtained from TSCreate() 5793dcb233daSLisandro Dalcin 5794dcb233daSLisandro Dalcin Level: advanced 5795dcb233daSLisandro Dalcin 5796dcb233daSLisandro Dalcin Notes: 5797dcb233daSLisandro Dalcin Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of 5798dcb233daSLisandro Dalcin discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution 5799dcb233daSLisandro Dalcin vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For 5800dcb233daSLisandro Dalcin the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce 5801dcb233daSLisandro Dalcin discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with 5802dcb233daSLisandro Dalcin discontinuous source terms). 5803dcb233daSLisandro Dalcin 5804dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep() 5805dcb233daSLisandro Dalcin @*/ 5806dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts) 5807dcb233daSLisandro Dalcin { 5808dcb233daSLisandro Dalcin PetscFunctionBegin; 5809dcb233daSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5810dcb233daSLisandro Dalcin ts->steprestart = PETSC_TRUE; 5811dcb233daSLisandro Dalcin PetscFunctionReturn(0); 5812dcb233daSLisandro Dalcin } 5813dcb233daSLisandro Dalcin 5814dcb233daSLisandro Dalcin /*@ 581524655328SShri TSRollBack - Rolls back one time step 581624655328SShri 581724655328SShri Collective on TS 581824655328SShri 581924655328SShri Input Parameter: 582024655328SShri . ts - the TS context obtained from TSCreate() 582124655328SShri 582224655328SShri Level: advanced 582324655328SShri 582424655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 582524655328SShri @*/ 582624655328SShri PetscErrorCode TSRollBack(TS ts) 582724655328SShri { 582824655328SShri PetscFunctionBegin; 582924655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 58303c633725SBarry Smith PetscCheck(!ts->steprollback,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called"); 58313c633725SBarry Smith PetscCheck(ts->ops->rollback,PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 58329566063dSJacob Faibussowitsch PetscCall((*ts->ops->rollback)(ts)); 583324655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 583424655328SShri ts->ptime = ts->ptime_prev; 5835be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime_prev_rollback; 58362808aa04SLisandro Dalcin ts->steps--; 5837b3de5cdeSLisandro Dalcin ts->steprollback = PETSC_TRUE; 583824655328SShri PetscFunctionReturn(0); 583924655328SShri } 5840aeb4809dSShri Abhyankar 5841ff22ae23SHong Zhang /*@ 5842ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 5843ff22ae23SHong Zhang 5844ff22ae23SHong Zhang Input Parameter: 5845ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 5846ff22ae23SHong Zhang 58470429704eSStefano Zampini Output Parameters: 58480429704eSStefano Zampini + ns - the number of stages 58490429704eSStefano Zampini - Y - the current stage vectors 58500429704eSStefano Zampini 5851ff22ae23SHong Zhang Level: advanced 5852ff22ae23SHong Zhang 58530429704eSStefano Zampini Notes: Both ns and Y can be NULL. 58540429704eSStefano Zampini 5855ff22ae23SHong Zhang .seealso: TSCreate() 5856ff22ae23SHong Zhang @*/ 5857ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns,Vec **Y) 5858ff22ae23SHong Zhang { 5859ff22ae23SHong Zhang PetscFunctionBegin; 5860ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 5861dadcf809SJacob Faibussowitsch if (ns) PetscValidIntPointer(ns,2); 58620429704eSStefano Zampini if (Y) PetscValidPointer(Y,3); 58630429704eSStefano Zampini if (!ts->ops->getstages) { 58640429704eSStefano Zampini if (ns) *ns = 0; 58650429704eSStefano Zampini if (Y) *Y = NULL; 58660429704eSStefano Zampini } else { 58679566063dSJacob Faibussowitsch PetscCall((*ts->ops->getstages)(ts,ns,Y)); 5868ff22ae23SHong Zhang } 5869ff22ae23SHong Zhang PetscFunctionReturn(0); 5870ff22ae23SHong Zhang } 5871ff22ae23SHong Zhang 5872847ff0e1SMatthew G. Knepley /*@C 5873847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 5874847ff0e1SMatthew G. Knepley 5875847ff0e1SMatthew G. Knepley Collective on SNES 5876847ff0e1SMatthew G. Knepley 5877847ff0e1SMatthew G. Knepley Input Parameters: 5878847ff0e1SMatthew G. Knepley + ts - the TS context 5879847ff0e1SMatthew G. Knepley . t - current timestep 5880847ff0e1SMatthew G. Knepley . U - state vector 5881847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 5882847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 5883847ff0e1SMatthew G. Knepley - ctx - an optional user context 5884847ff0e1SMatthew G. Knepley 5885847ff0e1SMatthew G. Knepley Output Parameters: 5886847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 5887847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 5888847ff0e1SMatthew G. Knepley 5889847ff0e1SMatthew G. Knepley Level: intermediate 5890847ff0e1SMatthew G. Knepley 5891847ff0e1SMatthew G. Knepley Notes: 5892847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 5893847ff0e1SMatthew G. Knepley 5894847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 5895847ff0e1SMatthew G. Knepley 5896847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 5897847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 5898847ff0e1SMatthew G. Knepley 5899847ff0e1SMatthew G. Knepley This will first try to get the coloring from the DM. If the DM type has no coloring 5900847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 5901847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 5902847ff0e1SMatthew G. Knepley 5903847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction() 5904847ff0e1SMatthew G. Knepley @*/ 5905847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx) 5906847ff0e1SMatthew G. Knepley { 5907847ff0e1SMatthew G. Knepley SNES snes; 5908847ff0e1SMatthew G. Knepley MatFDColoring color; 5909847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 5910847ff0e1SMatthew G. Knepley 5911847ff0e1SMatthew G. Knepley PetscFunctionBegin; 59129566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL)); 59139566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color)); 5914847ff0e1SMatthew G. Knepley if (!color) { 5915847ff0e1SMatthew G. Knepley DM dm; 5916847ff0e1SMatthew G. Knepley ISColoring iscoloring; 5917847ff0e1SMatthew G. Knepley 59189566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 59199566063dSJacob Faibussowitsch PetscCall(DMHasColoring(dm, &hascolor)); 5920847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 59219566063dSJacob Faibussowitsch PetscCall(DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring)); 59229566063dSJacob Faibussowitsch PetscCall(MatFDColoringCreate(B, iscoloring, &color)); 59239566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts)); 59249566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFromOptions(color)); 59259566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetUp(B, iscoloring, color)); 59269566063dSJacob Faibussowitsch PetscCall(ISColoringDestroy(&iscoloring)); 5927847ff0e1SMatthew G. Knepley } else { 5928847ff0e1SMatthew G. Knepley MatColoring mc; 5929847ff0e1SMatthew G. Knepley 59309566063dSJacob Faibussowitsch PetscCall(MatColoringCreate(B, &mc)); 59319566063dSJacob Faibussowitsch PetscCall(MatColoringSetDistance(mc, 2)); 59329566063dSJacob Faibussowitsch PetscCall(MatColoringSetType(mc, MATCOLORINGSL)); 59339566063dSJacob Faibussowitsch PetscCall(MatColoringSetFromOptions(mc)); 59349566063dSJacob Faibussowitsch PetscCall(MatColoringApply(mc, &iscoloring)); 59359566063dSJacob Faibussowitsch PetscCall(MatColoringDestroy(&mc)); 59369566063dSJacob Faibussowitsch PetscCall(MatFDColoringCreate(B, iscoloring, &color)); 59379566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts)); 59389566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFromOptions(color)); 59399566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetUp(B, iscoloring, color)); 59409566063dSJacob Faibussowitsch PetscCall(ISColoringDestroy(&iscoloring)); 5941847ff0e1SMatthew G. Knepley } 59429566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color)); 59439566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject) color)); 5944847ff0e1SMatthew G. Knepley } 59459566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 59469566063dSJacob Faibussowitsch PetscCall(MatFDColoringApply(B, color, U, snes)); 5947847ff0e1SMatthew G. Knepley if (J != B) { 59489566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 59499566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 5950847ff0e1SMatthew G. Knepley } 5951847ff0e1SMatthew G. Knepley PetscFunctionReturn(0); 5952847ff0e1SMatthew G. Knepley } 595393b34091SDebojyoti Ghosh 5954cb9d8021SPierre Barbier de Reuille /*@ 59556bc98fa9SBarry Smith TSSetFunctionDomainError - Set a function that tests if the current state vector is valid 5956cb9d8021SPierre Barbier de Reuille 5957cb9d8021SPierre Barbier de Reuille Input Parameters: 59586bc98fa9SBarry Smith + ts - the TS context 59596bc98fa9SBarry Smith - func - function called within TSFunctionDomainError 59606bc98fa9SBarry Smith 59616bc98fa9SBarry Smith Calling sequence of func: 59626bc98fa9SBarry Smith $ PetscErrorCode func(TS ts,PetscReal time,Vec state,PetscBool reject) 59636bc98fa9SBarry Smith 59646bc98fa9SBarry Smith + ts - the TS context 59656bc98fa9SBarry Smith . time - the current time (of the stage) 59666bc98fa9SBarry Smith . state - the state to check if it is valid 59676bc98fa9SBarry Smith - reject - (output parameter) PETSC_FALSE if the state is acceptable, PETSC_TRUE if not acceptable 5968cb9d8021SPierre Barbier de Reuille 5969cb9d8021SPierre Barbier de Reuille Level: intermediate 5970cb9d8021SPierre Barbier de Reuille 59716bc98fa9SBarry Smith Notes: 59726bc98fa9SBarry Smith If an implicit ODE solver is being used then, in addition to providing this routine, the 59736bc98fa9SBarry Smith user's code should call SNESSetFunctionDomainError() when domain errors occur during 59746bc98fa9SBarry Smith function evaluations where the functions are provided by TSSetIFunction() or TSSetRHSFunction(). 59756bc98fa9SBarry Smith Use TSGetSNES() to obtain the SNES object 59766bc98fa9SBarry Smith 59776bc98fa9SBarry Smith Developer Notes: 59786bc98fa9SBarry Smith The naming of this function is inconsistent with the SNESSetFunctionDomainError() 59796bc98fa9SBarry Smith since one takes a function pointer and the other does not. 59806bc98fa9SBarry Smith 59816bc98fa9SBarry Smith .seealso: TSAdaptCheckStage(), TSFunctionDomainError(), SNESSetFunctionDomainError(), TSGetSNES() 5982cb9d8021SPierre Barbier de Reuille @*/ 5983cb9d8021SPierre Barbier de Reuille 5984d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*)) 5985cb9d8021SPierre Barbier de Reuille { 5986cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 5987cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID,1); 5988cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 5989cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 5990cb9d8021SPierre Barbier de Reuille } 5991cb9d8021SPierre Barbier de Reuille 5992cb9d8021SPierre Barbier de Reuille /*@ 59936bc98fa9SBarry Smith TSFunctionDomainError - Checks if the current state is valid 5994cb9d8021SPierre Barbier de Reuille 5995cb9d8021SPierre Barbier de Reuille Input Parameters: 59966bc98fa9SBarry Smith + ts - the TS context 59976bc98fa9SBarry Smith . stagetime - time of the simulation 59986bc98fa9SBarry Smith - Y - state vector to check. 5999cb9d8021SPierre Barbier de Reuille 6000cb9d8021SPierre Barbier de Reuille Output Parameter: 60016bc98fa9SBarry Smith . accept - Set to PETSC_FALSE if the current state vector is valid. 6002cb9d8021SPierre Barbier de Reuille 6003cb9d8021SPierre Barbier de Reuille Note: 60046bc98fa9SBarry Smith This function is called by the TS integration routines and calls the user provided function (set with TSSetFunctionDomainError()) 60056bc98fa9SBarry Smith to check if the current state is valid. 600696a0c994SBarry Smith 60076bc98fa9SBarry Smith Level: developer 60086bc98fa9SBarry Smith 60096bc98fa9SBarry Smith .seealso: TSSetFunctionDomainError() 6010cb9d8021SPierre Barbier de Reuille @*/ 6011d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept) 6012cb9d8021SPierre Barbier de Reuille { 6013cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 6014cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6015cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 6016cb9d8021SPierre Barbier de Reuille if (ts->functiondomainerror) { 6017a74df02fSJacob Faibussowitsch PetscStackCallStandard((*ts->functiondomainerror),ts,stagetime,Y,accept); 6018cb9d8021SPierre Barbier de Reuille } 6019cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 6020cb9d8021SPierre Barbier de Reuille } 60211ceb14c0SBarry Smith 602293b34091SDebojyoti Ghosh /*@C 6023e5168f73SEmil Constantinescu TSClone - This function clones a time step object. 602493b34091SDebojyoti Ghosh 6025d083f849SBarry Smith Collective 602693b34091SDebojyoti Ghosh 602793b34091SDebojyoti Ghosh Input Parameter: 602893b34091SDebojyoti Ghosh . tsin - The input TS 602993b34091SDebojyoti Ghosh 603093b34091SDebojyoti Ghosh Output Parameter: 6031e5168f73SEmil Constantinescu . tsout - The output TS (cloned) 603293b34091SDebojyoti Ghosh 60335eca1a21SEmil Constantinescu Notes: 60345eca1a21SEmil 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. 60355eca1a21SEmil Constantinescu 6036928bb9adSStefano 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); 60375eca1a21SEmil Constantinescu 60385eca1a21SEmil Constantinescu Level: developer 603993b34091SDebojyoti Ghosh 6040e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType() 604193b34091SDebojyoti Ghosh @*/ 6042baa10174SEmil Constantinescu PetscErrorCode TSClone(TS tsin, TS *tsout) 604393b34091SDebojyoti Ghosh { 604493b34091SDebojyoti Ghosh TS t; 6045dc846ba4SSatish Balay SNES snes_start; 6046dc846ba4SSatish Balay DM dm; 6047dc846ba4SSatish Balay TSType type; 604893b34091SDebojyoti Ghosh 604993b34091SDebojyoti Ghosh PetscFunctionBegin; 605093b34091SDebojyoti Ghosh PetscValidPointer(tsin,1); 605193b34091SDebojyoti Ghosh *tsout = NULL; 605293b34091SDebojyoti Ghosh 60539566063dSJacob Faibussowitsch PetscCall(PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView)); 605493b34091SDebojyoti Ghosh 605593b34091SDebojyoti Ghosh /* General TS description */ 605693b34091SDebojyoti Ghosh t->numbermonitors = 0; 6057d0c080abSJoseph Pusztay t->monitorFrequency = 1; 605893b34091SDebojyoti Ghosh t->setupcalled = 0; 605993b34091SDebojyoti Ghosh t->ksp_its = 0; 606093b34091SDebojyoti Ghosh t->snes_its = 0; 606193b34091SDebojyoti Ghosh t->nwork = 0; 60627d51462cSStefano Zampini t->rhsjacobian.time = PETSC_MIN_REAL; 606393b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 606493b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 606593b34091SDebojyoti Ghosh 60669566063dSJacob Faibussowitsch PetscCall(TSGetSNES(tsin,&snes_start)); 60679566063dSJacob Faibussowitsch PetscCall(TSSetSNES(t,snes_start)); 6068d15a3a53SEmil Constantinescu 60699566063dSJacob Faibussowitsch PetscCall(TSGetDM(tsin,&dm)); 60709566063dSJacob Faibussowitsch PetscCall(TSSetDM(t,dm)); 607193b34091SDebojyoti Ghosh 607293b34091SDebojyoti Ghosh t->adapt = tsin->adapt; 60739566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)t->adapt)); 607493b34091SDebojyoti Ghosh 6075e7069c78SShri t->trajectory = tsin->trajectory; 60769566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)t->trajectory)); 6077e7069c78SShri 6078e7069c78SShri t->event = tsin->event; 60796b10a48eSSatish Balay if (t->event) t->event->refct++; 6080e7069c78SShri 608193b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 608293b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 6083e7069c78SShri t->ptime_prev = tsin->ptime_prev; 608493b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 608593b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 608693b34091SDebojyoti Ghosh t->steps = tsin->steps; 608793b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 608893b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 608993b34091SDebojyoti Ghosh t->atol = tsin->atol; 609093b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 609193b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 609293b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 609393b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 609493b34091SDebojyoti Ghosh 60959566063dSJacob Faibussowitsch PetscCall(TSGetType(tsin,&type)); 60969566063dSJacob Faibussowitsch PetscCall(TSSetType(t,type)); 609793b34091SDebojyoti Ghosh 609893b34091SDebojyoti Ghosh t->vec_sol = NULL; 609993b34091SDebojyoti Ghosh 610093b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 610193b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 610293b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 610393b34091SDebojyoti Ghosh 61049566063dSJacob Faibussowitsch PetscCall(PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps))); 610593b34091SDebojyoti Ghosh 61060d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 61070d4fed19SBarry Smith PetscInt i; 61089566063dSJacob Faibussowitsch PetscCall(PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers)); 61090d4fed19SBarry Smith for (i=0; i<10; i++) { 61100d4fed19SBarry Smith ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 61110d4fed19SBarry Smith } 61120d4fed19SBarry Smith } 611393b34091SDebojyoti Ghosh *tsout = t; 611493b34091SDebojyoti Ghosh PetscFunctionReturn(0); 611593b34091SDebojyoti Ghosh } 6116f3b1f45cSBarry Smith 6117f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y) 6118f3b1f45cSBarry Smith { 6119f3b1f45cSBarry Smith TS ts = (TS) ctx; 6120f3b1f45cSBarry Smith 6121f3b1f45cSBarry Smith PetscFunctionBegin; 61229566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts,0,x,y)); 6123f3b1f45cSBarry Smith PetscFunctionReturn(0); 6124f3b1f45cSBarry Smith } 6125f3b1f45cSBarry Smith 6126f3b1f45cSBarry Smith /*@ 6127f3b1f45cSBarry Smith TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function. 6128f3b1f45cSBarry Smith 6129d083f849SBarry Smith Logically Collective on TS 6130f3b1f45cSBarry Smith 6131f3b1f45cSBarry Smith Input Parameters: 6132f3b1f45cSBarry Smith TS - the time stepping routine 6133f3b1f45cSBarry Smith 6134f3b1f45cSBarry Smith Output Parameter: 6135f3b1f45cSBarry Smith . flg - PETSC_TRUE if the multiply is likely correct 6136f3b1f45cSBarry Smith 6137f3b1f45cSBarry Smith Options Database: 6138f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator 6139f3b1f45cSBarry Smith 6140f3b1f45cSBarry Smith Level: advanced 6141f3b1f45cSBarry Smith 614295452b02SPatrick Sanan Notes: 614395452b02SPatrick Sanan This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian 6144f3b1f45cSBarry Smith 6145f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose() 6146f3b1f45cSBarry Smith @*/ 6147f3b1f45cSBarry Smith PetscErrorCode TSRHSJacobianTest(TS ts,PetscBool *flg) 6148f3b1f45cSBarry Smith { 6149f3b1f45cSBarry Smith Mat J,B; 6150f3b1f45cSBarry Smith TSRHSJacobian func; 6151f3b1f45cSBarry Smith void* ctx; 6152f3b1f45cSBarry Smith 6153f3b1f45cSBarry Smith PetscFunctionBegin; 61549566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts,&J,&B,&func,&ctx)); 61559566063dSJacob Faibussowitsch PetscCall((*func)(ts,0.0,ts->vec_sol,J,B,ctx)); 61569566063dSJacob Faibussowitsch PetscCall(MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg)); 6157f3b1f45cSBarry Smith PetscFunctionReturn(0); 6158f3b1f45cSBarry Smith } 6159f3b1f45cSBarry Smith 6160f3b1f45cSBarry Smith /*@C 6161f3b1f45cSBarry Smith TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function. 6162f3b1f45cSBarry Smith 6163d083f849SBarry Smith Logically Collective on TS 6164f3b1f45cSBarry Smith 6165f3b1f45cSBarry Smith Input Parameters: 6166f3b1f45cSBarry Smith TS - the time stepping routine 6167f3b1f45cSBarry Smith 6168f3b1f45cSBarry Smith Output Parameter: 6169f3b1f45cSBarry Smith . flg - PETSC_TRUE if the multiply is likely correct 6170f3b1f45cSBarry Smith 6171f3b1f45cSBarry Smith Options Database: 6172f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator 6173f3b1f45cSBarry Smith 617495452b02SPatrick Sanan Notes: 617595452b02SPatrick Sanan This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian 6176f3b1f45cSBarry Smith 6177f3b1f45cSBarry Smith Level: advanced 6178f3b1f45cSBarry Smith 6179f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest() 6180f3b1f45cSBarry Smith @*/ 6181f3b1f45cSBarry Smith PetscErrorCode TSRHSJacobianTestTranspose(TS ts,PetscBool *flg) 6182f3b1f45cSBarry Smith { 6183f3b1f45cSBarry Smith Mat J,B; 6184f3b1f45cSBarry Smith void *ctx; 6185f3b1f45cSBarry Smith TSRHSJacobian func; 6186f3b1f45cSBarry Smith 6187f3b1f45cSBarry Smith PetscFunctionBegin; 61889566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts,&J,&B,&func,&ctx)); 61899566063dSJacob Faibussowitsch PetscCall((*func)(ts,0.0,ts->vec_sol,J,B,ctx)); 61909566063dSJacob Faibussowitsch PetscCall(MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg)); 6191f3b1f45cSBarry Smith PetscFunctionReturn(0); 6192f3b1f45cSBarry Smith } 61930fe4d17eSHong Zhang 61940fe4d17eSHong Zhang /*@ 61950fe4d17eSHong Zhang TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used. 61960fe4d17eSHong Zhang 61970fe4d17eSHong Zhang Logically collective 61980fe4d17eSHong Zhang 6199d8d19677SJose E. Roman Input Parameters: 62000fe4d17eSHong Zhang + ts - timestepping context 62010fe4d17eSHong Zhang - use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used 62020fe4d17eSHong Zhang 62030fe4d17eSHong Zhang Options Database: 62040fe4d17eSHong Zhang . -ts_use_splitrhsfunction - <true,false> 62050fe4d17eSHong Zhang 62060fe4d17eSHong Zhang Notes: 62070fe4d17eSHong Zhang This is only useful for multirate methods 62080fe4d17eSHong Zhang 62090fe4d17eSHong Zhang Level: intermediate 62100fe4d17eSHong Zhang 62110fe4d17eSHong Zhang .seealso: TSGetUseSplitRHSFunction() 62120fe4d17eSHong Zhang @*/ 62130fe4d17eSHong Zhang PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction) 62140fe4d17eSHong Zhang { 62150fe4d17eSHong Zhang PetscFunctionBegin; 62160fe4d17eSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 62170fe4d17eSHong Zhang ts->use_splitrhsfunction = use_splitrhsfunction; 62180fe4d17eSHong Zhang PetscFunctionReturn(0); 62190fe4d17eSHong Zhang } 62200fe4d17eSHong Zhang 62210fe4d17eSHong Zhang /*@ 62220fe4d17eSHong Zhang TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used. 62230fe4d17eSHong Zhang 62240fe4d17eSHong Zhang Not collective 62250fe4d17eSHong Zhang 62260fe4d17eSHong Zhang Input Parameter: 62270fe4d17eSHong Zhang . ts - timestepping context 62280fe4d17eSHong Zhang 62290fe4d17eSHong Zhang Output Parameter: 62300fe4d17eSHong Zhang . use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used 62310fe4d17eSHong Zhang 62320fe4d17eSHong Zhang Level: intermediate 62330fe4d17eSHong Zhang 62340fe4d17eSHong Zhang .seealso: TSSetUseSplitRHSFunction() 62350fe4d17eSHong Zhang @*/ 62360fe4d17eSHong Zhang PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction) 62370fe4d17eSHong Zhang { 62380fe4d17eSHong Zhang PetscFunctionBegin; 62390fe4d17eSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 62400fe4d17eSHong Zhang *use_splitrhsfunction = ts->use_splitrhsfunction; 62410fe4d17eSHong Zhang PetscFunctionReturn(0); 62420fe4d17eSHong Zhang } 6243d60b7d5cSBarry Smith 6244d60b7d5cSBarry Smith /*@ 6245d60b7d5cSBarry Smith TSSetMatStructure - sets the relationship between the nonzero structure of the RHS Jacobian matrix to the IJacobian matrix. 6246d60b7d5cSBarry Smith 6247d60b7d5cSBarry Smith Logically Collective on ts 6248d60b7d5cSBarry Smith 6249d60b7d5cSBarry Smith Input Parameters: 6250d60b7d5cSBarry Smith + ts - the time-stepper 6251d60b7d5cSBarry Smith - str - the structure (the default is UNKNOWN_NONZERO_PATTERN) 6252d60b7d5cSBarry Smith 6253d60b7d5cSBarry Smith Level: intermediate 6254d60b7d5cSBarry Smith 6255d60b7d5cSBarry Smith Notes: 6256d60b7d5cSBarry Smith When the relationship between the nonzero structures is known and supplied the solution process can be much faster 6257d60b7d5cSBarry Smith 6258d60b7d5cSBarry Smith .seealso: MatAXPY(), MatStructure 6259d60b7d5cSBarry Smith @*/ 6260d60b7d5cSBarry Smith PetscErrorCode TSSetMatStructure(TS ts,MatStructure str) 6261d60b7d5cSBarry Smith { 6262d60b7d5cSBarry Smith PetscFunctionBegin; 6263d60b7d5cSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6264d60b7d5cSBarry Smith ts->axpy_pattern = str; 6265d60b7d5cSBarry Smith PetscFunctionReturn(0); 6266d60b7d5cSBarry Smith } 62674a658b32SHong Zhang 62684a658b32SHong Zhang /*@ 62694a658b32SHong Zhang TSSetTimeSpan - sets the time span. The solution will be computed and stored for each time requested. 62704a658b32SHong Zhang 62714a658b32SHong Zhang Collective on ts 62724a658b32SHong Zhang 62734a658b32SHong Zhang Input Parameters: 62744a658b32SHong Zhang + ts - the time-stepper 62754a658b32SHong Zhang . n - number of the time points (>=2) 62764a658b32SHong Zhang - span_times - array of the time points. The first element and the last element are the initial time and the final time respectively. 62774a658b32SHong Zhang 62784a658b32SHong Zhang Options Database Keys: 62794a658b32SHong Zhang . -ts_time_span <t0,...tf> - Sets the time span 62804a658b32SHong Zhang 62814a658b32SHong Zhang Level: beginner 62824a658b32SHong Zhang 62834a658b32SHong Zhang Notes: 62844a658b32SHong Zhang The elements in tspan must be all increasing. They correspond to the intermediate points for time integration. 62854a658b32SHong Zhang TS_EXACTFINALTIME_MATCHSTEP must be used to make the last time step in each sub-interval match the intermediate points specified. 62864a658b32SHong Zhang The intermediate solutions are saved in a vector array that can be accessed with TSGetSolutions(). Thus using time span may 62874a658b32SHong Zhang pressure the memory system when using a large number of span points. 62884a658b32SHong Zhang 62894a658b32SHong Zhang .seealso: TSGetTimeSpan(),TSGetSolutions() 62904a658b32SHong Zhang @*/ 62914a658b32SHong Zhang PetscErrorCode TSSetTimeSpan(TS ts,PetscInt n,PetscReal *span_times) 62924a658b32SHong Zhang { 62934a658b32SHong Zhang PetscFunctionBegin; 62944a658b32SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 62954a658b32SHong Zhang PetscCheck(n >= 2,PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Minimum time span size is 2 but %D is provided",n); 62964a658b32SHong Zhang if (ts->tspan && n != ts->tspan->num_span_times) { 62974a658b32SHong Zhang PetscCall(PetscFree(ts->tspan->span_times)); 62984a658b32SHong Zhang PetscCall(VecDestroyVecs(ts->tspan->num_span_times,&ts->tspan->vecs_sol)); 62994a658b32SHong Zhang PetscCall(PetscMalloc1(n,&ts->tspan->span_times)); 63004a658b32SHong Zhang } 63014a658b32SHong Zhang if (!ts->tspan) { 63024a658b32SHong Zhang TSTimeSpan tspan; 63034a658b32SHong Zhang PetscCall(PetscNew(&tspan)); 63044a658b32SHong Zhang PetscCall(PetscMalloc1(n,&tspan->span_times)); 63054a658b32SHong Zhang ts->tspan = tspan; 63064a658b32SHong Zhang } 63074a658b32SHong Zhang ts->tspan->num_span_times = n; 63084a658b32SHong Zhang PetscCall(PetscArraycpy(ts->tspan->span_times,span_times,n)); 63094a658b32SHong Zhang PetscCall(TSSetTime(ts,ts->tspan->span_times[0])); 63104a658b32SHong Zhang PetscCall(TSSetMaxTime(ts,ts->tspan->span_times[n-1])); 63114a658b32SHong Zhang PetscFunctionReturn(0); 63124a658b32SHong Zhang } 63134a658b32SHong Zhang 63144a658b32SHong Zhang /*@C 63154a658b32SHong Zhang TSGetTimeSpan - gets the time span. 63164a658b32SHong Zhang 63174a658b32SHong Zhang Not Collective 63184a658b32SHong Zhang 63194a658b32SHong Zhang Input Parameter: 63204a658b32SHong Zhang . ts - the time-stepper 63214a658b32SHong Zhang 63224a658b32SHong Zhang Output Parameters: 63234a658b32SHong Zhang + n - number of the time points (>=2) 63244a658b32SHong 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. 63254a658b32SHong Zhang 63264a658b32SHong Zhang Level: beginner 63274a658b32SHong Zhang Notes: Both n and span_times can be NULL. 63284a658b32SHong Zhang 63294a658b32SHong Zhang .seealso: TSSetTimeSpan(),TSGetSolutions() 63304a658b32SHong Zhang @*/ 63314a658b32SHong Zhang PetscErrorCode TSGetTimeSpan(TS ts,PetscInt *n,const PetscReal **span_times) 63324a658b32SHong Zhang { 63334a658b32SHong Zhang PetscFunctionBegin; 63344a658b32SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 63354a658b32SHong Zhang if (n) PetscValidIntPointer(n,2); 63364a658b32SHong Zhang if (span_times) PetscValidPointer(span_times,3); 63374a658b32SHong Zhang if (!ts->tspan) { 63384a658b32SHong Zhang if (n) *n = 0; 63394a658b32SHong Zhang if (span_times) *span_times = NULL; 63404a658b32SHong Zhang } else { 63414a658b32SHong Zhang if (n) *n = ts->tspan->num_span_times; 63424a658b32SHong Zhang if (span_times) *span_times = ts->tspan->span_times; 63434a658b32SHong Zhang } 63444a658b32SHong Zhang PetscFunctionReturn(0); 63454a658b32SHong Zhang } 63464a658b32SHong Zhang 63474a658b32SHong Zhang /*@ 63484a658b32SHong Zhang TSGetTimeSpanSolutions - Get the number of solutions and the solutions at the time points specified by the time span. 63494a658b32SHong Zhang 63504a658b32SHong Zhang Input Parameter: 63514a658b32SHong Zhang . ts - the TS context obtained from TSCreate() 63524a658b32SHong Zhang 63534a658b32SHong Zhang Output Parameters: 63544a658b32SHong Zhang + nsol - the number of solutions 63554a658b32SHong Zhang - Sols - the solution vectors 63564a658b32SHong Zhang 63574a658b32SHong Zhang Level: beginner 63584a658b32SHong Zhang 63594a658b32SHong Zhang Notes: Both nsol and Sols can be NULL. 63604a658b32SHong Zhang 63614a658b32SHong Zhang .seealso: TSSetTimeSpan() 63624a658b32SHong Zhang @*/ 63634a658b32SHong Zhang PetscErrorCode TSGetTimeSpanSolutions(TS ts,PetscInt *nsol,Vec **Sols) 63644a658b32SHong Zhang { 63654a658b32SHong Zhang PetscFunctionBegin; 63664a658b32SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 63674a658b32SHong Zhang if (nsol) PetscValidIntPointer(nsol,2); 63684a658b32SHong Zhang if (Sols) PetscValidPointer(Sols,3); 63694a658b32SHong Zhang if (!ts->tspan) { 63704a658b32SHong Zhang if (nsol) *nsol = 0; 63714a658b32SHong Zhang if (Sols) *Sols = NULL; 63724a658b32SHong Zhang } else { 63734a658b32SHong Zhang if (nsol) *nsol = ts->tspan->num_span_times; 63744a658b32SHong Zhang if (Sols) *Sols = ts->tspan->vecs_sol; 63754a658b32SHong Zhang } 63764a658b32SHong Zhang PetscFunctionReturn(0); 63774a658b32SHong Zhang } 6378