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 PetscErrorCode ierr; 192ffb9264SLisandro Dalcin 202ffb9264SLisandro Dalcin PetscFunctionBegin; 21b92453a8SLisandro Dalcin PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1); 22b92453a8SLisandro Dalcin PetscValidCharPointer(default_type,2); 232ffb9264SLisandro Dalcin if (!((PetscObject)adapt)->type_name) { 242ffb9264SLisandro Dalcin ierr = TSAdaptSetType(adapt,default_type);CHKERRQ(ierr); 252ffb9264SLisandro Dalcin } 262ffb9264SLisandro Dalcin PetscFunctionReturn(0); 272ffb9264SLisandro Dalcin } 282ffb9264SLisandro Dalcin 29bdad233fSMatthew Knepley /*@ 30bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 31bdad233fSMatthew Knepley 32bdad233fSMatthew Knepley Collective on TS 33bdad233fSMatthew Knepley 34bdad233fSMatthew Knepley Input Parameter: 35bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 36bdad233fSMatthew Knepley 37bdad233fSMatthew Knepley Options Database Keys: 38e49d4f37SHong Zhang + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE, TSBSYMP 39ef222394SBarry Smith . -ts_save_trajectory - checkpoint the solution at each time-step 40ef85077eSLisandro Dalcin . -ts_max_time <time> - maximum time to compute to 41ef85077eSLisandro Dalcin . -ts_max_steps <steps> - maximum number of time-steps to take 42ef85077eSLisandro Dalcin . -ts_init_time <time> - initial time to start computation 434dc72f7fSBarry Smith . -ts_final_time <time> - final time to compute to (deprecated: use -ts_max_time) 443e4cdcaaSBarry Smith . -ts_dt <dt> - initial time step 451628793fSSatish 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 46a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 47a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 48a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 49a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 50a3cdaa26SBarry Smith . -ts_atol <atol> Absolute tolerance for local truncation error 51f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function 52f3b1f45cSBarry 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 53ef222394SBarry Smith . -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory) 54847ff0e1SMatthew G. Knepley . -ts_fd_color - Use finite differences with coloring to compute IJacobian 55bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 56aee7a9fbSMatthew G. Knepley . -ts_monitor_cancel - Cancel all monitors 57de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 58de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 597cf37e64SBarry Smith . -ts_monitor_error - Monitors norm of error 606934998bSLisandro Dalcin . -ts_monitor_lg_timestep - Monitor timestep size graphically 618b668821SLisandro Dalcin . -ts_monitor_lg_timestep_log - Monitor log timestep size graphically 62de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 63de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 64de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 65de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 663e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 673e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 68fde5950dSBarry Smith . -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep 69e4160dc7SJulian Andrej . -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu) 709e336e28SPatrick Sanan - -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 7153ea634cSHong Zhang 723d5a8a6aSBarry Smith Notes: 733d5a8a6aSBarry Smith See SNESSetFromOptions() and KSPSetFromOptions() for how to control the nonlinear and linear solves used by the time-stepper. 743d5a8a6aSBarry Smith 753d5a8a6aSBarry Smith Certain SNES options get reset for each new nonlinear solver, for example -snes_lag_jacobian <its> and -snes_lag_preconditioner <its>, in order 763d5a8a6aSBarry Smith to retain them over the multiple nonlinear solves that TS uses you mush also provide -snes_lag_jacobian_persists true and 773d5a8a6aSBarry Smith -snes_lag_preconditioner_persists true 783d5a8a6aSBarry Smith 799e336e28SPatrick Sanan Developer Note: 809e336e28SPatrick Sanan We should unify all the -ts_monitor options in the way that -xxx_view has been unified 81bdad233fSMatthew Knepley 82bdad233fSMatthew Knepley Level: beginner 83bdad233fSMatthew Knepley 84a313700dSBarry Smith .seealso: TSGetType() 85bdad233fSMatthew Knepley @*/ 867087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 87bdad233fSMatthew Knepley { 88bc952696SBarry Smith PetscBool opt,flg,tflg; 89dfbe8321SBarry Smith PetscErrorCode ierr; 90eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 9131748224SBarry Smith PetscReal time_step; 9249354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 93d1212d36SBarry Smith char dir[16]; 94cd11d68dSLisandro Dalcin TSIFunction ifun; 956991f827SBarry Smith const char *defaultType; 966991f827SBarry Smith char typeName[256]; 97bdad233fSMatthew Knepley 98bdad233fSMatthew Knepley PetscFunctionBegin; 990700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1006991f827SBarry Smith 1016991f827SBarry Smith ierr = TSRegisterAll();CHKERRQ(ierr); 102cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 103cd11d68dSLisandro Dalcin 104cd11d68dSLisandro Dalcin ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 1051ef27442SStefano Zampini if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 1061ef27442SStefano Zampini else defaultType = ifun ? TSBEULER : TSEULER; 1076991f827SBarry Smith ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr); 1086991f827SBarry Smith if (opt) { 1096991f827SBarry Smith ierr = TSSetType(ts,typeName);CHKERRQ(ierr); 1106991f827SBarry Smith } else { 1116991f827SBarry Smith ierr = TSSetType(ts,defaultType);CHKERRQ(ierr); 1126991f827SBarry Smith } 113bdad233fSMatthew Knepley 114bdad233fSMatthew Knepley /* Handle generic TS options */ 1154dc72f7fSBarry Smith ierr = PetscOptionsDeprecated("-ts_final_time","-ts_max_time","3.10",NULL);CHKERRQ(ierr); 116ef85077eSLisandro Dalcin ierr = PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 11719eac22cSLisandro Dalcin ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 1180298fd71SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr); 11931748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 120cd11d68dSLisandro Dalcin if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);} 12149354f04SShri Abhyankar ierr = PetscOptionsEnum("-ts_exact_final_time","Option for handling of final time step","TSSetExactFinalTime",TSExactFinalTimeOptions,(PetscEnum)ts->exact_final_time,(PetscEnum*)&eftopt,&flg);CHKERRQ(ierr); 12249354f04SShri Abhyankar if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 1230298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,NULL);CHKERRQ(ierr); 1240298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr); 1250298fd71SBarry Smith ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr); 1260298fd71SBarry Smith ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr); 1270298fd71SBarry Smith ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr); 128bdad233fSMatthew Knepley 129f3b1f45cSBarry Smith ierr = PetscOptionsBool("-ts_rhs_jacobian_test_mult","Test the RHS Jacobian for consistency with RHS at each solve ","None",ts->testjacobian,&ts->testjacobian,NULL);CHKERRQ(ierr); 130f3b1f45cSBarry Smith ierr = 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);CHKERRQ(ierr); 1310fe4d17eSHong Zhang ierr = PetscOptionsBool("-ts_use_splitrhsfunction","Use the split RHS function for multirate solvers ","TSSetUseSplitRHSFunction",ts->use_splitrhsfunction,&ts->use_splitrhsfunction,NULL);CHKERRQ(ierr); 13256f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 13356f85f32SBarry Smith { 13456f85f32SBarry Smith PetscBool set; 13556f85f32SBarry Smith flg = PETSC_FALSE; 13656f85f32SBarry Smith ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr); 13756f85f32SBarry Smith if (set) { 13856f85f32SBarry Smith ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr); 13956f85f32SBarry Smith } 14056f85f32SBarry Smith } 14156f85f32SBarry Smith #endif 14256f85f32SBarry Smith 143bdad233fSMatthew Knepley /* Monitor options */ 144d0c080abSJoseph Pusztay ierr = PetscOptionsInt("-ts_monitor_frequency", "Number of time steps between monitor output", "TSMonitorSetFrequency", ts->monitorFrequency, &ts->monitorFrequency, NULL);CHKERRQ(ierr); 145cd11d68dSLisandro Dalcin ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr); 146cc9c3a59SBarry Smith ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_extreme","Monitor extreme values of the solution","TSMonitorExtreme",TSMonitorExtreme,NULL);CHKERRQ(ierr); 147fde5950dSBarry Smith ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr); 148d0c080abSJoseph Pusztay ierr = TSMonitorSetFromOptions(ts,"-ts_dmswarm_monitor_moments","Monitor moments of particle distribution","TSDMSwarmMonitorMoments",TSDMSwarmMonitorMoments,NULL);CHKERRQ(ierr); 149fde5950dSBarry Smith 150589a23caSBarry Smith ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",NULL,monfilename,sizeof(monfilename),&flg);CHKERRQ(ierr); 1515180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 1525180491cSLisandro Dalcin 1534f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 154b3603a34SBarry Smith if (opt) { 1553923b477SBarry Smith PetscInt howoften = 1; 156e669de00SBarry Smith DM dm; 157e669de00SBarry Smith PetscBool net; 158b3603a34SBarry Smith 1590298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 160e669de00SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 161e669de00SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)dm,DMNETWORK,&net);CHKERRQ(ierr); 162e669de00SBarry Smith if (net) { 163e669de00SBarry Smith TSMonitorLGCtxNetwork ctx; 164e669de00SBarry Smith ierr = TSMonitorLGCtxNetworkCreate(ts,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 165e669de00SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGCtxNetworkSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxNetworkDestroy);CHKERRQ(ierr); 166e669de00SBarry Smith ierr = PetscOptionsBool("-ts_monitor_lg_solution_semilogy","Plot the solution with a semi-log axis","",ctx->semilogy,&ctx->semilogy,NULL);CHKERRQ(ierr); 167e669de00SBarry Smith } else { 168e669de00SBarry Smith TSMonitorLGCtx ctx; 169c793f718SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 1704f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 171bdad233fSMatthew Knepley } 172e669de00SBarry Smith } 1736ba87a44SLisandro Dalcin 1744f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 175ef20d060SBarry Smith if (opt) { 1760b039ecaSBarry Smith TSMonitorLGCtx ctx; 1773923b477SBarry Smith PetscInt howoften = 1; 178ef20d060SBarry Smith 1790298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr); 180c793f718SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 1814f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 182ef20d060SBarry Smith } 183edbaebb3SBarry Smith ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_error","View the error at each timestep","TSMonitorError",TSMonitorError,NULL);CHKERRQ(ierr); 1847cf37e64SBarry Smith 1856934998bSLisandro Dalcin ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 1866934998bSLisandro Dalcin if (opt) { 1876934998bSLisandro Dalcin TSMonitorLGCtx ctx; 1886934998bSLisandro Dalcin PetscInt howoften = 1; 1896934998bSLisandro Dalcin 1906934998bSLisandro Dalcin ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 1916ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 1926934998bSLisandro Dalcin ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 1936934998bSLisandro Dalcin } 1948b668821SLisandro Dalcin ierr = PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 1958b668821SLisandro Dalcin if (opt) { 1968b668821SLisandro Dalcin TSMonitorLGCtx ctx; 1978b668821SLisandro Dalcin PetscInt howoften = 1; 1988b668821SLisandro Dalcin 1998b668821SLisandro Dalcin ierr = PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 2008b668821SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 2018b668821SLisandro Dalcin ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 2028b668821SLisandro Dalcin ctx->semilogy = PETSC_TRUE; 2038b668821SLisandro Dalcin } 2048b668821SLisandro Dalcin 205201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 206201da799SBarry Smith if (opt) { 207201da799SBarry Smith TSMonitorLGCtx ctx; 208201da799SBarry Smith PetscInt howoften = 1; 209201da799SBarry Smith 2100298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 2116ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 212201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 213201da799SBarry Smith } 214201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 215201da799SBarry Smith if (opt) { 216201da799SBarry Smith TSMonitorLGCtx ctx; 217201da799SBarry Smith PetscInt howoften = 1; 218201da799SBarry Smith 2190298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 2206ba87a44SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr); 221201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 222201da799SBarry Smith } 2238189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 2248189c53fSBarry Smith if (opt) { 2258189c53fSBarry Smith TSMonitorSPEigCtx ctx; 2268189c53fSBarry Smith PetscInt howoften = 1; 2278189c53fSBarry Smith 2280298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr); 229c793f718SLisandro Dalcin ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 2308189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 2318189c53fSBarry Smith } 2320ec8ee2bSJoseph Pusztay ierr = PetscOptionsName("-ts_monitor_sp_swarm","Display particle phase from the DMSwarm","TSMonitorSPSwarm",&opt);CHKERRQ(ierr); 2331b575b74SJoseph Pusztay if (opt) { 2341b575b74SJoseph Pusztay TSMonitorSPCtx ctx; 2351b575b74SJoseph Pusztay PetscInt howoften = 1; 2360ec8ee2bSJoseph Pusztay ierr = PetscOptionsInt("-ts_monitor_sp_swarm","Display particles phase from the DMSwarm","TSMonitorSPSwarm",howoften,&howoften,NULL);CHKERRQ(ierr); 2371b575b74SJoseph Pusztay ierr = TSMonitorSPCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx);CHKERRQ(ierr); 2380ec8ee2bSJoseph Pusztay ierr = TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode (*)(void**))TSMonitorSPCtxDestroy);CHKERRQ(ierr); 2391b575b74SJoseph Pusztay } 240ef20d060SBarry Smith opt = PETSC_FALSE; 2410dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 242a7cc72afSBarry Smith if (opt) { 24383a4ac43SBarry Smith TSMonitorDrawCtx ctx; 24483a4ac43SBarry Smith PetscInt howoften = 1; 245a80ad3e0SBarry Smith 2460298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 247c793f718SLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 24883a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 249bdad233fSMatthew Knepley } 250fb1732b5SBarry Smith opt = PETSC_FALSE; 2512d5ee99bSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr); 2522d5ee99bSBarry Smith if (opt) { 2532d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2542d5ee99bSBarry Smith PetscReal bounds[4]; 2552d5ee99bSBarry Smith PetscInt n = 4; 2562d5ee99bSBarry Smith PetscDraw draw; 2576934998bSLisandro Dalcin PetscDrawAxis axis; 2582d5ee99bSBarry Smith 2592d5ee99bSBarry Smith ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr); 2602d5ee99bSBarry Smith if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 261c793f718SLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr); 2622d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr); 2636934998bSLisandro Dalcin ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr); 2646934998bSLisandro Dalcin ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr); 2656934998bSLisandro Dalcin ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr); 2662d5ee99bSBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2672d5ee99bSBarry Smith } 2682d5ee99bSBarry Smith opt = PETSC_FALSE; 2690dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 2703a471f94SBarry Smith if (opt) { 27183a4ac43SBarry Smith TSMonitorDrawCtx ctx; 27283a4ac43SBarry Smith PetscInt howoften = 1; 2733a471f94SBarry Smith 2740298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr); 275c793f718SLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 27683a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2773a471f94SBarry Smith } 2780ed3bfb6SBarry Smith opt = PETSC_FALSE; 2790ed3bfb6SBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt);CHKERRQ(ierr); 2800ed3bfb6SBarry Smith if (opt) { 2810ed3bfb6SBarry Smith TSMonitorDrawCtx ctx; 2820ed3bfb6SBarry Smith PetscInt howoften = 1; 2830ed3bfb6SBarry Smith 2840ed3bfb6SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL);CHKERRQ(ierr); 285c793f718SLisandro Dalcin ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),NULL,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 2860ed3bfb6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2870ed3bfb6SBarry Smith } 288fde5950dSBarry Smith 289ed81e22dSJed Brown opt = PETSC_FALSE; 290589a23caSBarry Smith ierr = PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",NULL,monfilename,sizeof(monfilename),&flg);CHKERRQ(ierr); 291ed81e22dSJed Brown if (flg) { 292ed81e22dSJed Brown const char *ptr,*ptr2; 293ed81e22dSJed Brown char *filetemplate; 294ce94432eSBarry Smith if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 295ed81e22dSJed Brown /* Do some cursory validation of the input. */ 296ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 297ce94432eSBarry Smith if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 298ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 299ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 300ce94432eSBarry Smith if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03D.vts"); 301ed81e22dSJed Brown if (ptr2) break; 302ed81e22dSJed Brown } 303ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 304ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 305ed81e22dSJed Brown } 306bdad233fSMatthew Knepley 307589a23caSBarry Smith ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,sizeof(dir),&flg);CHKERRQ(ierr); 308d1212d36SBarry Smith if (flg) { 309d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 310d1212d36SBarry Smith int ray = 0; 3113ee9839eSMatthew G. Knepley DMDirection ddir; 312d1212d36SBarry Smith DM da; 313d1212d36SBarry Smith PetscMPIInt rank; 314d1212d36SBarry Smith 315ce94432eSBarry Smith if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 3163ee9839eSMatthew G. Knepley if (dir[0] == 'x') ddir = DM_X; 3173ee9839eSMatthew G. Knepley else if (dir[0] == 'y') ddir = DM_Y; 318ce94432eSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 319d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 320d1212d36SBarry Smith 3215bd1e576SStefano Zampini ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %d\n",dir[0],ray);CHKERRQ(ierr); 322b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 323d1212d36SBarry Smith ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 324d1212d36SBarry Smith ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 325ffc4695bSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRMPI(ierr); 326d1212d36SBarry Smith if (!rank) { 327c793f718SLisandro Dalcin ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,NULL,NULL,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 328d1212d36SBarry Smith } 32951b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 330d1212d36SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 331d1212d36SBarry Smith } 332589a23caSBarry Smith ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,sizeof(dir),&flg);CHKERRQ(ierr); 33351b4a12fSMatthew G. Knepley if (flg) { 33451b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 33551b4a12fSMatthew G. Knepley int ray = 0; 3363ee9839eSMatthew G. Knepley DMDirection ddir; 33751b4a12fSMatthew G. Knepley DM da; 33851b4a12fSMatthew G. Knepley PetscInt howoften = 1; 339d1212d36SBarry Smith 34051b4a12fSMatthew G. Knepley if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 3413ee9839eSMatthew G. Knepley if (dir[0] == 'x') ddir = DM_X; 3423ee9839eSMatthew G. Knepley else if (dir[0] == 'y') ddir = DM_Y; 34351b4a12fSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 34451b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 3451c3436cfSJed Brown 3465bd1e576SStefano Zampini ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %d\n", dir[0], ray);CHKERRQ(ierr); 347b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 34851b4a12fSMatthew G. Knepley ierr = TSGetDM(ts, &da);CHKERRQ(ierr); 34951b4a12fSMatthew G. Knepley ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr); 350c793f718SLisandro Dalcin ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr); 35151b4a12fSMatthew G. Knepley ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr); 35251b4a12fSMatthew G. Knepley } 353a7a1495cSBarry Smith 354b3d3934dSBarry Smith ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr); 355b3d3934dSBarry Smith if (opt) { 356b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 357b3d3934dSBarry Smith 358b3d3934dSBarry Smith ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr); 359b3d3934dSBarry Smith ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr); 360b3d3934dSBarry Smith } 361aee7a9fbSMatthew G. Knepley flg = PETSC_FALSE; 362aee7a9fbSMatthew G. Knepley ierr = PetscOptionsBool("-ts_monitor_cancel","Remove all monitors","TSMonitorCancel",flg,&flg,&opt);CHKERRQ(ierr); 363aee7a9fbSMatthew G. Knepley if (opt && flg) {ierr = TSMonitorCancel(ts);CHKERRQ(ierr);} 364b3d3934dSBarry Smith 365847ff0e1SMatthew G. Knepley flg = PETSC_FALSE; 366847ff0e1SMatthew G. Knepley ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr); 367847ff0e1SMatthew G. Knepley if (flg) { 368847ff0e1SMatthew G. Knepley DM dm; 369847ff0e1SMatthew G. Knepley DMTS tdm; 370847ff0e1SMatthew G. Knepley 371847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 372847ff0e1SMatthew G. Knepley ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr); 373847ff0e1SMatthew G. Knepley tdm->ijacobianctx = NULL; 374c793f718SLisandro Dalcin ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, NULL);CHKERRQ(ierr); 375847ff0e1SMatthew G. Knepley ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr); 376847ff0e1SMatthew G. Knepley } 377847ff0e1SMatthew G. Knepley 378d763cef2SBarry Smith /* Handle specific TS options */ 379d763cef2SBarry Smith if (ts->ops->setfromoptions) { 3806991f827SBarry Smith ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr); 381d763cef2SBarry Smith } 382fbc52257SHong Zhang 383a7bdc993SLisandro Dalcin /* Handle TSAdapt options */ 384a7bdc993SLisandro Dalcin ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 385a7bdc993SLisandro Dalcin ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr); 386a7bdc993SLisandro Dalcin ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr); 387a7bdc993SLisandro Dalcin 38868bece0bSHong Zhang /* TS trajectory must be set after TS, since it may use some TS options above */ 3894f122a70SLisandro Dalcin tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE; 39068bece0bSHong Zhang ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr); 39168bece0bSHong Zhang if (tflg) { 39268bece0bSHong Zhang ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 39368bece0bSHong Zhang } 394a05bf03eSHong Zhang 395a05bf03eSHong Zhang ierr = TSAdjointSetFromOptions(PetscOptionsObject,ts);CHKERRQ(ierr); 396d763cef2SBarry Smith 397d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 3980633abcbSJed Brown ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr); 399fbc52257SHong Zhang ierr = PetscOptionsEnd();CHKERRQ(ierr); 400d763cef2SBarry Smith 4014f122a70SLisandro Dalcin if (ts->trajectory) { 4024f122a70SLisandro Dalcin ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr); 403d763cef2SBarry Smith } 40468bece0bSHong Zhang 4051ef27442SStefano Zampini /* why do we have to do this here and not during TSSetUp? */ 4064f122a70SLisandro Dalcin ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr); 4071ef27442SStefano Zampini if (ts->problem_type == TS_LINEAR) { 4081ef27442SStefano Zampini ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&flg,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr); 4091ef27442SStefano Zampini if (!flg) { ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); } 4101ef27442SStefano Zampini } 4114f122a70SLisandro Dalcin ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr); 412d763cef2SBarry Smith PetscFunctionReturn(0); 413d763cef2SBarry Smith } 414d763cef2SBarry Smith 415d2daff3dSHong Zhang /*@ 41678fbdcc8SBarry Smith TSGetTrajectory - Gets the trajectory from a TS if it exists 41778fbdcc8SBarry Smith 41878fbdcc8SBarry Smith Collective on TS 41978fbdcc8SBarry Smith 42078fbdcc8SBarry Smith Input Parameters: 42178fbdcc8SBarry Smith . ts - the TS context obtained from TSCreate() 42278fbdcc8SBarry Smith 4237a7aea1fSJed Brown Output Parameters: 42478fbdcc8SBarry Smith . tr - the TSTrajectory object, if it exists 42578fbdcc8SBarry Smith 42678fbdcc8SBarry Smith Note: This routine should be called after all TS options have been set 42778fbdcc8SBarry Smith 42878fbdcc8SBarry Smith Level: advanced 42978fbdcc8SBarry Smith 43078fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate() 43178fbdcc8SBarry Smith 43278fbdcc8SBarry Smith @*/ 43378fbdcc8SBarry Smith PetscErrorCode TSGetTrajectory(TS ts,TSTrajectory *tr) 43478fbdcc8SBarry Smith { 43578fbdcc8SBarry Smith PetscFunctionBegin; 43678fbdcc8SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 43778fbdcc8SBarry Smith *tr = ts->trajectory; 43878fbdcc8SBarry Smith PetscFunctionReturn(0); 43978fbdcc8SBarry Smith } 44078fbdcc8SBarry Smith 44178fbdcc8SBarry Smith /*@ 442bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 443d2daff3dSHong Zhang 444d2daff3dSHong Zhang Collective on TS 445d2daff3dSHong Zhang 446f899ff85SJose E. Roman Input Parameter: 447bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 448bc952696SBarry Smith 44978fbdcc8SBarry Smith Options Database: 45078fbdcc8SBarry Smith + -ts_save_trajectory - saves the trajectory to a file 45178fbdcc8SBarry Smith - -ts_trajectory_type type 45278fbdcc8SBarry Smith 45368bece0bSHong Zhang Note: This routine should be called after all TS options have been set 454d2daff3dSHong Zhang 455c3a89c15SBarry Smith The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and 45678fbdcc8SBarry Smith MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m 45778fbdcc8SBarry Smith 458d2daff3dSHong Zhang Level: intermediate 459d2daff3dSHong Zhang 4602d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve() 461d2daff3dSHong Zhang 462d2daff3dSHong Zhang @*/ 463bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 464d2daff3dSHong Zhang { 465bc952696SBarry Smith PetscErrorCode ierr; 466bc952696SBarry Smith 467d2daff3dSHong Zhang PetscFunctionBegin; 468d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 469bc952696SBarry Smith if (!ts->trajectory) { 470bc952696SBarry Smith ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 471bc952696SBarry Smith } 472d2daff3dSHong Zhang PetscFunctionReturn(0); 473d2daff3dSHong Zhang } 474d2daff3dSHong Zhang 475a7a1495cSBarry Smith /*@ 4762d29f1f2SStefano Zampini TSResetTrajectory - Destroys and recreates the internal TSTrajectory object 4772d29f1f2SStefano Zampini 4782d29f1f2SStefano Zampini Collective on TS 4792d29f1f2SStefano Zampini 4802d29f1f2SStefano Zampini Input Parameters: 4812d29f1f2SStefano Zampini . ts - the TS context obtained from TSCreate() 4822d29f1f2SStefano Zampini 4832d29f1f2SStefano Zampini Level: intermediate 4842d29f1f2SStefano Zampini 4852d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve() 4862d29f1f2SStefano Zampini 4872d29f1f2SStefano Zampini @*/ 4882d29f1f2SStefano Zampini PetscErrorCode TSResetTrajectory(TS ts) 4892d29f1f2SStefano Zampini { 4902d29f1f2SStefano Zampini PetscErrorCode ierr; 4912d29f1f2SStefano Zampini 4922d29f1f2SStefano Zampini PetscFunctionBegin; 4932d29f1f2SStefano Zampini PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4942d29f1f2SStefano Zampini if (ts->trajectory) { 4952d29f1f2SStefano Zampini ierr = TSTrajectoryDestroy(&ts->trajectory);CHKERRQ(ierr); 4962d29f1f2SStefano Zampini ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 4972d29f1f2SStefano Zampini } 4982d29f1f2SStefano Zampini PetscFunctionReturn(0); 4992d29f1f2SStefano Zampini } 5002d29f1f2SStefano Zampini 5012d29f1f2SStefano Zampini /*@ 502a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 503a7a1495cSBarry Smith set with TSSetRHSJacobian(). 504a7a1495cSBarry Smith 505d083f849SBarry Smith Collective on TS 506a7a1495cSBarry Smith 507a7a1495cSBarry Smith Input Parameters: 508316643e7SJed Brown + ts - the TS context 509a7a1495cSBarry Smith . t - current timestep 5100910c330SBarry Smith - U - input vector 511a7a1495cSBarry Smith 512a7a1495cSBarry Smith Output Parameters: 513a7a1495cSBarry Smith + A - Jacobian matrix 5146b867d5aSJose E. Roman - B - optional preconditioning matrix 515a7a1495cSBarry Smith 516a7a1495cSBarry Smith Notes: 517a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 518a7a1495cSBarry Smith is used internally within the nonlinear solvers. 519a7a1495cSBarry Smith 520a7a1495cSBarry Smith Level: developer 521a7a1495cSBarry Smith 52294b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 523a7a1495cSBarry Smith @*/ 524d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 525a7a1495cSBarry Smith { 526dfbe8321SBarry Smith PetscErrorCode ierr; 527270bf2e7SJed Brown PetscObjectState Ustate; 5286c1e1eecSBarry Smith PetscObjectId Uid; 52924989b8cSPeter Brune DM dm; 530942e3340SBarry Smith DMTS tsdm; 53124989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 53224989b8cSPeter Brune void *ctx; 533b2df71adSDebojyoti Ghosh TSRHSFunction rhsfunction; 534a7a1495cSBarry Smith 535a7a1495cSBarry Smith PetscFunctionBegin; 5360700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5370910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5380910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 53924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 540942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 5416374d434SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 5422663174eSHong Zhang ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 54359e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 5446c1e1eecSBarry Smith ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr); 545971015bcSStefano Zampini 5462663174eSHong Zhang if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) PetscFunctionReturn(0); 547d90be118SSean Farley 548ae692cf2SHong Zhang if (ts->rhsjacobian.shift && ts->rhsjacobian.reuse) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Should not call TSComputeRHSJacobian() on a shifted matrix (shift=%lf) when RHSJacobian is reusable.",ts->rhsjacobian.shift); 54924989b8cSPeter Brune if (rhsjacobianfunc) { 55094ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 551a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 552d1e9a80fSBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr); 553a7a1495cSBarry Smith PetscStackPop; 554a6ab3590SBarry Smith ts->rhsjacs++; 55594ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 556ef66eb69SBarry Smith } else { 55794ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 558971015bcSStefano Zampini if (B && A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 559ef66eb69SBarry Smith } 5600e4ef248SJed Brown ts->rhsjacobian.time = t; 561971015bcSStefano Zampini ts->rhsjacobian.shift = 0; 562971015bcSStefano Zampini ts->rhsjacobian.scale = 1.; 5637f79407eSBarry Smith ierr = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr); 56459e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 565a7a1495cSBarry Smith PetscFunctionReturn(0); 566a7a1495cSBarry Smith } 567a7a1495cSBarry Smith 568316643e7SJed Brown /*@ 569d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 570d763cef2SBarry Smith 571d083f849SBarry Smith Collective on TS 572316643e7SJed Brown 573316643e7SJed Brown Input Parameters: 574316643e7SJed Brown + ts - the TS context 575316643e7SJed Brown . t - current time 5760910c330SBarry Smith - U - state vector 577316643e7SJed Brown 578316643e7SJed Brown Output Parameter: 579316643e7SJed Brown . y - right hand side 580316643e7SJed Brown 581316643e7SJed Brown Note: 582316643e7SJed Brown Most users should not need to explicitly call this routine, as it 583316643e7SJed Brown is used internally within the nonlinear solvers. 584316643e7SJed Brown 585316643e7SJed Brown Level: developer 586316643e7SJed Brown 587316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 588316643e7SJed Brown @*/ 5890910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 590d763cef2SBarry Smith { 591dfbe8321SBarry Smith PetscErrorCode ierr; 59224989b8cSPeter Brune TSRHSFunction rhsfunction; 59324989b8cSPeter Brune TSIFunction ifunction; 59424989b8cSPeter Brune void *ctx; 59524989b8cSPeter Brune DM dm; 59624989b8cSPeter Brune 597d763cef2SBarry Smith PetscFunctionBegin; 5980700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5990910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6000700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 60124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 60224989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 6030298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 604d763cef2SBarry Smith 605ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 606d763cef2SBarry Smith 60724989b8cSPeter Brune if (rhsfunction) { 608a6ab3590SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 6091be370b1SHong Zhang ierr = VecLockReadPush(U);CHKERRQ(ierr); 610d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 6110910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 612d763cef2SBarry Smith PetscStackPop; 6131be370b1SHong Zhang ierr = VecLockReadPop(U);CHKERRQ(ierr); 614a6ab3590SBarry Smith ts->rhsfuncs++; 615a6ab3590SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 616214bc6a2SJed Brown } else { 617214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 618b2cd27e8SJed Brown } 619d763cef2SBarry Smith PetscFunctionReturn(0); 620d763cef2SBarry Smith } 621d763cef2SBarry Smith 622ef20d060SBarry Smith /*@ 623ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 624ef20d060SBarry Smith 625d083f849SBarry Smith Collective on TS 626ef20d060SBarry Smith 627ef20d060SBarry Smith Input Parameters: 628ef20d060SBarry Smith + ts - the TS context 629ef20d060SBarry Smith - t - current time 630ef20d060SBarry Smith 631ef20d060SBarry Smith Output Parameter: 6320910c330SBarry Smith . U - the solution 633ef20d060SBarry Smith 634ef20d060SBarry Smith Note: 635ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 636ef20d060SBarry Smith is used internally within the nonlinear solvers. 637ef20d060SBarry Smith 638ef20d060SBarry Smith Level: developer 639ef20d060SBarry Smith 640abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 641ef20d060SBarry Smith @*/ 6420910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 643ef20d060SBarry Smith { 644ef20d060SBarry Smith PetscErrorCode ierr; 645ef20d060SBarry Smith TSSolutionFunction solutionfunction; 646ef20d060SBarry Smith void *ctx; 647ef20d060SBarry Smith DM dm; 648ef20d060SBarry Smith 649ef20d060SBarry Smith PetscFunctionBegin; 650ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6510910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 652ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 653ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 654ef20d060SBarry Smith 655ef20d060SBarry Smith if (solutionfunction) { 6569b7cd975SBarry Smith PetscStackPush("TS user solution function"); 6570910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 658ef20d060SBarry Smith PetscStackPop; 659ef20d060SBarry Smith } 660ef20d060SBarry Smith PetscFunctionReturn(0); 661ef20d060SBarry Smith } 6629b7cd975SBarry Smith /*@ 6639b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 6649b7cd975SBarry Smith 665d083f849SBarry Smith Collective on TS 6669b7cd975SBarry Smith 6679b7cd975SBarry Smith Input Parameters: 6689b7cd975SBarry Smith + ts - the TS context 6699b7cd975SBarry Smith - t - current time 6709b7cd975SBarry Smith 6719b7cd975SBarry Smith Output Parameter: 6729b7cd975SBarry Smith . U - the function value 6739b7cd975SBarry Smith 6749b7cd975SBarry Smith Note: 6759b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 6769b7cd975SBarry Smith is used internally within the nonlinear solvers. 6779b7cd975SBarry Smith 6789b7cd975SBarry Smith Level: developer 6799b7cd975SBarry Smith 6809b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 6819b7cd975SBarry Smith @*/ 6829b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 6839b7cd975SBarry Smith { 6849b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 6859b7cd975SBarry Smith void *ctx; 6869b7cd975SBarry Smith DM dm; 6879b7cd975SBarry Smith 6889b7cd975SBarry Smith PetscFunctionBegin; 6899b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6909b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6919b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 6929b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 6939b7cd975SBarry Smith 6949b7cd975SBarry Smith if (forcing) { 6959b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 6969b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 6979b7cd975SBarry Smith PetscStackPop; 6989b7cd975SBarry Smith } 6999b7cd975SBarry Smith PetscFunctionReturn(0); 7009b7cd975SBarry Smith } 701ef20d060SBarry Smith 702214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 703214bc6a2SJed Brown { 7042dd45cf8SJed Brown Vec F; 705214bc6a2SJed Brown PetscErrorCode ierr; 706214bc6a2SJed Brown 707214bc6a2SJed Brown PetscFunctionBegin; 7080298fd71SBarry Smith *Frhs = NULL; 7090298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 710214bc6a2SJed Brown if (!ts->Frhs) { 7112dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 712214bc6a2SJed Brown } 713214bc6a2SJed Brown *Frhs = ts->Frhs; 714214bc6a2SJed Brown PetscFunctionReturn(0); 715214bc6a2SJed Brown } 716214bc6a2SJed Brown 717971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 718214bc6a2SJed Brown { 719214bc6a2SJed Brown Mat A,B; 7202dd45cf8SJed Brown PetscErrorCode ierr; 72141a1d4d2SBarry Smith TSIJacobian ijacobian; 722214bc6a2SJed Brown 723214bc6a2SJed Brown PetscFunctionBegin; 724c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 725c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 72641a1d4d2SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr); 727214bc6a2SJed Brown if (Arhs) { 728214bc6a2SJed Brown if (!ts->Arhs) { 72941a1d4d2SBarry Smith if (ijacobian) { 730214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 731d60b7d5cSBarry Smith ierr = TSSetMatStructure(ts,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 73241a1d4d2SBarry Smith } else { 73341a1d4d2SBarry Smith ts->Arhs = A; 73441a1d4d2SBarry Smith ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 73541a1d4d2SBarry Smith } 7363565c898SBarry Smith } else { 7373565c898SBarry Smith PetscBool flg; 7383565c898SBarry Smith ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr); 7393565c898SBarry Smith /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */ 7403565c898SBarry Smith if (flg && !ijacobian && ts->Arhs == ts->Brhs) { 7413565c898SBarry Smith ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr); 7423565c898SBarry Smith ts->Arhs = A; 7433565c898SBarry Smith ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 7443565c898SBarry Smith } 745214bc6a2SJed Brown } 746214bc6a2SJed Brown *Arhs = ts->Arhs; 747214bc6a2SJed Brown } 748214bc6a2SJed Brown if (Brhs) { 749214bc6a2SJed Brown if (!ts->Brhs) { 750bdb70873SJed Brown if (A != B) { 75141a1d4d2SBarry Smith if (ijacobian) { 752214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 753bdb70873SJed Brown } else { 75441a1d4d2SBarry Smith ts->Brhs = B; 75541a1d4d2SBarry Smith ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 75641a1d4d2SBarry Smith } 75741a1d4d2SBarry Smith } else { 758bdb70873SJed Brown ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 75951699248SLisandro Dalcin ts->Brhs = ts->Arhs; 760bdb70873SJed Brown } 761214bc6a2SJed Brown } 762214bc6a2SJed Brown *Brhs = ts->Brhs; 763214bc6a2SJed Brown } 764214bc6a2SJed Brown PetscFunctionReturn(0); 765214bc6a2SJed Brown } 766214bc6a2SJed Brown 767316643e7SJed Brown /*@ 7680910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 769316643e7SJed Brown 770d083f849SBarry Smith Collective on TS 771316643e7SJed Brown 772316643e7SJed Brown Input Parameters: 773316643e7SJed Brown + ts - the TS context 774316643e7SJed Brown . t - current time 7750910c330SBarry Smith . U - state vector 7760910c330SBarry Smith . Udot - time derivative of state vector 777214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 778316643e7SJed Brown 779316643e7SJed Brown Output Parameter: 780316643e7SJed Brown . Y - right hand side 781316643e7SJed Brown 782316643e7SJed Brown Note: 783316643e7SJed Brown Most users should not need to explicitly call this routine, as it 784316643e7SJed Brown is used internally within the nonlinear solvers. 785316643e7SJed Brown 786316643e7SJed Brown If the user did did not write their equations in implicit form, this 787316643e7SJed Brown function recasts them in implicit form. 788316643e7SJed Brown 789316643e7SJed Brown Level: developer 790316643e7SJed Brown 791316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 792316643e7SJed Brown @*/ 7930910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 794316643e7SJed Brown { 795316643e7SJed Brown PetscErrorCode ierr; 79624989b8cSPeter Brune TSIFunction ifunction; 79724989b8cSPeter Brune TSRHSFunction rhsfunction; 79824989b8cSPeter Brune void *ctx; 79924989b8cSPeter Brune DM dm; 800316643e7SJed Brown 801316643e7SJed Brown PetscFunctionBegin; 8020700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8030910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8040910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 8050700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 806316643e7SJed Brown 80724989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 80824989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 8090298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 81024989b8cSPeter Brune 811ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 812d90be118SSean Farley 8130910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 81424989b8cSPeter Brune if (ifunction) { 815316643e7SJed Brown PetscStackPush("TS user implicit function"); 8160910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 817316643e7SJed Brown PetscStackPop; 818a6ab3590SBarry Smith ts->ifuncs++; 819214bc6a2SJed Brown } 820214bc6a2SJed Brown if (imex) { 82124989b8cSPeter Brune if (!ifunction) { 8220910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 8232dd45cf8SJed Brown } 82424989b8cSPeter Brune } else if (rhsfunction) { 82524989b8cSPeter Brune if (ifunction) { 826214bc6a2SJed Brown Vec Frhs; 827214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 8280910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 829214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 8302dd45cf8SJed Brown } else { 8310910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 8320910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 833316643e7SJed Brown } 8344a6899ffSJed Brown } 8350910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 836316643e7SJed Brown PetscFunctionReturn(0); 837316643e7SJed Brown } 838316643e7SJed Brown 839cfa8a9a2SHong Zhang /* 840cfa8a9a2SHong Zhang TSRecoverRHSJacobian - Recover the Jacobian matrix so that one can call TSComputeRHSJacobian() on it. 841cfa8a9a2SHong Zhang 842cfa8a9a2SHong Zhang Note: 843cfa8a9a2SHong Zhang This routine is needed when one switches from TSComputeIJacobian() to TSComputeRHSJacobian() because the Jacobian matrix may be shifted or scaled in TSComputeIJacobian(). 844cfa8a9a2SHong Zhang 845cfa8a9a2SHong Zhang */ 846cfa8a9a2SHong Zhang static PetscErrorCode TSRecoverRHSJacobian(TS ts,Mat A,Mat B) 847cfa8a9a2SHong Zhang { 848cfa8a9a2SHong Zhang PetscErrorCode ierr; 849cfa8a9a2SHong Zhang 850cfa8a9a2SHong Zhang PetscFunctionBegin; 851cfa8a9a2SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 85203c97d52SHong Zhang if (A != ts->Arhs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Invalid Amat"); 85303c97d52SHong Zhang if (B != ts->Brhs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Invalid Bmat"); 854cfa8a9a2SHong Zhang 855cfa8a9a2SHong Zhang if (ts->rhsjacobian.shift) { 856cfa8a9a2SHong Zhang ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 857cfa8a9a2SHong Zhang } 858cfa8a9a2SHong Zhang if (ts->rhsjacobian.scale == -1.) { 859cfa8a9a2SHong Zhang ierr = MatScale(A,-1);CHKERRQ(ierr); 860cfa8a9a2SHong Zhang } 861cfa8a9a2SHong Zhang if (B && B == ts->Brhs && A != B) { 862cfa8a9a2SHong Zhang if (ts->rhsjacobian.shift) { 863cfa8a9a2SHong Zhang ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 864cfa8a9a2SHong Zhang } 865cfa8a9a2SHong Zhang if (ts->rhsjacobian.scale == -1.) { 866cfa8a9a2SHong Zhang ierr = MatScale(B,-1);CHKERRQ(ierr); 867cfa8a9a2SHong Zhang } 868cfa8a9a2SHong Zhang } 869cfa8a9a2SHong Zhang ts->rhsjacobian.shift = 0; 870cfa8a9a2SHong Zhang ts->rhsjacobian.scale = 1.; 871cfa8a9a2SHong Zhang PetscFunctionReturn(0); 872cfa8a9a2SHong Zhang } 873cfa8a9a2SHong Zhang 874316643e7SJed Brown /*@ 875316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 876316643e7SJed Brown 877d083f849SBarry Smith Collective on TS 878316643e7SJed Brown 879316643e7SJed Brown Input 880316643e7SJed Brown Input Parameters: 881316643e7SJed Brown + ts - the TS context 882316643e7SJed Brown . t - current timestep 8830910c330SBarry Smith . U - state vector 8840910c330SBarry Smith . Udot - time derivative of state vector 885214bc6a2SJed Brown . shift - shift to apply, see note below 886214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 887316643e7SJed Brown 888316643e7SJed Brown Output Parameters: 889316643e7SJed Brown + A - Jacobian matrix 8903565c898SBarry Smith - B - matrix from which the preconditioner is constructed; often the same as A 891316643e7SJed Brown 892316643e7SJed Brown Notes: 8930910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 894316643e7SJed Brown 8950910c330SBarry Smith dF/dU + shift*dF/dUdot 896316643e7SJed Brown 897316643e7SJed Brown Most users should not need to explicitly call this routine, as it 898316643e7SJed Brown is used internally within the nonlinear solvers. 899316643e7SJed Brown 900316643e7SJed Brown Level: developer 901316643e7SJed Brown 902316643e7SJed Brown .seealso: TSSetIJacobian() 90363495f91SJed Brown @*/ 904d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 905316643e7SJed Brown { 906316643e7SJed Brown PetscErrorCode ierr; 90724989b8cSPeter Brune TSIJacobian ijacobian; 90824989b8cSPeter Brune TSRHSJacobian rhsjacobian; 90924989b8cSPeter Brune DM dm; 91024989b8cSPeter Brune void *ctx; 911316643e7SJed Brown 912316643e7SJed Brown PetscFunctionBegin; 9130700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 9140910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 9150910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 916316643e7SJed Brown PetscValidPointer(A,6); 91794ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 918316643e7SJed Brown PetscValidPointer(B,7); 91994ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 92024989b8cSPeter Brune 92124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 92224989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 9230298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 92424989b8cSPeter Brune 925ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 926316643e7SJed Brown 92794ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 92824989b8cSPeter Brune if (ijacobian) { 929316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 930d1e9a80fSBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr); 931a6ab3590SBarry Smith ts->ijacs++; 932316643e7SJed Brown PetscStackPop; 9334a6899ffSJed Brown } 934214bc6a2SJed Brown if (imex) { 935b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 9364c26be97Sstefano_zampini PetscBool assembled; 937971015bcSStefano Zampini if (rhsjacobian) { 938971015bcSStefano Zampini Mat Arhs = NULL; 939971015bcSStefano Zampini ierr = TSGetRHSMats_Private(ts,&Arhs,NULL);CHKERRQ(ierr); 940971015bcSStefano Zampini if (A == Arhs) { 941e8b1e424SHong Zhang if (rhsjacobian == TSComputeRHSJacobianConstant) SETERRQ(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 */ 942971015bcSStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 943971015bcSStefano Zampini } 944971015bcSStefano Zampini } 94594ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 9464c26be97Sstefano_zampini ierr = MatAssembled(A,&assembled);CHKERRQ(ierr); 9474c26be97Sstefano_zampini if (!assembled) { 9484c26be97Sstefano_zampini ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9494c26be97Sstefano_zampini ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9504c26be97Sstefano_zampini } 95194ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 95294ab13aaSBarry Smith if (A != B) { 95394ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 9544c26be97Sstefano_zampini ierr = MatAssembled(B,&assembled);CHKERRQ(ierr); 9554c26be97Sstefano_zampini if (!assembled) { 9564c26be97Sstefano_zampini ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9574c26be97Sstefano_zampini ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9584c26be97Sstefano_zampini } 95994ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 960214bc6a2SJed Brown } 961214bc6a2SJed Brown } 962214bc6a2SJed Brown } else { 963e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 964e8b1e424SHong Zhang if (rhsjacobian) { /* RHSJacobian needs to be converted to part of IJacobian if exists */ 965214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 966e1244c69SJed Brown } 967e8b1e424SHong Zhang if (Arhs == A) { /* No IJacobian matrix, so we only have the RHS matrix */ 968e8b1e424SHong Zhang PetscObjectState Ustate; 969e8b1e424SHong Zhang PetscObjectId Uid; 970e8b1e424SHong Zhang TSRHSFunction rhsfunction; 971e8b1e424SHong Zhang 972e8b1e424SHong Zhang ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 973e8b1e424SHong Zhang ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 974e8b1e424SHong Zhang ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr); 975097a96a2SHong 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 */ 976e8b1e424SHong Zhang ierr = MatShift(A,shift-ts->rhsjacobian.shift);CHKERRQ(ierr); /* revert the old shift and add the new shift with a single call to MatShift */ 97734d322a1SHong Zhang if (A != B) { 97834d322a1SHong Zhang ierr = MatShift(B,shift-ts->rhsjacobian.shift);CHKERRQ(ierr); 97934d322a1SHong Zhang } 980e8b1e424SHong Zhang } else { 9813565c898SBarry Smith PetscBool flg; 982e8b1e424SHong Zhang 983e8b1e424SHong Zhang if (ts->rhsjacobian.reuse) { /* Undo the damage */ 984e8b1e424SHong Zhang /* MatScale has a short path for this case. 985e8b1e424SHong Zhang However, this code path is taken the first time TSComputeRHSJacobian is called 986e8b1e424SHong Zhang and the matrices have not been assembled yet */ 987cfa8a9a2SHong Zhang ierr = TSRecoverRHSJacobian(ts,A,B);CHKERRQ(ierr); 988e8b1e424SHong Zhang } 989e8b1e424SHong Zhang ierr = TSComputeRHSJacobian(ts,t,U,A,B);CHKERRQ(ierr); 9903565c898SBarry Smith ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr); 9913565c898SBarry Smith /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */ 9923565c898SBarry Smith if (!flg) { 99394ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 99494ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 9953565c898SBarry Smith } 99694ab13aaSBarry Smith if (A != B) { 99794ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 99894ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 999316643e7SJed Brown } 1000e8b1e424SHong Zhang } 1001e8b1e424SHong Zhang ts->rhsjacobian.scale = -1; 1002e8b1e424SHong Zhang ts->rhsjacobian.shift = shift; 1003d60b7d5cSBarry Smith } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 1004e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 100594ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 100694ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 100794ab13aaSBarry Smith if (A != B) { 100894ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 100994ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 1010214bc6a2SJed Brown } 1011316643e7SJed Brown } 1012e8b1e424SHong Zhang ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 1013d60b7d5cSBarry Smith ierr = MatAXPY(A,-1,Arhs,ts->axpy_pattern);CHKERRQ(ierr); 101494ab13aaSBarry Smith if (A != B) { 1015d60b7d5cSBarry Smith ierr = MatAXPY(B,-1,Brhs,ts->axpy_pattern);CHKERRQ(ierr); 1016316643e7SJed Brown } 1017316643e7SJed Brown } 1018316643e7SJed Brown } 101994ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 1020316643e7SJed Brown PetscFunctionReturn(0); 1021316643e7SJed Brown } 1022316643e7SJed Brown 1023d763cef2SBarry Smith /*@C 1024d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 1025b5abc632SBarry Smith where U_t = G(t,u). 1026d763cef2SBarry Smith 10273f9fe445SBarry Smith Logically Collective on TS 1028d763cef2SBarry Smith 1029d763cef2SBarry Smith Input Parameters: 1030d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 10310298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 1032d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 1033d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 10340298fd71SBarry Smith function evaluation routine (may be NULL) 1035d763cef2SBarry Smith 1036a96d6ef6SBarry Smith Calling sequence of f: 1037a96d6ef6SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec F,void *ctx); 1038d763cef2SBarry Smith 1039a96d6ef6SBarry Smith + ts - timestep context 1040a96d6ef6SBarry Smith . t - current timestep 1041d763cef2SBarry Smith . u - input vector 1042d763cef2SBarry Smith . F - function vector 1043d763cef2SBarry Smith - ctx - [optional] user-defined function context 1044d763cef2SBarry Smith 1045d763cef2SBarry Smith Level: beginner 1046d763cef2SBarry Smith 104795452b02SPatrick Sanan Notes: 104895452b02SPatrick Sanan You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 10492bbac0d3SBarry Smith 1050ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 1051d763cef2SBarry Smith @*/ 1052089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 1053d763cef2SBarry Smith { 1054089b2837SJed Brown PetscErrorCode ierr; 1055089b2837SJed Brown SNES snes; 10560298fd71SBarry Smith Vec ralloc = NULL; 105724989b8cSPeter Brune DM dm; 1058d763cef2SBarry Smith 1059089b2837SJed Brown PetscFunctionBegin; 10600700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1061ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 106224989b8cSPeter Brune 106324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 106424989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 1065089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1066e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 1067e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 1068e856ceecSJed Brown r = ralloc; 1069e856ceecSJed Brown } 1070089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 1071e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1072d763cef2SBarry Smith PetscFunctionReturn(0); 1073d763cef2SBarry Smith } 1074d763cef2SBarry Smith 1075ef20d060SBarry Smith /*@C 1076abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1077ef20d060SBarry Smith 1078ef20d060SBarry Smith Logically Collective on TS 1079ef20d060SBarry Smith 1080ef20d060SBarry Smith Input Parameters: 1081ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 1082ef20d060SBarry Smith . f - routine for evaluating the solution 1083ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 10840298fd71SBarry Smith function evaluation routine (may be NULL) 1085ef20d060SBarry Smith 1086a96d6ef6SBarry Smith Calling sequence of f: 1087a96d6ef6SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,void *ctx); 1088ef20d060SBarry Smith 1089ef20d060SBarry Smith + t - current timestep 1090ef20d060SBarry Smith . u - output vector 1091ef20d060SBarry Smith - ctx - [optional] user-defined function context 1092ef20d060SBarry Smith 10930ed3bfb6SBarry Smith Options Database: 10940ed3bfb6SBarry Smith + -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction() 10950ed3bfb6SBarry Smith - -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction() 10960ed3bfb6SBarry Smith 1097abd5a294SJed Brown Notes: 1098abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 1099abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 1100abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 1101abd5a294SJed Brown 11024f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 1103abd5a294SJed Brown 1104ef20d060SBarry Smith Level: beginner 1105ef20d060SBarry Smith 11060ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError() 1107ef20d060SBarry Smith @*/ 1108ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 1109ef20d060SBarry Smith { 1110ef20d060SBarry Smith PetscErrorCode ierr; 1111ef20d060SBarry Smith DM dm; 1112ef20d060SBarry Smith 1113ef20d060SBarry Smith PetscFunctionBegin; 1114ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1115ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1116ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 1117ef20d060SBarry Smith PetscFunctionReturn(0); 1118ef20d060SBarry Smith } 1119ef20d060SBarry Smith 11209b7cd975SBarry Smith /*@C 11219b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 11229b7cd975SBarry Smith 11239b7cd975SBarry Smith Logically Collective on TS 11249b7cd975SBarry Smith 11259b7cd975SBarry Smith Input Parameters: 11269b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 1127e162b725SBarry Smith . func - routine for evaluating the forcing function 11289b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 11290298fd71SBarry Smith function evaluation routine (may be NULL) 11309b7cd975SBarry Smith 11319b7cd975SBarry Smith Calling sequence of func: 11326bc98fa9SBarry Smith $ PetscErrorCode func (TS ts,PetscReal t,Vec f,void *ctx); 11339b7cd975SBarry Smith 11349b7cd975SBarry Smith + t - current timestep 1135e162b725SBarry Smith . f - output vector 11369b7cd975SBarry Smith - ctx - [optional] user-defined function context 11379b7cd975SBarry Smith 11389b7cd975SBarry Smith Notes: 11399b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 1140e162b725SBarry 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 1141e162b725SBarry Smith definition of the problem you are solving and hence possibly introducing bugs. 1142e162b725SBarry Smith 1143e162b725SBarry Smith This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0 1144e162b725SBarry Smith 1145e162b725SBarry 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 1146e162b725SBarry Smith parameters can be passed in the ctx variable. 11479b7cd975SBarry Smith 11489b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 11499b7cd975SBarry Smith 11509b7cd975SBarry Smith Level: beginner 11519b7cd975SBarry Smith 11529b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 11539b7cd975SBarry Smith @*/ 1154e162b725SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx) 11559b7cd975SBarry Smith { 11569b7cd975SBarry Smith PetscErrorCode ierr; 11579b7cd975SBarry Smith DM dm; 11589b7cd975SBarry Smith 11599b7cd975SBarry Smith PetscFunctionBegin; 11609b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 11619b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1162e162b725SBarry Smith ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr); 11639b7cd975SBarry Smith PetscFunctionReturn(0); 11649b7cd975SBarry Smith } 11659b7cd975SBarry Smith 1166d763cef2SBarry Smith /*@C 1167f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1168b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1169d763cef2SBarry Smith 11703f9fe445SBarry Smith Logically Collective on TS 1171d763cef2SBarry Smith 1172d763cef2SBarry Smith Input Parameters: 1173d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1174e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1175e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1176d763cef2SBarry Smith . f - the Jacobian evaluation routine 1177d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 11780298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1179d763cef2SBarry Smith 1180f7ab8db6SBarry Smith Calling sequence of f: 1181a96d6ef6SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1182d763cef2SBarry Smith 1183d763cef2SBarry Smith + t - current timestep 1184d763cef2SBarry Smith . u - input vector 1185e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1186e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1187d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1188d763cef2SBarry Smith 11896cd88445SBarry Smith Notes: 11906cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 11916cd88445SBarry Smith 11926cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1193ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1194d763cef2SBarry Smith 1195d763cef2SBarry Smith Level: beginner 1196d763cef2SBarry Smith 1197ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1198d763cef2SBarry Smith 1199d763cef2SBarry Smith @*/ 1200e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1201d763cef2SBarry Smith { 1202277b19d0SLisandro Dalcin PetscErrorCode ierr; 1203089b2837SJed Brown SNES snes; 120424989b8cSPeter Brune DM dm; 120524989b8cSPeter Brune TSIJacobian ijacobian; 1206277b19d0SLisandro Dalcin 1207d763cef2SBarry Smith PetscFunctionBegin; 12080700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1209e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1210e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1211e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1212e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1213d763cef2SBarry Smith 121424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 121524989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 12160298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1217089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 12185f659677SPeter Brune if (!ijacobian) { 1219e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 12200e4ef248SJed Brown } 1221e5d3d808SBarry Smith if (Amat) { 1222e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 12230e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1224e5d3d808SBarry Smith ts->Arhs = Amat; 12250e4ef248SJed Brown } 1226e5d3d808SBarry Smith if (Pmat) { 1227e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 12280e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1229e5d3d808SBarry Smith ts->Brhs = Pmat; 12300e4ef248SJed Brown } 1231d763cef2SBarry Smith PetscFunctionReturn(0); 1232d763cef2SBarry Smith } 1233d763cef2SBarry Smith 1234316643e7SJed Brown /*@C 1235b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1236316643e7SJed Brown 12373f9fe445SBarry Smith Logically Collective on TS 1238316643e7SJed Brown 1239316643e7SJed Brown Input Parameters: 1240316643e7SJed Brown + ts - the TS context obtained from TSCreate() 12410298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1242316643e7SJed Brown . f - the function evaluation routine 12430298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1244316643e7SJed Brown 1245316643e7SJed Brown Calling sequence of f: 12466bc98fa9SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1247316643e7SJed Brown 1248316643e7SJed Brown + t - time at step/stage being solved 1249316643e7SJed Brown . u - state vector 1250316643e7SJed Brown . u_t - time derivative of state vector 1251316643e7SJed Brown . F - function vector 1252316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1253316643e7SJed Brown 1254316643e7SJed Brown Important: 12552bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1256316643e7SJed Brown 1257316643e7SJed Brown Level: beginner 1258316643e7SJed Brown 1259d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1260316643e7SJed Brown @*/ 126151699248SLisandro Dalcin PetscErrorCode TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx) 1262316643e7SJed Brown { 1263089b2837SJed Brown PetscErrorCode ierr; 1264089b2837SJed Brown SNES snes; 126551699248SLisandro Dalcin Vec ralloc = NULL; 126624989b8cSPeter Brune DM dm; 1267316643e7SJed Brown 1268316643e7SJed Brown PetscFunctionBegin; 12690700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 127051699248SLisandro Dalcin if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 127124989b8cSPeter Brune 127224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 127324989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 127424989b8cSPeter Brune 1275089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 127651699248SLisandro Dalcin if (!r && !ts->dm && ts->vec_sol) { 127751699248SLisandro Dalcin ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 127851699248SLisandro Dalcin r = ralloc; 1279e856ceecSJed Brown } 128051699248SLisandro Dalcin ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 128151699248SLisandro Dalcin ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 1282089b2837SJed Brown PetscFunctionReturn(0); 1283089b2837SJed Brown } 1284089b2837SJed Brown 1285089b2837SJed Brown /*@C 1286a5b23f4aSJose E. Roman TSGetIFunction - Returns the vector where the implicit residual is stored and the function/context to compute it. 1287089b2837SJed Brown 1288089b2837SJed Brown Not Collective 1289089b2837SJed Brown 1290089b2837SJed Brown Input Parameter: 1291089b2837SJed Brown . ts - the TS context 1292089b2837SJed Brown 1293d8d19677SJose E. Roman Output Parameters: 12940298fd71SBarry Smith + r - vector to hold residual (or NULL) 12950298fd71SBarry Smith . func - the function to compute residual (or NULL) 12960298fd71SBarry Smith - ctx - the function context (or NULL) 1297089b2837SJed Brown 1298089b2837SJed Brown Level: advanced 1299089b2837SJed Brown 1300089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1301089b2837SJed Brown @*/ 1302089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1303089b2837SJed Brown { 1304089b2837SJed Brown PetscErrorCode ierr; 1305089b2837SJed Brown SNES snes; 130624989b8cSPeter Brune DM dm; 1307089b2837SJed Brown 1308089b2837SJed Brown PetscFunctionBegin; 1309089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1310089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 13110298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 131224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 131324989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1314089b2837SJed Brown PetscFunctionReturn(0); 1315089b2837SJed Brown } 1316089b2837SJed Brown 1317089b2837SJed Brown /*@C 1318089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1319089b2837SJed Brown 1320089b2837SJed Brown Not Collective 1321089b2837SJed Brown 1322089b2837SJed Brown Input Parameter: 1323089b2837SJed Brown . ts - the TS context 1324089b2837SJed Brown 1325d8d19677SJose E. Roman Output Parameters: 13260298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 13270298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 13280298fd71SBarry Smith - ctx - the function context (or NULL) 1329089b2837SJed Brown 1330089b2837SJed Brown Level: advanced 1331089b2837SJed Brown 13322bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction() 1333089b2837SJed Brown @*/ 1334089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1335089b2837SJed Brown { 1336089b2837SJed Brown PetscErrorCode ierr; 1337089b2837SJed Brown SNES snes; 133824989b8cSPeter Brune DM dm; 1339089b2837SJed Brown 1340089b2837SJed Brown PetscFunctionBegin; 1341089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1342089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 13430298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 134424989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 134524989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1346316643e7SJed Brown PetscFunctionReturn(0); 1347316643e7SJed Brown } 1348316643e7SJed Brown 1349316643e7SJed Brown /*@C 1350a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1351ae8867d6SBarry Smith provided with TSSetIFunction(). 1352316643e7SJed Brown 13533f9fe445SBarry Smith Logically Collective on TS 1354316643e7SJed Brown 1355316643e7SJed Brown Input Parameters: 1356316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1357e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1358e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1359316643e7SJed Brown . f - the Jacobian evaluation routine 13600298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1361316643e7SJed Brown 1362316643e7SJed Brown Calling sequence of f: 13636bc98fa9SBarry Smith $ PetscErrorCode f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1364316643e7SJed Brown 1365316643e7SJed Brown + t - time at step/stage being solved 13661b4a444bSJed Brown . U - state vector 13671b4a444bSJed Brown . U_t - time derivative of state vector 1368316643e7SJed Brown . a - shift 1369e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1370e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1371316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1372316643e7SJed Brown 1373316643e7SJed Brown Notes: 1374e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1375316643e7SJed Brown 1376895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 1377895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 1378895c21f2SBarry Smith 1379a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1380b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1381a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1382a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1383a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1384a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1385a4f0a591SBarry Smith 13866cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 13876cd88445SBarry Smith 13886cd88445SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f() 1389ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1390ca5f011dSBarry Smith 1391316643e7SJed Brown Level: beginner 1392316643e7SJed Brown 1393ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1394316643e7SJed Brown 1395316643e7SJed Brown @*/ 1396e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1397316643e7SJed Brown { 1398316643e7SJed Brown PetscErrorCode ierr; 1399089b2837SJed Brown SNES snes; 140024989b8cSPeter Brune DM dm; 1401316643e7SJed Brown 1402316643e7SJed Brown PetscFunctionBegin; 14030700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1404e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1405e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1406e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1407e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 140824989b8cSPeter Brune 140924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 141024989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 141124989b8cSPeter Brune 1412089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1413e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1414316643e7SJed Brown PetscFunctionReturn(0); 1415316643e7SJed Brown } 1416316643e7SJed Brown 1417e1244c69SJed Brown /*@ 1418e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1419e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1420e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1421e1244c69SJed Brown not been changed by the TS. 1422e1244c69SJed Brown 1423e1244c69SJed Brown Logically Collective 1424e1244c69SJed Brown 1425*4165533cSJose E. Roman Input Parameters: 1426e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1427e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1428e1244c69SJed Brown 1429e1244c69SJed Brown Level: intermediate 1430e1244c69SJed Brown 1431e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1432e1244c69SJed Brown @*/ 1433e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1434e1244c69SJed Brown { 1435e1244c69SJed Brown PetscFunctionBegin; 1436e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1437e1244c69SJed Brown PetscFunctionReturn(0); 1438e1244c69SJed Brown } 1439e1244c69SJed Brown 1440efe9872eSLisandro Dalcin /*@C 1441efe9872eSLisandro Dalcin TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved. 1442efe9872eSLisandro Dalcin 1443efe9872eSLisandro Dalcin Logically Collective on TS 1444efe9872eSLisandro Dalcin 1445efe9872eSLisandro Dalcin Input Parameters: 1446efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1447efe9872eSLisandro Dalcin . F - vector to hold the residual (or NULL to have it created internally) 1448efe9872eSLisandro Dalcin . fun - the function evaluation routine 1449efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1450efe9872eSLisandro Dalcin 1451efe9872eSLisandro Dalcin Calling sequence of fun: 14526bc98fa9SBarry Smith $ PetscErrorCode fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx); 1453efe9872eSLisandro Dalcin 1454efe9872eSLisandro Dalcin + t - time at step/stage being solved 1455efe9872eSLisandro Dalcin . U - state vector 1456efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1457efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1458efe9872eSLisandro Dalcin . F - function vector 1459efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine (may be NULL) 1460efe9872eSLisandro Dalcin 1461efe9872eSLisandro Dalcin Level: beginner 1462efe9872eSLisandro Dalcin 1463a96d6ef6SBarry Smith .seealso: TSSetI2Jacobian(), TSSetIFunction(), TSCreate(), TSSetRHSFunction() 1464efe9872eSLisandro Dalcin @*/ 1465efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx) 1466efe9872eSLisandro Dalcin { 1467efe9872eSLisandro Dalcin DM dm; 1468efe9872eSLisandro Dalcin PetscErrorCode ierr; 1469efe9872eSLisandro Dalcin 1470efe9872eSLisandro Dalcin PetscFunctionBegin; 1471efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1472efe9872eSLisandro Dalcin if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2); 1473efe9872eSLisandro Dalcin ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr); 1474efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1475efe9872eSLisandro Dalcin ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1476efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1477efe9872eSLisandro Dalcin } 1478efe9872eSLisandro Dalcin 1479efe9872eSLisandro Dalcin /*@C 1480a5b23f4aSJose E. Roman TSGetI2Function - Returns the vector where the implicit residual is stored and the function/context to compute it. 1481efe9872eSLisandro Dalcin 1482efe9872eSLisandro Dalcin Not Collective 1483efe9872eSLisandro Dalcin 1484efe9872eSLisandro Dalcin Input Parameter: 1485efe9872eSLisandro Dalcin . ts - the TS context 1486efe9872eSLisandro Dalcin 1487d8d19677SJose E. Roman Output Parameters: 1488efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL) 1489efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL) 1490efe9872eSLisandro Dalcin - ctx - the function context (or NULL) 1491efe9872eSLisandro Dalcin 1492efe9872eSLisandro Dalcin Level: advanced 1493efe9872eSLisandro Dalcin 1494a96d6ef6SBarry Smith .seealso: TSSetIFunction(), SNESGetFunction(), TSCreate() 1495efe9872eSLisandro Dalcin @*/ 1496efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx) 1497efe9872eSLisandro Dalcin { 1498efe9872eSLisandro Dalcin PetscErrorCode ierr; 1499efe9872eSLisandro Dalcin SNES snes; 1500efe9872eSLisandro Dalcin DM dm; 1501efe9872eSLisandro Dalcin 1502efe9872eSLisandro Dalcin PetscFunctionBegin; 1503efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1504efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1505efe9872eSLisandro Dalcin ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 1506efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1507efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr); 1508efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1509efe9872eSLisandro Dalcin } 1510efe9872eSLisandro Dalcin 1511efe9872eSLisandro Dalcin /*@C 1512bc77d74cSLisandro Dalcin TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t + a*dF/dU_tt 1513efe9872eSLisandro Dalcin where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function(). 1514efe9872eSLisandro Dalcin 1515efe9872eSLisandro Dalcin Logically Collective on TS 1516efe9872eSLisandro Dalcin 1517efe9872eSLisandro Dalcin Input Parameters: 1518efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1519efe9872eSLisandro Dalcin . J - Jacobian matrix 1520efe9872eSLisandro Dalcin . P - preconditioning matrix for J (may be same as J) 1521efe9872eSLisandro Dalcin . jac - the Jacobian evaluation routine 1522efe9872eSLisandro Dalcin - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1523efe9872eSLisandro Dalcin 1524efe9872eSLisandro Dalcin Calling sequence of jac: 15256bc98fa9SBarry 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); 1526efe9872eSLisandro Dalcin 1527efe9872eSLisandro Dalcin + t - time at step/stage being solved 1528efe9872eSLisandro Dalcin . U - state vector 1529efe9872eSLisandro Dalcin . U_t - time derivative of state vector 1530efe9872eSLisandro Dalcin . U_tt - second time derivative of state vector 1531efe9872eSLisandro Dalcin . v - shift for U_t 1532efe9872eSLisandro Dalcin . a - shift for U_tt 1533efe9872eSLisandro 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 1534efe9872eSLisandro Dalcin . P - preconditioning matrix for J, may be same as J 1535efe9872eSLisandro Dalcin - ctx - [optional] user-defined context for matrix evaluation routine 1536efe9872eSLisandro Dalcin 1537efe9872eSLisandro Dalcin Notes: 1538efe9872eSLisandro Dalcin The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve. 1539efe9872eSLisandro Dalcin 1540efe9872eSLisandro Dalcin The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be 1541efe9872eSLisandro 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. 1542efe9872eSLisandro Dalcin The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U where the positive "shift" 1543bc77d74cSLisandro Dalcin parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states. 1544efe9872eSLisandro Dalcin 1545efe9872eSLisandro Dalcin Level: beginner 1546efe9872eSLisandro Dalcin 1547a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Jacobian() 1548efe9872eSLisandro Dalcin @*/ 1549efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx) 1550efe9872eSLisandro Dalcin { 1551efe9872eSLisandro Dalcin DM dm; 1552efe9872eSLisandro Dalcin PetscErrorCode ierr; 1553efe9872eSLisandro Dalcin 1554efe9872eSLisandro Dalcin PetscFunctionBegin; 1555efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1556efe9872eSLisandro Dalcin if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2); 1557efe9872eSLisandro Dalcin if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3); 1558efe9872eSLisandro Dalcin ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr); 1559efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1560efe9872eSLisandro Dalcin ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1561efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1562efe9872eSLisandro Dalcin } 1563efe9872eSLisandro Dalcin 1564efe9872eSLisandro Dalcin /*@C 1565efe9872eSLisandro Dalcin TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep. 1566efe9872eSLisandro Dalcin 1567efe9872eSLisandro Dalcin Not Collective, but parallel objects are returned if TS is parallel 1568efe9872eSLisandro Dalcin 1569efe9872eSLisandro Dalcin Input Parameter: 1570efe9872eSLisandro Dalcin . ts - The TS context obtained from TSCreate() 1571efe9872eSLisandro Dalcin 1572efe9872eSLisandro Dalcin Output Parameters: 1573efe9872eSLisandro Dalcin + J - The (approximate) Jacobian of F(t,U,U_t,U_tt) 1574efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J 1575efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices 1576efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine 1577efe9872eSLisandro Dalcin 157895452b02SPatrick Sanan Notes: 157995452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 1580efe9872eSLisandro Dalcin 1581efe9872eSLisandro Dalcin Level: advanced 1582efe9872eSLisandro Dalcin 1583a96d6ef6SBarry Smith .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber(), TSSetI2Jacobian(), TSGetI2Function(), TSCreate() 1584efe9872eSLisandro Dalcin 1585efe9872eSLisandro Dalcin @*/ 1586efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx) 1587efe9872eSLisandro Dalcin { 1588efe9872eSLisandro Dalcin PetscErrorCode ierr; 1589efe9872eSLisandro Dalcin SNES snes; 1590efe9872eSLisandro Dalcin DM dm; 1591efe9872eSLisandro Dalcin 1592efe9872eSLisandro Dalcin PetscFunctionBegin; 1593efe9872eSLisandro Dalcin ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1594efe9872eSLisandro Dalcin ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 1595efe9872eSLisandro Dalcin ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr); 1596efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1597efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr); 1598efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1599efe9872eSLisandro Dalcin } 1600efe9872eSLisandro Dalcin 1601efe9872eSLisandro Dalcin /*@ 1602efe9872eSLisandro Dalcin TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0 1603efe9872eSLisandro Dalcin 1604d083f849SBarry Smith Collective on TS 1605efe9872eSLisandro Dalcin 1606efe9872eSLisandro Dalcin Input Parameters: 1607efe9872eSLisandro Dalcin + ts - the TS context 1608efe9872eSLisandro Dalcin . t - current time 1609efe9872eSLisandro Dalcin . U - state vector 1610efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t) 1611efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt) 1612efe9872eSLisandro Dalcin 1613efe9872eSLisandro Dalcin Output Parameter: 1614efe9872eSLisandro Dalcin . F - the residual vector 1615efe9872eSLisandro Dalcin 1616efe9872eSLisandro Dalcin Note: 1617efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1618efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1619efe9872eSLisandro Dalcin 1620efe9872eSLisandro Dalcin Level: developer 1621efe9872eSLisandro Dalcin 1622a96d6ef6SBarry Smith .seealso: TSSetI2Function(), TSGetI2Function() 1623efe9872eSLisandro Dalcin @*/ 1624efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F) 1625efe9872eSLisandro Dalcin { 1626efe9872eSLisandro Dalcin DM dm; 1627efe9872eSLisandro Dalcin TSI2Function I2Function; 1628efe9872eSLisandro Dalcin void *ctx; 1629efe9872eSLisandro Dalcin TSRHSFunction rhsfunction; 1630efe9872eSLisandro Dalcin PetscErrorCode ierr; 1631efe9872eSLisandro Dalcin 1632efe9872eSLisandro Dalcin PetscFunctionBegin; 1633efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1634efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1635efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1636efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1637efe9872eSLisandro Dalcin PetscValidHeaderSpecific(F,VEC_CLASSID,6); 1638efe9872eSLisandro Dalcin 1639efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1640efe9872eSLisandro Dalcin ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr); 1641efe9872eSLisandro Dalcin ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 1642efe9872eSLisandro Dalcin 1643efe9872eSLisandro Dalcin if (!I2Function) { 1644efe9872eSLisandro Dalcin ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr); 1645efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1646efe9872eSLisandro Dalcin } 1647efe9872eSLisandro Dalcin 1648efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1649efe9872eSLisandro Dalcin 1650efe9872eSLisandro Dalcin PetscStackPush("TS user implicit function"); 1651efe9872eSLisandro Dalcin ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr); 1652efe9872eSLisandro Dalcin PetscStackPop; 1653efe9872eSLisandro Dalcin 1654efe9872eSLisandro Dalcin if (rhsfunction) { 1655efe9872eSLisandro Dalcin Vec Frhs; 1656efe9872eSLisandro Dalcin ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 1657efe9872eSLisandro Dalcin ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 1658efe9872eSLisandro Dalcin ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr); 1659efe9872eSLisandro Dalcin } 1660efe9872eSLisandro Dalcin 1661efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr); 1662efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1663efe9872eSLisandro Dalcin } 1664efe9872eSLisandro Dalcin 1665efe9872eSLisandro Dalcin /*@ 1666efe9872eSLisandro Dalcin TSComputeI2Jacobian - Evaluates the Jacobian of the DAE 1667efe9872eSLisandro Dalcin 1668d083f849SBarry Smith Collective on TS 1669efe9872eSLisandro Dalcin 1670efe9872eSLisandro Dalcin Input Parameters: 1671efe9872eSLisandro Dalcin + ts - the TS context 1672efe9872eSLisandro Dalcin . t - current timestep 1673efe9872eSLisandro Dalcin . U - state vector 1674efe9872eSLisandro Dalcin . V - time derivative of state vector 1675efe9872eSLisandro Dalcin . A - second time derivative of state vector 1676efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below 1677efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below 1678efe9872eSLisandro Dalcin 1679efe9872eSLisandro Dalcin Output Parameters: 1680efe9872eSLisandro Dalcin + J - Jacobian matrix 1681efe9872eSLisandro Dalcin - P - optional preconditioning matrix 1682efe9872eSLisandro Dalcin 1683efe9872eSLisandro Dalcin Notes: 1684efe9872eSLisandro Dalcin If F(t,U,V,A)=0 is the DAE, the required Jacobian is 1685efe9872eSLisandro Dalcin 1686efe9872eSLisandro Dalcin dF/dU + shiftV*dF/dV + shiftA*dF/dA 1687efe9872eSLisandro Dalcin 1688efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1689efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1690efe9872eSLisandro Dalcin 1691efe9872eSLisandro Dalcin Level: developer 1692efe9872eSLisandro Dalcin 1693efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian() 1694efe9872eSLisandro Dalcin @*/ 1695efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P) 1696efe9872eSLisandro Dalcin { 1697efe9872eSLisandro Dalcin DM dm; 1698efe9872eSLisandro Dalcin TSI2Jacobian I2Jacobian; 1699efe9872eSLisandro Dalcin void *ctx; 1700efe9872eSLisandro Dalcin TSRHSJacobian rhsjacobian; 1701efe9872eSLisandro Dalcin PetscErrorCode ierr; 1702efe9872eSLisandro Dalcin 1703efe9872eSLisandro Dalcin PetscFunctionBegin; 1704efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1705efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 1706efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V,VEC_CLASSID,4); 1707efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A,VEC_CLASSID,5); 1708efe9872eSLisandro Dalcin PetscValidHeaderSpecific(J,MAT_CLASSID,8); 1709efe9872eSLisandro Dalcin PetscValidHeaderSpecific(P,MAT_CLASSID,9); 1710efe9872eSLisandro Dalcin 1711efe9872eSLisandro Dalcin ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1712efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr); 1713efe9872eSLisandro Dalcin ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 1714efe9872eSLisandro Dalcin 1715efe9872eSLisandro Dalcin if (!I2Jacobian) { 1716efe9872eSLisandro Dalcin ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr); 1717efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1718efe9872eSLisandro Dalcin } 1719efe9872eSLisandro Dalcin 1720efe9872eSLisandro Dalcin ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1721efe9872eSLisandro Dalcin 1722efe9872eSLisandro Dalcin PetscStackPush("TS user implicit Jacobian"); 1723efe9872eSLisandro Dalcin ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr); 1724efe9872eSLisandro Dalcin PetscStackPop; 1725efe9872eSLisandro Dalcin 1726efe9872eSLisandro Dalcin if (rhsjacobian) { 1727d60b7d5cSBarry Smith Mat Jrhs,Prhs; 1728efe9872eSLisandro Dalcin ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr); 1729efe9872eSLisandro Dalcin ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr); 1730d60b7d5cSBarry Smith ierr = MatAXPY(J,-1,Jrhs,ts->axpy_pattern);CHKERRQ(ierr); 1731d60b7d5cSBarry Smith if (P != J) {ierr = MatAXPY(P,-1,Prhs,ts->axpy_pattern);CHKERRQ(ierr);} 1732efe9872eSLisandro Dalcin } 1733efe9872eSLisandro Dalcin 1734efe9872eSLisandro Dalcin ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr); 1735efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1736efe9872eSLisandro Dalcin } 1737efe9872eSLisandro Dalcin 1738438f35afSJed Brown /*@C 1739438f35afSJed Brown TSSetTransientVariable - sets function to transform from state to transient variables 1740438f35afSJed Brown 1741438f35afSJed Brown Logically Collective 1742438f35afSJed Brown 1743*4165533cSJose E. Roman Input Parameters: 1744438f35afSJed Brown + ts - time stepping context on which to change the transient variable 1745a96d6ef6SBarry Smith . tvar - a function that transforms to transient variables 1746438f35afSJed Brown - ctx - a context for tvar 1747438f35afSJed Brown 1748a96d6ef6SBarry Smith Calling sequence of tvar: 1749a96d6ef6SBarry Smith $ PetscErrorCode tvar(TS ts,Vec p,Vec c,void *ctx); 1750a96d6ef6SBarry Smith 1751a96d6ef6SBarry Smith + ts - timestep context 1752a96d6ef6SBarry Smith . p - input vector (primative form) 1753a96d6ef6SBarry Smith . c - output vector, transient variables (conservative form) 1754a96d6ef6SBarry Smith - ctx - [optional] user-defined function context 1755a96d6ef6SBarry Smith 1756438f35afSJed Brown Level: advanced 1757438f35afSJed Brown 1758438f35afSJed Brown Notes: 1759438f35afSJed Brown This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., TSBDF) 1760438f35afSJed Brown can be conservative. In this context, primitive variables P are used to model the state (e.g., because they lead to 1761438f35afSJed Brown well-conditioned formulations even in limiting cases such as low-Mach or zero porosity). The transient variable is 1762438f35afSJed Brown C(P), specified by calling this function. An IFunction thus receives arguments (P, Cdot) and the IJacobian must be 1763438f35afSJed Brown evaluated via the chain rule, as in 1764438f35afSJed Brown 1765438f35afSJed Brown dF/dP + shift * dF/dCdot dC/dP. 1766438f35afSJed Brown 1767438f35afSJed Brown .seealso: DMTSSetTransientVariable(), DMTSGetTransientVariable(), TSSetIFunction(), TSSetIJacobian() 1768438f35afSJed Brown @*/ 1769438f35afSJed Brown PetscErrorCode TSSetTransientVariable(TS ts,TSTransientVariable tvar,void *ctx) 1770438f35afSJed Brown { 1771438f35afSJed Brown PetscErrorCode ierr; 1772438f35afSJed Brown DM dm; 1773438f35afSJed Brown 1774438f35afSJed Brown PetscFunctionBegin; 1775438f35afSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1776438f35afSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1777438f35afSJed Brown ierr = DMTSSetTransientVariable(dm,tvar,ctx);CHKERRQ(ierr); 1778438f35afSJed Brown PetscFunctionReturn(0); 1779438f35afSJed Brown } 1780438f35afSJed Brown 1781efe9872eSLisandro Dalcin /*@ 1782e3c11fc1SJed Brown TSComputeTransientVariable - transforms state (primitive) variables to transient (conservative) variables 1783e3c11fc1SJed Brown 1784e3c11fc1SJed Brown Logically Collective 1785e3c11fc1SJed Brown 1786e3c11fc1SJed Brown Input Parameters: 1787e3c11fc1SJed Brown + ts - TS on which to compute 1788e3c11fc1SJed Brown - U - state vector to be transformed to transient variables 1789e3c11fc1SJed Brown 1790e3c11fc1SJed Brown Output Parameters: 1791e3c11fc1SJed Brown . C - transient (conservative) variable 1792e3c11fc1SJed Brown 1793e3c11fc1SJed Brown Developer Notes: 1794e3c11fc1SJed Brown If DMTSSetTransientVariable() has not been called, then C is not modified in this routine and C=NULL is allowed. 1795e3c11fc1SJed Brown This makes it safe to call without a guard. One can use TSHasTransientVariable() to check if transient variables are 1796e3c11fc1SJed Brown being used. 1797e3c11fc1SJed Brown 1798e3c11fc1SJed Brown Level: developer 1799e3c11fc1SJed Brown 1800e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeIFunction(), TSComputeIJacobian() 1801e3c11fc1SJed Brown @*/ 1802e3c11fc1SJed Brown PetscErrorCode TSComputeTransientVariable(TS ts,Vec U,Vec C) 1803e3c11fc1SJed Brown { 1804e3c11fc1SJed Brown PetscErrorCode ierr; 1805e3c11fc1SJed Brown DM dm; 1806e3c11fc1SJed Brown DMTS dmts; 1807e3c11fc1SJed Brown 1808e3c11fc1SJed Brown PetscFunctionBegin; 1809e3c11fc1SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1810e3c11fc1SJed Brown PetscValidHeaderSpecific(U,VEC_CLASSID,2); 1811e3c11fc1SJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1812e3c11fc1SJed Brown ierr = DMGetDMTS(dm,&dmts);CHKERRQ(ierr); 1813e3c11fc1SJed Brown if (dmts->ops->transientvar) { 1814e3c11fc1SJed Brown PetscValidHeaderSpecific(C,VEC_CLASSID,3); 1815e3c11fc1SJed Brown ierr = (*dmts->ops->transientvar)(ts,U,C,dmts->transientvarctx);CHKERRQ(ierr); 1816e3c11fc1SJed Brown } 1817e3c11fc1SJed Brown PetscFunctionReturn(0); 1818e3c11fc1SJed Brown } 1819e3c11fc1SJed Brown 1820e3c11fc1SJed Brown /*@ 1821e3c11fc1SJed Brown TSHasTransientVariable - determine whether transient variables have been set 1822e3c11fc1SJed Brown 1823e3c11fc1SJed Brown Logically Collective 1824e3c11fc1SJed Brown 1825e3c11fc1SJed Brown Input Parameters: 1826e3c11fc1SJed Brown . ts - TS on which to compute 1827e3c11fc1SJed Brown 1828e3c11fc1SJed Brown Output Parameters: 1829e3c11fc1SJed Brown . has - PETSC_TRUE if transient variables have been set 1830e3c11fc1SJed Brown 1831e3c11fc1SJed Brown Level: developer 1832e3c11fc1SJed Brown 1833e3c11fc1SJed Brown .seealso: DMTSSetTransientVariable(), TSComputeTransientVariable() 1834e3c11fc1SJed Brown @*/ 1835e3c11fc1SJed Brown PetscErrorCode TSHasTransientVariable(TS ts,PetscBool *has) 1836e3c11fc1SJed Brown { 1837e3c11fc1SJed Brown PetscErrorCode ierr; 1838e3c11fc1SJed Brown DM dm; 1839e3c11fc1SJed Brown DMTS dmts; 1840e3c11fc1SJed Brown 1841e3c11fc1SJed Brown PetscFunctionBegin; 1842e3c11fc1SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1843e3c11fc1SJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 1844e3c11fc1SJed Brown ierr = DMGetDMTS(dm,&dmts);CHKERRQ(ierr); 1845e3c11fc1SJed Brown *has = dmts->ops->transientvar ? PETSC_TRUE : PETSC_FALSE; 1846e3c11fc1SJed Brown PetscFunctionReturn(0); 1847e3c11fc1SJed Brown } 1848e3c11fc1SJed Brown 1849e3c11fc1SJed Brown /*@ 1850efe9872eSLisandro Dalcin TS2SetSolution - Sets the initial solution and time derivative vectors 1851efe9872eSLisandro Dalcin for use by the TS routines handling second order equations. 1852efe9872eSLisandro Dalcin 1853d083f849SBarry Smith Logically Collective on TS 1854efe9872eSLisandro Dalcin 1855efe9872eSLisandro Dalcin Input Parameters: 1856efe9872eSLisandro Dalcin + ts - the TS context obtained from TSCreate() 1857efe9872eSLisandro Dalcin . u - the solution vector 1858efe9872eSLisandro Dalcin - v - the time derivative vector 1859efe9872eSLisandro Dalcin 1860efe9872eSLisandro Dalcin Level: beginner 1861efe9872eSLisandro Dalcin 1862efe9872eSLisandro Dalcin @*/ 1863efe9872eSLisandro Dalcin PetscErrorCode TS2SetSolution(TS ts,Vec u,Vec v) 1864efe9872eSLisandro Dalcin { 1865efe9872eSLisandro Dalcin PetscErrorCode ierr; 1866efe9872eSLisandro Dalcin 1867efe9872eSLisandro Dalcin PetscFunctionBegin; 1868efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1869efe9872eSLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 1870efe9872eSLisandro Dalcin PetscValidHeaderSpecific(v,VEC_CLASSID,3); 1871efe9872eSLisandro Dalcin ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 1872efe9872eSLisandro Dalcin ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr); 1873efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 1874efe9872eSLisandro Dalcin ts->vec_dot = v; 1875efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1876efe9872eSLisandro Dalcin } 1877efe9872eSLisandro Dalcin 1878efe9872eSLisandro Dalcin /*@ 1879efe9872eSLisandro Dalcin TS2GetSolution - Returns the solution and time derivative at the present timestep 1880efe9872eSLisandro Dalcin for second order equations. It is valid to call this routine inside the function 1881efe9872eSLisandro Dalcin that you are evaluating in order to move to the new timestep. This vector not 1882efe9872eSLisandro Dalcin changed until the solution at the next timestep has been calculated. 1883efe9872eSLisandro Dalcin 1884efe9872eSLisandro Dalcin Not Collective, but Vec returned is parallel if TS is parallel 1885efe9872eSLisandro Dalcin 1886efe9872eSLisandro Dalcin Input Parameter: 1887efe9872eSLisandro Dalcin . ts - the TS context obtained from TSCreate() 1888efe9872eSLisandro Dalcin 1889d8d19677SJose E. Roman Output Parameters: 1890efe9872eSLisandro Dalcin + u - the vector containing the solution 1891efe9872eSLisandro Dalcin - v - the vector containing the time derivative 1892efe9872eSLisandro Dalcin 1893efe9872eSLisandro Dalcin Level: intermediate 1894efe9872eSLisandro Dalcin 1895efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime() 1896efe9872eSLisandro Dalcin 1897efe9872eSLisandro Dalcin @*/ 1898efe9872eSLisandro Dalcin PetscErrorCode TS2GetSolution(TS ts,Vec *u,Vec *v) 1899efe9872eSLisandro Dalcin { 1900efe9872eSLisandro Dalcin PetscFunctionBegin; 1901efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1902efe9872eSLisandro Dalcin if (u) PetscValidPointer(u,2); 1903efe9872eSLisandro Dalcin if (v) PetscValidPointer(v,3); 1904efe9872eSLisandro Dalcin if (u) *u = ts->vec_sol; 1905efe9872eSLisandro Dalcin if (v) *v = ts->vec_dot; 1906efe9872eSLisandro Dalcin PetscFunctionReturn(0); 1907efe9872eSLisandro Dalcin } 1908efe9872eSLisandro Dalcin 190955849f57SBarry Smith /*@C 191055849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 191155849f57SBarry Smith 191255849f57SBarry Smith Collective on PetscViewer 191355849f57SBarry Smith 191455849f57SBarry Smith Input Parameters: 191555849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 191655849f57SBarry Smith some related function before a call to TSLoad(). 191755849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 191855849f57SBarry Smith 191955849f57SBarry Smith Level: intermediate 192055849f57SBarry Smith 192155849f57SBarry Smith Notes: 192255849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 192355849f57SBarry Smith 192455849f57SBarry Smith Notes for advanced users: 192555849f57SBarry Smith Most users should not need to know the details of the binary storage 192655849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 192755849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 192855849f57SBarry Smith format is 192955849f57SBarry Smith .vb 193055849f57SBarry Smith has not yet been determined 193155849f57SBarry Smith .ve 193255849f57SBarry Smith 193355849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 193455849f57SBarry Smith @*/ 1935f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 193655849f57SBarry Smith { 193755849f57SBarry Smith PetscErrorCode ierr; 193855849f57SBarry Smith PetscBool isbinary; 193955849f57SBarry Smith PetscInt classid; 194055849f57SBarry Smith char type[256]; 19412d53ad75SBarry Smith DMTS sdm; 1942ad6bc421SBarry Smith DM dm; 194355849f57SBarry Smith 194455849f57SBarry Smith PetscFunctionBegin; 1945f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 194655849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 194755849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 194855849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 194955849f57SBarry Smith 1950060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr); 1951ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 1952060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr); 1953f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1954f2c2a1b9SBarry Smith if (ts->ops->load) { 1955f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1956f2c2a1b9SBarry Smith } 1957ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1958ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1959ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1960f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1961f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 19622d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 19632d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 196455849f57SBarry Smith PetscFunctionReturn(0); 196555849f57SBarry Smith } 196655849f57SBarry Smith 19679804daf3SBarry Smith #include <petscdraw.h> 1968e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1969e04113cfSBarry Smith #include <petscviewersaws.h> 1970f05ece33SBarry Smith #endif 1971fe2efc57SMark 1972fe2efc57SMark /*@C 1973fe2efc57SMark TSViewFromOptions - View from Options 1974fe2efc57SMark 1975fe2efc57SMark Collective on TS 1976fe2efc57SMark 1977fe2efc57SMark Input Parameters: 1978fe2efc57SMark + A - the application ordering context 1979736c3998SJose E. Roman . obj - Optional object 1980736c3998SJose E. Roman - name - command line option 1981fe2efc57SMark 1982fe2efc57SMark Level: intermediate 1983fe2efc57SMark .seealso: TS, TSView, PetscObjectViewFromOptions(), TSCreate() 1984fe2efc57SMark @*/ 1985fe2efc57SMark PetscErrorCode TSViewFromOptions(TS A,PetscObject obj,const char name[]) 1986fe2efc57SMark { 1987fe2efc57SMark PetscErrorCode ierr; 1988fe2efc57SMark 1989fe2efc57SMark PetscFunctionBegin; 1990fe2efc57SMark PetscValidHeaderSpecific(A,TS_CLASSID,1); 1991fe2efc57SMark ierr = PetscObjectViewFromOptions((PetscObject)A,obj,name);CHKERRQ(ierr); 1992fe2efc57SMark PetscFunctionReturn(0); 1993fe2efc57SMark } 1994fe2efc57SMark 19957e2c5f70SBarry Smith /*@C 1996d763cef2SBarry Smith TSView - Prints the TS data structure. 1997d763cef2SBarry Smith 19984c49b128SBarry Smith Collective on TS 1999d763cef2SBarry Smith 2000d763cef2SBarry Smith Input Parameters: 2001d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2002d763cef2SBarry Smith - viewer - visualization context 2003d763cef2SBarry Smith 2004d763cef2SBarry Smith Options Database Key: 2005d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 2006d763cef2SBarry Smith 2007d763cef2SBarry Smith Notes: 2008d763cef2SBarry Smith The available visualization contexts include 2009b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 2010b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 2011d763cef2SBarry Smith output where only the first processor opens 2012d763cef2SBarry Smith the file. All other processors send their 2013d763cef2SBarry Smith data to the first processor to print. 2014d763cef2SBarry Smith 2015d763cef2SBarry Smith The user can open an alternative visualization context with 2016b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 2017d763cef2SBarry Smith 2018595c91d4SBarry Smith In the debugger you can do "call TSView(ts,0)" to display the TS solver. (The same holds for any PETSc object viewer). 2019595c91d4SBarry Smith 2020d763cef2SBarry Smith Level: beginner 2021d763cef2SBarry Smith 2022b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 2023d763cef2SBarry Smith @*/ 20247087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 2025d763cef2SBarry Smith { 2026dfbe8321SBarry Smith PetscErrorCode ierr; 202719fd82e9SBarry Smith TSType type; 20282b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 20292d53ad75SBarry Smith DMTS sdm; 2030e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 2031536b137fSBarry Smith PetscBool issaws; 2032f05ece33SBarry Smith #endif 2033d763cef2SBarry Smith 2034d763cef2SBarry Smith PetscFunctionBegin; 20350700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 20363050cee2SBarry Smith if (!viewer) { 2037ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 20383050cee2SBarry Smith } 20390700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 2040c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 2041fd16b177SBarry Smith 2042251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 2043251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 204455849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 20452b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 2046e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 2047536b137fSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 2048f05ece33SBarry Smith #endif 204932077d6dSBarry Smith if (iascii) { 2050dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 2051efd4aadfSBarry Smith if (ts->ops->view) { 2052efd4aadfSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 2053efd4aadfSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 2054efd4aadfSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2055efd4aadfSBarry Smith } 2056ef85077eSLisandro Dalcin if (ts->max_steps < PETSC_MAX_INT) { 205777431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 2058ef85077eSLisandro Dalcin } 2059ef85077eSLisandro Dalcin if (ts->max_time < PETSC_MAX_REAL) { 20607c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 2061ef85077eSLisandro Dalcin } 2062a6ab3590SBarry Smith if (ts->ifuncs) { 2063a6ab3590SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of I function evaluations=%D\n",ts->ifuncs);CHKERRQ(ierr); 2064a6ab3590SBarry Smith } 2065a6ab3590SBarry Smith if (ts->ijacs) { 2066a6ab3590SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of I Jacobian evaluations=%D\n",ts->ijacs);CHKERRQ(ierr); 2067a6ab3590SBarry Smith } 2068a6ab3590SBarry Smith if (ts->rhsfuncs) { 2069a6ab3590SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of RHS function evaluations=%D\n",ts->rhsfuncs);CHKERRQ(ierr); 2070a6ab3590SBarry Smith } 2071a6ab3590SBarry Smith if (ts->rhsjacs) { 2072a6ab3590SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of RHS Jacobian evaluations=%D\n",ts->rhsjacs);CHKERRQ(ierr); 2073a6ab3590SBarry Smith } 2074efd4aadfSBarry Smith if (ts->usessnes) { 2075efd4aadfSBarry Smith PetscBool lin; 2076d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 20775ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 2078d763cef2SBarry Smith } 20795ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 20801ef27442SStefano Zampini ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr); 2081efd4aadfSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr); 2082efd4aadfSBarry Smith } 2083193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 2084a0af407cSBarry Smith if (ts->vrtol) { 2085a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using vector of relative error tolerances, ");CHKERRQ(ierr); 2086a0af407cSBarry Smith } else { 2087a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr); 2088a0af407cSBarry Smith } 2089a0af407cSBarry Smith if (ts->vatol) { 2090a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using vector of absolute error tolerances\n");CHKERRQ(ierr); 2091a0af407cSBarry Smith } else { 2092a0af407cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr); 2093a0af407cSBarry Smith } 2094825ab935SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 2095efd4aadfSBarry Smith ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr); 2096825ab935SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 20970f5bd95cSBarry Smith } else if (isstring) { 2098a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 209936a9e3b9SBarry Smith ierr = PetscViewerStringSPrintf(viewer," TSType: %-7.7s",type);CHKERRQ(ierr); 210036a9e3b9SBarry Smith if (ts->ops->view) {ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);} 210155849f57SBarry Smith } else if (isbinary) { 210255849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 210355849f57SBarry Smith MPI_Comm comm; 210455849f57SBarry Smith PetscMPIInt rank; 210555849f57SBarry Smith char type[256]; 210655849f57SBarry Smith 210755849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 2108ffc4695bSBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr); 210955849f57SBarry Smith if (!rank) { 2110f253e43cSLisandro Dalcin ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr); 211155849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 2112f253e43cSLisandro Dalcin ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr); 211355849f57SBarry Smith } 211455849f57SBarry Smith if (ts->ops->view) { 211555849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 211655849f57SBarry Smith } 2117efd4aadfSBarry Smith if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);} 2118f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 2119f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 21202d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 21212d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 21222b0a91c0SBarry Smith } else if (isdraw) { 21232b0a91c0SBarry Smith PetscDraw draw; 21242b0a91c0SBarry Smith char str[36]; 212589fd9fafSBarry Smith PetscReal x,y,bottom,h; 21262b0a91c0SBarry Smith 21272b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 21282b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 21292b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 21302b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 213151fa3d41SBarry Smith ierr = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 213289fd9fafSBarry Smith bottom = y - h; 21332b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 21342b0a91c0SBarry Smith if (ts->ops->view) { 21352b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 21362b0a91c0SBarry Smith } 2137efd4aadfSBarry Smith if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);} 2138efd4aadfSBarry Smith if (ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);} 21392b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 2140e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 2141536b137fSBarry Smith } else if (issaws) { 2142d45a07a7SBarry Smith PetscMPIInt rank; 21432657e9d9SBarry Smith const char *name; 21442657e9d9SBarry Smith 21452657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 2146ffc4695bSBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRMPI(ierr); 2147d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 2148d45a07a7SBarry Smith char dir[1024]; 2149d45a07a7SBarry Smith 2150e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 2151a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 21522657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 21532657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 21542657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 2155d763cef2SBarry Smith } 21560acecf5bSBarry Smith if (ts->ops->view) { 21570acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 21580acecf5bSBarry Smith } 2159f05ece33SBarry Smith #endif 2160f05ece33SBarry Smith } 216136a9e3b9SBarry Smith if (ts->snes && ts->usessnes) { 216236a9e3b9SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 216336a9e3b9SBarry Smith ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr); 216436a9e3b9SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 216536a9e3b9SBarry Smith } 216636a9e3b9SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 216736a9e3b9SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 2168f05ece33SBarry Smith 2169b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 2170251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 2171b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2172d763cef2SBarry Smith PetscFunctionReturn(0); 2173d763cef2SBarry Smith } 2174d763cef2SBarry Smith 2175b07ff414SBarry Smith /*@ 2176d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 2177d763cef2SBarry Smith the timesteppers. 2178d763cef2SBarry Smith 21793f9fe445SBarry Smith Logically Collective on TS 2180d763cef2SBarry Smith 2181d763cef2SBarry Smith Input Parameters: 2182d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2183d763cef2SBarry Smith - usrP - optional user context 2184d763cef2SBarry Smith 218595452b02SPatrick Sanan Fortran Notes: 218695452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 2187daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2188daf670e6SBarry Smith 2189d763cef2SBarry Smith Level: intermediate 2190d763cef2SBarry Smith 2191d763cef2SBarry Smith .seealso: TSGetApplicationContext() 2192d763cef2SBarry Smith @*/ 21937087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 2194d763cef2SBarry Smith { 2195d763cef2SBarry Smith PetscFunctionBegin; 21960700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2197d763cef2SBarry Smith ts->user = usrP; 2198d763cef2SBarry Smith PetscFunctionReturn(0); 2199d763cef2SBarry Smith } 2200d763cef2SBarry Smith 2201b07ff414SBarry Smith /*@ 2202d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 2203d763cef2SBarry Smith timestepper. 2204d763cef2SBarry Smith 2205d763cef2SBarry Smith Not Collective 2206d763cef2SBarry Smith 2207d763cef2SBarry Smith Input Parameter: 2208d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2209d763cef2SBarry Smith 2210d763cef2SBarry Smith Output Parameter: 2211d763cef2SBarry Smith . usrP - user context 2212d763cef2SBarry Smith 221395452b02SPatrick Sanan Fortran Notes: 221495452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 2215daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 2216daf670e6SBarry Smith 2217d763cef2SBarry Smith Level: intermediate 2218d763cef2SBarry Smith 2219d763cef2SBarry Smith .seealso: TSSetApplicationContext() 2220d763cef2SBarry Smith @*/ 2221e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 2222d763cef2SBarry Smith { 2223d763cef2SBarry Smith PetscFunctionBegin; 22240700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2225e71120c6SJed Brown *(void**)usrP = ts->user; 2226d763cef2SBarry Smith PetscFunctionReturn(0); 2227d763cef2SBarry Smith } 2228d763cef2SBarry Smith 2229d763cef2SBarry Smith /*@ 223080275a0aSLisandro Dalcin TSGetStepNumber - Gets the number of steps completed. 2231d763cef2SBarry Smith 2232d763cef2SBarry Smith Not Collective 2233d763cef2SBarry Smith 2234d763cef2SBarry Smith Input Parameter: 2235d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2236d763cef2SBarry Smith 2237d763cef2SBarry Smith Output Parameter: 223880275a0aSLisandro Dalcin . steps - number of steps completed so far 2239d763cef2SBarry Smith 2240d763cef2SBarry Smith Level: intermediate 2241d763cef2SBarry Smith 22429be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 2243d763cef2SBarry Smith @*/ 224480275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps) 2245d763cef2SBarry Smith { 2246d763cef2SBarry Smith PetscFunctionBegin; 22470700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 224880275a0aSLisandro Dalcin PetscValidIntPointer(steps,2); 224980275a0aSLisandro Dalcin *steps = ts->steps; 225080275a0aSLisandro Dalcin PetscFunctionReturn(0); 225180275a0aSLisandro Dalcin } 225280275a0aSLisandro Dalcin 225380275a0aSLisandro Dalcin /*@ 225480275a0aSLisandro Dalcin TSSetStepNumber - Sets the number of steps completed. 225580275a0aSLisandro Dalcin 225680275a0aSLisandro Dalcin Logically Collective on TS 225780275a0aSLisandro Dalcin 225880275a0aSLisandro Dalcin Input Parameters: 225980275a0aSLisandro Dalcin + ts - the TS context 226080275a0aSLisandro Dalcin - steps - number of steps completed so far 226180275a0aSLisandro Dalcin 226280275a0aSLisandro Dalcin Notes: 226380275a0aSLisandro Dalcin For most uses of the TS solvers the user need not explicitly call 226480275a0aSLisandro Dalcin TSSetStepNumber(), as the step counter is appropriately updated in 226580275a0aSLisandro Dalcin TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to 226680275a0aSLisandro Dalcin reinitialize timestepping by setting the step counter to zero (and time 226780275a0aSLisandro Dalcin to the initial time) to solve a similar problem with different initial 226880275a0aSLisandro Dalcin conditions or parameters. Other possible use case is to continue 226980275a0aSLisandro Dalcin timestepping from a previously interrupted run in such a way that TS 227080275a0aSLisandro Dalcin monitors will be called with a initial nonzero step counter. 227180275a0aSLisandro Dalcin 227280275a0aSLisandro Dalcin Level: advanced 227380275a0aSLisandro Dalcin 227480275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution() 227580275a0aSLisandro Dalcin @*/ 227680275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps) 227780275a0aSLisandro Dalcin { 227880275a0aSLisandro Dalcin PetscFunctionBegin; 227980275a0aSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 228080275a0aSLisandro Dalcin PetscValidLogicalCollectiveInt(ts,steps,2); 228180275a0aSLisandro Dalcin if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative"); 228280275a0aSLisandro Dalcin ts->steps = steps; 2283d763cef2SBarry Smith PetscFunctionReturn(0); 2284d763cef2SBarry Smith } 2285d763cef2SBarry Smith 2286d763cef2SBarry Smith /*@ 2287d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 2288d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 2289d763cef2SBarry Smith 22903f9fe445SBarry Smith Logically Collective on TS 2291d763cef2SBarry Smith 2292d763cef2SBarry Smith Input Parameters: 2293d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2294d763cef2SBarry Smith - time_step - the size of the timestep 2295d763cef2SBarry Smith 2296d763cef2SBarry Smith Level: intermediate 2297d763cef2SBarry Smith 2298aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime() 2299d763cef2SBarry Smith 2300d763cef2SBarry Smith @*/ 23017087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 2302d763cef2SBarry Smith { 2303d763cef2SBarry Smith PetscFunctionBegin; 23040700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2305c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 2306d763cef2SBarry Smith ts->time_step = time_step; 2307d763cef2SBarry Smith PetscFunctionReturn(0); 2308d763cef2SBarry Smith } 2309d763cef2SBarry Smith 2310a43b19c4SJed Brown /*@ 231149354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 231249354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 231349354f04SShri Abhyankar or just return at the final time TS computed. 2314a43b19c4SJed Brown 2315a43b19c4SJed Brown Logically Collective on TS 2316a43b19c4SJed Brown 2317d8d19677SJose E. Roman Input Parameters: 2318a43b19c4SJed Brown + ts - the time-step context 231949354f04SShri Abhyankar - eftopt - exact final time option 2320a43b19c4SJed Brown 2321feed9e9dSBarry Smith $ TS_EXACTFINALTIME_STEPOVER - Don't do anything if final time is exceeded 2322feed9e9dSBarry Smith $ TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time 2323feed9e9dSBarry Smith $ TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time 2324feed9e9dSBarry Smith 2325feed9e9dSBarry Smith Options Database: 2326feed9e9dSBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime 2327feed9e9dSBarry Smith 2328ee346746SBarry Smith Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time 2329ee346746SBarry Smith then the final time you selected. 2330ee346746SBarry Smith 2331a43b19c4SJed Brown Level: beginner 2332a43b19c4SJed Brown 2333f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime() 2334a43b19c4SJed Brown @*/ 233549354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 2336a43b19c4SJed Brown { 2337a43b19c4SJed Brown PetscFunctionBegin; 2338a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 233949354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 234049354f04SShri Abhyankar ts->exact_final_time = eftopt; 2341a43b19c4SJed Brown PetscFunctionReturn(0); 2342a43b19c4SJed Brown } 2343a43b19c4SJed Brown 2344d763cef2SBarry Smith /*@ 2345f6953c82SLisandro Dalcin TSGetExactFinalTime - Gets the exact final time option. 2346f6953c82SLisandro Dalcin 2347f6953c82SLisandro Dalcin Not Collective 2348f6953c82SLisandro Dalcin 2349f6953c82SLisandro Dalcin Input Parameter: 2350f6953c82SLisandro Dalcin . ts - the TS context 2351f6953c82SLisandro Dalcin 2352f6953c82SLisandro Dalcin Output Parameter: 2353f6953c82SLisandro Dalcin . eftopt - exact final time option 2354f6953c82SLisandro Dalcin 2355f6953c82SLisandro Dalcin Level: beginner 2356f6953c82SLisandro Dalcin 2357f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime() 2358f6953c82SLisandro Dalcin @*/ 2359f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt) 2360f6953c82SLisandro Dalcin { 2361f6953c82SLisandro Dalcin PetscFunctionBegin; 2362f6953c82SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2363f6953c82SLisandro Dalcin PetscValidPointer(eftopt,2); 2364f6953c82SLisandro Dalcin *eftopt = ts->exact_final_time; 2365f6953c82SLisandro Dalcin PetscFunctionReturn(0); 2366f6953c82SLisandro Dalcin } 2367f6953c82SLisandro Dalcin 2368f6953c82SLisandro Dalcin /*@ 2369d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 2370d763cef2SBarry Smith 2371d763cef2SBarry Smith Not Collective 2372d763cef2SBarry Smith 2373d763cef2SBarry Smith Input Parameter: 2374d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2375d763cef2SBarry Smith 2376d763cef2SBarry Smith Output Parameter: 2377d763cef2SBarry Smith . dt - the current timestep size 2378d763cef2SBarry Smith 2379d763cef2SBarry Smith Level: intermediate 2380d763cef2SBarry Smith 2381aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime() 2382d763cef2SBarry Smith 2383d763cef2SBarry Smith @*/ 23847087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 2385d763cef2SBarry Smith { 2386d763cef2SBarry Smith PetscFunctionBegin; 23870700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2388f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 2389d763cef2SBarry Smith *dt = ts->time_step; 2390d763cef2SBarry Smith PetscFunctionReturn(0); 2391d763cef2SBarry Smith } 2392d763cef2SBarry Smith 2393d8e5e3e6SSatish Balay /*@ 2394d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 2395d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 2396d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 2397d763cef2SBarry Smith the solution at the next timestep has been calculated. 2398d763cef2SBarry Smith 2399d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 2400d763cef2SBarry Smith 2401d763cef2SBarry Smith Input Parameter: 2402d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2403d763cef2SBarry Smith 2404d763cef2SBarry Smith Output Parameter: 2405d763cef2SBarry Smith . v - the vector containing the solution 2406d763cef2SBarry Smith 240763e21af5SBarry Smith Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested 240863e21af5SBarry Smith final time. It returns the solution at the next timestep. 240963e21af5SBarry Smith 2410d763cef2SBarry Smith Level: intermediate 2411d763cef2SBarry Smith 24120ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction() 2413d763cef2SBarry Smith 2414d763cef2SBarry Smith @*/ 24157087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 2416d763cef2SBarry Smith { 2417d763cef2SBarry Smith PetscFunctionBegin; 24180700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 24194482741eSBarry Smith PetscValidPointer(v,2); 24208737fe31SLisandro Dalcin *v = ts->vec_sol; 2421d763cef2SBarry Smith PetscFunctionReturn(0); 2422d763cef2SBarry Smith } 2423d763cef2SBarry Smith 242403fe5f5eSDebojyoti Ghosh /*@ 2425b2bf4f3aSDebojyoti Ghosh TSGetSolutionComponents - Returns any solution components at the present 242603fe5f5eSDebojyoti Ghosh timestep, if available for the time integration method being used. 2427b2bf4f3aSDebojyoti Ghosh Solution components are quantities that share the same size and 242803fe5f5eSDebojyoti Ghosh structure as the solution vector. 242903fe5f5eSDebojyoti Ghosh 243003fe5f5eSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 243103fe5f5eSDebojyoti Ghosh 243203fe5f5eSDebojyoti Ghosh Parameters : 2433a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2434b2bf4f3aSDebojyoti Ghosh . n - If v is PETSC_NULL, then the number of solution components is 2435b2bf4f3aSDebojyoti Ghosh returned through n, else the n-th solution component is 243603fe5f5eSDebojyoti Ghosh returned in v. 2437a2b725a8SWilliam Gropp - v - the vector containing the n-th solution component 243803fe5f5eSDebojyoti Ghosh (may be PETSC_NULL to use this function to find out 2439b2bf4f3aSDebojyoti Ghosh the number of solutions components). 244003fe5f5eSDebojyoti Ghosh 24414cdd57e5SDebojyoti Ghosh Level: advanced 244203fe5f5eSDebojyoti Ghosh 244303fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution() 244403fe5f5eSDebojyoti Ghosh 244503fe5f5eSDebojyoti Ghosh @*/ 2446b2bf4f3aSDebojyoti Ghosh PetscErrorCode TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v) 244703fe5f5eSDebojyoti Ghosh { 244803fe5f5eSDebojyoti Ghosh PetscErrorCode ierr; 244903fe5f5eSDebojyoti Ghosh 245003fe5f5eSDebojyoti Ghosh PetscFunctionBegin; 245103fe5f5eSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2452b2bf4f3aSDebojyoti Ghosh if (!ts->ops->getsolutioncomponents) *n = 0; 245303fe5f5eSDebojyoti Ghosh else { 2454b2bf4f3aSDebojyoti Ghosh ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr); 245503fe5f5eSDebojyoti Ghosh } 245603fe5f5eSDebojyoti Ghosh PetscFunctionReturn(0); 245703fe5f5eSDebojyoti Ghosh } 245803fe5f5eSDebojyoti Ghosh 24594cdd57e5SDebojyoti Ghosh /*@ 24604cdd57e5SDebojyoti Ghosh TSGetAuxSolution - Returns an auxiliary solution at the present 24614cdd57e5SDebojyoti Ghosh timestep, if available for the time integration method being used. 24624cdd57e5SDebojyoti Ghosh 24634cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 24644cdd57e5SDebojyoti Ghosh 24654cdd57e5SDebojyoti Ghosh Parameters : 2466a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2467a2b725a8SWilliam Gropp - v - the vector containing the auxiliary solution 24684cdd57e5SDebojyoti Ghosh 24694cdd57e5SDebojyoti Ghosh Level: intermediate 24704cdd57e5SDebojyoti Ghosh 24714cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution() 24724cdd57e5SDebojyoti Ghosh 24734cdd57e5SDebojyoti Ghosh @*/ 24744cdd57e5SDebojyoti Ghosh PetscErrorCode TSGetAuxSolution(TS ts,Vec *v) 24754cdd57e5SDebojyoti Ghosh { 24764cdd57e5SDebojyoti Ghosh PetscErrorCode ierr; 24774cdd57e5SDebojyoti Ghosh 24784cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 24794cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2480f6356ec7SDebojyoti Ghosh if (ts->ops->getauxsolution) { 24814cdd57e5SDebojyoti Ghosh ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr); 2482f6356ec7SDebojyoti Ghosh } else { 2483f6356ec7SDebojyoti Ghosh ierr = VecZeroEntries(*v);CHKERRQ(ierr); 2484f6356ec7SDebojyoti Ghosh } 24854cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 24864cdd57e5SDebojyoti Ghosh } 24874cdd57e5SDebojyoti Ghosh 24884cdd57e5SDebojyoti Ghosh /*@ 24894cdd57e5SDebojyoti Ghosh TSGetTimeError - Returns the estimated error vector, if the chosen 24904cdd57e5SDebojyoti Ghosh TSType has an error estimation functionality. 24914cdd57e5SDebojyoti Ghosh 24924cdd57e5SDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 24934cdd57e5SDebojyoti Ghosh 24949657682dSDebojyoti Ghosh Note: MUST call after TSSetUp() 24959657682dSDebojyoti Ghosh 24964cdd57e5SDebojyoti Ghosh Parameters : 2497a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2498657c1e31SEmil Constantinescu . n - current estimate (n=0) or previous one (n=-1) 2499a2b725a8SWilliam Gropp - v - the vector containing the error (same size as the solution). 25004cdd57e5SDebojyoti Ghosh 25014cdd57e5SDebojyoti Ghosh Level: intermediate 25024cdd57e5SDebojyoti Ghosh 250357df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError() 25044cdd57e5SDebojyoti Ghosh 25054cdd57e5SDebojyoti Ghosh @*/ 25060a01e1b2SEmil Constantinescu PetscErrorCode TSGetTimeError(TS ts,PetscInt n,Vec *v) 25074cdd57e5SDebojyoti Ghosh { 25084cdd57e5SDebojyoti Ghosh PetscErrorCode ierr; 25094cdd57e5SDebojyoti Ghosh 25104cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 25114cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2512f6356ec7SDebojyoti Ghosh if (ts->ops->gettimeerror) { 25130a01e1b2SEmil Constantinescu ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr); 2514f6356ec7SDebojyoti Ghosh } else { 2515f6356ec7SDebojyoti Ghosh ierr = VecZeroEntries(*v);CHKERRQ(ierr); 2516f6356ec7SDebojyoti Ghosh } 25174cdd57e5SDebojyoti Ghosh PetscFunctionReturn(0); 25184cdd57e5SDebojyoti Ghosh } 25194cdd57e5SDebojyoti Ghosh 252057df6a1bSDebojyoti Ghosh /*@ 252157df6a1bSDebojyoti Ghosh TSSetTimeError - Sets the estimated error vector, if the chosen 252257df6a1bSDebojyoti Ghosh TSType has an error estimation functionality. This can be used 252357df6a1bSDebojyoti Ghosh to restart such a time integrator with a given error vector. 252457df6a1bSDebojyoti Ghosh 252557df6a1bSDebojyoti Ghosh Not Collective, but Vec returned is parallel if TS is parallel 252657df6a1bSDebojyoti Ghosh 252757df6a1bSDebojyoti Ghosh Parameters : 2528a2b725a8SWilliam Gropp + ts - the TS context obtained from TSCreate() (input parameter). 2529a2b725a8SWilliam Gropp - v - the vector containing the error (same size as the solution). 253057df6a1bSDebojyoti Ghosh 253157df6a1bSDebojyoti Ghosh Level: intermediate 253257df6a1bSDebojyoti Ghosh 253357df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError) 253457df6a1bSDebojyoti Ghosh 253557df6a1bSDebojyoti Ghosh @*/ 253657df6a1bSDebojyoti Ghosh PetscErrorCode TSSetTimeError(TS ts,Vec v) 253757df6a1bSDebojyoti Ghosh { 253857df6a1bSDebojyoti Ghosh PetscErrorCode ierr; 253957df6a1bSDebojyoti Ghosh 254057df6a1bSDebojyoti Ghosh PetscFunctionBegin; 254157df6a1bSDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 25429657682dSDebojyoti Ghosh if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first"); 254357df6a1bSDebojyoti Ghosh if (ts->ops->settimeerror) { 254457df6a1bSDebojyoti Ghosh ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr); 254557df6a1bSDebojyoti Ghosh } 254657df6a1bSDebojyoti Ghosh PetscFunctionReturn(0); 254757df6a1bSDebojyoti Ghosh } 254857df6a1bSDebojyoti Ghosh 2549bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 2550d8e5e3e6SSatish Balay /*@ 2551bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 2552d763cef2SBarry Smith 2553bdad233fSMatthew Knepley Not collective 2554d763cef2SBarry Smith 2555bdad233fSMatthew Knepley Input Parameters: 2556bdad233fSMatthew Knepley + ts - The TS 2557bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2558d763cef2SBarry Smith .vb 25590910c330SBarry Smith U_t - A U = 0 (linear) 25600910c330SBarry Smith U_t - A(t) U = 0 (linear) 25610910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 2562d763cef2SBarry Smith .ve 2563d763cef2SBarry Smith 2564d763cef2SBarry Smith Level: beginner 2565d763cef2SBarry Smith 2566bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2567d763cef2SBarry Smith @*/ 25687087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 2569a7cc72afSBarry Smith { 25709e2a6581SJed Brown PetscErrorCode ierr; 25719e2a6581SJed Brown 2572d763cef2SBarry Smith PetscFunctionBegin; 25730700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2574bdad233fSMatthew Knepley ts->problem_type = type; 25759e2a6581SJed Brown if (type == TS_LINEAR) { 25769e2a6581SJed Brown SNES snes; 25779e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 25789e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 25799e2a6581SJed Brown } 2580d763cef2SBarry Smith PetscFunctionReturn(0); 2581d763cef2SBarry Smith } 2582d763cef2SBarry Smith 2583bdad233fSMatthew Knepley /*@C 2584bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 2585bdad233fSMatthew Knepley 2586bdad233fSMatthew Knepley Not collective 2587bdad233fSMatthew Knepley 2588bdad233fSMatthew Knepley Input Parameter: 2589bdad233fSMatthew Knepley . ts - The TS 2590bdad233fSMatthew Knepley 2591bdad233fSMatthew Knepley Output Parameter: 2592bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 2593bdad233fSMatthew Knepley .vb 2594089b2837SJed Brown M U_t = A U 2595089b2837SJed Brown M(t) U_t = A(t) U 2596b5abc632SBarry Smith F(t,U,U_t) 2597bdad233fSMatthew Knepley .ve 2598bdad233fSMatthew Knepley 2599bdad233fSMatthew Knepley Level: beginner 2600bdad233fSMatthew Knepley 2601bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 2602bdad233fSMatthew Knepley @*/ 26037087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 2604a7cc72afSBarry Smith { 2605bdad233fSMatthew Knepley PetscFunctionBegin; 26060700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 26074482741eSBarry Smith PetscValidIntPointer(type,2); 2608bdad233fSMatthew Knepley *type = ts->problem_type; 2609bdad233fSMatthew Knepley PetscFunctionReturn(0); 2610bdad233fSMatthew Knepley } 2611d763cef2SBarry Smith 2612303a5415SBarry Smith /* 2613303a5415SBarry 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() 2614303a5415SBarry Smith */ 2615303a5415SBarry Smith static PetscErrorCode TSSetExactFinalTimeDefault(TS ts) 2616303a5415SBarry Smith { 2617303a5415SBarry Smith PetscErrorCode ierr; 2618303a5415SBarry Smith PetscBool isnone; 2619303a5415SBarry Smith 2620303a5415SBarry Smith PetscFunctionBegin; 2621303a5415SBarry Smith ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 2622303a5415SBarry Smith ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr); 2623303a5415SBarry Smith 2624303a5415SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr); 2625303a5415SBarry Smith if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) { 2626303a5415SBarry Smith ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP; 2627303a5415SBarry Smith } else if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) { 2628303a5415SBarry Smith ts->exact_final_time = TS_EXACTFINALTIME_INTERPOLATE; 2629303a5415SBarry Smith } 2630303a5415SBarry Smith PetscFunctionReturn(0); 2631303a5415SBarry Smith } 2632303a5415SBarry Smith 2633d763cef2SBarry Smith /*@ 2634303a5415SBarry Smith TSSetUp - Sets up the internal data structures for the later use of a timestepper. 2635d763cef2SBarry Smith 2636d763cef2SBarry Smith Collective on TS 2637d763cef2SBarry Smith 2638d763cef2SBarry Smith Input Parameter: 2639d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2640d763cef2SBarry Smith 2641d763cef2SBarry Smith Notes: 2642d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 2643d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 2644141bd67dSStefano Zampini the call to TSStep() or TSSolve(). However, if one wishes to control this 2645d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 2646141bd67dSStefano Zampini and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve(). 2647d763cef2SBarry Smith 2648d763cef2SBarry Smith Level: advanced 2649d763cef2SBarry Smith 2650141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve() 2651d763cef2SBarry Smith @*/ 26527087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 2653d763cef2SBarry Smith { 2654dfbe8321SBarry Smith PetscErrorCode ierr; 26556c6b9e74SPeter Brune DM dm; 26566c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 2657d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 2658cd11d68dSLisandro Dalcin TSIFunction ifun; 26596c6b9e74SPeter Brune TSIJacobian ijac; 2660efe9872eSLisandro Dalcin TSI2Jacobian i2jac; 26616c6b9e74SPeter Brune TSRHSJacobian rhsjac; 2662d763cef2SBarry Smith 2663d763cef2SBarry Smith PetscFunctionBegin; 26640700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2665277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 2666277b19d0SLisandro Dalcin 26677adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 2668cd11d68dSLisandro Dalcin ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr); 2669cd11d68dSLisandro Dalcin ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr); 2670d763cef2SBarry Smith } 2671277b19d0SLisandro Dalcin 26721a638600SStefano Zampini if (!ts->vec_sol) { 26731a638600SStefano Zampini if (ts->dm) { 26741a638600SStefano Zampini ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 26751a638600SStefano Zampini } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 26761a638600SStefano Zampini } 2677277b19d0SLisandro Dalcin 2678298bade4SHong Zhang if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */ 2679298bade4SHong Zhang ierr = PetscObjectReference((PetscObject)ts->Jacprhs);CHKERRQ(ierr); 2680298bade4SHong Zhang ts->Jacp = ts->Jacprhs; 2681298bade4SHong Zhang } 2682298bade4SHong Zhang 2683cd4cee2dSHong Zhang if (ts->quadraturets) { 2684cd4cee2dSHong Zhang ierr = TSSetUp(ts->quadraturets);CHKERRQ(ierr); 2685ecf68647SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 2686cd4cee2dSHong Zhang ierr = VecDuplicate(ts->quadraturets->vec_sol,&ts->vec_costintegrand);CHKERRQ(ierr); 2687cd4cee2dSHong Zhang } 2688cd4cee2dSHong Zhang 2689971015bcSStefano Zampini ierr = TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL);CHKERRQ(ierr); 2690f23ba4b3SHong Zhang if (rhsjac == TSComputeRHSJacobianConstant) { 2691e1244c69SJed Brown Mat Amat,Pmat; 2692e1244c69SJed Brown SNES snes; 2693e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2694e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 2695e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 2696e1244c69SJed Brown * have displaced the RHS matrix */ 2697971015bcSStefano Zampini if (Amat && Amat == ts->Arhs) { 2698abc0d4abSBarry 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 */ 2699abc0d4abSBarry Smith ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr); 2700e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 2701e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 2702e1244c69SJed Brown } 2703971015bcSStefano Zampini if (Pmat && Pmat == ts->Brhs) { 2704abc0d4abSBarry Smith ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 2705e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 2706e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 2707e1244c69SJed Brown } 2708e1244c69SJed Brown } 27092ffb9264SLisandro Dalcin 27102ffb9264SLisandro Dalcin ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 27112ffb9264SLisandro Dalcin ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr); 27122ffb9264SLisandro Dalcin 2713277b19d0SLisandro Dalcin if (ts->ops->setup) { 2714000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 2715277b19d0SLisandro Dalcin } 2716277b19d0SLisandro Dalcin 2717303a5415SBarry Smith ierr = TSSetExactFinalTimeDefault(ts);CHKERRQ(ierr); 27182ffb9264SLisandro Dalcin 2719a6772fa2SLisandro Dalcin /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 27206c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 27216c6b9e74SPeter Brune */ 27226c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 27230298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 27246c6b9e74SPeter Brune if (!func) { 27256c6b9e74SPeter Brune ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 27266c6b9e74SPeter Brune } 2727a6772fa2SLisandro 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. 27286c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 27296c6b9e74SPeter Brune */ 27300298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 27310298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 2732efe9872eSLisandro Dalcin ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr); 27330298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 2734efe9872eSLisandro Dalcin if (!jac && (ijac || i2jac || rhsjac)) { 27356c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 27366c6b9e74SPeter Brune } 2737c0517034SDebojyoti Ghosh 2738c0517034SDebojyoti Ghosh /* if time integration scheme has a starting method, call it */ 2739c0517034SDebojyoti Ghosh if (ts->ops->startingmethod) { 2740c0517034SDebojyoti Ghosh ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr); 2741c0517034SDebojyoti Ghosh } 2742c0517034SDebojyoti Ghosh 2743277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 2744277b19d0SLisandro Dalcin PetscFunctionReturn(0); 2745277b19d0SLisandro Dalcin } 2746277b19d0SLisandro Dalcin 2747f6a906c0SBarry Smith /*@ 2748277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 2749277b19d0SLisandro Dalcin 2750277b19d0SLisandro Dalcin Collective on TS 2751277b19d0SLisandro Dalcin 2752277b19d0SLisandro Dalcin Input Parameter: 2753277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 2754277b19d0SLisandro Dalcin 2755277b19d0SLisandro Dalcin Level: beginner 2756277b19d0SLisandro Dalcin 2757277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 2758277b19d0SLisandro Dalcin @*/ 2759277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 2760277b19d0SLisandro Dalcin { 27611d06f6b3SHong Zhang TS_RHSSplitLink ilink = ts->tsrhssplit,next; 2762277b19d0SLisandro Dalcin PetscErrorCode ierr; 2763277b19d0SLisandro Dalcin 2764277b19d0SLisandro Dalcin PetscFunctionBegin; 2765277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2766b18ea86cSHong Zhang 2767277b19d0SLisandro Dalcin if (ts->ops->reset) { 2768277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 2769277b19d0SLisandro Dalcin } 2770277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 2771e27a82bcSLisandro Dalcin if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);} 2772bbd56ea5SKarl Rupp 27734e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 27744e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 2775214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 27766bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2777efe9872eSLisandro Dalcin ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr); 2778e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 2779e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 278038637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 2781bbd56ea5SKarl Rupp 2782cd4cee2dSHong Zhang ierr = MatDestroy(&ts->Jacprhs);CHKERRQ(ierr); 2783ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 2784ecf68647SHong Zhang if (ts->forward_solve) { 2785ecf68647SHong Zhang ierr = TSForwardReset(ts);CHKERRQ(ierr); 2786ecf68647SHong Zhang } 2787cd4cee2dSHong Zhang if (ts->quadraturets) { 2788cd4cee2dSHong Zhang ierr = TSReset(ts->quadraturets);CHKERRQ(ierr); 278936eaed60SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 2790cd4cee2dSHong Zhang } 27911d06f6b3SHong Zhang while (ilink) { 27921d06f6b3SHong Zhang next = ilink->next; 27931d06f6b3SHong Zhang ierr = TSDestroy(&ilink->ts);CHKERRQ(ierr); 27941d06f6b3SHong Zhang ierr = PetscFree(ilink->splitname);CHKERRQ(ierr); 27951d06f6b3SHong Zhang ierr = ISDestroy(&ilink->is);CHKERRQ(ierr); 27961d06f6b3SHong Zhang ierr = PetscFree(ilink);CHKERRQ(ierr); 27971d06f6b3SHong Zhang ilink = next; 279887f4e208SHong Zhang } 2799545aaa6fSHong Zhang ts->num_rhs_splits = 0; 2800277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 2801d763cef2SBarry Smith PetscFunctionReturn(0); 2802d763cef2SBarry Smith } 2803d763cef2SBarry Smith 28041fb7b255SJunchao Zhang /*@C 2805d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2806d763cef2SBarry Smith with TSCreate(). 2807d763cef2SBarry Smith 2808d763cef2SBarry Smith Collective on TS 2809d763cef2SBarry Smith 2810d763cef2SBarry Smith Input Parameter: 2811d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2812d763cef2SBarry Smith 2813d763cef2SBarry Smith Level: beginner 2814d763cef2SBarry Smith 2815d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 2816d763cef2SBarry Smith @*/ 28176bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2818d763cef2SBarry Smith { 28196849ba73SBarry Smith PetscErrorCode ierr; 2820d763cef2SBarry Smith 2821d763cef2SBarry Smith PetscFunctionBegin; 28226bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 2823ecf68647SHong Zhang PetscValidHeaderSpecific(*ts,TS_CLASSID,1); 2824c793f718SLisandro Dalcin if (--((PetscObject)(*ts))->refct > 0) {*ts = NULL; PetscFunctionReturn(0);} 2825d763cef2SBarry Smith 2826ecf68647SHong Zhang ierr = TSReset(*ts);CHKERRQ(ierr); 2827ecf68647SHong Zhang ierr = TSAdjointReset(*ts);CHKERRQ(ierr); 2828ecf68647SHong Zhang if ((*ts)->forward_solve) { 2829ecf68647SHong Zhang ierr = TSForwardReset(*ts);CHKERRQ(ierr); 2830ecf68647SHong Zhang } 2831e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 2832e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 28336bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 28346d4c513bSLisandro Dalcin 2835bc952696SBarry Smith ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr); 2836bc952696SBarry Smith 283784df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 28386427ac75SLisandro Dalcin ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr); 28396427ac75SLisandro Dalcin 28406bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 28416bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 28426bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 28430dd9f2efSHong Zhang ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr); 28446d4c513bSLisandro Dalcin 2845cd4cee2dSHong Zhang ierr = TSDestroy(&(*ts)->quadraturets);CHKERRQ(ierr); 2846a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 2847d763cef2SBarry Smith PetscFunctionReturn(0); 2848d763cef2SBarry Smith } 2849d763cef2SBarry Smith 2850d8e5e3e6SSatish Balay /*@ 2851d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2852d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2853d763cef2SBarry Smith 2854d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2855d763cef2SBarry Smith 2856d763cef2SBarry Smith Input Parameter: 2857d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2858d763cef2SBarry Smith 2859d763cef2SBarry Smith Output Parameter: 2860d763cef2SBarry Smith . snes - the nonlinear solver context 2861d763cef2SBarry Smith 2862d763cef2SBarry Smith Notes: 2863d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2864d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 286594b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2866d763cef2SBarry Smith 2867d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 28680298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2869d763cef2SBarry Smith 2870d763cef2SBarry Smith Level: beginner 2871d763cef2SBarry Smith 2872d763cef2SBarry Smith @*/ 28737087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2874d763cef2SBarry Smith { 2875d372ba47SLisandro Dalcin PetscErrorCode ierr; 2876d372ba47SLisandro Dalcin 2877d763cef2SBarry Smith PetscFunctionBegin; 28780700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 28794482741eSBarry Smith PetscValidPointer(snes,2); 2880d372ba47SLisandro Dalcin if (!ts->snes) { 2881ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 288216413a6aSBarry Smith ierr = PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options);CHKERRQ(ierr); 28830298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 28843bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 2885d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 2886496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 28879e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 28889e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 28899e2a6581SJed Brown } 2890d372ba47SLisandro Dalcin } 2891d763cef2SBarry Smith *snes = ts->snes; 2892d763cef2SBarry Smith PetscFunctionReturn(0); 2893d763cef2SBarry Smith } 2894d763cef2SBarry Smith 2895deb2cd25SJed Brown /*@ 2896deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2897deb2cd25SJed Brown 2898deb2cd25SJed Brown Collective 2899deb2cd25SJed Brown 2900d8d19677SJose E. Roman Input Parameters: 2901deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2902deb2cd25SJed Brown - snes - the nonlinear solver context 2903deb2cd25SJed Brown 2904deb2cd25SJed Brown Notes: 2905deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2906deb2cd25SJed Brown 2907deb2cd25SJed Brown Level: developer 2908deb2cd25SJed Brown 2909deb2cd25SJed Brown @*/ 2910deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2911deb2cd25SJed Brown { 2912deb2cd25SJed Brown PetscErrorCode ierr; 2913d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2914deb2cd25SJed Brown 2915deb2cd25SJed Brown PetscFunctionBegin; 2916deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2917deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2918deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2919deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2920bbd56ea5SKarl Rupp 2921deb2cd25SJed Brown ts->snes = snes; 2922bbd56ea5SKarl Rupp 29230298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 29240298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2925740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 29260298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2927740132f1SEmil Constantinescu } 2928deb2cd25SJed Brown PetscFunctionReturn(0); 2929deb2cd25SJed Brown } 2930deb2cd25SJed Brown 2931d8e5e3e6SSatish Balay /*@ 293294b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2933d763cef2SBarry Smith a TS (timestepper) context. 2934d763cef2SBarry Smith 293594b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2936d763cef2SBarry Smith 2937d763cef2SBarry Smith Input Parameter: 2938d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2939d763cef2SBarry Smith 2940d763cef2SBarry Smith Output Parameter: 294194b7f48cSBarry Smith . ksp - the nonlinear solver context 2942d763cef2SBarry Smith 2943d763cef2SBarry Smith Notes: 294494b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2945d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2946d763cef2SBarry Smith KSP and PC contexts as well. 2947d763cef2SBarry Smith 294894b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 29490298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2950d763cef2SBarry Smith 2951d763cef2SBarry Smith Level: beginner 2952d763cef2SBarry Smith 2953d763cef2SBarry Smith @*/ 29547087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2955d763cef2SBarry Smith { 2956d372ba47SLisandro Dalcin PetscErrorCode ierr; 2957089b2837SJed Brown SNES snes; 2958d372ba47SLisandro Dalcin 2959d763cef2SBarry Smith PetscFunctionBegin; 29600700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 29614482741eSBarry Smith PetscValidPointer(ksp,2); 296217186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2963e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2964089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2965089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2966d763cef2SBarry Smith PetscFunctionReturn(0); 2967d763cef2SBarry Smith } 2968d763cef2SBarry Smith 2969d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2970d763cef2SBarry Smith 2971adb62b0dSMatthew Knepley /*@ 2972618ce8baSLisandro Dalcin TSSetMaxSteps - Sets the maximum number of steps to use. 2973618ce8baSLisandro Dalcin 2974618ce8baSLisandro Dalcin Logically Collective on TS 2975618ce8baSLisandro Dalcin 2976618ce8baSLisandro Dalcin Input Parameters: 2977618ce8baSLisandro Dalcin + ts - the TS context obtained from TSCreate() 2978618ce8baSLisandro Dalcin - maxsteps - maximum number of steps to use 2979618ce8baSLisandro Dalcin 2980618ce8baSLisandro Dalcin Options Database Keys: 2981618ce8baSLisandro Dalcin . -ts_max_steps <maxsteps> - Sets maxsteps 2982618ce8baSLisandro Dalcin 2983618ce8baSLisandro Dalcin Notes: 2984618ce8baSLisandro Dalcin The default maximum number of steps is 5000 2985618ce8baSLisandro Dalcin 2986618ce8baSLisandro Dalcin Level: intermediate 2987618ce8baSLisandro Dalcin 2988618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime() 2989618ce8baSLisandro Dalcin @*/ 2990618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps) 2991618ce8baSLisandro Dalcin { 2992618ce8baSLisandro Dalcin PetscFunctionBegin; 2993618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2994618ce8baSLisandro Dalcin PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2995618ce8baSLisandro Dalcin if (maxsteps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative"); 2996618ce8baSLisandro Dalcin ts->max_steps = maxsteps; 2997618ce8baSLisandro Dalcin PetscFunctionReturn(0); 2998618ce8baSLisandro Dalcin } 2999618ce8baSLisandro Dalcin 3000618ce8baSLisandro Dalcin /*@ 3001618ce8baSLisandro Dalcin TSGetMaxSteps - Gets the maximum number of steps to use. 3002618ce8baSLisandro Dalcin 3003618ce8baSLisandro Dalcin Not Collective 3004618ce8baSLisandro Dalcin 3005618ce8baSLisandro Dalcin Input Parameters: 3006618ce8baSLisandro Dalcin . ts - the TS context obtained from TSCreate() 3007618ce8baSLisandro Dalcin 3008618ce8baSLisandro Dalcin Output Parameter: 3009618ce8baSLisandro Dalcin . maxsteps - maximum number of steps to use 3010618ce8baSLisandro Dalcin 3011618ce8baSLisandro Dalcin Level: advanced 3012618ce8baSLisandro Dalcin 3013618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime() 3014618ce8baSLisandro Dalcin @*/ 3015618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps) 3016618ce8baSLisandro Dalcin { 3017618ce8baSLisandro Dalcin PetscFunctionBegin; 3018618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3019618ce8baSLisandro Dalcin PetscValidIntPointer(maxsteps,2); 3020618ce8baSLisandro Dalcin *maxsteps = ts->max_steps; 3021618ce8baSLisandro Dalcin PetscFunctionReturn(0); 3022618ce8baSLisandro Dalcin } 3023618ce8baSLisandro Dalcin 3024618ce8baSLisandro Dalcin /*@ 3025618ce8baSLisandro Dalcin TSSetMaxTime - Sets the maximum (or final) time for timestepping. 3026618ce8baSLisandro Dalcin 3027618ce8baSLisandro Dalcin Logically Collective on TS 3028618ce8baSLisandro Dalcin 3029618ce8baSLisandro Dalcin Input Parameters: 3030618ce8baSLisandro Dalcin + ts - the TS context obtained from TSCreate() 3031618ce8baSLisandro Dalcin - maxtime - final time to step to 3032618ce8baSLisandro Dalcin 3033618ce8baSLisandro Dalcin Options Database Keys: 3034ef85077eSLisandro Dalcin . -ts_max_time <maxtime> - Sets maxtime 3035618ce8baSLisandro Dalcin 3036618ce8baSLisandro Dalcin Notes: 3037618ce8baSLisandro Dalcin The default maximum time is 5.0 3038618ce8baSLisandro Dalcin 3039618ce8baSLisandro Dalcin Level: intermediate 3040618ce8baSLisandro Dalcin 3041618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime() 3042618ce8baSLisandro Dalcin @*/ 3043618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime) 3044618ce8baSLisandro Dalcin { 3045618ce8baSLisandro Dalcin PetscFunctionBegin; 3046618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3047618ce8baSLisandro Dalcin PetscValidLogicalCollectiveReal(ts,maxtime,2); 3048618ce8baSLisandro Dalcin ts->max_time = maxtime; 3049618ce8baSLisandro Dalcin PetscFunctionReturn(0); 3050618ce8baSLisandro Dalcin } 3051618ce8baSLisandro Dalcin 3052618ce8baSLisandro Dalcin /*@ 3053618ce8baSLisandro Dalcin TSGetMaxTime - Gets the maximum (or final) time for timestepping. 3054618ce8baSLisandro Dalcin 3055618ce8baSLisandro Dalcin Not Collective 3056618ce8baSLisandro Dalcin 3057618ce8baSLisandro Dalcin Input Parameters: 3058618ce8baSLisandro Dalcin . ts - the TS context obtained from TSCreate() 3059618ce8baSLisandro Dalcin 3060618ce8baSLisandro Dalcin Output Parameter: 3061618ce8baSLisandro Dalcin . maxtime - final time to step to 3062618ce8baSLisandro Dalcin 3063618ce8baSLisandro Dalcin Level: advanced 3064618ce8baSLisandro Dalcin 3065618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps() 3066618ce8baSLisandro Dalcin @*/ 3067618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime) 3068618ce8baSLisandro Dalcin { 3069618ce8baSLisandro Dalcin PetscFunctionBegin; 3070618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3071618ce8baSLisandro Dalcin PetscValidRealPointer(maxtime,2); 3072618ce8baSLisandro Dalcin *maxtime = ts->max_time; 3073618ce8baSLisandro Dalcin PetscFunctionReturn(0); 3074618ce8baSLisandro Dalcin } 3075618ce8baSLisandro Dalcin 3076618ce8baSLisandro Dalcin /*@ 3077aaa6c58dSLisandro Dalcin TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep(). 3078edc382c3SSatish Balay 3079edc382c3SSatish Balay Level: deprecated 3080edc382c3SSatish Balay 3081aaa6c58dSLisandro Dalcin @*/ 3082aaa6c58dSLisandro Dalcin PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 3083aaa6c58dSLisandro Dalcin { 3084aaa6c58dSLisandro Dalcin PetscErrorCode ierr; 3085aaa6c58dSLisandro Dalcin PetscFunctionBegin; 3086aaa6c58dSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3087aaa6c58dSLisandro Dalcin ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 3088aaa6c58dSLisandro Dalcin ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 3089aaa6c58dSLisandro Dalcin PetscFunctionReturn(0); 3090aaa6c58dSLisandro Dalcin } 3091aaa6c58dSLisandro Dalcin 3092aaa6c58dSLisandro Dalcin /*@ 309319eac22cSLisandro Dalcin TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime(). 3094edc382c3SSatish Balay 3095edc382c3SSatish Balay Level: deprecated 3096edc382c3SSatish Balay 3097adb62b0dSMatthew Knepley @*/ 30987087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 3099adb62b0dSMatthew Knepley { 3100adb62b0dSMatthew Knepley PetscFunctionBegin; 31010700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3102abc0a331SBarry Smith if (maxsteps) { 31034482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 3104adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 3105adb62b0dSMatthew Knepley } 3106abc0a331SBarry Smith if (maxtime) { 3107064a246eSJacob Faibussowitsch PetscValidRealPointer(maxtime,3); 3108adb62b0dSMatthew Knepley *maxtime = ts->max_time; 3109adb62b0dSMatthew Knepley } 3110adb62b0dSMatthew Knepley PetscFunctionReturn(0); 3111adb62b0dSMatthew Knepley } 3112adb62b0dSMatthew Knepley 3113d763cef2SBarry Smith /*@ 311419eac22cSLisandro Dalcin TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime(). 3115edc382c3SSatish Balay 3116edc382c3SSatish Balay Level: deprecated 3117edc382c3SSatish Balay 3118d763cef2SBarry Smith @*/ 31197087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 3120d763cef2SBarry Smith { 3121d763cef2SBarry Smith PetscFunctionBegin; 31220700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3123c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 3124064a246eSJacob Faibussowitsch PetscValidLogicalCollectiveReal(ts,maxtime,3); 312539b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 312639b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 3127d763cef2SBarry Smith PetscFunctionReturn(0); 3128d763cef2SBarry Smith } 3129d763cef2SBarry Smith 3130d763cef2SBarry Smith /*@ 31315c5f5948SLisandro Dalcin TSGetTimeStepNumber - Deprecated, use TSGetStepNumber(). 3132edc382c3SSatish Balay 3133edc382c3SSatish Balay Level: deprecated 3134edc382c3SSatish Balay 31355c5f5948SLisandro Dalcin @*/ 3136e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); } 31375c5f5948SLisandro Dalcin 313819eac22cSLisandro Dalcin /*@ 31394f4e0956SLisandro Dalcin TSGetTotalSteps - Deprecated, use TSGetStepNumber(). 3140edc382c3SSatish Balay 3141edc382c3SSatish Balay Level: deprecated 3142edc382c3SSatish Balay 31434f4e0956SLisandro Dalcin @*/ 31444f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); } 31454f4e0956SLisandro Dalcin 31464f4e0956SLisandro Dalcin /*@ 3147d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 3148d763cef2SBarry Smith for use by the TS routines. 3149d763cef2SBarry Smith 3150d083f849SBarry Smith Logically Collective on TS 3151d763cef2SBarry Smith 3152d763cef2SBarry Smith Input Parameters: 3153d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 31540910c330SBarry Smith - u - the solution vector 3155d763cef2SBarry Smith 3156d763cef2SBarry Smith Level: beginner 3157d763cef2SBarry Smith 31580ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate() 3159d763cef2SBarry Smith @*/ 31600910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 3161d763cef2SBarry Smith { 31628737fe31SLisandro Dalcin PetscErrorCode ierr; 3163496e6a7aSJed Brown DM dm; 31648737fe31SLisandro Dalcin 3165d763cef2SBarry Smith PetscFunctionBegin; 31660700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 31670910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 31680910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 31696bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 31700910c330SBarry Smith ts->vec_sol = u; 3171bbd56ea5SKarl Rupp 3172496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 31730910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 3174d763cef2SBarry Smith PetscFunctionReturn(0); 3175d763cef2SBarry Smith } 3176d763cef2SBarry Smith 3177ac226902SBarry Smith /*@C 3178000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 31793f2090d5SJed Brown called once at the beginning of each time step. 3180000e7ae3SMatthew Knepley 31813f9fe445SBarry Smith Logically Collective on TS 3182000e7ae3SMatthew Knepley 3183000e7ae3SMatthew Knepley Input Parameters: 3184000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3185000e7ae3SMatthew Knepley - func - The function 3186000e7ae3SMatthew Knepley 3187000e7ae3SMatthew Knepley Calling sequence of func: 31886bc98fa9SBarry Smith . PetscErrorCode func (TS ts); 3189000e7ae3SMatthew Knepley 3190000e7ae3SMatthew Knepley Level: intermediate 3191000e7ae3SMatthew Knepley 3192dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep() 3193000e7ae3SMatthew Knepley @*/ 31947087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 3195000e7ae3SMatthew Knepley { 3196000e7ae3SMatthew Knepley PetscFunctionBegin; 31970700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3198ae60f76fSBarry Smith ts->prestep = func; 3199000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3200000e7ae3SMatthew Knepley } 3201000e7ae3SMatthew Knepley 320209ee8438SJed Brown /*@ 32033f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 32043f2090d5SJed Brown 32053f2090d5SJed Brown Collective on TS 32063f2090d5SJed Brown 32073f2090d5SJed Brown Input Parameters: 32083f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 32093f2090d5SJed Brown 32103f2090d5SJed Brown Notes: 32113f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 32123f2090d5SJed Brown so most users would not generally call this routine themselves. 32133f2090d5SJed Brown 32143f2090d5SJed Brown Level: developer 32153f2090d5SJed Brown 32169be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 32173f2090d5SJed Brown @*/ 32187087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 32193f2090d5SJed Brown { 32203f2090d5SJed Brown PetscErrorCode ierr; 32213f2090d5SJed Brown 32223f2090d5SJed Brown PetscFunctionBegin; 32230700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3224ae60f76fSBarry Smith if (ts->prestep) { 32255efd42a4SStefano Zampini Vec U; 32265efd42a4SStefano Zampini PetscObjectState sprev,spost; 32275efd42a4SStefano Zampini 32285efd42a4SStefano Zampini ierr = TSGetSolution(ts,&U);CHKERRQ(ierr); 32295efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr); 3230ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 32315efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr); 3232dcb233daSLisandro Dalcin if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);} 3233312ce896SJed Brown } 32343f2090d5SJed Brown PetscFunctionReturn(0); 32353f2090d5SJed Brown } 32363f2090d5SJed Brown 3237b8123daeSJed Brown /*@C 3238b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 3239b8123daeSJed Brown called once at the beginning of each stage. 3240b8123daeSJed Brown 3241b8123daeSJed Brown Logically Collective on TS 3242b8123daeSJed Brown 3243b8123daeSJed Brown Input Parameters: 3244b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 3245b8123daeSJed Brown - func - The function 3246b8123daeSJed Brown 3247b8123daeSJed Brown Calling sequence of func: 3248b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 3249b8123daeSJed Brown 3250b8123daeSJed Brown Level: intermediate 3251b8123daeSJed Brown 3252b8123daeSJed Brown Note: 3253b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 325480275a0aSLisandro Dalcin The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being 3255b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 3256b8123daeSJed Brown 32579be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3258b8123daeSJed Brown @*/ 3259b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 3260b8123daeSJed Brown { 3261b8123daeSJed Brown PetscFunctionBegin; 3262b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3263ae60f76fSBarry Smith ts->prestage = func; 3264b8123daeSJed Brown PetscFunctionReturn(0); 3265b8123daeSJed Brown } 3266b8123daeSJed Brown 32679be3e283SDebojyoti Ghosh /*@C 32689be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 32699be3e283SDebojyoti Ghosh called once at the end of each stage. 32709be3e283SDebojyoti Ghosh 32719be3e283SDebojyoti Ghosh Logically Collective on TS 32729be3e283SDebojyoti Ghosh 32739be3e283SDebojyoti Ghosh Input Parameters: 32749be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 32759be3e283SDebojyoti Ghosh - func - The function 32769be3e283SDebojyoti Ghosh 32779be3e283SDebojyoti Ghosh Calling sequence of func: 32789be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 32799be3e283SDebojyoti Ghosh 32809be3e283SDebojyoti Ghosh Level: intermediate 32819be3e283SDebojyoti Ghosh 32829be3e283SDebojyoti Ghosh Note: 32839be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 328480275a0aSLisandro Dalcin The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being 32859be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 32869be3e283SDebojyoti Ghosh 32879be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 32889be3e283SDebojyoti Ghosh @*/ 32899be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 32909be3e283SDebojyoti Ghosh { 32919be3e283SDebojyoti Ghosh PetscFunctionBegin; 32929be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 32939be3e283SDebojyoti Ghosh ts->poststage = func; 32949be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 32959be3e283SDebojyoti Ghosh } 32969be3e283SDebojyoti Ghosh 3297c688d042SShri Abhyankar /*@C 3298c688d042SShri Abhyankar TSSetPostEvaluate - Sets the general-purpose function 3299c688d042SShri Abhyankar called once at the end of each step evaluation. 3300c688d042SShri Abhyankar 3301c688d042SShri Abhyankar Logically Collective on TS 3302c688d042SShri Abhyankar 3303c688d042SShri Abhyankar Input Parameters: 3304c688d042SShri Abhyankar + ts - The TS context obtained from TSCreate() 3305c688d042SShri Abhyankar - func - The function 3306c688d042SShri Abhyankar 3307c688d042SShri Abhyankar Calling sequence of func: 3308c688d042SShri Abhyankar . PetscErrorCode func(TS ts); 3309c688d042SShri Abhyankar 3310c688d042SShri Abhyankar Level: intermediate 3311c688d042SShri Abhyankar 3312c688d042SShri Abhyankar Note: 33131785ff2aSShri Abhyankar Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling 33141785ff2aSShri Abhyankar thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep() 3315e7e94ed4SShri Abhyankar may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step 3316e7e94ed4SShri Abhyankar solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step 3317e7e94ed4SShri Abhyankar with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime() 3318c688d042SShri Abhyankar 3319c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 3320c688d042SShri Abhyankar @*/ 3321c688d042SShri Abhyankar PetscErrorCode TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS)) 3322c688d042SShri Abhyankar { 3323c688d042SShri Abhyankar PetscFunctionBegin; 3324c688d042SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3325c688d042SShri Abhyankar ts->postevaluate = func; 3326c688d042SShri Abhyankar PetscFunctionReturn(0); 3327c688d042SShri Abhyankar } 3328c688d042SShri Abhyankar 3329b8123daeSJed Brown /*@ 3330b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 3331b8123daeSJed Brown 3332b8123daeSJed Brown Collective on TS 3333b8123daeSJed Brown 3334b8123daeSJed Brown Input Parameters: 3335b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 33369be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 3337b8123daeSJed Brown 3338b8123daeSJed Brown Notes: 3339b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 3340b8123daeSJed Brown most users would not generally call this routine themselves. 3341b8123daeSJed Brown 3342b8123daeSJed Brown Level: developer 3343b8123daeSJed Brown 33449be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 3345b8123daeSJed Brown @*/ 3346b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 3347b8123daeSJed Brown { 3348b8123daeSJed Brown PetscFunctionBegin; 3349b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3350ae60f76fSBarry Smith if (ts->prestage) { 3351ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 3352b8123daeSJed Brown } 3353b8123daeSJed Brown PetscFunctionReturn(0); 3354b8123daeSJed Brown } 3355b8123daeSJed Brown 33569be3e283SDebojyoti Ghosh /*@ 33579be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 33589be3e283SDebojyoti Ghosh 33599be3e283SDebojyoti Ghosh Collective on TS 33609be3e283SDebojyoti Ghosh 33619be3e283SDebojyoti Ghosh Input Parameters: 33629be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 33639be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 33649be3e283SDebojyoti Ghosh stageindex - Stage number 33659be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 33669be3e283SDebojyoti Ghosh of stages) with the stage solutions 33679be3e283SDebojyoti Ghosh 33689be3e283SDebojyoti Ghosh Notes: 33699be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 33709be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 33719be3e283SDebojyoti Ghosh 33729be3e283SDebojyoti Ghosh Level: developer 33739be3e283SDebojyoti Ghosh 33749be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 33759be3e283SDebojyoti Ghosh @*/ 33769be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 33779be3e283SDebojyoti Ghosh { 33789be3e283SDebojyoti Ghosh PetscFunctionBegin; 33799be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 33804beae5d8SLisandro Dalcin if (ts->poststage) { 33819be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 33829be3e283SDebojyoti Ghosh } 33839be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 33849be3e283SDebojyoti Ghosh } 33859be3e283SDebojyoti Ghosh 3386c688d042SShri Abhyankar /*@ 3387c688d042SShri Abhyankar TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate() 3388c688d042SShri Abhyankar 3389c688d042SShri Abhyankar Collective on TS 3390c688d042SShri Abhyankar 3391c688d042SShri Abhyankar Input Parameters: 3392c688d042SShri Abhyankar . ts - The TS context obtained from TSCreate() 3393c688d042SShri Abhyankar 3394c688d042SShri Abhyankar Notes: 3395c688d042SShri Abhyankar TSPostEvaluate() is typically used within time stepping implementations, 3396c688d042SShri Abhyankar most users would not generally call this routine themselves. 3397c688d042SShri Abhyankar 3398c688d042SShri Abhyankar Level: developer 3399c688d042SShri Abhyankar 3400c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep() 3401c688d042SShri Abhyankar @*/ 3402c688d042SShri Abhyankar PetscErrorCode TSPostEvaluate(TS ts) 3403c688d042SShri Abhyankar { 3404c688d042SShri Abhyankar PetscErrorCode ierr; 3405c688d042SShri Abhyankar 3406c688d042SShri Abhyankar PetscFunctionBegin; 3407c688d042SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3408c688d042SShri Abhyankar if (ts->postevaluate) { 3409dcb233daSLisandro Dalcin Vec U; 3410dcb233daSLisandro Dalcin PetscObjectState sprev,spost; 3411dcb233daSLisandro Dalcin 3412dcb233daSLisandro Dalcin ierr = TSGetSolution(ts,&U);CHKERRQ(ierr); 3413dcb233daSLisandro Dalcin ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr); 3414c688d042SShri Abhyankar PetscStackCallStandard((*ts->postevaluate),(ts)); 3415dcb233daSLisandro Dalcin ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr); 3416dcb233daSLisandro Dalcin if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);} 3417c688d042SShri Abhyankar } 3418c688d042SShri Abhyankar PetscFunctionReturn(0); 3419c688d042SShri Abhyankar } 3420c688d042SShri Abhyankar 3421ac226902SBarry Smith /*@C 3422000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 34233f2090d5SJed Brown called once at the end of each time step. 3424000e7ae3SMatthew Knepley 34253f9fe445SBarry Smith Logically Collective on TS 3426000e7ae3SMatthew Knepley 3427000e7ae3SMatthew Knepley Input Parameters: 3428000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 3429000e7ae3SMatthew Knepley - func - The function 3430000e7ae3SMatthew Knepley 3431000e7ae3SMatthew Knepley Calling sequence of func: 3432b8123daeSJed Brown $ func (TS ts); 3433000e7ae3SMatthew Knepley 34341785ff2aSShri Abhyankar Notes: 34351785ff2aSShri Abhyankar The function set by TSSetPostStep() is called after each successful step. The solution vector X 34361785ff2aSShri Abhyankar obtained by TSGetSolution() may be different than that computed at the step end if the event handler 34371785ff2aSShri Abhyankar locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead. 34381785ff2aSShri Abhyankar 3439000e7ae3SMatthew Knepley Level: intermediate 3440000e7ae3SMatthew Knepley 3441dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep() 3442000e7ae3SMatthew Knepley @*/ 34437087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 3444000e7ae3SMatthew Knepley { 3445000e7ae3SMatthew Knepley PetscFunctionBegin; 34460700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3447ae60f76fSBarry Smith ts->poststep = func; 3448000e7ae3SMatthew Knepley PetscFunctionReturn(0); 3449000e7ae3SMatthew Knepley } 3450000e7ae3SMatthew Knepley 345109ee8438SJed Brown /*@ 34523f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 34533f2090d5SJed Brown 34543f2090d5SJed Brown Collective on TS 34553f2090d5SJed Brown 34563f2090d5SJed Brown Input Parameters: 34573f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 34583f2090d5SJed Brown 34593f2090d5SJed Brown Notes: 34603f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 34613f2090d5SJed Brown so most users would not generally call this routine themselves. 34623f2090d5SJed Brown 34633f2090d5SJed Brown Level: developer 34643f2090d5SJed Brown 34653f2090d5SJed Brown @*/ 34667087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 34673f2090d5SJed Brown { 34683f2090d5SJed Brown PetscErrorCode ierr; 34693f2090d5SJed Brown 34703f2090d5SJed Brown PetscFunctionBegin; 34710700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3472ae60f76fSBarry Smith if (ts->poststep) { 34735efd42a4SStefano Zampini Vec U; 34745efd42a4SStefano Zampini PetscObjectState sprev,spost; 34755efd42a4SStefano Zampini 34765efd42a4SStefano Zampini ierr = TSGetSolution(ts,&U);CHKERRQ(ierr); 34775efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr); 3478ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 34795efd42a4SStefano Zampini ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr); 3480dcb233daSLisandro Dalcin if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);} 348172ac3e02SJed Brown } 34823f2090d5SJed Brown PetscFunctionReturn(0); 34833f2090d5SJed Brown } 34843f2090d5SJed Brown 3485cd652676SJed Brown /*@ 3486cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3487cd652676SJed Brown 3488cd652676SJed Brown Collective on TS 3489cd652676SJed Brown 3490*4165533cSJose E. Roman Input Parameters: 3491cd652676SJed Brown + ts - time stepping context 3492cd652676SJed Brown - t - time to interpolate to 3493cd652676SJed Brown 3494*4165533cSJose E. Roman Output Parameter: 34950910c330SBarry Smith . U - state at given time 3496cd652676SJed Brown 3497cd652676SJed Brown Level: intermediate 3498cd652676SJed Brown 3499cd652676SJed Brown Developer Notes: 3500cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3501cd652676SJed Brown 3502874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve() 3503cd652676SJed Brown @*/ 35040910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3505cd652676SJed Brown { 3506cd652676SJed Brown PetscErrorCode ierr; 3507cd652676SJed Brown 3508cd652676SJed Brown PetscFunctionBegin; 3509cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3510b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3511be5899b3SLisandro Dalcin if (t < ts->ptime_prev || t > ts->ptime) SETERRQ3(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); 3512ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 35130910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 3514cd652676SJed Brown PetscFunctionReturn(0); 3515cd652676SJed Brown } 3516cd652676SJed Brown 3517d763cef2SBarry Smith /*@ 35186d9e5789SSean Farley TSStep - Steps one time step 3519d763cef2SBarry Smith 3520d763cef2SBarry Smith Collective on TS 3521d763cef2SBarry Smith 3522d763cef2SBarry Smith Input Parameter: 3523d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3524d763cef2SBarry Smith 352527829d71SBarry Smith Level: developer 3526d763cef2SBarry Smith 3527b8123daeSJed Brown Notes: 352827829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 352927829d71SBarry Smith 3530b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3531b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3532b8123daeSJed Brown 353319eac22cSLisandro Dalcin This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the 353419eac22cSLisandro Dalcin time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 353525cb2221SBarry Smith 35369be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 3537d763cef2SBarry Smith @*/ 3538193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3539d763cef2SBarry Smith { 3540dfbe8321SBarry Smith PetscErrorCode ierr; 3541fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3542be5899b3SLisandro Dalcin PetscReal ptime; 3543d763cef2SBarry Smith 3544d763cef2SBarry Smith PetscFunctionBegin; 35450700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3546f1d62c27SHong Zhang ierr = PetscCitationsRegister("@article{tspaper,\n" 3547fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3548f1d62c27SHong Zhang " author = {Abhyankar, Shrirang and Brown, Jed and Constantinescu, Emil and Ghosh, Debojyoti and Smith, Barry F. and Zhang, Hong},\n" 3549f1d62c27SHong Zhang " journal = {arXiv e-preprints},\n" 3550f1d62c27SHong Zhang " eprint = {1806.01437},\n" 3551f1d62c27SHong Zhang " archivePrefix = {arXiv},\n" 3552f1d62c27SHong Zhang " year = {2018}\n}\n",&cite);CHKERRQ(ierr); 3553fffbeea8SBarry Smith 3554d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 355568bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3556d405a339SMatthew Knepley 3557fc8dbba5SLisandro Dalcin if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 3558ef85077eSLisandro Dalcin if (ts->max_time >= PETSC_MAX_REAL && ts->max_steps == PETSC_MAX_INT) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetMaxTime() or TSSetMaxSteps(), or use -ts_max_time <time> or -ts_max_steps <steps>"); 3559a6772fa2SLisandro Dalcin if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetExactFinalTime() or use -ts_exact_final_time <stepover,interpolate,matchstep> before calling TSStep()"); 3560be5899b3SLisandro Dalcin if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && !ts->adapt) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Since TS is not adaptive you cannot use TS_EXACTFINALTIME_MATCHSTEP, suggest TS_EXACTFINALTIME_INTERPOLATE"); 3561a6772fa2SLisandro Dalcin 3562be5899b3SLisandro Dalcin if (!ts->steps) ts->ptime_prev = ts->ptime; 3563be5899b3SLisandro Dalcin ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev; 35642808aa04SLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3565fc8dbba5SLisandro Dalcin 3566d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3567193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 3568d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3569fc8dbba5SLisandro Dalcin 3570fc8dbba5SLisandro Dalcin if (ts->reason >= 0) { 3571be5899b3SLisandro Dalcin ts->ptime_prev = ptime; 35722808aa04SLisandro Dalcin ts->steps++; 3573be5899b3SLisandro Dalcin ts->steprollback = PETSC_FALSE; 357428d5b5d6SLisandro Dalcin ts->steprestart = PETSC_FALSE; 3575d2daff3dSHong Zhang } 3576fc8dbba5SLisandro Dalcin 3577fc8dbba5SLisandro Dalcin if (!ts->reason) { 357808c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 357908c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 358008c7845fSBarry Smith } 3581fc8dbba5SLisandro Dalcin 3582fc8dbba5SLisandro Dalcin if (ts->reason < 0 && ts->errorifstepfailed && ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) SETERRQ1(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]); 3583fc8dbba5SLisandro Dalcin if (ts->reason < 0 && ts->errorifstepfailed) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 358408c7845fSBarry Smith PetscFunctionReturn(0); 358508c7845fSBarry Smith } 358608c7845fSBarry Smith 358708c7845fSBarry Smith /*@ 35887cbde773SLisandro Dalcin TSEvaluateWLTE - Evaluate the weighted local truncation error norm 35897cbde773SLisandro Dalcin at the end of a time step with a given order of accuracy. 35907cbde773SLisandro Dalcin 35917cbde773SLisandro Dalcin Collective on TS 35927cbde773SLisandro Dalcin 3593*4165533cSJose E. Roman Input Parameters: 35947cbde773SLisandro Dalcin + ts - time stepping context 35957cbde773SLisandro Dalcin . wnormtype - norm type, either NORM_2 or NORM_INFINITY 35967cbde773SLisandro Dalcin - order - optional, desired order for the error evaluation or PETSC_DECIDE 35977cbde773SLisandro Dalcin 3598*4165533cSJose E. Roman Output Parameters: 35997cbde773SLisandro Dalcin + order - optional, the actual order of the error evaluation 36007cbde773SLisandro Dalcin - wlte - the weighted local truncation error norm 36017cbde773SLisandro Dalcin 36027cbde773SLisandro Dalcin Level: advanced 36037cbde773SLisandro Dalcin 36047cbde773SLisandro Dalcin Notes: 36057cbde773SLisandro Dalcin If the timestepper cannot evaluate the error in a particular step 36067cbde773SLisandro Dalcin (eg. in the first step or restart steps after event handling), 36077cbde773SLisandro Dalcin this routine returns wlte=-1.0 . 36087cbde773SLisandro Dalcin 36097cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm() 36107cbde773SLisandro Dalcin @*/ 36117cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte) 36127cbde773SLisandro Dalcin { 36137cbde773SLisandro Dalcin PetscErrorCode ierr; 36147cbde773SLisandro Dalcin 36157cbde773SLisandro Dalcin PetscFunctionBegin; 36167cbde773SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 36177cbde773SLisandro Dalcin PetscValidType(ts,1); 3618064a246eSJacob Faibussowitsch PetscValidLogicalCollectiveEnum(ts,wnormtype,2); 36197cbde773SLisandro Dalcin if (order) PetscValidIntPointer(order,3); 36207cbde773SLisandro Dalcin if (order) PetscValidLogicalCollectiveInt(ts,*order,3); 36217cbde773SLisandro Dalcin PetscValidRealPointer(wlte,4); 36227cbde773SLisandro Dalcin if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 36237cbde773SLisandro Dalcin if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name); 36247cbde773SLisandro Dalcin ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr); 36257cbde773SLisandro Dalcin PetscFunctionReturn(0); 36267cbde773SLisandro Dalcin } 36277cbde773SLisandro Dalcin 362805175c85SJed Brown /*@ 362905175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 363005175c85SJed Brown 36311c3436cfSJed Brown Collective on TS 363205175c85SJed Brown 3633*4165533cSJose E. Roman Input Parameters: 36341c3436cfSJed Brown + ts - time stepping context 36351c3436cfSJed Brown . order - desired order of accuracy 36360298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 363705175c85SJed Brown 3638*4165533cSJose E. Roman Output Parameter: 36390910c330SBarry Smith . U - state at the end of the current step 364005175c85SJed Brown 364105175c85SJed Brown Level: advanced 364205175c85SJed Brown 3643108c343cSJed Brown Notes: 3644108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3645108c343cSJed 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. 3646108c343cSJed Brown 36471c3436cfSJed Brown .seealso: TSStep(), TSAdapt 364805175c85SJed Brown @*/ 36490910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 365005175c85SJed Brown { 365105175c85SJed Brown PetscErrorCode ierr; 365205175c85SJed Brown 365305175c85SJed Brown PetscFunctionBegin; 365405175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 365505175c85SJed Brown PetscValidType(ts,1); 36560910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3657ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 36580910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 365905175c85SJed Brown PetscFunctionReturn(0); 366005175c85SJed Brown } 366105175c85SJed Brown 3662aad739acSMatthew G. Knepley /*@C 36632e61be88SMatthew G. Knepley TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping. 3664aad739acSMatthew G. Knepley 3665aad739acSMatthew G. Knepley Not collective 3666aad739acSMatthew G. Knepley 3667*4165533cSJose E. Roman Input Parameter: 3668aad739acSMatthew G. Knepley . ts - time stepping context 3669aad739acSMatthew G. Knepley 3670*4165533cSJose E. Roman Output Parameter: 36712e61be88SMatthew G. Knepley . initConditions - The function which computes an initial condition 3672aad739acSMatthew G. Knepley 3673aad739acSMatthew G. Knepley Level: advanced 3674aad739acSMatthew G. Knepley 3675aad739acSMatthew G. Knepley Notes: 3676aad739acSMatthew G. Knepley The calling sequence for the function is 36772e61be88SMatthew G. Knepley $ initCondition(TS ts, Vec u) 3678aad739acSMatthew G. Knepley $ ts - The timestepping context 36792e61be88SMatthew G. Knepley $ u - The input vector in which the initial condition is stored 3680aad739acSMatthew G. Knepley 36812e61be88SMatthew G. Knepley .seealso: TSSetComputeInitialCondition(), TSComputeInitialCondition() 3682aad739acSMatthew G. Knepley @*/ 36832e61be88SMatthew G. Knepley PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS, Vec)) 3684aad739acSMatthew G. Knepley { 3685aad739acSMatthew G. Knepley PetscFunctionBegin; 3686aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 36872e61be88SMatthew G. Knepley PetscValidPointer(initCondition, 2); 36882e61be88SMatthew G. Knepley *initCondition = ts->ops->initcondition; 3689aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3690aad739acSMatthew G. Knepley } 3691aad739acSMatthew G. Knepley 3692aad739acSMatthew G. Knepley /*@C 36932e61be88SMatthew G. Knepley TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping. 3694aad739acSMatthew G. Knepley 3695aad739acSMatthew G. Knepley Logically collective on ts 3696aad739acSMatthew G. Knepley 3697*4165533cSJose E. Roman Input Parameters: 3698aad739acSMatthew G. Knepley + ts - time stepping context 36992e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition 3700aad739acSMatthew G. Knepley 3701aad739acSMatthew G. Knepley Level: advanced 3702aad739acSMatthew G. Knepley 3703a96d6ef6SBarry Smith Calling sequence for initCondition: 3704a96d6ef6SBarry Smith $ PetscErrorCode initCondition(TS ts, Vec u) 3705a96d6ef6SBarry Smith 3706a96d6ef6SBarry Smith + ts - The timestepping context 3707a96d6ef6SBarry Smith - u - The input vector in which the initial condition is to be stored 3708aad739acSMatthew G. Knepley 37092e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSComputeInitialCondition() 3710aad739acSMatthew G. Knepley @*/ 37112e61be88SMatthew G. Knepley PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS, Vec)) 3712aad739acSMatthew G. Knepley { 3713aad739acSMatthew G. Knepley PetscFunctionBegin; 3714aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 37152e61be88SMatthew G. Knepley PetscValidFunction(initCondition, 2); 37162e61be88SMatthew G. Knepley ts->ops->initcondition = initCondition; 3717aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3718aad739acSMatthew G. Knepley } 3719aad739acSMatthew G. Knepley 3720aad739acSMatthew G. Knepley /*@ 37212e61be88SMatthew G. Knepley TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set. 3722aad739acSMatthew G. Knepley 3723aad739acSMatthew G. Knepley Collective on ts 3724aad739acSMatthew G. Knepley 3725*4165533cSJose E. Roman Input Parameters: 3726aad739acSMatthew G. Knepley + ts - time stepping context 37272e61be88SMatthew G. Knepley - u - The Vec to store the condition in which will be used in TSSolve() 3728aad739acSMatthew G. Knepley 3729aad739acSMatthew G. Knepley Level: advanced 3730aad739acSMatthew G. Knepley 37312e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve() 3732aad739acSMatthew G. Knepley @*/ 37332e61be88SMatthew G. Knepley PetscErrorCode TSComputeInitialCondition(TS ts, Vec u) 3734aad739acSMatthew G. Knepley { 3735aad739acSMatthew G. Knepley PetscErrorCode ierr; 3736aad739acSMatthew G. Knepley 3737aad739acSMatthew G. Knepley PetscFunctionBegin; 3738aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3739aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 37402e61be88SMatthew G. Knepley if (ts->ops->initcondition) {ierr = (*ts->ops->initcondition)(ts, u);CHKERRQ(ierr);} 3741aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3742aad739acSMatthew G. Knepley } 3743aad739acSMatthew G. Knepley 3744aad739acSMatthew G. Knepley /*@C 3745aad739acSMatthew G. Knepley TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping. 3746aad739acSMatthew G. Knepley 3747aad739acSMatthew G. Knepley Not collective 3748aad739acSMatthew G. Knepley 3749*4165533cSJose E. Roman Input Parameter: 3750aad739acSMatthew G. Knepley . ts - time stepping context 3751aad739acSMatthew G. Knepley 3752*4165533cSJose E. Roman Output Parameter: 3753aad739acSMatthew G. Knepley . exactError - The function which computes the solution error 3754aad739acSMatthew G. Knepley 3755aad739acSMatthew G. Knepley Level: advanced 3756aad739acSMatthew G. Knepley 3757a96d6ef6SBarry Smith Calling sequence for exactError: 3758a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u) 3759a96d6ef6SBarry Smith 3760a96d6ef6SBarry Smith + ts - The timestepping context 3761a96d6ef6SBarry Smith . u - The approximate solution vector 3762a96d6ef6SBarry Smith - e - The input vector in which the error is stored 3763aad739acSMatthew G. Knepley 3764aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError() 3765aad739acSMatthew G. Knepley @*/ 3766aad739acSMatthew G. Knepley PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS, Vec, Vec)) 3767aad739acSMatthew G. Knepley { 3768aad739acSMatthew G. Knepley PetscFunctionBegin; 3769aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3770aad739acSMatthew G. Knepley PetscValidPointer(exactError, 2); 3771aad739acSMatthew G. Knepley *exactError = ts->ops->exacterror; 3772aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3773aad739acSMatthew G. Knepley } 3774aad739acSMatthew G. Knepley 3775aad739acSMatthew G. Knepley /*@C 3776aad739acSMatthew G. Knepley TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping. 3777aad739acSMatthew G. Knepley 3778aad739acSMatthew G. Knepley Logically collective on ts 3779aad739acSMatthew G. Knepley 3780*4165533cSJose E. Roman Input Parameters: 3781aad739acSMatthew G. Knepley + ts - time stepping context 3782aad739acSMatthew G. Knepley - exactError - The function which computes the solution error 3783aad739acSMatthew G. Knepley 3784aad739acSMatthew G. Knepley Level: advanced 3785aad739acSMatthew G. Knepley 3786a96d6ef6SBarry Smith Calling sequence for exactError: 3787a96d6ef6SBarry Smith $ PetscErrorCode exactError(TS ts, Vec u) 3788a96d6ef6SBarry Smith 3789a96d6ef6SBarry Smith + ts - The timestepping context 3790a96d6ef6SBarry Smith . u - The approximate solution vector 3791a96d6ef6SBarry Smith - e - The input vector in which the error is stored 3792aad739acSMatthew G. Knepley 3793aad739acSMatthew G. Knepley .seealso: TSGetComputeExactError(), TSComputeExactError() 3794aad739acSMatthew G. Knepley @*/ 3795aad739acSMatthew G. Knepley PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS, Vec, Vec)) 3796aad739acSMatthew G. Knepley { 3797aad739acSMatthew G. Knepley PetscFunctionBegin; 3798aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3799f907fdbfSMatthew G. Knepley PetscValidFunction(exactError, 2); 3800aad739acSMatthew G. Knepley ts->ops->exacterror = exactError; 3801aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3802aad739acSMatthew G. Knepley } 3803aad739acSMatthew G. Knepley 3804aad739acSMatthew G. Knepley /*@ 3805aad739acSMatthew G. Knepley TSComputeExactError - Compute the solution error for the timestepping using the function previously set. 3806aad739acSMatthew G. Knepley 3807aad739acSMatthew G. Knepley Collective on ts 3808aad739acSMatthew G. Knepley 3809*4165533cSJose E. Roman Input Parameters: 3810aad739acSMatthew G. Knepley + ts - time stepping context 3811aad739acSMatthew G. Knepley . u - The approximate solution 3812aad739acSMatthew G. Knepley - e - The Vec used to store the error 3813aad739acSMatthew G. Knepley 3814aad739acSMatthew G. Knepley Level: advanced 3815aad739acSMatthew G. Knepley 38162e61be88SMatthew G. Knepley .seealso: TSGetComputeInitialCondition(), TSSetComputeInitialCondition(), TSSolve() 3817aad739acSMatthew G. Knepley @*/ 3818aad739acSMatthew G. Knepley PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e) 3819aad739acSMatthew G. Knepley { 3820aad739acSMatthew G. Knepley PetscErrorCode ierr; 3821aad739acSMatthew G. Knepley 3822aad739acSMatthew G. Knepley PetscFunctionBegin; 3823aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3824aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3825aad739acSMatthew G. Knepley PetscValidHeaderSpecific(e, VEC_CLASSID, 3); 3826aad739acSMatthew G. Knepley if (ts->ops->exacterror) {ierr = (*ts->ops->exacterror)(ts, u, e);CHKERRQ(ierr);} 3827aad739acSMatthew G. Knepley PetscFunctionReturn(0); 3828aad739acSMatthew G. Knepley } 3829aad739acSMatthew G. Knepley 3830b1cb36f3SHong Zhang /*@ 38316a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 38326a4d4014SLisandro Dalcin 38336a4d4014SLisandro Dalcin Collective on TS 38346a4d4014SLisandro Dalcin 3835d8d19677SJose E. Roman Input Parameters: 38366a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 383763e21af5SBarry Smith - u - the solution vector (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used, 383863e21af5SBarry Smith otherwise must contain the initial conditions and will contain the solution at the final requested time 38395a3a76d0SJed Brown 38406a4d4014SLisandro Dalcin Level: beginner 38416a4d4014SLisandro Dalcin 38425a3a76d0SJed Brown Notes: 38435a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 38445a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 38455a3a76d0SJed Brown stepped over the final time. 38465a3a76d0SJed Brown 384763e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime() 38486a4d4014SLisandro Dalcin @*/ 3849cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 38506a4d4014SLisandro Dalcin { 3851b06615a5SLisandro Dalcin Vec solution; 38526a4d4014SLisandro Dalcin PetscErrorCode ierr; 3853f22f69f0SBarry Smith 38546a4d4014SLisandro Dalcin PetscFunctionBegin; 38550700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3856f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 3857303a5415SBarry Smith 3858303a5415SBarry Smith ierr = TSSetExactFinalTimeDefault(ts);CHKERRQ(ierr); 3859ee41a567SStefano 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 */ 38600910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 3861b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 3862b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 3863b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 38645a3a76d0SJed Brown } 38650910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 3866715f1b00SHong Zhang if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE"); 3867bbd56ea5SKarl Rupp } else if (u) { 38680910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 38695a3a76d0SJed Brown } 3870b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 387168bece0bSHong Zhang ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr); 3872a6772fa2SLisandro Dalcin 3873ef85077eSLisandro Dalcin if (ts->max_time >= PETSC_MAX_REAL && ts->max_steps == PETSC_MAX_INT) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetMaxTime() or TSSetMaxSteps(), or use -ts_max_time <time> or -ts_max_steps <steps>"); 3874a6772fa2SLisandro Dalcin if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetExactFinalTime() or use -ts_exact_final_time <stepover,interpolate,matchstep> before calling TSSolve()"); 3875a6772fa2SLisandro Dalcin if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && !ts->adapt) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Since TS is not adaptive you cannot use TS_EXACTFINALTIME_MATCHSTEP, suggest TS_EXACTFINALTIME_INTERPOLATE"); 3876a6772fa2SLisandro Dalcin 3877715f1b00SHong Zhang if (ts->forward_solve) { 3878715f1b00SHong Zhang ierr = TSForwardSetUp(ts);CHKERRQ(ierr); 3879715f1b00SHong Zhang } 3880715f1b00SHong Zhang 3881e7069c78SShri /* reset number of steps only when the step is not restarted. ARKIMEX 3882715f1b00SHong Zhang restarts the step after an event. Resetting these counters in such case causes 3883e7069c78SShri TSTrajectory to incorrectly save the output files 3884e7069c78SShri */ 3885715f1b00SHong Zhang /* reset time step and iteration counters */ 38862808aa04SLisandro Dalcin if (!ts->steps) { 38875ef26d82SJed Brown ts->ksp_its = 0; 38885ef26d82SJed Brown ts->snes_its = 0; 3889c610991cSLisandro Dalcin ts->num_snes_failures = 0; 3890c610991cSLisandro Dalcin ts->reject = 0; 38912808aa04SLisandro Dalcin ts->steprestart = PETSC_TRUE; 38922808aa04SLisandro Dalcin ts->steprollback = PETSC_FALSE; 38937d51462cSStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 38942808aa04SLisandro Dalcin } 3895e97c63d7SStefano Zampini 3896e97c63d7SStefano Zampini /* make sure initial time step does not overshoot final time */ 3897e97c63d7SStefano Zampini if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) { 3898e97c63d7SStefano Zampini PetscReal maxdt = ts->max_time-ts->ptime; 3899e97c63d7SStefano Zampini PetscReal dt = ts->time_step; 3900e97c63d7SStefano Zampini 3901e97c63d7SStefano Zampini ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt,maxdt,10*PETSC_MACHINE_EPSILON,0) ? maxdt : dt); 3902e97c63d7SStefano Zampini } 3903193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 3904193ac0bcSJed Brown 3905900f6b5bSMatthew G. Knepley { 3906900f6b5bSMatthew G. Knepley PetscViewer viewer; 3907900f6b5bSMatthew G. Knepley PetscViewerFormat format; 3908900f6b5bSMatthew G. Knepley PetscBool flg; 3909900f6b5bSMatthew G. Knepley static PetscBool incall = PETSC_FALSE; 3910900f6b5bSMatthew G. Knepley 3911900f6b5bSMatthew G. Knepley if (!incall) { 3912900f6b5bSMatthew G. Knepley /* Estimate the convergence rate of the time discretization */ 3913900f6b5bSMatthew G. Knepley ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) ts),((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg);CHKERRQ(ierr); 3914900f6b5bSMatthew G. Knepley if (flg) { 3915900f6b5bSMatthew G. Knepley PetscConvEst conv; 3916900f6b5bSMatthew G. Knepley DM dm; 3917900f6b5bSMatthew G. Knepley PetscReal *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */ 3918900f6b5bSMatthew G. Knepley PetscInt Nf; 3919f2ed2dc7SMatthew G. Knepley PetscBool checkTemporal = PETSC_TRUE; 3920900f6b5bSMatthew G. Knepley 3921900f6b5bSMatthew G. Knepley incall = PETSC_TRUE; 3922f2ed2dc7SMatthew G. Knepley ierr = PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject) ts)->prefix, "-ts_convergence_temporal", &checkTemporal, &flg);CHKERRQ(ierr); 3923900f6b5bSMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 3924900f6b5bSMatthew G. Knepley ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr); 3925900f6b5bSMatthew G. Knepley ierr = PetscCalloc1(PetscMax(Nf, 1), &alpha);CHKERRQ(ierr); 3926900f6b5bSMatthew G. Knepley ierr = PetscConvEstCreate(PetscObjectComm((PetscObject) ts), &conv);CHKERRQ(ierr); 3927f2ed2dc7SMatthew G. Knepley ierr = PetscConvEstUseTS(conv, checkTemporal);CHKERRQ(ierr); 3928900f6b5bSMatthew G. Knepley ierr = PetscConvEstSetSolver(conv, (PetscObject) ts);CHKERRQ(ierr); 3929900f6b5bSMatthew G. Knepley ierr = PetscConvEstSetFromOptions(conv);CHKERRQ(ierr); 3930900f6b5bSMatthew G. Knepley ierr = PetscConvEstSetUp(conv);CHKERRQ(ierr); 3931900f6b5bSMatthew G. Knepley ierr = PetscConvEstGetConvRate(conv, alpha);CHKERRQ(ierr); 3932900f6b5bSMatthew G. Knepley ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr); 3933900f6b5bSMatthew G. Knepley ierr = PetscConvEstRateView(conv, alpha, viewer);CHKERRQ(ierr); 3934900f6b5bSMatthew G. Knepley ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 3935900f6b5bSMatthew G. Knepley ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 3936900f6b5bSMatthew G. Knepley ierr = PetscConvEstDestroy(&conv);CHKERRQ(ierr); 3937900f6b5bSMatthew G. Knepley ierr = PetscFree(alpha);CHKERRQ(ierr); 3938900f6b5bSMatthew G. Knepley incall = PETSC_FALSE; 3939900f6b5bSMatthew G. Knepley } 3940900f6b5bSMatthew G. Knepley } 3941900f6b5bSMatthew G. Knepley } 3942900f6b5bSMatthew G. Knepley 3943ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 3944f05ece33SBarry Smith 3945193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 3946193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 3947a6772fa2SLisandro Dalcin if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 3948cc708dedSBarry Smith ts->solvetime = ts->ptime; 3949a6772fa2SLisandro Dalcin solution = ts->vec_sol; 3950be5899b3SLisandro Dalcin } else { /* Step the requested number of timesteps. */ 3951db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3952db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3953e7069c78SShri 39542808aa04SLisandro Dalcin if (!ts->steps) { 39552808aa04SLisandro Dalcin ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 39566427ac75SLisandro Dalcin ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 39572808aa04SLisandro Dalcin } 39586427ac75SLisandro Dalcin 3959e1a7a14fSJed Brown while (!ts->reason) { 39602808aa04SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 39619687d888SLisandro Dalcin if (!ts->steprollback) { 39629687d888SLisandro Dalcin ierr = TSPreStep(ts);CHKERRQ(ierr); 39639687d888SLisandro Dalcin } 3964193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 3965f3b1f45cSBarry Smith if (ts->testjacobian) { 3966f3b1f45cSBarry Smith ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr); 3967f3b1f45cSBarry Smith } 3968f3b1f45cSBarry Smith if (ts->testjacobiantranspose) { 3969f3b1f45cSBarry Smith ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr); 3970f3b1f45cSBarry Smith } 3971cd4cee2dSHong Zhang if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */ 39727b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */ 3973b1cb36f3SHong Zhang ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr); 39747b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps++; 3975b1cb36f3SHong Zhang } 397658818c2dSLisandro Dalcin if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */ 39777b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */ 3978715f1b00SHong Zhang ierr = TSForwardStep(ts);CHKERRQ(ierr); 39797b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps++; 3980715f1b00SHong Zhang } 398110b82f12SShri Abhyankar ierr = TSPostEvaluate(ts);CHKERRQ(ierr); 3982e783b05fSHong Zhang ierr = TSEventHandler(ts);CHKERRQ(ierr); /* The right-hand side may be changed due to event. Be careful with Any computation using the RHS information after this point. */ 398358818c2dSLisandro Dalcin if (ts->steprollback) { 398458818c2dSLisandro Dalcin ierr = TSPostEvaluate(ts);CHKERRQ(ierr); 398558818c2dSLisandro Dalcin } 3986e783b05fSHong Zhang if (!ts->steprollback) { 39872808aa04SLisandro Dalcin ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 39881eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 3989aeb4809dSShri Abhyankar } 3990193ac0bcSJed Brown } 39912808aa04SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 39926427ac75SLisandro Dalcin 399349354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 39940910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 3995cc708dedSBarry Smith ts->solvetime = ts->max_time; 3996b06615a5SLisandro Dalcin solution = u; 399763e21af5SBarry Smith ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr); 39980574a7fbSJed Brown } else { 3999ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 4000cc708dedSBarry Smith ts->solvetime = ts->ptime; 4001b06615a5SLisandro Dalcin solution = ts->vec_sol; 40020574a7fbSJed Brown } 4003193ac0bcSJed Brown } 4004d2daff3dSHong Zhang 4005ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 4006aee7a9fbSMatthew G. Knepley ierr = VecViewFromOptions(solution,(PetscObject)ts,"-ts_view_solution");CHKERRQ(ierr); 400756f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 4008ef222394SBarry Smith if (ts->adjoint_solve) { 4009ef222394SBarry Smith ierr = TSAdjointSolve(ts);CHKERRQ(ierr); 40102b0a91c0SBarry Smith } 40116a4d4014SLisandro Dalcin PetscFunctionReturn(0); 40126a4d4014SLisandro Dalcin } 40136a4d4014SLisandro Dalcin 4014d763cef2SBarry Smith /*@ 4015b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 4016d763cef2SBarry Smith 4017d763cef2SBarry Smith Not Collective 4018d763cef2SBarry Smith 4019d763cef2SBarry Smith Input Parameter: 4020d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 4021d763cef2SBarry Smith 4022d763cef2SBarry Smith Output Parameter: 402319eac22cSLisandro Dalcin . t - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime(). 4024d763cef2SBarry Smith 4025d763cef2SBarry Smith Level: beginner 4026d763cef2SBarry Smith 4027b8123daeSJed Brown Note: 4028b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 40299be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 4030b8123daeSJed Brown 40318f199f4dSPatrick Sanan .seealso: TSGetSolveTime(), TSSetTime(), TSGetTimeStep(), TSGetStepNumber() 4032d763cef2SBarry Smith 4033d763cef2SBarry Smith @*/ 40347087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 4035d763cef2SBarry Smith { 4036d763cef2SBarry Smith PetscFunctionBegin; 40370700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4038f7cf8827SBarry Smith PetscValidRealPointer(t,2); 4039d763cef2SBarry Smith *t = ts->ptime; 4040d763cef2SBarry Smith PetscFunctionReturn(0); 4041d763cef2SBarry Smith } 4042d763cef2SBarry Smith 4043e5e524a1SHong Zhang /*@ 4044e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 4045e5e524a1SHong Zhang 4046e5e524a1SHong Zhang Not Collective 4047e5e524a1SHong Zhang 4048e5e524a1SHong Zhang Input Parameter: 4049e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 4050e5e524a1SHong Zhang 4051e5e524a1SHong Zhang Output Parameter: 4052e5e524a1SHong Zhang . t - the previous time 4053e5e524a1SHong Zhang 4054e5e524a1SHong Zhang Level: beginner 4055e5e524a1SHong Zhang 4056aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep() 4057e5e524a1SHong Zhang 4058e5e524a1SHong Zhang @*/ 4059e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 4060e5e524a1SHong Zhang { 4061e5e524a1SHong Zhang PetscFunctionBegin; 4062e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4063e5e524a1SHong Zhang PetscValidRealPointer(t,2); 4064e5e524a1SHong Zhang *t = ts->ptime_prev; 4065e5e524a1SHong Zhang PetscFunctionReturn(0); 4066e5e524a1SHong Zhang } 4067e5e524a1SHong Zhang 40686a4d4014SLisandro Dalcin /*@ 40696a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 40706a4d4014SLisandro Dalcin 40713f9fe445SBarry Smith Logically Collective on TS 40726a4d4014SLisandro Dalcin 40736a4d4014SLisandro Dalcin Input Parameters: 40746a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 40756a4d4014SLisandro Dalcin - time - the time 40766a4d4014SLisandro Dalcin 40776a4d4014SLisandro Dalcin Level: intermediate 40786a4d4014SLisandro Dalcin 407919eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps() 40806a4d4014SLisandro Dalcin 40816a4d4014SLisandro Dalcin @*/ 40827087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 40836a4d4014SLisandro Dalcin { 40846a4d4014SLisandro Dalcin PetscFunctionBegin; 40850700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4086c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 40876a4d4014SLisandro Dalcin ts->ptime = t; 40886a4d4014SLisandro Dalcin PetscFunctionReturn(0); 40896a4d4014SLisandro Dalcin } 40906a4d4014SLisandro Dalcin 4091d763cef2SBarry Smith /*@C 4092d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 4093d763cef2SBarry Smith TS options in the database. 4094d763cef2SBarry Smith 40953f9fe445SBarry Smith Logically Collective on TS 4096d763cef2SBarry Smith 4097d8d19677SJose E. Roman Input Parameters: 4098d763cef2SBarry Smith + ts - The TS context 4099d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4100d763cef2SBarry Smith 4101d763cef2SBarry Smith Notes: 4102d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4103d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4104d763cef2SBarry Smith hyphen. 4105d763cef2SBarry Smith 4106d763cef2SBarry Smith Level: advanced 4107d763cef2SBarry Smith 4108d763cef2SBarry Smith .seealso: TSSetFromOptions() 4109d763cef2SBarry Smith 4110d763cef2SBarry Smith @*/ 41117087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 4112d763cef2SBarry Smith { 4113dfbe8321SBarry Smith PetscErrorCode ierr; 4114089b2837SJed Brown SNES snes; 4115d763cef2SBarry Smith 4116d763cef2SBarry Smith PetscFunctionBegin; 41170700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4118d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4119089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4120089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4121d763cef2SBarry Smith PetscFunctionReturn(0); 4122d763cef2SBarry Smith } 4123d763cef2SBarry Smith 4124d763cef2SBarry Smith /*@C 4125d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 4126d763cef2SBarry Smith TS options in the database. 4127d763cef2SBarry Smith 41283f9fe445SBarry Smith Logically Collective on TS 4129d763cef2SBarry Smith 4130d8d19677SJose E. Roman Input Parameters: 4131d763cef2SBarry Smith + ts - The TS context 4132d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4133d763cef2SBarry Smith 4134d763cef2SBarry Smith Notes: 4135d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4136d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4137d763cef2SBarry Smith hyphen. 4138d763cef2SBarry Smith 4139d763cef2SBarry Smith Level: advanced 4140d763cef2SBarry Smith 4141d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 4142d763cef2SBarry Smith 4143d763cef2SBarry Smith @*/ 41447087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 4145d763cef2SBarry Smith { 4146dfbe8321SBarry Smith PetscErrorCode ierr; 4147089b2837SJed Brown SNES snes; 4148d763cef2SBarry Smith 4149d763cef2SBarry Smith PetscFunctionBegin; 41500700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4151d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4152089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4153089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 4154d763cef2SBarry Smith PetscFunctionReturn(0); 4155d763cef2SBarry Smith } 4156d763cef2SBarry Smith 4157d763cef2SBarry Smith /*@C 4158d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 4159d763cef2SBarry Smith TS options in the database. 4160d763cef2SBarry Smith 4161d763cef2SBarry Smith Not Collective 4162d763cef2SBarry Smith 4163d763cef2SBarry Smith Input Parameter: 4164d763cef2SBarry Smith . ts - The TS context 4165d763cef2SBarry Smith 4166d763cef2SBarry Smith Output Parameter: 4167d763cef2SBarry Smith . prefix - A pointer to the prefix string used 4168d763cef2SBarry Smith 416995452b02SPatrick Sanan Notes: 417095452b02SPatrick Sanan On the fortran side, the user should pass in a string 'prifix' of 4171d763cef2SBarry Smith sufficient length to hold the prefix. 4172d763cef2SBarry Smith 4173d763cef2SBarry Smith Level: intermediate 4174d763cef2SBarry Smith 4175d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 4176d763cef2SBarry Smith @*/ 41777087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 4178d763cef2SBarry Smith { 4179dfbe8321SBarry Smith PetscErrorCode ierr; 4180d763cef2SBarry Smith 4181d763cef2SBarry Smith PetscFunctionBegin; 41820700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 41834482741eSBarry Smith PetscValidPointer(prefix,2); 4184d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 4185d763cef2SBarry Smith PetscFunctionReturn(0); 4186d763cef2SBarry Smith } 4187d763cef2SBarry Smith 4188d763cef2SBarry Smith /*@C 4189d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4190d763cef2SBarry Smith 4191d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 4192d763cef2SBarry Smith 4193d763cef2SBarry Smith Input Parameter: 4194d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 4195d763cef2SBarry Smith 4196d763cef2SBarry Smith Output Parameters: 4197e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 4198e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 4199e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 4200e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 4201d763cef2SBarry Smith 420295452b02SPatrick Sanan Notes: 420395452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 4204d763cef2SBarry Smith 4205d763cef2SBarry Smith Level: intermediate 4206d763cef2SBarry Smith 420780275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber() 4208d763cef2SBarry Smith 4209d763cef2SBarry Smith @*/ 4210e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 4211d763cef2SBarry Smith { 4212089b2837SJed Brown PetscErrorCode ierr; 421324989b8cSPeter Brune DM dm; 4214089b2837SJed Brown 4215d763cef2SBarry Smith PetscFunctionBegin; 421623a57915SBarry Smith if (Amat || Pmat) { 421723a57915SBarry Smith SNES snes; 4218089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 421923a57915SBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4220e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 422123a57915SBarry Smith } 422224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 422324989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 4224d763cef2SBarry Smith PetscFunctionReturn(0); 4225d763cef2SBarry Smith } 4226d763cef2SBarry Smith 42272eca1d9cSJed Brown /*@C 42282eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 42292eca1d9cSJed Brown 42302eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 42312eca1d9cSJed Brown 42322eca1d9cSJed Brown Input Parameter: 42332eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 42342eca1d9cSJed Brown 42352eca1d9cSJed Brown Output Parameters: 4236e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4237e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 42382eca1d9cSJed Brown . f - The function to compute the matrices 42392eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 42402eca1d9cSJed Brown 424195452b02SPatrick Sanan Notes: 424295452b02SPatrick Sanan You can pass in NULL for any return argument you do not need. 42432eca1d9cSJed Brown 42442eca1d9cSJed Brown Level: advanced 42452eca1d9cSJed Brown 424680275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber() 42472eca1d9cSJed Brown 42482eca1d9cSJed Brown @*/ 4249e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 42502eca1d9cSJed Brown { 4251089b2837SJed Brown PetscErrorCode ierr; 425224989b8cSPeter Brune DM dm; 42530910c330SBarry Smith 42542eca1d9cSJed Brown PetscFunctionBegin; 4255c0aab802Sstefano_zampini if (Amat || Pmat) { 4256c0aab802Sstefano_zampini SNES snes; 4257089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4258f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 4259e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 4260c0aab802Sstefano_zampini } 426124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 426224989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 42632eca1d9cSJed Brown PetscFunctionReturn(0); 42642eca1d9cSJed Brown } 42652eca1d9cSJed Brown 4266af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 42676c699258SBarry Smith /*@ 42682a808120SBarry Smith TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS 42696c699258SBarry Smith 4270d083f849SBarry Smith Logically Collective on ts 42716c699258SBarry Smith 42726c699258SBarry Smith Input Parameters: 42732a808120SBarry Smith + ts - the ODE integrator object 42742a808120SBarry Smith - dm - the dm, cannot be NULL 42756c699258SBarry Smith 4276e03a659cSJed Brown Notes: 4277e03a659cSJed Brown A DM can only be used for solving one problem at a time because information about the problem is stored on the DM, 4278e03a659cSJed Brown even when not using interfaces like DMTSSetIFunction(). Use DMClone() to get a distinct DM when solving 4279e03a659cSJed Brown different problems using the same function space. 4280e03a659cSJed Brown 42816c699258SBarry Smith Level: intermediate 42826c699258SBarry Smith 42836c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 42846c699258SBarry Smith @*/ 42857087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 42866c699258SBarry Smith { 42876c699258SBarry Smith PetscErrorCode ierr; 4288089b2837SJed Brown SNES snes; 4289942e3340SBarry Smith DMTS tsdm; 42906c699258SBarry Smith 42916c699258SBarry Smith PetscFunctionBegin; 42920700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 42932a808120SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,2); 429470663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 4295942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 42962a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 4297942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 4298942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 429924989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 430024989b8cSPeter Brune tsdm->originaldm = dm; 430124989b8cSPeter Brune } 430224989b8cSPeter Brune } 43036bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 430424989b8cSPeter Brune } 43056c699258SBarry Smith ts->dm = dm; 4306bbd56ea5SKarl Rupp 4307089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4308089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 43096c699258SBarry Smith PetscFunctionReturn(0); 43106c699258SBarry Smith } 43116c699258SBarry Smith 43126c699258SBarry Smith /*@ 43136c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 43146c699258SBarry Smith 43153f9fe445SBarry Smith Not Collective 43166c699258SBarry Smith 43176c699258SBarry Smith Input Parameter: 43186c699258SBarry Smith . ts - the preconditioner context 43196c699258SBarry Smith 43206c699258SBarry Smith Output Parameter: 43216c699258SBarry Smith . dm - the dm 43226c699258SBarry Smith 43236c699258SBarry Smith Level: intermediate 43246c699258SBarry Smith 43256c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 43266c699258SBarry Smith @*/ 43277087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 43286c699258SBarry Smith { 4329496e6a7aSJed Brown PetscErrorCode ierr; 4330496e6a7aSJed Brown 43316c699258SBarry Smith PetscFunctionBegin; 43320700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4333496e6a7aSJed Brown if (!ts->dm) { 4334ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 4335496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 4336496e6a7aSJed Brown } 43376c699258SBarry Smith *dm = ts->dm; 43386c699258SBarry Smith PetscFunctionReturn(0); 43396c699258SBarry Smith } 43401713a123SBarry Smith 43410f5c6efeSJed Brown /*@ 43420f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 43430f5c6efeSJed Brown 43443f9fe445SBarry Smith Logically Collective on SNES 43450f5c6efeSJed Brown 4346d8d19677SJose E. Roman Input Parameters: 4347d42a1c89SJed Brown + snes - nonlinear solver 43480910c330SBarry Smith . U - the current state at which to evaluate the residual 4349d42a1c89SJed Brown - ctx - user context, must be a TS 43500f5c6efeSJed Brown 43510f5c6efeSJed Brown Output Parameter: 43520f5c6efeSJed Brown . F - the nonlinear residual 43530f5c6efeSJed Brown 43540f5c6efeSJed Brown Notes: 43550f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 43560f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 43570f5c6efeSJed Brown 43580f5c6efeSJed Brown Level: advanced 43590f5c6efeSJed Brown 43600f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 43610f5c6efeSJed Brown @*/ 43620910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 43630f5c6efeSJed Brown { 43640f5c6efeSJed Brown TS ts = (TS)ctx; 43650f5c6efeSJed Brown PetscErrorCode ierr; 43660f5c6efeSJed Brown 43670f5c6efeSJed Brown PetscFunctionBegin; 43680f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 43690910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 43700f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 43710f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 43720910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 43730f5c6efeSJed Brown PetscFunctionReturn(0); 43740f5c6efeSJed Brown } 43750f5c6efeSJed Brown 43760f5c6efeSJed Brown /*@ 43770f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 43780f5c6efeSJed Brown 43790f5c6efeSJed Brown Collective on SNES 43800f5c6efeSJed Brown 4381d8d19677SJose E. Roman Input Parameters: 43820f5c6efeSJed Brown + snes - nonlinear solver 43830910c330SBarry Smith . U - the current state at which to evaluate the residual 43840f5c6efeSJed Brown - ctx - user context, must be a TS 43850f5c6efeSJed Brown 4386d8d19677SJose E. Roman Output Parameters: 43870f5c6efeSJed Brown + A - the Jacobian 43886b867d5aSJose E. Roman - B - the preconditioning matrix (may be the same as A) 43890f5c6efeSJed Brown 43900f5c6efeSJed Brown Notes: 43910f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 43920f5c6efeSJed Brown 43930f5c6efeSJed Brown Level: developer 43940f5c6efeSJed Brown 43950f5c6efeSJed Brown .seealso: SNESSetJacobian() 43960f5c6efeSJed Brown @*/ 4397d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 43980f5c6efeSJed Brown { 43990f5c6efeSJed Brown TS ts = (TS)ctx; 44000f5c6efeSJed Brown PetscErrorCode ierr; 44010f5c6efeSJed Brown 44020f5c6efeSJed Brown PetscFunctionBegin; 44030f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 44040910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 44050f5c6efeSJed Brown PetscValidPointer(A,3); 440694ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 44070f5c6efeSJed Brown PetscValidPointer(B,4); 440894ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 4409064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(ts,TS_CLASSID,5); 4410d1e9a80fSBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 44110f5c6efeSJed Brown PetscFunctionReturn(0); 44120f5c6efeSJed Brown } 4413325fc9f4SBarry Smith 44140e4ef248SJed Brown /*@C 44159ae8fd06SBarry Smith TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only 44160e4ef248SJed Brown 44170e4ef248SJed Brown Collective on TS 44180e4ef248SJed Brown 4419*4165533cSJose E. Roman Input Parameters: 44200e4ef248SJed Brown + ts - time stepping context 44210e4ef248SJed Brown . t - time at which to evaluate 44220910c330SBarry Smith . U - state at which to evaluate 44230e4ef248SJed Brown - ctx - context 44240e4ef248SJed Brown 4425*4165533cSJose E. Roman Output Parameter: 44260e4ef248SJed Brown . F - right hand side 44270e4ef248SJed Brown 44280e4ef248SJed Brown Level: intermediate 44290e4ef248SJed Brown 44300e4ef248SJed Brown Notes: 44310e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 44320e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 44330e4ef248SJed Brown 44340e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 44350e4ef248SJed Brown @*/ 44360910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 44370e4ef248SJed Brown { 44380e4ef248SJed Brown PetscErrorCode ierr; 44390e4ef248SJed Brown Mat Arhs,Brhs; 44400e4ef248SJed Brown 44410e4ef248SJed Brown PetscFunctionBegin; 44420e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 44432663174eSHong Zhang /* undo the damage caused by shifting */ 4444cfa8a9a2SHong Zhang ierr = TSRecoverRHSJacobian(ts,Arhs,Brhs);CHKERRQ(ierr); 4445d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 44460910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 44470e4ef248SJed Brown PetscFunctionReturn(0); 44480e4ef248SJed Brown } 44490e4ef248SJed Brown 44500e4ef248SJed Brown /*@C 44510e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 44520e4ef248SJed Brown 44530e4ef248SJed Brown Collective on TS 44540e4ef248SJed Brown 4455*4165533cSJose E. Roman Input Parameters: 44560e4ef248SJed Brown + ts - time stepping context 44570e4ef248SJed Brown . t - time at which to evaluate 44580910c330SBarry Smith . U - state at which to evaluate 44590e4ef248SJed Brown - ctx - context 44600e4ef248SJed Brown 4461*4165533cSJose E. Roman Output Parameters: 44620e4ef248SJed Brown + A - pointer to operator 44630e4ef248SJed Brown . B - pointer to preconditioning matrix 44640e4ef248SJed Brown - flg - matrix structure flag 44650e4ef248SJed Brown 44660e4ef248SJed Brown Level: intermediate 44670e4ef248SJed Brown 44680e4ef248SJed Brown Notes: 44690e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 44700e4ef248SJed Brown 44710e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 44720e4ef248SJed Brown @*/ 4473d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 44740e4ef248SJed Brown { 44750e4ef248SJed Brown PetscFunctionBegin; 44760e4ef248SJed Brown PetscFunctionReturn(0); 44770e4ef248SJed Brown } 44780e4ef248SJed Brown 44790026cea9SSean Farley /*@C 44800026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 44810026cea9SSean Farley 44820026cea9SSean Farley Collective on TS 44830026cea9SSean Farley 4484*4165533cSJose E. Roman Input Parameters: 44850026cea9SSean Farley + ts - time stepping context 44860026cea9SSean Farley . t - time at which to evaluate 44870910c330SBarry Smith . U - state at which to evaluate 44880910c330SBarry Smith . Udot - time derivative of state vector 44890026cea9SSean Farley - ctx - context 44900026cea9SSean Farley 4491*4165533cSJose E. Roman Output Parameter: 44920026cea9SSean Farley . F - left hand side 44930026cea9SSean Farley 44940026cea9SSean Farley Level: intermediate 44950026cea9SSean Farley 44960026cea9SSean Farley Notes: 44970910c330SBarry 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 44980026cea9SSean Farley user is required to write their own TSComputeIFunction. 44990026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 45000026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 45010026cea9SSean Farley 45029ae8fd06SBarry Smith Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U 45039ae8fd06SBarry Smith 45049ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear() 45050026cea9SSean Farley @*/ 45060910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 45070026cea9SSean Farley { 45080026cea9SSean Farley PetscErrorCode ierr; 45090026cea9SSean Farley Mat A,B; 45100026cea9SSean Farley 45110026cea9SSean Farley PetscFunctionBegin; 45120298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 4513d1e9a80fSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 45140910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 45150026cea9SSean Farley PetscFunctionReturn(0); 45160026cea9SSean Farley } 45170026cea9SSean Farley 45180026cea9SSean Farley /*@C 4519b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 45200026cea9SSean Farley 45210026cea9SSean Farley Collective on TS 45220026cea9SSean Farley 4523*4165533cSJose E. Roman Input Parameters: 45240026cea9SSean Farley + ts - time stepping context 45250026cea9SSean Farley . t - time at which to evaluate 45260910c330SBarry Smith . U - state at which to evaluate 45270910c330SBarry Smith . Udot - time derivative of state vector 45280026cea9SSean Farley . shift - shift to apply 45290026cea9SSean Farley - ctx - context 45300026cea9SSean Farley 4531*4165533cSJose E. Roman Output Parameters: 45320026cea9SSean Farley + A - pointer to operator 45330026cea9SSean Farley . B - pointer to preconditioning matrix 45340026cea9SSean Farley - flg - matrix structure flag 45350026cea9SSean Farley 4536b41af12eSJed Brown Level: advanced 45370026cea9SSean Farley 45380026cea9SSean Farley Notes: 45390026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 45400026cea9SSean Farley 4541b41af12eSJed Brown It is only appropriate for problems of the form 4542b41af12eSJed Brown 4543b41af12eSJed Brown $ M Udot = F(U,t) 4544b41af12eSJed Brown 4545b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 4546b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 4547b41af12eSJed Brown an implicit operator of the form 4548b41af12eSJed Brown 4549b41af12eSJed Brown $ shift*M + J 4550b41af12eSJed Brown 4551b41af12eSJed 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 4552b41af12eSJed Brown a copy of M or reassemble it when requested. 4553b41af12eSJed Brown 45540026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 45550026cea9SSean Farley @*/ 4556d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 45570026cea9SSean Farley { 4558b41af12eSJed Brown PetscErrorCode ierr; 4559b41af12eSJed Brown 45600026cea9SSean Farley PetscFunctionBegin; 456194ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 4562b41af12eSJed Brown ts->ijacobian.shift = shift; 45630026cea9SSean Farley PetscFunctionReturn(0); 45640026cea9SSean Farley } 4565b41af12eSJed Brown 4566e817cc15SEmil Constantinescu /*@ 4567e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 4568e817cc15SEmil Constantinescu 4569e817cc15SEmil Constantinescu Not Collective 4570e817cc15SEmil Constantinescu 4571e817cc15SEmil Constantinescu Input Parameter: 4572e817cc15SEmil Constantinescu . ts - the TS context 4573e817cc15SEmil Constantinescu 4574e817cc15SEmil Constantinescu Output Parameter: 45754e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 4576e817cc15SEmil Constantinescu 4577e817cc15SEmil Constantinescu Level: beginner 4578e817cc15SEmil Constantinescu 4579e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 4580e817cc15SEmil Constantinescu @*/ 4581e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 4582e817cc15SEmil Constantinescu { 4583e817cc15SEmil Constantinescu PetscFunctionBegin; 4584e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4585e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 4586e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 4587e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4588e817cc15SEmil Constantinescu } 4589e817cc15SEmil Constantinescu 4590e817cc15SEmil Constantinescu /*@ 4591e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 4592e817cc15SEmil Constantinescu 4593e817cc15SEmil Constantinescu Not Collective 4594e817cc15SEmil Constantinescu 4595d8d19677SJose E. Roman Input Parameters: 4596e817cc15SEmil Constantinescu + ts - the TS context 45971297b224SEmil Constantinescu - equation_type - see TSEquationType 4598e817cc15SEmil Constantinescu 4599e817cc15SEmil Constantinescu Level: advanced 4600e817cc15SEmil Constantinescu 4601e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 4602e817cc15SEmil Constantinescu @*/ 4603e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 4604e817cc15SEmil Constantinescu { 4605e817cc15SEmil Constantinescu PetscFunctionBegin; 4606e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4607e817cc15SEmil Constantinescu ts->equation_type = equation_type; 4608e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4609e817cc15SEmil Constantinescu } 46100026cea9SSean Farley 46114af1b03aSJed Brown /*@ 46124af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 46134af1b03aSJed Brown 46144af1b03aSJed Brown Not Collective 46154af1b03aSJed Brown 46164af1b03aSJed Brown Input Parameter: 46174af1b03aSJed Brown . ts - the TS context 46184af1b03aSJed Brown 46194af1b03aSJed Brown Output Parameter: 46204af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 46214af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 46224af1b03aSJed Brown 4623487e0bb9SJed Brown Level: beginner 46244af1b03aSJed Brown 4625cd652676SJed Brown Notes: 4626cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 46274af1b03aSJed Brown 46284af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 46294af1b03aSJed Brown @*/ 46304af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 46314af1b03aSJed Brown { 46324af1b03aSJed Brown PetscFunctionBegin; 46334af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 46344af1b03aSJed Brown PetscValidPointer(reason,2); 46354af1b03aSJed Brown *reason = ts->reason; 46364af1b03aSJed Brown PetscFunctionReturn(0); 46374af1b03aSJed Brown } 46384af1b03aSJed Brown 4639d6ad946cSShri Abhyankar /*@ 4640d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 4641d6ad946cSShri Abhyankar 46426b221cbeSPatrick Sanan Logically Collective; reason must contain common value 4643d6ad946cSShri Abhyankar 46446b221cbeSPatrick Sanan Input Parameters: 4645d6ad946cSShri Abhyankar + ts - the TS context 46466b221cbeSPatrick Sanan - reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 4647d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 4648d6ad946cSShri Abhyankar 4649f5abba47SShri Abhyankar Level: advanced 4650d6ad946cSShri Abhyankar 4651d6ad946cSShri Abhyankar Notes: 46526b221cbeSPatrick Sanan Can only be called while TSSolve() is active. 4653d6ad946cSShri Abhyankar 4654d6ad946cSShri Abhyankar .seealso: TSConvergedReason 4655d6ad946cSShri Abhyankar @*/ 4656d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 4657d6ad946cSShri Abhyankar { 4658d6ad946cSShri Abhyankar PetscFunctionBegin; 4659d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4660d6ad946cSShri Abhyankar ts->reason = reason; 4661d6ad946cSShri Abhyankar PetscFunctionReturn(0); 4662d6ad946cSShri Abhyankar } 4663d6ad946cSShri Abhyankar 4664cc708dedSBarry Smith /*@ 4665cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 4666cc708dedSBarry Smith 4667cc708dedSBarry Smith Not Collective 4668cc708dedSBarry Smith 4669cc708dedSBarry Smith Input Parameter: 4670cc708dedSBarry Smith . ts - the TS context 4671cc708dedSBarry Smith 4672cc708dedSBarry Smith Output Parameter: 467319eac22cSLisandro Dalcin . ftime - the final time. This time corresponds to the final time set with TSSetMaxTime() 4674cc708dedSBarry Smith 4675487e0bb9SJed Brown Level: beginner 4676cc708dedSBarry Smith 4677cc708dedSBarry Smith Notes: 4678cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 4679cc708dedSBarry Smith 4680cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 4681cc708dedSBarry Smith @*/ 4682cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 4683cc708dedSBarry Smith { 4684cc708dedSBarry Smith PetscFunctionBegin; 4685cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4686cc708dedSBarry Smith PetscValidPointer(ftime,2); 4687cc708dedSBarry Smith *ftime = ts->solvetime; 4688cc708dedSBarry Smith PetscFunctionReturn(0); 4689cc708dedSBarry Smith } 4690cc708dedSBarry Smith 46912c18e0fdSBarry Smith /*@ 46925ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 46939f67acb7SJed Brown used by the time integrator. 46949f67acb7SJed Brown 46959f67acb7SJed Brown Not Collective 46969f67acb7SJed Brown 46979f67acb7SJed Brown Input Parameter: 46989f67acb7SJed Brown . ts - TS context 46999f67acb7SJed Brown 47009f67acb7SJed Brown Output Parameter: 47019f67acb7SJed Brown . nits - number of nonlinear iterations 47029f67acb7SJed Brown 47039f67acb7SJed Brown Notes: 47049f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 47059f67acb7SJed Brown 47069f67acb7SJed Brown Level: intermediate 47079f67acb7SJed Brown 47085ef26d82SJed Brown .seealso: TSGetKSPIterations() 47099f67acb7SJed Brown @*/ 47105ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 47119f67acb7SJed Brown { 47129f67acb7SJed Brown PetscFunctionBegin; 47139f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 47149f67acb7SJed Brown PetscValidIntPointer(nits,2); 47155ef26d82SJed Brown *nits = ts->snes_its; 47169f67acb7SJed Brown PetscFunctionReturn(0); 47179f67acb7SJed Brown } 47189f67acb7SJed Brown 47199f67acb7SJed Brown /*@ 47205ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 47219f67acb7SJed Brown used by the time integrator. 47229f67acb7SJed Brown 47239f67acb7SJed Brown Not Collective 47249f67acb7SJed Brown 47259f67acb7SJed Brown Input Parameter: 47269f67acb7SJed Brown . ts - TS context 47279f67acb7SJed Brown 47289f67acb7SJed Brown Output Parameter: 47299f67acb7SJed Brown . lits - number of linear iterations 47309f67acb7SJed Brown 47319f67acb7SJed Brown Notes: 47329f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 47339f67acb7SJed Brown 47349f67acb7SJed Brown Level: intermediate 47359f67acb7SJed Brown 47365ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 47379f67acb7SJed Brown @*/ 47385ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 47399f67acb7SJed Brown { 47409f67acb7SJed Brown PetscFunctionBegin; 47419f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 47429f67acb7SJed Brown PetscValidIntPointer(lits,2); 47435ef26d82SJed Brown *lits = ts->ksp_its; 47449f67acb7SJed Brown PetscFunctionReturn(0); 47459f67acb7SJed Brown } 47469f67acb7SJed Brown 4747cef5090cSJed Brown /*@ 4748cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 4749cef5090cSJed Brown 4750cef5090cSJed Brown Not Collective 4751cef5090cSJed Brown 4752cef5090cSJed Brown Input Parameter: 4753cef5090cSJed Brown . ts - TS context 4754cef5090cSJed Brown 4755cef5090cSJed Brown Output Parameter: 4756cef5090cSJed Brown . rejects - number of steps rejected 4757cef5090cSJed Brown 4758cef5090cSJed Brown Notes: 4759cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4760cef5090cSJed Brown 4761cef5090cSJed Brown Level: intermediate 4762cef5090cSJed Brown 47635ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 4764cef5090cSJed Brown @*/ 4765cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 4766cef5090cSJed Brown { 4767cef5090cSJed Brown PetscFunctionBegin; 4768cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4769cef5090cSJed Brown PetscValidIntPointer(rejects,2); 4770cef5090cSJed Brown *rejects = ts->reject; 4771cef5090cSJed Brown PetscFunctionReturn(0); 4772cef5090cSJed Brown } 4773cef5090cSJed Brown 4774cef5090cSJed Brown /*@ 4775cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 4776cef5090cSJed Brown 4777cef5090cSJed Brown Not Collective 4778cef5090cSJed Brown 4779cef5090cSJed Brown Input Parameter: 4780cef5090cSJed Brown . ts - TS context 4781cef5090cSJed Brown 4782cef5090cSJed Brown Output Parameter: 4783cef5090cSJed Brown . fails - number of failed nonlinear solves 4784cef5090cSJed Brown 4785cef5090cSJed Brown Notes: 4786cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4787cef5090cSJed Brown 4788cef5090cSJed Brown Level: intermediate 4789cef5090cSJed Brown 47905ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 4791cef5090cSJed Brown @*/ 4792cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 4793cef5090cSJed Brown { 4794cef5090cSJed Brown PetscFunctionBegin; 4795cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4796cef5090cSJed Brown PetscValidIntPointer(fails,2); 4797cef5090cSJed Brown *fails = ts->num_snes_failures; 4798cef5090cSJed Brown PetscFunctionReturn(0); 4799cef5090cSJed Brown } 4800cef5090cSJed Brown 4801cef5090cSJed Brown /*@ 4802cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 4803cef5090cSJed Brown 4804cef5090cSJed Brown Not Collective 4805cef5090cSJed Brown 4806d8d19677SJose E. Roman Input Parameters: 4807cef5090cSJed Brown + ts - TS context 4808cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 4809cef5090cSJed Brown 4810cef5090cSJed Brown Notes: 4811cef5090cSJed Brown The counter is reset to zero for each step 4812cef5090cSJed Brown 4813cef5090cSJed Brown Options Database Key: 4814cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 4815cef5090cSJed Brown 4816cef5090cSJed Brown Level: intermediate 4817cef5090cSJed Brown 48185ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4819cef5090cSJed Brown @*/ 4820cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 4821cef5090cSJed Brown { 4822cef5090cSJed Brown PetscFunctionBegin; 4823cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4824cef5090cSJed Brown ts->max_reject = rejects; 4825cef5090cSJed Brown PetscFunctionReturn(0); 4826cef5090cSJed Brown } 4827cef5090cSJed Brown 4828cef5090cSJed Brown /*@ 4829cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 4830cef5090cSJed Brown 4831cef5090cSJed Brown Not Collective 4832cef5090cSJed Brown 4833d8d19677SJose E. Roman Input Parameters: 4834cef5090cSJed Brown + ts - TS context 4835cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 4836cef5090cSJed Brown 4837cef5090cSJed Brown Notes: 4838cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 4839cef5090cSJed Brown 4840cef5090cSJed Brown Options Database Key: 4841cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 4842cef5090cSJed Brown 4843cef5090cSJed Brown Level: intermediate 4844cef5090cSJed Brown 48455ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 4846cef5090cSJed Brown @*/ 4847cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 4848cef5090cSJed Brown { 4849cef5090cSJed Brown PetscFunctionBegin; 4850cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4851cef5090cSJed Brown ts->max_snes_failures = fails; 4852cef5090cSJed Brown PetscFunctionReturn(0); 4853cef5090cSJed Brown } 4854cef5090cSJed Brown 4855cef5090cSJed Brown /*@ 4856cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 4857cef5090cSJed Brown 4858cef5090cSJed Brown Not Collective 4859cef5090cSJed Brown 4860d8d19677SJose E. Roman Input Parameters: 4861cef5090cSJed Brown + ts - TS context 4862cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 4863cef5090cSJed Brown 4864cef5090cSJed Brown Options Database Key: 4865cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 4866cef5090cSJed Brown 4867cef5090cSJed Brown Level: intermediate 4868cef5090cSJed Brown 48695ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4870cef5090cSJed Brown @*/ 4871cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 4872cef5090cSJed Brown { 4873cef5090cSJed Brown PetscFunctionBegin; 4874cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4875cef5090cSJed Brown ts->errorifstepfailed = err; 4876cef5090cSJed Brown PetscFunctionReturn(0); 4877cef5090cSJed Brown } 4878cef5090cSJed Brown 487984df9cb4SJed Brown /*@ 4880552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 488184df9cb4SJed Brown 4882ed81e22dSJed Brown Collective on TS if controller has not been created yet 488384df9cb4SJed Brown 4884*4165533cSJose E. Roman Input Parameter: 4885ed81e22dSJed Brown . ts - time stepping context 488684df9cb4SJed Brown 4887*4165533cSJose E. Roman Output Parameter: 4888ed81e22dSJed Brown . adapt - adaptive controller 488984df9cb4SJed Brown 489084df9cb4SJed Brown Level: intermediate 489184df9cb4SJed Brown 4892ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 489384df9cb4SJed Brown @*/ 4894552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 489584df9cb4SJed Brown { 489684df9cb4SJed Brown PetscErrorCode ierr; 489784df9cb4SJed Brown 489884df9cb4SJed Brown PetscFunctionBegin; 489984df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4900bec58848SLisandro Dalcin PetscValidPointer(adapt,2); 490184df9cb4SJed Brown if (!ts->adapt) { 4902ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 49033bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 49041c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 490584df9cb4SJed Brown } 4906bec58848SLisandro Dalcin *adapt = ts->adapt; 490784df9cb4SJed Brown PetscFunctionReturn(0); 490884df9cb4SJed Brown } 4909d6ebe24aSShri Abhyankar 49101c3436cfSJed Brown /*@ 49111c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 49121c3436cfSJed Brown 49131c3436cfSJed Brown Logically Collective 49141c3436cfSJed Brown 4915*4165533cSJose E. Roman Input Parameters: 49161c3436cfSJed Brown + ts - time integration context 49171c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 49180298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 49191c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 49200298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 49211c3436cfSJed Brown 4922a3cdaa26SBarry Smith Options Database keys: 4923a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 4924a3cdaa26SBarry Smith - -ts_atol <atol> Absolute tolerance for local truncation error 4925a3cdaa26SBarry Smith 49263ff766beSShri Abhyankar Notes: 49273ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 49283ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 49293ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 49303ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 49313ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 49323ff766beSShri Abhyankar differential variables. 49333ff766beSShri Abhyankar 49341c3436cfSJed Brown Level: beginner 49351c3436cfSJed Brown 49365c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSGetTolerances() 49371c3436cfSJed Brown @*/ 49381c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 49391c3436cfSJed Brown { 49401c3436cfSJed Brown PetscErrorCode ierr; 49411c3436cfSJed Brown 49421c3436cfSJed Brown PetscFunctionBegin; 4943c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 49441c3436cfSJed Brown if (vatol) { 49451c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 49461c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 49471c3436cfSJed Brown ts->vatol = vatol; 49481c3436cfSJed Brown } 4949c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 49501c3436cfSJed Brown if (vrtol) { 49511c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 49521c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 49531c3436cfSJed Brown ts->vrtol = vrtol; 49541c3436cfSJed Brown } 49551c3436cfSJed Brown PetscFunctionReturn(0); 49561c3436cfSJed Brown } 49571c3436cfSJed Brown 4958c5033834SJed Brown /*@ 4959c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 4960c5033834SJed Brown 4961c5033834SJed Brown Logically Collective 4962c5033834SJed Brown 4963*4165533cSJose E. Roman Input Parameter: 4964c5033834SJed Brown . ts - time integration context 4965c5033834SJed Brown 4966*4165533cSJose E. Roman Output Parameters: 49670298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 49680298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 49690298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 49700298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 4971c5033834SJed Brown 4972c5033834SJed Brown Level: beginner 4973c5033834SJed Brown 49745c01a8f1SJed Brown .seealso: TS, TSAdapt, TSErrorWeightedNorm(), TSSetTolerances() 4975c5033834SJed Brown @*/ 4976c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 4977c5033834SJed Brown { 4978c5033834SJed Brown PetscFunctionBegin; 4979c5033834SJed Brown if (atol) *atol = ts->atol; 4980c5033834SJed Brown if (vatol) *vatol = ts->vatol; 4981c5033834SJed Brown if (rtol) *rtol = ts->rtol; 4982c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 4983c5033834SJed Brown PetscFunctionReturn(0); 4984c5033834SJed Brown } 4985c5033834SJed Brown 49869c6b16b5SShri Abhyankar /*@ 4987a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 49889c6b16b5SShri Abhyankar 49899c6b16b5SShri Abhyankar Collective on TS 49909c6b16b5SShri Abhyankar 4991*4165533cSJose E. Roman Input Parameters: 49929c6b16b5SShri Abhyankar + ts - time stepping context 4993a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 4994a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 49959c6b16b5SShri Abhyankar 4996*4165533cSJose E. Roman Output Parameters: 4997a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 49987453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 4999a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 50009c6b16b5SShri Abhyankar 50019c6b16b5SShri Abhyankar Level: developer 50029c6b16b5SShri Abhyankar 5003deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity() 50049c6b16b5SShri Abhyankar @*/ 50057453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 50069c6b16b5SShri Abhyankar { 50079c6b16b5SShri Abhyankar PetscErrorCode ierr; 50089c6b16b5SShri Abhyankar PetscInt i,n,N,rstart; 50097453f775SEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 50107453f775SEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 50119c6b16b5SShri Abhyankar const PetscScalar *u,*y; 50127453f775SEmil Constantinescu PetscReal sum,suma,sumr,gsum,gsuma,gsumr,diff; 50137453f775SEmil Constantinescu PetscReal tol,tola,tolr; 50147453f775SEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 50159c6b16b5SShri Abhyankar 50169c6b16b5SShri Abhyankar PetscFunctionBegin; 50179c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5018a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5019a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5020a4868fbcSLisandro Dalcin PetscValidType(U,2); 5021a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5022a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5023a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 50248a175baeSEmil Constantinescu PetscValidPointer(norma,5); 50258a175baeSEmil Constantinescu PetscValidPointer(normr,6); 5026a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 50279c6b16b5SShri Abhyankar 50289c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 50299c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 50309c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 50319c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 50329c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 50337453f775SEmil Constantinescu sum = 0.; n_loc = 0; 50347453f775SEmil Constantinescu suma = 0.; na_loc = 0; 50357453f775SEmil Constantinescu sumr = 0.; nr_loc = 0; 50369c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 50379c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 50389c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 50399c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 50409c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 504176cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 50427453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50437453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 50447453f775SEmil Constantinescu if (tola>0.) { 50457453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 50467453f775SEmil Constantinescu na_loc++; 50477453f775SEmil Constantinescu } 50487453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 50497453f775SEmil Constantinescu if (tolr>0.) { 50507453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 50517453f775SEmil Constantinescu nr_loc++; 50527453f775SEmil Constantinescu } 50537453f775SEmil Constantinescu tol=tola+tolr; 50547453f775SEmil Constantinescu if (tol>0.) { 50557453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 50567453f775SEmil Constantinescu n_loc++; 50577453f775SEmil Constantinescu } 50589c6b16b5SShri Abhyankar } 50599c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 50609c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 50619c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 50629c6b16b5SShri Abhyankar const PetscScalar *atol; 50639c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 50649c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 506576cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 50667453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50677453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 50687453f775SEmil Constantinescu if (tola>0.) { 50697453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 50707453f775SEmil Constantinescu na_loc++; 50717453f775SEmil Constantinescu } 50727453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 50737453f775SEmil Constantinescu if (tolr>0.) { 50747453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 50757453f775SEmil Constantinescu nr_loc++; 50767453f775SEmil Constantinescu } 50777453f775SEmil Constantinescu tol=tola+tolr; 50787453f775SEmil Constantinescu if (tol>0.) { 50797453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 50807453f775SEmil Constantinescu n_loc++; 50817453f775SEmil Constantinescu } 50829c6b16b5SShri Abhyankar } 50839c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 50849c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 50859c6b16b5SShri Abhyankar const PetscScalar *rtol; 50869c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 50879c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 508876cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 50897453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 50907453f775SEmil Constantinescu tola = ts->atol; 50917453f775SEmil Constantinescu if (tola>0.) { 50927453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 50937453f775SEmil Constantinescu na_loc++; 50947453f775SEmil Constantinescu } 50957453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 50967453f775SEmil Constantinescu if (tolr>0.) { 50977453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 50987453f775SEmil Constantinescu nr_loc++; 50997453f775SEmil Constantinescu } 51007453f775SEmil Constantinescu tol=tola+tolr; 51017453f775SEmil Constantinescu if (tol>0.) { 51027453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 51037453f775SEmil Constantinescu n_loc++; 51047453f775SEmil Constantinescu } 51059c6b16b5SShri Abhyankar } 51069c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 51079c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 51089c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 510976cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 51107453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 51117453f775SEmil Constantinescu tola = ts->atol; 51127453f775SEmil Constantinescu if (tola>0.) { 51137453f775SEmil Constantinescu suma += PetscSqr(diff/tola); 51147453f775SEmil Constantinescu na_loc++; 51157453f775SEmil Constantinescu } 51167453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 51177453f775SEmil Constantinescu if (tolr>0.) { 51187453f775SEmil Constantinescu sumr += PetscSqr(diff/tolr); 51197453f775SEmil Constantinescu nr_loc++; 51207453f775SEmil Constantinescu } 51217453f775SEmil Constantinescu tol=tola+tolr; 51227453f775SEmil Constantinescu if (tol>0.) { 51237453f775SEmil Constantinescu sum += PetscSqr(diff/tol); 51247453f775SEmil Constantinescu n_loc++; 51257453f775SEmil Constantinescu } 51269c6b16b5SShri Abhyankar } 51279c6b16b5SShri Abhyankar } 51289c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 51299c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 51309c6b16b5SShri Abhyankar 51317453f775SEmil Constantinescu err_loc[0] = sum; 51327453f775SEmil Constantinescu err_loc[1] = suma; 51337453f775SEmil Constantinescu err_loc[2] = sumr; 51347453f775SEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 51357453f775SEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 51367453f775SEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 51377453f775SEmil Constantinescu 5138820f2d46SBarry Smith ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRMPI(ierr); 51397453f775SEmil Constantinescu 51407453f775SEmil Constantinescu gsum = err_glb[0]; 51417453f775SEmil Constantinescu gsuma = err_glb[1]; 51427453f775SEmil Constantinescu gsumr = err_glb[2]; 51437453f775SEmil Constantinescu n_glb = err_glb[3]; 51447453f775SEmil Constantinescu na_glb = err_glb[4]; 51457453f775SEmil Constantinescu nr_glb = err_glb[5]; 51467453f775SEmil Constantinescu 5147b1316ef9SEmil Constantinescu *norm = 0.; 5148b1316ef9SEmil Constantinescu if (n_glb>0.) {*norm = PetscSqrtReal(gsum / n_glb);} 5149b1316ef9SEmil Constantinescu *norma = 0.; 5150b1316ef9SEmil Constantinescu if (na_glb>0.) {*norma = PetscSqrtReal(gsuma / na_glb);} 5151b1316ef9SEmil Constantinescu *normr = 0.; 5152b1316ef9SEmil Constantinescu if (nr_glb>0.) {*normr = PetscSqrtReal(gsumr / nr_glb);} 51539c6b16b5SShri Abhyankar 51549c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 51557453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 51567453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 51579c6b16b5SShri Abhyankar PetscFunctionReturn(0); 51589c6b16b5SShri Abhyankar } 51599c6b16b5SShri Abhyankar 51609c6b16b5SShri Abhyankar /*@ 5161a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 51629c6b16b5SShri Abhyankar 51639c6b16b5SShri Abhyankar Collective on TS 51649c6b16b5SShri Abhyankar 5165*4165533cSJose E. Roman Input Parameters: 51669c6b16b5SShri Abhyankar + ts - time stepping context 5167a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5168a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 51699c6b16b5SShri Abhyankar 5170*4165533cSJose E. Roman Output Parameters: 5171a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 51727453f775SEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5173a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 51749c6b16b5SShri Abhyankar 51759c6b16b5SShri Abhyankar Level: developer 51769c6b16b5SShri Abhyankar 5177deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2() 51789c6b16b5SShri Abhyankar @*/ 51797453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 51809c6b16b5SShri Abhyankar { 51819c6b16b5SShri Abhyankar PetscErrorCode ierr; 51827453f775SEmil Constantinescu PetscInt i,n,N,rstart; 51839c6b16b5SShri Abhyankar const PetscScalar *u,*y; 51847453f775SEmil Constantinescu PetscReal max,gmax,maxa,gmaxa,maxr,gmaxr; 51857453f775SEmil Constantinescu PetscReal tol,tola,tolr,diff; 51867453f775SEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 51879c6b16b5SShri Abhyankar 51889c6b16b5SShri Abhyankar PetscFunctionBegin; 51899c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5190a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5191a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5192a4868fbcSLisandro Dalcin PetscValidType(U,2); 5193a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5194a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5195a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 51968a175baeSEmil Constantinescu PetscValidPointer(norma,5); 51978a175baeSEmil Constantinescu PetscValidPointer(normr,6); 5198a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 51999c6b16b5SShri Abhyankar 52009c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 52019c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 52029c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 52039c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 52049c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 52057453f775SEmil Constantinescu 52067453f775SEmil Constantinescu max=0.; 52077453f775SEmil Constantinescu maxa=0.; 52087453f775SEmil Constantinescu maxr=0.; 52097453f775SEmil Constantinescu 52107453f775SEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 52119c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 52129c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 52139c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 52147453f775SEmil Constantinescu 52157453f775SEmil Constantinescu for (i=0; i<n; i++) { 521676cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 52177453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 52187453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 52197453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52207453f775SEmil Constantinescu tol = tola+tolr; 52217453f775SEmil Constantinescu if (tola>0.) { 52227453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 52237453f775SEmil Constantinescu } 52247453f775SEmil Constantinescu if (tolr>0.) { 52257453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 52267453f775SEmil Constantinescu } 52277453f775SEmil Constantinescu if (tol>0.) { 52287453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 52297453f775SEmil Constantinescu } 52309c6b16b5SShri Abhyankar } 52319c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 52329c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 52339c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 52349c6b16b5SShri Abhyankar const PetscScalar *atol; 52359c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 52367453f775SEmil Constantinescu for (i=0; i<n; i++) { 523776cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 52387453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 52397453f775SEmil Constantinescu tola = PetscRealPart(atol[i]); 52407453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52417453f775SEmil Constantinescu tol = tola+tolr; 52427453f775SEmil Constantinescu if (tola>0.) { 52437453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 52447453f775SEmil Constantinescu } 52457453f775SEmil Constantinescu if (tolr>0.) { 52467453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 52477453f775SEmil Constantinescu } 52487453f775SEmil Constantinescu if (tol>0.) { 52497453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 52507453f775SEmil Constantinescu } 52519c6b16b5SShri Abhyankar } 52529c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 52539c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 52549c6b16b5SShri Abhyankar const PetscScalar *rtol; 52559c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 52567453f775SEmil Constantinescu 52577453f775SEmil Constantinescu for (i=0; i<n; i++) { 525876cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 52597453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 52607453f775SEmil Constantinescu tola = ts->atol; 52617453f775SEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52627453f775SEmil Constantinescu tol = tola+tolr; 52637453f775SEmil Constantinescu if (tola>0.) { 52647453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 52657453f775SEmil Constantinescu } 52667453f775SEmil Constantinescu if (tolr>0.) { 52677453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 52687453f775SEmil Constantinescu } 52697453f775SEmil Constantinescu if (tol>0.) { 52707453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 52717453f775SEmil Constantinescu } 52729c6b16b5SShri Abhyankar } 52739c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 52749c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 52757453f775SEmil Constantinescu 52767453f775SEmil Constantinescu for (i=0; i<n; i++) { 527776cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 52787453f775SEmil Constantinescu diff = PetscAbsScalar(y[i] - u[i]); 52797453f775SEmil Constantinescu tola = ts->atol; 52807453f775SEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52817453f775SEmil Constantinescu tol = tola+tolr; 52827453f775SEmil Constantinescu if (tola>0.) { 52837453f775SEmil Constantinescu maxa = PetscMax(maxa,diff / tola); 52847453f775SEmil Constantinescu } 52857453f775SEmil Constantinescu if (tolr>0.) { 52867453f775SEmil Constantinescu maxr = PetscMax(maxr,diff / tolr); 52877453f775SEmil Constantinescu } 52887453f775SEmil Constantinescu if (tol>0.) { 52897453f775SEmil Constantinescu max = PetscMax(max,diff / tol); 52907453f775SEmil Constantinescu } 52919c6b16b5SShri Abhyankar } 52929c6b16b5SShri Abhyankar } 52939c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 52949c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 52957453f775SEmil Constantinescu err_loc[0] = max; 52967453f775SEmil Constantinescu err_loc[1] = maxa; 52977453f775SEmil Constantinescu err_loc[2] = maxr; 5298820f2d46SBarry Smith ierr = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRMPI(ierr); 52997453f775SEmil Constantinescu gmax = err_glb[0]; 53007453f775SEmil Constantinescu gmaxa = err_glb[1]; 53017453f775SEmil Constantinescu gmaxr = err_glb[2]; 53029c6b16b5SShri Abhyankar 53039c6b16b5SShri Abhyankar *norm = gmax; 53047453f775SEmil Constantinescu *norma = gmaxa; 53057453f775SEmil Constantinescu *normr = gmaxr; 53069c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 53077453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 53087453f775SEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 53099c6b16b5SShri Abhyankar PetscFunctionReturn(0); 53109c6b16b5SShri Abhyankar } 53119c6b16b5SShri Abhyankar 53121c3436cfSJed Brown /*@ 53138a175baeSEmil Constantinescu TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances 53141c3436cfSJed Brown 53151c3436cfSJed Brown Collective on TS 53161c3436cfSJed Brown 5317*4165533cSJose E. Roman Input Parameters: 53181c3436cfSJed Brown + ts - time stepping context 5319a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5320a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 5321a4868fbcSLisandro Dalcin - wnormtype - norm type, either NORM_2 or NORM_INFINITY 53227619abb3SShri 5323*4165533cSJose E. Roman Output Parameters: 5324a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 53258a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 5326a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 5327a4868fbcSLisandro Dalcin 5328a4868fbcSLisandro Dalcin Options Database Keys: 5329a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 5330a4868fbcSLisandro Dalcin 53311c3436cfSJed Brown Level: developer 53321c3436cfSJed Brown 53338a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm 53341c3436cfSJed Brown @*/ 53357453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 53361c3436cfSJed Brown { 53378beabaa1SBarry Smith PetscErrorCode ierr; 53381c3436cfSJed Brown 53391c3436cfSJed Brown PetscFunctionBegin; 5340a4868fbcSLisandro Dalcin if (wnormtype == NORM_2) { 53417453f775SEmil Constantinescu ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr); 5342a4868fbcSLisandro Dalcin } else if (wnormtype == NORM_INFINITY) { 53437453f775SEmil Constantinescu ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr); 5344a4868fbcSLisandro Dalcin } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 53451c3436cfSJed Brown PetscFunctionReturn(0); 53461c3436cfSJed Brown } 53471c3436cfSJed Brown 53488a175baeSEmil Constantinescu /*@ 53498a175baeSEmil Constantinescu TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances 53508a175baeSEmil Constantinescu 53518a175baeSEmil Constantinescu Collective on TS 53528a175baeSEmil Constantinescu 5353*4165533cSJose E. Roman Input Parameters: 53548a175baeSEmil Constantinescu + ts - time stepping context 53558a175baeSEmil Constantinescu . E - error vector 53568a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 53578a175baeSEmil Constantinescu - Y - state vector, previous time step 53588a175baeSEmil Constantinescu 5359*4165533cSJose E. Roman Output Parameters: 5360a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 53618a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5362a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 53638a175baeSEmil Constantinescu 53648a175baeSEmil Constantinescu Level: developer 53658a175baeSEmil Constantinescu 53668a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity() 53678a175baeSEmil Constantinescu @*/ 53688a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 53698a175baeSEmil Constantinescu { 53708a175baeSEmil Constantinescu PetscErrorCode ierr; 53718a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 53728a175baeSEmil Constantinescu PetscInt n_loc,na_loc,nr_loc; 53738a175baeSEmil Constantinescu PetscReal n_glb,na_glb,nr_glb; 53748a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 53758a175baeSEmil Constantinescu PetscReal err,sum,suma,sumr,gsum,gsuma,gsumr; 53768a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 53778a175baeSEmil Constantinescu PetscReal err_loc[6],err_glb[6]; 53788a175baeSEmil Constantinescu 53798a175baeSEmil Constantinescu PetscFunctionBegin; 53808a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 53818a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 53828a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 53838a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 53848a175baeSEmil Constantinescu PetscValidType(E,2); 53858a175baeSEmil Constantinescu PetscValidType(U,3); 53868a175baeSEmil Constantinescu PetscValidType(Y,4); 53878a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 5388064a246eSJacob Faibussowitsch PetscCheckSameComm(U,3,Y,4); 53898a175baeSEmil Constantinescu PetscValidPointer(norm,5); 53908a175baeSEmil Constantinescu PetscValidPointer(norma,6); 53918a175baeSEmil Constantinescu PetscValidPointer(normr,7); 53928a175baeSEmil Constantinescu 53938a175baeSEmil Constantinescu ierr = VecGetSize(E,&N);CHKERRQ(ierr); 53948a175baeSEmil Constantinescu ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr); 53958a175baeSEmil Constantinescu ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr); 53968a175baeSEmil Constantinescu ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr); 53978a175baeSEmil Constantinescu ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 53988a175baeSEmil Constantinescu ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 53998a175baeSEmil Constantinescu sum = 0.; n_loc = 0; 54008a175baeSEmil Constantinescu suma = 0.; na_loc = 0; 54018a175baeSEmil Constantinescu sumr = 0.; nr_loc = 0; 54028a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { 54038a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 54048a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 54058a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 54068a175baeSEmil Constantinescu for (i=0; i<n; i++) { 540776cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 54088a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 54098a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 54108a175baeSEmil Constantinescu if (tola>0.) { 54118a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 54128a175baeSEmil Constantinescu na_loc++; 54138a175baeSEmil Constantinescu } 54148a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54158a175baeSEmil Constantinescu if (tolr>0.) { 54168a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 54178a175baeSEmil Constantinescu nr_loc++; 54188a175baeSEmil Constantinescu } 54198a175baeSEmil Constantinescu tol=tola+tolr; 54208a175baeSEmil Constantinescu if (tol>0.) { 54218a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 54228a175baeSEmil Constantinescu n_loc++; 54238a175baeSEmil Constantinescu } 54248a175baeSEmil Constantinescu } 54258a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 54268a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 54278a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 54288a175baeSEmil Constantinescu const PetscScalar *atol; 54298a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 54308a175baeSEmil Constantinescu for (i=0; i<n; i++) { 543176cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 54328a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 54338a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 54348a175baeSEmil Constantinescu if (tola>0.) { 54358a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 54368a175baeSEmil Constantinescu na_loc++; 54378a175baeSEmil Constantinescu } 54388a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54398a175baeSEmil Constantinescu if (tolr>0.) { 54408a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 54418a175baeSEmil Constantinescu nr_loc++; 54428a175baeSEmil Constantinescu } 54438a175baeSEmil Constantinescu tol=tola+tolr; 54448a175baeSEmil Constantinescu if (tol>0.) { 54458a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 54468a175baeSEmil Constantinescu n_loc++; 54478a175baeSEmil Constantinescu } 54488a175baeSEmil Constantinescu } 54498a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 54508a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 54518a175baeSEmil Constantinescu const PetscScalar *rtol; 54528a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 54538a175baeSEmil Constantinescu for (i=0; i<n; i++) { 545476cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 54558a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 54568a175baeSEmil Constantinescu tola = ts->atol; 54578a175baeSEmil Constantinescu if (tola>0.) { 54588a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 54598a175baeSEmil Constantinescu na_loc++; 54608a175baeSEmil Constantinescu } 54618a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54628a175baeSEmil Constantinescu if (tolr>0.) { 54638a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 54648a175baeSEmil Constantinescu nr_loc++; 54658a175baeSEmil Constantinescu } 54668a175baeSEmil Constantinescu tol=tola+tolr; 54678a175baeSEmil Constantinescu if (tol>0.) { 54688a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 54698a175baeSEmil Constantinescu n_loc++; 54708a175baeSEmil Constantinescu } 54718a175baeSEmil Constantinescu } 54728a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 54738a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 54748a175baeSEmil Constantinescu for (i=0; i<n; i++) { 547576cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 54768a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 54778a175baeSEmil Constantinescu tola = ts->atol; 54788a175baeSEmil Constantinescu if (tola>0.) { 54798a175baeSEmil Constantinescu suma += PetscSqr(err/tola); 54808a175baeSEmil Constantinescu na_loc++; 54818a175baeSEmil Constantinescu } 54828a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 54838a175baeSEmil Constantinescu if (tolr>0.) { 54848a175baeSEmil Constantinescu sumr += PetscSqr(err/tolr); 54858a175baeSEmil Constantinescu nr_loc++; 54868a175baeSEmil Constantinescu } 54878a175baeSEmil Constantinescu tol=tola+tolr; 54888a175baeSEmil Constantinescu if (tol>0.) { 54898a175baeSEmil Constantinescu sum += PetscSqr(err/tol); 54908a175baeSEmil Constantinescu n_loc++; 54918a175baeSEmil Constantinescu } 54928a175baeSEmil Constantinescu } 54938a175baeSEmil Constantinescu } 54948a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr); 54958a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 54968a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 54978a175baeSEmil Constantinescu 54988a175baeSEmil Constantinescu err_loc[0] = sum; 54998a175baeSEmil Constantinescu err_loc[1] = suma; 55008a175baeSEmil Constantinescu err_loc[2] = sumr; 55018a175baeSEmil Constantinescu err_loc[3] = (PetscReal)n_loc; 55028a175baeSEmil Constantinescu err_loc[4] = (PetscReal)na_loc; 55038a175baeSEmil Constantinescu err_loc[5] = (PetscReal)nr_loc; 55048a175baeSEmil Constantinescu 5505820f2d46SBarry Smith ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRMPI(ierr); 55068a175baeSEmil Constantinescu 55078a175baeSEmil Constantinescu gsum = err_glb[0]; 55088a175baeSEmil Constantinescu gsuma = err_glb[1]; 55098a175baeSEmil Constantinescu gsumr = err_glb[2]; 55108a175baeSEmil Constantinescu n_glb = err_glb[3]; 55118a175baeSEmil Constantinescu na_glb = err_glb[4]; 55128a175baeSEmil Constantinescu nr_glb = err_glb[5]; 55138a175baeSEmil Constantinescu 55148a175baeSEmil Constantinescu *norm = 0.; 55158a175baeSEmil Constantinescu if (n_glb>0.) {*norm = PetscSqrtReal(gsum / n_glb);} 55168a175baeSEmil Constantinescu *norma = 0.; 55178a175baeSEmil Constantinescu if (na_glb>0.) {*norma = PetscSqrtReal(gsuma / na_glb);} 55188a175baeSEmil Constantinescu *normr = 0.; 55198a175baeSEmil Constantinescu if (nr_glb>0.) {*normr = PetscSqrtReal(gsumr / nr_glb);} 55208a175baeSEmil Constantinescu 55218a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 55228a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 55238a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 55248a175baeSEmil Constantinescu PetscFunctionReturn(0); 55258a175baeSEmil Constantinescu } 55268a175baeSEmil Constantinescu 55278a175baeSEmil Constantinescu /*@ 55288a175baeSEmil Constantinescu TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances 55298a175baeSEmil Constantinescu Collective on TS 55308a175baeSEmil Constantinescu 5531*4165533cSJose E. Roman Input Parameters: 55328a175baeSEmil Constantinescu + ts - time stepping context 55338a175baeSEmil Constantinescu . E - error vector 55348a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 55358a175baeSEmil Constantinescu - Y - state vector, previous time step 55368a175baeSEmil Constantinescu 5537*4165533cSJose E. Roman Output Parameters: 5538a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 means that the error matches the tolerances 55398a175baeSEmil Constantinescu . norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances 5540a2b725a8SWilliam Gropp - normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances 55418a175baeSEmil Constantinescu 55428a175baeSEmil Constantinescu Level: developer 55438a175baeSEmil Constantinescu 55448a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2() 55458a175baeSEmil Constantinescu @*/ 55468a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr) 55478a175baeSEmil Constantinescu { 55488a175baeSEmil Constantinescu PetscErrorCode ierr; 55498a175baeSEmil Constantinescu PetscInt i,n,N,rstart; 55508a175baeSEmil Constantinescu const PetscScalar *e,*u,*y; 55518a175baeSEmil Constantinescu PetscReal err,max,gmax,maxa,gmaxa,maxr,gmaxr; 55528a175baeSEmil Constantinescu PetscReal tol,tola,tolr; 55538a175baeSEmil Constantinescu PetscReal err_loc[3],err_glb[3]; 55548a175baeSEmil Constantinescu 55558a175baeSEmil Constantinescu PetscFunctionBegin; 55568a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 55578a175baeSEmil Constantinescu PetscValidHeaderSpecific(E,VEC_CLASSID,2); 55588a175baeSEmil Constantinescu PetscValidHeaderSpecific(U,VEC_CLASSID,3); 55598a175baeSEmil Constantinescu PetscValidHeaderSpecific(Y,VEC_CLASSID,4); 55608a175baeSEmil Constantinescu PetscValidType(E,2); 55618a175baeSEmil Constantinescu PetscValidType(U,3); 55628a175baeSEmil Constantinescu PetscValidType(Y,4); 55638a175baeSEmil Constantinescu PetscCheckSameComm(E,2,U,3); 5564064a246eSJacob Faibussowitsch PetscCheckSameComm(U,3,Y,4); 55658a175baeSEmil Constantinescu PetscValidPointer(norm,5); 55668a175baeSEmil Constantinescu PetscValidPointer(norma,6); 55678a175baeSEmil Constantinescu PetscValidPointer(normr,7); 55688a175baeSEmil Constantinescu 55698a175baeSEmil Constantinescu ierr = VecGetSize(E,&N);CHKERRQ(ierr); 55708a175baeSEmil Constantinescu ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr); 55718a175baeSEmil Constantinescu ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr); 55728a175baeSEmil Constantinescu ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr); 55738a175baeSEmil Constantinescu ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 55748a175baeSEmil Constantinescu ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 55758a175baeSEmil Constantinescu 55768a175baeSEmil Constantinescu max=0.; 55778a175baeSEmil Constantinescu maxa=0.; 55788a175baeSEmil Constantinescu maxr=0.; 55798a175baeSEmil Constantinescu 55808a175baeSEmil Constantinescu if (ts->vatol && ts->vrtol) { /* vector atol, vector rtol */ 55818a175baeSEmil Constantinescu const PetscScalar *atol,*rtol; 55828a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 55838a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 55848a175baeSEmil Constantinescu 55858a175baeSEmil Constantinescu for (i=0; i<n; i++) { 558676cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 55878a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 55888a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 55898a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 55908a175baeSEmil Constantinescu tol = tola+tolr; 55918a175baeSEmil Constantinescu if (tola>0.) { 55928a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 55938a175baeSEmil Constantinescu } 55948a175baeSEmil Constantinescu if (tolr>0.) { 55958a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 55968a175baeSEmil Constantinescu } 55978a175baeSEmil Constantinescu if (tol>0.) { 55988a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 55998a175baeSEmil Constantinescu } 56008a175baeSEmil Constantinescu } 56018a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 56028a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 56038a175baeSEmil Constantinescu } else if (ts->vatol) { /* vector atol, scalar rtol */ 56048a175baeSEmil Constantinescu const PetscScalar *atol; 56058a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 56068a175baeSEmil Constantinescu for (i=0; i<n; i++) { 560776cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 56088a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 56098a175baeSEmil Constantinescu tola = PetscRealPart(atol[i]); 56108a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 56118a175baeSEmil Constantinescu tol = tola+tolr; 56128a175baeSEmil Constantinescu if (tola>0.) { 56138a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 56148a175baeSEmil Constantinescu } 56158a175baeSEmil Constantinescu if (tolr>0.) { 56168a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 56178a175baeSEmil Constantinescu } 56188a175baeSEmil Constantinescu if (tol>0.) { 56198a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 56208a175baeSEmil Constantinescu } 56218a175baeSEmil Constantinescu } 56228a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 56238a175baeSEmil Constantinescu } else if (ts->vrtol) { /* scalar atol, vector rtol */ 56248a175baeSEmil Constantinescu const PetscScalar *rtol; 56258a175baeSEmil Constantinescu ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 56268a175baeSEmil Constantinescu 56278a175baeSEmil Constantinescu for (i=0; i<n; i++) { 562876cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 56298a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 56308a175baeSEmil Constantinescu tola = ts->atol; 56318a175baeSEmil Constantinescu tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 56328a175baeSEmil Constantinescu tol = tola+tolr; 56338a175baeSEmil Constantinescu if (tola>0.) { 56348a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 56358a175baeSEmil Constantinescu } 56368a175baeSEmil Constantinescu if (tolr>0.) { 56378a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 56388a175baeSEmil Constantinescu } 56398a175baeSEmil Constantinescu if (tol>0.) { 56408a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 56418a175baeSEmil Constantinescu } 56428a175baeSEmil Constantinescu } 56438a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 56448a175baeSEmil Constantinescu } else { /* scalar atol, scalar rtol */ 56458a175baeSEmil Constantinescu 56468a175baeSEmil Constantinescu for (i=0; i<n; i++) { 564776cddca1SEmil Constantinescu SkipSmallValue(y[i],u[i],ts->adapt->ignore_max); 56488a175baeSEmil Constantinescu err = PetscAbsScalar(e[i]); 56498a175baeSEmil Constantinescu tola = ts->atol; 56508a175baeSEmil Constantinescu tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 56518a175baeSEmil Constantinescu tol = tola+tolr; 56528a175baeSEmil Constantinescu if (tola>0.) { 56538a175baeSEmil Constantinescu maxa = PetscMax(maxa,err / tola); 56548a175baeSEmil Constantinescu } 56558a175baeSEmil Constantinescu if (tolr>0.) { 56568a175baeSEmil Constantinescu maxr = PetscMax(maxr,err / tolr); 56578a175baeSEmil Constantinescu } 56588a175baeSEmil Constantinescu if (tol>0.) { 56598a175baeSEmil Constantinescu max = PetscMax(max,err / tol); 56608a175baeSEmil Constantinescu } 56618a175baeSEmil Constantinescu } 56628a175baeSEmil Constantinescu } 56638a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr); 56648a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 56658a175baeSEmil Constantinescu ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 56668a175baeSEmil Constantinescu err_loc[0] = max; 56678a175baeSEmil Constantinescu err_loc[1] = maxa; 56688a175baeSEmil Constantinescu err_loc[2] = maxr; 5669820f2d46SBarry Smith ierr = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRMPI(ierr); 56708a175baeSEmil Constantinescu gmax = err_glb[0]; 56718a175baeSEmil Constantinescu gmaxa = err_glb[1]; 56728a175baeSEmil Constantinescu gmaxr = err_glb[2]; 56738a175baeSEmil Constantinescu 56748a175baeSEmil Constantinescu *norm = gmax; 56758a175baeSEmil Constantinescu *norma = gmaxa; 56768a175baeSEmil Constantinescu *normr = gmaxr; 56778a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 56788a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma"); 56798a175baeSEmil Constantinescu if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr"); 56808a175baeSEmil Constantinescu PetscFunctionReturn(0); 56818a175baeSEmil Constantinescu } 56828a175baeSEmil Constantinescu 56838a175baeSEmil Constantinescu /*@ 56848a175baeSEmil Constantinescu TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances 56858a175baeSEmil Constantinescu 56868a175baeSEmil Constantinescu Collective on TS 56878a175baeSEmil Constantinescu 5688*4165533cSJose E. Roman Input Parameters: 56898a175baeSEmil Constantinescu + ts - time stepping context 56908a175baeSEmil Constantinescu . E - error vector 56918a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 56928a175baeSEmil Constantinescu . Y - state vector, previous time step 56938a175baeSEmil Constantinescu - wnormtype - norm type, either NORM_2 or NORM_INFINITY 56948a175baeSEmil Constantinescu 5695*4165533cSJose E. Roman Output Parameters: 5696a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 56978a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 5698a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 56998a175baeSEmil Constantinescu 57008a175baeSEmil Constantinescu Options Database Keys: 57018a175baeSEmil Constantinescu . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 57028a175baeSEmil Constantinescu 57038a175baeSEmil Constantinescu Level: developer 57048a175baeSEmil Constantinescu 57058a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2() 57068a175baeSEmil Constantinescu @*/ 57078a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr) 57088a175baeSEmil Constantinescu { 57098a175baeSEmil Constantinescu PetscErrorCode ierr; 57108a175baeSEmil Constantinescu 57118a175baeSEmil Constantinescu PetscFunctionBegin; 57128a175baeSEmil Constantinescu if (wnormtype == NORM_2) { 57138a175baeSEmil Constantinescu ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr); 57148a175baeSEmil Constantinescu } else if (wnormtype == NORM_INFINITY) { 57158a175baeSEmil Constantinescu ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr); 57168a175baeSEmil Constantinescu } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 57178a175baeSEmil Constantinescu PetscFunctionReturn(0); 57188a175baeSEmil Constantinescu } 57198a175baeSEmil Constantinescu 57208d59e960SJed Brown /*@ 57218d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 57228d59e960SJed Brown 57238d59e960SJed Brown Logically Collective on TS 57248d59e960SJed Brown 5725*4165533cSJose E. Roman Input Parameters: 57268d59e960SJed Brown + ts - time stepping context 57278d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 57288d59e960SJed Brown 57298d59e960SJed Brown Note: 57308d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 57318d59e960SJed Brown 57328d59e960SJed Brown Level: intermediate 57338d59e960SJed Brown 57348d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 57358d59e960SJed Brown @*/ 57368d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 57378d59e960SJed Brown { 57388d59e960SJed Brown PetscFunctionBegin; 57398d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 57408d59e960SJed Brown ts->cfltime_local = cfltime; 57418d59e960SJed Brown ts->cfltime = -1.; 57428d59e960SJed Brown PetscFunctionReturn(0); 57438d59e960SJed Brown } 57448d59e960SJed Brown 57458d59e960SJed Brown /*@ 57468d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 57478d59e960SJed Brown 57488d59e960SJed Brown Collective on TS 57498d59e960SJed Brown 5750*4165533cSJose E. Roman Input Parameter: 57518d59e960SJed Brown . ts - time stepping context 57528d59e960SJed Brown 5753*4165533cSJose E. Roman Output Parameter: 57548d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 57558d59e960SJed Brown 57568d59e960SJed Brown Level: advanced 57578d59e960SJed Brown 57588d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 57598d59e960SJed Brown @*/ 57608d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 57618d59e960SJed Brown { 57628d59e960SJed Brown PetscErrorCode ierr; 57638d59e960SJed Brown 57648d59e960SJed Brown PetscFunctionBegin; 57658d59e960SJed Brown if (ts->cfltime < 0) { 5766820f2d46SBarry Smith ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRMPI(ierr); 57678d59e960SJed Brown } 57688d59e960SJed Brown *cfltime = ts->cfltime; 57698d59e960SJed Brown PetscFunctionReturn(0); 57708d59e960SJed Brown } 57718d59e960SJed Brown 5772d6ebe24aSShri Abhyankar /*@ 5773d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 5774d6ebe24aSShri Abhyankar 5775d6ebe24aSShri Abhyankar Input Parameters: 5776a2b725a8SWilliam Gropp + ts - the TS context. 5777d6ebe24aSShri Abhyankar . xl - lower bound. 5778a2b725a8SWilliam Gropp - xu - upper bound. 5779d6ebe24aSShri Abhyankar 5780d6ebe24aSShri Abhyankar Notes: 5781d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 5782e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 5783d6ebe24aSShri Abhyankar 57842bd2b0e6SSatish Balay Level: advanced 57852bd2b0e6SSatish Balay 5786d6ebe24aSShri Abhyankar @*/ 5787d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 5788d6ebe24aSShri Abhyankar { 5789d6ebe24aSShri Abhyankar PetscErrorCode ierr; 5790d6ebe24aSShri Abhyankar SNES snes; 5791d6ebe24aSShri Abhyankar 5792d6ebe24aSShri Abhyankar PetscFunctionBegin; 5793d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 5794d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 5795d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 5796d6ebe24aSShri Abhyankar } 5797d6ebe24aSShri Abhyankar 5798f9c1d6abSBarry Smith /*@ 5799f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 5800f9c1d6abSBarry Smith 5801d083f849SBarry Smith Collective on TS 5802f9c1d6abSBarry Smith 5803f9c1d6abSBarry Smith Input Parameters: 5804f9c1d6abSBarry Smith + ts - the TS context 5805f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 5806f9c1d6abSBarry Smith 5807f9c1d6abSBarry Smith Output Parameters: 5808f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 5809f9c1d6abSBarry Smith 5810f9c1d6abSBarry Smith Level: developer 5811f9c1d6abSBarry Smith 5812f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 5813f9c1d6abSBarry Smith @*/ 5814f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 5815f9c1d6abSBarry Smith { 5816f9c1d6abSBarry Smith PetscErrorCode ierr; 5817f9c1d6abSBarry Smith 5818f9c1d6abSBarry Smith PetscFunctionBegin; 5819f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5820ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 5821f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 5822f9c1d6abSBarry Smith PetscFunctionReturn(0); 5823f9c1d6abSBarry Smith } 582424655328SShri 582524655328SShri /*@ 5826dcb233daSLisandro Dalcin TSRestartStep - Flags the solver to restart the next step 5827dcb233daSLisandro Dalcin 5828dcb233daSLisandro Dalcin Collective on TS 5829dcb233daSLisandro Dalcin 5830dcb233daSLisandro Dalcin Input Parameter: 5831dcb233daSLisandro Dalcin . ts - the TS context obtained from TSCreate() 5832dcb233daSLisandro Dalcin 5833dcb233daSLisandro Dalcin Level: advanced 5834dcb233daSLisandro Dalcin 5835dcb233daSLisandro Dalcin Notes: 5836dcb233daSLisandro Dalcin Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of 5837dcb233daSLisandro Dalcin discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution 5838dcb233daSLisandro Dalcin vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For 5839dcb233daSLisandro Dalcin the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce 5840dcb233daSLisandro Dalcin discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with 5841dcb233daSLisandro Dalcin discontinuous source terms). 5842dcb233daSLisandro Dalcin 5843dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep() 5844dcb233daSLisandro Dalcin @*/ 5845dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts) 5846dcb233daSLisandro Dalcin { 5847dcb233daSLisandro Dalcin PetscFunctionBegin; 5848dcb233daSLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5849dcb233daSLisandro Dalcin ts->steprestart = PETSC_TRUE; 5850dcb233daSLisandro Dalcin PetscFunctionReturn(0); 5851dcb233daSLisandro Dalcin } 5852dcb233daSLisandro Dalcin 5853dcb233daSLisandro Dalcin /*@ 585424655328SShri TSRollBack - Rolls back one time step 585524655328SShri 585624655328SShri Collective on TS 585724655328SShri 585824655328SShri Input Parameter: 585924655328SShri . ts - the TS context obtained from TSCreate() 586024655328SShri 586124655328SShri Level: advanced 586224655328SShri 586324655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 586424655328SShri @*/ 586524655328SShri PetscErrorCode TSRollBack(TS ts) 586624655328SShri { 586724655328SShri PetscErrorCode ierr; 586824655328SShri 586924655328SShri PetscFunctionBegin; 587024655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 5871b3de5cdeSLisandro Dalcin if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called"); 587224655328SShri if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 587324655328SShri ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 587424655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 587524655328SShri ts->ptime = ts->ptime_prev; 5876be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime_prev_rollback; 58772808aa04SLisandro Dalcin ts->steps--; 5878b3de5cdeSLisandro Dalcin ts->steprollback = PETSC_TRUE; 587924655328SShri PetscFunctionReturn(0); 588024655328SShri } 5881aeb4809dSShri Abhyankar 5882ff22ae23SHong Zhang /*@ 5883ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 5884ff22ae23SHong Zhang 5885ff22ae23SHong Zhang Input Parameter: 5886ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 5887ff22ae23SHong Zhang 58880429704eSStefano Zampini Output Parameters: 58890429704eSStefano Zampini + ns - the number of stages 58900429704eSStefano Zampini - Y - the current stage vectors 58910429704eSStefano Zampini 5892ff22ae23SHong Zhang Level: advanced 5893ff22ae23SHong Zhang 58940429704eSStefano Zampini Notes: Both ns and Y can be NULL. 58950429704eSStefano Zampini 5896ff22ae23SHong Zhang .seealso: TSCreate() 5897ff22ae23SHong Zhang @*/ 5898ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns,Vec **Y) 5899ff22ae23SHong Zhang { 5900ff22ae23SHong Zhang PetscErrorCode ierr; 5901ff22ae23SHong Zhang 5902ff22ae23SHong Zhang PetscFunctionBegin; 5903ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 59040429704eSStefano Zampini if (ns) PetscValidPointer(ns,2); 59050429704eSStefano Zampini if (Y) PetscValidPointer(Y,3); 59060429704eSStefano Zampini if (!ts->ops->getstages) { 59070429704eSStefano Zampini if (ns) *ns = 0; 59080429704eSStefano Zampini if (Y) *Y = NULL; 59090429704eSStefano Zampini } else { 5910ff22ae23SHong Zhang ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 5911ff22ae23SHong Zhang } 5912ff22ae23SHong Zhang PetscFunctionReturn(0); 5913ff22ae23SHong Zhang } 5914ff22ae23SHong Zhang 5915847ff0e1SMatthew G. Knepley /*@C 5916847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 5917847ff0e1SMatthew G. Knepley 5918847ff0e1SMatthew G. Knepley Collective on SNES 5919847ff0e1SMatthew G. Knepley 5920847ff0e1SMatthew G. Knepley Input Parameters: 5921847ff0e1SMatthew G. Knepley + ts - the TS context 5922847ff0e1SMatthew G. Knepley . t - current timestep 5923847ff0e1SMatthew G. Knepley . U - state vector 5924847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 5925847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 5926847ff0e1SMatthew G. Knepley - ctx - an optional user context 5927847ff0e1SMatthew G. Knepley 5928847ff0e1SMatthew G. Knepley Output Parameters: 5929847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 5930847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 5931847ff0e1SMatthew G. Knepley 5932847ff0e1SMatthew G. Knepley Level: intermediate 5933847ff0e1SMatthew G. Knepley 5934847ff0e1SMatthew G. Knepley Notes: 5935847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 5936847ff0e1SMatthew G. Knepley 5937847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 5938847ff0e1SMatthew G. Knepley 5939847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 5940847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 5941847ff0e1SMatthew G. Knepley 5942847ff0e1SMatthew G. Knepley This will first try to get the coloring from the DM. If the DM type has no coloring 5943847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 5944847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 5945847ff0e1SMatthew G. Knepley 5946847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction() 5947847ff0e1SMatthew G. Knepley @*/ 5948847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx) 5949847ff0e1SMatthew G. Knepley { 5950847ff0e1SMatthew G. Knepley SNES snes; 5951847ff0e1SMatthew G. Knepley MatFDColoring color; 5952847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 5953847ff0e1SMatthew G. Knepley PetscErrorCode ierr; 5954847ff0e1SMatthew G. Knepley 5955847ff0e1SMatthew G. Knepley PetscFunctionBegin; 5956c5929fdfSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr); 5957847ff0e1SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr); 5958847ff0e1SMatthew G. Knepley if (!color) { 5959847ff0e1SMatthew G. Knepley DM dm; 5960847ff0e1SMatthew G. Knepley ISColoring iscoloring; 5961847ff0e1SMatthew G. Knepley 5962847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 5963847ff0e1SMatthew G. Knepley ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr); 5964847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 5965847ff0e1SMatthew G. Knepley ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr); 5966847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 5967847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 5968847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 5969847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 5970847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 5971847ff0e1SMatthew G. Knepley } else { 5972847ff0e1SMatthew G. Knepley MatColoring mc; 5973847ff0e1SMatthew G. Knepley 5974847ff0e1SMatthew G. Knepley ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr); 5975847ff0e1SMatthew G. Knepley ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr); 5976847ff0e1SMatthew G. Knepley ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr); 5977847ff0e1SMatthew G. Knepley ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr); 5978847ff0e1SMatthew G. Knepley ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr); 5979847ff0e1SMatthew G. Knepley ierr = MatColoringDestroy(&mc);CHKERRQ(ierr); 5980847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 5981847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 5982847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 5983847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 5984847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 5985847ff0e1SMatthew G. Knepley } 5986847ff0e1SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr); 5987847ff0e1SMatthew G. Knepley ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr); 5988847ff0e1SMatthew G. Knepley } 5989847ff0e1SMatthew G. Knepley ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr); 5990847ff0e1SMatthew G. Knepley ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr); 5991847ff0e1SMatthew G. Knepley if (J != B) { 5992847ff0e1SMatthew G. Knepley ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 5993847ff0e1SMatthew G. Knepley ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 5994847ff0e1SMatthew G. Knepley } 5995847ff0e1SMatthew G. Knepley PetscFunctionReturn(0); 5996847ff0e1SMatthew G. Knepley } 599793b34091SDebojyoti Ghosh 5998cb9d8021SPierre Barbier de Reuille /*@ 59996bc98fa9SBarry Smith TSSetFunctionDomainError - Set a function that tests if the current state vector is valid 6000cb9d8021SPierre Barbier de Reuille 6001cb9d8021SPierre Barbier de Reuille Input Parameters: 60026bc98fa9SBarry Smith + ts - the TS context 60036bc98fa9SBarry Smith - func - function called within TSFunctionDomainError 60046bc98fa9SBarry Smith 60056bc98fa9SBarry Smith Calling sequence of func: 60066bc98fa9SBarry Smith $ PetscErrorCode func(TS ts,PetscReal time,Vec state,PetscBool reject) 60076bc98fa9SBarry Smith 60086bc98fa9SBarry Smith + ts - the TS context 60096bc98fa9SBarry Smith . time - the current time (of the stage) 60106bc98fa9SBarry Smith . state - the state to check if it is valid 60116bc98fa9SBarry Smith - reject - (output parameter) PETSC_FALSE if the state is acceptable, PETSC_TRUE if not acceptable 6012cb9d8021SPierre Barbier de Reuille 6013cb9d8021SPierre Barbier de Reuille Level: intermediate 6014cb9d8021SPierre Barbier de Reuille 60156bc98fa9SBarry Smith Notes: 60166bc98fa9SBarry Smith If an implicit ODE solver is being used then, in addition to providing this routine, the 60176bc98fa9SBarry Smith user's code should call SNESSetFunctionDomainError() when domain errors occur during 60186bc98fa9SBarry Smith function evaluations where the functions are provided by TSSetIFunction() or TSSetRHSFunction(). 60196bc98fa9SBarry Smith Use TSGetSNES() to obtain the SNES object 60206bc98fa9SBarry Smith 60216bc98fa9SBarry Smith Developer Notes: 60226bc98fa9SBarry Smith The naming of this function is inconsistent with the SNESSetFunctionDomainError() 60236bc98fa9SBarry Smith since one takes a function pointer and the other does not. 60246bc98fa9SBarry Smith 60256bc98fa9SBarry Smith .seealso: TSAdaptCheckStage(), TSFunctionDomainError(), SNESSetFunctionDomainError(), TSGetSNES() 6026cb9d8021SPierre Barbier de Reuille @*/ 6027cb9d8021SPierre Barbier de Reuille 6028d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*)) 6029cb9d8021SPierre Barbier de Reuille { 6030cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 6031cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID,1); 6032cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 6033cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 6034cb9d8021SPierre Barbier de Reuille } 6035cb9d8021SPierre Barbier de Reuille 6036cb9d8021SPierre Barbier de Reuille /*@ 60376bc98fa9SBarry Smith TSFunctionDomainError - Checks if the current state is valid 6038cb9d8021SPierre Barbier de Reuille 6039cb9d8021SPierre Barbier de Reuille Input Parameters: 60406bc98fa9SBarry Smith + ts - the TS context 60416bc98fa9SBarry Smith . stagetime - time of the simulation 60426bc98fa9SBarry Smith - Y - state vector to check. 6043cb9d8021SPierre Barbier de Reuille 6044cb9d8021SPierre Barbier de Reuille Output Parameter: 60456bc98fa9SBarry Smith . accept - Set to PETSC_FALSE if the current state vector is valid. 6046cb9d8021SPierre Barbier de Reuille 6047cb9d8021SPierre Barbier de Reuille Note: 60486bc98fa9SBarry Smith This function is called by the TS integration routines and calls the user provided function (set with TSSetFunctionDomainError()) 60496bc98fa9SBarry Smith to check if the current state is valid. 605096a0c994SBarry Smith 60516bc98fa9SBarry Smith Level: developer 60526bc98fa9SBarry Smith 60536bc98fa9SBarry Smith .seealso: TSSetFunctionDomainError() 6054cb9d8021SPierre Barbier de Reuille @*/ 6055d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept) 6056cb9d8021SPierre Barbier de Reuille { 6057cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 6058cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6059cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 6060cb9d8021SPierre Barbier de Reuille if (ts->functiondomainerror) { 6061d183316bSPierre Barbier de Reuille PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept)); 6062cb9d8021SPierre Barbier de Reuille } 6063cb9d8021SPierre Barbier de Reuille PetscFunctionReturn(0); 6064cb9d8021SPierre Barbier de Reuille } 60651ceb14c0SBarry Smith 606693b34091SDebojyoti Ghosh /*@C 6067e5168f73SEmil Constantinescu TSClone - This function clones a time step object. 606893b34091SDebojyoti Ghosh 6069d083f849SBarry Smith Collective 607093b34091SDebojyoti Ghosh 607193b34091SDebojyoti Ghosh Input Parameter: 607293b34091SDebojyoti Ghosh . tsin - The input TS 607393b34091SDebojyoti Ghosh 607493b34091SDebojyoti Ghosh Output Parameter: 6075e5168f73SEmil Constantinescu . tsout - The output TS (cloned) 607693b34091SDebojyoti Ghosh 60775eca1a21SEmil Constantinescu Notes: 60785eca1a21SEmil 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. 60795eca1a21SEmil Constantinescu 6080928bb9adSStefano 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); 60815eca1a21SEmil Constantinescu 60825eca1a21SEmil Constantinescu Level: developer 608393b34091SDebojyoti Ghosh 6084e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType() 608593b34091SDebojyoti Ghosh @*/ 6086baa10174SEmil Constantinescu PetscErrorCode TSClone(TS tsin, TS *tsout) 608793b34091SDebojyoti Ghosh { 608893b34091SDebojyoti Ghosh TS t; 608993b34091SDebojyoti Ghosh PetscErrorCode ierr; 6090dc846ba4SSatish Balay SNES snes_start; 6091dc846ba4SSatish Balay DM dm; 6092dc846ba4SSatish Balay TSType type; 609393b34091SDebojyoti Ghosh 609493b34091SDebojyoti Ghosh PetscFunctionBegin; 609593b34091SDebojyoti Ghosh PetscValidPointer(tsin,1); 609693b34091SDebojyoti Ghosh *tsout = NULL; 609793b34091SDebojyoti Ghosh 60987a37829fSSatish Balay ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr); 609993b34091SDebojyoti Ghosh 610093b34091SDebojyoti Ghosh /* General TS description */ 610193b34091SDebojyoti Ghosh t->numbermonitors = 0; 6102d0c080abSJoseph Pusztay t->monitorFrequency = 1; 610393b34091SDebojyoti Ghosh t->setupcalled = 0; 610493b34091SDebojyoti Ghosh t->ksp_its = 0; 610593b34091SDebojyoti Ghosh t->snes_its = 0; 610693b34091SDebojyoti Ghosh t->nwork = 0; 61077d51462cSStefano Zampini t->rhsjacobian.time = PETSC_MIN_REAL; 610893b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 610993b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 611093b34091SDebojyoti Ghosh 611134561852SEmil Constantinescu ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr); 611234561852SEmil Constantinescu ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr); 6113d15a3a53SEmil Constantinescu 611493b34091SDebojyoti Ghosh ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr); 611593b34091SDebojyoti Ghosh ierr = TSSetDM(t,dm);CHKERRQ(ierr); 611693b34091SDebojyoti Ghosh 611793b34091SDebojyoti Ghosh t->adapt = tsin->adapt; 611851699248SLisandro Dalcin ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr); 611993b34091SDebojyoti Ghosh 6120e7069c78SShri t->trajectory = tsin->trajectory; 6121e7069c78SShri ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr); 6122e7069c78SShri 6123e7069c78SShri t->event = tsin->event; 61246b10a48eSSatish Balay if (t->event) t->event->refct++; 6125e7069c78SShri 612693b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 612793b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 6128e7069c78SShri t->ptime_prev = tsin->ptime_prev; 612993b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 613093b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 613193b34091SDebojyoti Ghosh t->steps = tsin->steps; 613293b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 613393b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 613493b34091SDebojyoti Ghosh t->atol = tsin->atol; 613593b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 613693b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 613793b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 613893b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 613993b34091SDebojyoti Ghosh 614093b34091SDebojyoti Ghosh ierr = TSGetType(tsin,&type);CHKERRQ(ierr); 614193b34091SDebojyoti Ghosh ierr = TSSetType(t,type);CHKERRQ(ierr); 614293b34091SDebojyoti Ghosh 614393b34091SDebojyoti Ghosh t->vec_sol = NULL; 614493b34091SDebojyoti Ghosh 614593b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 614693b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 614793b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 614893b34091SDebojyoti Ghosh 614993b34091SDebojyoti Ghosh ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr); 615093b34091SDebojyoti Ghosh 61510d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 61520d4fed19SBarry Smith PetscInt i; 61530d4fed19SBarry Smith ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr); 61540d4fed19SBarry Smith for (i=0; i<10; i++) { 61550d4fed19SBarry Smith ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 61560d4fed19SBarry Smith } 61570d4fed19SBarry Smith } 615893b34091SDebojyoti Ghosh *tsout = t; 615993b34091SDebojyoti Ghosh PetscFunctionReturn(0); 616093b34091SDebojyoti Ghosh } 6161f3b1f45cSBarry Smith 6162f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y) 6163f3b1f45cSBarry Smith { 6164f3b1f45cSBarry Smith PetscErrorCode ierr; 6165f3b1f45cSBarry Smith TS ts = (TS) ctx; 6166f3b1f45cSBarry Smith 6167f3b1f45cSBarry Smith PetscFunctionBegin; 6168f3b1f45cSBarry Smith ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr); 6169f3b1f45cSBarry Smith PetscFunctionReturn(0); 6170f3b1f45cSBarry Smith } 6171f3b1f45cSBarry Smith 6172f3b1f45cSBarry Smith /*@ 6173f3b1f45cSBarry Smith TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function. 6174f3b1f45cSBarry Smith 6175d083f849SBarry Smith Logically Collective on TS 6176f3b1f45cSBarry Smith 6177f3b1f45cSBarry Smith Input Parameters: 6178f3b1f45cSBarry Smith TS - the time stepping routine 6179f3b1f45cSBarry Smith 6180f3b1f45cSBarry Smith Output Parameter: 6181f3b1f45cSBarry Smith . flg - PETSC_TRUE if the multiply is likely correct 6182f3b1f45cSBarry Smith 6183f3b1f45cSBarry Smith Options Database: 6184f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator 6185f3b1f45cSBarry Smith 6186f3b1f45cSBarry Smith Level: advanced 6187f3b1f45cSBarry Smith 618895452b02SPatrick Sanan Notes: 618995452b02SPatrick Sanan This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian 6190f3b1f45cSBarry Smith 6191f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose() 6192f3b1f45cSBarry Smith @*/ 6193f3b1f45cSBarry Smith PetscErrorCode TSRHSJacobianTest(TS ts,PetscBool *flg) 6194f3b1f45cSBarry Smith { 6195f3b1f45cSBarry Smith Mat J,B; 6196f3b1f45cSBarry Smith PetscErrorCode ierr; 6197f3b1f45cSBarry Smith TSRHSJacobian func; 6198f3b1f45cSBarry Smith void* ctx; 6199f3b1f45cSBarry Smith 6200f3b1f45cSBarry Smith PetscFunctionBegin; 6201f3b1f45cSBarry Smith ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr); 6202f3b1f45cSBarry Smith ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr); 6203f3b1f45cSBarry Smith ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr); 6204f3b1f45cSBarry Smith PetscFunctionReturn(0); 6205f3b1f45cSBarry Smith } 6206f3b1f45cSBarry Smith 6207f3b1f45cSBarry Smith /*@C 6208f3b1f45cSBarry Smith TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function. 6209f3b1f45cSBarry Smith 6210d083f849SBarry Smith Logically Collective on TS 6211f3b1f45cSBarry Smith 6212f3b1f45cSBarry Smith Input Parameters: 6213f3b1f45cSBarry Smith TS - the time stepping routine 6214f3b1f45cSBarry Smith 6215f3b1f45cSBarry Smith Output Parameter: 6216f3b1f45cSBarry Smith . flg - PETSC_TRUE if the multiply is likely correct 6217f3b1f45cSBarry Smith 6218f3b1f45cSBarry Smith Options Database: 6219f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator 6220f3b1f45cSBarry Smith 622195452b02SPatrick Sanan Notes: 622295452b02SPatrick Sanan This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian 6223f3b1f45cSBarry Smith 6224f3b1f45cSBarry Smith Level: advanced 6225f3b1f45cSBarry Smith 6226f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest() 6227f3b1f45cSBarry Smith @*/ 6228f3b1f45cSBarry Smith PetscErrorCode TSRHSJacobianTestTranspose(TS ts,PetscBool *flg) 6229f3b1f45cSBarry Smith { 6230f3b1f45cSBarry Smith Mat J,B; 6231f3b1f45cSBarry Smith PetscErrorCode ierr; 6232f3b1f45cSBarry Smith void *ctx; 6233f3b1f45cSBarry Smith TSRHSJacobian func; 6234f3b1f45cSBarry Smith 6235f3b1f45cSBarry Smith PetscFunctionBegin; 6236f3b1f45cSBarry Smith ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr); 6237f3b1f45cSBarry Smith ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr); 6238f3b1f45cSBarry Smith ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr); 6239f3b1f45cSBarry Smith PetscFunctionReturn(0); 6240f3b1f45cSBarry Smith } 62410fe4d17eSHong Zhang 62420fe4d17eSHong Zhang /*@ 62430fe4d17eSHong Zhang TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used. 62440fe4d17eSHong Zhang 62450fe4d17eSHong Zhang Logically collective 62460fe4d17eSHong Zhang 6247d8d19677SJose E. Roman Input Parameters: 62480fe4d17eSHong Zhang + ts - timestepping context 62490fe4d17eSHong Zhang - use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used 62500fe4d17eSHong Zhang 62510fe4d17eSHong Zhang Options Database: 62520fe4d17eSHong Zhang . -ts_use_splitrhsfunction - <true,false> 62530fe4d17eSHong Zhang 62540fe4d17eSHong Zhang Notes: 62550fe4d17eSHong Zhang This is only useful for multirate methods 62560fe4d17eSHong Zhang 62570fe4d17eSHong Zhang Level: intermediate 62580fe4d17eSHong Zhang 62590fe4d17eSHong Zhang .seealso: TSGetUseSplitRHSFunction() 62600fe4d17eSHong Zhang @*/ 62610fe4d17eSHong Zhang PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction) 62620fe4d17eSHong Zhang { 62630fe4d17eSHong Zhang PetscFunctionBegin; 62640fe4d17eSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 62650fe4d17eSHong Zhang ts->use_splitrhsfunction = use_splitrhsfunction; 62660fe4d17eSHong Zhang PetscFunctionReturn(0); 62670fe4d17eSHong Zhang } 62680fe4d17eSHong Zhang 62690fe4d17eSHong Zhang /*@ 62700fe4d17eSHong Zhang TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used. 62710fe4d17eSHong Zhang 62720fe4d17eSHong Zhang Not collective 62730fe4d17eSHong Zhang 62740fe4d17eSHong Zhang Input Parameter: 62750fe4d17eSHong Zhang . ts - timestepping context 62760fe4d17eSHong Zhang 62770fe4d17eSHong Zhang Output Parameter: 62780fe4d17eSHong Zhang . use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used 62790fe4d17eSHong Zhang 62800fe4d17eSHong Zhang Level: intermediate 62810fe4d17eSHong Zhang 62820fe4d17eSHong Zhang .seealso: TSSetUseSplitRHSFunction() 62830fe4d17eSHong Zhang @*/ 62840fe4d17eSHong Zhang PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction) 62850fe4d17eSHong Zhang { 62860fe4d17eSHong Zhang PetscFunctionBegin; 62870fe4d17eSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 62880fe4d17eSHong Zhang *use_splitrhsfunction = ts->use_splitrhsfunction; 62890fe4d17eSHong Zhang PetscFunctionReturn(0); 62900fe4d17eSHong Zhang } 6291d60b7d5cSBarry Smith 6292d60b7d5cSBarry Smith /*@ 6293d60b7d5cSBarry Smith TSSetMatStructure - sets the relationship between the nonzero structure of the RHS Jacobian matrix to the IJacobian matrix. 6294d60b7d5cSBarry Smith 6295d60b7d5cSBarry Smith Logically Collective on ts 6296d60b7d5cSBarry Smith 6297d60b7d5cSBarry Smith Input Parameters: 6298d60b7d5cSBarry Smith + ts - the time-stepper 6299d60b7d5cSBarry Smith - str - the structure (the default is UNKNOWN_NONZERO_PATTERN) 6300d60b7d5cSBarry Smith 6301d60b7d5cSBarry Smith Level: intermediate 6302d60b7d5cSBarry Smith 6303d60b7d5cSBarry Smith Notes: 6304d60b7d5cSBarry Smith When the relationship between the nonzero structures is known and supplied the solution process can be much faster 6305d60b7d5cSBarry Smith 6306d60b7d5cSBarry Smith .seealso: MatAXPY(), MatStructure 6307d60b7d5cSBarry Smith @*/ 6308d60b7d5cSBarry Smith PetscErrorCode TSSetMatStructure(TS ts,MatStructure str) 6309d60b7d5cSBarry Smith { 6310d60b7d5cSBarry Smith PetscFunctionBegin; 6311d60b7d5cSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6312d60b7d5cSBarry Smith ts->axpy_pattern = str; 6313d60b7d5cSBarry Smith PetscFunctionReturn(0); 6314d60b7d5cSBarry Smith } 6315