163dd3a1aSKris Buschelman 2af0996ceSBarry Smith #include <petsc/private/tsimpl.h> /*I "petscts.h" I*/ 3496e6a7aSJed Brown #include <petscdmshell.h> 41e25c274SJed Brown #include <petscdmda.h> 52d5ee99bSBarry Smith #include <petscviewer.h> 62d5ee99bSBarry Smith #include <petscdraw.h> 7d763cef2SBarry Smith 8d5ba7fb7SMatthew Knepley /* Logging support */ 9d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 10166c7f25SBarry Smith PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 11d405a339SMatthew Knepley 1249354f04SShri Abhyankar const char *const TSExactFinalTimeOptions[] = {"STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0}; 1349354f04SShri Abhyankar 142d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx { 152d5ee99bSBarry Smith PetscViewer viewer; 164b363babSBarry Smith PetscDrawAxis axis; 172d5ee99bSBarry Smith Vec initialsolution; 182d5ee99bSBarry Smith PetscBool showinitial; 192d5ee99bSBarry Smith PetscInt howoften; /* when > 0 uses step % howoften, when negative only final solution plotted */ 202d5ee99bSBarry Smith PetscBool showtimestepandtime; 212d5ee99bSBarry Smith int color; 222d5ee99bSBarry Smith }; 232d5ee99bSBarry Smith 24bdad233fSMatthew Knepley #undef __FUNCT__ 25bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions" 26bdad233fSMatthew Knepley /*@ 27bdad233fSMatthew Knepley TSSetFromOptions - Sets various TS parameters from user options. 28bdad233fSMatthew Knepley 29bdad233fSMatthew Knepley Collective on TS 30bdad233fSMatthew Knepley 31bdad233fSMatthew Knepley Input Parameter: 32bdad233fSMatthew Knepley . ts - the TS context obtained from TSCreate() 33bdad233fSMatthew Knepley 34bdad233fSMatthew Knepley Options Database Keys: 354d91e141SJed Brown + -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP 36ef222394SBarry Smith . -ts_save_trajectory - checkpoint the solution at each time-step 373e4cdcaaSBarry Smith . -ts_max_steps <maxsteps> - maximum number of time-steps to take 383e4cdcaaSBarry Smith . -ts_final_time <time> - maximum time to compute to 393e4cdcaaSBarry Smith . -ts_dt <dt> - initial time step 40a3cdaa26SBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> whether to stop at the exact given final time and how to compute the solution at that ti,e 41a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 42a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 43a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 44a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 45a3cdaa26SBarry Smith . -ts_atol <atol> Absolute tolerance for local truncation error 46ef222394SBarry Smith . -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory) 47847ff0e1SMatthew G. Knepley . -ts_fd_color - Use finite differences with coloring to compute IJacobian 48bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 49de06c3feSJed Brown . -ts_monitor_lg_timestep - Monitor timestep size graphically 50de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 51de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 52de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 53de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 54de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 55de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 563e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 573e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 5891b97e58SBarry Smith . -ts_monitor_solution_binary <filename> - Save each solution to a binary file 59b3d3934dSBarry Smith . -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts 60b3d3934dSBarry Smith - -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 6191b97e58SBarry Smith 6291b97e58SBarry Smith Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified 63bdad233fSMatthew Knepley 64bdad233fSMatthew Knepley Level: beginner 65bdad233fSMatthew Knepley 66bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database 67bdad233fSMatthew Knepley 68a313700dSBarry Smith .seealso: TSGetType() 69bdad233fSMatthew Knepley @*/ 707087cfbeSBarry Smith PetscErrorCode TSSetFromOptions(TS ts) 71bdad233fSMatthew Knepley { 72bc952696SBarry Smith PetscBool opt,flg,tflg; 73dfbe8321SBarry Smith PetscErrorCode ierr; 74649052a6SBarry Smith PetscViewer monviewer; 75eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 76089b2837SJed Brown SNES snes; 771c3436cfSJed Brown TSAdapt adapt; 7831748224SBarry Smith PetscReal time_step; 7949354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 80d1212d36SBarry Smith char dir[16]; 816991f827SBarry Smith const char *defaultType; 826991f827SBarry Smith char typeName[256]; 83bdad233fSMatthew Knepley 84bdad233fSMatthew Knepley PetscFunctionBegin; 850700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 863194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr); 876991f827SBarry Smith if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 886991f827SBarry Smith else defaultType = TSEULER; 896991f827SBarry Smith 906991f827SBarry Smith ierr = TSRegisterAll();CHKERRQ(ierr); 916991f827SBarry Smith ierr = PetscOptionsFList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr); 926991f827SBarry Smith if (opt) { 936991f827SBarry Smith ierr = TSSetType(ts, typeName);CHKERRQ(ierr); 946991f827SBarry Smith } else { 956991f827SBarry Smith ierr = TSSetType(ts, defaultType);CHKERRQ(ierr); 966991f827SBarry Smith } 97bdad233fSMatthew Knepley 98bdad233fSMatthew Knepley /* Handle generic TS options */ 99844594baSBarry Smith if (ts->trajectory) tflg = PETSC_TRUE; 100844594baSBarry Smith else tflg = PETSC_FALSE; 101920aabf2SBarry Smith ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr); 102bc952696SBarry Smith if (tflg) {ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);} 103ef222394SBarry Smith if (ts->adjoint_solve) tflg = PETSC_TRUE; 104ef222394SBarry Smith else tflg = PETSC_FALSE; 105ef222394SBarry Smith ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr); 106ef222394SBarry Smith if (flg) { 107ef222394SBarry Smith ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr); 108e87fd094SBarry Smith ts->adjoint_solve = tflg; 109ef222394SBarry Smith } 1100298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr); 1110298fd71SBarry Smith ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr); 1120298fd71SBarry Smith ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr); 11331748224SBarry Smith ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr); 11431748224SBarry Smith if (flg) { 11531748224SBarry Smith ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 11631748224SBarry Smith } 11749354f04SShri 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); 11849354f04SShri Abhyankar if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);} 1190298fd71SBarry 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); 1200298fd71SBarry Smith ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr); 1210298fd71SBarry Smith ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr); 1220298fd71SBarry Smith ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr); 1230298fd71SBarry Smith ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr); 124bdad233fSMatthew Knepley 12556f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 12656f85f32SBarry Smith { 12756f85f32SBarry Smith PetscBool set; 12856f85f32SBarry Smith flg = PETSC_FALSE; 12956f85f32SBarry Smith ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr); 13056f85f32SBarry Smith if (set) { 13156f85f32SBarry Smith ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr); 13256f85f32SBarry Smith } 13356f85f32SBarry Smith } 13456f85f32SBarry Smith #endif 13556f85f32SBarry Smith 136bdad233fSMatthew Knepley /* Monitor options */ 137a6570f20SBarry Smith ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 138eabae89aSBarry Smith if (flg) { 139ce94432eSBarry Smith ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr); 140649052a6SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 141bdad233fSMatthew Knepley } 1425180491cSLisandro Dalcin ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1435180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);} 1445180491cSLisandro Dalcin 1454f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr); 146a7cc72afSBarry Smith if (opt) { 1470b039ecaSBarry Smith TSMonitorLGCtx ctx; 1483923b477SBarry Smith PetscInt howoften = 1; 149a80ad3e0SBarry Smith 1500298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr); 151ce94432eSBarry Smith ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 1524f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 153b3603a34SBarry Smith } 1544f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr); 155b3603a34SBarry Smith if (opt) { 1560b039ecaSBarry Smith TSMonitorLGCtx ctx; 1573923b477SBarry Smith PetscInt howoften = 1; 158b3603a34SBarry Smith 1590298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 16022d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1614f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 162bdad233fSMatthew Knepley } 1634f09c107SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr); 164ef20d060SBarry Smith if (opt) { 1650b039ecaSBarry Smith TSMonitorLGCtx ctx; 1663923b477SBarry Smith PetscInt howoften = 1; 167ef20d060SBarry Smith 1680298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr); 16922d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1704f09c107SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 171ef20d060SBarry Smith } 172201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr); 173201da799SBarry Smith if (opt) { 174201da799SBarry Smith TSMonitorLGCtx ctx; 175201da799SBarry Smith PetscInt howoften = 1; 176201da799SBarry Smith 1770298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 17822d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 179201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 180201da799SBarry Smith } 181201da799SBarry Smith ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr); 182201da799SBarry Smith if (opt) { 183201da799SBarry Smith TSMonitorLGCtx ctx; 184201da799SBarry Smith PetscInt howoften = 1; 185201da799SBarry Smith 1860298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr); 18722d28d08SBarry Smith ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr); 188201da799SBarry Smith ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr); 189201da799SBarry Smith } 1908189c53fSBarry Smith ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr); 1918189c53fSBarry Smith if (opt) { 1928189c53fSBarry Smith TSMonitorSPEigCtx ctx; 1938189c53fSBarry Smith PetscInt howoften = 1; 1948189c53fSBarry Smith 1950298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr); 19622d28d08SBarry Smith ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 1978189c53fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr); 1988189c53fSBarry Smith } 199ef20d060SBarry Smith opt = PETSC_FALSE; 2000dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr); 201a7cc72afSBarry Smith if (opt) { 20283a4ac43SBarry Smith TSMonitorDrawCtx ctx; 20383a4ac43SBarry Smith PetscInt howoften = 1; 204a80ad3e0SBarry Smith 2050298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr); 206ce94432eSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 20783a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 208bdad233fSMatthew Knepley } 209fb1732b5SBarry Smith opt = PETSC_FALSE; 2109110b2e7SHong Zhang ierr = PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);CHKERRQ(ierr); 2119110b2e7SHong Zhang if (opt) { 2129110b2e7SHong Zhang TSMonitorDrawCtx ctx; 2139110b2e7SHong Zhang PetscInt howoften = 1; 2149110b2e7SHong Zhang 2159110b2e7SHong Zhang ierr = PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);CHKERRQ(ierr); 2169110b2e7SHong Zhang ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 2179110b2e7SHong Zhang ierr = TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2189110b2e7SHong Zhang } 2199110b2e7SHong Zhang opt = PETSC_FALSE; 2202d5ee99bSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr); 2212d5ee99bSBarry Smith if (opt) { 2222d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2232d5ee99bSBarry Smith PetscReal bounds[4]; 2242d5ee99bSBarry Smith PetscInt n = 4; 2252d5ee99bSBarry Smith PetscDraw draw; 2262d5ee99bSBarry Smith 2272d5ee99bSBarry Smith ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr); 2282d5ee99bSBarry Smith if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field"); 2292d5ee99bSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr); 2302d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr); 2312d5ee99bSBarry Smith ierr = PetscDrawClear(draw);CHKERRQ(ierr); 2324b363babSBarry Smith ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr); 2334b363babSBarry Smith ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr); 234f54adfc0SBarry Smith ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr); 2354b363babSBarry Smith ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr); 23607995780SPeter Brune /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */ 2372d5ee99bSBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2382d5ee99bSBarry Smith } 2392d5ee99bSBarry Smith opt = PETSC_FALSE; 2400dcf80beSBarry Smith ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr); 2413a471f94SBarry Smith if (opt) { 24283a4ac43SBarry Smith TSMonitorDrawCtx ctx; 24383a4ac43SBarry Smith PetscInt howoften = 1; 2443a471f94SBarry Smith 2450298fd71SBarry Smith ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr); 246ce94432eSBarry Smith ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr); 24783a4ac43SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr); 2483a471f94SBarry Smith } 2493a471f94SBarry Smith opt = PETSC_FALSE; 25091b97e58SBarry Smith ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 251fb1732b5SBarry Smith if (flg) { 252fb1732b5SBarry Smith PetscViewer ctx; 253fb1732b5SBarry Smith if (monfilename[0]) { 254ce94432eSBarry Smith ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr); 255c2fbc07fSBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 256fb1732b5SBarry Smith } else { 257ce94432eSBarry Smith ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts)); 2580298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr); 259fb1732b5SBarry Smith } 260fb1732b5SBarry Smith } 261ed81e22dSJed Brown opt = PETSC_FALSE; 26291b97e58SBarry Smith ierr = PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 263ed81e22dSJed Brown if (flg) { 264ed81e22dSJed Brown const char *ptr,*ptr2; 265ed81e22dSJed Brown char *filetemplate; 266ce94432eSBarry Smith if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 267ed81e22dSJed Brown /* Do some cursory validation of the input. */ 268ed81e22dSJed Brown ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr); 269ce94432eSBarry Smith if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts"); 270ed81e22dSJed Brown for (ptr++; ptr && *ptr; ptr++) { 271ed81e22dSJed Brown ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr); 272ce94432eSBarry 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"); 273ed81e22dSJed Brown if (ptr2) break; 274ed81e22dSJed Brown } 275ed81e22dSJed Brown ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr); 276ed81e22dSJed Brown ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr); 277ed81e22dSJed Brown } 278bdad233fSMatthew Knepley 279d1212d36SBarry Smith ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr); 280d1212d36SBarry Smith if (flg) { 281d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 282d1212d36SBarry Smith int ray = 0; 283d1212d36SBarry Smith DMDADirection ddir; 284d1212d36SBarry Smith DM da; 285d1212d36SBarry Smith PetscMPIInt rank; 286d1212d36SBarry Smith 287ce94432eSBarry Smith if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 288d1212d36SBarry Smith if (dir[0] == 'x') ddir = DMDA_X; 289d1212d36SBarry Smith else if (dir[0] == 'y') ddir = DMDA_Y; 290ce94432eSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir); 291d1212d36SBarry Smith sscanf(dir+2,"%d",&ray); 292d1212d36SBarry Smith 293d1212d36SBarry Smith ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr); 294b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 295d1212d36SBarry Smith ierr = TSGetDM(ts,&da);CHKERRQ(ierr); 296d1212d36SBarry Smith ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr); 297ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr); 298d1212d36SBarry Smith if (!rank) { 299d1212d36SBarry Smith ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr); 300d1212d36SBarry Smith } 30151b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 302d1212d36SBarry Smith ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr); 303d1212d36SBarry Smith } 30451b4a12fSMatthew G. Knepley ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr); 30551b4a12fSMatthew G. Knepley if (flg) { 30651b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 30751b4a12fSMatthew G. Knepley int ray = 0; 30851b4a12fSMatthew G. Knepley DMDADirection ddir; 30951b4a12fSMatthew G. Knepley DM da; 31051b4a12fSMatthew G. Knepley PetscInt howoften = 1; 311d1212d36SBarry Smith 31251b4a12fSMatthew G. Knepley if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 31351b4a12fSMatthew G. Knepley if (dir[0] == 'x') ddir = DMDA_X; 31451b4a12fSMatthew G. Knepley else if (dir[0] == 'y') ddir = DMDA_Y; 31551b4a12fSMatthew G. Knepley else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 31651b4a12fSMatthew G. Knepley sscanf(dir+2, "%d", &ray); 3171c3436cfSJed Brown 31851b4a12fSMatthew G. Knepley ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr); 319b00a9115SJed Brown ierr = PetscNew(&rayctx);CHKERRQ(ierr); 32051b4a12fSMatthew G. Knepley ierr = TSGetDM(ts, &da);CHKERRQ(ierr); 32151b4a12fSMatthew G. Knepley ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr); 32251b4a12fSMatthew G. Knepley ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr); 32351b4a12fSMatthew G. Knepley ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr); 32451b4a12fSMatthew G. Knepley } 325a7a1495cSBarry Smith 326b3d3934dSBarry Smith ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr); 327b3d3934dSBarry Smith if (opt) { 328b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 329b3d3934dSBarry Smith 330b3d3934dSBarry Smith ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr); 331b3d3934dSBarry Smith ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr); 332b3d3934dSBarry Smith } 333b3d3934dSBarry Smith 334847ff0e1SMatthew G. Knepley flg = PETSC_FALSE; 335847ff0e1SMatthew G. Knepley ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr); 336847ff0e1SMatthew G. Knepley if (flg) { 337847ff0e1SMatthew G. Knepley DM dm; 338847ff0e1SMatthew G. Knepley DMTS tdm; 339847ff0e1SMatthew G. Knepley 340847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 341847ff0e1SMatthew G. Knepley ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr); 342847ff0e1SMatthew G. Knepley tdm->ijacobianctx = NULL; 343847ff0e1SMatthew G. Knepley ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr); 344847ff0e1SMatthew G. Knepley ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr); 345847ff0e1SMatthew G. Knepley } 346847ff0e1SMatthew G. Knepley 3477781c08eSBarry Smith /* 3487781c08eSBarry Smith This code is all wrong. One is creating objects inside the TSSetFromOptions() so if run with the options gui 3497781c08eSBarry Smith will bleed memory. Also one is using a PetscOptionsBegin() inside a PetscOptionsBegin() 3507781c08eSBarry Smith */ 351552698daSJed Brown ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr); 352e55864a3SBarry Smith ierr = TSAdaptSetFromOptions(PetscOptionsObject,adapt);CHKERRQ(ierr); 353d763cef2SBarry Smith 354d763cef2SBarry Smith /* Handle specific TS options */ 355d763cef2SBarry Smith if (ts->ops->setfromoptions) { 3566991f827SBarry Smith ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr); 357d763cef2SBarry Smith } 3586991f827SBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 359d763cef2SBarry Smith 360d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 361d763cef2SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr); 362d763cef2SBarry Smith 363bc952696SBarry Smith if (ts->trajectory) { 364bc952696SBarry Smith ierr = TSTrajectorySetFromOptions(ts->trajectory);CHKERRQ(ierr); 365bc952696SBarry Smith } 366bc952696SBarry Smith 3676991f827SBarry Smith ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3686991f827SBarry Smith if (snes) { 3696991f827SBarry Smith if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);} 3706991f827SBarry Smith ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr); 371d763cef2SBarry Smith } 372d763cef2SBarry Smith PetscFunctionReturn(0); 373d763cef2SBarry Smith } 374d763cef2SBarry Smith 375d763cef2SBarry Smith #undef __FUNCT__ 376bc952696SBarry Smith #define __FUNCT__ "TSSetSaveTrajectory" 377d2daff3dSHong Zhang /*@ 378bc952696SBarry Smith TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object 379d2daff3dSHong Zhang 380d2daff3dSHong Zhang Collective on TS 381d2daff3dSHong Zhang 382d2daff3dSHong Zhang Input Parameters: 383bc952696SBarry Smith . ts - the TS context obtained from TSCreate() 384bc952696SBarry Smith 385d2daff3dSHong Zhang 386d2daff3dSHong Zhang Level: intermediate 387d2daff3dSHong Zhang 388bc952696SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve() 389d2daff3dSHong Zhang 390bc952696SBarry Smith .keywords: TS, set, checkpoint, 391d2daff3dSHong Zhang @*/ 392bc952696SBarry Smith PetscErrorCode TSSetSaveTrajectory(TS ts) 393d2daff3dSHong Zhang { 394bc952696SBarry Smith PetscErrorCode ierr; 395bc952696SBarry Smith 396d2daff3dSHong Zhang PetscFunctionBegin; 397d2daff3dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 398bc952696SBarry Smith if (!ts->trajectory) { 399bc952696SBarry Smith ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr); 400920aabf2SBarry Smith ierr = TSTrajectorySetType(ts->trajectory,TSTRAJECTORYBASIC);CHKERRQ(ierr); 401648b50deSBarry Smith ierr = TSTrajectorySetFromOptions(ts->trajectory);CHKERRQ(ierr); 402bc952696SBarry Smith } 403d2daff3dSHong Zhang PetscFunctionReturn(0); 404d2daff3dSHong Zhang } 405d2daff3dSHong Zhang 406a7a1495cSBarry Smith #undef __FUNCT__ 407a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian" 408a7a1495cSBarry Smith /*@ 409a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 410a7a1495cSBarry Smith set with TSSetRHSJacobian(). 411a7a1495cSBarry Smith 412a7a1495cSBarry Smith Collective on TS and Vec 413a7a1495cSBarry Smith 414a7a1495cSBarry Smith Input Parameters: 415316643e7SJed Brown + ts - the TS context 416a7a1495cSBarry Smith . t - current timestep 4170910c330SBarry Smith - U - input vector 418a7a1495cSBarry Smith 419a7a1495cSBarry Smith Output Parameters: 420a7a1495cSBarry Smith + A - Jacobian matrix 421a7a1495cSBarry Smith . B - optional preconditioning matrix 422a7a1495cSBarry Smith - flag - flag indicating matrix structure 423a7a1495cSBarry Smith 424a7a1495cSBarry Smith Notes: 425a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 426a7a1495cSBarry Smith is used internally within the nonlinear solvers. 427a7a1495cSBarry Smith 42894b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 429a7a1495cSBarry Smith flag parameter. 430a7a1495cSBarry Smith 431a7a1495cSBarry Smith Level: developer 432a7a1495cSBarry Smith 433a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix 434a7a1495cSBarry Smith 43594b7f48cSBarry Smith .seealso: TSSetRHSJacobian(), KSPSetOperators() 436a7a1495cSBarry Smith @*/ 437d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B) 438a7a1495cSBarry Smith { 439dfbe8321SBarry Smith PetscErrorCode ierr; 440270bf2e7SJed Brown PetscObjectState Ustate; 44124989b8cSPeter Brune DM dm; 442942e3340SBarry Smith DMTS tsdm; 44324989b8cSPeter Brune TSRHSJacobian rhsjacobianfunc; 44424989b8cSPeter Brune void *ctx; 44524989b8cSPeter Brune TSIJacobian ijacobianfunc; 446b2df71adSDebojyoti Ghosh TSRHSFunction rhsfunction; 447a7a1495cSBarry Smith 448a7a1495cSBarry Smith PetscFunctionBegin; 4490700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4500910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 4510910c330SBarry Smith PetscCheckSameComm(ts,1,U,3); 45224989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 453942e3340SBarry Smith ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr); 45424989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr); 4550298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr); 456b2df71adSDebojyoti Ghosh ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 45759e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr); 458b2df71adSDebojyoti Ghosh if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) { 4590e4ef248SJed Brown PetscFunctionReturn(0); 460f8ede8e7SJed Brown } 461d90be118SSean Farley 462ce94432eSBarry Smith if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 463d90be118SSean Farley 464e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 46594ab13aaSBarry Smith ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr); 46694ab13aaSBarry Smith ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 46794ab13aaSBarry Smith if (A != B) { 46894ab13aaSBarry Smith ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr); 46994ab13aaSBarry Smith ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr); 470e1244c69SJed Brown } 471e1244c69SJed Brown ts->rhsjacobian.shift = 0; 472e1244c69SJed Brown ts->rhsjacobian.scale = 1.; 473e1244c69SJed Brown } 474e1244c69SJed Brown 47524989b8cSPeter Brune if (rhsjacobianfunc) { 47694ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 477a7a1495cSBarry Smith PetscStackPush("TS user Jacobian function"); 478d1e9a80fSBarry Smith ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr); 479a7a1495cSBarry Smith PetscStackPop; 48094ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 481a7a1495cSBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 48294ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,4); 48394ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,5); 484ef66eb69SBarry Smith } else { 48594ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 48694ab13aaSBarry Smith if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);} 487ef66eb69SBarry Smith } 4880e4ef248SJed Brown ts->rhsjacobian.time = t; 4890910c330SBarry Smith ts->rhsjacobian.X = U; 49059e4f3c8SBarry Smith ierr = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr); 491a7a1495cSBarry Smith PetscFunctionReturn(0); 492a7a1495cSBarry Smith } 493a7a1495cSBarry Smith 4944a2ae208SSatish Balay #undef __FUNCT__ 4954a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction" 496316643e7SJed Brown /*@ 497d763cef2SBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function. 498d763cef2SBarry Smith 499316643e7SJed Brown Collective on TS and Vec 500316643e7SJed Brown 501316643e7SJed Brown Input Parameters: 502316643e7SJed Brown + ts - the TS context 503316643e7SJed Brown . t - current time 5040910c330SBarry Smith - U - state vector 505316643e7SJed Brown 506316643e7SJed Brown Output Parameter: 507316643e7SJed Brown . y - right hand side 508316643e7SJed Brown 509316643e7SJed Brown Note: 510316643e7SJed Brown Most users should not need to explicitly call this routine, as it 511316643e7SJed Brown is used internally within the nonlinear solvers. 512316643e7SJed Brown 513316643e7SJed Brown Level: developer 514316643e7SJed Brown 515316643e7SJed Brown .keywords: TS, compute 516316643e7SJed Brown 517316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction() 518316643e7SJed Brown @*/ 5190910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y) 520d763cef2SBarry Smith { 521dfbe8321SBarry Smith PetscErrorCode ierr; 52224989b8cSPeter Brune TSRHSFunction rhsfunction; 52324989b8cSPeter Brune TSIFunction ifunction; 52424989b8cSPeter Brune void *ctx; 52524989b8cSPeter Brune DM dm; 52624989b8cSPeter Brune 527d763cef2SBarry Smith PetscFunctionBegin; 5280700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5290910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 5300700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,4); 53124989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 53224989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr); 5330298fd71SBarry Smith ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr); 534d763cef2SBarry Smith 535ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 536d763cef2SBarry Smith 5370910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 53824989b8cSPeter Brune if (rhsfunction) { 539d763cef2SBarry Smith PetscStackPush("TS user right-hand-side function"); 5400910c330SBarry Smith ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr); 541d763cef2SBarry Smith PetscStackPop; 542214bc6a2SJed Brown } else { 543214bc6a2SJed Brown ierr = VecZeroEntries(y);CHKERRQ(ierr); 544b2cd27e8SJed Brown } 54544a41b28SSean Farley 5460910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr); 547d763cef2SBarry Smith PetscFunctionReturn(0); 548d763cef2SBarry Smith } 549d763cef2SBarry Smith 5504a2ae208SSatish Balay #undef __FUNCT__ 551ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction" 552ef20d060SBarry Smith /*@ 553ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 554ef20d060SBarry Smith 555ef20d060SBarry Smith Collective on TS and Vec 556ef20d060SBarry Smith 557ef20d060SBarry Smith Input Parameters: 558ef20d060SBarry Smith + ts - the TS context 559ef20d060SBarry Smith - t - current time 560ef20d060SBarry Smith 561ef20d060SBarry Smith Output Parameter: 5620910c330SBarry Smith . U - the solution 563ef20d060SBarry Smith 564ef20d060SBarry Smith Note: 565ef20d060SBarry Smith Most users should not need to explicitly call this routine, as it 566ef20d060SBarry Smith is used internally within the nonlinear solvers. 567ef20d060SBarry Smith 568ef20d060SBarry Smith Level: developer 569ef20d060SBarry Smith 570ef20d060SBarry Smith .keywords: TS, compute 571ef20d060SBarry Smith 572abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 573ef20d060SBarry Smith @*/ 5740910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U) 575ef20d060SBarry Smith { 576ef20d060SBarry Smith PetscErrorCode ierr; 577ef20d060SBarry Smith TSSolutionFunction solutionfunction; 578ef20d060SBarry Smith void *ctx; 579ef20d060SBarry Smith DM dm; 580ef20d060SBarry Smith 581ef20d060SBarry Smith PetscFunctionBegin; 582ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5830910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 584ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 585ef20d060SBarry Smith ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr); 586ef20d060SBarry Smith 587ef20d060SBarry Smith if (solutionfunction) { 5889b7cd975SBarry Smith PetscStackPush("TS user solution function"); 5890910c330SBarry Smith ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr); 590ef20d060SBarry Smith PetscStackPop; 591ef20d060SBarry Smith } 592ef20d060SBarry Smith PetscFunctionReturn(0); 593ef20d060SBarry Smith } 5949b7cd975SBarry Smith #undef __FUNCT__ 5959b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction" 5969b7cd975SBarry Smith /*@ 5979b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 5989b7cd975SBarry Smith 5999b7cd975SBarry Smith Collective on TS and Vec 6009b7cd975SBarry Smith 6019b7cd975SBarry Smith Input Parameters: 6029b7cd975SBarry Smith + ts - the TS context 6039b7cd975SBarry Smith - t - current time 6049b7cd975SBarry Smith 6059b7cd975SBarry Smith Output Parameter: 6069b7cd975SBarry Smith . U - the function value 6079b7cd975SBarry Smith 6089b7cd975SBarry Smith Note: 6099b7cd975SBarry Smith Most users should not need to explicitly call this routine, as it 6109b7cd975SBarry Smith is used internally within the nonlinear solvers. 6119b7cd975SBarry Smith 6129b7cd975SBarry Smith Level: developer 6139b7cd975SBarry Smith 6149b7cd975SBarry Smith .keywords: TS, compute 6159b7cd975SBarry Smith 6169b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction() 6179b7cd975SBarry Smith @*/ 6189b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U) 6199b7cd975SBarry Smith { 6209b7cd975SBarry Smith PetscErrorCode ierr, (*forcing)(TS,PetscReal,Vec,void*); 6219b7cd975SBarry Smith void *ctx; 6229b7cd975SBarry Smith DM dm; 6239b7cd975SBarry Smith 6249b7cd975SBarry Smith PetscFunctionBegin; 6259b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6269b7cd975SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 6279b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 6289b7cd975SBarry Smith ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr); 6299b7cd975SBarry Smith 6309b7cd975SBarry Smith if (forcing) { 6319b7cd975SBarry Smith PetscStackPush("TS user forcing function"); 6329b7cd975SBarry Smith ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr); 6339b7cd975SBarry Smith PetscStackPop; 6349b7cd975SBarry Smith } 6359b7cd975SBarry Smith PetscFunctionReturn(0); 6369b7cd975SBarry Smith } 637ef20d060SBarry Smith 638ef20d060SBarry Smith #undef __FUNCT__ 639214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private" 640214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs) 641214bc6a2SJed Brown { 6422dd45cf8SJed Brown Vec F; 643214bc6a2SJed Brown PetscErrorCode ierr; 644214bc6a2SJed Brown 645214bc6a2SJed Brown PetscFunctionBegin; 6460298fd71SBarry Smith *Frhs = NULL; 6470298fd71SBarry Smith ierr = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr); 648214bc6a2SJed Brown if (!ts->Frhs) { 6492dd45cf8SJed Brown ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr); 650214bc6a2SJed Brown } 651214bc6a2SJed Brown *Frhs = ts->Frhs; 652214bc6a2SJed Brown PetscFunctionReturn(0); 653214bc6a2SJed Brown } 654214bc6a2SJed Brown 655214bc6a2SJed Brown #undef __FUNCT__ 656214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private" 657214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs) 658214bc6a2SJed Brown { 659214bc6a2SJed Brown Mat A,B; 6602dd45cf8SJed Brown PetscErrorCode ierr; 661214bc6a2SJed Brown 662214bc6a2SJed Brown PetscFunctionBegin; 663c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 664c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 6650298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 666214bc6a2SJed Brown if (Arhs) { 667214bc6a2SJed Brown if (!ts->Arhs) { 668214bc6a2SJed Brown ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr); 669214bc6a2SJed Brown } 670214bc6a2SJed Brown *Arhs = ts->Arhs; 671214bc6a2SJed Brown } 672214bc6a2SJed Brown if (Brhs) { 673214bc6a2SJed Brown if (!ts->Brhs) { 674bdb70873SJed Brown if (A != B) { 675214bc6a2SJed Brown ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr); 676bdb70873SJed Brown } else { 677bdb70873SJed Brown ts->Brhs = ts->Arhs; 678bdb70873SJed Brown ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr); 679bdb70873SJed Brown } 680214bc6a2SJed Brown } 681214bc6a2SJed Brown *Brhs = ts->Brhs; 682214bc6a2SJed Brown } 683214bc6a2SJed Brown PetscFunctionReturn(0); 684214bc6a2SJed Brown } 685214bc6a2SJed Brown 686214bc6a2SJed Brown #undef __FUNCT__ 687316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction" 688316643e7SJed Brown /*@ 6890910c330SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0 690316643e7SJed Brown 691316643e7SJed Brown Collective on TS and Vec 692316643e7SJed Brown 693316643e7SJed Brown Input Parameters: 694316643e7SJed Brown + ts - the TS context 695316643e7SJed Brown . t - current time 6960910c330SBarry Smith . U - state vector 6970910c330SBarry Smith . Udot - time derivative of state vector 698214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate 699316643e7SJed Brown 700316643e7SJed Brown Output Parameter: 701316643e7SJed Brown . Y - right hand side 702316643e7SJed Brown 703316643e7SJed Brown Note: 704316643e7SJed Brown Most users should not need to explicitly call this routine, as it 705316643e7SJed Brown is used internally within the nonlinear solvers. 706316643e7SJed Brown 707316643e7SJed Brown If the user did did not write their equations in implicit form, this 708316643e7SJed Brown function recasts them in implicit form. 709316643e7SJed Brown 710316643e7SJed Brown Level: developer 711316643e7SJed Brown 712316643e7SJed Brown .keywords: TS, compute 713316643e7SJed Brown 714316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction() 715316643e7SJed Brown @*/ 7160910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex) 717316643e7SJed Brown { 718316643e7SJed Brown PetscErrorCode ierr; 71924989b8cSPeter Brune TSIFunction ifunction; 72024989b8cSPeter Brune TSRHSFunction rhsfunction; 72124989b8cSPeter Brune void *ctx; 72224989b8cSPeter Brune DM dm; 723316643e7SJed Brown 724316643e7SJed Brown PetscFunctionBegin; 7250700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 7260910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 7270910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 7280700a824SBarry Smith PetscValidHeaderSpecific(Y,VEC_CLASSID,5); 729316643e7SJed Brown 73024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 73124989b8cSPeter Brune ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr); 7320298fd71SBarry Smith ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr); 73324989b8cSPeter Brune 734ce94432eSBarry Smith if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()"); 735d90be118SSean Farley 7360910c330SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 73724989b8cSPeter Brune if (ifunction) { 738316643e7SJed Brown PetscStackPush("TS user implicit function"); 7390910c330SBarry Smith ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr); 740316643e7SJed Brown PetscStackPop; 741214bc6a2SJed Brown } 742214bc6a2SJed Brown if (imex) { 74324989b8cSPeter Brune if (!ifunction) { 7440910c330SBarry Smith ierr = VecCopy(Udot,Y);CHKERRQ(ierr); 7452dd45cf8SJed Brown } 74624989b8cSPeter Brune } else if (rhsfunction) { 74724989b8cSPeter Brune if (ifunction) { 748214bc6a2SJed Brown Vec Frhs; 749214bc6a2SJed Brown ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr); 7500910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr); 751214bc6a2SJed Brown ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr); 7522dd45cf8SJed Brown } else { 7530910c330SBarry Smith ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr); 7540910c330SBarry Smith ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr); 755316643e7SJed Brown } 7564a6899ffSJed Brown } 7570910c330SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr); 758316643e7SJed Brown PetscFunctionReturn(0); 759316643e7SJed Brown } 760316643e7SJed Brown 761316643e7SJed Brown #undef __FUNCT__ 762316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian" 763316643e7SJed Brown /*@ 764316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 765316643e7SJed Brown 766316643e7SJed Brown Collective on TS and Vec 767316643e7SJed Brown 768316643e7SJed Brown Input 769316643e7SJed Brown Input Parameters: 770316643e7SJed Brown + ts - the TS context 771316643e7SJed Brown . t - current timestep 7720910c330SBarry Smith . U - state vector 7730910c330SBarry Smith . Udot - time derivative of state vector 774214bc6a2SJed Brown . shift - shift to apply, see note below 775214bc6a2SJed Brown - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate 776316643e7SJed Brown 777316643e7SJed Brown Output Parameters: 778316643e7SJed Brown + A - Jacobian matrix 779316643e7SJed Brown . B - optional preconditioning matrix 780316643e7SJed Brown - flag - flag indicating matrix structure 781316643e7SJed Brown 782316643e7SJed Brown Notes: 7830910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 784316643e7SJed Brown 7850910c330SBarry Smith dF/dU + shift*dF/dUdot 786316643e7SJed Brown 787316643e7SJed Brown Most users should not need to explicitly call this routine, as it 788316643e7SJed Brown is used internally within the nonlinear solvers. 789316643e7SJed Brown 790316643e7SJed Brown Level: developer 791316643e7SJed Brown 792316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix 793316643e7SJed Brown 794316643e7SJed Brown .seealso: TSSetIJacobian() 79563495f91SJed Brown @*/ 796d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex) 797316643e7SJed Brown { 798316643e7SJed Brown PetscErrorCode ierr; 79924989b8cSPeter Brune TSIJacobian ijacobian; 80024989b8cSPeter Brune TSRHSJacobian rhsjacobian; 80124989b8cSPeter Brune DM dm; 80224989b8cSPeter Brune void *ctx; 803316643e7SJed Brown 804316643e7SJed Brown PetscFunctionBegin; 8050700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 8060910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 8070910c330SBarry Smith PetscValidHeaderSpecific(Udot,VEC_CLASSID,4); 808316643e7SJed Brown PetscValidPointer(A,6); 80994ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,6); 810316643e7SJed Brown PetscValidPointer(B,7); 81194ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,7); 81224989b8cSPeter Brune 81324989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 81424989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr); 8150298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr); 81624989b8cSPeter Brune 817ce94432eSBarry Smith if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 818316643e7SJed Brown 81994ab13aaSBarry Smith ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 82024989b8cSPeter Brune if (ijacobian) { 821316643e7SJed Brown PetscStackPush("TS user implicit Jacobian"); 822d1e9a80fSBarry Smith ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr); 823316643e7SJed Brown PetscStackPop; 824214bc6a2SJed Brown /* make sure user returned a correct Jacobian and preconditioner */ 82594ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,4); 82694ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,5); 8274a6899ffSJed Brown } 828214bc6a2SJed Brown if (imex) { 829b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 83094ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 83194ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 83294ab13aaSBarry Smith if (A != B) { 83394ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 83494ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 835214bc6a2SJed Brown } 836214bc6a2SJed Brown } 837214bc6a2SJed Brown } else { 838e1244c69SJed Brown Mat Arhs = NULL,Brhs = NULL; 839e1244c69SJed Brown if (rhsjacobian) { 840bd373395SBarry Smith if (ijacobian) { 841214bc6a2SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 842bd373395SBarry Smith } else { 843bd373395SBarry Smith ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr); 844214bc6a2SJed Brown } 845d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 846e1244c69SJed Brown } 84794ab13aaSBarry Smith if (Arhs == A) { /* No IJacobian, so we only have the RHS matrix */ 848e1244c69SJed Brown ts->rhsjacobian.scale = -1; 849e1244c69SJed Brown ts->rhsjacobian.shift = shift; 85094ab13aaSBarry Smith ierr = MatScale(A,-1);CHKERRQ(ierr); 85194ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 85294ab13aaSBarry Smith if (A != B) { 85394ab13aaSBarry Smith ierr = MatScale(B,-1);CHKERRQ(ierr); 85494ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 855316643e7SJed Brown } 856e1244c69SJed Brown } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 857e1244c69SJed Brown MatStructure axpy = DIFFERENT_NONZERO_PATTERN; 858e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 85994ab13aaSBarry Smith ierr = MatZeroEntries(A);CHKERRQ(ierr); 86094ab13aaSBarry Smith ierr = MatShift(A,shift);CHKERRQ(ierr); 86194ab13aaSBarry Smith if (A != B) { 86294ab13aaSBarry Smith ierr = MatZeroEntries(B);CHKERRQ(ierr); 86394ab13aaSBarry Smith ierr = MatShift(B,shift);CHKERRQ(ierr); 864214bc6a2SJed Brown } 865316643e7SJed Brown } 86694ab13aaSBarry Smith ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr); 86794ab13aaSBarry Smith if (A != B) { 86894ab13aaSBarry Smith ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr); 869316643e7SJed Brown } 870316643e7SJed Brown } 871316643e7SJed Brown } 87294ab13aaSBarry Smith ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr); 873316643e7SJed Brown PetscFunctionReturn(0); 874316643e7SJed Brown } 875316643e7SJed Brown 876316643e7SJed Brown #undef __FUNCT__ 8774a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction" 878d763cef2SBarry Smith /*@C 879d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 880b5abc632SBarry Smith where U_t = G(t,u). 881d763cef2SBarry Smith 8823f9fe445SBarry Smith Logically Collective on TS 883d763cef2SBarry Smith 884d763cef2SBarry Smith Input Parameters: 885d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 8860298fd71SBarry Smith . r - vector to put the computed right hand side (or NULL to have it created) 887d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 888d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 8890298fd71SBarry Smith function evaluation routine (may be NULL) 890d763cef2SBarry Smith 891d763cef2SBarry Smith Calling sequence of func: 89287828ca2SBarry Smith $ func (TS ts,PetscReal t,Vec u,Vec F,void *ctx); 893d763cef2SBarry Smith 894d763cef2SBarry Smith + t - current timestep 895d763cef2SBarry Smith . u - input vector 896d763cef2SBarry Smith . F - function vector 897d763cef2SBarry Smith - ctx - [optional] user-defined function context 898d763cef2SBarry Smith 899d763cef2SBarry Smith Level: beginner 900d763cef2SBarry Smith 9012bbac0d3SBarry Smith Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE. 9022bbac0d3SBarry Smith 903d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 904d763cef2SBarry Smith 905ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction() 906d763cef2SBarry Smith @*/ 907089b2837SJed Brown PetscErrorCode TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx) 908d763cef2SBarry Smith { 909089b2837SJed Brown PetscErrorCode ierr; 910089b2837SJed Brown SNES snes; 9110298fd71SBarry Smith Vec ralloc = NULL; 91224989b8cSPeter Brune DM dm; 913d763cef2SBarry Smith 914089b2837SJed Brown PetscFunctionBegin; 9150700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 916ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2); 91724989b8cSPeter Brune 91824989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 91924989b8cSPeter Brune ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr); 920089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 921e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 922e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr); 923e856ceecSJed Brown r = ralloc; 924e856ceecSJed Brown } 925089b2837SJed Brown ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr); 926e856ceecSJed Brown ierr = VecDestroy(&ralloc);CHKERRQ(ierr); 927d763cef2SBarry Smith PetscFunctionReturn(0); 928d763cef2SBarry Smith } 929d763cef2SBarry Smith 9304a2ae208SSatish Balay #undef __FUNCT__ 931ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction" 932ef20d060SBarry Smith /*@C 933abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 934ef20d060SBarry Smith 935ef20d060SBarry Smith Logically Collective on TS 936ef20d060SBarry Smith 937ef20d060SBarry Smith Input Parameters: 938ef20d060SBarry Smith + ts - the TS context obtained from TSCreate() 939ef20d060SBarry Smith . f - routine for evaluating the solution 940ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 9410298fd71SBarry Smith function evaluation routine (may be NULL) 942ef20d060SBarry Smith 943ef20d060SBarry Smith Calling sequence of func: 944ef20d060SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 945ef20d060SBarry Smith 946ef20d060SBarry Smith + t - current timestep 947ef20d060SBarry Smith . u - output vector 948ef20d060SBarry Smith - ctx - [optional] user-defined function context 949ef20d060SBarry Smith 950abd5a294SJed Brown Notes: 951abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 952abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 953abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 954abd5a294SJed Brown 9554f09c107SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 956abd5a294SJed Brown 957ef20d060SBarry Smith Level: beginner 958ef20d060SBarry Smith 959ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 960ef20d060SBarry Smith 9619b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction() 962ef20d060SBarry Smith @*/ 963ef20d060SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 964ef20d060SBarry Smith { 965ef20d060SBarry Smith PetscErrorCode ierr; 966ef20d060SBarry Smith DM dm; 967ef20d060SBarry Smith 968ef20d060SBarry Smith PetscFunctionBegin; 969ef20d060SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 970ef20d060SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 971ef20d060SBarry Smith ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr); 972ef20d060SBarry Smith PetscFunctionReturn(0); 973ef20d060SBarry Smith } 974ef20d060SBarry Smith 975ef20d060SBarry Smith #undef __FUNCT__ 9769b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction" 9779b7cd975SBarry Smith /*@C 9789b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 9799b7cd975SBarry Smith 9809b7cd975SBarry Smith Logically Collective on TS 9819b7cd975SBarry Smith 9829b7cd975SBarry Smith Input Parameters: 9839b7cd975SBarry Smith + ts - the TS context obtained from TSCreate() 9849b7cd975SBarry Smith . f - routine for evaluating the forcing function 9859b7cd975SBarry Smith - ctx - [optional] user-defined context for private data for the 9860298fd71SBarry Smith function evaluation routine (may be NULL) 9879b7cd975SBarry Smith 9889b7cd975SBarry Smith Calling sequence of func: 9899b7cd975SBarry Smith $ func (TS ts,PetscReal t,Vec u,void *ctx); 9909b7cd975SBarry Smith 9919b7cd975SBarry Smith + t - current timestep 9929b7cd975SBarry Smith . u - output vector 9939b7cd975SBarry Smith - ctx - [optional] user-defined function context 9949b7cd975SBarry Smith 9959b7cd975SBarry Smith Notes: 9969b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 9979b7cd975SBarry Smith create closed-form solutions with a non-physical forcing term. 9989b7cd975SBarry Smith 9999b7cd975SBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history. 10009b7cd975SBarry Smith 10019b7cd975SBarry Smith Level: beginner 10029b7cd975SBarry Smith 10039b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function 10049b7cd975SBarry Smith 10059b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction() 10069b7cd975SBarry Smith @*/ 10079b7cd975SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx) 10089b7cd975SBarry Smith { 10099b7cd975SBarry Smith PetscErrorCode ierr; 10109b7cd975SBarry Smith DM dm; 10119b7cd975SBarry Smith 10129b7cd975SBarry Smith PetscFunctionBegin; 10139b7cd975SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 10149b7cd975SBarry Smith ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 10159b7cd975SBarry Smith ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr); 10169b7cd975SBarry Smith PetscFunctionReturn(0); 10179b7cd975SBarry Smith } 10189b7cd975SBarry Smith 10199b7cd975SBarry Smith #undef __FUNCT__ 10204a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian" 1021d763cef2SBarry Smith /*@C 1022f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1023b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1024d763cef2SBarry Smith 10253f9fe445SBarry Smith Logically Collective on TS 1026d763cef2SBarry Smith 1027d763cef2SBarry Smith Input Parameters: 1028d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1029e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1030e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1031d763cef2SBarry Smith . f - the Jacobian evaluation routine 1032d763cef2SBarry Smith - ctx - [optional] user-defined context for private data for the 10330298fd71SBarry Smith Jacobian evaluation routine (may be NULL) 1034d763cef2SBarry Smith 1035f7ab8db6SBarry Smith Calling sequence of f: 1036ae8867d6SBarry Smith $ func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx); 1037d763cef2SBarry Smith 1038d763cef2SBarry Smith + t - current timestep 1039d763cef2SBarry Smith . u - input vector 1040e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1041e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat) 1042d763cef2SBarry Smith - ctx - [optional] user-defined context for matrix evaluation routine 1043d763cef2SBarry Smith 1044d763cef2SBarry Smith 1045d763cef2SBarry Smith Level: beginner 1046d763cef2SBarry Smith 1047d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian 1048d763cef2SBarry Smith 1049ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian() 1050d763cef2SBarry Smith 1051d763cef2SBarry Smith @*/ 1052e5d3d808SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx) 1053d763cef2SBarry Smith { 1054277b19d0SLisandro Dalcin PetscErrorCode ierr; 1055089b2837SJed Brown SNES snes; 105624989b8cSPeter Brune DM dm; 105724989b8cSPeter Brune TSIJacobian ijacobian; 1058277b19d0SLisandro Dalcin 1059d763cef2SBarry Smith PetscFunctionBegin; 10600700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1061e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1062e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1063e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1064e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 1065d763cef2SBarry Smith 106624989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 106724989b8cSPeter Brune ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr); 1068e1244c69SJed Brown if (f == TSComputeRHSJacobianConstant) { 1069e1244c69SJed Brown /* Handle this case automatically for the user; otherwise user should call themselves. */ 1070e1244c69SJed Brown ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr); 1071e1244c69SJed Brown } 10720298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr); 1073089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 10745f659677SPeter Brune if (!ijacobian) { 1075e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 10760e4ef248SJed Brown } 1077e5d3d808SBarry Smith if (Amat) { 1078e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 10790e4ef248SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 1080bbd56ea5SKarl Rupp 1081e5d3d808SBarry Smith ts->Arhs = Amat; 10820e4ef248SJed Brown } 1083e5d3d808SBarry Smith if (Pmat) { 1084e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 10850e4ef248SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 1086bbd56ea5SKarl Rupp 1087e5d3d808SBarry Smith ts->Brhs = Pmat; 10880e4ef248SJed Brown } 1089d763cef2SBarry Smith PetscFunctionReturn(0); 1090d763cef2SBarry Smith } 1091d763cef2SBarry Smith 1092316643e7SJed Brown 1093316643e7SJed Brown #undef __FUNCT__ 1094316643e7SJed Brown #define __FUNCT__ "TSSetIFunction" 1095316643e7SJed Brown /*@C 1096b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1097316643e7SJed Brown 10983f9fe445SBarry Smith Logically Collective on TS 1099316643e7SJed Brown 1100316643e7SJed Brown Input Parameters: 1101316643e7SJed Brown + ts - the TS context obtained from TSCreate() 11020298fd71SBarry Smith . r - vector to hold the residual (or NULL to have it created internally) 1103316643e7SJed Brown . f - the function evaluation routine 11040298fd71SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be NULL) 1105316643e7SJed Brown 1106316643e7SJed Brown Calling sequence of f: 1107316643e7SJed Brown $ f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx); 1108316643e7SJed Brown 1109316643e7SJed Brown + t - time at step/stage being solved 1110316643e7SJed Brown . u - state vector 1111316643e7SJed Brown . u_t - time derivative of state vector 1112316643e7SJed Brown . F - function vector 1113316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1114316643e7SJed Brown 1115316643e7SJed Brown Important: 11162bbac0d3SBarry Smith The user MUST call either this routine or TSSetRHSFunction() to define the ODE. When solving DAEs you must use this function. 1117316643e7SJed Brown 1118316643e7SJed Brown Level: beginner 1119316643e7SJed Brown 1120316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian 1121316643e7SJed Brown 1122d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian() 1123316643e7SJed Brown @*/ 1124089b2837SJed Brown PetscErrorCode TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx) 1125316643e7SJed Brown { 1126089b2837SJed Brown PetscErrorCode ierr; 1127089b2837SJed Brown SNES snes; 11280298fd71SBarry Smith Vec resalloc = NULL; 112924989b8cSPeter Brune DM dm; 1130316643e7SJed Brown 1131316643e7SJed Brown PetscFunctionBegin; 11320700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1133ca94891dSJed Brown if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2); 113424989b8cSPeter Brune 113524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 113624989b8cSPeter Brune ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr); 113724989b8cSPeter Brune 1138089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1139e856ceecSJed Brown if (!res && !ts->dm && ts->vec_sol) { 1140e856ceecSJed Brown ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr); 1141e856ceecSJed Brown res = resalloc; 1142e856ceecSJed Brown } 1143089b2837SJed Brown ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr); 1144e856ceecSJed Brown ierr = VecDestroy(&resalloc);CHKERRQ(ierr); 1145089b2837SJed Brown PetscFunctionReturn(0); 1146089b2837SJed Brown } 1147089b2837SJed Brown 1148089b2837SJed Brown #undef __FUNCT__ 1149089b2837SJed Brown #define __FUNCT__ "TSGetIFunction" 1150089b2837SJed Brown /*@C 1151089b2837SJed Brown TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it. 1152089b2837SJed Brown 1153089b2837SJed Brown Not Collective 1154089b2837SJed Brown 1155089b2837SJed Brown Input Parameter: 1156089b2837SJed Brown . ts - the TS context 1157089b2837SJed Brown 1158089b2837SJed Brown Output Parameter: 11590298fd71SBarry Smith + r - vector to hold residual (or NULL) 11600298fd71SBarry Smith . func - the function to compute residual (or NULL) 11610298fd71SBarry Smith - ctx - the function context (or NULL) 1162089b2837SJed Brown 1163089b2837SJed Brown Level: advanced 1164089b2837SJed Brown 1165089b2837SJed Brown .keywords: TS, nonlinear, get, function 1166089b2837SJed Brown 1167089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction() 1168089b2837SJed Brown @*/ 1169089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx) 1170089b2837SJed Brown { 1171089b2837SJed Brown PetscErrorCode ierr; 1172089b2837SJed Brown SNES snes; 117324989b8cSPeter Brune DM dm; 1174089b2837SJed Brown 1175089b2837SJed Brown PetscFunctionBegin; 1176089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1177089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 11780298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 117924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 118024989b8cSPeter Brune ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr); 1181089b2837SJed Brown PetscFunctionReturn(0); 1182089b2837SJed Brown } 1183089b2837SJed Brown 1184089b2837SJed Brown #undef __FUNCT__ 1185089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction" 1186089b2837SJed Brown /*@C 1187089b2837SJed Brown TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it. 1188089b2837SJed Brown 1189089b2837SJed Brown Not Collective 1190089b2837SJed Brown 1191089b2837SJed Brown Input Parameter: 1192089b2837SJed Brown . ts - the TS context 1193089b2837SJed Brown 1194089b2837SJed Brown Output Parameter: 11950298fd71SBarry Smith + r - vector to hold computed right hand side (or NULL) 11960298fd71SBarry Smith . func - the function to compute right hand side (or NULL) 11970298fd71SBarry Smith - ctx - the function context (or NULL) 1198089b2837SJed Brown 1199089b2837SJed Brown Level: advanced 1200089b2837SJed Brown 1201089b2837SJed Brown .keywords: TS, nonlinear, get, function 1202089b2837SJed Brown 12032bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction() 1204089b2837SJed Brown @*/ 1205089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx) 1206089b2837SJed Brown { 1207089b2837SJed Brown PetscErrorCode ierr; 1208089b2837SJed Brown SNES snes; 120924989b8cSPeter Brune DM dm; 1210089b2837SJed Brown 1211089b2837SJed Brown PetscFunctionBegin; 1212089b2837SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1213089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 12140298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 121524989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 121624989b8cSPeter Brune ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr); 1217316643e7SJed Brown PetscFunctionReturn(0); 1218316643e7SJed Brown } 1219316643e7SJed Brown 1220316643e7SJed Brown #undef __FUNCT__ 1221316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian" 1222316643e7SJed Brown /*@C 1223a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1224ae8867d6SBarry Smith provided with TSSetIFunction(). 1225316643e7SJed Brown 12263f9fe445SBarry Smith Logically Collective on TS 1227316643e7SJed Brown 1228316643e7SJed Brown Input Parameters: 1229316643e7SJed Brown + ts - the TS context obtained from TSCreate() 1230e5d3d808SBarry Smith . Amat - (approximate) Jacobian matrix 1231e5d3d808SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as Amat) 1232316643e7SJed Brown . f - the Jacobian evaluation routine 12330298fd71SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL) 1234316643e7SJed Brown 1235316643e7SJed Brown Calling sequence of f: 1236ae8867d6SBarry Smith $ f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx); 1237316643e7SJed Brown 1238316643e7SJed Brown + t - time at step/stage being solved 12391b4a444bSJed Brown . U - state vector 12401b4a444bSJed Brown . U_t - time derivative of state vector 1241316643e7SJed Brown . a - shift 1242e5d3d808SBarry Smith . Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t 1243e5d3d808SBarry Smith . Pmat - matrix used for constructing preconditioner, usually the same as Amat 1244316643e7SJed Brown - ctx - [optional] user-defined context for matrix evaluation routine 1245316643e7SJed Brown 1246316643e7SJed Brown Notes: 1247e5d3d808SBarry Smith The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve. 1248316643e7SJed Brown 1249895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 1250895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 1251895c21f2SBarry Smith 1252a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1253b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1254a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1255a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1256a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1257a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1258a4f0a591SBarry Smith 1259316643e7SJed Brown Level: beginner 1260316643e7SJed Brown 1261316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian 1262316643e7SJed Brown 1263ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction() 1264316643e7SJed Brown 1265316643e7SJed Brown @*/ 1266e5d3d808SBarry Smith PetscErrorCode TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx) 1267316643e7SJed Brown { 1268316643e7SJed Brown PetscErrorCode ierr; 1269089b2837SJed Brown SNES snes; 127024989b8cSPeter Brune DM dm; 1271316643e7SJed Brown 1272316643e7SJed Brown PetscFunctionBegin; 12730700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1274e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 1275e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 1276e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts,1,Amat,2); 1277e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts,1,Pmat,3); 127824989b8cSPeter Brune 127924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 128024989b8cSPeter Brune ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr); 128124989b8cSPeter Brune 1282089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1283e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr); 1284316643e7SJed Brown PetscFunctionReturn(0); 1285316643e7SJed Brown } 1286316643e7SJed Brown 12874a2ae208SSatish Balay #undef __FUNCT__ 1288e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse" 1289e1244c69SJed Brown /*@ 1290e1244c69SJed Brown TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating. Without this flag, TS will change the sign and 1291e1244c69SJed Brown shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute 1292e1244c69SJed Brown the entire Jacobian. The reuse flag must be set if the evaluation function will assume that the matrix entries have 1293e1244c69SJed Brown not been changed by the TS. 1294e1244c69SJed Brown 1295e1244c69SJed Brown Logically Collective 1296e1244c69SJed Brown 1297e1244c69SJed Brown Input Arguments: 1298e1244c69SJed Brown + ts - TS context obtained from TSCreate() 1299e1244c69SJed Brown - reuse - PETSC_TRUE if the RHS Jacobian 1300e1244c69SJed Brown 1301e1244c69SJed Brown Level: intermediate 1302e1244c69SJed Brown 1303e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 1304e1244c69SJed Brown @*/ 1305e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse) 1306e1244c69SJed Brown { 1307e1244c69SJed Brown PetscFunctionBegin; 1308e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 1309e1244c69SJed Brown PetscFunctionReturn(0); 1310e1244c69SJed Brown } 1311e1244c69SJed Brown 1312e1244c69SJed Brown #undef __FUNCT__ 131355849f57SBarry Smith #define __FUNCT__ "TSLoad" 131455849f57SBarry Smith /*@C 131555849f57SBarry Smith TSLoad - Loads a KSP that has been stored in binary with KSPView(). 131655849f57SBarry Smith 131755849f57SBarry Smith Collective on PetscViewer 131855849f57SBarry Smith 131955849f57SBarry Smith Input Parameters: 132055849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or 132155849f57SBarry Smith some related function before a call to TSLoad(). 132255849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 132355849f57SBarry Smith 132455849f57SBarry Smith Level: intermediate 132555849f57SBarry Smith 132655849f57SBarry Smith Notes: 132755849f57SBarry Smith The type is determined by the data in the file, any type set into the TS before this call is ignored. 132855849f57SBarry Smith 132955849f57SBarry Smith Notes for advanced users: 133055849f57SBarry Smith Most users should not need to know the details of the binary storage 133155849f57SBarry Smith format, since TSLoad() and TSView() completely hide these details. 133255849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 133355849f57SBarry Smith format is 133455849f57SBarry Smith .vb 133555849f57SBarry Smith has not yet been determined 133655849f57SBarry Smith .ve 133755849f57SBarry Smith 133855849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad() 133955849f57SBarry Smith @*/ 1340f2c2a1b9SBarry Smith PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 134155849f57SBarry Smith { 134255849f57SBarry Smith PetscErrorCode ierr; 134355849f57SBarry Smith PetscBool isbinary; 134455849f57SBarry Smith PetscInt classid; 134555849f57SBarry Smith char type[256]; 13462d53ad75SBarry Smith DMTS sdm; 1347ad6bc421SBarry Smith DM dm; 134855849f57SBarry Smith 134955849f57SBarry Smith PetscFunctionBegin; 1350f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 135155849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 135255849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 135355849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 135455849f57SBarry Smith 1355060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr); 1356ce94432eSBarry Smith if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file"); 1357060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr); 1358f2c2a1b9SBarry Smith ierr = TSSetType(ts, type);CHKERRQ(ierr); 1359f2c2a1b9SBarry Smith if (ts->ops->load) { 1360f2c2a1b9SBarry Smith ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr); 1361f2c2a1b9SBarry Smith } 1362ce94432eSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr); 1363ad6bc421SBarry Smith ierr = DMLoad(dm,viewer);CHKERRQ(ierr); 1364ad6bc421SBarry Smith ierr = TSSetDM(ts,dm);CHKERRQ(ierr); 1365f2c2a1b9SBarry Smith ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr); 1366f2c2a1b9SBarry Smith ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr); 13672d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 13682d53ad75SBarry Smith ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr); 136955849f57SBarry Smith PetscFunctionReturn(0); 137055849f57SBarry Smith } 137155849f57SBarry Smith 13729804daf3SBarry Smith #include <petscdraw.h> 1373e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1374e04113cfSBarry Smith #include <petscviewersaws.h> 1375f05ece33SBarry Smith #endif 137655849f57SBarry Smith #undef __FUNCT__ 13774a2ae208SSatish Balay #define __FUNCT__ "TSView" 13787e2c5f70SBarry Smith /*@C 1379d763cef2SBarry Smith TSView - Prints the TS data structure. 1380d763cef2SBarry Smith 13814c49b128SBarry Smith Collective on TS 1382d763cef2SBarry Smith 1383d763cef2SBarry Smith Input Parameters: 1384d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1385d763cef2SBarry Smith - viewer - visualization context 1386d763cef2SBarry Smith 1387d763cef2SBarry Smith Options Database Key: 1388d763cef2SBarry Smith . -ts_view - calls TSView() at end of TSStep() 1389d763cef2SBarry Smith 1390d763cef2SBarry Smith Notes: 1391d763cef2SBarry Smith The available visualization contexts include 1392b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 1393b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 1394d763cef2SBarry Smith output where only the first processor opens 1395d763cef2SBarry Smith the file. All other processors send their 1396d763cef2SBarry Smith data to the first processor to print. 1397d763cef2SBarry Smith 1398d763cef2SBarry Smith The user can open an alternative visualization context with 1399b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1400d763cef2SBarry Smith 1401d763cef2SBarry Smith Level: beginner 1402d763cef2SBarry Smith 1403d763cef2SBarry Smith .keywords: TS, timestep, view 1404d763cef2SBarry Smith 1405b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1406d763cef2SBarry Smith @*/ 14077087cfbeSBarry Smith PetscErrorCode TSView(TS ts,PetscViewer viewer) 1408d763cef2SBarry Smith { 1409dfbe8321SBarry Smith PetscErrorCode ierr; 141019fd82e9SBarry Smith TSType type; 14112b0a91c0SBarry Smith PetscBool iascii,isstring,isundials,isbinary,isdraw; 14122d53ad75SBarry Smith DMTS sdm; 1413e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1414536b137fSBarry Smith PetscBool issaws; 1415f05ece33SBarry Smith #endif 1416d763cef2SBarry Smith 1417d763cef2SBarry Smith PetscFunctionBegin; 14180700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 14193050cee2SBarry Smith if (!viewer) { 1420ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr); 14213050cee2SBarry Smith } 14220700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 1423c9780b6fSBarry Smith PetscCheckSameComm(ts,1,viewer,2); 1424fd16b177SBarry Smith 1425251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1426251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 142755849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 14282b0a91c0SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1429e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1430536b137fSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 1431f05ece33SBarry Smith #endif 143232077d6dSBarry Smith if (iascii) { 1433dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr); 143477431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr); 14357c8652ddSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr); 1436d763cef2SBarry Smith if (ts->problem_type == TS_NONLINEAR) { 14375ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr); 1438c610991cSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer," total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr); 1439d763cef2SBarry Smith } 14405ef26d82SJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr); 1441193ac0bcSJed Brown ierr = PetscViewerASCIIPrintf(viewer," total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr); 14422d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 14432d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 1444d52bd9f3SBarry Smith if (ts->ops->view) { 1445d52bd9f3SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1446d52bd9f3SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 1447d52bd9f3SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1448d52bd9f3SBarry Smith } 14490f5bd95cSBarry Smith } else if (isstring) { 1450a313700dSBarry Smith ierr = TSGetType(ts,&type);CHKERRQ(ierr); 1451b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr); 145255849f57SBarry Smith } else if (isbinary) { 145355849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 145455849f57SBarry Smith MPI_Comm comm; 145555849f57SBarry Smith PetscMPIInt rank; 145655849f57SBarry Smith char type[256]; 145755849f57SBarry Smith 145855849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 145955849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 146055849f57SBarry Smith if (!rank) { 146155849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 146255849f57SBarry Smith ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr); 146355849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 146455849f57SBarry Smith } 146555849f57SBarry Smith if (ts->ops->view) { 146655849f57SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 146755849f57SBarry Smith } 1468f2c2a1b9SBarry Smith ierr = DMView(ts->dm,viewer);CHKERRQ(ierr); 1469f2c2a1b9SBarry Smith ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr); 14702d53ad75SBarry Smith ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr); 14712d53ad75SBarry Smith ierr = DMTSView(sdm,viewer);CHKERRQ(ierr); 14722b0a91c0SBarry Smith } else if (isdraw) { 14732b0a91c0SBarry Smith PetscDraw draw; 14742b0a91c0SBarry Smith char str[36]; 147589fd9fafSBarry Smith PetscReal x,y,bottom,h; 14762b0a91c0SBarry Smith 14772b0a91c0SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 14782b0a91c0SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 14792b0a91c0SBarry Smith ierr = PetscStrcpy(str,"TS: ");CHKERRQ(ierr); 14802b0a91c0SBarry Smith ierr = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr); 148151fa3d41SBarry Smith ierr = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 148289fd9fafSBarry Smith bottom = y - h; 14832b0a91c0SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 14842b0a91c0SBarry Smith if (ts->ops->view) { 14852b0a91c0SBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 14862b0a91c0SBarry Smith } 14872b0a91c0SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 1488e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1489536b137fSBarry Smith } else if (issaws) { 1490d45a07a7SBarry Smith PetscMPIInt rank; 14912657e9d9SBarry Smith const char *name; 14922657e9d9SBarry Smith 14932657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr); 1494d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 1495d45a07a7SBarry Smith if (!((PetscObject)ts)->amsmem && !rank) { 1496d45a07a7SBarry Smith char dir[1024]; 1497d45a07a7SBarry Smith 1498e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr); 1499a0931e03SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr); 15002657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT)); 15012657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr); 15022657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE)); 1503d763cef2SBarry Smith } 15040acecf5bSBarry Smith if (ts->ops->view) { 15050acecf5bSBarry Smith ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr); 15060acecf5bSBarry Smith } 1507f05ece33SBarry Smith #endif 1508f05ece33SBarry Smith } 1509f05ece33SBarry Smith 1510b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1511251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr); 1512b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1513d763cef2SBarry Smith PetscFunctionReturn(0); 1514d763cef2SBarry Smith } 1515d763cef2SBarry Smith 1516d763cef2SBarry Smith 15174a2ae208SSatish Balay #undef __FUNCT__ 15184a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext" 1519b07ff414SBarry Smith /*@ 1520d763cef2SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for 1521d763cef2SBarry Smith the timesteppers. 1522d763cef2SBarry Smith 15233f9fe445SBarry Smith Logically Collective on TS 1524d763cef2SBarry Smith 1525d763cef2SBarry Smith Input Parameters: 1526d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1527d763cef2SBarry Smith - usrP - optional user context 1528d763cef2SBarry Smith 1529d763cef2SBarry Smith Level: intermediate 1530d763cef2SBarry Smith 1531d763cef2SBarry Smith .keywords: TS, timestep, set, application, context 1532d763cef2SBarry Smith 1533d763cef2SBarry Smith .seealso: TSGetApplicationContext() 1534d763cef2SBarry Smith @*/ 15357087cfbeSBarry Smith PetscErrorCode TSSetApplicationContext(TS ts,void *usrP) 1536d763cef2SBarry Smith { 1537d763cef2SBarry Smith PetscFunctionBegin; 15380700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1539d763cef2SBarry Smith ts->user = usrP; 1540d763cef2SBarry Smith PetscFunctionReturn(0); 1541d763cef2SBarry Smith } 1542d763cef2SBarry Smith 15434a2ae208SSatish Balay #undef __FUNCT__ 15444a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext" 1545b07ff414SBarry Smith /*@ 1546d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 1547d763cef2SBarry Smith timestepper. 1548d763cef2SBarry Smith 1549d763cef2SBarry Smith Not Collective 1550d763cef2SBarry Smith 1551d763cef2SBarry Smith Input Parameter: 1552d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1553d763cef2SBarry Smith 1554d763cef2SBarry Smith Output Parameter: 1555d763cef2SBarry Smith . usrP - user context 1556d763cef2SBarry Smith 1557d763cef2SBarry Smith Level: intermediate 1558d763cef2SBarry Smith 1559d763cef2SBarry Smith .keywords: TS, timestep, get, application, context 1560d763cef2SBarry Smith 1561d763cef2SBarry Smith .seealso: TSSetApplicationContext() 1562d763cef2SBarry Smith @*/ 1563e71120c6SJed Brown PetscErrorCode TSGetApplicationContext(TS ts,void *usrP) 1564d763cef2SBarry Smith { 1565d763cef2SBarry Smith PetscFunctionBegin; 15660700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1567e71120c6SJed Brown *(void**)usrP = ts->user; 1568d763cef2SBarry Smith PetscFunctionReturn(0); 1569d763cef2SBarry Smith } 1570d763cef2SBarry Smith 15714a2ae208SSatish Balay #undef __FUNCT__ 15724a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber" 1573d763cef2SBarry Smith /*@ 1574b8123daeSJed Brown TSGetTimeStepNumber - Gets the number of time steps completed. 1575d763cef2SBarry Smith 1576d763cef2SBarry Smith Not Collective 1577d763cef2SBarry Smith 1578d763cef2SBarry Smith Input Parameter: 1579d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1580d763cef2SBarry Smith 1581d763cef2SBarry Smith Output Parameter: 1582b8123daeSJed Brown . iter - number of steps completed so far 1583d763cef2SBarry Smith 1584d763cef2SBarry Smith Level: intermediate 1585d763cef2SBarry Smith 1586d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number 15879be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep() 1588d763cef2SBarry Smith @*/ 15897087cfbeSBarry Smith PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *iter) 1590d763cef2SBarry Smith { 1591d763cef2SBarry Smith PetscFunctionBegin; 15920700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 15934482741eSBarry Smith PetscValidIntPointer(iter,2); 1594d763cef2SBarry Smith *iter = ts->steps; 1595d763cef2SBarry Smith PetscFunctionReturn(0); 1596d763cef2SBarry Smith } 1597d763cef2SBarry Smith 15984a2ae208SSatish Balay #undef __FUNCT__ 15994a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep" 1600d763cef2SBarry Smith /*@ 1601d763cef2SBarry Smith TSSetInitialTimeStep - Sets the initial timestep to be used, 1602d763cef2SBarry Smith as well as the initial time. 1603d763cef2SBarry Smith 16043f9fe445SBarry Smith Logically Collective on TS 1605d763cef2SBarry Smith 1606d763cef2SBarry Smith Input Parameters: 1607d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1608d763cef2SBarry Smith . initial_time - the initial time 1609d763cef2SBarry Smith - time_step - the size of the timestep 1610d763cef2SBarry Smith 1611d763cef2SBarry Smith Level: intermediate 1612d763cef2SBarry Smith 1613d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep() 1614d763cef2SBarry Smith 1615d763cef2SBarry Smith .keywords: TS, set, initial, timestep 1616d763cef2SBarry Smith @*/ 16177087cfbeSBarry Smith PetscErrorCode TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step) 1618d763cef2SBarry Smith { 1619e144a568SJed Brown PetscErrorCode ierr; 1620e144a568SJed Brown 1621d763cef2SBarry Smith PetscFunctionBegin; 16220700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1623e144a568SJed Brown ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr); 1624d8cd7023SBarry Smith ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr); 1625d763cef2SBarry Smith PetscFunctionReturn(0); 1626d763cef2SBarry Smith } 1627d763cef2SBarry Smith 16284a2ae208SSatish Balay #undef __FUNCT__ 16294a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep" 1630d763cef2SBarry Smith /*@ 1631d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 1632d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 1633d763cef2SBarry Smith 16343f9fe445SBarry Smith Logically Collective on TS 1635d763cef2SBarry Smith 1636d763cef2SBarry Smith Input Parameters: 1637d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 1638d763cef2SBarry Smith - time_step - the size of the timestep 1639d763cef2SBarry Smith 1640d763cef2SBarry Smith Level: intermediate 1641d763cef2SBarry Smith 1642d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1643d763cef2SBarry Smith 1644d763cef2SBarry Smith .keywords: TS, set, timestep 1645d763cef2SBarry Smith @*/ 16467087cfbeSBarry Smith PetscErrorCode TSSetTimeStep(TS ts,PetscReal time_step) 1647d763cef2SBarry Smith { 1648d763cef2SBarry Smith PetscFunctionBegin; 16490700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1650c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,time_step,2); 1651d763cef2SBarry Smith ts->time_step = time_step; 165231748224SBarry Smith ts->time_step_orig = time_step; 1653d763cef2SBarry Smith PetscFunctionReturn(0); 1654d763cef2SBarry Smith } 1655d763cef2SBarry Smith 16564a2ae208SSatish Balay #undef __FUNCT__ 1657a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime" 1658a43b19c4SJed Brown /*@ 165949354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 166049354f04SShri Abhyankar match the exact final time, interpolate solution to the exact final time, 166149354f04SShri Abhyankar or just return at the final time TS computed. 1662a43b19c4SJed Brown 1663a43b19c4SJed Brown Logically Collective on TS 1664a43b19c4SJed Brown 1665a43b19c4SJed Brown Input Parameter: 1666a43b19c4SJed Brown + ts - the time-step context 166749354f04SShri Abhyankar - eftopt - exact final time option 1668a43b19c4SJed Brown 1669a43b19c4SJed Brown Level: beginner 1670a43b19c4SJed Brown 1671a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption 1672a43b19c4SJed Brown @*/ 167349354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt) 1674a43b19c4SJed Brown { 1675a43b19c4SJed Brown PetscFunctionBegin; 1676a43b19c4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 167749354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts,eftopt,2); 167849354f04SShri Abhyankar ts->exact_final_time = eftopt; 1679a43b19c4SJed Brown PetscFunctionReturn(0); 1680a43b19c4SJed Brown } 1681a43b19c4SJed Brown 1682a43b19c4SJed Brown #undef __FUNCT__ 16834a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep" 1684d763cef2SBarry Smith /*@ 1685d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 1686d763cef2SBarry Smith 1687d763cef2SBarry Smith Not Collective 1688d763cef2SBarry Smith 1689d763cef2SBarry Smith Input Parameter: 1690d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1691d763cef2SBarry Smith 1692d763cef2SBarry Smith Output Parameter: 1693d763cef2SBarry Smith . dt - the current timestep size 1694d763cef2SBarry Smith 1695d763cef2SBarry Smith Level: intermediate 1696d763cef2SBarry Smith 1697d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 1698d763cef2SBarry Smith 1699d763cef2SBarry Smith .keywords: TS, get, timestep 1700d763cef2SBarry Smith @*/ 17017087cfbeSBarry Smith PetscErrorCode TSGetTimeStep(TS ts,PetscReal *dt) 1702d763cef2SBarry Smith { 1703d763cef2SBarry Smith PetscFunctionBegin; 17040700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1705f7cf8827SBarry Smith PetscValidRealPointer(dt,2); 1706d763cef2SBarry Smith *dt = ts->time_step; 1707d763cef2SBarry Smith PetscFunctionReturn(0); 1708d763cef2SBarry Smith } 1709d763cef2SBarry Smith 17104a2ae208SSatish Balay #undef __FUNCT__ 17114a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution" 1712d8e5e3e6SSatish Balay /*@ 1713d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 1714d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 1715d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 1716d763cef2SBarry Smith the solution at the next timestep has been calculated. 1717d763cef2SBarry Smith 1718d763cef2SBarry Smith Not Collective, but Vec returned is parallel if TS is parallel 1719d763cef2SBarry Smith 1720d763cef2SBarry Smith Input Parameter: 1721d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1722d763cef2SBarry Smith 1723d763cef2SBarry Smith Output Parameter: 1724d763cef2SBarry Smith . v - the vector containing the solution 1725d763cef2SBarry Smith 1726d763cef2SBarry Smith Level: intermediate 1727d763cef2SBarry Smith 1728d763cef2SBarry Smith .seealso: TSGetTimeStep() 1729d763cef2SBarry Smith 1730d763cef2SBarry Smith .keywords: TS, timestep, get, solution 1731d763cef2SBarry Smith @*/ 17327087cfbeSBarry Smith PetscErrorCode TSGetSolution(TS ts,Vec *v) 1733d763cef2SBarry Smith { 1734d763cef2SBarry Smith PetscFunctionBegin; 17350700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 17364482741eSBarry Smith PetscValidPointer(v,2); 17378737fe31SLisandro Dalcin *v = ts->vec_sol; 1738d763cef2SBarry Smith PetscFunctionReturn(0); 1739d763cef2SBarry Smith } 1740d763cef2SBarry Smith 1741c235aa8dSHong Zhang #undef __FUNCT__ 1742dfb21088SHong Zhang #define __FUNCT__ "TSGetCostGradients" 1743c235aa8dSHong Zhang /*@ 1744dfb21088SHong Zhang TSGetCostGradients - Returns the gradients from the TSAdjointSolve() 1745c235aa8dSHong Zhang 1746c235aa8dSHong Zhang Not Collective, but Vec returned is parallel if TS is parallel 1747c235aa8dSHong Zhang 1748c235aa8dSHong Zhang Input Parameter: 1749c235aa8dSHong Zhang . ts - the TS context obtained from TSCreate() 1750c235aa8dSHong Zhang 1751c235aa8dSHong Zhang Output Parameter: 1752abc2977eSBarry Smith + lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 1753abc2977eSBarry Smith - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 1754c235aa8dSHong Zhang 1755c235aa8dSHong Zhang Level: intermediate 1756c235aa8dSHong Zhang 1757c235aa8dSHong Zhang .seealso: TSGetTimeStep() 1758c235aa8dSHong Zhang 1759c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity 1760c235aa8dSHong Zhang @*/ 1761dfb21088SHong Zhang PetscErrorCode TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu) 1762c235aa8dSHong Zhang { 1763c235aa8dSHong Zhang PetscFunctionBegin; 1764c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1765abc2977eSBarry Smith if (numcost) *numcost = ts->numcost; 1766abc2977eSBarry Smith if (lambda) *lambda = ts->vecs_sensi; 1767abc2977eSBarry Smith if (mu) *mu = ts->vecs_sensip; 1768c235aa8dSHong Zhang PetscFunctionReturn(0); 1769c235aa8dSHong Zhang } 1770c235aa8dSHong Zhang 1771bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 17724a2ae208SSatish Balay #undef __FUNCT__ 1773bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType" 1774d8e5e3e6SSatish Balay /*@ 1775bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 1776d763cef2SBarry Smith 1777bdad233fSMatthew Knepley Not collective 1778d763cef2SBarry Smith 1779bdad233fSMatthew Knepley Input Parameters: 1780bdad233fSMatthew Knepley + ts - The TS 1781bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1782d763cef2SBarry Smith .vb 17830910c330SBarry Smith U_t - A U = 0 (linear) 17840910c330SBarry Smith U_t - A(t) U = 0 (linear) 17850910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 1786d763cef2SBarry Smith .ve 1787d763cef2SBarry Smith 1788d763cef2SBarry Smith Level: beginner 1789d763cef2SBarry Smith 1790bdad233fSMatthew Knepley .keywords: TS, problem type 1791bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1792d763cef2SBarry Smith @*/ 17937087cfbeSBarry Smith PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 1794a7cc72afSBarry Smith { 17959e2a6581SJed Brown PetscErrorCode ierr; 17969e2a6581SJed Brown 1797d763cef2SBarry Smith PetscFunctionBegin; 17980700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 1799bdad233fSMatthew Knepley ts->problem_type = type; 18009e2a6581SJed Brown if (type == TS_LINEAR) { 18019e2a6581SJed Brown SNES snes; 18029e2a6581SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 18039e2a6581SJed Brown ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr); 18049e2a6581SJed Brown } 1805d763cef2SBarry Smith PetscFunctionReturn(0); 1806d763cef2SBarry Smith } 1807d763cef2SBarry Smith 1808bdad233fSMatthew Knepley #undef __FUNCT__ 1809bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType" 1810bdad233fSMatthew Knepley /*@C 1811bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 1812bdad233fSMatthew Knepley 1813bdad233fSMatthew Knepley Not collective 1814bdad233fSMatthew Knepley 1815bdad233fSMatthew Knepley Input Parameter: 1816bdad233fSMatthew Knepley . ts - The TS 1817bdad233fSMatthew Knepley 1818bdad233fSMatthew Knepley Output Parameter: 1819bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms 1820bdad233fSMatthew Knepley .vb 1821089b2837SJed Brown M U_t = A U 1822089b2837SJed Brown M(t) U_t = A(t) U 1823b5abc632SBarry Smith F(t,U,U_t) 1824bdad233fSMatthew Knepley .ve 1825bdad233fSMatthew Knepley 1826bdad233fSMatthew Knepley Level: beginner 1827bdad233fSMatthew Knepley 1828bdad233fSMatthew Knepley .keywords: TS, problem type 1829bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS 1830bdad233fSMatthew Knepley @*/ 18317087cfbeSBarry Smith PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 1832a7cc72afSBarry Smith { 1833bdad233fSMatthew Knepley PetscFunctionBegin; 18340700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 18354482741eSBarry Smith PetscValidIntPointer(type,2); 1836bdad233fSMatthew Knepley *type = ts->problem_type; 1837bdad233fSMatthew Knepley PetscFunctionReturn(0); 1838bdad233fSMatthew Knepley } 1839d763cef2SBarry Smith 18404a2ae208SSatish Balay #undef __FUNCT__ 18414a2ae208SSatish Balay #define __FUNCT__ "TSSetUp" 1842d763cef2SBarry Smith /*@ 1843d763cef2SBarry Smith TSSetUp - Sets up the internal data structures for the later use 1844d763cef2SBarry Smith of a timestepper. 1845d763cef2SBarry Smith 1846d763cef2SBarry Smith Collective on TS 1847d763cef2SBarry Smith 1848d763cef2SBarry Smith Input Parameter: 1849d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 1850d763cef2SBarry Smith 1851d763cef2SBarry Smith Notes: 1852d763cef2SBarry Smith For basic use of the TS solvers the user need not explicitly call 1853d763cef2SBarry Smith TSSetUp(), since these actions will automatically occur during 1854d763cef2SBarry Smith the call to TSStep(). However, if one wishes to control this 1855d763cef2SBarry Smith phase separately, TSSetUp() should be called after TSCreate() 1856d763cef2SBarry Smith and optional routines of the form TSSetXXX(), but before TSStep(). 1857d763cef2SBarry Smith 1858d763cef2SBarry Smith Level: advanced 1859d763cef2SBarry Smith 1860d763cef2SBarry Smith .keywords: TS, timestep, setup 1861d763cef2SBarry Smith 1862d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy() 1863d763cef2SBarry Smith @*/ 18647087cfbeSBarry Smith PetscErrorCode TSSetUp(TS ts) 1865d763cef2SBarry Smith { 1866dfbe8321SBarry Smith PetscErrorCode ierr; 18676c6b9e74SPeter Brune DM dm; 18686c6b9e74SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 1869d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 18706c6b9e74SPeter Brune TSIJacobian ijac; 18716c6b9e74SPeter Brune TSRHSJacobian rhsjac; 1872d763cef2SBarry Smith 1873d763cef2SBarry Smith PetscFunctionBegin; 18740700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1875277b19d0SLisandro Dalcin if (ts->setupcalled) PetscFunctionReturn(0); 1876277b19d0SLisandro Dalcin 18772c18e0fdSBarry Smith ts->total_steps = 0; 18787adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 18799596e0b4SJed Brown ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr); 1880d763cef2SBarry Smith } 1881277b19d0SLisandro Dalcin 1882277b19d0SLisandro Dalcin if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first"); 1883277b19d0SLisandro Dalcin 18841c3436cfSJed Brown 1885552698daSJed Brown ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr); 1886277b19d0SLisandro Dalcin 1887e1244c69SJed Brown if (ts->rhsjacobian.reuse) { 1888e1244c69SJed Brown Mat Amat,Pmat; 1889e1244c69SJed Brown SNES snes; 1890e1244c69SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 1891e1244c69SJed Brown ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr); 1892e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 1893e1244c69SJed Brown * have displaced the RHS matrix */ 1894e1244c69SJed Brown if (Amat == ts->Arhs) { 1895e1244c69SJed Brown ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr); 1896e1244c69SJed Brown ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr); 1897e1244c69SJed Brown ierr = MatDestroy(&Amat);CHKERRQ(ierr); 1898e1244c69SJed Brown } 1899e1244c69SJed Brown if (Pmat == ts->Brhs) { 1900e1244c69SJed Brown ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr); 1901e1244c69SJed Brown ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr); 1902e1244c69SJed Brown ierr = MatDestroy(&Pmat);CHKERRQ(ierr); 1903e1244c69SJed Brown } 1904e1244c69SJed Brown } 1905277b19d0SLisandro Dalcin if (ts->ops->setup) { 1906000e7ae3SMatthew Knepley ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr); 1907277b19d0SLisandro Dalcin } 1908277b19d0SLisandro Dalcin 19096c6b9e74SPeter Brune /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 19106c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 19116c6b9e74SPeter Brune */ 19126c6b9e74SPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 19130298fd71SBarry Smith ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr); 19146c6b9e74SPeter Brune if (!func) { 19156c6b9e74SPeter Brune ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr); 19166c6b9e74SPeter Brune } 19176c6b9e74SPeter Brune /* if the SNES doesn't have a jacobian set and the TS has an ijacobian or rhsjacobian set, set the SNES to use it. 19186c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 19196c6b9e74SPeter Brune */ 19200298fd71SBarry Smith ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr); 19210298fd71SBarry Smith ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr); 19220298fd71SBarry Smith ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr); 19236c6b9e74SPeter Brune if (!jac && (ijac || rhsjac)) { 19246c6b9e74SPeter Brune ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr); 19256c6b9e74SPeter Brune } 1926277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 1927277b19d0SLisandro Dalcin PetscFunctionReturn(0); 1928277b19d0SLisandro Dalcin } 1929277b19d0SLisandro Dalcin 1930277b19d0SLisandro Dalcin #undef __FUNCT__ 1931f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp" 1932f6a906c0SBarry Smith /*@ 1933f6a906c0SBarry Smith TSAdjointSetUp - Sets up the internal data structures for the later use 1934f6a906c0SBarry Smith of an adjoint solver 1935f6a906c0SBarry Smith 1936f6a906c0SBarry Smith Collective on TS 1937f6a906c0SBarry Smith 1938f6a906c0SBarry Smith Input Parameter: 1939f6a906c0SBarry Smith . ts - the TS context obtained from TSCreate() 1940f6a906c0SBarry Smith 1941f6a906c0SBarry Smith Level: advanced 1942f6a906c0SBarry Smith 1943f6a906c0SBarry Smith .keywords: TS, timestep, setup 1944f6a906c0SBarry Smith 1945947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients() 1946f6a906c0SBarry Smith @*/ 1947f6a906c0SBarry Smith PetscErrorCode TSAdjointSetUp(TS ts) 1948f6a906c0SBarry Smith { 1949f6a906c0SBarry Smith PetscErrorCode ierr; 1950f6a906c0SBarry Smith 1951f6a906c0SBarry Smith PetscFunctionBegin; 1952f6a906c0SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1953f6a906c0SBarry Smith if (ts->adjointsetupcalled) PetscFunctionReturn(0); 1954dfb21088SHong Zhang if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first"); 1955d8151eeaSBarry Smith 1956947abb85SHong Zhang if (ts->vec_costintegral) { /* if there is integral in the cost function*/ 1957d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 1958d8151eeaSBarry Smith if (ts->vecs_sensip){ 1959d8151eeaSBarry Smith ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 1960d8151eeaSBarry Smith } 1961947abb85SHong Zhang } 1962d8151eeaSBarry Smith 196342f2b339SBarry Smith if (ts->ops->adjointsetup) { 196442f2b339SBarry Smith ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr); 1965f6a906c0SBarry Smith } 1966f6a906c0SBarry Smith ts->adjointsetupcalled = PETSC_TRUE; 1967f6a906c0SBarry Smith PetscFunctionReturn(0); 1968f6a906c0SBarry Smith } 1969f6a906c0SBarry Smith 1970f6a906c0SBarry Smith #undef __FUNCT__ 1971277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset" 1972277b19d0SLisandro Dalcin /*@ 1973277b19d0SLisandro Dalcin TSReset - Resets a TS context and removes any allocated Vecs and Mats. 1974277b19d0SLisandro Dalcin 1975277b19d0SLisandro Dalcin Collective on TS 1976277b19d0SLisandro Dalcin 1977277b19d0SLisandro Dalcin Input Parameter: 1978277b19d0SLisandro Dalcin . ts - the TS context obtained from TSCreate() 1979277b19d0SLisandro Dalcin 1980277b19d0SLisandro Dalcin Level: beginner 1981277b19d0SLisandro Dalcin 1982277b19d0SLisandro Dalcin .keywords: TS, timestep, reset 1983277b19d0SLisandro Dalcin 1984277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy() 1985277b19d0SLisandro Dalcin @*/ 1986277b19d0SLisandro Dalcin PetscErrorCode TSReset(TS ts) 1987277b19d0SLisandro Dalcin { 1988277b19d0SLisandro Dalcin PetscErrorCode ierr; 1989277b19d0SLisandro Dalcin 1990277b19d0SLisandro Dalcin PetscFunctionBegin; 1991277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 1992b18ea86cSHong Zhang 1993277b19d0SLisandro Dalcin if (ts->ops->reset) { 1994277b19d0SLisandro Dalcin ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr); 1995277b19d0SLisandro Dalcin } 1996277b19d0SLisandro Dalcin if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);} 1997e27a82bcSLisandro Dalcin if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);} 1998bbd56ea5SKarl Rupp 19994e684422SJed Brown ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr); 20004e684422SJed Brown ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr); 2001214bc6a2SJed Brown ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr); 20026bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2003e3d84a46SJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 2004e3d84a46SJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 200538637c2eSJed Brown ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr); 2006bbd56ea5SKarl Rupp 2007947abb85SHong Zhang if (ts->vec_costintegral) { 2008d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr); 2009d8151eeaSBarry Smith if (ts->vecs_drdp){ 2010d8151eeaSBarry Smith ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr); 2011d8151eeaSBarry Smith } 2012947abb85SHong Zhang } 2013d8151eeaSBarry Smith ts->vecs_sensi = NULL; 2014d8151eeaSBarry Smith ts->vecs_sensip = NULL; 2015ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 20162c39e106SBarry Smith ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr); 201736eaed60SHong Zhang ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr); 2018277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 2019d763cef2SBarry Smith PetscFunctionReturn(0); 2020d763cef2SBarry Smith } 2021d763cef2SBarry Smith 20224a2ae208SSatish Balay #undef __FUNCT__ 20234a2ae208SSatish Balay #define __FUNCT__ "TSDestroy" 2024d8e5e3e6SSatish Balay /*@ 2025d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2026d763cef2SBarry Smith with TSCreate(). 2027d763cef2SBarry Smith 2028d763cef2SBarry Smith Collective on TS 2029d763cef2SBarry Smith 2030d763cef2SBarry Smith Input Parameter: 2031d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2032d763cef2SBarry Smith 2033d763cef2SBarry Smith Level: beginner 2034d763cef2SBarry Smith 2035d763cef2SBarry Smith .keywords: TS, timestepper, destroy 2036d763cef2SBarry Smith 2037d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve() 2038d763cef2SBarry Smith @*/ 20396bf464f9SBarry Smith PetscErrorCode TSDestroy(TS *ts) 2040d763cef2SBarry Smith { 20416849ba73SBarry Smith PetscErrorCode ierr; 2042d763cef2SBarry Smith 2043d763cef2SBarry Smith PetscFunctionBegin; 20446bf464f9SBarry Smith if (!*ts) PetscFunctionReturn(0); 20456bf464f9SBarry Smith PetscValidHeaderSpecific((*ts),TS_CLASSID,1); 20466bf464f9SBarry Smith if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);} 2047d763cef2SBarry Smith 20486bf464f9SBarry Smith ierr = TSReset((*ts));CHKERRQ(ierr); 2049277b19d0SLisandro Dalcin 2050e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 2051e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr); 20526bf464f9SBarry Smith if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);} 20536d4c513bSLisandro Dalcin 2054bc952696SBarry Smith ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr); 2055bc952696SBarry Smith 205684df9cb4SJed Brown ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr); 2057aeb4809dSShri Abhyankar if ((*ts)->event) { 2058aeb4809dSShri Abhyankar ierr = TSEventMonitorDestroy(&(*ts)->event);CHKERRQ(ierr); 2059aeb4809dSShri Abhyankar } 20606bf464f9SBarry Smith ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr); 20616bf464f9SBarry Smith ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr); 20626bf464f9SBarry Smith ierr = TSMonitorCancel((*ts));CHKERRQ(ierr); 2063*0dd9f2efSHong Zhang ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr); 20646d4c513bSLisandro Dalcin 2065a79aaaedSSatish Balay ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr); 2066d763cef2SBarry Smith PetscFunctionReturn(0); 2067d763cef2SBarry Smith } 2068d763cef2SBarry Smith 20694a2ae208SSatish Balay #undef __FUNCT__ 20704a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES" 2071d8e5e3e6SSatish Balay /*@ 2072d763cef2SBarry Smith TSGetSNES - Returns the SNES (nonlinear solver) associated with 2073d763cef2SBarry Smith a TS (timestepper) context. Valid only for nonlinear problems. 2074d763cef2SBarry Smith 2075d763cef2SBarry Smith Not Collective, but SNES is parallel if TS is parallel 2076d763cef2SBarry Smith 2077d763cef2SBarry Smith Input Parameter: 2078d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2079d763cef2SBarry Smith 2080d763cef2SBarry Smith Output Parameter: 2081d763cef2SBarry Smith . snes - the nonlinear solver context 2082d763cef2SBarry Smith 2083d763cef2SBarry Smith Notes: 2084d763cef2SBarry Smith The user can then directly manipulate the SNES context to set various 2085d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 208694b7f48cSBarry Smith KSP, KSP, and PC contexts as well. 2087d763cef2SBarry Smith 2088d763cef2SBarry Smith TSGetSNES() does not work for integrators that do not use SNES; in 20890298fd71SBarry Smith this case TSGetSNES() returns NULL in snes. 2090d763cef2SBarry Smith 2091d763cef2SBarry Smith Level: beginner 2092d763cef2SBarry Smith 2093d763cef2SBarry Smith .keywords: timestep, get, SNES 2094d763cef2SBarry Smith @*/ 20957087cfbeSBarry Smith PetscErrorCode TSGetSNES(TS ts,SNES *snes) 2096d763cef2SBarry Smith { 2097d372ba47SLisandro Dalcin PetscErrorCode ierr; 2098d372ba47SLisandro Dalcin 2099d763cef2SBarry Smith PetscFunctionBegin; 21000700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 21014482741eSBarry Smith PetscValidPointer(snes,2); 2102d372ba47SLisandro Dalcin if (!ts->snes) { 2103ce94432eSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr); 21040298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 21053bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr); 2106d372ba47SLisandro Dalcin ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 2107496e6a7aSJed Brown if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 21089e2a6581SJed Brown if (ts->problem_type == TS_LINEAR) { 21099e2a6581SJed Brown ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); 21109e2a6581SJed Brown } 2111d372ba47SLisandro Dalcin } 2112d763cef2SBarry Smith *snes = ts->snes; 2113d763cef2SBarry Smith PetscFunctionReturn(0); 2114d763cef2SBarry Smith } 2115d763cef2SBarry Smith 21164a2ae208SSatish Balay #undef __FUNCT__ 2117deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES" 2118deb2cd25SJed Brown /*@ 2119deb2cd25SJed Brown TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context 2120deb2cd25SJed Brown 2121deb2cd25SJed Brown Collective 2122deb2cd25SJed Brown 2123deb2cd25SJed Brown Input Parameter: 2124deb2cd25SJed Brown + ts - the TS context obtained from TSCreate() 2125deb2cd25SJed Brown - snes - the nonlinear solver context 2126deb2cd25SJed Brown 2127deb2cd25SJed Brown Notes: 2128deb2cd25SJed Brown Most users should have the TS created by calling TSGetSNES() 2129deb2cd25SJed Brown 2130deb2cd25SJed Brown Level: developer 2131deb2cd25SJed Brown 2132deb2cd25SJed Brown .keywords: timestep, set, SNES 2133deb2cd25SJed Brown @*/ 2134deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes) 2135deb2cd25SJed Brown { 2136deb2cd25SJed Brown PetscErrorCode ierr; 2137d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*); 2138deb2cd25SJed Brown 2139deb2cd25SJed Brown PetscFunctionBegin; 2140deb2cd25SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2141deb2cd25SJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,2); 2142deb2cd25SJed Brown ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr); 2143deb2cd25SJed Brown ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr); 2144bbd56ea5SKarl Rupp 2145deb2cd25SJed Brown ts->snes = snes; 2146bbd56ea5SKarl Rupp 21470298fd71SBarry Smith ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr); 21480298fd71SBarry Smith ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr); 2149740132f1SEmil Constantinescu if (func == SNESTSFormJacobian) { 21500298fd71SBarry Smith ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr); 2151740132f1SEmil Constantinescu } 2152deb2cd25SJed Brown PetscFunctionReturn(0); 2153deb2cd25SJed Brown } 2154deb2cd25SJed Brown 2155deb2cd25SJed Brown #undef __FUNCT__ 215694b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP" 2157d8e5e3e6SSatish Balay /*@ 215894b7f48cSBarry Smith TSGetKSP - Returns the KSP (linear solver) associated with 2159d763cef2SBarry Smith a TS (timestepper) context. 2160d763cef2SBarry Smith 216194b7f48cSBarry Smith Not Collective, but KSP is parallel if TS is parallel 2162d763cef2SBarry Smith 2163d763cef2SBarry Smith Input Parameter: 2164d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2165d763cef2SBarry Smith 2166d763cef2SBarry Smith Output Parameter: 216794b7f48cSBarry Smith . ksp - the nonlinear solver context 2168d763cef2SBarry Smith 2169d763cef2SBarry Smith Notes: 217094b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 2171d763cef2SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2172d763cef2SBarry Smith KSP and PC contexts as well. 2173d763cef2SBarry Smith 217494b7f48cSBarry Smith TSGetKSP() does not work for integrators that do not use KSP; 21750298fd71SBarry Smith in this case TSGetKSP() returns NULL in ksp. 2176d763cef2SBarry Smith 2177d763cef2SBarry Smith Level: beginner 2178d763cef2SBarry Smith 217994b7f48cSBarry Smith .keywords: timestep, get, KSP 2180d763cef2SBarry Smith @*/ 21817087cfbeSBarry Smith PetscErrorCode TSGetKSP(TS ts,KSP *ksp) 2182d763cef2SBarry Smith { 2183d372ba47SLisandro Dalcin PetscErrorCode ierr; 2184089b2837SJed Brown SNES snes; 2185d372ba47SLisandro Dalcin 2186d763cef2SBarry Smith PetscFunctionBegin; 21870700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 21884482741eSBarry Smith PetscValidPointer(ksp,2); 218917186662SBarry Smith if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first"); 2190e32f2f54SBarry Smith if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()"); 2191089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 2192089b2837SJed Brown ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr); 2193d763cef2SBarry Smith PetscFunctionReturn(0); 2194d763cef2SBarry Smith } 2195d763cef2SBarry Smith 2196d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2197d763cef2SBarry Smith 21984a2ae208SSatish Balay #undef __FUNCT__ 2199adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration" 2200adb62b0dSMatthew Knepley /*@ 2201adb62b0dSMatthew Knepley TSGetDuration - Gets the maximum number of timesteps to use and 2202adb62b0dSMatthew Knepley maximum time for iteration. 2203adb62b0dSMatthew Knepley 22043f9fe445SBarry Smith Not Collective 2205adb62b0dSMatthew Knepley 2206adb62b0dSMatthew Knepley Input Parameters: 2207adb62b0dSMatthew Knepley + ts - the TS context obtained from TSCreate() 22080298fd71SBarry Smith . maxsteps - maximum number of iterations to use, or NULL 22090298fd71SBarry Smith - maxtime - final time to iterate to, or NULL 2210adb62b0dSMatthew Knepley 2211adb62b0dSMatthew Knepley Level: intermediate 2212adb62b0dSMatthew Knepley 2213adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time 2214adb62b0dSMatthew Knepley @*/ 22157087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 2216adb62b0dSMatthew Knepley { 2217adb62b0dSMatthew Knepley PetscFunctionBegin; 22180700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2219abc0a331SBarry Smith if (maxsteps) { 22204482741eSBarry Smith PetscValidIntPointer(maxsteps,2); 2221adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 2222adb62b0dSMatthew Knepley } 2223abc0a331SBarry Smith if (maxtime) { 22244482741eSBarry Smith PetscValidScalarPointer(maxtime,3); 2225adb62b0dSMatthew Knepley *maxtime = ts->max_time; 2226adb62b0dSMatthew Knepley } 2227adb62b0dSMatthew Knepley PetscFunctionReturn(0); 2228adb62b0dSMatthew Knepley } 2229adb62b0dSMatthew Knepley 2230adb62b0dSMatthew Knepley #undef __FUNCT__ 22314a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration" 2232d763cef2SBarry Smith /*@ 2233d763cef2SBarry Smith TSSetDuration - Sets the maximum number of timesteps to use and 2234d763cef2SBarry Smith maximum time for iteration. 2235d763cef2SBarry Smith 22363f9fe445SBarry Smith Logically Collective on TS 2237d763cef2SBarry Smith 2238d763cef2SBarry Smith Input Parameters: 2239d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2240d763cef2SBarry Smith . maxsteps - maximum number of iterations to use 2241d763cef2SBarry Smith - maxtime - final time to iterate to 2242d763cef2SBarry Smith 2243d763cef2SBarry Smith Options Database Keys: 2244d763cef2SBarry Smith . -ts_max_steps <maxsteps> - Sets maxsteps 22453bca7d26SBarry Smith . -ts_final_time <maxtime> - Sets maxtime 2246d763cef2SBarry Smith 2247d763cef2SBarry Smith Notes: 2248d763cef2SBarry Smith The default maximum number of iterations is 5000. Default time is 5.0 2249d763cef2SBarry Smith 2250d763cef2SBarry Smith Level: intermediate 2251d763cef2SBarry Smith 2252d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations 2253a43b19c4SJed Brown 2254a43b19c4SJed Brown .seealso: TSSetExactFinalTime() 2255d763cef2SBarry Smith @*/ 22567087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime) 2257d763cef2SBarry Smith { 2258d763cef2SBarry Smith PetscFunctionBegin; 22590700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2260c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(ts,maxsteps,2); 2261c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,maxtime,2); 226239b7ec4bSSean Farley if (maxsteps >= 0) ts->max_steps = maxsteps; 226339b7ec4bSSean Farley if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime; 2264d763cef2SBarry Smith PetscFunctionReturn(0); 2265d763cef2SBarry Smith } 2266d763cef2SBarry Smith 22674a2ae208SSatish Balay #undef __FUNCT__ 22684a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution" 2269d763cef2SBarry Smith /*@ 2270d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 2271d763cef2SBarry Smith for use by the TS routines. 2272d763cef2SBarry Smith 22733f9fe445SBarry Smith Logically Collective on TS and Vec 2274d763cef2SBarry Smith 2275d763cef2SBarry Smith Input Parameters: 2276d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 22770910c330SBarry Smith - u - the solution vector 2278d763cef2SBarry Smith 2279d763cef2SBarry Smith Level: beginner 2280d763cef2SBarry Smith 2281d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions 2282d763cef2SBarry Smith @*/ 22830910c330SBarry Smith PetscErrorCode TSSetSolution(TS ts,Vec u) 2284d763cef2SBarry Smith { 22858737fe31SLisandro Dalcin PetscErrorCode ierr; 2286496e6a7aSJed Brown DM dm; 22878737fe31SLisandro Dalcin 2288d763cef2SBarry Smith PetscFunctionBegin; 22890700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 22900910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,2); 22910910c330SBarry Smith ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr); 22926bf464f9SBarry Smith ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr); 2293bbd56ea5SKarl Rupp 22940910c330SBarry Smith ts->vec_sol = u; 2295bbd56ea5SKarl Rupp 2296496e6a7aSJed Brown ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 22970910c330SBarry Smith ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr); 2298d763cef2SBarry Smith PetscFunctionReturn(0); 2299d763cef2SBarry Smith } 2300d763cef2SBarry Smith 2301e74ef692SMatthew Knepley #undef __FUNCT__ 230273b18844SBarry Smith #define __FUNCT__ "TSAdjointSetSteps" 230373b18844SBarry Smith /*@ 230473b18844SBarry Smith TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time 230573b18844SBarry Smith 230673b18844SBarry Smith Logically Collective on TS 230773b18844SBarry Smith 230873b18844SBarry Smith Input Parameters: 230973b18844SBarry Smith + ts - the TS context obtained from TSCreate() 231073b18844SBarry Smith . steps - number of steps to use 231173b18844SBarry Smith 231273b18844SBarry Smith Level: intermediate 231373b18844SBarry Smith 231473b18844SBarry Smith Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this 231573b18844SBarry Smith so as to integrate back to less than the original timestep 231673b18844SBarry Smith 231773b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations 231873b18844SBarry Smith 231973b18844SBarry Smith .seealso: TSSetExactFinalTime() 232073b18844SBarry Smith @*/ 232173b18844SBarry Smith PetscErrorCode TSAdjointSetSteps(TS ts,PetscInt steps) 232273b18844SBarry Smith { 232373b18844SBarry Smith PetscFunctionBegin; 232473b18844SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 232573b18844SBarry Smith PetscValidLogicalCollectiveInt(ts,steps,2); 232673b18844SBarry Smith if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps"); 232773b18844SBarry Smith if (steps > ts->total_steps) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back more than the total number of forward steps"); 232873b18844SBarry Smith ts->adjoint_max_steps = steps; 232973b18844SBarry Smith PetscFunctionReturn(0); 233073b18844SBarry Smith } 233173b18844SBarry Smith 233273b18844SBarry Smith #undef __FUNCT__ 2333dfb21088SHong Zhang #define __FUNCT__ "TSSetCostGradients" 2334c235aa8dSHong Zhang /*@ 2335dfb21088SHong Zhang TSSetCostGradients - Sets the initial value of the gradients of the cost function w.r.t. initial conditions and w.r.t. the problem parameters 23360cf82b59SBarry Smith for use by the TSAdjoint routines. 2337c235aa8dSHong Zhang 2338c235aa8dSHong Zhang Logically Collective on TS and Vec 2339c235aa8dSHong Zhang 2340c235aa8dSHong Zhang Input Parameters: 2341c235aa8dSHong Zhang + ts - the TS context obtained from TSCreate() 2342abc2977eSBarry Smith . lambda - gradients with respect to the initial condition variables, the dimension and parallel layout of these vectors is the same as the ODE solution vector 2343abc2977eSBarry Smith - mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters 2344c235aa8dSHong Zhang 2345c235aa8dSHong Zhang Level: beginner 2346c235aa8dSHong Zhang 2347abc2977eSBarry Smith Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime mu_i = df/dp|finaltime 23480cf82b59SBarry Smith 2349c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions 2350c235aa8dSHong Zhang @*/ 2351dfb21088SHong Zhang PetscErrorCode TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu) 2352c235aa8dSHong Zhang { 2353c235aa8dSHong Zhang PetscFunctionBegin; 2354c235aa8dSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2355abc2977eSBarry Smith PetscValidPointer(lambda,2); 2356abc2977eSBarry Smith ts->vecs_sensi = lambda; 2357abc2977eSBarry Smith ts->vecs_sensip = mu; 2358dfb21088SHong Zhang if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostIntegrand"); 2359abc2977eSBarry Smith ts->numcost = numcost; 2360ad8e2604SHong Zhang PetscFunctionReturn(0); 2361ad8e2604SHong Zhang } 2362ad8e2604SHong Zhang 2363ad8e2604SHong Zhang #undef __FUNCT__ 23645bf8c567SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobian" 2365ad8e2604SHong Zhang /*@C 23660cf82b59SBarry Smith TSAdjointSetRHSJacobian - Sets the function that computes the Jacobian of G w.r.t. the parameters p where y_t = G(y,p,t), as well as the location to store the matrix. 2367ad8e2604SHong Zhang 2368ad8e2604SHong Zhang Logically Collective on TS 2369ad8e2604SHong Zhang 2370ad8e2604SHong Zhang Input Parameters: 2371ad8e2604SHong Zhang + ts - The TS context obtained from TSCreate() 2372ad8e2604SHong Zhang - func - The function 2373ad8e2604SHong Zhang 2374ad8e2604SHong Zhang Calling sequence of func: 23750cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx); 237605755b9cSHong Zhang + t - current timestep 23770cf82b59SBarry Smith . y - input vector (current ODE solution) 237805755b9cSHong Zhang . A - output matrix 237905755b9cSHong Zhang - ctx - [optional] user-defined function context 2380ad8e2604SHong Zhang 2381ad8e2604SHong Zhang Level: intermediate 2382ad8e2604SHong Zhang 23830cf82b59SBarry Smith Notes: Amat has the same number of rows and the same row parallel layout as u, Amat has the same number of columns and parallel layout as p 23840cf82b59SBarry Smith 2385ad8e2604SHong Zhang .keywords: TS, sensitivity 2386ad8e2604SHong Zhang .seealso: 2387ad8e2604SHong Zhang @*/ 23885bf8c567SBarry Smith PetscErrorCode TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx) 2389ad8e2604SHong Zhang { 2390ad8e2604SHong Zhang PetscErrorCode ierr; 2391ad8e2604SHong Zhang 2392ad8e2604SHong Zhang PetscFunctionBegin; 2393ad8e2604SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2394ad8e2604SHong Zhang if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 2395ad8e2604SHong Zhang 2396ad8e2604SHong Zhang ts->rhsjacobianp = func; 2397ad8e2604SHong Zhang ts->rhsjacobianpctx = ctx; 2398ad8e2604SHong Zhang if(Amat) { 2399ad8e2604SHong Zhang ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 2400ad8e2604SHong Zhang ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr); 2401ad8e2604SHong Zhang ts->Jacp = Amat; 2402ad8e2604SHong Zhang } 2403ad8e2604SHong Zhang PetscFunctionReturn(0); 2404ad8e2604SHong Zhang } 2405ad8e2604SHong Zhang 2406ad8e2604SHong Zhang #undef __FUNCT__ 24075bf8c567SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobian" 24080cf82b59SBarry Smith /*@C 24095bf8c567SBarry Smith TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function. 2410ad8e2604SHong Zhang 2411ad8e2604SHong Zhang Collective on TS 2412ad8e2604SHong Zhang 2413ad8e2604SHong Zhang Input Parameters: 2414ad8e2604SHong Zhang . ts - The TS context obtained from TSCreate() 2415ad8e2604SHong Zhang 2416ad8e2604SHong Zhang Level: developer 2417ad8e2604SHong Zhang 2418ad8e2604SHong Zhang .keywords: TS, sensitivity 24195bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian() 2420ad8e2604SHong Zhang @*/ 24215bf8c567SBarry Smith PetscErrorCode TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat) 2422ad8e2604SHong Zhang { 2423ad8e2604SHong Zhang PetscErrorCode ierr; 2424ad8e2604SHong Zhang 2425ad8e2604SHong Zhang PetscFunctionBegin; 2426ad8e2604SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2427ad8e2604SHong Zhang PetscValidHeaderSpecific(X,VEC_CLASSID,3); 2428ad8e2604SHong Zhang PetscValidPointer(Amat,4); 2429ad8e2604SHong Zhang 2430ad8e2604SHong Zhang PetscStackPush("TS user JacobianP function for sensitivity analysis"); 2431ad8e2604SHong Zhang ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr); 2432ad8e2604SHong Zhang PetscStackPop; 2433c235aa8dSHong Zhang PetscFunctionReturn(0); 2434c235aa8dSHong Zhang } 2435c235aa8dSHong Zhang 2436c235aa8dSHong Zhang #undef __FUNCT__ 2437dfb21088SHong Zhang #define __FUNCT__ "TSSetCostIntegrand" 24386fd0887fSHong Zhang /*@C 2439dfb21088SHong Zhang TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions 24406fd0887fSHong Zhang 24416fd0887fSHong Zhang Logically Collective on TS 24426fd0887fSHong Zhang 24436fd0887fSHong Zhang Input Parameters: 24446fd0887fSHong Zhang + ts - the TS context obtained from TSCreate() 2445abc2977eSBarry Smith . numcost - number of gradients to be computed, this is the number of cost functions 24460cf82b59SBarry Smith . rf - routine for evaluating the integrand function 2447b612ec54SBarry Smith . drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y 2448b612ec54SBarry Smith . drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p 2449b612ec54SBarry Smith - ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL) 24506fd0887fSHong Zhang 24510cf82b59SBarry Smith Calling sequence of rf: 24520cf82b59SBarry Smith $ rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx); 24536fd0887fSHong Zhang 24546fd0887fSHong Zhang + t - current timestep 24550cf82b59SBarry Smith . y - input vector 24560cf82b59SBarry Smith . f - function result; one vector entry for each cost function 24576fd0887fSHong Zhang - ctx - [optional] user-defined function context 24586fd0887fSHong Zhang 2459b612ec54SBarry Smith Calling sequence of drdyf: 24600cf82b59SBarry Smith $ PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx); 2461b612ec54SBarry Smith 2462b612ec54SBarry Smith Calling sequence of drdpf: 24630cf82b59SBarry Smith $ PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx); 2464b612ec54SBarry Smith 2465b612ec54SBarry Smith Level: intermediate 24666fd0887fSHong Zhang 2467abc2977eSBarry Smith Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions 24680cf82b59SBarry Smith 24696fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function 24706fd0887fSHong Zhang 2471dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients() 24726fd0887fSHong Zhang @*/ 2473dfb21088SHong Zhang PetscErrorCode TSSetCostIntegrand(TS ts,PetscInt numcost, PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*), 2474d8151eeaSBarry Smith PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*), 2475d8151eeaSBarry Smith PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),void *ctx) 24766fd0887fSHong Zhang { 24776fd0887fSHong Zhang PetscErrorCode ierr; 24786fd0887fSHong Zhang 24796fd0887fSHong Zhang PetscFunctionBegin; 24806fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2481dfb21088SHong Zhang if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostGradients()"); 2482c72ed514SHong Zhang if (!ts->numcost) ts->numcost=numcost; 24836fd0887fSHong Zhang 2484abc2977eSBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr); 24852c39e106SBarry Smith ierr = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr); 24860cf82b59SBarry Smith ts->costintegrand = rf; 248705755b9cSHong Zhang ts->costintegrandctx = ctx; 2488b612ec54SBarry Smith ts->drdyfunction = drdyf; 2489b612ec54SBarry Smith ts->drdpfunction = drdpf; 24906fd0887fSHong Zhang PetscFunctionReturn(0); 24916fd0887fSHong Zhang } 24926fd0887fSHong Zhang 24936fd0887fSHong Zhang #undef __FUNCT__ 2494dfb21088SHong Zhang #define __FUNCT__ "TSGetCostIntegral" 249536eaed60SHong Zhang /*@ 2496dfb21088SHong Zhang TSGetCostIntegral - Returns the values of the integral term in the cost functions. 249736eaed60SHong Zhang It is valid to call the routine after a backward run. 249836eaed60SHong Zhang 249936eaed60SHong Zhang Not Collective 250036eaed60SHong Zhang 250136eaed60SHong Zhang Input Parameter: 250236eaed60SHong Zhang . ts - the TS context obtained from TSCreate() 250336eaed60SHong Zhang 250436eaed60SHong Zhang Output Parameter: 25052c39e106SBarry Smith . v - the vector containing the integrals for each cost function 250636eaed60SHong Zhang 250736eaed60SHong Zhang Level: intermediate 250836eaed60SHong Zhang 2509dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 251036eaed60SHong Zhang 251136eaed60SHong Zhang .keywords: TS, sensitivity analysis 251236eaed60SHong Zhang @*/ 2513dfb21088SHong Zhang PetscErrorCode TSGetCostIntegral(TS ts,Vec *v) 251436eaed60SHong Zhang { 251536eaed60SHong Zhang PetscFunctionBegin; 251636eaed60SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 251736eaed60SHong Zhang PetscValidPointer(v,2); 25182c39e106SBarry Smith *v = ts->vec_costintegral; 251936eaed60SHong Zhang PetscFunctionReturn(0); 252036eaed60SHong Zhang } 252136eaed60SHong Zhang 252236eaed60SHong Zhang #undef __FUNCT__ 2523d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand" 25246fd0887fSHong Zhang /*@ 25252c39e106SBarry Smith TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions. 25266fd0887fSHong Zhang 25276fd0887fSHong Zhang Input Parameters: 25286fd0887fSHong Zhang + ts - the TS context 25296fd0887fSHong Zhang . t - current time 25300cf82b59SBarry Smith - y - state vector, i.e. current solution 25316fd0887fSHong Zhang 25326fd0887fSHong Zhang Output Parameter: 2533abc2977eSBarry Smith . q - vector of size numcost to hold the outputs 25346fd0887fSHong Zhang 25356fd0887fSHong Zhang Note: 25366fd0887fSHong Zhang Most users should not need to explicitly call this routine, as it 25376fd0887fSHong Zhang is used internally within the sensitivity analysis context. 25386fd0887fSHong Zhang 25396fd0887fSHong Zhang Level: developer 25406fd0887fSHong Zhang 25416fd0887fSHong Zhang .keywords: TS, compute 25426fd0887fSHong Zhang 2543dfb21088SHong Zhang .seealso: TSSetCostIntegrand() 25446fd0887fSHong Zhang @*/ 25450cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q) 25466fd0887fSHong Zhang { 25476fd0887fSHong Zhang PetscErrorCode ierr; 25486fd0887fSHong Zhang 25496fd0887fSHong Zhang PetscFunctionBegin; 25506fd0887fSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 25510cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 25526fd0887fSHong Zhang PetscValidHeaderSpecific(q,VEC_CLASSID,4); 25536fd0887fSHong Zhang 25540cf82b59SBarry Smith ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 2555e4680132SHong Zhang if (ts->costintegrand) { 2556e4680132SHong Zhang PetscStackPush("TS user integrand in the cost function"); 25570cf82b59SBarry Smith ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr); 25586fd0887fSHong Zhang PetscStackPop; 25596fd0887fSHong Zhang } else { 25606fd0887fSHong Zhang ierr = VecZeroEntries(q);CHKERRQ(ierr); 25616fd0887fSHong Zhang } 25626fd0887fSHong Zhang 25630cf82b59SBarry Smith ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr); 25646fd0887fSHong Zhang PetscFunctionReturn(0); 25656fd0887fSHong Zhang } 25666fd0887fSHong Zhang 25676fd0887fSHong Zhang #undef __FUNCT__ 2568d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction" 256905755b9cSHong Zhang /*@ 2570d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function. 257105755b9cSHong Zhang 257205755b9cSHong Zhang Collective on TS 257305755b9cSHong Zhang 257405755b9cSHong Zhang Input Parameters: 257505755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 257605755b9cSHong Zhang 257705755b9cSHong Zhang Notes: 2578d4aa0a58SBarry Smith TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation, 257905755b9cSHong Zhang so most users would not generally call this routine themselves. 258005755b9cSHong Zhang 258105755b9cSHong Zhang Level: developer 258205755b9cSHong Zhang 258305755b9cSHong Zhang .keywords: TS, sensitivity 2584d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction() 258505755b9cSHong Zhang @*/ 25860cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy) 258705755b9cSHong Zhang { 258805755b9cSHong Zhang PetscErrorCode ierr; 258905755b9cSHong Zhang 259005755b9cSHong Zhang PetscFunctionBegin; 259105755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 25920cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 259305755b9cSHong Zhang 259405755b9cSHong Zhang PetscStackPush("TS user DRDY function for sensitivity analysis"); 25950cf82b59SBarry Smith ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr); 259605755b9cSHong Zhang PetscStackPop; 259705755b9cSHong Zhang PetscFunctionReturn(0); 259805755b9cSHong Zhang } 259905755b9cSHong Zhang 260005755b9cSHong Zhang #undef __FUNCT__ 2601d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction" 260205755b9cSHong Zhang /*@ 2603d4aa0a58SBarry Smith TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function. 260405755b9cSHong Zhang 260505755b9cSHong Zhang Collective on TS 260605755b9cSHong Zhang 260705755b9cSHong Zhang Input Parameters: 260805755b9cSHong Zhang . ts - The TS context obtained from TSCreate() 260905755b9cSHong Zhang 261005755b9cSHong Zhang Notes: 261105755b9cSHong Zhang TSDRDPFunction() is typically used for sensitivity implementation, 261205755b9cSHong Zhang so most users would not generally call this routine themselves. 261305755b9cSHong Zhang 261405755b9cSHong Zhang Level: developer 261505755b9cSHong Zhang 261605755b9cSHong Zhang .keywords: TS, sensitivity 2617d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction() 261805755b9cSHong Zhang @*/ 26190cf82b59SBarry Smith PetscErrorCode TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp) 262005755b9cSHong Zhang { 262105755b9cSHong Zhang PetscErrorCode ierr; 262205755b9cSHong Zhang 262305755b9cSHong Zhang PetscFunctionBegin; 262405755b9cSHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 26250cf82b59SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 262605755b9cSHong Zhang 262705755b9cSHong Zhang PetscStackPush("TS user DRDP function for sensitivity analysis"); 26280cf82b59SBarry Smith ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr); 262905755b9cSHong Zhang PetscStackPop; 263005755b9cSHong Zhang PetscFunctionReturn(0); 263105755b9cSHong Zhang } 263205755b9cSHong Zhang 263305755b9cSHong Zhang #undef __FUNCT__ 2634e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep" 2635ac226902SBarry Smith /*@C 2636000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 26373f2090d5SJed Brown called once at the beginning of each time step. 2638000e7ae3SMatthew Knepley 26393f9fe445SBarry Smith Logically Collective on TS 2640000e7ae3SMatthew Knepley 2641000e7ae3SMatthew Knepley Input Parameters: 2642000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2643000e7ae3SMatthew Knepley - func - The function 2644000e7ae3SMatthew Knepley 2645000e7ae3SMatthew Knepley Calling sequence of func: 2646000e7ae3SMatthew Knepley . func (TS ts); 2647000e7ae3SMatthew Knepley 2648000e7ae3SMatthew Knepley Level: intermediate 2649000e7ae3SMatthew Knepley 2650b8123daeSJed Brown Note: 2651b8123daeSJed Brown If a step is rejected, TSStep() will call this routine again before each attempt. 2652b8123daeSJed Brown The last completed time step number can be queried using TSGetTimeStepNumber(), the 2653b8123daeSJed Brown size of the step being attempted can be obtained using TSGetTimeStep(). 2654b8123daeSJed Brown 2655000e7ae3SMatthew Knepley .keywords: TS, timestep 26569be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep() 2657000e7ae3SMatthew Knepley @*/ 26587087cfbeSBarry Smith PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS)) 2659000e7ae3SMatthew Knepley { 2660000e7ae3SMatthew Knepley PetscFunctionBegin; 26610700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2662ae60f76fSBarry Smith ts->prestep = func; 2663000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2664000e7ae3SMatthew Knepley } 2665000e7ae3SMatthew Knepley 2666e74ef692SMatthew Knepley #undef __FUNCT__ 26673f2090d5SJed Brown #define __FUNCT__ "TSPreStep" 266809ee8438SJed Brown /*@ 26693f2090d5SJed Brown TSPreStep - Runs the user-defined pre-step function. 26703f2090d5SJed Brown 26713f2090d5SJed Brown Collective on TS 26723f2090d5SJed Brown 26733f2090d5SJed Brown Input Parameters: 26743f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 26753f2090d5SJed Brown 26763f2090d5SJed Brown Notes: 26773f2090d5SJed Brown TSPreStep() is typically used within time stepping implementations, 26783f2090d5SJed Brown so most users would not generally call this routine themselves. 26793f2090d5SJed Brown 26803f2090d5SJed Brown Level: developer 26813f2090d5SJed Brown 26823f2090d5SJed Brown .keywords: TS, timestep 26839be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep() 26843f2090d5SJed Brown @*/ 26857087cfbeSBarry Smith PetscErrorCode TSPreStep(TS ts) 26863f2090d5SJed Brown { 26873f2090d5SJed Brown PetscErrorCode ierr; 26883f2090d5SJed Brown 26893f2090d5SJed Brown PetscFunctionBegin; 26900700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2691ae60f76fSBarry Smith if (ts->prestep) { 2692ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestep),(ts)); 2693312ce896SJed Brown } 26943f2090d5SJed Brown PetscFunctionReturn(0); 26953f2090d5SJed Brown } 26963f2090d5SJed Brown 26973f2090d5SJed Brown #undef __FUNCT__ 2698b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage" 2699b8123daeSJed Brown /*@C 2700b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 2701b8123daeSJed Brown called once at the beginning of each stage. 2702b8123daeSJed Brown 2703b8123daeSJed Brown Logically Collective on TS 2704b8123daeSJed Brown 2705b8123daeSJed Brown Input Parameters: 2706b8123daeSJed Brown + ts - The TS context obtained from TSCreate() 2707b8123daeSJed Brown - func - The function 2708b8123daeSJed Brown 2709b8123daeSJed Brown Calling sequence of func: 2710b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime); 2711b8123daeSJed Brown 2712b8123daeSJed Brown Level: intermediate 2713b8123daeSJed Brown 2714b8123daeSJed Brown Note: 2715b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 2716b8123daeSJed Brown The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 2717b8123daeSJed Brown attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 2718b8123daeSJed Brown 2719b8123daeSJed Brown .keywords: TS, timestep 27209be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 2721b8123daeSJed Brown @*/ 2722b8123daeSJed Brown PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal)) 2723b8123daeSJed Brown { 2724b8123daeSJed Brown PetscFunctionBegin; 2725b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2726ae60f76fSBarry Smith ts->prestage = func; 2727b8123daeSJed Brown PetscFunctionReturn(0); 2728b8123daeSJed Brown } 2729b8123daeSJed Brown 2730b8123daeSJed Brown #undef __FUNCT__ 27319be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage" 27329be3e283SDebojyoti Ghosh /*@C 27339be3e283SDebojyoti Ghosh TSSetPostStage - Sets the general-purpose function 27349be3e283SDebojyoti Ghosh called once at the end of each stage. 27359be3e283SDebojyoti Ghosh 27369be3e283SDebojyoti Ghosh Logically Collective on TS 27379be3e283SDebojyoti Ghosh 27389be3e283SDebojyoti Ghosh Input Parameters: 27399be3e283SDebojyoti Ghosh + ts - The TS context obtained from TSCreate() 27409be3e283SDebojyoti Ghosh - func - The function 27419be3e283SDebojyoti Ghosh 27429be3e283SDebojyoti Ghosh Calling sequence of func: 27439be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y); 27449be3e283SDebojyoti Ghosh 27459be3e283SDebojyoti Ghosh Level: intermediate 27469be3e283SDebojyoti Ghosh 27479be3e283SDebojyoti Ghosh Note: 27489be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 27499be3e283SDebojyoti Ghosh The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being 27509be3e283SDebojyoti Ghosh attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime(). 27519be3e283SDebojyoti Ghosh 27529be3e283SDebojyoti Ghosh .keywords: TS, timestep 27539be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext() 27549be3e283SDebojyoti Ghosh @*/ 27559be3e283SDebojyoti Ghosh PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*)) 27569be3e283SDebojyoti Ghosh { 27579be3e283SDebojyoti Ghosh PetscFunctionBegin; 27589be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID,1); 27599be3e283SDebojyoti Ghosh ts->poststage = func; 27609be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 27619be3e283SDebojyoti Ghosh } 27629be3e283SDebojyoti Ghosh 27639be3e283SDebojyoti Ghosh #undef __FUNCT__ 2764b8123daeSJed Brown #define __FUNCT__ "TSPreStage" 2765b8123daeSJed Brown /*@ 2766b8123daeSJed Brown TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage() 2767b8123daeSJed Brown 2768b8123daeSJed Brown Collective on TS 2769b8123daeSJed Brown 2770b8123daeSJed Brown Input Parameters: 2771b8123daeSJed Brown . ts - The TS context obtained from TSCreate() 27729be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 2773b8123daeSJed Brown 2774b8123daeSJed Brown Notes: 2775b8123daeSJed Brown TSPreStage() is typically used within time stepping implementations, 2776b8123daeSJed Brown most users would not generally call this routine themselves. 2777b8123daeSJed Brown 2778b8123daeSJed Brown Level: developer 2779b8123daeSJed Brown 2780b8123daeSJed Brown .keywords: TS, timestep 27819be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 2782b8123daeSJed Brown @*/ 2783b8123daeSJed Brown PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 2784b8123daeSJed Brown { 2785b8123daeSJed Brown PetscErrorCode ierr; 2786b8123daeSJed Brown 2787b8123daeSJed Brown PetscFunctionBegin; 2788b8123daeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2789ae60f76fSBarry Smith if (ts->prestage) { 2790ae60f76fSBarry Smith PetscStackCallStandard((*ts->prestage),(ts,stagetime)); 2791b8123daeSJed Brown } 2792b8123daeSJed Brown PetscFunctionReturn(0); 2793b8123daeSJed Brown } 2794b8123daeSJed Brown 2795b8123daeSJed Brown #undef __FUNCT__ 27969be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage" 27979be3e283SDebojyoti Ghosh /*@ 27989be3e283SDebojyoti Ghosh TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage() 27999be3e283SDebojyoti Ghosh 28009be3e283SDebojyoti Ghosh Collective on TS 28019be3e283SDebojyoti Ghosh 28029be3e283SDebojyoti Ghosh Input Parameters: 28039be3e283SDebojyoti Ghosh . ts - The TS context obtained from TSCreate() 28049be3e283SDebojyoti Ghosh stagetime - The absolute time of the current stage 28059be3e283SDebojyoti Ghosh stageindex - Stage number 28069be3e283SDebojyoti Ghosh Y - Array of vectors (of size = total number 28079be3e283SDebojyoti Ghosh of stages) with the stage solutions 28089be3e283SDebojyoti Ghosh 28099be3e283SDebojyoti Ghosh Notes: 28109be3e283SDebojyoti Ghosh TSPostStage() is typically used within time stepping implementations, 28119be3e283SDebojyoti Ghosh most users would not generally call this routine themselves. 28129be3e283SDebojyoti Ghosh 28139be3e283SDebojyoti Ghosh Level: developer 28149be3e283SDebojyoti Ghosh 28159be3e283SDebojyoti Ghosh .keywords: TS, timestep 28169be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep() 28179be3e283SDebojyoti Ghosh @*/ 28189be3e283SDebojyoti Ghosh PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 28199be3e283SDebojyoti Ghosh { 28209be3e283SDebojyoti Ghosh PetscErrorCode ierr; 28219be3e283SDebojyoti Ghosh 28229be3e283SDebojyoti Ghosh PetscFunctionBegin; 28239be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts,TS_CLASSID,1); 28244beae5d8SLisandro Dalcin if (ts->poststage) { 28259be3e283SDebojyoti Ghosh PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y)); 28269be3e283SDebojyoti Ghosh } 28279be3e283SDebojyoti Ghosh PetscFunctionReturn(0); 28289be3e283SDebojyoti Ghosh } 28299be3e283SDebojyoti Ghosh 28309be3e283SDebojyoti Ghosh #undef __FUNCT__ 2831e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep" 2832ac226902SBarry Smith /*@C 2833000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 28343f2090d5SJed Brown called once at the end of each time step. 2835000e7ae3SMatthew Knepley 28363f9fe445SBarry Smith Logically Collective on TS 2837000e7ae3SMatthew Knepley 2838000e7ae3SMatthew Knepley Input Parameters: 2839000e7ae3SMatthew Knepley + ts - The TS context obtained from TSCreate() 2840000e7ae3SMatthew Knepley - func - The function 2841000e7ae3SMatthew Knepley 2842000e7ae3SMatthew Knepley Calling sequence of func: 2843b8123daeSJed Brown $ func (TS ts); 2844000e7ae3SMatthew Knepley 2845000e7ae3SMatthew Knepley Level: intermediate 2846000e7ae3SMatthew Knepley 2847000e7ae3SMatthew Knepley .keywords: TS, timestep 2848b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime() 2849000e7ae3SMatthew Knepley @*/ 28507087cfbeSBarry Smith PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS)) 2851000e7ae3SMatthew Knepley { 2852000e7ae3SMatthew Knepley PetscFunctionBegin; 28530700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 2854ae60f76fSBarry Smith ts->poststep = func; 2855000e7ae3SMatthew Knepley PetscFunctionReturn(0); 2856000e7ae3SMatthew Knepley } 2857000e7ae3SMatthew Knepley 2858e74ef692SMatthew Knepley #undef __FUNCT__ 28593f2090d5SJed Brown #define __FUNCT__ "TSPostStep" 286009ee8438SJed Brown /*@ 28613f2090d5SJed Brown TSPostStep - Runs the user-defined post-step function. 28623f2090d5SJed Brown 28633f2090d5SJed Brown Collective on TS 28643f2090d5SJed Brown 28653f2090d5SJed Brown Input Parameters: 28663f2090d5SJed Brown . ts - The TS context obtained from TSCreate() 28673f2090d5SJed Brown 28683f2090d5SJed Brown Notes: 28693f2090d5SJed Brown TSPostStep() is typically used within time stepping implementations, 28703f2090d5SJed Brown so most users would not generally call this routine themselves. 28713f2090d5SJed Brown 28723f2090d5SJed Brown Level: developer 28733f2090d5SJed Brown 28743f2090d5SJed Brown .keywords: TS, timestep 28753f2090d5SJed Brown @*/ 28767087cfbeSBarry Smith PetscErrorCode TSPostStep(TS ts) 28773f2090d5SJed Brown { 28783f2090d5SJed Brown PetscErrorCode ierr; 28793f2090d5SJed Brown 28803f2090d5SJed Brown PetscFunctionBegin; 28810700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2882ae60f76fSBarry Smith if (ts->poststep) { 2883ae60f76fSBarry Smith PetscStackCallStandard((*ts->poststep),(ts)); 288472ac3e02SJed Brown } 28853f2090d5SJed Brown PetscFunctionReturn(0); 28863f2090d5SJed Brown } 28873f2090d5SJed Brown 2888d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 2889d763cef2SBarry Smith 28904a2ae208SSatish Balay #undef __FUNCT__ 2891a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet" 2892d763cef2SBarry Smith /*@C 2893a6570f20SBarry Smith TSMonitorSet - Sets an ADDITIONAL function that is to be used at every 2894d763cef2SBarry Smith timestep to display the iteration's progress. 2895d763cef2SBarry Smith 28963f9fe445SBarry Smith Logically Collective on TS 2897d763cef2SBarry Smith 2898d763cef2SBarry Smith Input Parameters: 2899d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 2900e213d8f1SJed Brown . monitor - monitoring routine 2901329f5518SBarry Smith . mctx - [optional] user-defined context for private data for the 29020298fd71SBarry Smith monitor routine (use NULL if no context is desired) 2903b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 29040298fd71SBarry Smith (may be NULL) 2905d763cef2SBarry Smith 2906e213d8f1SJed Brown Calling sequence of monitor: 29070910c330SBarry Smith $ int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx) 2908d763cef2SBarry Smith 2909d763cef2SBarry Smith + ts - the TS context 291088c05cc5SBarry Smith . steps - iteration number (after the final time step the monitor routine is called with a step of -1, this is at the final time which may have 291188c05cc5SBarry Smith been interpolated to) 29121f06c33eSBarry Smith . time - current time 29130910c330SBarry Smith . u - current iterate 2914d763cef2SBarry Smith - mctx - [optional] monitoring context 2915d763cef2SBarry Smith 2916d763cef2SBarry Smith Notes: 2917d763cef2SBarry Smith This routine adds an additional monitor to the list of monitors that 2918d763cef2SBarry Smith already has been loaded. 2919d763cef2SBarry Smith 2920025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each TS object 2921025f1a04SBarry Smith 2922d763cef2SBarry Smith Level: intermediate 2923d763cef2SBarry Smith 2924d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2925d763cef2SBarry Smith 2926a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel() 2927d763cef2SBarry Smith @*/ 2928c2efdce3SBarry Smith PetscErrorCode TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**)) 2929d763cef2SBarry Smith { 2930d763cef2SBarry Smith PetscFunctionBegin; 29310700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 293217186662SBarry Smith if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 2933d763cef2SBarry Smith ts->monitor[ts->numbermonitors] = monitor; 29348704b422SBarry Smith ts->monitordestroy[ts->numbermonitors] = mdestroy; 2935d763cef2SBarry Smith ts->monitorcontext[ts->numbermonitors++] = (void*)mctx; 2936d763cef2SBarry Smith PetscFunctionReturn(0); 2937d763cef2SBarry Smith } 2938d763cef2SBarry Smith 29394a2ae208SSatish Balay #undef __FUNCT__ 2940a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel" 2941d763cef2SBarry Smith /*@C 2942a6570f20SBarry Smith TSMonitorCancel - Clears all the monitors that have been set on a time-step object. 2943d763cef2SBarry Smith 29443f9fe445SBarry Smith Logically Collective on TS 2945d763cef2SBarry Smith 2946d763cef2SBarry Smith Input Parameters: 2947d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 2948d763cef2SBarry Smith 2949d763cef2SBarry Smith Notes: 2950d763cef2SBarry Smith There is no way to remove a single, specific monitor. 2951d763cef2SBarry Smith 2952d763cef2SBarry Smith Level: intermediate 2953d763cef2SBarry Smith 2954d763cef2SBarry Smith .keywords: TS, timestep, set, monitor 2955d763cef2SBarry Smith 2956a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet() 2957d763cef2SBarry Smith @*/ 29587087cfbeSBarry Smith PetscErrorCode TSMonitorCancel(TS ts) 2959d763cef2SBarry Smith { 2960d952e501SBarry Smith PetscErrorCode ierr; 2961d952e501SBarry Smith PetscInt i; 2962d952e501SBarry Smith 2963d763cef2SBarry Smith PetscFunctionBegin; 29640700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 2965d952e501SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 29668704b422SBarry Smith if (ts->monitordestroy[i]) { 29678704b422SBarry Smith ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr); 2968d952e501SBarry Smith } 2969d952e501SBarry Smith } 2970d763cef2SBarry Smith ts->numbermonitors = 0; 2971d763cef2SBarry Smith PetscFunctionReturn(0); 2972d763cef2SBarry Smith } 2973d763cef2SBarry Smith 29744a2ae208SSatish Balay #undef __FUNCT__ 2975a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault" 2976d8e5e3e6SSatish Balay /*@ 2977a6570f20SBarry Smith TSMonitorDefault - Sets the Default monitor 29785516499fSSatish Balay 29795516499fSSatish Balay Level: intermediate 298041251cbbSSatish Balay 29815516499fSSatish Balay .keywords: TS, set, monitor 29825516499fSSatish Balay 298341251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet() 298441251cbbSSatish Balay @*/ 2985649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy) 2986d763cef2SBarry Smith { 2987dfbe8321SBarry Smith PetscErrorCode ierr; 29884d4332d5SBarry Smith PetscViewer viewer = (PetscViewer) dummy; 2989d132466eSBarry Smith 2990d763cef2SBarry Smith PetscFunctionBegin; 29914d4332d5SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); 2992649052a6SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 29938392e04aSShri Abhyankar ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)\n" : "\n");CHKERRQ(ierr); 2994649052a6SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr); 2995d763cef2SBarry Smith PetscFunctionReturn(0); 2996d763cef2SBarry Smith } 2997d763cef2SBarry Smith 29984a2ae208SSatish Balay #undef __FUNCT__ 29999110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorSet" 30009110b2e7SHong Zhang /*@C 30019110b2e7SHong Zhang TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every 30029110b2e7SHong Zhang timestep to display the iteration's progress. 30039110b2e7SHong Zhang 30049110b2e7SHong Zhang Logically Collective on TS 30059110b2e7SHong Zhang 30069110b2e7SHong Zhang Input Parameters: 30079110b2e7SHong Zhang + ts - the TS context obtained from TSCreate() 30089110b2e7SHong Zhang . adjointmonitor - monitoring routine 30099110b2e7SHong Zhang . adjointmctx - [optional] user-defined context for private data for the 30109110b2e7SHong Zhang monitor routine (use NULL if no context is desired) 30119110b2e7SHong Zhang - adjointmonitordestroy - [optional] routine that frees monitor context 30129110b2e7SHong Zhang (may be NULL) 30139110b2e7SHong Zhang 30149110b2e7SHong Zhang Calling sequence of monitor: 30159110b2e7SHong Zhang $ int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx) 30169110b2e7SHong Zhang 30179110b2e7SHong Zhang + ts - the TS context 30189110b2e7SHong Zhang . steps - iteration number (after the final time step the monitor routine is called with a step of -1, this is at the final time which may have 30199110b2e7SHong Zhang been interpolated to) 30209110b2e7SHong Zhang . time - current time 30219110b2e7SHong Zhang . u - current iterate 30229110b2e7SHong Zhang . numcost - number of cost functionos 30239110b2e7SHong Zhang . lambda - sensitivities to initial conditions 30249110b2e7SHong Zhang . mu - sensitivities to parameters 30259110b2e7SHong Zhang - adjointmctx - [optional] adjoint monitoring context 30269110b2e7SHong Zhang 30279110b2e7SHong Zhang Notes: 30289110b2e7SHong Zhang This routine adds an additional monitor to the list of monitors that 30299110b2e7SHong Zhang already has been loaded. 30309110b2e7SHong Zhang 30319110b2e7SHong Zhang Fortran notes: Only a single monitor function can be set for each TS object 30329110b2e7SHong Zhang 30339110b2e7SHong Zhang Level: intermediate 30349110b2e7SHong Zhang 30359110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 30369110b2e7SHong Zhang 30379110b2e7SHong Zhang .seealso: TSAdjointMonitorCancel() 30389110b2e7SHong Zhang @*/ 30399110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**)) 30409110b2e7SHong Zhang { 30419110b2e7SHong Zhang PetscFunctionBegin; 30429110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 30439110b2e7SHong Zhang if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set"); 30449110b2e7SHong Zhang ts->adjointmonitor[ts->numberadjointmonitors] = adjointmonitor; 30459110b2e7SHong Zhang ts->adjointmonitordestroy[ts->numberadjointmonitors] = adjointmdestroy; 30469110b2e7SHong Zhang ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx; 30479110b2e7SHong Zhang PetscFunctionReturn(0); 30489110b2e7SHong Zhang } 30499110b2e7SHong Zhang 30509110b2e7SHong Zhang #undef __FUNCT__ 30519110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorCancel" 30529110b2e7SHong Zhang /*@C 30539110b2e7SHong Zhang TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object. 30549110b2e7SHong Zhang 30559110b2e7SHong Zhang Logically Collective on TS 30569110b2e7SHong Zhang 30579110b2e7SHong Zhang Input Parameters: 30589110b2e7SHong Zhang . ts - the TS context obtained from TSCreate() 30599110b2e7SHong Zhang 30609110b2e7SHong Zhang Notes: 30619110b2e7SHong Zhang There is no way to remove a single, specific monitor. 30629110b2e7SHong Zhang 30639110b2e7SHong Zhang Level: intermediate 30649110b2e7SHong Zhang 30659110b2e7SHong Zhang .keywords: TS, timestep, set, adjoint, monitor 30669110b2e7SHong Zhang 30679110b2e7SHong Zhang .seealso: TSAdjointMonitorSet() 30689110b2e7SHong Zhang @*/ 30699110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorCancel(TS ts) 30709110b2e7SHong Zhang { 30719110b2e7SHong Zhang PetscErrorCode ierr; 30729110b2e7SHong Zhang PetscInt i; 30739110b2e7SHong Zhang 30749110b2e7SHong Zhang PetscFunctionBegin; 30759110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 30769110b2e7SHong Zhang for (i=0; i<ts->numberadjointmonitors; i++) { 30779110b2e7SHong Zhang if (ts->adjointmonitordestroy[i]) { 30789110b2e7SHong Zhang ierr = (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 30799110b2e7SHong Zhang } 30809110b2e7SHong Zhang } 30819110b2e7SHong Zhang ts->numberadjointmonitors = 0; 30829110b2e7SHong Zhang PetscFunctionReturn(0); 30839110b2e7SHong Zhang } 30849110b2e7SHong Zhang 30859110b2e7SHong Zhang #undef __FUNCT__ 3086cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages" 3087cd652676SJed Brown /*@ 3088cd652676SJed Brown TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available. 3089cd652676SJed Brown 3090cd652676SJed Brown Logically Collective on TS 3091cd652676SJed Brown 3092cd652676SJed Brown Input Argument: 3093cd652676SJed Brown . ts - time stepping context 3094cd652676SJed Brown 3095cd652676SJed Brown Output Argument: 3096cd652676SJed Brown . flg - PETSC_TRUE or PETSC_FALSE 3097cd652676SJed Brown 3098cd652676SJed Brown Level: intermediate 3099cd652676SJed Brown 3100cd652676SJed Brown .keywords: TS, set 3101cd652676SJed Brown 3102cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep() 3103cd652676SJed Brown @*/ 3104cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg) 3105cd652676SJed Brown { 3106cd652676SJed Brown PetscFunctionBegin; 3107cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3108cd652676SJed Brown ts->retain_stages = flg; 3109cd652676SJed Brown PetscFunctionReturn(0); 3110cd652676SJed Brown } 3111cd652676SJed Brown 3112cd652676SJed Brown #undef __FUNCT__ 3113cd652676SJed Brown #define __FUNCT__ "TSInterpolate" 3114cd652676SJed Brown /*@ 3115cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3116cd652676SJed Brown 3117cd652676SJed Brown Collective on TS 3118cd652676SJed Brown 3119cd652676SJed Brown Input Argument: 3120cd652676SJed Brown + ts - time stepping context 3121cd652676SJed Brown - t - time to interpolate to 3122cd652676SJed Brown 3123cd652676SJed Brown Output Argument: 31240910c330SBarry Smith . U - state at given time 3125cd652676SJed Brown 3126cd652676SJed Brown Notes: 3127cd652676SJed Brown The user should call TSSetRetainStages() before taking a step in which interpolation will be requested. 3128cd652676SJed Brown 3129cd652676SJed Brown Level: intermediate 3130cd652676SJed Brown 3131cd652676SJed Brown Developer Notes: 3132cd652676SJed Brown TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3133cd652676SJed Brown 3134cd652676SJed Brown .keywords: TS, set 3135cd652676SJed Brown 3136cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep() 3137cd652676SJed Brown @*/ 31380910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U) 3139cd652676SJed Brown { 3140cd652676SJed Brown PetscErrorCode ierr; 3141cd652676SJed Brown 3142cd652676SJed Brown PetscFunctionBegin; 3143cd652676SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3144b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,3); 31456712e2f1SBarry Smith if (t < ts->ptime - ts->time_step_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-ts->time_step_prev),(double)ts->ptime); 3146ce94432eSBarry Smith if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name); 31470910c330SBarry Smith ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr); 3148cd652676SJed Brown PetscFunctionReturn(0); 3149cd652676SJed Brown } 3150cd652676SJed Brown 3151cd652676SJed Brown #undef __FUNCT__ 31524a2ae208SSatish Balay #define __FUNCT__ "TSStep" 3153d763cef2SBarry Smith /*@ 31546d9e5789SSean Farley TSStep - Steps one time step 3155d763cef2SBarry Smith 3156d763cef2SBarry Smith Collective on TS 3157d763cef2SBarry Smith 3158d763cef2SBarry Smith Input Parameter: 3159d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3160d763cef2SBarry Smith 316127829d71SBarry Smith Level: developer 3162d763cef2SBarry Smith 3163b8123daeSJed Brown Notes: 316427829d71SBarry Smith The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine. 316527829d71SBarry Smith 3166b8123daeSJed Brown The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may 3167b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3168b8123daeSJed Brown 316925cb2221SBarry Smith This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the 317025cb2221SBarry Smith time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep. 317125cb2221SBarry Smith 3172d763cef2SBarry Smith .keywords: TS, timestep, solve 3173d763cef2SBarry Smith 31749be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate() 3175d763cef2SBarry Smith @*/ 3176193ac0bcSJed Brown PetscErrorCode TSStep(TS ts) 3177d763cef2SBarry Smith { 31782fb8446cSMatthew G. Knepley DM dm; 3179dfbe8321SBarry Smith PetscErrorCode ierr; 3180fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3181d763cef2SBarry Smith 3182d763cef2SBarry Smith PetscFunctionBegin; 31830700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID,1); 3184fffbeea8SBarry Smith ierr = PetscCitationsRegister("@techreport{tspaper,\n" 3185fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3186fffbeea8SBarry Smith " author = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n" 3187fffbeea8SBarry Smith " type = {Preprint},\n" 3188fffbeea8SBarry Smith " number = {ANL/MCS-P5061-0114},\n" 3189fffbeea8SBarry Smith " institution = {Argonne National Laboratory},\n" 3190302440fdSBarry Smith " year = {2014}\n}\n",&cite);CHKERRQ(ierr); 3191fffbeea8SBarry Smith 31922fb8446cSMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 3193d405a339SMatthew Knepley ierr = TSSetUp(ts);CHKERRQ(ierr); 3194d405a339SMatthew Knepley 3195362cd11cSLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 319624655328SShri ts->ptime_prev = ts->ptime; 3197cdb7a50dSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3198bbd56ea5SKarl Rupp 3199e04979a6SLisandro Dalcin if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name); 3200d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3201193ac0bcSJed Brown ierr = (*ts->ops->step)(ts);CHKERRQ(ierr); 3202d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3203bbd56ea5SKarl Rupp 320424655328SShri ts->time_step_prev = ts->ptime - ts->ptime_prev; 3205cdb7a50dSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3206362cd11cSLisandro Dalcin 3207362cd11cSLisandro Dalcin if (ts->reason < 0) { 3208cef5090cSJed Brown if (ts->errorifstepfailed) { 320908c7845fSBarry Smith if (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]); 321008c7845fSBarry Smith else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3211d2daff3dSHong Zhang } 321208c7845fSBarry Smith } else if (!ts->reason) { 321308c7845fSBarry Smith if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 321408c7845fSBarry Smith else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 321508c7845fSBarry Smith } 32162c18e0fdSBarry Smith ts->total_steps++; 32178392e04aSShri Abhyankar ts->steprollback = PETSC_FALSE; 321808c7845fSBarry Smith PetscFunctionReturn(0); 321908c7845fSBarry Smith } 322008c7845fSBarry Smith 322108c7845fSBarry Smith #undef __FUNCT__ 322208c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep" 322308c7845fSBarry Smith /*@ 3224b957a604SHong Zhang TSAdjointStep - Steps one time step backward in the adjoint run 322508c7845fSBarry Smith 322608c7845fSBarry Smith Collective on TS 322708c7845fSBarry Smith 322808c7845fSBarry Smith Input Parameter: 322908c7845fSBarry Smith . ts - the TS context obtained from TSCreate() 323008c7845fSBarry Smith 32316a4d4014SLisandro Dalcin Level: intermediate 32326a4d4014SLisandro Dalcin 3233b957a604SHong Zhang .keywords: TS, adjoint, step 32346a4d4014SLisandro Dalcin 3235b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve() 32366a4d4014SLisandro Dalcin @*/ 323708c7845fSBarry Smith PetscErrorCode TSAdjointStep(TS ts) 32386a4d4014SLisandro Dalcin { 323908c7845fSBarry Smith DM dm; 32406a4d4014SLisandro Dalcin PetscErrorCode ierr; 32416a4d4014SLisandro Dalcin 32426a4d4014SLisandro Dalcin PetscFunctionBegin; 32434a2ae208SSatish Balay PetscValidHeaderSpecific(ts, TS_CLASSID,1); 324408c7845fSBarry Smith ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 324508c7845fSBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 3246d763cef2SBarry Smith 3247d763cef2SBarry Smith ts->reason = TS_CONVERGED_ITERATING; 324808c7845fSBarry Smith ts->ptime_prev = ts->ptime; 324908c7845fSBarry Smith ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3250685405a1SBarry Smith ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts, "-ts_view_solution");CHKERRQ(ierr); 3251d763cef2SBarry Smith 32526849ba73SBarry Smith ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr); 325342f2b339SBarry Smith if (!ts->ops->adjointstep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed because the adjoint of %s has not been implemented, try other time stepping methods for adjoint sensitivity analysis",((PetscObject)ts)->type_name); 325442f2b339SBarry Smith ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr); 3255a6570f20SBarry Smith ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr); 3256d763cef2SBarry Smith 3257cef5090cSJed Brown ts->time_step_prev = ts->ptime - ts->ptime_prev; 3258cef5090cSJed Brown ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3259362cd11cSLisandro Dalcin 3260362cd11cSLisandro Dalcin if (ts->reason < 0) { 3261cef5090cSJed Brown if (ts->errorifstepfailed) { 3262cef5090cSJed Brown if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) { 3263ce94432eSBarry Smith 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]); 32642a3a10ceSLisandro Dalcin } else if (ts->reason == TS_DIVERGED_STEP_REJECTED) { 32652a3a10ceSLisandro Dalcin SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_reject or make negative to attempt recovery",TSConvergedReasons[ts->reason]); 3266ce94432eSBarry Smith } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]); 3267cef5090cSJed Brown } 3268362cd11cSLisandro Dalcin } else if (!ts->reason) { 326973b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3270db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3271362cd11cSLisandro Dalcin } 32722c18e0fdSBarry Smith ts->total_steps--; 3273d763cef2SBarry Smith PetscFunctionReturn(0); 3274d763cef2SBarry Smith } 3275d763cef2SBarry Smith 3276d763cef2SBarry Smith #undef __FUNCT__ 327705175c85SJed Brown #define __FUNCT__ "TSEvaluateStep" 327805175c85SJed Brown /*@ 327905175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 328005175c85SJed Brown 32811c3436cfSJed Brown Collective on TS 328205175c85SJed Brown 328305175c85SJed Brown Input Arguments: 32841c3436cfSJed Brown + ts - time stepping context 32851c3436cfSJed Brown . order - desired order of accuracy 32860298fd71SBarry Smith - done - whether the step was evaluated at this order (pass NULL to generate an error if not available) 328705175c85SJed Brown 328805175c85SJed Brown Output Arguments: 32890910c330SBarry Smith . U - state at the end of the current step 329005175c85SJed Brown 329105175c85SJed Brown Level: advanced 329205175c85SJed Brown 3293108c343cSJed Brown Notes: 3294108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3295108c343cSJed 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. 3296108c343cSJed Brown 32971c3436cfSJed Brown .seealso: TSStep(), TSAdapt 329805175c85SJed Brown @*/ 32990910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done) 330005175c85SJed Brown { 330105175c85SJed Brown PetscErrorCode ierr; 330205175c85SJed Brown 330305175c85SJed Brown PetscFunctionBegin; 330405175c85SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 330505175c85SJed Brown PetscValidType(ts,1); 33060910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,3); 3307ce94432eSBarry Smith if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name); 33080910c330SBarry Smith ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr); 330905175c85SJed Brown PetscFunctionReturn(0); 331005175c85SJed Brown } 331105175c85SJed Brown 3312d67e68b7SBarry Smith 331305175c85SJed Brown #undef __FUNCT__ 3314d763cef2SBarry Smith #define __FUNCT__ "TSSolve" 3315d763cef2SBarry Smith /*@ 3316d763cef2SBarry Smith TSSolve - Steps the requested number of timesteps. 3317d763cef2SBarry Smith 3318d763cef2SBarry Smith Collective on TS 3319d763cef2SBarry Smith 3320d763cef2SBarry Smith Input Parameter: 3321d763cef2SBarry Smith + ts - the TS context obtained from TSCreate() 3322cc708dedSBarry Smith - u - the solution vector (can be null if TSSetSolution() was used, otherwise must contain the initial conditions) 33235a3a76d0SJed Brown 3324d763cef2SBarry Smith Level: beginner 3325d763cef2SBarry Smith 33265a3a76d0SJed Brown Notes: 33275a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 33285a3a76d0SJed Brown held state accessible by TSGetSolution() and TSGetTime() because the method may have 33295a3a76d0SJed Brown stepped over the final time. 33305a3a76d0SJed Brown 3331d763cef2SBarry Smith .keywords: TS, timestep, solve 3332d763cef2SBarry Smith 3333d763cef2SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep() 3334d763cef2SBarry Smith @*/ 3335cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u) 3336d763cef2SBarry Smith { 3337b06615a5SLisandro Dalcin Vec solution; 3338d763cef2SBarry Smith PetscErrorCode ierr; 3339d763cef2SBarry Smith 3340d763cef2SBarry Smith PetscFunctionBegin; 3341d763cef2SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3342f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2); 334349354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE) { /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */ 3344b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,2); 33450910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 3346b06615a5SLisandro Dalcin ierr = VecDuplicate(u,&solution);CHKERRQ(ierr); 3347b06615a5SLisandro Dalcin ierr = TSSetSolution(ts,solution);CHKERRQ(ierr); 3348b06615a5SLisandro Dalcin ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */ 33495a3a76d0SJed Brown } 33500910c330SBarry Smith ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr); 3351bbd56ea5SKarl Rupp } else if (u) { 33520910c330SBarry Smith ierr = TSSetSolution(ts,u);CHKERRQ(ierr); 33535a3a76d0SJed Brown } 3354b5d403baSSean Farley ierr = TSSetUp(ts);CHKERRQ(ierr); 3355d763cef2SBarry Smith /* reset time step and iteration counters */ 3356193ac0bcSJed Brown ts->steps = 0; 33575ef26d82SJed Brown ts->ksp_its = 0; 33585ef26d82SJed Brown ts->snes_its = 0; 3359c610991cSLisandro Dalcin ts->num_snes_failures = 0; 3360c610991cSLisandro Dalcin ts->reject = 0; 3361193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 3362193ac0bcSJed Brown 3363ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr); 3364ec8db0baSMatthew G. Knepley { 3365ec8db0baSMatthew G. Knepley DM dm; 3366ec8db0baSMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 3367ec8db0baSMatthew G. Knepley ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr); 3368ec8db0baSMatthew G. Knepley } 3369f05ece33SBarry Smith 3370193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 3371193ac0bcSJed Brown ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr); 33720910c330SBarry Smith ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr); 3373cc708dedSBarry Smith ts->solvetime = ts->ptime; 3374193ac0bcSJed Brown } else { 3375d763cef2SBarry Smith /* steps the requested number of timesteps. */ 3376db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 3377db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 3378cdf0de3dSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 337911b05f00SHong Zhang if (ts->vec_costintegral) ts->costintegralfwd=PETSC_TRUE; 33806fea3669SShri Abhyankar if(ts->event) { 33816fea3669SShri Abhyankar ierr = TSEventMonitorInitialize(ts);CHKERRQ(ierr); 33826fea3669SShri Abhyankar } 3383e1a7a14fSJed Brown while (!ts->reason) { 3384193ac0bcSJed Brown ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 3385193ac0bcSJed Brown ierr = TSStep(ts);CHKERRQ(ierr); 3386aeb4809dSShri Abhyankar if (ts->event) { 3387aeb4809dSShri Abhyankar ierr = TSEventMonitor(ts);CHKERRQ(ierr); 33881eda64f1SShri Abhyankar } 33898392e04aSShri Abhyankar if(!ts->steprollback) { 3390cdf0de3dSShri Abhyankar ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 33911eda64f1SShri Abhyankar ierr = TSPostStep(ts);CHKERRQ(ierr); 3392aeb4809dSShri Abhyankar } 3393193ac0bcSJed Brown } 339449354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 33950910c330SBarry Smith ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr); 3396cc708dedSBarry Smith ts->solvetime = ts->max_time; 3397b06615a5SLisandro Dalcin solution = u; 33980574a7fbSJed Brown } else { 3399ad6bc421SBarry Smith if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);} 3400cc708dedSBarry Smith ts->solvetime = ts->ptime; 3401b06615a5SLisandro Dalcin solution = ts->vec_sol; 34020574a7fbSJed Brown } 3403b06615a5SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr); 3404685405a1SBarry Smith ierr = VecViewFromOptions(solution,(PetscObject) ts,"-ts_view_solution");CHKERRQ(ierr); 3405193ac0bcSJed Brown } 3406d2daff3dSHong Zhang 3407ce1779c8SBarry Smith ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr); 3408dc1cb503SEmil Constantinescu ierr = VecViewFromOptions(ts->vec_sol,NULL,"-ts_view_solution");CHKERRQ(ierr); 340956f85f32SBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr); 3410ef222394SBarry Smith if (ts->adjoint_solve) { 3411ef222394SBarry Smith ierr = TSAdjointSolve(ts);CHKERRQ(ierr); 34122b0a91c0SBarry Smith } 3413d763cef2SBarry Smith PetscFunctionReturn(0); 3414d763cef2SBarry Smith } 3415d763cef2SBarry Smith 3416d763cef2SBarry Smith #undef __FUNCT__ 3417c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve" 3418c88f2d44SBarry Smith /*@ 3419c88f2d44SBarry Smith TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE 3420c88f2d44SBarry Smith 3421c88f2d44SBarry Smith Collective on TS 3422c88f2d44SBarry Smith 3423c88f2d44SBarry Smith Input Parameter: 34242c39e106SBarry Smith . ts - the TS context obtained from TSCreate() 3425c88f2d44SBarry Smith 3426e87fd094SBarry Smith Options Database: 3427e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions 3428e87fd094SBarry Smith 3429c88f2d44SBarry Smith Level: intermediate 3430c88f2d44SBarry Smith 3431c88f2d44SBarry Smith Notes: 3432c88f2d44SBarry Smith This must be called after a call to TSSolve() that solves the forward problem 3433c88f2d44SBarry Smith 343473b18844SBarry Smith By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time 343573b18844SBarry Smith 3436c88f2d44SBarry Smith .keywords: TS, timestep, solve 3437c88f2d44SBarry Smith 3438b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep() 3439c88f2d44SBarry Smith @*/ 34402c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts) 3441c88f2d44SBarry Smith { 3442c88f2d44SBarry Smith PetscErrorCode ierr; 3443c88f2d44SBarry Smith 3444c88f2d44SBarry Smith PetscFunctionBegin; 3445c88f2d44SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3446c88f2d44SBarry Smith ierr = TSAdjointSetUp(ts);CHKERRQ(ierr); 3447c88f2d44SBarry Smith /* reset time step and iteration counters */ 3448c88f2d44SBarry Smith ts->steps = 0; 3449c88f2d44SBarry Smith ts->ksp_its = 0; 3450c88f2d44SBarry Smith ts->snes_its = 0; 3451c88f2d44SBarry Smith ts->num_snes_failures = 0; 3452c88f2d44SBarry Smith ts->reject = 0; 3453c88f2d44SBarry Smith ts->reason = TS_CONVERGED_ITERATING; 3454c88f2d44SBarry Smith 345573b18844SBarry Smith if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps; 3456c88f2d44SBarry Smith 345773b18844SBarry Smith if (ts->steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS; 3458c88f2d44SBarry Smith while (!ts->reason) { 345955c52731SHong Zhang ierr = TSTrajectoryGet(ts->trajectory,ts,ts->total_steps,&ts->ptime);CHKERRQ(ierr); 34609110b2e7SHong Zhang ierr = TSAdjointMonitor(ts,ts->total_steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);CHKERRQ(ierr); 3461c88f2d44SBarry Smith if (ts->event) { 3462d0578d90SShri Abhyankar ierr = TSAdjointEventMonitor(ts);CHKERRQ(ierr); 3463d0578d90SShri Abhyankar } 3464616946c1SHong Zhang ierr = TSAdjointStep(ts);CHKERRQ(ierr); 3465c88f2d44SBarry Smith } 3466c88f2d44SBarry Smith ts->solvetime = ts->ptime; 3467685405a1SBarry Smith ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr); 3468c88f2d44SBarry Smith PetscFunctionReturn(0); 3469c88f2d44SBarry Smith } 3470c88f2d44SBarry Smith 3471c88f2d44SBarry Smith #undef __FUNCT__ 3472d763cef2SBarry Smith #define __FUNCT__ "TSMonitor" 3473228d79bcSJed Brown /*@ 3474228d79bcSJed Brown TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet() 3475228d79bcSJed Brown 3476228d79bcSJed Brown Collective on TS 3477228d79bcSJed Brown 3478228d79bcSJed Brown Input Parameters: 3479228d79bcSJed Brown + ts - time stepping context obtained from TSCreate() 3480228d79bcSJed Brown . step - step number that has just completed 3481228d79bcSJed Brown . ptime - model time of the state 34820910c330SBarry Smith - u - state at the current model time 3483228d79bcSJed Brown 3484228d79bcSJed Brown Notes: 3485228d79bcSJed Brown TSMonitor() is typically used within the time stepping implementations. 3486228d79bcSJed Brown Users might call this function when using the TSStep() interface instead of TSSolve(). 3487228d79bcSJed Brown 3488228d79bcSJed Brown Level: advanced 3489228d79bcSJed Brown 3490228d79bcSJed Brown .keywords: TS, timestep 3491228d79bcSJed Brown @*/ 34920910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u) 3493d763cef2SBarry Smith { 3494d763cef2SBarry Smith PetscErrorCode ierr; 3495d763cef2SBarry Smith PetscInt i,n = ts->numbermonitors; 3496d763cef2SBarry Smith 3497d763cef2SBarry Smith PetscFunctionBegin; 3498b06615a5SLisandro Dalcin PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3499b06615a5SLisandro Dalcin PetscValidHeaderSpecific(u,VEC_CLASSID,4); 35005edff71fSBarry Smith ierr = VecLockPush(u);CHKERRQ(ierr); 3501d763cef2SBarry Smith for (i=0; i<n; i++) { 35020910c330SBarry Smith ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr); 3503d763cef2SBarry Smith } 35045edff71fSBarry Smith ierr = VecLockPop(u);CHKERRQ(ierr); 3505d763cef2SBarry Smith PetscFunctionReturn(0); 3506d763cef2SBarry Smith } 3507d763cef2SBarry Smith 35089110b2e7SHong Zhang #undef __FUNCT__ 35099110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitor" 35109110b2e7SHong Zhang /*@ 35119110b2e7SHong Zhang TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet() 35129110b2e7SHong Zhang 35139110b2e7SHong Zhang Collective on TS 35149110b2e7SHong Zhang 35159110b2e7SHong Zhang Input Parameters: 35169110b2e7SHong Zhang + ts - time stepping context obtained from TSCreate() 35179110b2e7SHong Zhang . step - step number that has just completed 35189110b2e7SHong Zhang . ptime - model time of the state 35199110b2e7SHong Zhang . u - state at the current model time 35209110b2e7SHong Zhang . numcost - number of cost functions (dimension of lambda or mu) 35219110b2e7SHong Zhang . lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables 35229110b2e7SHong Zhang - mu - vectors containing the gradients of the cost functions with respect to the problem parameters 35239110b2e7SHong Zhang 35249110b2e7SHong Zhang Notes: 35259110b2e7SHong Zhang TSAdjointMonitor() is typically used within the adjoint implementations. 35269110b2e7SHong Zhang Users might call this function when using the TSAdjointStep() interface instead of TSAdjointSolve(). 35279110b2e7SHong Zhang 35289110b2e7SHong Zhang Level: advanced 35299110b2e7SHong Zhang 35309110b2e7SHong Zhang .keywords: TS, timestep 35319110b2e7SHong Zhang @*/ 35329110b2e7SHong Zhang PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu) 35339110b2e7SHong Zhang { 35349110b2e7SHong Zhang PetscErrorCode ierr; 35359110b2e7SHong Zhang PetscInt i,n = ts->numberadjointmonitors; 35369110b2e7SHong Zhang 35379110b2e7SHong Zhang PetscFunctionBegin; 35389110b2e7SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 35399110b2e7SHong Zhang PetscValidHeaderSpecific(u,VEC_CLASSID,4); 35409110b2e7SHong Zhang ierr = VecLockPush(u);CHKERRQ(ierr); 35419110b2e7SHong Zhang for (i=0; i<n; i++) { 35429110b2e7SHong Zhang ierr = (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);CHKERRQ(ierr); 35439110b2e7SHong Zhang } 35449110b2e7SHong Zhang ierr = VecLockPop(u);CHKERRQ(ierr); 35459110b2e7SHong Zhang PetscFunctionReturn(0); 35469110b2e7SHong Zhang } 35479110b2e7SHong Zhang 3548d763cef2SBarry Smith /* ------------------------------------------------------------------------*/ 3549d763cef2SBarry Smith #undef __FUNCT__ 3550a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate" 3551d763cef2SBarry Smith /*@C 3552a9f9c1f6SBarry Smith TSMonitorLGCtxCreate - Creates a line graph context for use with 3553a9f9c1f6SBarry Smith TS to monitor the solution process graphically in various ways 3554d763cef2SBarry Smith 3555d763cef2SBarry Smith Collective on TS 3556d763cef2SBarry Smith 3557d763cef2SBarry Smith Input Parameters: 3558d763cef2SBarry Smith + host - the X display to open, or null for the local machine 3559d763cef2SBarry Smith . label - the title to put in the title bar 35607c922b88SBarry Smith . x, y - the screen coordinates of the upper left coordinate of the window 3561a9f9c1f6SBarry Smith . m, n - the screen width and height in pixels 3562a9f9c1f6SBarry Smith - howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time 3563d763cef2SBarry Smith 3564d763cef2SBarry Smith Output Parameter: 35650b039ecaSBarry Smith . ctx - the context 3566d763cef2SBarry Smith 3567d763cef2SBarry Smith Options Database Key: 35684f09c107SBarry Smith + -ts_monitor_lg_timestep - automatically sets line graph monitor 35694f09c107SBarry Smith . -ts_monitor_lg_solution - 357099fdda47SBarry Smith . -ts_monitor_lg_error - 357199fdda47SBarry Smith . -ts_monitor_lg_ksp_iterations - 357299fdda47SBarry Smith . -ts_monitor_lg_snes_iterations - 3573b6fe0379SLisandro Dalcin - -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true 3574d763cef2SBarry Smith 3575d763cef2SBarry Smith Notes: 3576a9f9c1f6SBarry Smith Use TSMonitorLGCtxDestroy() to destroy. 3577d763cef2SBarry Smith 3578d763cef2SBarry Smith Level: intermediate 3579d763cef2SBarry Smith 35807c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso 3581d763cef2SBarry Smith 35824f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 35837c922b88SBarry Smith 3584d763cef2SBarry Smith @*/ 3585a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx) 3586d763cef2SBarry Smith { 3587b0a32e0cSBarry Smith PetscDraw win; 3588dfbe8321SBarry Smith PetscErrorCode ierr; 3589d763cef2SBarry Smith 3590d763cef2SBarry Smith PetscFunctionBegin; 3591b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 3592a80ad3e0SBarry Smith ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr); 35933fbbecb0SBarry Smith ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr); 35940b039ecaSBarry Smith ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr); 35953bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)(*ctx)->lg,(PetscObject)win);CHKERRQ(ierr); 3596b6fe0379SLisandro Dalcin ierr = PetscDrawLGSetUseMarkers((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr); 3597287de1a7SBarry Smith ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr); 3598a9f9c1f6SBarry Smith (*ctx)->howoften = howoften; 3599d763cef2SBarry Smith PetscFunctionReturn(0); 3600d763cef2SBarry Smith } 3601d763cef2SBarry Smith 36024a2ae208SSatish Balay #undef __FUNCT__ 36034f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep" 3604b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx) 3605d763cef2SBarry Smith { 36060b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 3607c32365f1SBarry Smith PetscReal x = ptime,y; 3608dfbe8321SBarry Smith PetscErrorCode ierr; 3609d763cef2SBarry Smith 3610d763cef2SBarry Smith PetscFunctionBegin; 3611b06615a5SLisandro Dalcin if (!step) { 3612a9f9c1f6SBarry Smith PetscDrawAxis axis; 3613a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 3614a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr); 3615a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 3616a9f9c1f6SBarry Smith } 3617c32365f1SBarry Smith ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr); 36180b039ecaSBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 3619b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 36200b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 36213923b477SBarry Smith } 3622d763cef2SBarry Smith PetscFunctionReturn(0); 3623d763cef2SBarry Smith } 3624d763cef2SBarry Smith 36254a2ae208SSatish Balay #undef __FUNCT__ 3626a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy" 3627d763cef2SBarry Smith /*@C 3628a9f9c1f6SBarry Smith TSMonitorLGCtxDestroy - Destroys a line graph context that was created 3629a9f9c1f6SBarry Smith with TSMonitorLGCtxCreate(). 3630d763cef2SBarry Smith 36310b039ecaSBarry Smith Collective on TSMonitorLGCtx 3632d763cef2SBarry Smith 3633d763cef2SBarry Smith Input Parameter: 36340b039ecaSBarry Smith . ctx - the monitor context 3635d763cef2SBarry Smith 3636d763cef2SBarry Smith Level: intermediate 3637d763cef2SBarry Smith 3638d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy 3639d763cef2SBarry Smith 36404f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 3641d763cef2SBarry Smith @*/ 3642a9f9c1f6SBarry Smith PetscErrorCode TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx) 3643d763cef2SBarry Smith { 3644b0a32e0cSBarry Smith PetscDraw draw; 3645dfbe8321SBarry Smith PetscErrorCode ierr; 3646d763cef2SBarry Smith 3647d763cef2SBarry Smith PetscFunctionBegin; 36487684fa3eSBarry Smith if ((*ctx)->transformdestroy) { 36497684fa3eSBarry Smith ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr); 36507684fa3eSBarry Smith } 36510b039ecaSBarry Smith ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr); 36526bf464f9SBarry Smith ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr); 36530b039ecaSBarry Smith ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr); 365431152f8aSBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr); 3655387f4636SBarry Smith ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr); 3656387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr); 3657387f4636SBarry Smith ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr); 36580b039ecaSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 3659d763cef2SBarry Smith PetscFunctionReturn(0); 3660d763cef2SBarry Smith } 3661d763cef2SBarry Smith 36624a2ae208SSatish Balay #undef __FUNCT__ 36634a2ae208SSatish Balay #define __FUNCT__ "TSGetTime" 3664d763cef2SBarry Smith /*@ 3665b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 3666d763cef2SBarry Smith 3667d763cef2SBarry Smith Not Collective 3668d763cef2SBarry Smith 3669d763cef2SBarry Smith Input Parameter: 3670d763cef2SBarry Smith . ts - the TS context obtained from TSCreate() 3671d763cef2SBarry Smith 3672d763cef2SBarry Smith Output Parameter: 3673d763cef2SBarry Smith . t - the current time 3674d763cef2SBarry Smith 3675d763cef2SBarry Smith Level: beginner 3676d763cef2SBarry Smith 3677b8123daeSJed Brown Note: 3678b8123daeSJed Brown When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(), 36799be3e283SDebojyoti Ghosh TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated. 3680b8123daeSJed Brown 3681d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 3682d763cef2SBarry Smith 3683d763cef2SBarry Smith .keywords: TS, get, time 3684d763cef2SBarry Smith @*/ 36857087cfbeSBarry Smith PetscErrorCode TSGetTime(TS ts,PetscReal *t) 3686d763cef2SBarry Smith { 3687d763cef2SBarry Smith PetscFunctionBegin; 36880700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3689f7cf8827SBarry Smith PetscValidRealPointer(t,2); 3690d763cef2SBarry Smith *t = ts->ptime; 3691d763cef2SBarry Smith PetscFunctionReturn(0); 3692d763cef2SBarry Smith } 3693d763cef2SBarry Smith 36944a2ae208SSatish Balay #undef __FUNCT__ 3695e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime" 3696e5e524a1SHong Zhang /*@ 3697e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 3698e5e524a1SHong Zhang 3699e5e524a1SHong Zhang Not Collective 3700e5e524a1SHong Zhang 3701e5e524a1SHong Zhang Input Parameter: 3702e5e524a1SHong Zhang . ts - the TS context obtained from TSCreate() 3703e5e524a1SHong Zhang 3704e5e524a1SHong Zhang Output Parameter: 3705e5e524a1SHong Zhang . t - the previous time 3706e5e524a1SHong Zhang 3707e5e524a1SHong Zhang Level: beginner 3708e5e524a1SHong Zhang 3709e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep() 3710e5e524a1SHong Zhang 3711e5e524a1SHong Zhang .keywords: TS, get, time 3712e5e524a1SHong Zhang @*/ 3713e5e524a1SHong Zhang PetscErrorCode TSGetPrevTime(TS ts,PetscReal *t) 3714e5e524a1SHong Zhang { 3715e5e524a1SHong Zhang PetscFunctionBegin; 3716e5e524a1SHong Zhang PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3717e5e524a1SHong Zhang PetscValidRealPointer(t,2); 3718e5e524a1SHong Zhang *t = ts->ptime_prev; 3719e5e524a1SHong Zhang PetscFunctionReturn(0); 3720e5e524a1SHong Zhang } 3721e5e524a1SHong Zhang 3722e5e524a1SHong Zhang #undef __FUNCT__ 37236a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime" 37246a4d4014SLisandro Dalcin /*@ 37256a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 37266a4d4014SLisandro Dalcin 37273f9fe445SBarry Smith Logically Collective on TS 37286a4d4014SLisandro Dalcin 37296a4d4014SLisandro Dalcin Input Parameters: 37306a4d4014SLisandro Dalcin + ts - the TS context obtained from TSCreate() 37316a4d4014SLisandro Dalcin - time - the time 37326a4d4014SLisandro Dalcin 37336a4d4014SLisandro Dalcin Level: intermediate 37346a4d4014SLisandro Dalcin 37356a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration() 37366a4d4014SLisandro Dalcin 37376a4d4014SLisandro Dalcin .keywords: TS, set, time 37386a4d4014SLisandro Dalcin @*/ 37397087cfbeSBarry Smith PetscErrorCode TSSetTime(TS ts, PetscReal t) 37406a4d4014SLisandro Dalcin { 37416a4d4014SLisandro Dalcin PetscFunctionBegin; 37420700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3743c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts,t,2); 37446a4d4014SLisandro Dalcin ts->ptime = t; 37456a4d4014SLisandro Dalcin PetscFunctionReturn(0); 37466a4d4014SLisandro Dalcin } 37476a4d4014SLisandro Dalcin 37486a4d4014SLisandro Dalcin #undef __FUNCT__ 37494a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix" 3750d763cef2SBarry Smith /*@C 3751d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 3752d763cef2SBarry Smith TS options in the database. 3753d763cef2SBarry Smith 37543f9fe445SBarry Smith Logically Collective on TS 3755d763cef2SBarry Smith 3756d763cef2SBarry Smith Input Parameter: 3757d763cef2SBarry Smith + ts - The TS context 3758d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3759d763cef2SBarry Smith 3760d763cef2SBarry Smith Notes: 3761d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3762d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3763d763cef2SBarry Smith hyphen. 3764d763cef2SBarry Smith 3765d763cef2SBarry Smith Level: advanced 3766d763cef2SBarry Smith 3767d763cef2SBarry Smith .keywords: TS, set, options, prefix, database 3768d763cef2SBarry Smith 3769d763cef2SBarry Smith .seealso: TSSetFromOptions() 3770d763cef2SBarry Smith 3771d763cef2SBarry Smith @*/ 37727087cfbeSBarry Smith PetscErrorCode TSSetOptionsPrefix(TS ts,const char prefix[]) 3773d763cef2SBarry Smith { 3774dfbe8321SBarry Smith PetscErrorCode ierr; 3775089b2837SJed Brown SNES snes; 3776d763cef2SBarry Smith 3777d763cef2SBarry Smith PetscFunctionBegin; 37780700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3779d763cef2SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3780089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3781089b2837SJed Brown ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr); 3782d763cef2SBarry Smith PetscFunctionReturn(0); 3783d763cef2SBarry Smith } 3784d763cef2SBarry Smith 3785d763cef2SBarry Smith 37864a2ae208SSatish Balay #undef __FUNCT__ 37874a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix" 3788d763cef2SBarry Smith /*@C 3789d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 3790d763cef2SBarry Smith TS options in the database. 3791d763cef2SBarry Smith 37923f9fe445SBarry Smith Logically Collective on TS 3793d763cef2SBarry Smith 3794d763cef2SBarry Smith Input Parameter: 3795d763cef2SBarry Smith + ts - The TS context 3796d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 3797d763cef2SBarry Smith 3798d763cef2SBarry Smith Notes: 3799d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3800d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 3801d763cef2SBarry Smith hyphen. 3802d763cef2SBarry Smith 3803d763cef2SBarry Smith Level: advanced 3804d763cef2SBarry Smith 3805d763cef2SBarry Smith .keywords: TS, append, options, prefix, database 3806d763cef2SBarry Smith 3807d763cef2SBarry Smith .seealso: TSGetOptionsPrefix() 3808d763cef2SBarry Smith 3809d763cef2SBarry Smith @*/ 38107087cfbeSBarry Smith PetscErrorCode TSAppendOptionsPrefix(TS ts,const char prefix[]) 3811d763cef2SBarry Smith { 3812dfbe8321SBarry Smith PetscErrorCode ierr; 3813089b2837SJed Brown SNES snes; 3814d763cef2SBarry Smith 3815d763cef2SBarry Smith PetscFunctionBegin; 38160700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 3817d763cef2SBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3818089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3819089b2837SJed Brown ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr); 3820d763cef2SBarry Smith PetscFunctionReturn(0); 3821d763cef2SBarry Smith } 3822d763cef2SBarry Smith 38234a2ae208SSatish Balay #undef __FUNCT__ 38244a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix" 3825d763cef2SBarry Smith /*@C 3826d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 3827d763cef2SBarry Smith TS options in the database. 3828d763cef2SBarry Smith 3829d763cef2SBarry Smith Not Collective 3830d763cef2SBarry Smith 3831d763cef2SBarry Smith Input Parameter: 3832d763cef2SBarry Smith . ts - The TS context 3833d763cef2SBarry Smith 3834d763cef2SBarry Smith Output Parameter: 3835d763cef2SBarry Smith . prefix - A pointer to the prefix string used 3836d763cef2SBarry Smith 3837d763cef2SBarry Smith Notes: On the fortran side, the user should pass in a string 'prifix' of 3838d763cef2SBarry Smith sufficient length to hold the prefix. 3839d763cef2SBarry Smith 3840d763cef2SBarry Smith Level: intermediate 3841d763cef2SBarry Smith 3842d763cef2SBarry Smith .keywords: TS, get, options, prefix, database 3843d763cef2SBarry Smith 3844d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix() 3845d763cef2SBarry Smith @*/ 38467087cfbeSBarry Smith PetscErrorCode TSGetOptionsPrefix(TS ts,const char *prefix[]) 3847d763cef2SBarry Smith { 3848dfbe8321SBarry Smith PetscErrorCode ierr; 3849d763cef2SBarry Smith 3850d763cef2SBarry Smith PetscFunctionBegin; 38510700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 38524482741eSBarry Smith PetscValidPointer(prefix,2); 3853d763cef2SBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr); 3854d763cef2SBarry Smith PetscFunctionReturn(0); 3855d763cef2SBarry Smith } 3856d763cef2SBarry Smith 38574a2ae208SSatish Balay #undef __FUNCT__ 38584a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian" 3859d763cef2SBarry Smith /*@C 3860d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 3861d763cef2SBarry Smith 3862d763cef2SBarry Smith Not Collective, but parallel objects are returned if TS is parallel 3863d763cef2SBarry Smith 3864d763cef2SBarry Smith Input Parameter: 3865d763cef2SBarry Smith . ts - The TS context obtained from TSCreate() 3866d763cef2SBarry Smith 3867d763cef2SBarry Smith Output Parameters: 3868e4357dc4SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or NULL) 3869e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat (or NULL) 3870e4357dc4SBarry Smith . func - Function to compute the Jacobian of the RHS (or NULL) 3871e4357dc4SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or NULL) 3872d763cef2SBarry Smith 38730298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 3874d763cef2SBarry Smith 3875d763cef2SBarry Smith Level: intermediate 3876d763cef2SBarry Smith 387726d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 3878d763cef2SBarry Smith 3879d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian 3880d763cef2SBarry Smith @*/ 3881e4357dc4SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx) 3882d763cef2SBarry Smith { 3883089b2837SJed Brown PetscErrorCode ierr; 3884089b2837SJed Brown SNES snes; 388524989b8cSPeter Brune DM dm; 3886089b2837SJed Brown 3887d763cef2SBarry Smith PetscFunctionBegin; 3888089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3889e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 389024989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 389124989b8cSPeter Brune ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr); 3892d763cef2SBarry Smith PetscFunctionReturn(0); 3893d763cef2SBarry Smith } 3894d763cef2SBarry Smith 38951713a123SBarry Smith #undef __FUNCT__ 38962eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian" 38972eca1d9cSJed Brown /*@C 38982eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 38992eca1d9cSJed Brown 39002eca1d9cSJed Brown Not Collective, but parallel objects are returned if TS is parallel 39012eca1d9cSJed Brown 39022eca1d9cSJed Brown Input Parameter: 39032eca1d9cSJed Brown . ts - The TS context obtained from TSCreate() 39042eca1d9cSJed Brown 39052eca1d9cSJed Brown Output Parameters: 3906e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 3907e4357dc4SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as Amat 39082eca1d9cSJed Brown . f - The function to compute the matrices 39092eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 39102eca1d9cSJed Brown 39110298fd71SBarry Smith Notes: You can pass in NULL for any return argument you do not need. 39122eca1d9cSJed Brown 39132eca1d9cSJed Brown Level: advanced 39142eca1d9cSJed Brown 39152eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber() 39162eca1d9cSJed Brown 39172eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian 39182eca1d9cSJed Brown @*/ 3919e4357dc4SBarry Smith PetscErrorCode TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx) 39202eca1d9cSJed Brown { 3921089b2837SJed Brown PetscErrorCode ierr; 3922089b2837SJed Brown SNES snes; 392324989b8cSPeter Brune DM dm; 39240910c330SBarry Smith 39252eca1d9cSJed Brown PetscFunctionBegin; 3926089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 3927f7d39f7aSBarry Smith ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr); 3928e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 392924989b8cSPeter Brune ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 393024989b8cSPeter Brune ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr); 39312eca1d9cSJed Brown PetscFunctionReturn(0); 39322eca1d9cSJed Brown } 39332eca1d9cSJed Brown 39346083293cSBarry Smith 39352eca1d9cSJed Brown #undef __FUNCT__ 393683a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution" 39371713a123SBarry Smith /*@C 393883a4ac43SBarry Smith TSMonitorDrawSolution - Monitors progress of the TS solvers by calling 39391713a123SBarry Smith VecView() for the solution at each timestep 39401713a123SBarry Smith 39411713a123SBarry Smith Collective on TS 39421713a123SBarry Smith 39431713a123SBarry Smith Input Parameters: 39441713a123SBarry Smith + ts - the TS context 39451713a123SBarry Smith . step - current time-step 3946142b95e3SSatish Balay . ptime - current time 39470298fd71SBarry Smith - dummy - either a viewer or NULL 39481713a123SBarry Smith 394999fdda47SBarry Smith Options Database: 395099fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 395199fdda47SBarry Smith 3952387f4636SBarry Smith Notes: the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial 395399fdda47SBarry Smith will look bad 395499fdda47SBarry Smith 39551713a123SBarry Smith Level: intermediate 39561713a123SBarry Smith 39571713a123SBarry Smith .keywords: TS, vector, monitor, view 39581713a123SBarry Smith 3959a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 39601713a123SBarry Smith @*/ 39610910c330SBarry Smith PetscErrorCode TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 39621713a123SBarry Smith { 3963dfbe8321SBarry Smith PetscErrorCode ierr; 396483a4ac43SBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 3965473a3ab2SBarry Smith PetscDraw draw; 39661713a123SBarry Smith 39671713a123SBarry Smith PetscFunctionBegin; 39686083293cSBarry Smith if (!step && ictx->showinitial) { 39696083293cSBarry Smith if (!ictx->initialsolution) { 39700910c330SBarry Smith ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr); 39711713a123SBarry Smith } 39720910c330SBarry Smith ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr); 39736083293cSBarry Smith } 3974b06615a5SLisandro Dalcin if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 39750dcf80beSBarry Smith 39766083293cSBarry Smith if (ictx->showinitial) { 39776083293cSBarry Smith PetscReal pause; 39786083293cSBarry Smith ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr); 39796083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr); 39806083293cSBarry Smith ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr); 39816083293cSBarry Smith ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr); 39826083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr); 39836083293cSBarry Smith } 39840910c330SBarry Smith ierr = VecView(u,ictx->viewer);CHKERRQ(ierr); 3985473a3ab2SBarry Smith if (ictx->showtimestepandtime) { 398651fa3d41SBarry Smith PetscReal xl,yl,xr,yr,h; 3987473a3ab2SBarry Smith char time[32]; 3988473a3ab2SBarry Smith 3989473a3ab2SBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 399085b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 3991473a3ab2SBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 3992473a3ab2SBarry Smith h = yl + .95*(yr - yl); 399351fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 3994473a3ab2SBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 3995473a3ab2SBarry Smith } 3996473a3ab2SBarry Smith 39976083293cSBarry Smith if (ictx->showinitial) { 39986083293cSBarry Smith ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr); 39996083293cSBarry Smith } 40001713a123SBarry Smith PetscFunctionReturn(0); 40011713a123SBarry Smith } 40021713a123SBarry Smith 40032d5ee99bSBarry Smith #undef __FUNCT__ 40049110b2e7SHong Zhang #define __FUNCT__ "TSAdjointMonitorDrawSensi" 40059110b2e7SHong Zhang /*@C 40069110b2e7SHong Zhang TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling 40079110b2e7SHong Zhang VecView() for the sensitivities to initial states at each timestep 40089110b2e7SHong Zhang 40099110b2e7SHong Zhang Collective on TS 40109110b2e7SHong Zhang 40119110b2e7SHong Zhang Input Parameters: 40129110b2e7SHong Zhang + ts - the TS context 40139110b2e7SHong Zhang . step - current time-step 40149110b2e7SHong Zhang . ptime - current time 40159110b2e7SHong Zhang . u - current state 40169110b2e7SHong Zhang . numcost - number of cost functions 40179110b2e7SHong Zhang . lambda - sensitivities to initial conditions 40189110b2e7SHong Zhang . mu - sensitivities to parameters 40199110b2e7SHong Zhang - dummy - either a viewer or NULL 40209110b2e7SHong Zhang 40219110b2e7SHong Zhang Level: intermediate 40229110b2e7SHong Zhang 40239110b2e7SHong Zhang .keywords: TS, vector, adjoint, monitor, view 40249110b2e7SHong Zhang 40259110b2e7SHong Zhang .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView() 40269110b2e7SHong Zhang @*/ 40279110b2e7SHong Zhang PetscErrorCode TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy) 40289110b2e7SHong Zhang { 40299110b2e7SHong Zhang PetscErrorCode ierr; 40309110b2e7SHong Zhang TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 40319110b2e7SHong Zhang PetscDraw draw; 40329110b2e7SHong Zhang 40339110b2e7SHong Zhang PetscFunctionBegin; 40349110b2e7SHong Zhang if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 40359110b2e7SHong Zhang 40369110b2e7SHong Zhang ierr = VecView(lambda[0],ictx->viewer);CHKERRQ(ierr); 40379110b2e7SHong Zhang if (ictx->showtimestepandtime) { 40389110b2e7SHong Zhang PetscReal xl,yl,xr,yr,h; 40399110b2e7SHong Zhang char time[32]; 40409110b2e7SHong Zhang 40419110b2e7SHong Zhang ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 40429110b2e7SHong Zhang ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 40439110b2e7SHong Zhang ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 40449110b2e7SHong Zhang h = yl + .95*(yr - yl); 40459110b2e7SHong Zhang ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 40469110b2e7SHong Zhang ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 40479110b2e7SHong Zhang } 40489110b2e7SHong Zhang 40499110b2e7SHong Zhang PetscFunctionReturn(0); 40509110b2e7SHong Zhang } 40519110b2e7SHong Zhang 40529110b2e7SHong Zhang #undef __FUNCT__ 40532d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase" 40542d5ee99bSBarry Smith /*@C 40552d5ee99bSBarry Smith TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram 40562d5ee99bSBarry Smith 40572d5ee99bSBarry Smith Collective on TS 40582d5ee99bSBarry Smith 40592d5ee99bSBarry Smith Input Parameters: 40602d5ee99bSBarry Smith + ts - the TS context 40612d5ee99bSBarry Smith . step - current time-step 40622d5ee99bSBarry Smith . ptime - current time 40632d5ee99bSBarry Smith - dummy - either a viewer or NULL 40642d5ee99bSBarry Smith 40652d5ee99bSBarry Smith Level: intermediate 40662d5ee99bSBarry Smith 40672d5ee99bSBarry Smith .keywords: TS, vector, monitor, view 40682d5ee99bSBarry Smith 40692d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 40702d5ee99bSBarry Smith @*/ 40712d5ee99bSBarry Smith PetscErrorCode TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 40722d5ee99bSBarry Smith { 40732d5ee99bSBarry Smith PetscErrorCode ierr; 40742d5ee99bSBarry Smith TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy; 40752d5ee99bSBarry Smith PetscDraw draw; 40762d5ee99bSBarry Smith MPI_Comm comm; 40772d5ee99bSBarry Smith PetscInt n; 40782d5ee99bSBarry Smith PetscMPIInt size; 407951fa3d41SBarry Smith PetscReal xl,yl,xr,yr,h; 40802d5ee99bSBarry Smith char time[32]; 40812d5ee99bSBarry Smith const PetscScalar *U; 40822d5ee99bSBarry Smith 40832d5ee99bSBarry Smith PetscFunctionBegin; 40842d5ee99bSBarry Smith ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr); 40852d5ee99bSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 40862d5ee99bSBarry Smith if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs"); 40872d5ee99bSBarry Smith ierr = VecGetSize(u,&n);CHKERRQ(ierr); 40882d5ee99bSBarry Smith if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns"); 40892d5ee99bSBarry Smith 40902d5ee99bSBarry Smith ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr); 40912d5ee99bSBarry Smith 40922d5ee99bSBarry Smith ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr); 40934b363babSBarry Smith ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr); 4094ba5783adSMatthew G Knepley if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) { 4095a4494fc1SBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 4096a4494fc1SBarry Smith PetscFunctionReturn(0); 4097a4494fc1SBarry Smith } 40982d5ee99bSBarry Smith if (!step) ictx->color++; 40992d5ee99bSBarry Smith ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr); 41002d5ee99bSBarry Smith ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr); 41012d5ee99bSBarry Smith 41022d5ee99bSBarry Smith if (ictx->showtimestepandtime) { 41034b363babSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 410485b1acf9SLisandro Dalcin ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr); 41052d5ee99bSBarry Smith h = yl + .95*(yr - yl); 410651fa3d41SBarry Smith ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr); 41072d5ee99bSBarry Smith } 41082d5ee99bSBarry Smith ierr = PetscDrawFlush(draw);CHKERRQ(ierr); 41092d5ee99bSBarry Smith PetscFunctionReturn(0); 41102d5ee99bSBarry Smith } 41112d5ee99bSBarry Smith 41121713a123SBarry Smith 41136c699258SBarry Smith #undef __FUNCT__ 411483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy" 41156083293cSBarry Smith /*@C 411683a4ac43SBarry Smith TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution() 41176083293cSBarry Smith 41186083293cSBarry Smith Collective on TS 41196083293cSBarry Smith 41206083293cSBarry Smith Input Parameters: 41216083293cSBarry Smith . ctx - the monitor context 41226083293cSBarry Smith 41236083293cSBarry Smith Level: intermediate 41246083293cSBarry Smith 41256083293cSBarry Smith .keywords: TS, vector, monitor, view 41266083293cSBarry Smith 412783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError() 41286083293cSBarry Smith @*/ 412983a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx) 41306083293cSBarry Smith { 41316083293cSBarry Smith PetscErrorCode ierr; 41326083293cSBarry Smith 41336083293cSBarry Smith PetscFunctionBegin; 41344b363babSBarry Smith ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr); 413583a4ac43SBarry Smith ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr); 413683a4ac43SBarry Smith ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr); 413783a4ac43SBarry Smith ierr = PetscFree(*ictx);CHKERRQ(ierr); 41386083293cSBarry Smith PetscFunctionReturn(0); 41396083293cSBarry Smith } 41406083293cSBarry Smith 41416083293cSBarry Smith #undef __FUNCT__ 414283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate" 41436083293cSBarry Smith /*@C 414483a4ac43SBarry Smith TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx 41456083293cSBarry Smith 41466083293cSBarry Smith Collective on TS 41476083293cSBarry Smith 41486083293cSBarry Smith Input Parameter: 41496083293cSBarry Smith . ts - time-step context 41506083293cSBarry Smith 41516083293cSBarry Smith Output Patameter: 41526083293cSBarry Smith . ctx - the monitor context 41536083293cSBarry Smith 415499fdda47SBarry Smith Options Database: 415599fdda47SBarry Smith . -ts_monitor_draw_solution_initial - show initial solution as well as current solution 415699fdda47SBarry Smith 41576083293cSBarry Smith Level: intermediate 41586083293cSBarry Smith 41596083293cSBarry Smith .keywords: TS, vector, monitor, view 41606083293cSBarry Smith 416183a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx() 41626083293cSBarry Smith @*/ 416383a4ac43SBarry Smith PetscErrorCode TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx) 41646083293cSBarry Smith { 41656083293cSBarry Smith PetscErrorCode ierr; 41666083293cSBarry Smith 41676083293cSBarry Smith PetscFunctionBegin; 4168b00a9115SJed Brown ierr = PetscNew(ctx);CHKERRQ(ierr); 416983a4ac43SBarry Smith ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr); 4170e9457bf7SBarry Smith ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr); 4171bbd56ea5SKarl Rupp 417283a4ac43SBarry Smith (*ctx)->howoften = howoften; 4173473a3ab2SBarry Smith (*ctx)->showinitial = PETSC_FALSE; 41740298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr); 4175473a3ab2SBarry Smith 4176473a3ab2SBarry Smith (*ctx)->showtimestepandtime = PETSC_FALSE; 41770298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr); 41782d5ee99bSBarry Smith (*ctx)->color = PETSC_DRAW_WHITE; 41796083293cSBarry Smith PetscFunctionReturn(0); 41806083293cSBarry Smith } 41816083293cSBarry Smith 41826083293cSBarry Smith #undef __FUNCT__ 418383a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError" 41843a471f94SBarry Smith /*@C 418583a4ac43SBarry Smith TSMonitorDrawError - Monitors progress of the TS solvers by calling 41863a471f94SBarry Smith VecView() for the error at each timestep 41873a471f94SBarry Smith 41883a471f94SBarry Smith Collective on TS 41893a471f94SBarry Smith 41903a471f94SBarry Smith Input Parameters: 41913a471f94SBarry Smith + ts - the TS context 41923a471f94SBarry Smith . step - current time-step 41933a471f94SBarry Smith . ptime - current time 41940298fd71SBarry Smith - dummy - either a viewer or NULL 41953a471f94SBarry Smith 41963a471f94SBarry Smith Level: intermediate 41973a471f94SBarry Smith 41983a471f94SBarry Smith .keywords: TS, vector, monitor, view 41993a471f94SBarry Smith 42003a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 42013a471f94SBarry Smith @*/ 42020910c330SBarry Smith PetscErrorCode TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 42033a471f94SBarry Smith { 42043a471f94SBarry Smith PetscErrorCode ierr; 420583a4ac43SBarry Smith TSMonitorDrawCtx ctx = (TSMonitorDrawCtx)dummy; 420683a4ac43SBarry Smith PetscViewer viewer = ctx->viewer; 42073a471f94SBarry Smith Vec work; 42083a471f94SBarry Smith 42093a471f94SBarry Smith PetscFunctionBegin; 4210b06615a5SLisandro Dalcin if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0); 42110910c330SBarry Smith ierr = VecDuplicate(u,&work);CHKERRQ(ierr); 42123a471f94SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr); 42130910c330SBarry Smith ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr); 42143a471f94SBarry Smith ierr = VecView(work,viewer);CHKERRQ(ierr); 42153a471f94SBarry Smith ierr = VecDestroy(&work);CHKERRQ(ierr); 42163a471f94SBarry Smith PetscFunctionReturn(0); 42173a471f94SBarry Smith } 42183a471f94SBarry Smith 4219af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 42203a471f94SBarry Smith #undef __FUNCT__ 42216c699258SBarry Smith #define __FUNCT__ "TSSetDM" 42226c699258SBarry Smith /*@ 42236c699258SBarry Smith TSSetDM - Sets the DM that may be used by some preconditioners 42246c699258SBarry Smith 42253f9fe445SBarry Smith Logically Collective on TS and DM 42266c699258SBarry Smith 42276c699258SBarry Smith Input Parameters: 42286c699258SBarry Smith + ts - the preconditioner context 42296c699258SBarry Smith - dm - the dm 42306c699258SBarry Smith 42316c699258SBarry Smith Level: intermediate 42326c699258SBarry Smith 42336c699258SBarry Smith 42346c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM() 42356c699258SBarry Smith @*/ 42367087cfbeSBarry Smith PetscErrorCode TSSetDM(TS ts,DM dm) 42376c699258SBarry Smith { 42386c699258SBarry Smith PetscErrorCode ierr; 4239089b2837SJed Brown SNES snes; 4240942e3340SBarry Smith DMTS tsdm; 42416c699258SBarry Smith 42426c699258SBarry Smith PetscFunctionBegin; 42430700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 424470663e4aSLisandro Dalcin ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 4245942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 42462a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 4247942e3340SBarry Smith ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr); 4248942e3340SBarry Smith ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr); 424924989b8cSPeter Brune if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */ 425024989b8cSPeter Brune tsdm->originaldm = dm; 425124989b8cSPeter Brune } 425224989b8cSPeter Brune } 42536bf464f9SBarry Smith ierr = DMDestroy(&ts->dm);CHKERRQ(ierr); 425424989b8cSPeter Brune } 42556c699258SBarry Smith ts->dm = dm; 4256bbd56ea5SKarl Rupp 4257089b2837SJed Brown ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 4258089b2837SJed Brown ierr = SNESSetDM(snes,dm);CHKERRQ(ierr); 42596c699258SBarry Smith PetscFunctionReturn(0); 42606c699258SBarry Smith } 42616c699258SBarry Smith 42626c699258SBarry Smith #undef __FUNCT__ 42636c699258SBarry Smith #define __FUNCT__ "TSGetDM" 42646c699258SBarry Smith /*@ 42656c699258SBarry Smith TSGetDM - Gets the DM that may be used by some preconditioners 42666c699258SBarry Smith 42673f9fe445SBarry Smith Not Collective 42686c699258SBarry Smith 42696c699258SBarry Smith Input Parameter: 42706c699258SBarry Smith . ts - the preconditioner context 42716c699258SBarry Smith 42726c699258SBarry Smith Output Parameter: 42736c699258SBarry Smith . dm - the dm 42746c699258SBarry Smith 42756c699258SBarry Smith Level: intermediate 42766c699258SBarry Smith 42776c699258SBarry Smith 42786c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM() 42796c699258SBarry Smith @*/ 42807087cfbeSBarry Smith PetscErrorCode TSGetDM(TS ts,DM *dm) 42816c699258SBarry Smith { 4282496e6a7aSJed Brown PetscErrorCode ierr; 4283496e6a7aSJed Brown 42846c699258SBarry Smith PetscFunctionBegin; 42850700a824SBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4286496e6a7aSJed Brown if (!ts->dm) { 4287ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr); 4288496e6a7aSJed Brown if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);} 4289496e6a7aSJed Brown } 42906c699258SBarry Smith *dm = ts->dm; 42916c699258SBarry Smith PetscFunctionReturn(0); 42926c699258SBarry Smith } 42931713a123SBarry Smith 42940f5c6efeSJed Brown #undef __FUNCT__ 42950f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction" 42960f5c6efeSJed Brown /*@ 42970f5c6efeSJed Brown SNESTSFormFunction - Function to evaluate nonlinear residual 42980f5c6efeSJed Brown 42993f9fe445SBarry Smith Logically Collective on SNES 43000f5c6efeSJed Brown 43010f5c6efeSJed Brown Input Parameter: 4302d42a1c89SJed Brown + snes - nonlinear solver 43030910c330SBarry Smith . U - the current state at which to evaluate the residual 4304d42a1c89SJed Brown - ctx - user context, must be a TS 43050f5c6efeSJed Brown 43060f5c6efeSJed Brown Output Parameter: 43070f5c6efeSJed Brown . F - the nonlinear residual 43080f5c6efeSJed Brown 43090f5c6efeSJed Brown Notes: 43100f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 43110f5c6efeSJed Brown It is most frequently passed to MatFDColoringSetFunction(). 43120f5c6efeSJed Brown 43130f5c6efeSJed Brown Level: advanced 43140f5c6efeSJed Brown 43150f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction() 43160f5c6efeSJed Brown @*/ 43170910c330SBarry Smith PetscErrorCode SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx) 43180f5c6efeSJed Brown { 43190f5c6efeSJed Brown TS ts = (TS)ctx; 43200f5c6efeSJed Brown PetscErrorCode ierr; 43210f5c6efeSJed Brown 43220f5c6efeSJed Brown PetscFunctionBegin; 43230f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 43240910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 43250f5c6efeSJed Brown PetscValidHeaderSpecific(F,VEC_CLASSID,3); 43260f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,4); 43270910c330SBarry Smith ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr); 43280f5c6efeSJed Brown PetscFunctionReturn(0); 43290f5c6efeSJed Brown } 43300f5c6efeSJed Brown 43310f5c6efeSJed Brown #undef __FUNCT__ 43320f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian" 43330f5c6efeSJed Brown /*@ 43340f5c6efeSJed Brown SNESTSFormJacobian - Function to evaluate the Jacobian 43350f5c6efeSJed Brown 43360f5c6efeSJed Brown Collective on SNES 43370f5c6efeSJed Brown 43380f5c6efeSJed Brown Input Parameter: 43390f5c6efeSJed Brown + snes - nonlinear solver 43400910c330SBarry Smith . U - the current state at which to evaluate the residual 43410f5c6efeSJed Brown - ctx - user context, must be a TS 43420f5c6efeSJed Brown 43430f5c6efeSJed Brown Output Parameter: 43440f5c6efeSJed Brown + A - the Jacobian 43450f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A) 43460f5c6efeSJed Brown - flag - indicates any structure change in the matrix 43470f5c6efeSJed Brown 43480f5c6efeSJed Brown Notes: 43490f5c6efeSJed Brown This function is not normally called by users and is automatically registered with the SNES used by TS. 43500f5c6efeSJed Brown 43510f5c6efeSJed Brown Level: developer 43520f5c6efeSJed Brown 43530f5c6efeSJed Brown .seealso: SNESSetJacobian() 43540f5c6efeSJed Brown @*/ 4355d1e9a80fSBarry Smith PetscErrorCode SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx) 43560f5c6efeSJed Brown { 43570f5c6efeSJed Brown TS ts = (TS)ctx; 43580f5c6efeSJed Brown PetscErrorCode ierr; 43590f5c6efeSJed Brown 43600f5c6efeSJed Brown PetscFunctionBegin; 43610f5c6efeSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 43620910c330SBarry Smith PetscValidHeaderSpecific(U,VEC_CLASSID,2); 43630f5c6efeSJed Brown PetscValidPointer(A,3); 436494ab13aaSBarry Smith PetscValidHeaderSpecific(A,MAT_CLASSID,3); 43650f5c6efeSJed Brown PetscValidPointer(B,4); 436694ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); 43670f5c6efeSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,6); 4368d1e9a80fSBarry Smith ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr); 43690f5c6efeSJed Brown PetscFunctionReturn(0); 43700f5c6efeSJed Brown } 4371325fc9f4SBarry Smith 43720e4ef248SJed Brown #undef __FUNCT__ 43730e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear" 43740e4ef248SJed Brown /*@C 43750e4ef248SJed Brown TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only 43760e4ef248SJed Brown 43770e4ef248SJed Brown Collective on TS 43780e4ef248SJed Brown 43790e4ef248SJed Brown Input Arguments: 43800e4ef248SJed Brown + ts - time stepping context 43810e4ef248SJed Brown . t - time at which to evaluate 43820910c330SBarry Smith . U - state at which to evaluate 43830e4ef248SJed Brown - ctx - context 43840e4ef248SJed Brown 43850e4ef248SJed Brown Output Arguments: 43860e4ef248SJed Brown . F - right hand side 43870e4ef248SJed Brown 43880e4ef248SJed Brown Level: intermediate 43890e4ef248SJed Brown 43900e4ef248SJed Brown Notes: 43910e4ef248SJed Brown This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems. 43920e4ef248SJed Brown The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian(). 43930e4ef248SJed Brown 43940e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant() 43950e4ef248SJed Brown @*/ 43960910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx) 43970e4ef248SJed Brown { 43980e4ef248SJed Brown PetscErrorCode ierr; 43990e4ef248SJed Brown Mat Arhs,Brhs; 44000e4ef248SJed Brown 44010e4ef248SJed Brown PetscFunctionBegin; 44020e4ef248SJed Brown ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr); 4403d1e9a80fSBarry Smith ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr); 44040910c330SBarry Smith ierr = MatMult(Arhs,U,F);CHKERRQ(ierr); 44050e4ef248SJed Brown PetscFunctionReturn(0); 44060e4ef248SJed Brown } 44070e4ef248SJed Brown 44080e4ef248SJed Brown #undef __FUNCT__ 44090e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant" 44100e4ef248SJed Brown /*@C 44110e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 44120e4ef248SJed Brown 44130e4ef248SJed Brown Collective on TS 44140e4ef248SJed Brown 44150e4ef248SJed Brown Input Arguments: 44160e4ef248SJed Brown + ts - time stepping context 44170e4ef248SJed Brown . t - time at which to evaluate 44180910c330SBarry Smith . U - state at which to evaluate 44190e4ef248SJed Brown - ctx - context 44200e4ef248SJed Brown 44210e4ef248SJed Brown Output Arguments: 44220e4ef248SJed Brown + A - pointer to operator 44230e4ef248SJed Brown . B - pointer to preconditioning matrix 44240e4ef248SJed Brown - flg - matrix structure flag 44250e4ef248SJed Brown 44260e4ef248SJed Brown Level: intermediate 44270e4ef248SJed Brown 44280e4ef248SJed Brown Notes: 44290e4ef248SJed Brown This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems. 44300e4ef248SJed Brown 44310e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear() 44320e4ef248SJed Brown @*/ 4433d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx) 44340e4ef248SJed Brown { 44350e4ef248SJed Brown PetscFunctionBegin; 44360e4ef248SJed Brown PetscFunctionReturn(0); 44370e4ef248SJed Brown } 44380e4ef248SJed Brown 44390026cea9SSean Farley #undef __FUNCT__ 44400026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear" 44410026cea9SSean Farley /*@C 44420026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 44430026cea9SSean Farley 44440026cea9SSean Farley Collective on TS 44450026cea9SSean Farley 44460026cea9SSean Farley Input Arguments: 44470026cea9SSean Farley + ts - time stepping context 44480026cea9SSean Farley . t - time at which to evaluate 44490910c330SBarry Smith . U - state at which to evaluate 44500910c330SBarry Smith . Udot - time derivative of state vector 44510026cea9SSean Farley - ctx - context 44520026cea9SSean Farley 44530026cea9SSean Farley Output Arguments: 44540026cea9SSean Farley . F - left hand side 44550026cea9SSean Farley 44560026cea9SSean Farley Level: intermediate 44570026cea9SSean Farley 44580026cea9SSean Farley Notes: 44590910c330SBarry 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 44600026cea9SSean Farley user is required to write their own TSComputeIFunction. 44610026cea9SSean Farley This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems. 44620026cea9SSean Farley The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian(). 44630026cea9SSean Farley 44640026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant() 44650026cea9SSean Farley @*/ 44660910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx) 44670026cea9SSean Farley { 44680026cea9SSean Farley PetscErrorCode ierr; 44690026cea9SSean Farley Mat A,B; 44700026cea9SSean Farley 44710026cea9SSean Farley PetscFunctionBegin; 44720298fd71SBarry Smith ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr); 4473d1e9a80fSBarry Smith ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr); 44740910c330SBarry Smith ierr = MatMult(A,Udot,F);CHKERRQ(ierr); 44750026cea9SSean Farley PetscFunctionReturn(0); 44760026cea9SSean Farley } 44770026cea9SSean Farley 44780026cea9SSean Farley #undef __FUNCT__ 44790026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant" 44800026cea9SSean Farley /*@C 4481b41af12eSJed Brown TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE 44820026cea9SSean Farley 44830026cea9SSean Farley Collective on TS 44840026cea9SSean Farley 44850026cea9SSean Farley Input Arguments: 44860026cea9SSean Farley + ts - time stepping context 44870026cea9SSean Farley . t - time at which to evaluate 44880910c330SBarry Smith . U - state at which to evaluate 44890910c330SBarry Smith . Udot - time derivative of state vector 44900026cea9SSean Farley . shift - shift to apply 44910026cea9SSean Farley - ctx - context 44920026cea9SSean Farley 44930026cea9SSean Farley Output Arguments: 44940026cea9SSean Farley + A - pointer to operator 44950026cea9SSean Farley . B - pointer to preconditioning matrix 44960026cea9SSean Farley - flg - matrix structure flag 44970026cea9SSean Farley 4498b41af12eSJed Brown Level: advanced 44990026cea9SSean Farley 45000026cea9SSean Farley Notes: 45010026cea9SSean Farley This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems. 45020026cea9SSean Farley 4503b41af12eSJed Brown It is only appropriate for problems of the form 4504b41af12eSJed Brown 4505b41af12eSJed Brown $ M Udot = F(U,t) 4506b41af12eSJed Brown 4507b41af12eSJed Brown where M is constant and F is non-stiff. The user must pass M to TSSetIJacobian(). The current implementation only 4508b41af12eSJed Brown works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing 4509b41af12eSJed Brown an implicit operator of the form 4510b41af12eSJed Brown 4511b41af12eSJed Brown $ shift*M + J 4512b41af12eSJed Brown 4513b41af12eSJed 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 4514b41af12eSJed Brown a copy of M or reassemble it when requested. 4515b41af12eSJed Brown 45160026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear() 45170026cea9SSean Farley @*/ 4518d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx) 45190026cea9SSean Farley { 4520b41af12eSJed Brown PetscErrorCode ierr; 4521b41af12eSJed Brown 45220026cea9SSean Farley PetscFunctionBegin; 452394ab13aaSBarry Smith ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr); 4524b41af12eSJed Brown ts->ijacobian.shift = shift; 45250026cea9SSean Farley PetscFunctionReturn(0); 45260026cea9SSean Farley } 4527b41af12eSJed Brown 4528e817cc15SEmil Constantinescu #undef __FUNCT__ 4529e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType" 4530e817cc15SEmil Constantinescu /*@ 4531e817cc15SEmil Constantinescu TSGetEquationType - Gets the type of the equation that TS is solving. 4532e817cc15SEmil Constantinescu 4533e817cc15SEmil Constantinescu Not Collective 4534e817cc15SEmil Constantinescu 4535e817cc15SEmil Constantinescu Input Parameter: 4536e817cc15SEmil Constantinescu . ts - the TS context 4537e817cc15SEmil Constantinescu 4538e817cc15SEmil Constantinescu Output Parameter: 45394e6b9ce4SEmil Constantinescu . equation_type - see TSEquationType 4540e817cc15SEmil Constantinescu 4541e817cc15SEmil Constantinescu Level: beginner 4542e817cc15SEmil Constantinescu 4543e817cc15SEmil Constantinescu .keywords: TS, equation type 4544e817cc15SEmil Constantinescu 4545e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType 4546e817cc15SEmil Constantinescu @*/ 4547e817cc15SEmil Constantinescu PetscErrorCode TSGetEquationType(TS ts,TSEquationType *equation_type) 4548e817cc15SEmil Constantinescu { 4549e817cc15SEmil Constantinescu PetscFunctionBegin; 4550e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4551e817cc15SEmil Constantinescu PetscValidPointer(equation_type,2); 4552e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 4553e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4554e817cc15SEmil Constantinescu } 4555e817cc15SEmil Constantinescu 4556e817cc15SEmil Constantinescu #undef __FUNCT__ 4557e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType" 4558e817cc15SEmil Constantinescu /*@ 4559e817cc15SEmil Constantinescu TSSetEquationType - Sets the type of the equation that TS is solving. 4560e817cc15SEmil Constantinescu 4561e817cc15SEmil Constantinescu Not Collective 4562e817cc15SEmil Constantinescu 4563e817cc15SEmil Constantinescu Input Parameter: 4564e817cc15SEmil Constantinescu + ts - the TS context 45651297b224SEmil Constantinescu - equation_type - see TSEquationType 4566e817cc15SEmil Constantinescu 4567e817cc15SEmil Constantinescu Level: advanced 4568e817cc15SEmil Constantinescu 4569e817cc15SEmil Constantinescu .keywords: TS, equation type 4570e817cc15SEmil Constantinescu 4571e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType 4572e817cc15SEmil Constantinescu @*/ 4573e817cc15SEmil Constantinescu PetscErrorCode TSSetEquationType(TS ts,TSEquationType equation_type) 4574e817cc15SEmil Constantinescu { 4575e817cc15SEmil Constantinescu PetscFunctionBegin; 4576e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4577e817cc15SEmil Constantinescu ts->equation_type = equation_type; 4578e817cc15SEmil Constantinescu PetscFunctionReturn(0); 4579e817cc15SEmil Constantinescu } 45800026cea9SSean Farley 45814af1b03aSJed Brown #undef __FUNCT__ 45824af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason" 45834af1b03aSJed Brown /*@ 45844af1b03aSJed Brown TSGetConvergedReason - Gets the reason the TS iteration was stopped. 45854af1b03aSJed Brown 45864af1b03aSJed Brown Not Collective 45874af1b03aSJed Brown 45884af1b03aSJed Brown Input Parameter: 45894af1b03aSJed Brown . ts - the TS context 45904af1b03aSJed Brown 45914af1b03aSJed Brown Output Parameter: 45924af1b03aSJed Brown . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 45934af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 45944af1b03aSJed Brown 4595487e0bb9SJed Brown Level: beginner 45964af1b03aSJed Brown 4597cd652676SJed Brown Notes: 4598cd652676SJed Brown Can only be called after the call to TSSolve() is complete. 45994af1b03aSJed Brown 46004af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test 46014af1b03aSJed Brown 46024af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason 46034af1b03aSJed Brown @*/ 46044af1b03aSJed Brown PetscErrorCode TSGetConvergedReason(TS ts,TSConvergedReason *reason) 46054af1b03aSJed Brown { 46064af1b03aSJed Brown PetscFunctionBegin; 46074af1b03aSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 46084af1b03aSJed Brown PetscValidPointer(reason,2); 46094af1b03aSJed Brown *reason = ts->reason; 46104af1b03aSJed Brown PetscFunctionReturn(0); 46114af1b03aSJed Brown } 46124af1b03aSJed Brown 4613fb1732b5SBarry Smith #undef __FUNCT__ 4614d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason" 4615d6ad946cSShri Abhyankar /*@ 4616d6ad946cSShri Abhyankar TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve. 4617d6ad946cSShri Abhyankar 4618d6ad946cSShri Abhyankar Not Collective 4619d6ad946cSShri Abhyankar 4620d6ad946cSShri Abhyankar Input Parameter: 4621d6ad946cSShri Abhyankar + ts - the TS context 4622d6ad946cSShri Abhyankar . reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the 4623d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 4624d6ad946cSShri Abhyankar 4625f5abba47SShri Abhyankar Level: advanced 4626d6ad946cSShri Abhyankar 4627d6ad946cSShri Abhyankar Notes: 4628d6ad946cSShri Abhyankar Can only be called during TSSolve() is active. 4629d6ad946cSShri Abhyankar 4630d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test 4631d6ad946cSShri Abhyankar 4632d6ad946cSShri Abhyankar .seealso: TSConvergedReason 4633d6ad946cSShri Abhyankar @*/ 4634d6ad946cSShri Abhyankar PetscErrorCode TSSetConvergedReason(TS ts,TSConvergedReason reason) 4635d6ad946cSShri Abhyankar { 4636d6ad946cSShri Abhyankar PetscFunctionBegin; 4637d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4638d6ad946cSShri Abhyankar ts->reason = reason; 4639d6ad946cSShri Abhyankar PetscFunctionReturn(0); 4640d6ad946cSShri Abhyankar } 4641d6ad946cSShri Abhyankar 4642d6ad946cSShri Abhyankar #undef __FUNCT__ 4643cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime" 4644cc708dedSBarry Smith /*@ 4645cc708dedSBarry Smith TSGetSolveTime - Gets the time after a call to TSSolve() 4646cc708dedSBarry Smith 4647cc708dedSBarry Smith Not Collective 4648cc708dedSBarry Smith 4649cc708dedSBarry Smith Input Parameter: 4650cc708dedSBarry Smith . ts - the TS context 4651cc708dedSBarry Smith 4652cc708dedSBarry Smith Output Parameter: 4653cc708dedSBarry Smith . ftime - the final time. This time should correspond to the final time set with TSSetDuration() 4654cc708dedSBarry Smith 4655487e0bb9SJed Brown Level: beginner 4656cc708dedSBarry Smith 4657cc708dedSBarry Smith Notes: 4658cc708dedSBarry Smith Can only be called after the call to TSSolve() is complete. 4659cc708dedSBarry Smith 4660cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test 4661cc708dedSBarry Smith 4662cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 4663cc708dedSBarry Smith @*/ 4664cc708dedSBarry Smith PetscErrorCode TSGetSolveTime(TS ts,PetscReal *ftime) 4665cc708dedSBarry Smith { 4666cc708dedSBarry Smith PetscFunctionBegin; 4667cc708dedSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4668cc708dedSBarry Smith PetscValidPointer(ftime,2); 4669cc708dedSBarry Smith *ftime = ts->solvetime; 4670cc708dedSBarry Smith PetscFunctionReturn(0); 4671cc708dedSBarry Smith } 4672cc708dedSBarry Smith 4673cc708dedSBarry Smith #undef __FUNCT__ 46742c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps" 46752c18e0fdSBarry Smith /*@ 46762c18e0fdSBarry Smith TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate() 46772c18e0fdSBarry Smith 46782c18e0fdSBarry Smith Not Collective 46792c18e0fdSBarry Smith 46802c18e0fdSBarry Smith Input Parameter: 46812c18e0fdSBarry Smith . ts - the TS context 46822c18e0fdSBarry Smith 46832c18e0fdSBarry Smith Output Parameter: 46842c18e0fdSBarry Smith . steps - the number of steps 46852c18e0fdSBarry Smith 46862c18e0fdSBarry Smith Level: beginner 46872c18e0fdSBarry Smith 46882c18e0fdSBarry Smith Notes: 46892c18e0fdSBarry Smith Includes the number of steps for all calls to TSSolve() since TSSetUp() was called 46902c18e0fdSBarry Smith 46912c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test 46922c18e0fdSBarry Smith 46932c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason 46942c18e0fdSBarry Smith @*/ 46952c18e0fdSBarry Smith PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) 46962c18e0fdSBarry Smith { 46972c18e0fdSBarry Smith PetscFunctionBegin; 46982c18e0fdSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 46992c18e0fdSBarry Smith PetscValidPointer(steps,2); 47002c18e0fdSBarry Smith *steps = ts->total_steps; 47012c18e0fdSBarry Smith PetscFunctionReturn(0); 47022c18e0fdSBarry Smith } 47032c18e0fdSBarry Smith 47042c18e0fdSBarry Smith #undef __FUNCT__ 47055ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations" 47069f67acb7SJed Brown /*@ 47075ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 47089f67acb7SJed Brown used by the time integrator. 47099f67acb7SJed Brown 47109f67acb7SJed Brown Not Collective 47119f67acb7SJed Brown 47129f67acb7SJed Brown Input Parameter: 47139f67acb7SJed Brown . ts - TS context 47149f67acb7SJed Brown 47159f67acb7SJed Brown Output Parameter: 47169f67acb7SJed Brown . nits - number of nonlinear iterations 47179f67acb7SJed Brown 47189f67acb7SJed Brown Notes: 47199f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 47209f67acb7SJed Brown 47219f67acb7SJed Brown Level: intermediate 47229f67acb7SJed Brown 47239f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations 47249f67acb7SJed Brown 47255ef26d82SJed Brown .seealso: TSGetKSPIterations() 47269f67acb7SJed Brown @*/ 47275ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits) 47289f67acb7SJed Brown { 47299f67acb7SJed Brown PetscFunctionBegin; 47309f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 47319f67acb7SJed Brown PetscValidIntPointer(nits,2); 47325ef26d82SJed Brown *nits = ts->snes_its; 47339f67acb7SJed Brown PetscFunctionReturn(0); 47349f67acb7SJed Brown } 47359f67acb7SJed Brown 47369f67acb7SJed Brown #undef __FUNCT__ 47375ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations" 47389f67acb7SJed Brown /*@ 47395ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 47409f67acb7SJed Brown used by the time integrator. 47419f67acb7SJed Brown 47429f67acb7SJed Brown Not Collective 47439f67acb7SJed Brown 47449f67acb7SJed Brown Input Parameter: 47459f67acb7SJed Brown . ts - TS context 47469f67acb7SJed Brown 47479f67acb7SJed Brown Output Parameter: 47489f67acb7SJed Brown . lits - number of linear iterations 47499f67acb7SJed Brown 47509f67acb7SJed Brown Notes: 47519f67acb7SJed Brown This counter is reset to zero for each successive call to TSSolve(). 47529f67acb7SJed Brown 47539f67acb7SJed Brown Level: intermediate 47549f67acb7SJed Brown 47559f67acb7SJed Brown .keywords: TS, get, number, linear, iterations 47569f67acb7SJed Brown 47575ef26d82SJed Brown .seealso: TSGetSNESIterations(), SNESGetKSPIterations() 47589f67acb7SJed Brown @*/ 47595ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits) 47609f67acb7SJed Brown { 47619f67acb7SJed Brown PetscFunctionBegin; 47629f67acb7SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 47639f67acb7SJed Brown PetscValidIntPointer(lits,2); 47645ef26d82SJed Brown *lits = ts->ksp_its; 47659f67acb7SJed Brown PetscFunctionReturn(0); 47669f67acb7SJed Brown } 47679f67acb7SJed Brown 47689f67acb7SJed Brown #undef __FUNCT__ 4769cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections" 4770cef5090cSJed Brown /*@ 4771cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 4772cef5090cSJed Brown 4773cef5090cSJed Brown Not Collective 4774cef5090cSJed Brown 4775cef5090cSJed Brown Input Parameter: 4776cef5090cSJed Brown . ts - TS context 4777cef5090cSJed Brown 4778cef5090cSJed Brown Output Parameter: 4779cef5090cSJed Brown . rejects - number of steps rejected 4780cef5090cSJed Brown 4781cef5090cSJed Brown Notes: 4782cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4783cef5090cSJed Brown 4784cef5090cSJed Brown Level: intermediate 4785cef5090cSJed Brown 4786cef5090cSJed Brown .keywords: TS, get, number 4787cef5090cSJed Brown 47885ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails() 4789cef5090cSJed Brown @*/ 4790cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects) 4791cef5090cSJed Brown { 4792cef5090cSJed Brown PetscFunctionBegin; 4793cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4794cef5090cSJed Brown PetscValidIntPointer(rejects,2); 4795cef5090cSJed Brown *rejects = ts->reject; 4796cef5090cSJed Brown PetscFunctionReturn(0); 4797cef5090cSJed Brown } 4798cef5090cSJed Brown 4799cef5090cSJed Brown #undef __FUNCT__ 4800cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures" 4801cef5090cSJed Brown /*@ 4802cef5090cSJed Brown TSGetSNESFailures - Gets the total number of failed SNES solves 4803cef5090cSJed Brown 4804cef5090cSJed Brown Not Collective 4805cef5090cSJed Brown 4806cef5090cSJed Brown Input Parameter: 4807cef5090cSJed Brown . ts - TS context 4808cef5090cSJed Brown 4809cef5090cSJed Brown Output Parameter: 4810cef5090cSJed Brown . fails - number of failed nonlinear solves 4811cef5090cSJed Brown 4812cef5090cSJed Brown Notes: 4813cef5090cSJed Brown This counter is reset to zero for each successive call to TSSolve(). 4814cef5090cSJed Brown 4815cef5090cSJed Brown Level: intermediate 4816cef5090cSJed Brown 4817cef5090cSJed Brown .keywords: TS, get, number 4818cef5090cSJed Brown 48195ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures() 4820cef5090cSJed Brown @*/ 4821cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails) 4822cef5090cSJed Brown { 4823cef5090cSJed Brown PetscFunctionBegin; 4824cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4825cef5090cSJed Brown PetscValidIntPointer(fails,2); 4826cef5090cSJed Brown *fails = ts->num_snes_failures; 4827cef5090cSJed Brown PetscFunctionReturn(0); 4828cef5090cSJed Brown } 4829cef5090cSJed Brown 4830cef5090cSJed Brown #undef __FUNCT__ 4831cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections" 4832cef5090cSJed Brown /*@ 4833cef5090cSJed Brown TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails 4834cef5090cSJed Brown 4835cef5090cSJed Brown Not Collective 4836cef5090cSJed Brown 4837cef5090cSJed Brown Input Parameter: 4838cef5090cSJed Brown + ts - TS context 4839cef5090cSJed Brown - rejects - maximum number of rejected steps, pass -1 for unlimited 4840cef5090cSJed Brown 4841cef5090cSJed Brown Notes: 4842cef5090cSJed Brown The counter is reset to zero for each step 4843cef5090cSJed Brown 4844cef5090cSJed Brown Options Database Key: 4845cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 4846cef5090cSJed Brown 4847cef5090cSJed Brown Level: intermediate 4848cef5090cSJed Brown 4849cef5090cSJed Brown .keywords: TS, set, maximum, number 4850cef5090cSJed Brown 48515ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4852cef5090cSJed Brown @*/ 4853cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects) 4854cef5090cSJed Brown { 4855cef5090cSJed Brown PetscFunctionBegin; 4856cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4857cef5090cSJed Brown ts->max_reject = rejects; 4858cef5090cSJed Brown PetscFunctionReturn(0); 4859cef5090cSJed Brown } 4860cef5090cSJed Brown 4861cef5090cSJed Brown #undef __FUNCT__ 4862cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures" 4863cef5090cSJed Brown /*@ 4864cef5090cSJed Brown TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves 4865cef5090cSJed Brown 4866cef5090cSJed Brown Not Collective 4867cef5090cSJed Brown 4868cef5090cSJed Brown Input Parameter: 4869cef5090cSJed Brown + ts - TS context 4870cef5090cSJed Brown - fails - maximum number of failed nonlinear solves, pass -1 for unlimited 4871cef5090cSJed Brown 4872cef5090cSJed Brown Notes: 4873cef5090cSJed Brown The counter is reset to zero for each successive call to TSSolve(). 4874cef5090cSJed Brown 4875cef5090cSJed Brown Options Database Key: 4876cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 4877cef5090cSJed Brown 4878cef5090cSJed Brown Level: intermediate 4879cef5090cSJed Brown 4880cef5090cSJed Brown .keywords: TS, set, maximum, number 4881cef5090cSJed Brown 48825ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason() 4883cef5090cSJed Brown @*/ 4884cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails) 4885cef5090cSJed Brown { 4886cef5090cSJed Brown PetscFunctionBegin; 4887cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4888cef5090cSJed Brown ts->max_snes_failures = fails; 4889cef5090cSJed Brown PetscFunctionReturn(0); 4890cef5090cSJed Brown } 4891cef5090cSJed Brown 4892cef5090cSJed Brown #undef __FUNCT__ 48934e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails" 4894cef5090cSJed Brown /*@ 4895cef5090cSJed Brown TSSetErrorIfStepFails - Error if no step succeeds 4896cef5090cSJed Brown 4897cef5090cSJed Brown Not Collective 4898cef5090cSJed Brown 4899cef5090cSJed Brown Input Parameter: 4900cef5090cSJed Brown + ts - TS context 4901cef5090cSJed Brown - err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure 4902cef5090cSJed Brown 4903cef5090cSJed Brown Options Database Key: 4904cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 4905cef5090cSJed Brown 4906cef5090cSJed Brown Level: intermediate 4907cef5090cSJed Brown 4908cef5090cSJed Brown .keywords: TS, set, error 4909cef5090cSJed Brown 49105ef26d82SJed Brown .seealso: TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason() 4911cef5090cSJed Brown @*/ 4912cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err) 4913cef5090cSJed Brown { 4914cef5090cSJed Brown PetscFunctionBegin; 4915cef5090cSJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 4916cef5090cSJed Brown ts->errorifstepfailed = err; 4917cef5090cSJed Brown PetscFunctionReturn(0); 4918cef5090cSJed Brown } 4919cef5090cSJed Brown 4920cef5090cSJed Brown #undef __FUNCT__ 4921fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary" 4922fb1732b5SBarry Smith /*@C 4923fb1732b5SBarry Smith TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file 4924fb1732b5SBarry Smith 4925fb1732b5SBarry Smith Collective on TS 4926fb1732b5SBarry Smith 4927fb1732b5SBarry Smith Input Parameters: 4928fb1732b5SBarry Smith + ts - the TS context 4929fb1732b5SBarry Smith . step - current time-step 4930fb1732b5SBarry Smith . ptime - current time 49310910c330SBarry Smith . u - current state 4932fb1732b5SBarry Smith - viewer - binary viewer 4933fb1732b5SBarry Smith 4934fb1732b5SBarry Smith Level: intermediate 4935fb1732b5SBarry Smith 4936fb1732b5SBarry Smith .keywords: TS, vector, monitor, view 4937fb1732b5SBarry Smith 4938fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4939fb1732b5SBarry Smith @*/ 49400910c330SBarry Smith PetscErrorCode TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer) 4941fb1732b5SBarry Smith { 4942fb1732b5SBarry Smith PetscErrorCode ierr; 4943ed81e22dSJed Brown PetscViewer v = (PetscViewer)viewer; 4944fb1732b5SBarry Smith 4945fb1732b5SBarry Smith PetscFunctionBegin; 49460910c330SBarry Smith ierr = VecView(u,v);CHKERRQ(ierr); 4947ed81e22dSJed Brown PetscFunctionReturn(0); 4948ed81e22dSJed Brown } 4949ed81e22dSJed Brown 4950ed81e22dSJed Brown #undef __FUNCT__ 4951ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK" 4952ed81e22dSJed Brown /*@C 4953ed81e22dSJed Brown TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep. 4954ed81e22dSJed Brown 4955ed81e22dSJed Brown Collective on TS 4956ed81e22dSJed Brown 4957ed81e22dSJed Brown Input Parameters: 4958ed81e22dSJed Brown + ts - the TS context 4959ed81e22dSJed Brown . step - current time-step 4960ed81e22dSJed Brown . ptime - current time 49610910c330SBarry Smith . u - current state 4962ed81e22dSJed Brown - filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 4963ed81e22dSJed Brown 4964ed81e22dSJed Brown Level: intermediate 4965ed81e22dSJed Brown 4966ed81e22dSJed Brown Notes: 4967ed81e22dSJed Brown The VTK format does not allow writing multiple time steps in the same file, therefore a different file will be written for each time step. 4968ed81e22dSJed Brown These are named according to the file name template. 4969ed81e22dSJed Brown 4970ed81e22dSJed Brown This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy(). 4971ed81e22dSJed Brown 4972ed81e22dSJed Brown .keywords: TS, vector, monitor, view 4973ed81e22dSJed Brown 4974ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 4975ed81e22dSJed Brown @*/ 49760910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate) 4977ed81e22dSJed Brown { 4978ed81e22dSJed Brown PetscErrorCode ierr; 4979ed81e22dSJed Brown char filename[PETSC_MAX_PATH_LEN]; 4980ed81e22dSJed Brown PetscViewer viewer; 4981ed81e22dSJed Brown 4982ed81e22dSJed Brown PetscFunctionBegin; 49838caf3d72SBarry Smith ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr); 4984ce94432eSBarry Smith ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); 49850910c330SBarry Smith ierr = VecView(u,viewer);CHKERRQ(ierr); 4986ed81e22dSJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 4987ed81e22dSJed Brown PetscFunctionReturn(0); 4988ed81e22dSJed Brown } 4989ed81e22dSJed Brown 4990ed81e22dSJed Brown #undef __FUNCT__ 4991ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy" 4992ed81e22dSJed Brown /*@C 4993ed81e22dSJed Brown TSMonitorSolutionVTKDestroy - Destroy context for monitoring 4994ed81e22dSJed Brown 4995ed81e22dSJed Brown Collective on TS 4996ed81e22dSJed Brown 4997ed81e22dSJed Brown Input Parameters: 4998ed81e22dSJed Brown . filenametemplate - string containing a format specifier for the integer time step (e.g. %03D) 4999ed81e22dSJed Brown 5000ed81e22dSJed Brown Level: intermediate 5001ed81e22dSJed Brown 5002ed81e22dSJed Brown Note: 5003ed81e22dSJed Brown This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK(). 5004ed81e22dSJed Brown 5005ed81e22dSJed Brown .keywords: TS, vector, monitor, view 5006ed81e22dSJed Brown 5007ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK() 5008ed81e22dSJed Brown @*/ 5009ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate) 5010ed81e22dSJed Brown { 5011ed81e22dSJed Brown PetscErrorCode ierr; 5012ed81e22dSJed Brown 5013ed81e22dSJed Brown PetscFunctionBegin; 5014ed81e22dSJed Brown ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr); 5015fb1732b5SBarry Smith PetscFunctionReturn(0); 5016fb1732b5SBarry Smith } 5017fb1732b5SBarry Smith 501884df9cb4SJed Brown #undef __FUNCT__ 5019552698daSJed Brown #define __FUNCT__ "TSGetAdapt" 502084df9cb4SJed Brown /*@ 5021552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 502284df9cb4SJed Brown 5023ed81e22dSJed Brown Collective on TS if controller has not been created yet 502484df9cb4SJed Brown 502584df9cb4SJed Brown Input Arguments: 5026ed81e22dSJed Brown . ts - time stepping context 502784df9cb4SJed Brown 502884df9cb4SJed Brown Output Arguments: 5029ed81e22dSJed Brown . adapt - adaptive controller 503084df9cb4SJed Brown 503184df9cb4SJed Brown Level: intermediate 503284df9cb4SJed Brown 5033ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose() 503484df9cb4SJed Brown @*/ 5035552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt) 503684df9cb4SJed Brown { 503784df9cb4SJed Brown PetscErrorCode ierr; 503884df9cb4SJed Brown 503984df9cb4SJed Brown PetscFunctionBegin; 504084df9cb4SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 504184df9cb4SJed Brown PetscValidPointer(adapt,2); 504284df9cb4SJed Brown if (!ts->adapt) { 5043ce94432eSBarry Smith ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr); 50443bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr); 50451c3436cfSJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr); 504684df9cb4SJed Brown } 504784df9cb4SJed Brown *adapt = ts->adapt; 504884df9cb4SJed Brown PetscFunctionReturn(0); 504984df9cb4SJed Brown } 5050d6ebe24aSShri Abhyankar 5051d6ebe24aSShri Abhyankar #undef __FUNCT__ 50521c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances" 50531c3436cfSJed Brown /*@ 50541c3436cfSJed Brown TSSetTolerances - Set tolerances for local truncation error when using adaptive controller 50551c3436cfSJed Brown 50561c3436cfSJed Brown Logically Collective 50571c3436cfSJed Brown 50581c3436cfSJed Brown Input Arguments: 50591c3436cfSJed Brown + ts - time integration context 50601c3436cfSJed Brown . atol - scalar absolute tolerances, PETSC_DECIDE to leave current value 50610298fd71SBarry Smith . vatol - vector of absolute tolerances or NULL, used in preference to atol if present 50621c3436cfSJed Brown . rtol - scalar relative tolerances, PETSC_DECIDE to leave current value 50630298fd71SBarry Smith - vrtol - vector of relative tolerances or NULL, used in preference to atol if present 50641c3436cfSJed Brown 5065a3cdaa26SBarry Smith Options Database keys: 5066a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 5067a3cdaa26SBarry Smith - -ts_atol <atol> Absolute tolerance for local truncation error 5068a3cdaa26SBarry Smith 50693ff766beSShri Abhyankar Notes: 50703ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 50713ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 50723ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 50733ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 50743ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 50753ff766beSShri Abhyankar differential variables. 50763ff766beSShri Abhyankar 50771c3436cfSJed Brown Level: beginner 50781c3436cfSJed Brown 5079c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances() 50801c3436cfSJed Brown @*/ 50811c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol) 50821c3436cfSJed Brown { 50831c3436cfSJed Brown PetscErrorCode ierr; 50841c3436cfSJed Brown 50851c3436cfSJed Brown PetscFunctionBegin; 5086c5033834SJed Brown if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol; 50871c3436cfSJed Brown if (vatol) { 50881c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr); 50891c3436cfSJed Brown ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr); 5090bbd56ea5SKarl Rupp 50911c3436cfSJed Brown ts->vatol = vatol; 50921c3436cfSJed Brown } 5093c5033834SJed Brown if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol; 50941c3436cfSJed Brown if (vrtol) { 50951c3436cfSJed Brown ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr); 50961c3436cfSJed Brown ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr); 5097bbd56ea5SKarl Rupp 50981c3436cfSJed Brown ts->vrtol = vrtol; 50991c3436cfSJed Brown } 51001c3436cfSJed Brown PetscFunctionReturn(0); 51011c3436cfSJed Brown } 51021c3436cfSJed Brown 51031c3436cfSJed Brown #undef __FUNCT__ 5104c5033834SJed Brown #define __FUNCT__ "TSGetTolerances" 5105c5033834SJed Brown /*@ 5106c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 5107c5033834SJed Brown 5108c5033834SJed Brown Logically Collective 5109c5033834SJed Brown 5110c5033834SJed Brown Input Arguments: 5111c5033834SJed Brown . ts - time integration context 5112c5033834SJed Brown 5113c5033834SJed Brown Output Arguments: 51140298fd71SBarry Smith + atol - scalar absolute tolerances, NULL to ignore 51150298fd71SBarry Smith . vatol - vector of absolute tolerances, NULL to ignore 51160298fd71SBarry Smith . rtol - scalar relative tolerances, NULL to ignore 51170298fd71SBarry Smith - vrtol - vector of relative tolerances, NULL to ignore 5118c5033834SJed Brown 5119c5033834SJed Brown Level: beginner 5120c5033834SJed Brown 5121c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances() 5122c5033834SJed Brown @*/ 5123c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol) 5124c5033834SJed Brown { 5125c5033834SJed Brown PetscFunctionBegin; 5126c5033834SJed Brown if (atol) *atol = ts->atol; 5127c5033834SJed Brown if (vatol) *vatol = ts->vatol; 5128c5033834SJed Brown if (rtol) *rtol = ts->rtol; 5129c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 5130c5033834SJed Brown PetscFunctionReturn(0); 5131c5033834SJed Brown } 5132c5033834SJed Brown 5133c5033834SJed Brown #undef __FUNCT__ 51349c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNorm2" 51359c6b16b5SShri Abhyankar /*@ 5136a4868fbcSLisandro Dalcin TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors 51379c6b16b5SShri Abhyankar 51389c6b16b5SShri Abhyankar Collective on TS 51399c6b16b5SShri Abhyankar 51409c6b16b5SShri Abhyankar Input Arguments: 51419c6b16b5SShri Abhyankar + ts - time stepping context 5142a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5143a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 51449c6b16b5SShri Abhyankar 51459c6b16b5SShri Abhyankar Output Arguments: 51469c6b16b5SShri Abhyankar . norm - weighted norm, a value of 1.0 is considered small 51479c6b16b5SShri Abhyankar 51489c6b16b5SShri Abhyankar Level: developer 51499c6b16b5SShri Abhyankar 5150deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity() 51519c6b16b5SShri Abhyankar @*/ 5152a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm) 51539c6b16b5SShri Abhyankar { 51549c6b16b5SShri Abhyankar PetscErrorCode ierr; 51559c6b16b5SShri Abhyankar PetscInt i,n,N,rstart; 51569c6b16b5SShri Abhyankar const PetscScalar *u,*y; 51579c6b16b5SShri Abhyankar PetscReal sum,gsum; 51589c6b16b5SShri Abhyankar PetscReal tol; 51599c6b16b5SShri Abhyankar 51609c6b16b5SShri Abhyankar PetscFunctionBegin; 51619c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5162a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5163a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5164a4868fbcSLisandro Dalcin PetscValidType(U,2); 5165a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5166a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5167a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 5168a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 51699c6b16b5SShri Abhyankar 51709c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 51719c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 51729c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 51739c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 51749c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 51759c6b16b5SShri Abhyankar sum = 0.; 51769c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 51779c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 51789c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 51799c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 51809c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 51819c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 51829c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 51839c6b16b5SShri Abhyankar } 51849c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 51859c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 51869c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 51879c6b16b5SShri Abhyankar const PetscScalar *atol; 51889c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 51899c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 51909c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 51919c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 51929c6b16b5SShri Abhyankar } 51939c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 51949c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 51959c6b16b5SShri Abhyankar const PetscScalar *rtol; 51969c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 51979c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 51989c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 51999c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 52009c6b16b5SShri Abhyankar } 52019c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 52029c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 52039c6b16b5SShri Abhyankar for (i=0; i<n; i++) { 52049c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52059c6b16b5SShri Abhyankar sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol); 52069c6b16b5SShri Abhyankar } 52079c6b16b5SShri Abhyankar } 52089c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 52099c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 52109c6b16b5SShri Abhyankar 52119c6b16b5SShri Abhyankar ierr = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 52129c6b16b5SShri Abhyankar *norm = PetscSqrtReal(gsum / N); 52139c6b16b5SShri Abhyankar 52149c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 52159c6b16b5SShri Abhyankar PetscFunctionReturn(0); 52169c6b16b5SShri Abhyankar } 52179c6b16b5SShri Abhyankar 52189c6b16b5SShri Abhyankar #undef __FUNCT__ 52199c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNormInfinity" 52209c6b16b5SShri Abhyankar /*@ 5221a4868fbcSLisandro Dalcin TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors 52229c6b16b5SShri Abhyankar 52239c6b16b5SShri Abhyankar Collective on TS 52249c6b16b5SShri Abhyankar 52259c6b16b5SShri Abhyankar Input Arguments: 52269c6b16b5SShri Abhyankar + ts - time stepping context 5227a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5228a4868fbcSLisandro Dalcin - Y - state vector to be compared to U 52299c6b16b5SShri Abhyankar 52309c6b16b5SShri Abhyankar Output Arguments: 52319c6b16b5SShri Abhyankar . norm - weighted norm, a value of 1.0 is considered small 52329c6b16b5SShri Abhyankar 52339c6b16b5SShri Abhyankar Level: developer 52349c6b16b5SShri Abhyankar 5235deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2() 52369c6b16b5SShri Abhyankar @*/ 5237a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm) 52389c6b16b5SShri Abhyankar { 52399c6b16b5SShri Abhyankar PetscErrorCode ierr; 52409c6b16b5SShri Abhyankar PetscInt i,n,N,rstart,k; 52419c6b16b5SShri Abhyankar const PetscScalar *u,*y; 52429c6b16b5SShri Abhyankar PetscReal max,gmax; 52439c6b16b5SShri Abhyankar PetscReal tol; 52449c6b16b5SShri Abhyankar 52459c6b16b5SShri Abhyankar PetscFunctionBegin; 52469c6b16b5SShri Abhyankar PetscValidHeaderSpecific(ts,TS_CLASSID,1); 5247a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(U,VEC_CLASSID,2); 5248a4868fbcSLisandro Dalcin PetscValidHeaderSpecific(Y,VEC_CLASSID,3); 5249a4868fbcSLisandro Dalcin PetscValidType(U,2); 5250a4868fbcSLisandro Dalcin PetscValidType(Y,3); 5251a4868fbcSLisandro Dalcin PetscCheckSameComm(U,2,Y,3); 5252a4868fbcSLisandro Dalcin PetscValidPointer(norm,4); 5253a4868fbcSLisandro Dalcin if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector"); 52549c6b16b5SShri Abhyankar 52559c6b16b5SShri Abhyankar ierr = VecGetSize(U,&N);CHKERRQ(ierr); 52569c6b16b5SShri Abhyankar ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr); 52579c6b16b5SShri Abhyankar ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr); 52589c6b16b5SShri Abhyankar ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); 52599c6b16b5SShri Abhyankar ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr); 52609c6b16b5SShri Abhyankar if (ts->vatol && ts->vrtol) { 52619c6b16b5SShri Abhyankar const PetscScalar *atol,*rtol; 52629c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 52639c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 52649c6b16b5SShri Abhyankar k = 0; 52659c6b16b5SShri Abhyankar tol = PetscRealPart(atol[k]) + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 52669c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 52679c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 52689c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52699c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 52709c6b16b5SShri Abhyankar } 52719c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 52729c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 52739c6b16b5SShri Abhyankar } else if (ts->vatol) { /* vector atol, scalar rtol */ 52749c6b16b5SShri Abhyankar const PetscScalar *atol; 52759c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 52769c6b16b5SShri Abhyankar k = 0; 52779c6b16b5SShri Abhyankar tol = PetscRealPart(atol[k]) + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 52789c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 52799c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 52809c6b16b5SShri Abhyankar tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52819c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 52829c6b16b5SShri Abhyankar } 52839c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr); 52849c6b16b5SShri Abhyankar } else if (ts->vrtol) { /* scalar atol, vector rtol */ 52859c6b16b5SShri Abhyankar const PetscScalar *rtol; 52869c6b16b5SShri Abhyankar ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 52879c6b16b5SShri Abhyankar k = 0; 52889c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 52899c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 52909c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 52919c6b16b5SShri Abhyankar tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 52929c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 52939c6b16b5SShri Abhyankar } 52949c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr); 52959c6b16b5SShri Abhyankar } else { /* scalar atol, scalar rtol */ 52969c6b16b5SShri Abhyankar k = 0; 52979c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k])); 52989c6b16b5SShri Abhyankar max = PetscAbsScalar(y[k] - u[k]) / tol; 52999c6b16b5SShri Abhyankar for (i=1; i<n; i++) { 53009c6b16b5SShri Abhyankar tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i])); 53019c6b16b5SShri Abhyankar max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol); 53029c6b16b5SShri Abhyankar } 53039c6b16b5SShri Abhyankar } 53049c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); 53059c6b16b5SShri Abhyankar ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr); 53069c6b16b5SShri Abhyankar 53079c6b16b5SShri Abhyankar ierr = MPI_Allreduce(&max,&gmax,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 53089c6b16b5SShri Abhyankar *norm = gmax; 53099c6b16b5SShri Abhyankar 53109c6b16b5SShri Abhyankar if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm"); 53119c6b16b5SShri Abhyankar PetscFunctionReturn(0); 53129c6b16b5SShri Abhyankar } 53139c6b16b5SShri Abhyankar 53149c6b16b5SShri Abhyankar #undef __FUNCT__ 53157619abb3SShri #define __FUNCT__ "TSErrorWeightedNorm" 53161c3436cfSJed Brown /*@ 5317a4868fbcSLisandro Dalcin TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors 53181c3436cfSJed Brown 53191c3436cfSJed Brown Collective on TS 53201c3436cfSJed Brown 53211c3436cfSJed Brown Input Arguments: 53221c3436cfSJed Brown + ts - time stepping context 5323a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5324a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 5325a4868fbcSLisandro Dalcin - wnormtype - norm type, either NORM_2 or NORM_INFINITY 53267619abb3SShri 53271c3436cfSJed Brown Output Arguments: 53281c3436cfSJed Brown . norm - weighted norm, a value of 1.0 is considered small 53291c3436cfSJed Brown 5330a4868fbcSLisandro Dalcin 5331a4868fbcSLisandro Dalcin Options Database Keys: 5332a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 5333a4868fbcSLisandro Dalcin 53341c3436cfSJed Brown Level: developer 53351c3436cfSJed Brown 5336deea92deSShri .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2() 53371c3436cfSJed Brown @*/ 5338a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm) 53391c3436cfSJed Brown { 53408beabaa1SBarry Smith PetscErrorCode ierr; 53411c3436cfSJed Brown 53421c3436cfSJed Brown PetscFunctionBegin; 5343a4868fbcSLisandro Dalcin if (wnormtype == NORM_2) { 5344a4868fbcSLisandro Dalcin ierr = TSErrorWeightedNorm2(ts,U,Y,norm);CHKERRQ(ierr); 5345a4868fbcSLisandro Dalcin } else if(wnormtype == NORM_INFINITY) { 5346a4868fbcSLisandro Dalcin ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm);CHKERRQ(ierr); 5347a4868fbcSLisandro Dalcin } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]); 53481c3436cfSJed Brown PetscFunctionReturn(0); 53491c3436cfSJed Brown } 53501c3436cfSJed Brown 53511c3436cfSJed Brown #undef __FUNCT__ 53528d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal" 53538d59e960SJed Brown /*@ 53548d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 53558d59e960SJed Brown 53568d59e960SJed Brown Logically Collective on TS 53578d59e960SJed Brown 53588d59e960SJed Brown Input Arguments: 53598d59e960SJed Brown + ts - time stepping context 53608d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 53618d59e960SJed Brown 53628d59e960SJed Brown Note: 53638d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 53648d59e960SJed Brown 53658d59e960SJed Brown Level: intermediate 53668d59e960SJed Brown 53678d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL 53688d59e960SJed Brown @*/ 53698d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime) 53708d59e960SJed Brown { 53718d59e960SJed Brown PetscFunctionBegin; 53728d59e960SJed Brown PetscValidHeaderSpecific(ts,TS_CLASSID,1); 53738d59e960SJed Brown ts->cfltime_local = cfltime; 53748d59e960SJed Brown ts->cfltime = -1.; 53758d59e960SJed Brown PetscFunctionReturn(0); 53768d59e960SJed Brown } 53778d59e960SJed Brown 53788d59e960SJed Brown #undef __FUNCT__ 53798d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime" 53808d59e960SJed Brown /*@ 53818d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 53828d59e960SJed Brown 53838d59e960SJed Brown Collective on TS 53848d59e960SJed Brown 53858d59e960SJed Brown Input Arguments: 53868d59e960SJed Brown . ts - time stepping context 53878d59e960SJed Brown 53888d59e960SJed Brown Output Arguments: 53898d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 53908d59e960SJed Brown 53918d59e960SJed Brown Level: advanced 53928d59e960SJed Brown 53938d59e960SJed Brown .seealso: TSSetCFLTimeLocal() 53948d59e960SJed Brown @*/ 53958d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime) 53968d59e960SJed Brown { 53978d59e960SJed Brown PetscErrorCode ierr; 53988d59e960SJed Brown 53998d59e960SJed Brown PetscFunctionBegin; 54008d59e960SJed Brown if (ts->cfltime < 0) { 5401ce94432eSBarry Smith ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr); 54028d59e960SJed Brown } 54038d59e960SJed Brown *cfltime = ts->cfltime; 54048d59e960SJed Brown PetscFunctionReturn(0); 54058d59e960SJed Brown } 54068d59e960SJed Brown 54078d59e960SJed Brown #undef __FUNCT__ 5408d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds" 5409d6ebe24aSShri Abhyankar /*@ 5410d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 5411d6ebe24aSShri Abhyankar 5412d6ebe24aSShri Abhyankar Input Parameters: 5413d6ebe24aSShri Abhyankar . ts - the TS context. 5414d6ebe24aSShri Abhyankar . xl - lower bound. 5415d6ebe24aSShri Abhyankar . xu - upper bound. 5416d6ebe24aSShri Abhyankar 5417d6ebe24aSShri Abhyankar Notes: 5418d6ebe24aSShri Abhyankar If this routine is not called then the lower and upper bounds are set to 5419e270355aSBarry Smith PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp(). 5420d6ebe24aSShri Abhyankar 54212bd2b0e6SSatish Balay Level: advanced 54222bd2b0e6SSatish Balay 5423d6ebe24aSShri Abhyankar @*/ 5424d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 5425d6ebe24aSShri Abhyankar { 5426d6ebe24aSShri Abhyankar PetscErrorCode ierr; 5427d6ebe24aSShri Abhyankar SNES snes; 5428d6ebe24aSShri Abhyankar 5429d6ebe24aSShri Abhyankar PetscFunctionBegin; 5430d6ebe24aSShri Abhyankar ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 5431d6ebe24aSShri Abhyankar ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr); 5432d6ebe24aSShri Abhyankar PetscFunctionReturn(0); 5433d6ebe24aSShri Abhyankar } 5434d6ebe24aSShri Abhyankar 5435325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 5436c6db04a5SJed Brown #include <mex.h> 5437325fc9f4SBarry Smith 5438325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext; 5439325fc9f4SBarry Smith 5440325fc9f4SBarry Smith #undef __FUNCT__ 5441325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab" 5442325fc9f4SBarry Smith /* 5443325fc9f4SBarry Smith TSComputeFunction_Matlab - Calls the function that has been set with 5444325fc9f4SBarry Smith TSSetFunctionMatlab(). 5445325fc9f4SBarry Smith 5446325fc9f4SBarry Smith Collective on TS 5447325fc9f4SBarry Smith 5448325fc9f4SBarry Smith Input Parameters: 5449325fc9f4SBarry Smith + snes - the TS context 54500910c330SBarry Smith - u - input vector 5451325fc9f4SBarry Smith 5452325fc9f4SBarry Smith Output Parameter: 5453325fc9f4SBarry Smith . y - function vector, as set by TSSetFunction() 5454325fc9f4SBarry Smith 5455325fc9f4SBarry Smith Notes: 5456325fc9f4SBarry Smith TSComputeFunction() is typically used within nonlinear solvers 5457325fc9f4SBarry Smith implementations, so most users would not generally call this routine 5458325fc9f4SBarry Smith themselves. 5459325fc9f4SBarry Smith 5460325fc9f4SBarry Smith Level: developer 5461325fc9f4SBarry Smith 5462325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 5463325fc9f4SBarry Smith 5464325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5465325fc9f4SBarry Smith */ 54660910c330SBarry Smith PetscErrorCode TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx) 5467325fc9f4SBarry Smith { 5468325fc9f4SBarry Smith PetscErrorCode ierr; 5469325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5470325fc9f4SBarry Smith int nlhs = 1,nrhs = 7; 5471325fc9f4SBarry Smith mxArray *plhs[1],*prhs[7]; 5472325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,ly = 0,ls = 0; 5473325fc9f4SBarry Smith 5474325fc9f4SBarry Smith PetscFunctionBegin; 5475325fc9f4SBarry Smith PetscValidHeaderSpecific(snes,TS_CLASSID,1); 54760910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 54770910c330SBarry Smith PetscValidHeaderSpecific(udot,VEC_CLASSID,4); 5478325fc9f4SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,5); 54790910c330SBarry Smith PetscCheckSameComm(snes,1,u,3); 5480325fc9f4SBarry Smith PetscCheckSameComm(snes,1,y,5); 5481325fc9f4SBarry Smith 5482325fc9f4SBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 54830910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 54840910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr); 54850910c330SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr); 5486bbd56ea5SKarl Rupp 5487325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5488325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar(time); 5489325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 5490325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 5491325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)ly); 5492325fc9f4SBarry Smith prhs[5] = mxCreateString(sctx->funcname); 5493325fc9f4SBarry Smith prhs[6] = sctx->ctx; 5494325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr); 5495325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5496325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 5497325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 5498325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 5499325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 5500325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 5501325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 5502325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 5503325fc9f4SBarry Smith PetscFunctionReturn(0); 5504325fc9f4SBarry Smith } 5505325fc9f4SBarry Smith 5506325fc9f4SBarry Smith 5507325fc9f4SBarry Smith #undef __FUNCT__ 5508325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab" 5509325fc9f4SBarry Smith /* 5510325fc9f4SBarry Smith TSSetFunctionMatlab - Sets the function evaluation routine and function 5511325fc9f4SBarry Smith vector for use by the TS routines in solving ODEs 5512e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 5513325fc9f4SBarry Smith 5514325fc9f4SBarry Smith Logically Collective on TS 5515325fc9f4SBarry Smith 5516325fc9f4SBarry Smith Input Parameters: 5517325fc9f4SBarry Smith + ts - the TS context 5518325fc9f4SBarry Smith - func - function evaluation routine 5519325fc9f4SBarry Smith 5520325fc9f4SBarry Smith Calling sequence of func: 55210910c330SBarry Smith $ func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx); 5522325fc9f4SBarry Smith 5523325fc9f4SBarry Smith Level: beginner 5524325fc9f4SBarry Smith 5525325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 5526325fc9f4SBarry Smith 5527325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5528325fc9f4SBarry Smith */ 5529cdcf91faSSean Farley PetscErrorCode TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx) 5530325fc9f4SBarry Smith { 5531325fc9f4SBarry Smith PetscErrorCode ierr; 5532325fc9f4SBarry Smith TSMatlabContext *sctx; 5533325fc9f4SBarry Smith 5534325fc9f4SBarry Smith PetscFunctionBegin; 5535325fc9f4SBarry Smith /* currently sctx is memory bleed */ 5536325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5537325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5538325fc9f4SBarry Smith /* 5539325fc9f4SBarry Smith This should work, but it doesn't 5540325fc9f4SBarry Smith sctx->ctx = ctx; 5541325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5542325fc9f4SBarry Smith */ 5543325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5544bbd56ea5SKarl Rupp 55450298fd71SBarry Smith ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr); 5546325fc9f4SBarry Smith PetscFunctionReturn(0); 5547325fc9f4SBarry Smith } 5548325fc9f4SBarry Smith 5549325fc9f4SBarry Smith #undef __FUNCT__ 5550325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab" 5551325fc9f4SBarry Smith /* 5552325fc9f4SBarry Smith TSComputeJacobian_Matlab - Calls the function that has been set with 5553325fc9f4SBarry Smith TSSetJacobianMatlab(). 5554325fc9f4SBarry Smith 5555325fc9f4SBarry Smith Collective on TS 5556325fc9f4SBarry Smith 5557325fc9f4SBarry Smith Input Parameters: 5558cdcf91faSSean Farley + ts - the TS context 55590910c330SBarry Smith . u - input vector 5560325fc9f4SBarry Smith . A, B - the matrices 5561325fc9f4SBarry Smith - ctx - user context 5562325fc9f4SBarry Smith 5563325fc9f4SBarry Smith Level: developer 5564325fc9f4SBarry Smith 5565325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function 5566325fc9f4SBarry Smith 5567325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5568325fc9f4SBarry Smith @*/ 5569f3229a78SSatish Balay PetscErrorCode TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx) 5570325fc9f4SBarry Smith { 5571325fc9f4SBarry Smith PetscErrorCode ierr; 5572325fc9f4SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5573325fc9f4SBarry Smith int nlhs = 2,nrhs = 9; 5574325fc9f4SBarry Smith mxArray *plhs[2],*prhs[9]; 5575325fc9f4SBarry Smith long long int lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0; 5576325fc9f4SBarry Smith 5577325fc9f4SBarry Smith PetscFunctionBegin; 5578cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 55790910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,3); 5580325fc9f4SBarry Smith 55810910c330SBarry Smith /* call Matlab function in ctx with arguments u and y */ 5582325fc9f4SBarry Smith 5583cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 55840910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 55850910c330SBarry Smith ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr); 55860910c330SBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr); 55870910c330SBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr); 5588bbd56ea5SKarl Rupp 5589325fc9f4SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5590325fc9f4SBarry Smith prhs[1] = mxCreateDoubleScalar((double)time); 5591325fc9f4SBarry Smith prhs[2] = mxCreateDoubleScalar((double)lx); 5592325fc9f4SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lxdot); 5593325fc9f4SBarry Smith prhs[4] = mxCreateDoubleScalar((double)shift); 5594325fc9f4SBarry Smith prhs[5] = mxCreateDoubleScalar((double)lA); 5595325fc9f4SBarry Smith prhs[6] = mxCreateDoubleScalar((double)lB); 5596325fc9f4SBarry Smith prhs[7] = mxCreateString(sctx->funcname); 5597325fc9f4SBarry Smith prhs[8] = sctx->ctx; 5598325fc9f4SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr); 5599325fc9f4SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5600325fc9f4SBarry Smith mxDestroyArray(prhs[0]); 5601325fc9f4SBarry Smith mxDestroyArray(prhs[1]); 5602325fc9f4SBarry Smith mxDestroyArray(prhs[2]); 5603325fc9f4SBarry Smith mxDestroyArray(prhs[3]); 5604325fc9f4SBarry Smith mxDestroyArray(prhs[4]); 5605325fc9f4SBarry Smith mxDestroyArray(prhs[5]); 5606325fc9f4SBarry Smith mxDestroyArray(prhs[6]); 5607325fc9f4SBarry Smith mxDestroyArray(prhs[7]); 5608325fc9f4SBarry Smith mxDestroyArray(plhs[0]); 5609325fc9f4SBarry Smith mxDestroyArray(plhs[1]); 5610325fc9f4SBarry Smith PetscFunctionReturn(0); 5611325fc9f4SBarry Smith } 5612325fc9f4SBarry Smith 5613325fc9f4SBarry Smith 5614325fc9f4SBarry Smith #undef __FUNCT__ 5615325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab" 5616325fc9f4SBarry Smith /* 5617325fc9f4SBarry Smith TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 5618e3c5b3baSBarry Smith vector for use by the TS routines in solving ODEs from MATLAB. Here the function is a string containing the name of a MATLAB function 5619325fc9f4SBarry Smith 5620325fc9f4SBarry Smith Logically Collective on TS 5621325fc9f4SBarry Smith 5622325fc9f4SBarry Smith Input Parameters: 5623cdcf91faSSean Farley + ts - the TS context 5624325fc9f4SBarry Smith . A,B - Jacobian matrices 5625325fc9f4SBarry Smith . func - function evaluation routine 5626325fc9f4SBarry Smith - ctx - user context 5627325fc9f4SBarry Smith 5628325fc9f4SBarry Smith Calling sequence of func: 56290910c330SBarry Smith $ flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx); 5630325fc9f4SBarry Smith 5631325fc9f4SBarry Smith 5632325fc9f4SBarry Smith Level: developer 5633325fc9f4SBarry Smith 5634325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function 5635325fc9f4SBarry Smith 5636325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5637325fc9f4SBarry Smith */ 5638cdcf91faSSean Farley PetscErrorCode TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx) 5639325fc9f4SBarry Smith { 5640325fc9f4SBarry Smith PetscErrorCode ierr; 5641325fc9f4SBarry Smith TSMatlabContext *sctx; 5642325fc9f4SBarry Smith 5643325fc9f4SBarry Smith PetscFunctionBegin; 5644325fc9f4SBarry Smith /* currently sctx is memory bleed */ 5645325fc9f4SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5646325fc9f4SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5647325fc9f4SBarry Smith /* 5648325fc9f4SBarry Smith This should work, but it doesn't 5649325fc9f4SBarry Smith sctx->ctx = ctx; 5650325fc9f4SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5651325fc9f4SBarry Smith */ 5652325fc9f4SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5653bbd56ea5SKarl Rupp 5654cdcf91faSSean Farley ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 5655325fc9f4SBarry Smith PetscFunctionReturn(0); 5656325fc9f4SBarry Smith } 5657325fc9f4SBarry Smith 5658b5b1a830SBarry Smith #undef __FUNCT__ 5659b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab" 5660b5b1a830SBarry Smith /* 5661b5b1a830SBarry Smith TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab(). 5662b5b1a830SBarry Smith 5663b5b1a830SBarry Smith Collective on TS 5664b5b1a830SBarry Smith 5665b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction() 5666b5b1a830SBarry Smith @*/ 56670910c330SBarry Smith PetscErrorCode TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx) 5668b5b1a830SBarry Smith { 5669b5b1a830SBarry Smith PetscErrorCode ierr; 5670b5b1a830SBarry Smith TSMatlabContext *sctx = (TSMatlabContext*)ctx; 5671a530c242SBarry Smith int nlhs = 1,nrhs = 6; 5672b5b1a830SBarry Smith mxArray *plhs[1],*prhs[6]; 5673b5b1a830SBarry Smith long long int lx = 0,ls = 0; 5674b5b1a830SBarry Smith 5675b5b1a830SBarry Smith PetscFunctionBegin; 5676cdcf91faSSean Farley PetscValidHeaderSpecific(ts,TS_CLASSID,1); 56770910c330SBarry Smith PetscValidHeaderSpecific(u,VEC_CLASSID,4); 5678b5b1a830SBarry Smith 5679cdcf91faSSean Farley ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr); 56800910c330SBarry Smith ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr); 5681bbd56ea5SKarl Rupp 5682b5b1a830SBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 5683b5b1a830SBarry Smith prhs[1] = mxCreateDoubleScalar((double)it); 5684b5b1a830SBarry Smith prhs[2] = mxCreateDoubleScalar((double)time); 5685b5b1a830SBarry Smith prhs[3] = mxCreateDoubleScalar((double)lx); 5686b5b1a830SBarry Smith prhs[4] = mxCreateString(sctx->funcname); 5687b5b1a830SBarry Smith prhs[5] = sctx->ctx; 5688b5b1a830SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr); 5689b5b1a830SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5690b5b1a830SBarry Smith mxDestroyArray(prhs[0]); 5691b5b1a830SBarry Smith mxDestroyArray(prhs[1]); 5692b5b1a830SBarry Smith mxDestroyArray(prhs[2]); 5693b5b1a830SBarry Smith mxDestroyArray(prhs[3]); 5694b5b1a830SBarry Smith mxDestroyArray(prhs[4]); 5695b5b1a830SBarry Smith mxDestroyArray(plhs[0]); 5696b5b1a830SBarry Smith PetscFunctionReturn(0); 5697b5b1a830SBarry Smith } 5698b5b1a830SBarry Smith 5699b5b1a830SBarry Smith 5700b5b1a830SBarry Smith #undef __FUNCT__ 5701b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab" 5702b5b1a830SBarry Smith /* 5703b5b1a830SBarry Smith TSMonitorSetMatlab - Sets the monitor function from Matlab 5704b5b1a830SBarry Smith 5705b5b1a830SBarry Smith Level: developer 5706b5b1a830SBarry Smith 5707b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function 5708b5b1a830SBarry Smith 5709b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction() 5710b5b1a830SBarry Smith */ 5711cdcf91faSSean Farley PetscErrorCode TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx) 5712b5b1a830SBarry Smith { 5713b5b1a830SBarry Smith PetscErrorCode ierr; 5714b5b1a830SBarry Smith TSMatlabContext *sctx; 5715b5b1a830SBarry Smith 5716b5b1a830SBarry Smith PetscFunctionBegin; 5717b5b1a830SBarry Smith /* currently sctx is memory bleed */ 5718b5b1a830SBarry Smith ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr); 5719b5b1a830SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 5720b5b1a830SBarry Smith /* 5721b5b1a830SBarry Smith This should work, but it doesn't 5722b5b1a830SBarry Smith sctx->ctx = ctx; 5723b5b1a830SBarry Smith mexMakeArrayPersistent(sctx->ctx); 5724b5b1a830SBarry Smith */ 5725b5b1a830SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 5726bbd56ea5SKarl Rupp 57270298fd71SBarry Smith ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 5728b5b1a830SBarry Smith PetscFunctionReturn(0); 5729b5b1a830SBarry Smith } 5730325fc9f4SBarry Smith #endif 5731b3603a34SBarry Smith 5732b3603a34SBarry Smith #undef __FUNCT__ 57334f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution" 5734b3603a34SBarry Smith /*@C 57354f09c107SBarry Smith TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector 5736b3603a34SBarry Smith in a time based line graph 5737b3603a34SBarry Smith 5738b3603a34SBarry Smith Collective on TS 5739b3603a34SBarry Smith 5740b3603a34SBarry Smith Input Parameters: 5741b3603a34SBarry Smith + ts - the TS context 5742b3603a34SBarry Smith . step - current time-step 5743b3603a34SBarry Smith . ptime - current time 5744b3603a34SBarry Smith - lg - a line graph object 5745b3603a34SBarry Smith 5746b3d3934dSBarry Smith Options Database: 57479ae14b6eSBarry Smith . -ts_monitor_lg_solution_variables 5748b3d3934dSBarry Smith 5749b3603a34SBarry Smith Level: intermediate 5750b3603a34SBarry Smith 57510b039ecaSBarry Smith Notes: each process in a parallel run displays its component solutions in a separate window 5752b3603a34SBarry Smith 5753b3603a34SBarry Smith .keywords: TS, vector, monitor, view 5754b3603a34SBarry Smith 5755b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView() 5756b3603a34SBarry Smith @*/ 57570910c330SBarry Smith PetscErrorCode TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 5758b3603a34SBarry Smith { 5759b3603a34SBarry Smith PetscErrorCode ierr; 57600b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 5761b3603a34SBarry Smith const PetscScalar *yy; 576258ff32f7SBarry Smith PetscInt dim; 576380666b62SBarry Smith Vec v; 5764b3603a34SBarry Smith 5765b3603a34SBarry Smith PetscFunctionBegin; 576658ff32f7SBarry Smith if (!step) { 5767a9f9c1f6SBarry Smith PetscDrawAxis axis; 5768a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 5769a9f9c1f6SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr); 5770387f4636SBarry Smith if (ctx->names && !ctx->displaynames) { 5771387f4636SBarry Smith char **displaynames; 5772387f4636SBarry Smith PetscBool flg; 5773387f4636SBarry Smith 5774387f4636SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 5775387f4636SBarry Smith ierr = PetscMalloc((dim+1)*sizeof(char*),&displaynames);CHKERRQ(ierr); 5776387f4636SBarry Smith ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr); 57779ae14b6eSBarry Smith ierr = PetscOptionsGetStringArray(((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr); 5778387f4636SBarry Smith if (flg) { 5779a66092f1SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr); 5780387f4636SBarry Smith } 5781387f4636SBarry Smith ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr); 5782387f4636SBarry Smith } 5783387f4636SBarry Smith if (ctx->displaynames) { 5784387f4636SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr); 5785387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr); 5786387f4636SBarry Smith } else if (ctx->names) { 57870910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 57880b039ecaSBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 5789387f4636SBarry Smith ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr); 5790387f4636SBarry Smith } 57910b039ecaSBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 579258ff32f7SBarry Smith } 579380666b62SBarry Smith if (ctx->transform) { 5794e673d494SBarry Smith ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr); 579580666b62SBarry Smith } else { 579680666b62SBarry Smith v = u; 579780666b62SBarry Smith } 579880666b62SBarry Smith ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr); 5799e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 5800e3efe391SJed Brown { 5801e3efe391SJed Brown PetscReal *yreal; 5802e3efe391SJed Brown PetscInt i,n; 580380666b62SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 5804785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 5805e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 58060b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 5807e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 5808e3efe391SJed Brown } 5809e3efe391SJed Brown #else 5810387f4636SBarry Smith if (ctx->displaynames) { 5811387f4636SBarry Smith PetscInt i; 5812387f4636SBarry Smith for (i=0; i<ctx->ndisplayvariables; i++) { 5813387f4636SBarry Smith ctx->displayvalues[i] = yy[ctx->displayvariables[i]]; 5814387f4636SBarry Smith } 5815387f4636SBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr); 5816387f4636SBarry Smith } else { 58170b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 5818387f4636SBarry Smith } 5819e3efe391SJed Brown #endif 582080666b62SBarry Smith ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr); 582180666b62SBarry Smith if (ctx->transform) { 582280666b62SBarry Smith ierr = VecDestroy(&v);CHKERRQ(ierr); 582380666b62SBarry Smith } 5824b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 58250b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 58263923b477SBarry Smith } 5827b3603a34SBarry Smith PetscFunctionReturn(0); 5828b3603a34SBarry Smith } 5829b3603a34SBarry Smith 5830387f4636SBarry Smith 5831b3603a34SBarry Smith #undef __FUNCT__ 583231152f8aSBarry Smith #define __FUNCT__ "TSMonitorLGSetVariableNames" 5833b037adc7SBarry Smith /*@C 583431152f8aSBarry Smith TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 5835b037adc7SBarry Smith 5836b037adc7SBarry Smith Collective on TS 5837b037adc7SBarry Smith 5838b037adc7SBarry Smith Input Parameters: 5839b037adc7SBarry Smith + ts - the TS context 5840b3d3934dSBarry Smith - names - the names of the components, final string must be NULL 5841b037adc7SBarry Smith 5842b037adc7SBarry Smith Level: intermediate 5843b037adc7SBarry Smith 5844b037adc7SBarry Smith .keywords: TS, vector, monitor, view 5845b037adc7SBarry Smith 5846a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames() 5847b037adc7SBarry Smith @*/ 584831152f8aSBarry Smith PetscErrorCode TSMonitorLGSetVariableNames(TS ts,const char * const *names) 5849b037adc7SBarry Smith { 5850b037adc7SBarry Smith PetscErrorCode ierr; 5851b037adc7SBarry Smith PetscInt i; 5852b037adc7SBarry Smith 5853b037adc7SBarry Smith PetscFunctionBegin; 5854b037adc7SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 5855b037adc7SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 58565537e223SBarry Smith ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr); 5857b3d3934dSBarry Smith break; 5858b3d3934dSBarry Smith } 5859b3d3934dSBarry Smith } 5860b3d3934dSBarry Smith PetscFunctionReturn(0); 5861b3d3934dSBarry Smith } 5862b3d3934dSBarry Smith 5863b3d3934dSBarry Smith #undef __FUNCT__ 5864e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetVariableNames" 5865e673d494SBarry Smith /*@C 5866e673d494SBarry Smith TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot 5867e673d494SBarry Smith 5868e673d494SBarry Smith Collective on TS 5869e673d494SBarry Smith 5870e673d494SBarry Smith Input Parameters: 5871e673d494SBarry Smith + ts - the TS context 5872e673d494SBarry Smith - names - the names of the components, final string must be NULL 5873e673d494SBarry Smith 5874e673d494SBarry Smith Level: intermediate 5875e673d494SBarry Smith 5876e673d494SBarry Smith .keywords: TS, vector, monitor, view 5877e673d494SBarry Smith 5878a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames() 5879e673d494SBarry Smith @*/ 5880e673d494SBarry Smith PetscErrorCode TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names) 5881e673d494SBarry Smith { 5882e673d494SBarry Smith PetscErrorCode ierr; 5883e673d494SBarry Smith 5884e673d494SBarry Smith PetscFunctionBegin; 5885e673d494SBarry Smith ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr); 5886e673d494SBarry Smith ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr); 5887e673d494SBarry Smith PetscFunctionReturn(0); 5888e673d494SBarry Smith } 5889e673d494SBarry Smith 5890e673d494SBarry Smith #undef __FUNCT__ 5891b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorLGGetVariableNames" 5892b3d3934dSBarry Smith /*@C 5893b3d3934dSBarry Smith TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot 5894b3d3934dSBarry Smith 5895b3d3934dSBarry Smith Collective on TS 5896b3d3934dSBarry Smith 5897b3d3934dSBarry Smith Input Parameter: 5898b3d3934dSBarry Smith . ts - the TS context 5899b3d3934dSBarry Smith 5900b3d3934dSBarry Smith Output Parameter: 5901b3d3934dSBarry Smith . names - the names of the components, final string must be NULL 5902b3d3934dSBarry Smith 5903b3d3934dSBarry Smith Level: intermediate 5904b3d3934dSBarry Smith 5905b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 5906b3d3934dSBarry Smith 5907b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 5908b3d3934dSBarry Smith @*/ 5909b3d3934dSBarry Smith PetscErrorCode TSMonitorLGGetVariableNames(TS ts,const char *const **names) 5910b3d3934dSBarry Smith { 5911b3d3934dSBarry Smith PetscInt i; 5912b3d3934dSBarry Smith 5913b3d3934dSBarry Smith PetscFunctionBegin; 5914b3d3934dSBarry Smith *names = NULL; 5915b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 5916b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 59175537e223SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) ts->monitorcontext[i]; 5918b3d3934dSBarry Smith *names = (const char *const *)ctx->names; 5919b3d3934dSBarry Smith break; 5920387f4636SBarry Smith } 5921387f4636SBarry Smith } 5922387f4636SBarry Smith PetscFunctionReturn(0); 5923387f4636SBarry Smith } 5924387f4636SBarry Smith 5925387f4636SBarry Smith #undef __FUNCT__ 5926a66092f1SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetDisplayVariables" 5927a66092f1SBarry Smith /*@C 5928a66092f1SBarry Smith TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor 5929a66092f1SBarry Smith 5930a66092f1SBarry Smith Collective on TS 5931a66092f1SBarry Smith 5932a66092f1SBarry Smith Input Parameters: 5933a66092f1SBarry Smith + ctx - the TSMonitorLG context 5934a66092f1SBarry Smith . displaynames - the names of the components, final string must be NULL 5935a66092f1SBarry Smith 5936a66092f1SBarry Smith Level: intermediate 5937a66092f1SBarry Smith 5938a66092f1SBarry Smith .keywords: TS, vector, monitor, view 5939a66092f1SBarry Smith 5940a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 5941a66092f1SBarry Smith @*/ 5942a66092f1SBarry Smith PetscErrorCode TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames) 5943a66092f1SBarry Smith { 5944a66092f1SBarry Smith PetscInt j = 0,k; 5945a66092f1SBarry Smith PetscErrorCode ierr; 5946a66092f1SBarry Smith 5947a66092f1SBarry Smith PetscFunctionBegin; 5948a66092f1SBarry Smith if (!ctx->names) PetscFunctionReturn(0); 5949a66092f1SBarry Smith ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr); 5950a66092f1SBarry Smith ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr); 5951a66092f1SBarry Smith while (displaynames[j]) j++; 5952a66092f1SBarry Smith ctx->ndisplayvariables = j; 5953a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr); 5954a66092f1SBarry Smith ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr); 5955a66092f1SBarry Smith j = 0; 5956a66092f1SBarry Smith while (displaynames[j]) { 5957a66092f1SBarry Smith k = 0; 5958a66092f1SBarry Smith while (ctx->names[k]) { 5959a66092f1SBarry Smith PetscBool flg; 5960a66092f1SBarry Smith ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr); 5961a66092f1SBarry Smith if (flg) { 5962a66092f1SBarry Smith ctx->displayvariables[j] = k; 5963a66092f1SBarry Smith break; 5964a66092f1SBarry Smith } 5965a66092f1SBarry Smith k++; 5966a66092f1SBarry Smith } 5967a66092f1SBarry Smith j++; 5968a66092f1SBarry Smith } 5969a66092f1SBarry Smith PetscFunctionReturn(0); 5970a66092f1SBarry Smith } 5971a66092f1SBarry Smith 5972a66092f1SBarry Smith 5973a66092f1SBarry Smith #undef __FUNCT__ 5974387f4636SBarry Smith #define __FUNCT__ "TSMonitorLGSetDisplayVariables" 5975387f4636SBarry Smith /*@C 5976387f4636SBarry Smith TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor 5977387f4636SBarry Smith 5978387f4636SBarry Smith Collective on TS 5979387f4636SBarry Smith 5980387f4636SBarry Smith Input Parameters: 5981387f4636SBarry Smith + ts - the TS context 5982387f4636SBarry Smith . displaynames - the names of the components, final string must be NULL 5983387f4636SBarry Smith 5984387f4636SBarry Smith Level: intermediate 5985387f4636SBarry Smith 5986387f4636SBarry Smith .keywords: TS, vector, monitor, view 5987387f4636SBarry Smith 5988387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames() 5989387f4636SBarry Smith @*/ 5990387f4636SBarry Smith PetscErrorCode TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames) 5991387f4636SBarry Smith { 5992a66092f1SBarry Smith PetscInt i; 5993387f4636SBarry Smith PetscErrorCode ierr; 5994387f4636SBarry Smith 5995387f4636SBarry Smith PetscFunctionBegin; 5996387f4636SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 5997387f4636SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 59985537e223SBarry Smith ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr); 5999b3d3934dSBarry Smith break; 6000b037adc7SBarry Smith } 6001b037adc7SBarry Smith } 6002b037adc7SBarry Smith PetscFunctionReturn(0); 6003b037adc7SBarry Smith } 6004b037adc7SBarry Smith 6005b037adc7SBarry Smith #undef __FUNCT__ 600680666b62SBarry Smith #define __FUNCT__ "TSMonitorLGSetTransform" 600780666b62SBarry Smith /*@C 600880666b62SBarry Smith TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed 600980666b62SBarry Smith 601080666b62SBarry Smith Collective on TS 601180666b62SBarry Smith 601280666b62SBarry Smith Input Parameters: 601380666b62SBarry Smith + ts - the TS context 601480666b62SBarry Smith . transform - the transform function 60157684fa3eSBarry Smith . destroy - function to destroy the optional context 601680666b62SBarry Smith - ctx - optional context used by transform function 601780666b62SBarry Smith 601880666b62SBarry Smith Level: intermediate 601980666b62SBarry Smith 602080666b62SBarry Smith .keywords: TS, vector, monitor, view 602180666b62SBarry Smith 6022a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform() 602380666b62SBarry Smith @*/ 60247684fa3eSBarry Smith PetscErrorCode TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 602580666b62SBarry Smith { 602680666b62SBarry Smith PetscInt i; 6027a66092f1SBarry Smith PetscErrorCode ierr; 602880666b62SBarry Smith 602980666b62SBarry Smith PetscFunctionBegin; 603080666b62SBarry Smith for (i=0; i<ts->numbermonitors; i++) { 603180666b62SBarry Smith if (ts->monitor[i] == TSMonitorLGSolution) { 60325537e223SBarry Smith ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr); 603380666b62SBarry Smith } 603480666b62SBarry Smith } 603580666b62SBarry Smith PetscFunctionReturn(0); 603680666b62SBarry Smith } 603780666b62SBarry Smith 603880666b62SBarry Smith #undef __FUNCT__ 6039e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetTransform" 6040e673d494SBarry Smith /*@C 6041e673d494SBarry Smith TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed 6042e673d494SBarry Smith 6043e673d494SBarry Smith Collective on TSLGCtx 6044e673d494SBarry Smith 6045e673d494SBarry Smith Input Parameters: 6046e673d494SBarry Smith + ts - the TS context 6047e673d494SBarry Smith . transform - the transform function 60487684fa3eSBarry Smith . destroy - function to destroy the optional context 6049e673d494SBarry Smith - ctx - optional context used by transform function 6050e673d494SBarry Smith 6051e673d494SBarry Smith Level: intermediate 6052e673d494SBarry Smith 6053e673d494SBarry Smith .keywords: TS, vector, monitor, view 6054e673d494SBarry Smith 6055a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform() 6056e673d494SBarry Smith @*/ 60577684fa3eSBarry Smith PetscErrorCode TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx) 6058e673d494SBarry Smith { 6059e673d494SBarry Smith PetscFunctionBegin; 6060e673d494SBarry Smith ctx->transform = transform; 60617684fa3eSBarry Smith ctx->transformdestroy = destroy; 6062e673d494SBarry Smith ctx->transformctx = tctx; 6063e673d494SBarry Smith PetscFunctionReturn(0); 6064e673d494SBarry Smith } 6065e673d494SBarry Smith 6066b3603a34SBarry Smith #undef __FUNCT__ 60674f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError" 6068ef20d060SBarry Smith /*@C 60694f09c107SBarry Smith TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector 6070ef20d060SBarry Smith in a time based line graph 6071ef20d060SBarry Smith 6072ef20d060SBarry Smith Collective on TS 6073ef20d060SBarry Smith 6074ef20d060SBarry Smith Input Parameters: 6075ef20d060SBarry Smith + ts - the TS context 6076ef20d060SBarry Smith . step - current time-step 6077ef20d060SBarry Smith . ptime - current time 6078ef20d060SBarry Smith - lg - a line graph object 6079ef20d060SBarry Smith 6080ef20d060SBarry Smith Level: intermediate 6081ef20d060SBarry Smith 6082abd5a294SJed Brown Notes: 6083abd5a294SJed Brown Only for sequential solves. 6084abd5a294SJed Brown 6085abd5a294SJed Brown The user must provide the solution using TSSetSolutionFunction() to use this monitor. 6086abd5a294SJed Brown 6087abd5a294SJed Brown Options Database Keys: 60884f09c107SBarry Smith . -ts_monitor_lg_error - create a graphical monitor of error history 6089ef20d060SBarry Smith 6090ef20d060SBarry Smith .keywords: TS, vector, monitor, view 6091ef20d060SBarry Smith 6092abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction() 6093ef20d060SBarry Smith @*/ 60940910c330SBarry Smith PetscErrorCode TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 6095ef20d060SBarry Smith { 6096ef20d060SBarry Smith PetscErrorCode ierr; 60970b039ecaSBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx)dummy; 6098ef20d060SBarry Smith const PetscScalar *yy; 6099ef20d060SBarry Smith Vec y; 6100a9f9c1f6SBarry Smith PetscInt dim; 6101ef20d060SBarry Smith 6102ef20d060SBarry Smith PetscFunctionBegin; 6103a9f9c1f6SBarry Smith if (!step) { 6104a9f9c1f6SBarry Smith PetscDrawAxis axis; 6105a9f9c1f6SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 61061ae185eaSBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr); 61070910c330SBarry Smith ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr); 6108a9f9c1f6SBarry Smith ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr); 6109a9f9c1f6SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6110a9f9c1f6SBarry Smith } 61110910c330SBarry Smith ierr = VecDuplicate(u,&y);CHKERRQ(ierr); 6112ef20d060SBarry Smith ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr); 61130910c330SBarry Smith ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr); 6114ef20d060SBarry Smith ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr); 6115e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX) 6116e3efe391SJed Brown { 6117e3efe391SJed Brown PetscReal *yreal; 6118e3efe391SJed Brown PetscInt i,n; 6119e3efe391SJed Brown ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr); 6120785e854fSJed Brown ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr); 6121e3efe391SJed Brown for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]); 61220b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr); 6123e3efe391SJed Brown ierr = PetscFree(yreal);CHKERRQ(ierr); 6124e3efe391SJed Brown } 6125e3efe391SJed Brown #else 61260b039ecaSBarry Smith ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr); 6127e3efe391SJed Brown #endif 6128ef20d060SBarry Smith ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr); 6129ef20d060SBarry Smith ierr = VecDestroy(&y);CHKERRQ(ierr); 6130b06615a5SLisandro Dalcin if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) { 61310b039ecaSBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 61323923b477SBarry Smith } 6133ef20d060SBarry Smith PetscFunctionReturn(0); 6134ef20d060SBarry Smith } 6135ef20d060SBarry Smith 6136201da799SBarry Smith #undef __FUNCT__ 6137201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations" 6138201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6139201da799SBarry Smith { 6140201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6141201da799SBarry Smith PetscReal x = ptime,y; 6142201da799SBarry Smith PetscErrorCode ierr; 6143201da799SBarry Smith PetscInt its; 6144201da799SBarry Smith 6145201da799SBarry Smith PetscFunctionBegin; 6146201da799SBarry Smith if (!n) { 6147201da799SBarry Smith PetscDrawAxis axis; 6148bbd56ea5SKarl Rupp 6149201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6150201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr); 6151201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6152bbd56ea5SKarl Rupp 6153201da799SBarry Smith ctx->snes_its = 0; 6154201da799SBarry Smith } 6155201da799SBarry Smith ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr); 6156201da799SBarry Smith y = its - ctx->snes_its; 6157201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 61583fbbecb0SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6159201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 6160201da799SBarry Smith } 6161201da799SBarry Smith ctx->snes_its = its; 6162201da799SBarry Smith PetscFunctionReturn(0); 6163201da799SBarry Smith } 6164201da799SBarry Smith 6165201da799SBarry Smith #undef __FUNCT__ 6166201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations" 6167201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx) 6168201da799SBarry Smith { 6169201da799SBarry Smith TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx; 6170201da799SBarry Smith PetscReal x = ptime,y; 6171201da799SBarry Smith PetscErrorCode ierr; 6172201da799SBarry Smith PetscInt its; 6173201da799SBarry Smith 6174201da799SBarry Smith PetscFunctionBegin; 6175201da799SBarry Smith if (!n) { 6176201da799SBarry Smith PetscDrawAxis axis; 6177bbd56ea5SKarl Rupp 6178201da799SBarry Smith ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr); 6179201da799SBarry Smith ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr); 6180201da799SBarry Smith ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr); 6181bbd56ea5SKarl Rupp 6182201da799SBarry Smith ctx->ksp_its = 0; 6183201da799SBarry Smith } 6184201da799SBarry Smith ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr); 6185201da799SBarry Smith y = its - ctx->ksp_its; 6186201da799SBarry Smith ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr); 618799fdda47SBarry Smith if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) { 6188201da799SBarry Smith ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr); 6189201da799SBarry Smith } 6190201da799SBarry Smith ctx->ksp_its = its; 6191201da799SBarry Smith PetscFunctionReturn(0); 6192201da799SBarry Smith } 6193f9c1d6abSBarry Smith 6194f9c1d6abSBarry Smith #undef __FUNCT__ 6195f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability" 6196f9c1d6abSBarry Smith /*@ 6197f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 6198f9c1d6abSBarry Smith 6199f9c1d6abSBarry Smith Collective on TS and Vec 6200f9c1d6abSBarry Smith 6201f9c1d6abSBarry Smith Input Parameters: 6202f9c1d6abSBarry Smith + ts - the TS context 6203f9c1d6abSBarry Smith - xr,xi - real and imaginary part of input arguments 6204f9c1d6abSBarry Smith 6205f9c1d6abSBarry Smith Output Parameters: 6206f9c1d6abSBarry Smith . yr,yi - real and imaginary part of function value 6207f9c1d6abSBarry Smith 6208f9c1d6abSBarry Smith Level: developer 6209f9c1d6abSBarry Smith 6210f9c1d6abSBarry Smith .keywords: TS, compute 6211f9c1d6abSBarry Smith 6212f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction() 6213f9c1d6abSBarry Smith @*/ 6214f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi) 6215f9c1d6abSBarry Smith { 6216f9c1d6abSBarry Smith PetscErrorCode ierr; 6217f9c1d6abSBarry Smith 6218f9c1d6abSBarry Smith PetscFunctionBegin; 6219f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts,TS_CLASSID,1); 6220ce94432eSBarry Smith if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method"); 6221f9c1d6abSBarry Smith ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr); 6222f9c1d6abSBarry Smith PetscFunctionReturn(0); 6223f9c1d6abSBarry Smith } 622424655328SShri 6225b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/ 6226b3d3934dSBarry Smith #undef __FUNCT__ 6227b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxCreate" 6228b3d3934dSBarry Smith /*@C 6229b3d3934dSBarry Smith TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope() 6230b3d3934dSBarry Smith 6231b3d3934dSBarry Smith Collective on TS 6232b3d3934dSBarry Smith 6233b3d3934dSBarry Smith Input Parameters: 6234b3d3934dSBarry Smith . ts - the ODE solver object 6235b3d3934dSBarry Smith 6236b3d3934dSBarry Smith Output Parameter: 6237b3d3934dSBarry Smith . ctx - the context 6238b3d3934dSBarry Smith 6239b3d3934dSBarry Smith Level: intermediate 6240b3d3934dSBarry Smith 6241b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso 6242b3d3934dSBarry Smith 6243b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError() 6244b3d3934dSBarry Smith 6245b3d3934dSBarry Smith @*/ 6246b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx) 6247b3d3934dSBarry Smith { 6248b3d3934dSBarry Smith PetscErrorCode ierr; 6249b3d3934dSBarry Smith 6250b3d3934dSBarry Smith PetscFunctionBegin; 6251a74656a8SBarry Smith ierr = PetscNew(ctx);CHKERRQ(ierr); 6252b3d3934dSBarry Smith PetscFunctionReturn(0); 6253b3d3934dSBarry Smith } 6254b3d3934dSBarry Smith 6255b3d3934dSBarry Smith #undef __FUNCT__ 6256b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelope" 6257b3d3934dSBarry Smith /*@C 6258b3d3934dSBarry Smith TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution 6259b3d3934dSBarry Smith 6260b3d3934dSBarry Smith Collective on TS 6261b3d3934dSBarry Smith 6262b3d3934dSBarry Smith Input Parameters: 6263b3d3934dSBarry Smith + ts - the TS context 6264b3d3934dSBarry Smith . step - current time-step 6265b3d3934dSBarry Smith . ptime - current time 6266b3d3934dSBarry Smith - ctx - the envelope context 6267b3d3934dSBarry Smith 6268b3d3934dSBarry Smith Options Database: 6269b3d3934dSBarry Smith . -ts_monitor_envelope 6270b3d3934dSBarry Smith 6271b3d3934dSBarry Smith Level: intermediate 6272b3d3934dSBarry Smith 6273b3d3934dSBarry Smith Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope 6274b3d3934dSBarry Smith 6275b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6276b3d3934dSBarry Smith 6277b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds() 6278b3d3934dSBarry Smith @*/ 6279b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy) 6280b3d3934dSBarry Smith { 6281b3d3934dSBarry Smith PetscErrorCode ierr; 6282b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dummy; 6283b3d3934dSBarry Smith 6284b3d3934dSBarry Smith PetscFunctionBegin; 6285b3d3934dSBarry Smith if (!ctx->max) { 6286b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr); 6287b3d3934dSBarry Smith ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr); 6288b3d3934dSBarry Smith ierr = VecCopy(u,ctx->max);CHKERRQ(ierr); 6289b3d3934dSBarry Smith ierr = VecCopy(u,ctx->min);CHKERRQ(ierr); 6290b3d3934dSBarry Smith } else { 6291b3d3934dSBarry Smith ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr); 6292b3d3934dSBarry Smith ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr); 6293b3d3934dSBarry Smith } 6294b3d3934dSBarry Smith PetscFunctionReturn(0); 6295b3d3934dSBarry Smith } 6296b3d3934dSBarry Smith 6297b3d3934dSBarry Smith 6298b3d3934dSBarry Smith #undef __FUNCT__ 6299b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeGetBounds" 6300b3d3934dSBarry Smith /*@C 6301b3d3934dSBarry Smith TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution 6302b3d3934dSBarry Smith 6303b3d3934dSBarry Smith Collective on TS 6304b3d3934dSBarry Smith 6305b3d3934dSBarry Smith Input Parameter: 6306b3d3934dSBarry Smith . ts - the TS context 6307b3d3934dSBarry Smith 6308b3d3934dSBarry Smith Output Parameter: 6309b3d3934dSBarry Smith + max - the maximum values 6310b3d3934dSBarry Smith - min - the minimum values 6311b3d3934dSBarry Smith 6312b3d3934dSBarry Smith Level: intermediate 6313b3d3934dSBarry Smith 6314b3d3934dSBarry Smith .keywords: TS, vector, monitor, view 6315b3d3934dSBarry Smith 6316b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables() 6317b3d3934dSBarry Smith @*/ 6318b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min) 6319b3d3934dSBarry Smith { 6320b3d3934dSBarry Smith PetscInt i; 6321b3d3934dSBarry Smith 6322b3d3934dSBarry Smith PetscFunctionBegin; 6323b3d3934dSBarry Smith if (max) *max = NULL; 6324b3d3934dSBarry Smith if (min) *min = NULL; 6325b3d3934dSBarry Smith for (i=0; i<ts->numbermonitors; i++) { 6326b3d3934dSBarry Smith if (ts->monitor[i] == TSMonitorEnvelope) { 63275537e223SBarry Smith TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i]; 6328b3d3934dSBarry Smith if (max) *max = ctx->max; 6329b3d3934dSBarry Smith if (min) *min = ctx->min; 6330b3d3934dSBarry Smith break; 6331b3d3934dSBarry Smith } 6332b3d3934dSBarry Smith } 6333b3d3934dSBarry Smith PetscFunctionReturn(0); 6334b3d3934dSBarry Smith } 6335b3d3934dSBarry Smith 6336b3d3934dSBarry Smith #undef __FUNCT__ 6337b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxDestroy" 6338b3d3934dSBarry Smith /*@C 6339b3d3934dSBarry Smith TSMonitorEnvelopeCtxDestroy - Destroys a context that was created with TSMonitorEnvelopeCtxCreate(). 6340b3d3934dSBarry Smith 6341b3d3934dSBarry Smith Collective on TSMonitorEnvelopeCtx 6342b3d3934dSBarry Smith 6343b3d3934dSBarry Smith Input Parameter: 6344b3d3934dSBarry Smith . ctx - the monitor context 6345b3d3934dSBarry Smith 6346b3d3934dSBarry Smith Level: intermediate 6347b3d3934dSBarry Smith 6348b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy 6349b3d3934dSBarry Smith 6350b3d3934dSBarry Smith .seealso: TSMonitorLGCtxCreate(), TSMonitorSet(), TSMonitorLGTimeStep(); 6351b3d3934dSBarry Smith @*/ 6352b3d3934dSBarry Smith PetscErrorCode TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx) 6353b3d3934dSBarry Smith { 6354b3d3934dSBarry Smith PetscErrorCode ierr; 6355b3d3934dSBarry Smith 6356b3d3934dSBarry Smith PetscFunctionBegin; 6357b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr); 6358b3d3934dSBarry Smith ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr); 6359b3d3934dSBarry Smith ierr = PetscFree(*ctx);CHKERRQ(ierr); 6360b3d3934dSBarry Smith PetscFunctionReturn(0); 6361b3d3934dSBarry Smith } 6362f2dee214SBarry Smith 636324655328SShri #undef __FUNCT__ 636424655328SShri #define __FUNCT__ "TSRollBack" 636524655328SShri /*@ 636624655328SShri TSRollBack - Rolls back one time step 636724655328SShri 636824655328SShri Collective on TS 636924655328SShri 637024655328SShri Input Parameter: 637124655328SShri . ts - the TS context obtained from TSCreate() 637224655328SShri 637324655328SShri Level: advanced 637424655328SShri 637524655328SShri .keywords: TS, timestep, rollback 637624655328SShri 637724655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate() 637824655328SShri @*/ 637924655328SShri PetscErrorCode TSRollBack(TS ts) 638024655328SShri { 638124655328SShri PetscErrorCode ierr; 638224655328SShri 638324655328SShri PetscFunctionBegin; 638424655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID,1); 638524655328SShri 638624655328SShri if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name); 638724655328SShri ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr); 638824655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 638924655328SShri ts->ptime = ts->ptime_prev; 63908392e04aSShri Abhyankar ts->steprollback = PETSC_TRUE; /* Flag to indicate that the step is rollbacked */ 639124655328SShri PetscFunctionReturn(0); 639224655328SShri } 6393aeb4809dSShri Abhyankar 6394ff22ae23SHong Zhang #undef __FUNCT__ 6395ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages" 6396ff22ae23SHong Zhang /*@ 6397ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 6398ff22ae23SHong Zhang 6399ff22ae23SHong Zhang Input Parameter: 6400ff22ae23SHong Zhang . ts - the TS context obtained from TSCreate() 6401ff22ae23SHong Zhang 6402ff22ae23SHong Zhang Level: advanced 6403ff22ae23SHong Zhang 6404ff22ae23SHong Zhang .keywords: TS, getstages 6405ff22ae23SHong Zhang 6406ff22ae23SHong Zhang .seealso: TSCreate() 6407ff22ae23SHong Zhang @*/ 6408ff22ae23SHong Zhang PetscErrorCode TSGetStages(TS ts,PetscInt *ns, Vec **Y) 6409ff22ae23SHong Zhang { 6410ff22ae23SHong Zhang PetscErrorCode ierr; 6411ff22ae23SHong Zhang 6412ff22ae23SHong Zhang PetscFunctionBegin; 6413ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID,1); 6414ff22ae23SHong Zhang PetscValidPointer(ns,2); 6415ff22ae23SHong Zhang 6416ff22ae23SHong Zhang if (!ts->ops->getstages) *ns=0; 6417ff22ae23SHong Zhang else { 6418ff22ae23SHong Zhang ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr); 6419ff22ae23SHong Zhang } 6420ff22ae23SHong Zhang PetscFunctionReturn(0); 6421ff22ae23SHong Zhang } 6422ff22ae23SHong Zhang 6423847ff0e1SMatthew G. Knepley #undef __FUNCT__ 6424847ff0e1SMatthew G. Knepley #define __FUNCT__ "TSComputeIJacobianDefaultColor" 6425847ff0e1SMatthew G. Knepley /*@C 6426847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 6427847ff0e1SMatthew G. Knepley 6428847ff0e1SMatthew G. Knepley Collective on SNES 6429847ff0e1SMatthew G. Knepley 6430847ff0e1SMatthew G. Knepley Input Parameters: 6431847ff0e1SMatthew G. Knepley + ts - the TS context 6432847ff0e1SMatthew G. Knepley . t - current timestep 6433847ff0e1SMatthew G. Knepley . U - state vector 6434847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 6435847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 6436847ff0e1SMatthew G. Knepley - ctx - an optional user context 6437847ff0e1SMatthew G. Knepley 6438847ff0e1SMatthew G. Knepley Output Parameters: 6439847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 6440847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J) 6441847ff0e1SMatthew G. Knepley 6442847ff0e1SMatthew G. Knepley Level: intermediate 6443847ff0e1SMatthew G. Knepley 6444847ff0e1SMatthew G. Knepley Notes: 6445847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 6446847ff0e1SMatthew G. Knepley 6447847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 6448847ff0e1SMatthew G. Knepley 6449847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 6450847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 6451847ff0e1SMatthew G. Knepley 6452847ff0e1SMatthew G. Knepley This will first try to get the coloring from the DM. If the DM type has no coloring 6453847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 6454847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 6455847ff0e1SMatthew G. Knepley 6456847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse 6457847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction() 6458847ff0e1SMatthew G. Knepley @*/ 6459847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx) 6460847ff0e1SMatthew G. Knepley { 6461847ff0e1SMatthew G. Knepley SNES snes; 6462847ff0e1SMatthew G. Knepley MatFDColoring color; 6463847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 6464847ff0e1SMatthew G. Knepley PetscErrorCode ierr; 6465847ff0e1SMatthew G. Knepley 6466847ff0e1SMatthew G. Knepley PetscFunctionBegin; 6467847ff0e1SMatthew G. Knepley ierr = PetscOptionsGetBool(((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr); 6468847ff0e1SMatthew G. Knepley ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr); 6469847ff0e1SMatthew G. Knepley if (!color) { 6470847ff0e1SMatthew G. Knepley DM dm; 6471847ff0e1SMatthew G. Knepley ISColoring iscoloring; 6472847ff0e1SMatthew G. Knepley 6473847ff0e1SMatthew G. Knepley ierr = TSGetDM(ts, &dm);CHKERRQ(ierr); 6474847ff0e1SMatthew G. Knepley ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr); 6475847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 6476847ff0e1SMatthew G. Knepley ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr); 6477847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 6478847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 6479847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 6480847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 6481847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 6482847ff0e1SMatthew G. Knepley } else { 6483847ff0e1SMatthew G. Knepley MatColoring mc; 6484847ff0e1SMatthew G. Knepley 6485847ff0e1SMatthew G. Knepley ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr); 6486847ff0e1SMatthew G. Knepley ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr); 6487847ff0e1SMatthew G. Knepley ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr); 6488847ff0e1SMatthew G. Knepley ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr); 6489847ff0e1SMatthew G. Knepley ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr); 6490847ff0e1SMatthew G. Knepley ierr = MatColoringDestroy(&mc);CHKERRQ(ierr); 6491847ff0e1SMatthew G. Knepley ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr); 6492847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr); 6493847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr); 6494847ff0e1SMatthew G. Knepley ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr); 6495847ff0e1SMatthew G. Knepley ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 6496847ff0e1SMatthew G. Knepley } 6497847ff0e1SMatthew G. Knepley ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr); 6498847ff0e1SMatthew G. Knepley ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr); 6499847ff0e1SMatthew G. Knepley } 6500847ff0e1SMatthew G. Knepley ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr); 6501847ff0e1SMatthew G. Knepley ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr); 6502847ff0e1SMatthew G. Knepley if (J != B) { 6503847ff0e1SMatthew G. Knepley ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 6504847ff0e1SMatthew G. Knepley ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 6505847ff0e1SMatthew G. Knepley } 6506847ff0e1SMatthew G. Knepley PetscFunctionReturn(0); 6507847ff0e1SMatthew G. Knepley } 650893b34091SDebojyoti Ghosh 650993b34091SDebojyoti Ghosh #undef __FUNCT__ 6510e5168f73SEmil Constantinescu #define __FUNCT__ "TSClone" 651193b34091SDebojyoti Ghosh /*@C 6512e5168f73SEmil Constantinescu TSClone - This function clones a time step object. 651393b34091SDebojyoti Ghosh 651493b34091SDebojyoti Ghosh Collective on MPI_Comm 651593b34091SDebojyoti Ghosh 651693b34091SDebojyoti Ghosh Input Parameter: 651793b34091SDebojyoti Ghosh . tsin - The input TS 651893b34091SDebojyoti Ghosh 651993b34091SDebojyoti Ghosh Output Parameter: 6520e5168f73SEmil Constantinescu . tsout - The output TS (cloned) 652193b34091SDebojyoti Ghosh 65225eca1a21SEmil Constantinescu Notes: 65235eca1a21SEmil 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. 65245eca1a21SEmil Constantinescu 6525baa10174SEmil Constantinescu 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); ierr = TSSetSNES(ts,snes_dup); 65265eca1a21SEmil Constantinescu 65275eca1a21SEmil Constantinescu Level: developer 652893b34091SDebojyoti Ghosh 6529e5168f73SEmil Constantinescu .keywords: TS, clone 6530e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType() 653193b34091SDebojyoti Ghosh @*/ 6532baa10174SEmil Constantinescu PetscErrorCode TSClone(TS tsin, TS *tsout) 653393b34091SDebojyoti Ghosh { 653493b34091SDebojyoti Ghosh TS t; 653593b34091SDebojyoti Ghosh PetscErrorCode ierr; 6536dc846ba4SSatish Balay SNES snes_start; 6537dc846ba4SSatish Balay DM dm; 6538dc846ba4SSatish Balay TSType type; 653993b34091SDebojyoti Ghosh 654093b34091SDebojyoti Ghosh PetscFunctionBegin; 654193b34091SDebojyoti Ghosh PetscValidPointer(tsin,1); 654293b34091SDebojyoti Ghosh *tsout = NULL; 654393b34091SDebojyoti Ghosh 65447a37829fSSatish Balay ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr); 654593b34091SDebojyoti Ghosh 654693b34091SDebojyoti Ghosh /* General TS description */ 654793b34091SDebojyoti Ghosh t->numbermonitors = 0; 654893b34091SDebojyoti Ghosh t->setupcalled = 0; 654993b34091SDebojyoti Ghosh t->ksp_its = 0; 655093b34091SDebojyoti Ghosh t->snes_its = 0; 655193b34091SDebojyoti Ghosh t->nwork = 0; 655293b34091SDebojyoti Ghosh t->rhsjacobian.time = -1e20; 655393b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 655493b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 655593b34091SDebojyoti Ghosh 655634561852SEmil Constantinescu ierr = TSGetSNES(tsin,&snes_start); CHKERRQ(ierr); 655734561852SEmil Constantinescu ierr = TSSetSNES(t,snes_start); CHKERRQ(ierr); 6558d15a3a53SEmil Constantinescu 655993b34091SDebojyoti Ghosh ierr = TSGetDM(tsin,&dm); CHKERRQ(ierr); 656093b34091SDebojyoti Ghosh ierr = TSSetDM(t,dm); CHKERRQ(ierr); 656193b34091SDebojyoti Ghosh 656293b34091SDebojyoti Ghosh t->adapt=tsin->adapt; 656393b34091SDebojyoti Ghosh PetscObjectReference((PetscObject)t->adapt); 656493b34091SDebojyoti Ghosh 656593b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 656693b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 656793b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 656893b34091SDebojyoti Ghosh t->time_step_orig = tsin->time_step_orig; 656993b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 657093b34091SDebojyoti Ghosh t->steps = tsin->steps; 657193b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 657293b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 657393b34091SDebojyoti Ghosh t->atol = tsin->atol; 657493b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 657593b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 657693b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 657793b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 657893b34091SDebojyoti Ghosh 657993b34091SDebojyoti Ghosh ierr = TSGetType(tsin,&type); CHKERRQ(ierr); 658093b34091SDebojyoti Ghosh ierr = TSSetType(t,type); CHKERRQ(ierr); 658193b34091SDebojyoti Ghosh 658293b34091SDebojyoti Ghosh t->vec_sol = NULL; 658393b34091SDebojyoti Ghosh 658493b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 658593b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 658693b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 658793b34091SDebojyoti Ghosh 658893b34091SDebojyoti Ghosh ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr); 658993b34091SDebojyoti Ghosh 659093b34091SDebojyoti Ghosh *tsout = t; 659193b34091SDebojyoti Ghosh PetscFunctionReturn(0); 659293b34091SDebojyoti Ghosh } 6593