xref: /petsc/src/ts/interface/ts.c (revision ec8db0ba6e2fa5334be5e24c1ae2a051b9ce6ed9)
163dd3a1aSKris Buschelman 
2b45d2f2cSJed Brown #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 
144a2ae208SSatish Balay #undef __FUNCT__
15e55864a3SBarry Smith #define __FUNCT__ "TSSetTypeFromOptions_Private"
16bdad233fSMatthew Knepley /*
17bdad233fSMatthew Knepley   TSSetTypeFromOptions - Sets the type of ts from user options.
18bdad233fSMatthew Knepley 
19bdad233fSMatthew Knepley   Collective on TS
20bdad233fSMatthew Knepley 
21bdad233fSMatthew Knepley   Input Parameter:
22bdad233fSMatthew Knepley . ts - The ts
23bdad233fSMatthew Knepley 
24bdad233fSMatthew Knepley   Level: intermediate
25bdad233fSMatthew Knepley 
26bdad233fSMatthew Knepley .keywords: TS, set, options, database, type
27bdad233fSMatthew Knepley .seealso: TSSetFromOptions(), TSSetType()
28bdad233fSMatthew Knepley */
298c34d3f5SBarry Smith static PetscErrorCode TSSetTypeFromOptions_Private(PetscOptions *PetscOptionsObject,TS ts)
30bdad233fSMatthew Knepley {
31ace3abfcSBarry Smith   PetscBool      opt;
322fc52814SBarry Smith   const char     *defaultType;
33bdad233fSMatthew Knepley   char           typeName[256];
34dfbe8321SBarry Smith   PetscErrorCode ierr;
35bdad233fSMatthew Knepley 
36bdad233fSMatthew Knepley   PetscFunctionBegin;
37bbd56ea5SKarl Rupp   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
38bbd56ea5SKarl Rupp   else defaultType = TSEULER;
39bdad233fSMatthew Knepley 
400f51fdf8SToby Isaac   ierr = TSRegisterAll();CHKERRQ(ierr);
41a264d7a6SBarry Smith   ierr = PetscOptionsFList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
42a7cc72afSBarry Smith   if (opt) {
43bdad233fSMatthew Knepley     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
44bdad233fSMatthew Knepley   } else {
45bdad233fSMatthew Knepley     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
46bdad233fSMatthew Knepley   }
47bdad233fSMatthew Knepley   PetscFunctionReturn(0);
48bdad233fSMatthew Knepley }
49bdad233fSMatthew Knepley 
502d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx {
512d5ee99bSBarry Smith   PetscViewer   viewer;
524b363babSBarry Smith   PetscDrawAxis axis;
532d5ee99bSBarry Smith   Vec           initialsolution;
542d5ee99bSBarry Smith   PetscBool     showinitial;
552d5ee99bSBarry Smith   PetscInt      howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
562d5ee99bSBarry Smith   PetscBool     showtimestepandtime;
572d5ee99bSBarry Smith   int           color;
582d5ee99bSBarry Smith };
592d5ee99bSBarry Smith 
60bdad233fSMatthew Knepley #undef __FUNCT__
61bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions"
62bdad233fSMatthew Knepley /*@
63bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
64bdad233fSMatthew Knepley 
65bdad233fSMatthew Knepley    Collective on TS
66bdad233fSMatthew Knepley 
67bdad233fSMatthew Knepley    Input Parameter:
68bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
69bdad233fSMatthew Knepley 
70bdad233fSMatthew Knepley    Options Database Keys:
714d91e141SJed Brown +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP
72d2daff3dSHong Zhang .  -ts_checkpoint - checkpoint for adjoint sensitivity analysis
73bdad233fSMatthew Knepley .  -ts_max_steps maxsteps - maximum number of time-steps to take
743bca7d26SBarry Smith .  -ts_final_time time - maximum time to compute to
75bdad233fSMatthew Knepley .  -ts_dt dt - initial time step
76a3cdaa26SBarry 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
77a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
78a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
79a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
80a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
81a3cdaa26SBarry Smith .  -ts_atol <atol> Absolute tolerance for local truncation error
82bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
83de06c3feSJed Brown .  -ts_monitor_lg_timestep - Monitor timestep size graphically
84de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
85de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
86de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
87de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
88de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
89de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
902d5ee99bSBarry Smith .  -ts_monitor_draw_solution_phase - Monitor solution graphically with phase diagram
91de06c3feSJed Brown .  -ts_monitor_draw_error - Monitor error graphically
9291b97e58SBarry Smith .  -ts_monitor_solution_binary <filename> - Save each solution to a binary file
9391b97e58SBarry Smith -  -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
9491b97e58SBarry Smith 
9591b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
96bdad233fSMatthew Knepley 
97bdad233fSMatthew Knepley    Level: beginner
98bdad233fSMatthew Knepley 
99bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
100bdad233fSMatthew Knepley 
101a313700dSBarry Smith .seealso: TSGetType()
102bdad233fSMatthew Knepley @*/
1037087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
104bdad233fSMatthew Knepley {
105bc952696SBarry Smith   PetscBool              opt,flg,tflg;
106dfbe8321SBarry Smith   PetscErrorCode         ierr;
107649052a6SBarry Smith   PetscViewer            monviewer;
108eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
109089b2837SJed Brown   SNES                   snes;
1101c3436cfSJed Brown   TSAdapt                adapt;
11131748224SBarry Smith   PetscReal              time_step;
11249354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
113d1212d36SBarry Smith   char                   dir[16];
114bdad233fSMatthew Knepley 
115bdad233fSMatthew Knepley   PetscFunctionBegin;
1160700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1173194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
118a43b19c4SJed Brown   /* Handle TS type options */
119e55864a3SBarry Smith   ierr = TSSetTypeFromOptions_Private(PetscOptionsObject,ts);CHKERRQ(ierr);
120bdad233fSMatthew Knepley 
121bdad233fSMatthew Knepley   /* Handle generic TS options */
122844594baSBarry Smith   if (ts->trajectory) tflg = PETSC_TRUE;
123844594baSBarry Smith   else tflg = PETSC_FALSE;
124bc952696SBarry Smith   ierr = PetscOptionsBool("-ts_save_trajectories","Checkpoint for adjoint sensitivity analysis","TSSetSaveTrajectories",tflg,&tflg,NULL);CHKERRQ(ierr);
125bc952696SBarry Smith   if (tflg) {ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);}
1260298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1270298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
1280298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
12931748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
13031748224SBarry Smith   if (flg) {
13131748224SBarry Smith     ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
13231748224SBarry Smith   }
13349354f04SShri 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);
13449354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1350298fd71SBarry 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);
1360298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1370298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1380298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1390298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
140bdad233fSMatthew Knepley 
14156f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
14256f85f32SBarry Smith   {
14356f85f32SBarry Smith   PetscBool set;
14456f85f32SBarry Smith   flg  = PETSC_FALSE;
14556f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
14656f85f32SBarry Smith   if (set) {
14756f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
14856f85f32SBarry Smith   }
14956f85f32SBarry Smith   }
15056f85f32SBarry Smith #endif
15156f85f32SBarry Smith 
152bdad233fSMatthew Knepley   /* Monitor options */
153a6570f20SBarry Smith   ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
154eabae89aSBarry Smith   if (flg) {
155ce94432eSBarry Smith     ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr);
156649052a6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
157bdad233fSMatthew Knepley   }
1585180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1595180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1605180491cSLisandro Dalcin 
1614f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
162a7cc72afSBarry Smith   if (opt) {
1630b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1643923b477SBarry Smith     PetscInt       howoften = 1;
165a80ad3e0SBarry Smith 
1660298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
167ce94432eSBarry Smith     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
1684f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
169b3603a34SBarry Smith   }
1704f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
171b3603a34SBarry Smith   if (opt) {
1720b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1733923b477SBarry Smith     PetscInt       howoften = 1;
174b3603a34SBarry Smith 
1750298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
17622d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1774f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
178bdad233fSMatthew Knepley   }
1794f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
180ef20d060SBarry Smith   if (opt) {
1810b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1823923b477SBarry Smith     PetscInt       howoften = 1;
183ef20d060SBarry Smith 
1840298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
18522d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1864f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
187ef20d060SBarry Smith   }
188201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
189201da799SBarry Smith   if (opt) {
190201da799SBarry Smith     TSMonitorLGCtx ctx;
191201da799SBarry Smith     PetscInt       howoften = 1;
192201da799SBarry Smith 
1930298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
19422d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
195201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
196201da799SBarry Smith   }
197201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
198201da799SBarry Smith   if (opt) {
199201da799SBarry Smith     TSMonitorLGCtx ctx;
200201da799SBarry Smith     PetscInt       howoften = 1;
201201da799SBarry Smith 
2020298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
20322d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
204201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
205201da799SBarry Smith   }
2068189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
2078189c53fSBarry Smith   if (opt) {
2088189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2098189c53fSBarry Smith     PetscInt          howoften = 1;
2108189c53fSBarry Smith 
2110298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
21222d28d08SBarry Smith     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
2138189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2148189c53fSBarry Smith   }
215ef20d060SBarry Smith   opt  = PETSC_FALSE;
2160dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
217a7cc72afSBarry Smith   if (opt) {
21883a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
21983a4ac43SBarry Smith     PetscInt         howoften = 1;
220a80ad3e0SBarry Smith 
2210298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
222ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
22383a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
224bdad233fSMatthew Knepley   }
225fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2262d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2272d5ee99bSBarry Smith   if (opt) {
2282d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2292d5ee99bSBarry Smith     PetscReal        bounds[4];
2302d5ee99bSBarry Smith     PetscInt         n = 4;
2312d5ee99bSBarry Smith     PetscDraw        draw;
2322d5ee99bSBarry Smith 
2332d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
2342d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
2352d5ee99bSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr);
2362d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
2372d5ee99bSBarry Smith     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
2384b363babSBarry Smith     ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr);
2394b363babSBarry Smith     ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
240f54adfc0SBarry Smith     ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
2414b363babSBarry Smith     ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr);
24207995780SPeter Brune     /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */
2432d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2442d5ee99bSBarry Smith   }
2452d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2460dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
2473a471f94SBarry Smith   if (opt) {
24883a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
24983a4ac43SBarry Smith     PetscInt         howoften = 1;
2503a471f94SBarry Smith 
2510298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
252ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
25383a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2543a471f94SBarry Smith   }
2553a471f94SBarry Smith   opt  = PETSC_FALSE;
25691b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
257fb1732b5SBarry Smith   if (flg) {
258fb1732b5SBarry Smith     PetscViewer ctx;
259fb1732b5SBarry Smith     if (monfilename[0]) {
260ce94432eSBarry Smith       ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
261c2fbc07fSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
262fb1732b5SBarry Smith     } else {
263ce94432eSBarry Smith       ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts));
2640298fd71SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr);
265fb1732b5SBarry Smith     }
266fb1732b5SBarry Smith   }
267ed81e22dSJed Brown   opt  = PETSC_FALSE;
26891b97e58SBarry 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);
269ed81e22dSJed Brown   if (flg) {
270ed81e22dSJed Brown     const char *ptr,*ptr2;
271ed81e22dSJed Brown     char       *filetemplate;
272ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
273ed81e22dSJed Brown     /* Do some cursory validation of the input. */
274ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
275ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
276ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
277ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
278ce94432eSBarry 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");
279ed81e22dSJed Brown       if (ptr2) break;
280ed81e22dSJed Brown     }
281ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
282ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
283ed81e22dSJed Brown   }
284bdad233fSMatthew Knepley 
285d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
286d1212d36SBarry Smith   if (flg) {
287d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
288d1212d36SBarry Smith     int                  ray = 0;
289d1212d36SBarry Smith     DMDADirection        ddir;
290d1212d36SBarry Smith     DM                   da;
291d1212d36SBarry Smith     PetscMPIInt          rank;
292d1212d36SBarry Smith 
293ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
294d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
295d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
296ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
297d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
298d1212d36SBarry Smith 
299d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
300b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
301d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
302d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
303ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
304d1212d36SBarry Smith     if (!rank) {
305d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
306d1212d36SBarry Smith     }
30751b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
308d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
309d1212d36SBarry Smith   }
31051b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
31151b4a12fSMatthew G. Knepley   if (flg) {
31251b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
31351b4a12fSMatthew G. Knepley     int                 ray = 0;
31451b4a12fSMatthew G. Knepley     DMDADirection       ddir;
31551b4a12fSMatthew G. Knepley     DM                  da;
31651b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
317d1212d36SBarry Smith 
31851b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
31951b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
32051b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
32151b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
32251b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
32351b4a12fSMatthew G. Knepley 
32451b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
325b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
32651b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
32751b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
32851b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
32951b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
33051b4a12fSMatthew G. Knepley   }
331a7a1495cSBarry Smith 
3327781c08eSBarry Smith   /*
3337781c08eSBarry Smith      This code is all wrong. One is creating objects inside the TSSetFromOptions() so if run with the options gui
3347781c08eSBarry Smith      will bleed memory. Also one is using a PetscOptionsBegin() inside a PetscOptionsBegin()
3357781c08eSBarry Smith   */
336552698daSJed Brown   ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
337e55864a3SBarry Smith   ierr = TSAdaptSetFromOptions(PetscOptionsObject,adapt);CHKERRQ(ierr);
338d763cef2SBarry Smith 
339d763cef2SBarry Smith   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
340d763cef2SBarry Smith   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
341d763cef2SBarry Smith 
342bc952696SBarry Smith   if (ts->trajectory) {
343bc952696SBarry Smith     ierr = TSTrajectorySetFromOptions(ts->trajectory);CHKERRQ(ierr);
344bc952696SBarry Smith   }
345bc952696SBarry Smith 
346d763cef2SBarry Smith   /* Handle specific TS options */
347d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
348e55864a3SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
349d763cef2SBarry Smith   }
350d763cef2SBarry Smith 
351d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
352d763cef2SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
353d763cef2SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
354d763cef2SBarry Smith   PetscFunctionReturn(0);
355d763cef2SBarry Smith }
356d763cef2SBarry Smith 
357d763cef2SBarry Smith #undef __FUNCT__
358bc952696SBarry Smith #define __FUNCT__ "TSSetSaveTrajectory"
359d2daff3dSHong Zhang /*@
360bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
361d2daff3dSHong Zhang 
362d2daff3dSHong Zhang    Collective on TS
363d2daff3dSHong Zhang 
364d2daff3dSHong Zhang    Input Parameters:
365bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
366bc952696SBarry Smith 
367d2daff3dSHong Zhang 
368d2daff3dSHong Zhang    Level: intermediate
369d2daff3dSHong Zhang 
370bc952696SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve()
371d2daff3dSHong Zhang 
372bc952696SBarry Smith .keywords: TS, set, checkpoint,
373d2daff3dSHong Zhang @*/
374bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
375d2daff3dSHong Zhang {
376bc952696SBarry Smith   PetscErrorCode ierr;
377bc952696SBarry Smith 
378d2daff3dSHong Zhang   PetscFunctionBegin;
379d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
380bc952696SBarry Smith   if (!ts->trajectory) {
381bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
382bc952696SBarry Smith     /* should it set a default trajectory? */
383bc952696SBarry Smith   }
384d2daff3dSHong Zhang   PetscFunctionReturn(0);
385d2daff3dSHong Zhang }
386d2daff3dSHong Zhang 
387a7a1495cSBarry Smith #undef __FUNCT__
388a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian"
389a7a1495cSBarry Smith /*@
390a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
391a7a1495cSBarry Smith       set with TSSetRHSJacobian().
392a7a1495cSBarry Smith 
393a7a1495cSBarry Smith    Collective on TS and Vec
394a7a1495cSBarry Smith 
395a7a1495cSBarry Smith    Input Parameters:
396316643e7SJed Brown +  ts - the TS context
397a7a1495cSBarry Smith .  t - current timestep
3980910c330SBarry Smith -  U - input vector
399a7a1495cSBarry Smith 
400a7a1495cSBarry Smith    Output Parameters:
401a7a1495cSBarry Smith +  A - Jacobian matrix
402a7a1495cSBarry Smith .  B - optional preconditioning matrix
403a7a1495cSBarry Smith -  flag - flag indicating matrix structure
404a7a1495cSBarry Smith 
405a7a1495cSBarry Smith    Notes:
406a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
407a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
408a7a1495cSBarry Smith 
40994b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
410a7a1495cSBarry Smith    flag parameter.
411a7a1495cSBarry Smith 
412a7a1495cSBarry Smith    Level: developer
413a7a1495cSBarry Smith 
414a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
415a7a1495cSBarry Smith 
41694b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
417a7a1495cSBarry Smith @*/
418d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
419a7a1495cSBarry Smith {
420dfbe8321SBarry Smith   PetscErrorCode ierr;
421270bf2e7SJed Brown   PetscObjectState Ustate;
42224989b8cSPeter Brune   DM             dm;
423942e3340SBarry Smith   DMTS           tsdm;
42424989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
42524989b8cSPeter Brune   void           *ctx;
42624989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
427a7a1495cSBarry Smith 
428a7a1495cSBarry Smith   PetscFunctionBegin;
4290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4300910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4310910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
43224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
433942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
43424989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
4350298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
43659e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
4370910c330SBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) {
4380e4ef248SJed Brown     PetscFunctionReturn(0);
439f8ede8e7SJed Brown   }
440d90be118SSean Farley 
441ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
442d90be118SSean Farley 
443e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
44494ab13aaSBarry Smith     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
44594ab13aaSBarry Smith     ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
44694ab13aaSBarry Smith     if (A != B) {
44794ab13aaSBarry Smith       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
44894ab13aaSBarry Smith       ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
449e1244c69SJed Brown     }
450e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
451e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
452e1244c69SJed Brown   }
453e1244c69SJed Brown 
45424989b8cSPeter Brune   if (rhsjacobianfunc) {
45594ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
456a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
457d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
458a7a1495cSBarry Smith     PetscStackPop;
45994ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
460a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
46194ab13aaSBarry Smith     PetscValidHeaderSpecific(A,MAT_CLASSID,4);
46294ab13aaSBarry Smith     PetscValidHeaderSpecific(B,MAT_CLASSID,5);
463ef66eb69SBarry Smith   } else {
46494ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
46594ab13aaSBarry Smith     if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
466ef66eb69SBarry Smith   }
4670e4ef248SJed Brown   ts->rhsjacobian.time       = t;
4680910c330SBarry Smith   ts->rhsjacobian.X          = U;
46959e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
470a7a1495cSBarry Smith   PetscFunctionReturn(0);
471a7a1495cSBarry Smith }
472a7a1495cSBarry Smith 
4734a2ae208SSatish Balay #undef __FUNCT__
4744a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
475316643e7SJed Brown /*@
476d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
477d763cef2SBarry Smith 
478316643e7SJed Brown    Collective on TS and Vec
479316643e7SJed Brown 
480316643e7SJed Brown    Input Parameters:
481316643e7SJed Brown +  ts - the TS context
482316643e7SJed Brown .  t - current time
4830910c330SBarry Smith -  U - state vector
484316643e7SJed Brown 
485316643e7SJed Brown    Output Parameter:
486316643e7SJed Brown .  y - right hand side
487316643e7SJed Brown 
488316643e7SJed Brown    Note:
489316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
490316643e7SJed Brown    is used internally within the nonlinear solvers.
491316643e7SJed Brown 
492316643e7SJed Brown    Level: developer
493316643e7SJed Brown 
494316643e7SJed Brown .keywords: TS, compute
495316643e7SJed Brown 
496316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
497316643e7SJed Brown @*/
4980910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
499d763cef2SBarry Smith {
500dfbe8321SBarry Smith   PetscErrorCode ierr;
50124989b8cSPeter Brune   TSRHSFunction  rhsfunction;
50224989b8cSPeter Brune   TSIFunction    ifunction;
50324989b8cSPeter Brune   void           *ctx;
50424989b8cSPeter Brune   DM             dm;
50524989b8cSPeter Brune 
506d763cef2SBarry Smith   PetscFunctionBegin;
5070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5080910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5090700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
51024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
51124989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
5120298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
513d763cef2SBarry Smith 
514ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
515d763cef2SBarry Smith 
5160910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
51724989b8cSPeter Brune   if (rhsfunction) {
518d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
5190910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
520d763cef2SBarry Smith     PetscStackPop;
521214bc6a2SJed Brown   } else {
522214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
523b2cd27e8SJed Brown   }
52444a41b28SSean Farley 
5250910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
526d763cef2SBarry Smith   PetscFunctionReturn(0);
527d763cef2SBarry Smith }
528d763cef2SBarry Smith 
5294a2ae208SSatish Balay #undef __FUNCT__
530ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
531ef20d060SBarry Smith /*@
532ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
533ef20d060SBarry Smith 
534ef20d060SBarry Smith    Collective on TS and Vec
535ef20d060SBarry Smith 
536ef20d060SBarry Smith    Input Parameters:
537ef20d060SBarry Smith +  ts - the TS context
538ef20d060SBarry Smith -  t - current time
539ef20d060SBarry Smith 
540ef20d060SBarry Smith    Output Parameter:
5410910c330SBarry Smith .  U - the solution
542ef20d060SBarry Smith 
543ef20d060SBarry Smith    Note:
544ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
545ef20d060SBarry Smith    is used internally within the nonlinear solvers.
546ef20d060SBarry Smith 
547ef20d060SBarry Smith    Level: developer
548ef20d060SBarry Smith 
549ef20d060SBarry Smith .keywords: TS, compute
550ef20d060SBarry Smith 
551abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
552ef20d060SBarry Smith @*/
5530910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
554ef20d060SBarry Smith {
555ef20d060SBarry Smith   PetscErrorCode     ierr;
556ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
557ef20d060SBarry Smith   void               *ctx;
558ef20d060SBarry Smith   DM                 dm;
559ef20d060SBarry Smith 
560ef20d060SBarry Smith   PetscFunctionBegin;
561ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5620910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
563ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
564ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
565ef20d060SBarry Smith 
566ef20d060SBarry Smith   if (solutionfunction) {
5679b7cd975SBarry Smith     PetscStackPush("TS user solution function");
5680910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
569ef20d060SBarry Smith     PetscStackPop;
570ef20d060SBarry Smith   }
571ef20d060SBarry Smith   PetscFunctionReturn(0);
572ef20d060SBarry Smith }
5739b7cd975SBarry Smith #undef __FUNCT__
5749b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
5759b7cd975SBarry Smith /*@
5769b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
5779b7cd975SBarry Smith 
5789b7cd975SBarry Smith    Collective on TS and Vec
5799b7cd975SBarry Smith 
5809b7cd975SBarry Smith    Input Parameters:
5819b7cd975SBarry Smith +  ts - the TS context
5829b7cd975SBarry Smith -  t - current time
5839b7cd975SBarry Smith 
5849b7cd975SBarry Smith    Output Parameter:
5859b7cd975SBarry Smith .  U - the function value
5869b7cd975SBarry Smith 
5879b7cd975SBarry Smith    Note:
5889b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
5899b7cd975SBarry Smith    is used internally within the nonlinear solvers.
5909b7cd975SBarry Smith 
5919b7cd975SBarry Smith    Level: developer
5929b7cd975SBarry Smith 
5939b7cd975SBarry Smith .keywords: TS, compute
5949b7cd975SBarry Smith 
5959b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
5969b7cd975SBarry Smith @*/
5979b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
5989b7cd975SBarry Smith {
5999b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
6009b7cd975SBarry Smith   void               *ctx;
6019b7cd975SBarry Smith   DM                 dm;
6029b7cd975SBarry Smith 
6039b7cd975SBarry Smith   PetscFunctionBegin;
6049b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6059b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6069b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
6079b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
6089b7cd975SBarry Smith 
6099b7cd975SBarry Smith   if (forcing) {
6109b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
6119b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
6129b7cd975SBarry Smith     PetscStackPop;
6139b7cd975SBarry Smith   }
6149b7cd975SBarry Smith   PetscFunctionReturn(0);
6159b7cd975SBarry Smith }
616ef20d060SBarry Smith 
617ef20d060SBarry Smith #undef __FUNCT__
618214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
619214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
620214bc6a2SJed Brown {
6212dd45cf8SJed Brown   Vec            F;
622214bc6a2SJed Brown   PetscErrorCode ierr;
623214bc6a2SJed Brown 
624214bc6a2SJed Brown   PetscFunctionBegin;
6250298fd71SBarry Smith   *Frhs = NULL;
6260298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
627214bc6a2SJed Brown   if (!ts->Frhs) {
6282dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
629214bc6a2SJed Brown   }
630214bc6a2SJed Brown   *Frhs = ts->Frhs;
631214bc6a2SJed Brown   PetscFunctionReturn(0);
632214bc6a2SJed Brown }
633214bc6a2SJed Brown 
634214bc6a2SJed Brown #undef __FUNCT__
635214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
636214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
637214bc6a2SJed Brown {
638214bc6a2SJed Brown   Mat            A,B;
6392dd45cf8SJed Brown   PetscErrorCode ierr;
640214bc6a2SJed Brown 
641214bc6a2SJed Brown   PetscFunctionBegin;
642c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
643c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
6440298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
645214bc6a2SJed Brown   if (Arhs) {
646214bc6a2SJed Brown     if (!ts->Arhs) {
647214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
648214bc6a2SJed Brown     }
649214bc6a2SJed Brown     *Arhs = ts->Arhs;
650214bc6a2SJed Brown   }
651214bc6a2SJed Brown   if (Brhs) {
652214bc6a2SJed Brown     if (!ts->Brhs) {
653bdb70873SJed Brown       if (A != B) {
654214bc6a2SJed Brown         ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
655bdb70873SJed Brown       } else {
656bdb70873SJed Brown         ts->Brhs = ts->Arhs;
657bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
658bdb70873SJed Brown       }
659214bc6a2SJed Brown     }
660214bc6a2SJed Brown     *Brhs = ts->Brhs;
661214bc6a2SJed Brown   }
662214bc6a2SJed Brown   PetscFunctionReturn(0);
663214bc6a2SJed Brown }
664214bc6a2SJed Brown 
665214bc6a2SJed Brown #undef __FUNCT__
666316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
667316643e7SJed Brown /*@
6680910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
669316643e7SJed Brown 
670316643e7SJed Brown    Collective on TS and Vec
671316643e7SJed Brown 
672316643e7SJed Brown    Input Parameters:
673316643e7SJed Brown +  ts - the TS context
674316643e7SJed Brown .  t - current time
6750910c330SBarry Smith .  U - state vector
6760910c330SBarry Smith .  Udot - time derivative of state vector
677214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
678316643e7SJed Brown 
679316643e7SJed Brown    Output Parameter:
680316643e7SJed Brown .  Y - right hand side
681316643e7SJed Brown 
682316643e7SJed Brown    Note:
683316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
684316643e7SJed Brown    is used internally within the nonlinear solvers.
685316643e7SJed Brown 
686316643e7SJed Brown    If the user did did not write their equations in implicit form, this
687316643e7SJed Brown    function recasts them in implicit form.
688316643e7SJed Brown 
689316643e7SJed Brown    Level: developer
690316643e7SJed Brown 
691316643e7SJed Brown .keywords: TS, compute
692316643e7SJed Brown 
693316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
694316643e7SJed Brown @*/
6950910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
696316643e7SJed Brown {
697316643e7SJed Brown   PetscErrorCode ierr;
69824989b8cSPeter Brune   TSIFunction    ifunction;
69924989b8cSPeter Brune   TSRHSFunction  rhsfunction;
70024989b8cSPeter Brune   void           *ctx;
70124989b8cSPeter Brune   DM             dm;
702316643e7SJed Brown 
703316643e7SJed Brown   PetscFunctionBegin;
7040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7050910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7060910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
7070700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
708316643e7SJed Brown 
70924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
71024989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
7110298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
71224989b8cSPeter Brune 
713ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
714d90be118SSean Farley 
7150910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
71624989b8cSPeter Brune   if (ifunction) {
717316643e7SJed Brown     PetscStackPush("TS user implicit function");
7180910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
719316643e7SJed Brown     PetscStackPop;
720214bc6a2SJed Brown   }
721214bc6a2SJed Brown   if (imex) {
72224989b8cSPeter Brune     if (!ifunction) {
7230910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
7242dd45cf8SJed Brown     }
72524989b8cSPeter Brune   } else if (rhsfunction) {
72624989b8cSPeter Brune     if (ifunction) {
727214bc6a2SJed Brown       Vec Frhs;
728214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
7290910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
730214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
7312dd45cf8SJed Brown     } else {
7320910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
7330910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
734316643e7SJed Brown     }
7354a6899ffSJed Brown   }
7360910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
737316643e7SJed Brown   PetscFunctionReturn(0);
738316643e7SJed Brown }
739316643e7SJed Brown 
740316643e7SJed Brown #undef __FUNCT__
741316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
742316643e7SJed Brown /*@
743316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
744316643e7SJed Brown 
745316643e7SJed Brown    Collective on TS and Vec
746316643e7SJed Brown 
747316643e7SJed Brown    Input
748316643e7SJed Brown       Input Parameters:
749316643e7SJed Brown +  ts - the TS context
750316643e7SJed Brown .  t - current timestep
7510910c330SBarry Smith .  U - state vector
7520910c330SBarry Smith .  Udot - time derivative of state vector
753214bc6a2SJed Brown .  shift - shift to apply, see note below
754214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
755316643e7SJed Brown 
756316643e7SJed Brown    Output Parameters:
757316643e7SJed Brown +  A - Jacobian matrix
758316643e7SJed Brown .  B - optional preconditioning matrix
759316643e7SJed Brown -  flag - flag indicating matrix structure
760316643e7SJed Brown 
761316643e7SJed Brown    Notes:
7620910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
763316643e7SJed Brown 
7640910c330SBarry Smith    dF/dU + shift*dF/dUdot
765316643e7SJed Brown 
766316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
767316643e7SJed Brown    is used internally within the nonlinear solvers.
768316643e7SJed Brown 
769316643e7SJed Brown    Level: developer
770316643e7SJed Brown 
771316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
772316643e7SJed Brown 
773316643e7SJed Brown .seealso:  TSSetIJacobian()
77463495f91SJed Brown @*/
775d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
776316643e7SJed Brown {
777316643e7SJed Brown   PetscErrorCode ierr;
77824989b8cSPeter Brune   TSIJacobian    ijacobian;
77924989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
78024989b8cSPeter Brune   DM             dm;
78124989b8cSPeter Brune   void           *ctx;
782316643e7SJed Brown 
783316643e7SJed Brown   PetscFunctionBegin;
7840700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7850910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7860910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
787316643e7SJed Brown   PetscValidPointer(A,6);
78894ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
789316643e7SJed Brown   PetscValidPointer(B,7);
79094ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
79124989b8cSPeter Brune 
79224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
79324989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
7940298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
79524989b8cSPeter Brune 
796ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
797316643e7SJed Brown 
79894ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
79924989b8cSPeter Brune   if (ijacobian) {
800316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
801d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
802316643e7SJed Brown     PetscStackPop;
803214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
80494ab13aaSBarry Smith     PetscValidHeaderSpecific(A,MAT_CLASSID,4);
80594ab13aaSBarry Smith     PetscValidHeaderSpecific(B,MAT_CLASSID,5);
8064a6899ffSJed Brown   }
807214bc6a2SJed Brown   if (imex) {
808b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
80994ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
81094ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
81194ab13aaSBarry Smith       if (A != B) {
81294ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
81394ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
814214bc6a2SJed Brown       }
815214bc6a2SJed Brown     }
816214bc6a2SJed Brown   } else {
817e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
818e1244c69SJed Brown     if (rhsjacobian) {
819bd373395SBarry Smith       if (ijacobian) {
820e1244c69SJed Brown         ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
821bd373395SBarry Smith       } else {
822bd373395SBarry Smith         ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr);
823e1244c69SJed Brown       }
824d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
825e1244c69SJed Brown     }
82694ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
827e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
828e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
82994ab13aaSBarry Smith       ierr = MatScale(A,-1);CHKERRQ(ierr);
83094ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
83194ab13aaSBarry Smith       if (A != B) {
83294ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
83394ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
834316643e7SJed Brown       }
835e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
836e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
837e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
83894ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
83994ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
84094ab13aaSBarry Smith         if (A != B) {
84194ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
84294ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
843e1244c69SJed Brown         }
844e1244c69SJed Brown       }
84594ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
84694ab13aaSBarry Smith       if (A != B) {
84794ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
848214bc6a2SJed Brown       }
849316643e7SJed Brown     }
850316643e7SJed Brown   }
85194ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
852316643e7SJed Brown   PetscFunctionReturn(0);
853316643e7SJed Brown }
854316643e7SJed Brown 
855316643e7SJed Brown #undef __FUNCT__
8564a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
857d763cef2SBarry Smith /*@C
858d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
859b5abc632SBarry Smith     where U_t = G(t,u).
860d763cef2SBarry Smith 
8613f9fe445SBarry Smith     Logically Collective on TS
862d763cef2SBarry Smith 
863d763cef2SBarry Smith     Input Parameters:
864d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
8650298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
866d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
867d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
8680298fd71SBarry Smith           function evaluation routine (may be NULL)
869d763cef2SBarry Smith 
870d763cef2SBarry Smith     Calling sequence of func:
87187828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
872d763cef2SBarry Smith 
873d763cef2SBarry Smith +   t - current timestep
874d763cef2SBarry Smith .   u - input vector
875d763cef2SBarry Smith .   F - function vector
876d763cef2SBarry Smith -   ctx - [optional] user-defined function context
877d763cef2SBarry Smith 
878d763cef2SBarry Smith     Level: beginner
879d763cef2SBarry Smith 
8802bbac0d3SBarry Smith     Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
8812bbac0d3SBarry Smith 
882d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
883d763cef2SBarry Smith 
884ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
885d763cef2SBarry Smith @*/
886089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
887d763cef2SBarry Smith {
888089b2837SJed Brown   PetscErrorCode ierr;
889089b2837SJed Brown   SNES           snes;
8900298fd71SBarry Smith   Vec            ralloc = NULL;
89124989b8cSPeter Brune   DM             dm;
892d763cef2SBarry Smith 
893089b2837SJed Brown   PetscFunctionBegin;
8940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
895ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
89624989b8cSPeter Brune 
89724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
89824989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
899089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
900e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
901e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
902e856ceecSJed Brown     r    = ralloc;
903e856ceecSJed Brown   }
904089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
905e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
906d763cef2SBarry Smith   PetscFunctionReturn(0);
907d763cef2SBarry Smith }
908d763cef2SBarry Smith 
9094a2ae208SSatish Balay #undef __FUNCT__
910ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
911ef20d060SBarry Smith /*@C
912abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
913ef20d060SBarry Smith 
914ef20d060SBarry Smith     Logically Collective on TS
915ef20d060SBarry Smith 
916ef20d060SBarry Smith     Input Parameters:
917ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
918ef20d060SBarry Smith .   f - routine for evaluating the solution
919ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
9200298fd71SBarry Smith           function evaluation routine (may be NULL)
921ef20d060SBarry Smith 
922ef20d060SBarry Smith     Calling sequence of func:
923ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
924ef20d060SBarry Smith 
925ef20d060SBarry Smith +   t - current timestep
926ef20d060SBarry Smith .   u - output vector
927ef20d060SBarry Smith -   ctx - [optional] user-defined function context
928ef20d060SBarry Smith 
929abd5a294SJed Brown     Notes:
930abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
931abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
932abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
933abd5a294SJed Brown 
9344f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
935abd5a294SJed Brown 
936ef20d060SBarry Smith     Level: beginner
937ef20d060SBarry Smith 
938ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
939ef20d060SBarry Smith 
9409b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
941ef20d060SBarry Smith @*/
942ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
943ef20d060SBarry Smith {
944ef20d060SBarry Smith   PetscErrorCode ierr;
945ef20d060SBarry Smith   DM             dm;
946ef20d060SBarry Smith 
947ef20d060SBarry Smith   PetscFunctionBegin;
948ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
949ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
950ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
951ef20d060SBarry Smith   PetscFunctionReturn(0);
952ef20d060SBarry Smith }
953ef20d060SBarry Smith 
954ef20d060SBarry Smith #undef __FUNCT__
9559b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
9569b7cd975SBarry Smith /*@C
9579b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
9589b7cd975SBarry Smith 
9599b7cd975SBarry Smith     Logically Collective on TS
9609b7cd975SBarry Smith 
9619b7cd975SBarry Smith     Input Parameters:
9629b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
9639b7cd975SBarry Smith .   f - routine for evaluating the forcing function
9649b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
9650298fd71SBarry Smith           function evaluation routine (may be NULL)
9669b7cd975SBarry Smith 
9679b7cd975SBarry Smith     Calling sequence of func:
9689b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
9699b7cd975SBarry Smith 
9709b7cd975SBarry Smith +   t - current timestep
9719b7cd975SBarry Smith .   u - output vector
9729b7cd975SBarry Smith -   ctx - [optional] user-defined function context
9739b7cd975SBarry Smith 
9749b7cd975SBarry Smith     Notes:
9759b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
9769b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
9779b7cd975SBarry Smith 
9789b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
9799b7cd975SBarry Smith 
9809b7cd975SBarry Smith     Level: beginner
9819b7cd975SBarry Smith 
9829b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
9839b7cd975SBarry Smith 
9849b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
9859b7cd975SBarry Smith @*/
9869b7cd975SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
9879b7cd975SBarry Smith {
9889b7cd975SBarry Smith   PetscErrorCode ierr;
9899b7cd975SBarry Smith   DM             dm;
9909b7cd975SBarry Smith 
9919b7cd975SBarry Smith   PetscFunctionBegin;
9929b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9939b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
9949b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
9959b7cd975SBarry Smith   PetscFunctionReturn(0);
9969b7cd975SBarry Smith }
9979b7cd975SBarry Smith 
9989b7cd975SBarry Smith #undef __FUNCT__
9994a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
1000d763cef2SBarry Smith /*@C
1001f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1002b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1003d763cef2SBarry Smith 
10043f9fe445SBarry Smith    Logically Collective on TS
1005d763cef2SBarry Smith 
1006d763cef2SBarry Smith    Input Parameters:
1007d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1008e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1009e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1010d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1011d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
10120298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1013d763cef2SBarry Smith 
1014f7ab8db6SBarry Smith    Calling sequence of f:
1015ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1016d763cef2SBarry Smith 
1017d763cef2SBarry Smith +  t - current timestep
1018d763cef2SBarry Smith .  u - input vector
1019e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1020e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1021d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1022d763cef2SBarry Smith 
1023d763cef2SBarry Smith 
1024d763cef2SBarry Smith    Level: beginner
1025d763cef2SBarry Smith 
1026d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1027d763cef2SBarry Smith 
1028ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1029d763cef2SBarry Smith 
1030d763cef2SBarry Smith @*/
1031e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1032d763cef2SBarry Smith {
1033277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1034089b2837SJed Brown   SNES           snes;
103524989b8cSPeter Brune   DM             dm;
103624989b8cSPeter Brune   TSIJacobian    ijacobian;
1037277b19d0SLisandro Dalcin 
1038d763cef2SBarry Smith   PetscFunctionBegin;
10390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1040e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1041e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1042e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1043e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1044d763cef2SBarry Smith 
104524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
104624989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1047e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1048e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1049e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1050e1244c69SJed Brown   }
10510298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1052089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
10535f659677SPeter Brune   if (!ijacobian) {
1054e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
10550e4ef248SJed Brown   }
1056e5d3d808SBarry Smith   if (Amat) {
1057e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
10580e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1059bbd56ea5SKarl Rupp 
1060e5d3d808SBarry Smith     ts->Arhs = Amat;
10610e4ef248SJed Brown   }
1062e5d3d808SBarry Smith   if (Pmat) {
1063e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
10640e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1065bbd56ea5SKarl Rupp 
1066e5d3d808SBarry Smith     ts->Brhs = Pmat;
10670e4ef248SJed Brown   }
1068d763cef2SBarry Smith   PetscFunctionReturn(0);
1069d763cef2SBarry Smith }
1070d763cef2SBarry Smith 
1071316643e7SJed Brown 
1072316643e7SJed Brown #undef __FUNCT__
1073316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
1074316643e7SJed Brown /*@C
1075b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1076316643e7SJed Brown 
10773f9fe445SBarry Smith    Logically Collective on TS
1078316643e7SJed Brown 
1079316643e7SJed Brown    Input Parameters:
1080316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
10810298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1082316643e7SJed Brown .  f   - the function evaluation routine
10830298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1084316643e7SJed Brown 
1085316643e7SJed Brown    Calling sequence of f:
1086316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1087316643e7SJed Brown 
1088316643e7SJed Brown +  t   - time at step/stage being solved
1089316643e7SJed Brown .  u   - state vector
1090316643e7SJed Brown .  u_t - time derivative of state vector
1091316643e7SJed Brown .  F   - function vector
1092316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1093316643e7SJed Brown 
1094316643e7SJed Brown    Important:
10952bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1096316643e7SJed Brown 
1097316643e7SJed Brown    Level: beginner
1098316643e7SJed Brown 
1099316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1100316643e7SJed Brown 
1101d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1102316643e7SJed Brown @*/
1103089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
1104316643e7SJed Brown {
1105089b2837SJed Brown   PetscErrorCode ierr;
1106089b2837SJed Brown   SNES           snes;
11070298fd71SBarry Smith   Vec            resalloc = NULL;
110824989b8cSPeter Brune   DM             dm;
1109316643e7SJed Brown 
1110316643e7SJed Brown   PetscFunctionBegin;
11110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1112ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
111324989b8cSPeter Brune 
111424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
111524989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
111624989b8cSPeter Brune 
1117089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1118e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
1119e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
1120e856ceecSJed Brown     res  = resalloc;
1121e856ceecSJed Brown   }
1122089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
1123e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
1124089b2837SJed Brown   PetscFunctionReturn(0);
1125089b2837SJed Brown }
1126089b2837SJed Brown 
1127089b2837SJed Brown #undef __FUNCT__
1128089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1129089b2837SJed Brown /*@C
1130089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1131089b2837SJed Brown 
1132089b2837SJed Brown    Not Collective
1133089b2837SJed Brown 
1134089b2837SJed Brown    Input Parameter:
1135089b2837SJed Brown .  ts - the TS context
1136089b2837SJed Brown 
1137089b2837SJed Brown    Output Parameter:
11380298fd71SBarry Smith +  r - vector to hold residual (or NULL)
11390298fd71SBarry Smith .  func - the function to compute residual (or NULL)
11400298fd71SBarry Smith -  ctx - the function context (or NULL)
1141089b2837SJed Brown 
1142089b2837SJed Brown    Level: advanced
1143089b2837SJed Brown 
1144089b2837SJed Brown .keywords: TS, nonlinear, get, function
1145089b2837SJed Brown 
1146089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1147089b2837SJed Brown @*/
1148089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1149089b2837SJed Brown {
1150089b2837SJed Brown   PetscErrorCode ierr;
1151089b2837SJed Brown   SNES           snes;
115224989b8cSPeter Brune   DM             dm;
1153089b2837SJed Brown 
1154089b2837SJed Brown   PetscFunctionBegin;
1155089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1156089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11570298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
115824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
115924989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1160089b2837SJed Brown   PetscFunctionReturn(0);
1161089b2837SJed Brown }
1162089b2837SJed Brown 
1163089b2837SJed Brown #undef __FUNCT__
1164089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1165089b2837SJed Brown /*@C
1166089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1167089b2837SJed Brown 
1168089b2837SJed Brown    Not Collective
1169089b2837SJed Brown 
1170089b2837SJed Brown    Input Parameter:
1171089b2837SJed Brown .  ts - the TS context
1172089b2837SJed Brown 
1173089b2837SJed Brown    Output Parameter:
11740298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
11750298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
11760298fd71SBarry Smith -  ctx - the function context (or NULL)
1177089b2837SJed Brown 
1178089b2837SJed Brown    Level: advanced
1179089b2837SJed Brown 
1180089b2837SJed Brown .keywords: TS, nonlinear, get, function
1181089b2837SJed Brown 
11822bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1183089b2837SJed Brown @*/
1184089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1185089b2837SJed Brown {
1186089b2837SJed Brown   PetscErrorCode ierr;
1187089b2837SJed Brown   SNES           snes;
118824989b8cSPeter Brune   DM             dm;
1189089b2837SJed Brown 
1190089b2837SJed Brown   PetscFunctionBegin;
1191089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1192089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11930298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
119424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
119524989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1196316643e7SJed Brown   PetscFunctionReturn(0);
1197316643e7SJed Brown }
1198316643e7SJed Brown 
1199316643e7SJed Brown #undef __FUNCT__
1200316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1201316643e7SJed Brown /*@C
1202a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1203ae8867d6SBarry Smith         provided with TSSetIFunction().
1204316643e7SJed Brown 
12053f9fe445SBarry Smith    Logically Collective on TS
1206316643e7SJed Brown 
1207316643e7SJed Brown    Input Parameters:
1208316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1209e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1210e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1211316643e7SJed Brown .  f   - the Jacobian evaluation routine
12120298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1213316643e7SJed Brown 
1214316643e7SJed Brown    Calling sequence of f:
1215ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1216316643e7SJed Brown 
1217316643e7SJed Brown +  t    - time at step/stage being solved
12181b4a444bSJed Brown .  U    - state vector
12191b4a444bSJed Brown .  U_t  - time derivative of state vector
1220316643e7SJed Brown .  a    - shift
1221e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1222e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1223316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1224316643e7SJed Brown 
1225316643e7SJed Brown    Notes:
1226e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1227316643e7SJed Brown 
1228a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1229b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1230a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1231a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1232a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1233a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1234a4f0a591SBarry Smith 
1235316643e7SJed Brown    Level: beginner
1236316643e7SJed Brown 
1237316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1238316643e7SJed Brown 
1239ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1240316643e7SJed Brown 
1241316643e7SJed Brown @*/
1242e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1243316643e7SJed Brown {
1244316643e7SJed Brown   PetscErrorCode ierr;
1245089b2837SJed Brown   SNES           snes;
124624989b8cSPeter Brune   DM             dm;
1247316643e7SJed Brown 
1248316643e7SJed Brown   PetscFunctionBegin;
12490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1250e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1251e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1252e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1253e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
125424989b8cSPeter Brune 
125524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
125624989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
125724989b8cSPeter Brune 
1258089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1259e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1260316643e7SJed Brown   PetscFunctionReturn(0);
1261316643e7SJed Brown }
1262316643e7SJed Brown 
12634a2ae208SSatish Balay #undef __FUNCT__
1264e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse"
1265e1244c69SJed Brown /*@
1266e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1267e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1268e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1269e1244c69SJed Brown    not been changed by the TS.
1270e1244c69SJed Brown 
1271e1244c69SJed Brown    Logically Collective
1272e1244c69SJed Brown 
1273e1244c69SJed Brown    Input Arguments:
1274e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1275e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1276e1244c69SJed Brown 
1277e1244c69SJed Brown    Level: intermediate
1278e1244c69SJed Brown 
1279e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1280e1244c69SJed Brown @*/
1281e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1282e1244c69SJed Brown {
1283e1244c69SJed Brown   PetscFunctionBegin;
1284e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1285e1244c69SJed Brown   PetscFunctionReturn(0);
1286e1244c69SJed Brown }
1287e1244c69SJed Brown 
1288e1244c69SJed Brown #undef __FUNCT__
128955849f57SBarry Smith #define __FUNCT__ "TSLoad"
129055849f57SBarry Smith /*@C
129155849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
129255849f57SBarry Smith 
129355849f57SBarry Smith   Collective on PetscViewer
129455849f57SBarry Smith 
129555849f57SBarry Smith   Input Parameters:
129655849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
129755849f57SBarry Smith            some related function before a call to TSLoad().
129855849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
129955849f57SBarry Smith 
130055849f57SBarry Smith    Level: intermediate
130155849f57SBarry Smith 
130255849f57SBarry Smith   Notes:
130355849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
130455849f57SBarry Smith 
130555849f57SBarry Smith   Notes for advanced users:
130655849f57SBarry Smith   Most users should not need to know the details of the binary storage
130755849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
130855849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
130955849f57SBarry Smith   format is
131055849f57SBarry Smith .vb
131155849f57SBarry Smith      has not yet been determined
131255849f57SBarry Smith .ve
131355849f57SBarry Smith 
131455849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
131555849f57SBarry Smith @*/
1316f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
131755849f57SBarry Smith {
131855849f57SBarry Smith   PetscErrorCode ierr;
131955849f57SBarry Smith   PetscBool      isbinary;
132055849f57SBarry Smith   PetscInt       classid;
132155849f57SBarry Smith   char           type[256];
13222d53ad75SBarry Smith   DMTS           sdm;
1323ad6bc421SBarry Smith   DM             dm;
132455849f57SBarry Smith 
132555849f57SBarry Smith   PetscFunctionBegin;
1326f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
132755849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
132855849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
132955849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
133055849f57SBarry Smith 
133155849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
1332ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
133355849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
1334f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1335f2c2a1b9SBarry Smith   if (ts->ops->load) {
1336f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1337f2c2a1b9SBarry Smith   }
1338ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1339ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1340ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1341f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1342f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
13432d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13442d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
134555849f57SBarry Smith   PetscFunctionReturn(0);
134655849f57SBarry Smith }
134755849f57SBarry Smith 
13489804daf3SBarry Smith #include <petscdraw.h>
1349e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1350e04113cfSBarry Smith #include <petscviewersaws.h>
1351f05ece33SBarry Smith #endif
135255849f57SBarry Smith #undef __FUNCT__
13534a2ae208SSatish Balay #define __FUNCT__ "TSView"
13547e2c5f70SBarry Smith /*@C
1355d763cef2SBarry Smith     TSView - Prints the TS data structure.
1356d763cef2SBarry Smith 
13574c49b128SBarry Smith     Collective on TS
1358d763cef2SBarry Smith 
1359d763cef2SBarry Smith     Input Parameters:
1360d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1361d763cef2SBarry Smith -   viewer - visualization context
1362d763cef2SBarry Smith 
1363d763cef2SBarry Smith     Options Database Key:
1364d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1365d763cef2SBarry Smith 
1366d763cef2SBarry Smith     Notes:
1367d763cef2SBarry Smith     The available visualization contexts include
1368b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1369b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1370d763cef2SBarry Smith          output where only the first processor opens
1371d763cef2SBarry Smith          the file.  All other processors send their
1372d763cef2SBarry Smith          data to the first processor to print.
1373d763cef2SBarry Smith 
1374d763cef2SBarry Smith     The user can open an alternative visualization context with
1375b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1376d763cef2SBarry Smith 
1377d763cef2SBarry Smith     Level: beginner
1378d763cef2SBarry Smith 
1379d763cef2SBarry Smith .keywords: TS, timestep, view
1380d763cef2SBarry Smith 
1381b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1382d763cef2SBarry Smith @*/
13837087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1384d763cef2SBarry Smith {
1385dfbe8321SBarry Smith   PetscErrorCode ierr;
138619fd82e9SBarry Smith   TSType         type;
13872b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
13882d53ad75SBarry Smith   DMTS           sdm;
1389e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1390f05ece33SBarry Smith   PetscBool      isams;
1391f05ece33SBarry Smith #endif
1392d763cef2SBarry Smith 
1393d763cef2SBarry Smith   PetscFunctionBegin;
13940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13953050cee2SBarry Smith   if (!viewer) {
1396ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
13973050cee2SBarry Smith   }
13980700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1399c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1400fd16b177SBarry Smith 
1401251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1402251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
140355849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
14042b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1405e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1406e04113cfSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&isams);CHKERRQ(ierr);
1407f05ece33SBarry Smith #endif
140832077d6dSBarry Smith   if (iascii) {
1409dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
141077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
14117c8652ddSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1412d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
14135ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1414c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1415d763cef2SBarry Smith     }
14165ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1417193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
14182d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14192d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1420d52bd9f3SBarry Smith     if (ts->ops->view) {
1421d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1422d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1423d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1424d52bd9f3SBarry Smith     }
14250f5bd95cSBarry Smith   } else if (isstring) {
1426a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1427b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
142855849f57SBarry Smith   } else if (isbinary) {
142955849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
143055849f57SBarry Smith     MPI_Comm    comm;
143155849f57SBarry Smith     PetscMPIInt rank;
143255849f57SBarry Smith     char        type[256];
143355849f57SBarry Smith 
143455849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
143555849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
143655849f57SBarry Smith     if (!rank) {
143755849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
143855849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
143955849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
144055849f57SBarry Smith     }
144155849f57SBarry Smith     if (ts->ops->view) {
144255849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
144355849f57SBarry Smith     }
1444f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1445f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
14462d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14472d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
14482b0a91c0SBarry Smith   } else if (isdraw) {
14492b0a91c0SBarry Smith     PetscDraw draw;
14502b0a91c0SBarry Smith     char      str[36];
145189fd9fafSBarry Smith     PetscReal x,y,bottom,h;
14522b0a91c0SBarry Smith 
14532b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
14542b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
14552b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
14562b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
14570298fd71SBarry Smith     ierr   = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
145889fd9fafSBarry Smith     bottom = y - h;
14592b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
14602b0a91c0SBarry Smith     if (ts->ops->view) {
14612b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14622b0a91c0SBarry Smith     }
14632b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1464e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1465f05ece33SBarry Smith   } else if (isams) {
1466d45a07a7SBarry Smith     PetscMPIInt rank;
14672657e9d9SBarry Smith     const char  *name;
14682657e9d9SBarry Smith 
14692657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
1470d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1471d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
1472d45a07a7SBarry Smith       char       dir[1024];
1473d45a07a7SBarry Smith 
1474e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
1475a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
14762657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
14772657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
14782657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
1479d763cef2SBarry Smith     }
14800acecf5bSBarry Smith     if (ts->ops->view) {
14810acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14820acecf5bSBarry Smith     }
1483f05ece33SBarry Smith #endif
1484f05ece33SBarry Smith   }
1485f05ece33SBarry Smith 
1486b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1487251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1488b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1489d763cef2SBarry Smith   PetscFunctionReturn(0);
1490d763cef2SBarry Smith }
1491d763cef2SBarry Smith 
1492d763cef2SBarry Smith 
14934a2ae208SSatish Balay #undef __FUNCT__
14944a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1495b07ff414SBarry Smith /*@
1496d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1497d763cef2SBarry Smith    the timesteppers.
1498d763cef2SBarry Smith 
14993f9fe445SBarry Smith    Logically Collective on TS
1500d763cef2SBarry Smith 
1501d763cef2SBarry Smith    Input Parameters:
1502d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1503d763cef2SBarry Smith -  usrP - optional user context
1504d763cef2SBarry Smith 
1505d763cef2SBarry Smith    Level: intermediate
1506d763cef2SBarry Smith 
1507d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1508d763cef2SBarry Smith 
1509d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1510d763cef2SBarry Smith @*/
15117087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1512d763cef2SBarry Smith {
1513d763cef2SBarry Smith   PetscFunctionBegin;
15140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1515d763cef2SBarry Smith   ts->user = usrP;
1516d763cef2SBarry Smith   PetscFunctionReturn(0);
1517d763cef2SBarry Smith }
1518d763cef2SBarry Smith 
15194a2ae208SSatish Balay #undef __FUNCT__
15204a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1521b07ff414SBarry Smith /*@
1522d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1523d763cef2SBarry Smith     timestepper.
1524d763cef2SBarry Smith 
1525d763cef2SBarry Smith     Not Collective
1526d763cef2SBarry Smith 
1527d763cef2SBarry Smith     Input Parameter:
1528d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1529d763cef2SBarry Smith 
1530d763cef2SBarry Smith     Output Parameter:
1531d763cef2SBarry Smith .   usrP - user context
1532d763cef2SBarry Smith 
1533d763cef2SBarry Smith     Level: intermediate
1534d763cef2SBarry Smith 
1535d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1536d763cef2SBarry Smith 
1537d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1538d763cef2SBarry Smith @*/
1539e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1540d763cef2SBarry Smith {
1541d763cef2SBarry Smith   PetscFunctionBegin;
15420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1543e71120c6SJed Brown   *(void**)usrP = ts->user;
1544d763cef2SBarry Smith   PetscFunctionReturn(0);
1545d763cef2SBarry Smith }
1546d763cef2SBarry Smith 
15474a2ae208SSatish Balay #undef __FUNCT__
15484a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1549d763cef2SBarry Smith /*@
1550b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1551d763cef2SBarry Smith 
1552d763cef2SBarry Smith    Not Collective
1553d763cef2SBarry Smith 
1554d763cef2SBarry Smith    Input Parameter:
1555d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1556d763cef2SBarry Smith 
1557d763cef2SBarry Smith    Output Parameter:
1558b8123daeSJed Brown .  iter - number of steps completed so far
1559d763cef2SBarry Smith 
1560d763cef2SBarry Smith    Level: intermediate
1561d763cef2SBarry Smith 
1562d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
15639be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
1564d763cef2SBarry Smith @*/
15657087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1566d763cef2SBarry Smith {
1567d763cef2SBarry Smith   PetscFunctionBegin;
15680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15694482741eSBarry Smith   PetscValidIntPointer(iter,2);
1570d763cef2SBarry Smith   *iter = ts->steps;
1571d763cef2SBarry Smith   PetscFunctionReturn(0);
1572d763cef2SBarry Smith }
1573d763cef2SBarry Smith 
15744a2ae208SSatish Balay #undef __FUNCT__
15754a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1576d763cef2SBarry Smith /*@
1577d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1578d763cef2SBarry Smith    as well as the initial time.
1579d763cef2SBarry Smith 
15803f9fe445SBarry Smith    Logically Collective on TS
1581d763cef2SBarry Smith 
1582d763cef2SBarry Smith    Input Parameters:
1583d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1584d763cef2SBarry Smith .  initial_time - the initial time
1585d763cef2SBarry Smith -  time_step - the size of the timestep
1586d763cef2SBarry Smith 
1587d763cef2SBarry Smith    Level: intermediate
1588d763cef2SBarry Smith 
1589d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1590d763cef2SBarry Smith 
1591d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1592d763cef2SBarry Smith @*/
15937087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1594d763cef2SBarry Smith {
1595e144a568SJed Brown   PetscErrorCode ierr;
1596e144a568SJed Brown 
1597d763cef2SBarry Smith   PetscFunctionBegin;
15980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1599e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1600d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1601d763cef2SBarry Smith   PetscFunctionReturn(0);
1602d763cef2SBarry Smith }
1603d763cef2SBarry Smith 
16044a2ae208SSatish Balay #undef __FUNCT__
16054a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1606d763cef2SBarry Smith /*@
1607d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1608d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1609d763cef2SBarry Smith 
16103f9fe445SBarry Smith    Logically Collective on TS
1611d763cef2SBarry Smith 
1612d763cef2SBarry Smith    Input Parameters:
1613d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1614d763cef2SBarry Smith -  time_step - the size of the timestep
1615d763cef2SBarry Smith 
1616d763cef2SBarry Smith    Level: intermediate
1617d763cef2SBarry Smith 
1618d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1619d763cef2SBarry Smith 
1620d763cef2SBarry Smith .keywords: TS, set, timestep
1621d763cef2SBarry Smith @*/
16227087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1623d763cef2SBarry Smith {
1624d763cef2SBarry Smith   PetscFunctionBegin;
16250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1626c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1627d763cef2SBarry Smith   ts->time_step      = time_step;
162831748224SBarry Smith   ts->time_step_orig = time_step;
1629d763cef2SBarry Smith   PetscFunctionReturn(0);
1630d763cef2SBarry Smith }
1631d763cef2SBarry Smith 
16324a2ae208SSatish Balay #undef __FUNCT__
1633a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1634a43b19c4SJed Brown /*@
163549354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
163649354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
163749354f04SShri Abhyankar      or just return at the final time TS computed.
1638a43b19c4SJed Brown 
1639a43b19c4SJed Brown   Logically Collective on TS
1640a43b19c4SJed Brown 
1641a43b19c4SJed Brown    Input Parameter:
1642a43b19c4SJed Brown +   ts - the time-step context
164349354f04SShri Abhyankar -   eftopt - exact final time option
1644a43b19c4SJed Brown 
1645a43b19c4SJed Brown    Level: beginner
1646a43b19c4SJed Brown 
1647a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1648a43b19c4SJed Brown @*/
164949354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1650a43b19c4SJed Brown {
1651a43b19c4SJed Brown   PetscFunctionBegin;
1652a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
165349354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
165449354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1655a43b19c4SJed Brown   PetscFunctionReturn(0);
1656a43b19c4SJed Brown }
1657a43b19c4SJed Brown 
1658a43b19c4SJed Brown #undef __FUNCT__
16594a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1660d763cef2SBarry Smith /*@
1661d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1662d763cef2SBarry Smith 
1663d763cef2SBarry Smith    Not Collective
1664d763cef2SBarry Smith 
1665d763cef2SBarry Smith    Input Parameter:
1666d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1667d763cef2SBarry Smith 
1668d763cef2SBarry Smith    Output Parameter:
1669d763cef2SBarry Smith .  dt - the current timestep size
1670d763cef2SBarry Smith 
1671d763cef2SBarry Smith    Level: intermediate
1672d763cef2SBarry Smith 
1673d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1674d763cef2SBarry Smith 
1675d763cef2SBarry Smith .keywords: TS, get, timestep
1676d763cef2SBarry Smith @*/
16777087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1678d763cef2SBarry Smith {
1679d763cef2SBarry Smith   PetscFunctionBegin;
16800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1681f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1682d763cef2SBarry Smith   *dt = ts->time_step;
1683d763cef2SBarry Smith   PetscFunctionReturn(0);
1684d763cef2SBarry Smith }
1685d763cef2SBarry Smith 
16864a2ae208SSatish Balay #undef __FUNCT__
16874a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1688d8e5e3e6SSatish Balay /*@
1689d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1690d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1691d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1692d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1693d763cef2SBarry Smith 
1694d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1695d763cef2SBarry Smith 
1696d763cef2SBarry Smith    Input Parameter:
1697d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1698d763cef2SBarry Smith 
1699d763cef2SBarry Smith    Output Parameter:
1700d763cef2SBarry Smith .  v - the vector containing the solution
1701d763cef2SBarry Smith 
1702d763cef2SBarry Smith    Level: intermediate
1703d763cef2SBarry Smith 
1704d763cef2SBarry Smith .seealso: TSGetTimeStep()
1705d763cef2SBarry Smith 
1706d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1707d763cef2SBarry Smith @*/
17087087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1709d763cef2SBarry Smith {
1710d763cef2SBarry Smith   PetscFunctionBegin;
17110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17124482741eSBarry Smith   PetscValidPointer(v,2);
17138737fe31SLisandro Dalcin   *v = ts->vec_sol;
1714d763cef2SBarry Smith   PetscFunctionReturn(0);
1715d763cef2SBarry Smith }
1716d763cef2SBarry Smith 
1717c235aa8dSHong Zhang #undef __FUNCT__
1718419a887dSBarry Smith #define __FUNCT__ "TSAdjointGetGradients"
1719c235aa8dSHong Zhang /*@
1720419a887dSBarry Smith    TSAdjointGetGradients - Returns the gradients from the TSAdjointSolve()
1721c235aa8dSHong Zhang 
1722c235aa8dSHong Zhang    Not Collective, but Vec returned is parallel if TS is parallel
1723c235aa8dSHong Zhang 
1724c235aa8dSHong Zhang    Input Parameter:
1725c235aa8dSHong Zhang .  ts - the TS context obtained from TSCreate()
1726c235aa8dSHong Zhang 
1727c235aa8dSHong Zhang    Output Parameter:
1728419a887dSBarry Smith +  v - vectors containing the gradients with respect to the ODE/DAE solution variables
1729419a887dSBarry Smith -  w - vectors containing the gradients with respect to the problem parameters
1730c235aa8dSHong Zhang 
1731c235aa8dSHong Zhang    Level: intermediate
1732c235aa8dSHong Zhang 
1733c235aa8dSHong Zhang .seealso: TSGetTimeStep()
1734c235aa8dSHong Zhang 
1735c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity
1736c235aa8dSHong Zhang @*/
17375bf8c567SBarry Smith PetscErrorCode  TSAdjointGetGradients(TS ts,PetscInt *numberadjs,Vec **v,Vec **w)
1738c235aa8dSHong Zhang {
1739c235aa8dSHong Zhang   PetscFunctionBegin;
1740c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1741b18ea86cSHong Zhang   if (numberadjs) *numberadjs = ts->numberadjs;
1742419a887dSBarry Smith   if (v)          *v          = ts->vecs_sensi;
1743419a887dSBarry Smith   if (w)          *w          = ts->vecs_sensip;
1744c235aa8dSHong Zhang   PetscFunctionReturn(0);
1745c235aa8dSHong Zhang }
1746c235aa8dSHong Zhang 
1747bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
17484a2ae208SSatish Balay #undef __FUNCT__
1749bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1750d8e5e3e6SSatish Balay /*@
1751bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1752d763cef2SBarry Smith 
1753bdad233fSMatthew Knepley   Not collective
1754d763cef2SBarry Smith 
1755bdad233fSMatthew Knepley   Input Parameters:
1756bdad233fSMatthew Knepley + ts   - The TS
1757bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1758d763cef2SBarry Smith .vb
17590910c330SBarry Smith          U_t - A U = 0      (linear)
17600910c330SBarry Smith          U_t - A(t) U = 0   (linear)
17610910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1762d763cef2SBarry Smith .ve
1763d763cef2SBarry Smith 
1764d763cef2SBarry Smith    Level: beginner
1765d763cef2SBarry Smith 
1766bdad233fSMatthew Knepley .keywords: TS, problem type
1767bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1768d763cef2SBarry Smith @*/
17697087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1770a7cc72afSBarry Smith {
17719e2a6581SJed Brown   PetscErrorCode ierr;
17729e2a6581SJed Brown 
1773d763cef2SBarry Smith   PetscFunctionBegin;
17740700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1775bdad233fSMatthew Knepley   ts->problem_type = type;
17769e2a6581SJed Brown   if (type == TS_LINEAR) {
17779e2a6581SJed Brown     SNES snes;
17789e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
17799e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
17809e2a6581SJed Brown   }
1781d763cef2SBarry Smith   PetscFunctionReturn(0);
1782d763cef2SBarry Smith }
1783d763cef2SBarry Smith 
1784bdad233fSMatthew Knepley #undef __FUNCT__
1785bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1786bdad233fSMatthew Knepley /*@C
1787bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1788bdad233fSMatthew Knepley 
1789bdad233fSMatthew Knepley   Not collective
1790bdad233fSMatthew Knepley 
1791bdad233fSMatthew Knepley   Input Parameter:
1792bdad233fSMatthew Knepley . ts   - The TS
1793bdad233fSMatthew Knepley 
1794bdad233fSMatthew Knepley   Output Parameter:
1795bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1796bdad233fSMatthew Knepley .vb
1797089b2837SJed Brown          M U_t = A U
1798089b2837SJed Brown          M(t) U_t = A(t) U
1799b5abc632SBarry Smith          F(t,U,U_t)
1800bdad233fSMatthew Knepley .ve
1801bdad233fSMatthew Knepley 
1802bdad233fSMatthew Knepley    Level: beginner
1803bdad233fSMatthew Knepley 
1804bdad233fSMatthew Knepley .keywords: TS, problem type
1805bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1806bdad233fSMatthew Knepley @*/
18077087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1808a7cc72afSBarry Smith {
1809bdad233fSMatthew Knepley   PetscFunctionBegin;
18100700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
18114482741eSBarry Smith   PetscValidIntPointer(type,2);
1812bdad233fSMatthew Knepley   *type = ts->problem_type;
1813bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1814bdad233fSMatthew Knepley }
1815d763cef2SBarry Smith 
18164a2ae208SSatish Balay #undef __FUNCT__
18174a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1818d763cef2SBarry Smith /*@
1819d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1820d763cef2SBarry Smith    of a timestepper.
1821d763cef2SBarry Smith 
1822d763cef2SBarry Smith    Collective on TS
1823d763cef2SBarry Smith 
1824d763cef2SBarry Smith    Input Parameter:
1825d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1826d763cef2SBarry Smith 
1827d763cef2SBarry Smith    Notes:
1828d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1829d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1830d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1831d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1832d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1833d763cef2SBarry Smith 
1834d763cef2SBarry Smith    Level: advanced
1835d763cef2SBarry Smith 
1836d763cef2SBarry Smith .keywords: TS, timestep, setup
1837d763cef2SBarry Smith 
1838d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1839d763cef2SBarry Smith @*/
18407087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1841d763cef2SBarry Smith {
1842dfbe8321SBarry Smith   PetscErrorCode ierr;
18436c6b9e74SPeter Brune   DM             dm;
18446c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
1845d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
18466c6b9e74SPeter Brune   TSIJacobian    ijac;
18476c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1848d763cef2SBarry Smith 
1849d763cef2SBarry Smith   PetscFunctionBegin;
18500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1851277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1852277b19d0SLisandro Dalcin 
18532c18e0fdSBarry Smith   ts->total_steps = 0;
18547adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
18559596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1856d763cef2SBarry Smith   }
1857277b19d0SLisandro Dalcin 
1858277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1859277b19d0SLisandro Dalcin 
1860c235aa8dSHong Zhang 
1861552698daSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
18621c3436cfSJed Brown 
1863e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
1864e1244c69SJed Brown     Mat Amat,Pmat;
1865e1244c69SJed Brown     SNES snes;
1866e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1867e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
1868e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
1869e1244c69SJed Brown      * have displaced the RHS matrix */
1870e1244c69SJed Brown     if (Amat == ts->Arhs) {
1871e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
1872e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
1873e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
1874e1244c69SJed Brown     }
1875e1244c69SJed Brown     if (Pmat == ts->Brhs) {
1876e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
1877e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
1878e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
1879e1244c69SJed Brown     }
1880e1244c69SJed Brown   }
1881277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1882000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1883277b19d0SLisandro Dalcin   }
1884277b19d0SLisandro Dalcin 
18856c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
18866c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
18876c6b9e74SPeter Brune    */
18886c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
18890298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
18906c6b9e74SPeter Brune   if (!func) {
18916c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
18926c6b9e74SPeter Brune   }
18936c6b9e74SPeter 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.
18946c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
18956c6b9e74SPeter Brune    */
18960298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
18970298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
18980298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
18996c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
19006c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
19016c6b9e74SPeter Brune   }
1902277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1903277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1904277b19d0SLisandro Dalcin }
1905277b19d0SLisandro Dalcin 
1906277b19d0SLisandro Dalcin #undef __FUNCT__
1907f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp"
1908f6a906c0SBarry Smith /*@
1909f6a906c0SBarry Smith    TSAdjointSetUp - Sets up the internal data structures for the later use
1910f6a906c0SBarry Smith    of an adjoint solver
1911f6a906c0SBarry Smith 
1912f6a906c0SBarry Smith    Collective on TS
1913f6a906c0SBarry Smith 
1914f6a906c0SBarry Smith    Input Parameter:
1915f6a906c0SBarry Smith .  ts - the TS context obtained from TSCreate()
1916f6a906c0SBarry Smith 
1917f6a906c0SBarry Smith    Notes:
1918f6a906c0SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1919f6a906c0SBarry Smith    TSSetUp(), since these actions will automatically occur during
1920f6a906c0SBarry Smith    the call to TSStep().  However, if one wishes to control this
1921f6a906c0SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1922f6a906c0SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1923f6a906c0SBarry Smith 
1924f6a906c0SBarry Smith    Level: advanced
1925f6a906c0SBarry Smith 
1926f6a906c0SBarry Smith .keywords: TS, timestep, setup
1927f6a906c0SBarry Smith 
1928f6a906c0SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1929f6a906c0SBarry Smith @*/
1930f6a906c0SBarry Smith PetscErrorCode  TSAdjointSetUp(TS ts)
1931f6a906c0SBarry Smith {
1932f6a906c0SBarry Smith   PetscErrorCode ierr;
1933f6a906c0SBarry Smith 
1934f6a906c0SBarry Smith   PetscFunctionBegin;
1935f6a906c0SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1936f6a906c0SBarry Smith   if (ts->adjointsetupcalled) PetscFunctionReturn(0);
1937419a887dSBarry Smith   if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSAdjointSetGradients() first");
193842f2b339SBarry Smith   if (ts->ops->adjointsetup) {
193942f2b339SBarry Smith     ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr);
1940f6a906c0SBarry Smith   }
1941f6a906c0SBarry Smith   ts->adjointsetupcalled = PETSC_TRUE;
1942f6a906c0SBarry Smith   PetscFunctionReturn(0);
1943f6a906c0SBarry Smith }
1944f6a906c0SBarry Smith 
1945f6a906c0SBarry Smith #undef __FUNCT__
1946277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1947277b19d0SLisandro Dalcin /*@
1948277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1949277b19d0SLisandro Dalcin 
1950277b19d0SLisandro Dalcin    Collective on TS
1951277b19d0SLisandro Dalcin 
1952277b19d0SLisandro Dalcin    Input Parameter:
1953277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1954277b19d0SLisandro Dalcin 
1955277b19d0SLisandro Dalcin    Level: beginner
1956277b19d0SLisandro Dalcin 
1957277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1958277b19d0SLisandro Dalcin 
1959277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1960277b19d0SLisandro Dalcin @*/
1961277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1962277b19d0SLisandro Dalcin {
1963277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1964277b19d0SLisandro Dalcin 
1965277b19d0SLisandro Dalcin   PetscFunctionBegin;
1966277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1967b18ea86cSHong Zhang 
1968277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1969277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1970277b19d0SLisandro Dalcin   }
1971277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
1972bbd56ea5SKarl Rupp 
19734e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
19744e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1975214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
19766bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1977e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1978e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
197938637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1980f8fee759SHong Zhang   ts->vecs_sensi = 0;
1981f8fee759SHong Zhang   ts->vecs_sensip = 0;
1982ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
19832c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
198436eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
1985277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1986d763cef2SBarry Smith   PetscFunctionReturn(0);
1987d763cef2SBarry Smith }
1988d763cef2SBarry Smith 
19894a2ae208SSatish Balay #undef __FUNCT__
19904a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1991d8e5e3e6SSatish Balay /*@
1992d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1993d763cef2SBarry Smith    with TSCreate().
1994d763cef2SBarry Smith 
1995d763cef2SBarry Smith    Collective on TS
1996d763cef2SBarry Smith 
1997d763cef2SBarry Smith    Input Parameter:
1998d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1999d763cef2SBarry Smith 
2000d763cef2SBarry Smith    Level: beginner
2001d763cef2SBarry Smith 
2002d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2003d763cef2SBarry Smith 
2004d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2005d763cef2SBarry Smith @*/
20066bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2007d763cef2SBarry Smith {
20086849ba73SBarry Smith   PetscErrorCode ierr;
2009d763cef2SBarry Smith 
2010d763cef2SBarry Smith   PetscFunctionBegin;
20116bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
20126bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
20136bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2014d763cef2SBarry Smith 
20156bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2016277b19d0SLisandro Dalcin 
2017e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2018e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
20196bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
20206d4c513bSLisandro Dalcin 
2021bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2022bc952696SBarry Smith 
202384df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
2024aeb4809dSShri Abhyankar   if ((*ts)->event) {
2025aeb4809dSShri Abhyankar     ierr = TSEventMonitorDestroy(&(*ts)->event);CHKERRQ(ierr);
2026aeb4809dSShri Abhyankar   }
20276bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
20286bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
20296bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
20306d4c513bSLisandro Dalcin 
2031a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2032d763cef2SBarry Smith   PetscFunctionReturn(0);
2033d763cef2SBarry Smith }
2034d763cef2SBarry Smith 
20354a2ae208SSatish Balay #undef __FUNCT__
20364a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
2037d8e5e3e6SSatish Balay /*@
2038d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2039d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2040d763cef2SBarry Smith 
2041d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2042d763cef2SBarry Smith 
2043d763cef2SBarry Smith    Input Parameter:
2044d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2045d763cef2SBarry Smith 
2046d763cef2SBarry Smith    Output Parameter:
2047d763cef2SBarry Smith .  snes - the nonlinear solver context
2048d763cef2SBarry Smith 
2049d763cef2SBarry Smith    Notes:
2050d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2051d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
205294b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2053d763cef2SBarry Smith 
2054d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
20550298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2056d763cef2SBarry Smith 
2057d763cef2SBarry Smith    Level: beginner
2058d763cef2SBarry Smith 
2059d763cef2SBarry Smith .keywords: timestep, get, SNES
2060d763cef2SBarry Smith @*/
20617087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2062d763cef2SBarry Smith {
2063d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2064d372ba47SLisandro Dalcin 
2065d763cef2SBarry Smith   PetscFunctionBegin;
20660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20674482741eSBarry Smith   PetscValidPointer(snes,2);
2068d372ba47SLisandro Dalcin   if (!ts->snes) {
2069ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
20700298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
20713bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2072d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2073496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
20749e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
20759e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
20769e2a6581SJed Brown     }
2077d372ba47SLisandro Dalcin   }
2078d763cef2SBarry Smith   *snes = ts->snes;
2079d763cef2SBarry Smith   PetscFunctionReturn(0);
2080d763cef2SBarry Smith }
2081d763cef2SBarry Smith 
20824a2ae208SSatish Balay #undef __FUNCT__
2083deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
2084deb2cd25SJed Brown /*@
2085deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2086deb2cd25SJed Brown 
2087deb2cd25SJed Brown    Collective
2088deb2cd25SJed Brown 
2089deb2cd25SJed Brown    Input Parameter:
2090deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2091deb2cd25SJed Brown -  snes - the nonlinear solver context
2092deb2cd25SJed Brown 
2093deb2cd25SJed Brown    Notes:
2094deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2095deb2cd25SJed Brown 
2096deb2cd25SJed Brown    Level: developer
2097deb2cd25SJed Brown 
2098deb2cd25SJed Brown .keywords: timestep, set, SNES
2099deb2cd25SJed Brown @*/
2100deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2101deb2cd25SJed Brown {
2102deb2cd25SJed Brown   PetscErrorCode ierr;
2103d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2104deb2cd25SJed Brown 
2105deb2cd25SJed Brown   PetscFunctionBegin;
2106deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2107deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2108deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2109deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2110bbd56ea5SKarl Rupp 
2111deb2cd25SJed Brown   ts->snes = snes;
2112bbd56ea5SKarl Rupp 
21130298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
21140298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2115740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
21160298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2117740132f1SEmil Constantinescu   }
2118deb2cd25SJed Brown   PetscFunctionReturn(0);
2119deb2cd25SJed Brown }
2120deb2cd25SJed Brown 
2121deb2cd25SJed Brown #undef __FUNCT__
212294b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
2123d8e5e3e6SSatish Balay /*@
212494b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2125d763cef2SBarry Smith    a TS (timestepper) context.
2126d763cef2SBarry Smith 
212794b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2128d763cef2SBarry Smith 
2129d763cef2SBarry Smith    Input Parameter:
2130d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2131d763cef2SBarry Smith 
2132d763cef2SBarry Smith    Output Parameter:
213394b7f48cSBarry Smith .  ksp - the nonlinear solver context
2134d763cef2SBarry Smith 
2135d763cef2SBarry Smith    Notes:
213694b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2137d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2138d763cef2SBarry Smith    KSP and PC contexts as well.
2139d763cef2SBarry Smith 
214094b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
21410298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2142d763cef2SBarry Smith 
2143d763cef2SBarry Smith    Level: beginner
2144d763cef2SBarry Smith 
214594b7f48cSBarry Smith .keywords: timestep, get, KSP
2146d763cef2SBarry Smith @*/
21477087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2148d763cef2SBarry Smith {
2149d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2150089b2837SJed Brown   SNES           snes;
2151d372ba47SLisandro Dalcin 
2152d763cef2SBarry Smith   PetscFunctionBegin;
21530700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21544482741eSBarry Smith   PetscValidPointer(ksp,2);
215517186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2156e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2157089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2158089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2159d763cef2SBarry Smith   PetscFunctionReturn(0);
2160d763cef2SBarry Smith }
2161d763cef2SBarry Smith 
2162d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2163d763cef2SBarry Smith 
21644a2ae208SSatish Balay #undef __FUNCT__
2165adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
2166adb62b0dSMatthew Knepley /*@
2167adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2168adb62b0dSMatthew Knepley    maximum time for iteration.
2169adb62b0dSMatthew Knepley 
21703f9fe445SBarry Smith    Not Collective
2171adb62b0dSMatthew Knepley 
2172adb62b0dSMatthew Knepley    Input Parameters:
2173adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
21740298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
21750298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2176adb62b0dSMatthew Knepley 
2177adb62b0dSMatthew Knepley    Level: intermediate
2178adb62b0dSMatthew Knepley 
2179adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2180adb62b0dSMatthew Knepley @*/
21817087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2182adb62b0dSMatthew Knepley {
2183adb62b0dSMatthew Knepley   PetscFunctionBegin;
21840700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2185abc0a331SBarry Smith   if (maxsteps) {
21864482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2187adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2188adb62b0dSMatthew Knepley   }
2189abc0a331SBarry Smith   if (maxtime) {
21904482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2191adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2192adb62b0dSMatthew Knepley   }
2193adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2194adb62b0dSMatthew Knepley }
2195adb62b0dSMatthew Knepley 
2196adb62b0dSMatthew Knepley #undef __FUNCT__
21974a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
2198d763cef2SBarry Smith /*@
2199d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2200d763cef2SBarry Smith    maximum time for iteration.
2201d763cef2SBarry Smith 
22023f9fe445SBarry Smith    Logically Collective on TS
2203d763cef2SBarry Smith 
2204d763cef2SBarry Smith    Input Parameters:
2205d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2206d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2207d763cef2SBarry Smith -  maxtime - final time to iterate to
2208d763cef2SBarry Smith 
2209d763cef2SBarry Smith    Options Database Keys:
2210d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
22113bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2212d763cef2SBarry Smith 
2213d763cef2SBarry Smith    Notes:
2214d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2215d763cef2SBarry Smith 
2216d763cef2SBarry Smith    Level: intermediate
2217d763cef2SBarry Smith 
2218d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2219a43b19c4SJed Brown 
2220a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2221d763cef2SBarry Smith @*/
22227087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2223d763cef2SBarry Smith {
2224d763cef2SBarry Smith   PetscFunctionBegin;
22250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2226c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2227c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
222839b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
222939b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2230d763cef2SBarry Smith   PetscFunctionReturn(0);
2231d763cef2SBarry Smith }
2232d763cef2SBarry Smith 
22334a2ae208SSatish Balay #undef __FUNCT__
22344a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
2235d763cef2SBarry Smith /*@
2236d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2237d763cef2SBarry Smith    for use by the TS routines.
2238d763cef2SBarry Smith 
22393f9fe445SBarry Smith    Logically Collective on TS and Vec
2240d763cef2SBarry Smith 
2241d763cef2SBarry Smith    Input Parameters:
2242d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
22430910c330SBarry Smith -  u - the solution vector
2244d763cef2SBarry Smith 
2245d763cef2SBarry Smith    Level: beginner
2246d763cef2SBarry Smith 
2247d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2248d763cef2SBarry Smith @*/
22490910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2250d763cef2SBarry Smith {
22518737fe31SLisandro Dalcin   PetscErrorCode ierr;
2252496e6a7aSJed Brown   DM             dm;
22538737fe31SLisandro Dalcin 
2254d763cef2SBarry Smith   PetscFunctionBegin;
22550700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22560910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
22570910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
22586bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2259bbd56ea5SKarl Rupp 
22600910c330SBarry Smith   ts->vec_sol = u;
2261bbd56ea5SKarl Rupp 
2262496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
22630910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2264d763cef2SBarry Smith   PetscFunctionReturn(0);
2265d763cef2SBarry Smith }
2266d763cef2SBarry Smith 
2267e74ef692SMatthew Knepley #undef __FUNCT__
226873b18844SBarry Smith #define __FUNCT__ "TSAdjointSetSteps"
226973b18844SBarry Smith /*@
227073b18844SBarry Smith    TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time
227173b18844SBarry Smith 
227273b18844SBarry Smith    Logically Collective on TS
227373b18844SBarry Smith 
227473b18844SBarry Smith    Input Parameters:
227573b18844SBarry Smith +  ts - the TS context obtained from TSCreate()
227673b18844SBarry Smith .  steps - number of steps to use
227773b18844SBarry Smith 
227873b18844SBarry Smith    Level: intermediate
227973b18844SBarry Smith 
228073b18844SBarry Smith    Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this
228173b18844SBarry Smith           so as to integrate back to less than the original timestep
228273b18844SBarry Smith 
228373b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations
228473b18844SBarry Smith 
228573b18844SBarry Smith .seealso: TSSetExactFinalTime()
228673b18844SBarry Smith @*/
228773b18844SBarry Smith PetscErrorCode  TSAdjointSetSteps(TS ts,PetscInt steps)
228873b18844SBarry Smith {
228973b18844SBarry Smith   PetscFunctionBegin;
229073b18844SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
229173b18844SBarry Smith   PetscValidLogicalCollectiveInt(ts,steps,2);
229273b18844SBarry Smith   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps");
229373b18844SBarry 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");
229473b18844SBarry Smith   ts->adjoint_max_steps = steps;
229573b18844SBarry Smith   PetscFunctionReturn(0);
229673b18844SBarry Smith }
229773b18844SBarry Smith 
229873b18844SBarry Smith #undef __FUNCT__
2299419a887dSBarry Smith #define __FUNCT__ "TSAdjointSetGradients"
2300c235aa8dSHong Zhang /*@
2301419a887dSBarry Smith    TSAdjointSetGradients - Sets the initial value of gradients w.r.t. initial conditions and w.r.t. the problem parameters  for use by the TS routines.
2302c235aa8dSHong Zhang 
2303c235aa8dSHong Zhang    Logically Collective on TS and Vec
2304c235aa8dSHong Zhang 
2305c235aa8dSHong Zhang    Input Parameters:
2306c235aa8dSHong Zhang +  ts - the TS context obtained from TSCreate()
2307419a887dSBarry Smith .  u - gradients with respect to the initial condition variables
2308419a887dSBarry Smith -  w - gradients with respect to the parameters
2309c235aa8dSHong Zhang 
2310c235aa8dSHong Zhang    Level: beginner
2311c235aa8dSHong Zhang 
2312c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions
2313c235aa8dSHong Zhang @*/
2314419a887dSBarry Smith PetscErrorCode  TSAdjointSetGradients(TS ts,PetscInt numberadjs,Vec *u,Vec *w)
2315c235aa8dSHong Zhang {
2316c235aa8dSHong Zhang   PetscFunctionBegin;
2317c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2318b18ea86cSHong Zhang   PetscValidPointer(u,2);
2319b18ea86cSHong Zhang   ts->vecs_sensi  = u;
2320419a887dSBarry Smith   ts->vecs_sensip = w;
2321b18ea86cSHong Zhang   ts->numberadjs  = numberadjs;
2322ad8e2604SHong Zhang   PetscFunctionReturn(0);
2323ad8e2604SHong Zhang }
2324ad8e2604SHong Zhang 
2325ad8e2604SHong Zhang #undef __FUNCT__
23265bf8c567SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobian"
2327ad8e2604SHong Zhang /*@C
23285bf8c567SBarry Smith   TSAdjointSetRHSJacobian - Sets the function that computes the Jacobian w.r.t. parameters.
2329ad8e2604SHong Zhang 
2330ad8e2604SHong Zhang   Logically Collective on TS
2331ad8e2604SHong Zhang 
2332ad8e2604SHong Zhang   Input Parameters:
2333ad8e2604SHong Zhang + ts   - The TS context obtained from TSCreate()
2334ad8e2604SHong Zhang - func - The function
2335ad8e2604SHong Zhang 
2336ad8e2604SHong Zhang   Calling sequence of func:
233705755b9cSHong Zhang $ func (TS ts,PetscReal t,Vec u,Mat A,void *ctx);
233805755b9cSHong Zhang +   t - current timestep
233905755b9cSHong Zhang .   u - input vector
234005755b9cSHong Zhang .   A - output matrix
234105755b9cSHong Zhang -   ctx - [optional] user-defined function context
2342ad8e2604SHong Zhang 
2343ad8e2604SHong Zhang   Level: intermediate
2344ad8e2604SHong Zhang 
2345ad8e2604SHong Zhang .keywords: TS, sensitivity
2346ad8e2604SHong Zhang .seealso:
2347ad8e2604SHong Zhang @*/
23485bf8c567SBarry Smith PetscErrorCode  TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
2349ad8e2604SHong Zhang {
2350ad8e2604SHong Zhang   PetscErrorCode ierr;
2351ad8e2604SHong Zhang 
2352ad8e2604SHong Zhang   PetscFunctionBegin;
2353ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2354ad8e2604SHong Zhang   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
2355ad8e2604SHong Zhang 
2356ad8e2604SHong Zhang   ts->rhsjacobianp    = func;
2357ad8e2604SHong Zhang   ts->rhsjacobianpctx = ctx;
2358ad8e2604SHong Zhang   if(Amat) {
2359ad8e2604SHong Zhang     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
2360ad8e2604SHong Zhang     ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2361ad8e2604SHong Zhang 
2362ad8e2604SHong Zhang     ts->Jacp = Amat;
2363ad8e2604SHong Zhang   }
2364ad8e2604SHong Zhang   PetscFunctionReturn(0);
2365ad8e2604SHong Zhang }
2366ad8e2604SHong Zhang 
2367ad8e2604SHong Zhang #undef __FUNCT__
23685bf8c567SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobian"
2369ad8e2604SHong Zhang /*@
23705bf8c567SBarry Smith   TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function.
2371ad8e2604SHong Zhang 
2372ad8e2604SHong Zhang   Collective on TS
2373ad8e2604SHong Zhang 
2374ad8e2604SHong Zhang   Input Parameters:
2375ad8e2604SHong Zhang . ts   - The TS context obtained from TSCreate()
2376ad8e2604SHong Zhang 
2377ad8e2604SHong Zhang   Level: developer
2378ad8e2604SHong Zhang 
2379ad8e2604SHong Zhang .keywords: TS, sensitivity
23805bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian()
2381ad8e2604SHong Zhang @*/
23825bf8c567SBarry Smith PetscErrorCode  TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat)
2383ad8e2604SHong Zhang {
2384ad8e2604SHong Zhang   PetscErrorCode ierr;
2385ad8e2604SHong Zhang 
2386ad8e2604SHong Zhang   PetscFunctionBegin;
2387ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2388ad8e2604SHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
2389ad8e2604SHong Zhang   PetscValidPointer(Amat,4);
2390ad8e2604SHong Zhang 
2391ad8e2604SHong Zhang   PetscStackPush("TS user JacobianP function for sensitivity analysis");
2392ad8e2604SHong Zhang   ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr);
2393ad8e2604SHong Zhang   PetscStackPop;
2394ad8e2604SHong Zhang 
2395c235aa8dSHong Zhang   PetscFunctionReturn(0);
2396c235aa8dSHong Zhang }
2397c235aa8dSHong Zhang 
2398c235aa8dSHong Zhang #undef __FUNCT__
2399d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetCostIntegrand"
24006fd0887fSHong Zhang /*@C
24018a2f769dSBarry Smith     TSAdjointSetCostIntegrand - Sets the routine for evaluating the integral term in a cost function
24026fd0887fSHong Zhang 
24036fd0887fSHong Zhang     Logically Collective on TS
24046fd0887fSHong Zhang 
24056fd0887fSHong Zhang     Input Parameters:
24066fd0887fSHong Zhang +   ts - the TS context obtained from TSCreate()
24078a2f769dSBarry Smith .   numberadjs - number of gradients to be computed
24086fd0887fSHong Zhang .   fq - routine for evaluating the right-hand-side function
2409b612ec54SBarry Smith .   drdy - array of vectors to contain the gradient of the r's with respect to y, NULL if not a function of y
2410b612ec54SBarry Smith .   drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y
2411b612ec54SBarry Smith .   drdp - array of vectors to contain the gradient of the r's with respect to p, NULL if not a function of p
2412b612ec54SBarry Smith .   drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p
2413b612ec54SBarry Smith -   ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL)
24146fd0887fSHong Zhang 
2415b612ec54SBarry Smith     Calling sequence of fq:
2416a2ae3dd2SHong Zhang $     TSCostIntegrand(TS ts,PetscReal t,Vec u,PetscReal *f,void *ctx);
24176fd0887fSHong Zhang 
24186fd0887fSHong Zhang +   t - current timestep
24196fd0887fSHong Zhang .   u - input vector
242036eaed60SHong Zhang .   f - function vector
24216fd0887fSHong Zhang -   ctx - [optional] user-defined function context
24226fd0887fSHong Zhang 
2423b612ec54SBarry Smith    Calling sequence of drdyf:
2424b612ec54SBarry Smith $    PetscErroCode drdyf(TS ts,PetscReal t,Vec U,Vec *drdy,void *ctx);
2425b612ec54SBarry Smith 
2426b612ec54SBarry Smith    Calling sequence of drdpf:
2427b612ec54SBarry Smith $    PetscErroCode drdpf(TS ts,PetscReal t,Vec U,Vec *drdp,void *ctx);
2428b612ec54SBarry Smith 
2429b612ec54SBarry Smith     Level: intermediate
24306fd0887fSHong Zhang 
24316fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function
24326fd0887fSHong Zhang 
24335bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian(),TSAdjointGetGradients(), TSAdjointSetGradients()
24346fd0887fSHong Zhang @*/
24358a2f769dSBarry Smith PetscErrorCode  TSAdjointSetCostIntegrand(TS ts,PetscInt numberadjs,          PetscErrorCode (*fq)(TS,PetscReal,Vec,Vec,void*),
2436b612ec54SBarry Smith                                                                     Vec *drdy,PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*),
2437b612ec54SBarry Smith                                                                     Vec *drdp,PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),void *ctx)
24386fd0887fSHong Zhang {
24396fd0887fSHong Zhang   PetscErrorCode ierr;
24406fd0887fSHong Zhang 
24416fd0887fSHong Zhang   PetscFunctionBegin;
24426fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2443419a887dSBarry Smith   if (!ts->numberadjs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Call TSAdjointSetGradients() first so that the number of cost functions can be determined.");
2444419a887dSBarry Smith   if (ts->numberadjs && ts->numberadjs!=numberadjs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSAdjointSetCostIntegrand()) is inconsistent with the one set by TSAdjointSetGradients()");
24456fd0887fSHong Zhang 
24468a2f769dSBarry Smith   ierr                  = VecCreateSeq(PETSC_COMM_SELF,numberadjs,&ts->vec_costintegral);CHKERRQ(ierr);
24472c39e106SBarry Smith   ierr                  = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr);
2448e4680132SHong Zhang   ts->costintegrand     = fq;
244905755b9cSHong Zhang   ts->costintegrandctx  = ctx;
24506fd0887fSHong Zhang 
2451b612ec54SBarry Smith   ts->drdyfunction    = drdyf;
2452b612ec54SBarry Smith   ts->vecs_drdy       = drdy;
2453b612ec54SBarry Smith 
2454b612ec54SBarry Smith   ts->drdpfunction    = drdpf;
2455b612ec54SBarry Smith   ts->vecs_drdp       = drdp;
2456b612ec54SBarry Smith 
24576fd0887fSHong Zhang   PetscFunctionReturn(0);
24586fd0887fSHong Zhang }
24596fd0887fSHong Zhang 
24606fd0887fSHong Zhang #undef __FUNCT__
24612c39e106SBarry Smith #define __FUNCT__ "TSAdjointGetCostIntegral"
246236eaed60SHong Zhang /*@
24632c39e106SBarry Smith    TSAdjointGetCostIntegral - Returns the values of the integral term in the cost functions.
246436eaed60SHong Zhang    It is valid to call the routine after a backward run.
246536eaed60SHong Zhang 
246636eaed60SHong Zhang    Not Collective
246736eaed60SHong Zhang 
246836eaed60SHong Zhang    Input Parameter:
246936eaed60SHong Zhang .  ts - the TS context obtained from TSCreate()
247036eaed60SHong Zhang 
247136eaed60SHong Zhang    Output Parameter:
24722c39e106SBarry Smith .  v - the vector containing the integrals for each cost function
247336eaed60SHong Zhang 
247436eaed60SHong Zhang    Level: intermediate
247536eaed60SHong Zhang 
2476d4aa0a58SBarry Smith .seealso: TSAdjointSetCostIntegrand()
247736eaed60SHong Zhang 
247836eaed60SHong Zhang .keywords: TS, sensitivity analysis
247936eaed60SHong Zhang @*/
24802c39e106SBarry Smith PetscErrorCode  TSAdjointGetCostIntegral(TS ts,Vec *v)
248136eaed60SHong Zhang {
248236eaed60SHong Zhang   PetscFunctionBegin;
248336eaed60SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
248436eaed60SHong Zhang   PetscValidPointer(v,2);
24852c39e106SBarry Smith   *v = ts->vec_costintegral;
248636eaed60SHong Zhang   PetscFunctionReturn(0);
248736eaed60SHong Zhang }
248836eaed60SHong Zhang 
248936eaed60SHong Zhang #undef __FUNCT__
2490d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand"
24916fd0887fSHong Zhang /*@
24922c39e106SBarry Smith    TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions.
24936fd0887fSHong Zhang 
24946fd0887fSHong Zhang    Input Parameters:
24956fd0887fSHong Zhang +  ts - the TS context
24966fd0887fSHong Zhang .  t - current time
249705755b9cSHong Zhang -  U - state vector
24986fd0887fSHong Zhang 
24996fd0887fSHong Zhang    Output Parameter:
25006fd0887fSHong Zhang .  q - vector of size numberadjs to hold the outputs
25016fd0887fSHong Zhang 
25026fd0887fSHong Zhang    Note:
25036fd0887fSHong Zhang    Most users should not need to explicitly call this routine, as it
25046fd0887fSHong Zhang    is used internally within the sensitivity analysis context.
25056fd0887fSHong Zhang 
25066fd0887fSHong Zhang    Level: developer
25076fd0887fSHong Zhang 
25086fd0887fSHong Zhang .keywords: TS, compute
25096fd0887fSHong Zhang 
2510d4aa0a58SBarry Smith .seealso: TSAdjointSetCostIntegrand()
25116fd0887fSHong Zhang @*/
2512d4aa0a58SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec U,Vec q)
25136fd0887fSHong Zhang {
25146fd0887fSHong Zhang   PetscErrorCode ierr;
25156fd0887fSHong Zhang 
25166fd0887fSHong Zhang   PetscFunctionBegin;
25176fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25186fd0887fSHong Zhang   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
25196fd0887fSHong Zhang   PetscValidHeaderSpecific(q,VEC_CLASSID,4);
25206fd0887fSHong Zhang 
25216fd0887fSHong Zhang   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,q,0);CHKERRQ(ierr);
2522e4680132SHong Zhang   if (ts->costintegrand) {
2523e4680132SHong Zhang     PetscStackPush("TS user integrand in the cost function");
252405755b9cSHong Zhang     ierr = (*ts->costintegrand)(ts,t,U,q,ts->costintegrandctx);CHKERRQ(ierr);
25256fd0887fSHong Zhang     PetscStackPop;
25266fd0887fSHong Zhang   } else {
25276fd0887fSHong Zhang     ierr = VecZeroEntries(q);CHKERRQ(ierr);
25286fd0887fSHong Zhang   }
25296fd0887fSHong Zhang 
25306fd0887fSHong Zhang   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,q,0);CHKERRQ(ierr);
25316fd0887fSHong Zhang   PetscFunctionReturn(0);
25326fd0887fSHong Zhang }
25336fd0887fSHong Zhang 
25346fd0887fSHong Zhang #undef __FUNCT__
2535d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction"
253605755b9cSHong Zhang /*@
2537d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function.
253805755b9cSHong Zhang 
253905755b9cSHong Zhang   Collective on TS
254005755b9cSHong Zhang 
254105755b9cSHong Zhang   Input Parameters:
254205755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
254305755b9cSHong Zhang 
254405755b9cSHong Zhang   Notes:
2545d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation,
254605755b9cSHong Zhang   so most users would not generally call this routine themselves.
254705755b9cSHong Zhang 
254805755b9cSHong Zhang   Level: developer
254905755b9cSHong Zhang 
255005755b9cSHong Zhang .keywords: TS, sensitivity
2551d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction()
255205755b9cSHong Zhang @*/
2553d4aa0a58SBarry Smith PetscErrorCode  TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec X,Vec *drdy)
255405755b9cSHong Zhang {
255505755b9cSHong Zhang   PetscErrorCode ierr;
255605755b9cSHong Zhang 
255705755b9cSHong Zhang   PetscFunctionBegin;
255805755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
255905755b9cSHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
256005755b9cSHong Zhang 
256105755b9cSHong Zhang   PetscStackPush("TS user DRDY function for sensitivity analysis");
2562b612ec54SBarry Smith   ierr = (*ts->drdyfunction)(ts,t,X,drdy,ts->costintegrandctx); CHKERRQ(ierr);
256305755b9cSHong Zhang   PetscStackPop;
256405755b9cSHong Zhang   PetscFunctionReturn(0);
256505755b9cSHong Zhang }
256605755b9cSHong Zhang 
256705755b9cSHong Zhang #undef __FUNCT__
2568d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction"
256905755b9cSHong Zhang /*@
2570d4aa0a58SBarry Smith   TSAdjointComputeDRDPFunction - Runs the user-defined DRDP 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:
257805755b9cSHong Zhang   TSDRDPFunction() 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: TSAdjointSetDRDPFunction()
258505755b9cSHong Zhang @*/
2586d4aa0a58SBarry Smith PetscErrorCode  TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec X,Vec *drdp)
258705755b9cSHong Zhang {
258805755b9cSHong Zhang   PetscErrorCode ierr;
258905755b9cSHong Zhang 
259005755b9cSHong Zhang   PetscFunctionBegin;
259105755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
259205755b9cSHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
259305755b9cSHong Zhang 
259405755b9cSHong Zhang   PetscStackPush("TS user DRDP function for sensitivity analysis");
2595b612ec54SBarry Smith   ierr = (*ts->drdpfunction)(ts,t,X,drdp,ts->costintegrandctx); CHKERRQ(ierr);
259605755b9cSHong Zhang   PetscStackPop;
259705755b9cSHong Zhang   PetscFunctionReturn(0);
259805755b9cSHong Zhang }
259905755b9cSHong Zhang 
260005755b9cSHong Zhang #undef __FUNCT__
2601e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2602ac226902SBarry Smith /*@C
2603000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
26043f2090d5SJed Brown   called once at the beginning of each time step.
2605000e7ae3SMatthew Knepley 
26063f9fe445SBarry Smith   Logically Collective on TS
2607000e7ae3SMatthew Knepley 
2608000e7ae3SMatthew Knepley   Input Parameters:
2609000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2610000e7ae3SMatthew Knepley - func - The function
2611000e7ae3SMatthew Knepley 
2612000e7ae3SMatthew Knepley   Calling sequence of func:
2613000e7ae3SMatthew Knepley . func (TS ts);
2614000e7ae3SMatthew Knepley 
2615000e7ae3SMatthew Knepley   Level: intermediate
2616000e7ae3SMatthew Knepley 
2617b8123daeSJed Brown   Note:
2618b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2619b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2620b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2621b8123daeSJed Brown 
2622000e7ae3SMatthew Knepley .keywords: TS, timestep
26239be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep()
2624000e7ae3SMatthew Knepley @*/
26257087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2626000e7ae3SMatthew Knepley {
2627000e7ae3SMatthew Knepley   PetscFunctionBegin;
26280700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2629ae60f76fSBarry Smith   ts->prestep = func;
2630000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2631000e7ae3SMatthew Knepley }
2632000e7ae3SMatthew Knepley 
2633e74ef692SMatthew Knepley #undef __FUNCT__
26343f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
263509ee8438SJed Brown /*@
26363f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
26373f2090d5SJed Brown 
26383f2090d5SJed Brown   Collective on TS
26393f2090d5SJed Brown 
26403f2090d5SJed Brown   Input Parameters:
26413f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
26423f2090d5SJed Brown 
26433f2090d5SJed Brown   Notes:
26443f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
26453f2090d5SJed Brown   so most users would not generally call this routine themselves.
26463f2090d5SJed Brown 
26473f2090d5SJed Brown   Level: developer
26483f2090d5SJed Brown 
26493f2090d5SJed Brown .keywords: TS, timestep
26509be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
26513f2090d5SJed Brown @*/
26527087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
26533f2090d5SJed Brown {
26543f2090d5SJed Brown   PetscErrorCode ierr;
26553f2090d5SJed Brown 
26563f2090d5SJed Brown   PetscFunctionBegin;
26570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2658ae60f76fSBarry Smith   if (ts->prestep) {
2659ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
2660312ce896SJed Brown   }
26613f2090d5SJed Brown   PetscFunctionReturn(0);
26623f2090d5SJed Brown }
26633f2090d5SJed Brown 
26643f2090d5SJed Brown #undef __FUNCT__
2665b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2666b8123daeSJed Brown /*@C
2667b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2668b8123daeSJed Brown   called once at the beginning of each stage.
2669b8123daeSJed Brown 
2670b8123daeSJed Brown   Logically Collective on TS
2671b8123daeSJed Brown 
2672b8123daeSJed Brown   Input Parameters:
2673b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2674b8123daeSJed Brown - func - The function
2675b8123daeSJed Brown 
2676b8123daeSJed Brown   Calling sequence of func:
2677b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2678b8123daeSJed Brown 
2679b8123daeSJed Brown   Level: intermediate
2680b8123daeSJed Brown 
2681b8123daeSJed Brown   Note:
2682b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2683b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2684b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2685b8123daeSJed Brown 
2686b8123daeSJed Brown .keywords: TS, timestep
26879be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2688b8123daeSJed Brown @*/
2689b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2690b8123daeSJed Brown {
2691b8123daeSJed Brown   PetscFunctionBegin;
2692b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2693ae60f76fSBarry Smith   ts->prestage = func;
2694b8123daeSJed Brown   PetscFunctionReturn(0);
2695b8123daeSJed Brown }
2696b8123daeSJed Brown 
2697b8123daeSJed Brown #undef __FUNCT__
26989be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage"
26999be3e283SDebojyoti Ghosh /*@C
27009be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
27019be3e283SDebojyoti Ghosh   called once at the end of each stage.
27029be3e283SDebojyoti Ghosh 
27039be3e283SDebojyoti Ghosh   Logically Collective on TS
27049be3e283SDebojyoti Ghosh 
27059be3e283SDebojyoti Ghosh   Input Parameters:
27069be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
27079be3e283SDebojyoti Ghosh - func - The function
27089be3e283SDebojyoti Ghosh 
27099be3e283SDebojyoti Ghosh   Calling sequence of func:
27109be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
27119be3e283SDebojyoti Ghosh 
27129be3e283SDebojyoti Ghosh   Level: intermediate
27139be3e283SDebojyoti Ghosh 
27149be3e283SDebojyoti Ghosh   Note:
27159be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
27169be3e283SDebojyoti Ghosh   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
27179be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
27189be3e283SDebojyoti Ghosh 
27199be3e283SDebojyoti Ghosh .keywords: TS, timestep
27209be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
27219be3e283SDebojyoti Ghosh @*/
27229be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
27239be3e283SDebojyoti Ghosh {
27249be3e283SDebojyoti Ghosh   PetscFunctionBegin;
27259be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
27269be3e283SDebojyoti Ghosh   ts->poststage = func;
27279be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
27289be3e283SDebojyoti Ghosh }
27299be3e283SDebojyoti Ghosh 
27309be3e283SDebojyoti Ghosh #undef __FUNCT__
2731b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2732b8123daeSJed Brown /*@
2733b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2734b8123daeSJed Brown 
2735b8123daeSJed Brown   Collective on TS
2736b8123daeSJed Brown 
2737b8123daeSJed Brown   Input Parameters:
2738b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
27399be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
2740b8123daeSJed Brown 
2741b8123daeSJed Brown   Notes:
2742b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2743b8123daeSJed Brown   most users would not generally call this routine themselves.
2744b8123daeSJed Brown 
2745b8123daeSJed Brown   Level: developer
2746b8123daeSJed Brown 
2747b8123daeSJed Brown .keywords: TS, timestep
27489be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
2749b8123daeSJed Brown @*/
2750b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2751b8123daeSJed Brown {
2752b8123daeSJed Brown   PetscErrorCode ierr;
2753b8123daeSJed Brown 
2754b8123daeSJed Brown   PetscFunctionBegin;
2755b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2756ae60f76fSBarry Smith   if (ts->prestage) {
2757ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
2758b8123daeSJed Brown   }
2759b8123daeSJed Brown   PetscFunctionReturn(0);
2760b8123daeSJed Brown }
2761b8123daeSJed Brown 
2762b8123daeSJed Brown #undef __FUNCT__
27639be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage"
27649be3e283SDebojyoti Ghosh /*@
27659be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
27669be3e283SDebojyoti Ghosh 
27679be3e283SDebojyoti Ghosh   Collective on TS
27689be3e283SDebojyoti Ghosh 
27699be3e283SDebojyoti Ghosh   Input Parameters:
27709be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
27719be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
27729be3e283SDebojyoti Ghosh   stageindex  - Stage number
27739be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
27749be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
27759be3e283SDebojyoti Ghosh 
27769be3e283SDebojyoti Ghosh   Notes:
27779be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
27789be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
27799be3e283SDebojyoti Ghosh 
27809be3e283SDebojyoti Ghosh   Level: developer
27819be3e283SDebojyoti Ghosh 
27829be3e283SDebojyoti Ghosh .keywords: TS, timestep
27839be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
27849be3e283SDebojyoti Ghosh @*/
27859be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
27869be3e283SDebojyoti Ghosh {
27879be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
27889be3e283SDebojyoti Ghosh 
27899be3e283SDebojyoti Ghosh   PetscFunctionBegin;
27909be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27914beae5d8SLisandro Dalcin   if (ts->poststage) {
27929be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
27939be3e283SDebojyoti Ghosh   }
27949be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
27959be3e283SDebojyoti Ghosh }
27969be3e283SDebojyoti Ghosh 
27979be3e283SDebojyoti Ghosh #undef __FUNCT__
2798e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2799ac226902SBarry Smith /*@C
2800000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
28013f2090d5SJed Brown   called once at the end of each time step.
2802000e7ae3SMatthew Knepley 
28033f9fe445SBarry Smith   Logically Collective on TS
2804000e7ae3SMatthew Knepley 
2805000e7ae3SMatthew Knepley   Input Parameters:
2806000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2807000e7ae3SMatthew Knepley - func - The function
2808000e7ae3SMatthew Knepley 
2809000e7ae3SMatthew Knepley   Calling sequence of func:
2810b8123daeSJed Brown $ func (TS ts);
2811000e7ae3SMatthew Knepley 
2812000e7ae3SMatthew Knepley   Level: intermediate
2813000e7ae3SMatthew Knepley 
2814000e7ae3SMatthew Knepley .keywords: TS, timestep
2815b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2816000e7ae3SMatthew Knepley @*/
28177087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2818000e7ae3SMatthew Knepley {
2819000e7ae3SMatthew Knepley   PetscFunctionBegin;
28200700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2821ae60f76fSBarry Smith   ts->poststep = func;
2822000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2823000e7ae3SMatthew Knepley }
2824000e7ae3SMatthew Knepley 
2825e74ef692SMatthew Knepley #undef __FUNCT__
28263f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
282709ee8438SJed Brown /*@
28283f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
28293f2090d5SJed Brown 
28303f2090d5SJed Brown   Collective on TS
28313f2090d5SJed Brown 
28323f2090d5SJed Brown   Input Parameters:
28333f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
28343f2090d5SJed Brown 
28353f2090d5SJed Brown   Notes:
28363f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
28373f2090d5SJed Brown   so most users would not generally call this routine themselves.
28383f2090d5SJed Brown 
28393f2090d5SJed Brown   Level: developer
28403f2090d5SJed Brown 
28413f2090d5SJed Brown .keywords: TS, timestep
28423f2090d5SJed Brown @*/
28437087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
28443f2090d5SJed Brown {
28453f2090d5SJed Brown   PetscErrorCode ierr;
28463f2090d5SJed Brown 
28473f2090d5SJed Brown   PetscFunctionBegin;
28480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2849ae60f76fSBarry Smith   if (ts->poststep) {
2850ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
285172ac3e02SJed Brown   }
28523f2090d5SJed Brown   PetscFunctionReturn(0);
28533f2090d5SJed Brown }
28543f2090d5SJed Brown 
2855d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2856d763cef2SBarry Smith 
28574a2ae208SSatish Balay #undef __FUNCT__
2858a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2859d763cef2SBarry Smith /*@C
2860a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2861d763cef2SBarry Smith    timestep to display the iteration's  progress.
2862d763cef2SBarry Smith 
28633f9fe445SBarry Smith    Logically Collective on TS
2864d763cef2SBarry Smith 
2865d763cef2SBarry Smith    Input Parameters:
2866d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2867e213d8f1SJed Brown .  monitor - monitoring routine
2868329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
28690298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
2870b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
28710298fd71SBarry Smith           (may be NULL)
2872d763cef2SBarry Smith 
2873e213d8f1SJed Brown    Calling sequence of monitor:
28740910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2875d763cef2SBarry Smith 
2876d763cef2SBarry Smith +    ts - the TS context
287788c05cc5SBarry 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
287888c05cc5SBarry Smith                                been interpolated to)
28791f06c33eSBarry Smith .    time - current time
28800910c330SBarry Smith .    u - current iterate
2881d763cef2SBarry Smith -    mctx - [optional] monitoring context
2882d763cef2SBarry Smith 
2883d763cef2SBarry Smith    Notes:
2884d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2885d763cef2SBarry Smith    already has been loaded.
2886d763cef2SBarry Smith 
2887025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2888025f1a04SBarry Smith 
2889d763cef2SBarry Smith    Level: intermediate
2890d763cef2SBarry Smith 
2891d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2892d763cef2SBarry Smith 
2893a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2894d763cef2SBarry Smith @*/
2895c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2896d763cef2SBarry Smith {
2897d763cef2SBarry Smith   PetscFunctionBegin;
28980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
289917186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2900d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
29018704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2902d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2903d763cef2SBarry Smith   PetscFunctionReturn(0);
2904d763cef2SBarry Smith }
2905d763cef2SBarry Smith 
29064a2ae208SSatish Balay #undef __FUNCT__
2907a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2908d763cef2SBarry Smith /*@C
2909a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2910d763cef2SBarry Smith 
29113f9fe445SBarry Smith    Logically Collective on TS
2912d763cef2SBarry Smith 
2913d763cef2SBarry Smith    Input Parameters:
2914d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2915d763cef2SBarry Smith 
2916d763cef2SBarry Smith    Notes:
2917d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2918d763cef2SBarry Smith 
2919d763cef2SBarry Smith    Level: intermediate
2920d763cef2SBarry Smith 
2921d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2922d763cef2SBarry Smith 
2923a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2924d763cef2SBarry Smith @*/
29257087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2926d763cef2SBarry Smith {
2927d952e501SBarry Smith   PetscErrorCode ierr;
2928d952e501SBarry Smith   PetscInt       i;
2929d952e501SBarry Smith 
2930d763cef2SBarry Smith   PetscFunctionBegin;
29310700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2932d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
29338704b422SBarry Smith     if (ts->monitordestroy[i]) {
29348704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2935d952e501SBarry Smith     }
2936d952e501SBarry Smith   }
2937d763cef2SBarry Smith   ts->numbermonitors = 0;
2938d763cef2SBarry Smith   PetscFunctionReturn(0);
2939d763cef2SBarry Smith }
2940d763cef2SBarry Smith 
29414a2ae208SSatish Balay #undef __FUNCT__
2942a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2943d8e5e3e6SSatish Balay /*@
2944a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
29455516499fSSatish Balay 
29465516499fSSatish Balay    Level: intermediate
294741251cbbSSatish Balay 
29485516499fSSatish Balay .keywords: TS, set, monitor
29495516499fSSatish Balay 
295041251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
295141251cbbSSatish Balay @*/
2952649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2953d763cef2SBarry Smith {
2954dfbe8321SBarry Smith   PetscErrorCode ierr;
2955ce94432eSBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts));
2956d132466eSBarry Smith 
2957d763cef2SBarry Smith   PetscFunctionBegin;
2958649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2959971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2960649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2961d763cef2SBarry Smith   PetscFunctionReturn(0);
2962d763cef2SBarry Smith }
2963d763cef2SBarry Smith 
29644a2ae208SSatish Balay #undef __FUNCT__
2965cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2966cd652676SJed Brown /*@
2967cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2968cd652676SJed Brown 
2969cd652676SJed Brown    Logically Collective on TS
2970cd652676SJed Brown 
2971cd652676SJed Brown    Input Argument:
2972cd652676SJed Brown .  ts - time stepping context
2973cd652676SJed Brown 
2974cd652676SJed Brown    Output Argument:
2975cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2976cd652676SJed Brown 
2977cd652676SJed Brown    Level: intermediate
2978cd652676SJed Brown 
2979cd652676SJed Brown .keywords: TS, set
2980cd652676SJed Brown 
2981cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2982cd652676SJed Brown @*/
2983cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2984cd652676SJed Brown {
2985cd652676SJed Brown   PetscFunctionBegin;
2986cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2987cd652676SJed Brown   ts->retain_stages = flg;
2988cd652676SJed Brown   PetscFunctionReturn(0);
2989cd652676SJed Brown }
2990cd652676SJed Brown 
2991cd652676SJed Brown #undef __FUNCT__
2992cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2993cd652676SJed Brown /*@
2994cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2995cd652676SJed Brown 
2996cd652676SJed Brown    Collective on TS
2997cd652676SJed Brown 
2998cd652676SJed Brown    Input Argument:
2999cd652676SJed Brown +  ts - time stepping context
3000cd652676SJed Brown -  t - time to interpolate to
3001cd652676SJed Brown 
3002cd652676SJed Brown    Output Argument:
30030910c330SBarry Smith .  U - state at given time
3004cd652676SJed Brown 
3005cd652676SJed Brown    Notes:
3006cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
3007cd652676SJed Brown 
3008cd652676SJed Brown    Level: intermediate
3009cd652676SJed Brown 
3010cd652676SJed Brown    Developer Notes:
3011cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3012cd652676SJed Brown 
3013cd652676SJed Brown .keywords: TS, set
3014cd652676SJed Brown 
3015cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
3016cd652676SJed Brown @*/
30170910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3018cd652676SJed Brown {
3019cd652676SJed Brown   PetscErrorCode ierr;
3020cd652676SJed Brown 
3021cd652676SJed Brown   PetscFunctionBegin;
3022cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3023b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
30246712e2f1SBarry 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);
3025ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
30260910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3027cd652676SJed Brown   PetscFunctionReturn(0);
3028cd652676SJed Brown }
3029cd652676SJed Brown 
3030cd652676SJed Brown #undef __FUNCT__
30314a2ae208SSatish Balay #define __FUNCT__ "TSStep"
3032d763cef2SBarry Smith /*@
30336d9e5789SSean Farley    TSStep - Steps one time step
3034d763cef2SBarry Smith 
3035d763cef2SBarry Smith    Collective on TS
3036d763cef2SBarry Smith 
3037d763cef2SBarry Smith    Input Parameter:
3038d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3039d763cef2SBarry Smith 
304027829d71SBarry Smith    Level: developer
3041d763cef2SBarry Smith 
3042b8123daeSJed Brown    Notes:
304327829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
304427829d71SBarry Smith 
3045b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3046b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3047b8123daeSJed Brown 
304825cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
304925cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
305025cb2221SBarry Smith 
3051d763cef2SBarry Smith .keywords: TS, timestep, solve
3052d763cef2SBarry Smith 
30539be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3054d763cef2SBarry Smith @*/
3055193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3056d763cef2SBarry Smith {
30572fb8446cSMatthew G. Knepley   DM               dm;
3058dfbe8321SBarry Smith   PetscErrorCode   ierr;
3059fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3060d763cef2SBarry Smith 
3061d763cef2SBarry Smith   PetscFunctionBegin;
30620700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3063fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3064fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3065fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3066fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3067fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3068fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3069fffbeea8SBarry Smith                                 "  year        = {2014}\n}\n",&cite);
3070fffbeea8SBarry Smith 
30712fb8446cSMatthew G. Knepley   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
3072d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
3073d405a339SMatthew Knepley 
3074362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
307524655328SShri   ts->ptime_prev = ts->ptime;
3076cdb7a50dSMatthew G. Knepley   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3077bbd56ea5SKarl Rupp 
3078e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3079d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3080193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3081d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3082bbd56ea5SKarl Rupp 
308324655328SShri   ts->time_step_prev = ts->ptime - ts->ptime_prev;
3084cdb7a50dSMatthew G. Knepley   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3085362cd11cSLisandro Dalcin 
3086362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3087cef5090cSJed Brown     if (ts->errorifstepfailed) {
308808c7845fSBarry 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]);
308908c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3090d2daff3dSHong Zhang     }
309108c7845fSBarry Smith   } else if (!ts->reason) {
309208c7845fSBarry Smith     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
309308c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
309408c7845fSBarry Smith   }
30952c18e0fdSBarry Smith   ts->total_steps++;
309608c7845fSBarry Smith   PetscFunctionReturn(0);
309708c7845fSBarry Smith }
309808c7845fSBarry Smith 
309908c7845fSBarry Smith #undef __FUNCT__
310008c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep"
310108c7845fSBarry Smith /*@
310208c7845fSBarry Smith    TSAdjointStep - Steps one time step
310308c7845fSBarry Smith 
310408c7845fSBarry Smith    Collective on TS
310508c7845fSBarry Smith 
310608c7845fSBarry Smith    Input Parameter:
310708c7845fSBarry Smith .  ts - the TS context obtained from TSCreate()
310808c7845fSBarry Smith 
310908c7845fSBarry Smith    Level: intermediate
311008c7845fSBarry Smith 
311108c7845fSBarry Smith    Notes:
311208c7845fSBarry Smith    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
311308c7845fSBarry Smith    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
311408c7845fSBarry Smith 
311508c7845fSBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
311608c7845fSBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
311708c7845fSBarry Smith 
311808c7845fSBarry Smith .keywords: TS, timestep, solve
311908c7845fSBarry Smith 
312008c7845fSBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
312108c7845fSBarry Smith @*/
312208c7845fSBarry Smith PetscErrorCode  TSAdjointStep(TS ts)
312308c7845fSBarry Smith {
312408c7845fSBarry Smith   DM               dm;
312508c7845fSBarry Smith   PetscErrorCode   ierr;
312608c7845fSBarry Smith 
312708c7845fSBarry Smith   PetscFunctionBegin;
312808c7845fSBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
312908c7845fSBarry Smith   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
313008c7845fSBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
313108c7845fSBarry Smith 
313208c7845fSBarry Smith   ts->reason = TS_CONVERGED_ITERATING;
313308c7845fSBarry Smith   ts->ptime_prev = ts->ptime;
313408c7845fSBarry Smith   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
313508c7845fSBarry Smith   ierr = VecViewFromOptions(ts->vec_sol, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr);
313608c7845fSBarry Smith 
313708c7845fSBarry Smith   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
313842f2b339SBarry 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);
313942f2b339SBarry Smith   ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr);
3140362cd11cSLisandro Dalcin   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3141362cd11cSLisandro Dalcin 
3142cef5090cSJed Brown   ts->time_step_prev = ts->ptime - ts->ptime_prev;
3143cef5090cSJed Brown   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3144ce94432eSBarry Smith 
3145ce94432eSBarry Smith   if (ts->reason < 0) {
3146cef5090cSJed Brown     if (ts->errorifstepfailed) {
3147362cd11cSLisandro Dalcin       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
3148db4deed7SKarl Rupp         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]);
31492a3a10ceSLisandro Dalcin       } else if (ts->reason == TS_DIVERGED_STEP_REJECTED) {
31502a3a10ceSLisandro 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]);
3151362cd11cSLisandro Dalcin       } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3152362cd11cSLisandro Dalcin     }
3153362cd11cSLisandro Dalcin   } else if (!ts->reason) {
315473b18844SBarry Smith     if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
3155db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time)         ts->reason = TS_CONVERGED_TIME;
3156362cd11cSLisandro Dalcin   }
31572c18e0fdSBarry Smith   ts->total_steps--;
3158d763cef2SBarry Smith   PetscFunctionReturn(0);
3159d763cef2SBarry Smith }
3160d763cef2SBarry Smith 
31614a2ae208SSatish Balay #undef __FUNCT__
316205175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
316305175c85SJed Brown /*@
316405175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
316505175c85SJed Brown 
31661c3436cfSJed Brown    Collective on TS
316705175c85SJed Brown 
316805175c85SJed Brown    Input Arguments:
31691c3436cfSJed Brown +  ts - time stepping context
31701c3436cfSJed Brown .  order - desired order of accuracy
31710298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
317205175c85SJed Brown 
317305175c85SJed Brown    Output Arguments:
31740910c330SBarry Smith .  U - state at the end of the current step
317505175c85SJed Brown 
317605175c85SJed Brown    Level: advanced
317705175c85SJed Brown 
3178108c343cSJed Brown    Notes:
3179108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3180108c343cSJed 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.
3181108c343cSJed Brown 
31821c3436cfSJed Brown .seealso: TSStep(), TSAdapt
318305175c85SJed Brown @*/
31840910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
318505175c85SJed Brown {
318605175c85SJed Brown   PetscErrorCode ierr;
318705175c85SJed Brown 
318805175c85SJed Brown   PetscFunctionBegin;
318905175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
319005175c85SJed Brown   PetscValidType(ts,1);
31910910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3192ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
31930910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
319405175c85SJed Brown   PetscFunctionReturn(0);
319505175c85SJed Brown }
319605175c85SJed Brown 
3197d67e68b7SBarry Smith 
319805175c85SJed Brown #undef __FUNCT__
31996a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
32006a4d4014SLisandro Dalcin /*@
32016a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
32026a4d4014SLisandro Dalcin 
32036a4d4014SLisandro Dalcin    Collective on TS
32046a4d4014SLisandro Dalcin 
32056a4d4014SLisandro Dalcin    Input Parameter:
32066a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3207cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
32085a3a76d0SJed Brown 
32096a4d4014SLisandro Dalcin    Level: beginner
32106a4d4014SLisandro Dalcin 
32115a3a76d0SJed Brown    Notes:
32125a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
32135a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
32145a3a76d0SJed Brown    stepped over the final time.
32155a3a76d0SJed Brown 
32166a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
32176a4d4014SLisandro Dalcin 
32186a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
32196a4d4014SLisandro Dalcin @*/
3220cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
32216a4d4014SLisandro Dalcin {
3222b06615a5SLisandro Dalcin   Vec               solution;
32236a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
3224f22f69f0SBarry Smith 
32256a4d4014SLisandro Dalcin   PetscFunctionBegin;
32260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3227f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
322849354f04SShri 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 */
3229b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
32300910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3231b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3232b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3233b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
32345a3a76d0SJed Brown     }
32350910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3236bbd56ea5SKarl Rupp   } else if (u) {
32370910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
32385a3a76d0SJed Brown   }
3239d2daff3dSHong Zhang   ierr = TSSetUp(ts);CHKERRQ(ierr); /*compute adj coefficients if the reverse mode is on*/
32406a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
3241193ac0bcSJed Brown   ts->steps             = 0;
32425ef26d82SJed Brown   ts->ksp_its           = 0;
32435ef26d82SJed Brown   ts->snes_its          = 0;
3244c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
3245c610991cSLisandro Dalcin   ts->reject            = 0;
3246193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
3247193ac0bcSJed Brown 
3248ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3249*ec8db0baSMatthew G. Knepley   {
3250*ec8db0baSMatthew G. Knepley     DM dm;
3251*ec8db0baSMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
3252*ec8db0baSMatthew G. Knepley     ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3253*ec8db0baSMatthew G. Knepley   }
3254f05ece33SBarry Smith 
3255193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
3256193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
32570910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
3258cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3259193ac0bcSJed Brown   } else {
32606a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
3261db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
3262db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3263e1a7a14fSJed Brown     while (!ts->reason) {
3264b06615a5SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
3265bc952696SBarry Smith       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
3266193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
3267aeb4809dSShri Abhyankar       if (ts->event) {
3268aeb4809dSShri Abhyankar 	ierr = TSEventMonitor(ts);CHKERRQ(ierr);
32691eda64f1SShri Abhyankar 	if (ts->event->status != TSEVENT_PROCESSING) {
32701eda64f1SShri Abhyankar 	  ierr = TSPostStep(ts);CHKERRQ(ierr);
32711eda64f1SShri Abhyankar 	}
32721eda64f1SShri Abhyankar       } else {
32731eda64f1SShri Abhyankar 	ierr = TSPostStep(ts);CHKERRQ(ierr);
3274aeb4809dSShri Abhyankar       }
3275193ac0bcSJed Brown     }
327649354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
32770910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
3278cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3279b06615a5SLisandro Dalcin       solution = u;
32800574a7fbSJed Brown     } else {
3281ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3282cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3283b06615a5SLisandro Dalcin       solution = ts->vec_sol;
32840574a7fbSJed Brown     }
3285bc952696SBarry Smith     ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr);
3286b06615a5SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr);
328727829d71SBarry Smith     ierr = VecViewFromOptions(solution, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr);
3288193ac0bcSJed Brown   }
3289d2daff3dSHong Zhang 
3290ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
329156f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
32926a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
32936a4d4014SLisandro Dalcin }
32946a4d4014SLisandro Dalcin 
32956a4d4014SLisandro Dalcin #undef __FUNCT__
3296c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve"
3297c88f2d44SBarry Smith /*@
3298c88f2d44SBarry Smith    TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE
3299c88f2d44SBarry Smith 
3300c88f2d44SBarry Smith    Collective on TS
3301c88f2d44SBarry Smith 
3302c88f2d44SBarry Smith    Input Parameter:
33032c39e106SBarry Smith .  ts - the TS context obtained from TSCreate()
3304c88f2d44SBarry Smith 
3305c88f2d44SBarry Smith    Level: intermediate
3306c88f2d44SBarry Smith 
3307c88f2d44SBarry Smith    Notes:
3308c88f2d44SBarry Smith    This must be called after a call to TSSolve() that solves the forward problem
3309c88f2d44SBarry Smith 
331073b18844SBarry Smith    By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time
331173b18844SBarry Smith 
3312c88f2d44SBarry Smith .keywords: TS, timestep, solve
3313c88f2d44SBarry Smith 
3314c88f2d44SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep()
3315c88f2d44SBarry Smith @*/
33162c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts)
3317c88f2d44SBarry Smith {
3318c88f2d44SBarry Smith   PetscErrorCode    ierr;
3319c88f2d44SBarry Smith 
3320c88f2d44SBarry Smith   PetscFunctionBegin;
3321c88f2d44SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3322c88f2d44SBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
3323c88f2d44SBarry Smith   /* reset time step and iteration counters */
3324c88f2d44SBarry Smith   ts->steps             = 0;
3325c88f2d44SBarry Smith   ts->ksp_its           = 0;
3326c88f2d44SBarry Smith   ts->snes_its          = 0;
3327c88f2d44SBarry Smith   ts->num_snes_failures = 0;
3328c88f2d44SBarry Smith   ts->reject            = 0;
3329c88f2d44SBarry Smith   ts->reason            = TS_CONVERGED_ITERATING;
3330c88f2d44SBarry Smith 
333173b18844SBarry Smith   if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps;
3332c88f2d44SBarry Smith 
333373b18844SBarry Smith   if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
3334c88f2d44SBarry Smith   while (!ts->reason) {
333573b18844SBarry Smith     ierr = TSTrajectoryGet(ts->trajectory,ts,ts->adjoint_max_steps-ts->steps,ts->ptime);CHKERRQ(ierr);
333673b18844SBarry Smith     ierr = TSMonitor(ts,ts->adjoint_max_steps-ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
333708c7845fSBarry Smith     ierr = TSAdjointStep(ts);CHKERRQ(ierr);
3338c88f2d44SBarry Smith     if (ts->event) {
3339c88f2d44SBarry Smith       ierr = TSEventMonitor(ts);CHKERRQ(ierr);
3340c88f2d44SBarry Smith       if (ts->event->status != TSEVENT_PROCESSING) {
3341c88f2d44SBarry Smith         ierr = TSPostStep(ts);CHKERRQ(ierr);
3342c88f2d44SBarry Smith       }
3343c88f2d44SBarry Smith     } else {
3344c88f2d44SBarry Smith       ierr = TSPostStep(ts);CHKERRQ(ierr);
3345c88f2d44SBarry Smith     }
3346c88f2d44SBarry Smith   }
3347c88f2d44SBarry Smith   ts->solvetime = ts->ptime;
3348c88f2d44SBarry Smith   PetscFunctionReturn(0);
3349c88f2d44SBarry Smith }
3350c88f2d44SBarry Smith 
3351c88f2d44SBarry Smith #undef __FUNCT__
33524a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
3353228d79bcSJed Brown /*@
3354228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
3355228d79bcSJed Brown 
3356228d79bcSJed Brown    Collective on TS
3357228d79bcSJed Brown 
3358228d79bcSJed Brown    Input Parameters:
3359228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
3360228d79bcSJed Brown .  step - step number that has just completed
3361228d79bcSJed Brown .  ptime - model time of the state
33620910c330SBarry Smith -  u - state at the current model time
3363228d79bcSJed Brown 
3364228d79bcSJed Brown    Notes:
3365228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
3366228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
3367228d79bcSJed Brown 
3368228d79bcSJed Brown    Level: advanced
3369228d79bcSJed Brown 
3370228d79bcSJed Brown .keywords: TS, timestep
3371228d79bcSJed Brown @*/
33720910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
3373d763cef2SBarry Smith {
33746849ba73SBarry Smith   PetscErrorCode ierr;
3375a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
3376d763cef2SBarry Smith 
3377d763cef2SBarry Smith   PetscFunctionBegin;
3378b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3379b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
33805edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
3381d763cef2SBarry Smith   for (i=0; i<n; i++) {
33820910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
3383d763cef2SBarry Smith   }
33845edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
3385d763cef2SBarry Smith   PetscFunctionReturn(0);
3386d763cef2SBarry Smith }
3387d763cef2SBarry Smith 
3388d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
33894a2ae208SSatish Balay #undef __FUNCT__
3390a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
3391d763cef2SBarry Smith /*@C
3392a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
3393a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
3394d763cef2SBarry Smith 
3395d763cef2SBarry Smith    Collective on TS
3396d763cef2SBarry Smith 
3397d763cef2SBarry Smith    Input Parameters:
3398d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
3399d763cef2SBarry Smith .  label - the title to put in the title bar
34007c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
3401a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
3402a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
3403d763cef2SBarry Smith 
3404d763cef2SBarry Smith    Output Parameter:
34050b039ecaSBarry Smith .  ctx - the context
3406d763cef2SBarry Smith 
3407d763cef2SBarry Smith    Options Database Key:
34084f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
34094f09c107SBarry Smith .  -ts_monitor_lg_solution -
341099fdda47SBarry Smith .  -ts_monitor_lg_error -
341199fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
341299fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
341399fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
3414d763cef2SBarry Smith 
3415d763cef2SBarry Smith    Notes:
3416a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
3417d763cef2SBarry Smith 
3418d763cef2SBarry Smith    Level: intermediate
3419d763cef2SBarry Smith 
34207c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
3421d763cef2SBarry Smith 
34224f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
34237c922b88SBarry Smith 
3424d763cef2SBarry Smith @*/
3425a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
3426d763cef2SBarry Smith {
3427b0a32e0cSBarry Smith   PetscDraw      win;
3428dfbe8321SBarry Smith   PetscErrorCode ierr;
3429d763cef2SBarry Smith 
3430d763cef2SBarry Smith   PetscFunctionBegin;
3431b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
3432a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
34333fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
34340b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
34353bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)(*ctx)->lg,(PetscObject)win);CHKERRQ(ierr);
3436287de1a7SBarry Smith   ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr);
3437287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
3438a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
3439d763cef2SBarry Smith   PetscFunctionReturn(0);
3440d763cef2SBarry Smith }
3441d763cef2SBarry Smith 
34424a2ae208SSatish Balay #undef __FUNCT__
34434f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
3444b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
3445d763cef2SBarry Smith {
34460b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
3447c32365f1SBarry Smith   PetscReal      x   = ptime,y;
3448dfbe8321SBarry Smith   PetscErrorCode ierr;
3449d763cef2SBarry Smith 
3450d763cef2SBarry Smith   PetscFunctionBegin;
3451b06615a5SLisandro Dalcin   if (!step) {
3452a9f9c1f6SBarry Smith     PetscDrawAxis axis;
3453a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
3454a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
3455a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
3456287de1a7SBarry Smith     ierr = PetscDrawLGIndicateDataPoints(ctx->lg,PETSC_TRUE);CHKERRQ(ierr);
3457a9f9c1f6SBarry Smith   }
3458c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
34590b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
3460b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
34610b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
34623923b477SBarry Smith   }
3463d763cef2SBarry Smith   PetscFunctionReturn(0);
3464d763cef2SBarry Smith }
3465d763cef2SBarry Smith 
34664a2ae208SSatish Balay #undef __FUNCT__
3467a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
3468d763cef2SBarry Smith /*@C
3469a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
3470a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
3471d763cef2SBarry Smith 
34720b039ecaSBarry Smith    Collective on TSMonitorLGCtx
3473d763cef2SBarry Smith 
3474d763cef2SBarry Smith    Input Parameter:
34750b039ecaSBarry Smith .  ctx - the monitor context
3476d763cef2SBarry Smith 
3477d763cef2SBarry Smith    Level: intermediate
3478d763cef2SBarry Smith 
3479d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
3480d763cef2SBarry Smith 
34814f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
3482d763cef2SBarry Smith @*/
3483a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
3484d763cef2SBarry Smith {
3485b0a32e0cSBarry Smith   PetscDraw      draw;
3486dfbe8321SBarry Smith   PetscErrorCode ierr;
3487d763cef2SBarry Smith 
3488d763cef2SBarry Smith   PetscFunctionBegin;
34890b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
34906bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
34910b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
34920b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
3493d763cef2SBarry Smith   PetscFunctionReturn(0);
3494d763cef2SBarry Smith }
3495d763cef2SBarry Smith 
34964a2ae208SSatish Balay #undef __FUNCT__
34974a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
3498d763cef2SBarry Smith /*@
3499b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
3500d763cef2SBarry Smith 
3501d763cef2SBarry Smith    Not Collective
3502d763cef2SBarry Smith 
3503d763cef2SBarry Smith    Input Parameter:
3504d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3505d763cef2SBarry Smith 
3506d763cef2SBarry Smith    Output Parameter:
3507d763cef2SBarry Smith .  t  - the current time
3508d763cef2SBarry Smith 
3509d763cef2SBarry Smith    Level: beginner
3510d763cef2SBarry Smith 
3511b8123daeSJed Brown    Note:
3512b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
35139be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
3514b8123daeSJed Brown 
3515d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3516d763cef2SBarry Smith 
3517d763cef2SBarry Smith .keywords: TS, get, time
3518d763cef2SBarry Smith @*/
35197087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
3520d763cef2SBarry Smith {
3521d763cef2SBarry Smith   PetscFunctionBegin;
35220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3523f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
3524d763cef2SBarry Smith   *t = ts->ptime;
3525d763cef2SBarry Smith   PetscFunctionReturn(0);
3526d763cef2SBarry Smith }
3527d763cef2SBarry Smith 
35284a2ae208SSatish Balay #undef __FUNCT__
3529e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime"
3530e5e524a1SHong Zhang /*@
3531e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
3532e5e524a1SHong Zhang 
3533e5e524a1SHong Zhang    Not Collective
3534e5e524a1SHong Zhang 
3535e5e524a1SHong Zhang    Input Parameter:
3536e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
3537e5e524a1SHong Zhang 
3538e5e524a1SHong Zhang    Output Parameter:
3539e5e524a1SHong Zhang .  t  - the previous time
3540e5e524a1SHong Zhang 
3541e5e524a1SHong Zhang    Level: beginner
3542e5e524a1SHong Zhang 
3543e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3544e5e524a1SHong Zhang 
3545e5e524a1SHong Zhang .keywords: TS, get, time
3546e5e524a1SHong Zhang @*/
3547e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
3548e5e524a1SHong Zhang {
3549e5e524a1SHong Zhang   PetscFunctionBegin;
3550e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3551e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
3552e5e524a1SHong Zhang   *t = ts->ptime_prev;
3553e5e524a1SHong Zhang   PetscFunctionReturn(0);
3554e5e524a1SHong Zhang }
3555e5e524a1SHong Zhang 
3556e5e524a1SHong Zhang #undef __FUNCT__
35576a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
35586a4d4014SLisandro Dalcin /*@
35596a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
35606a4d4014SLisandro Dalcin 
35613f9fe445SBarry Smith    Logically Collective on TS
35626a4d4014SLisandro Dalcin 
35636a4d4014SLisandro Dalcin    Input Parameters:
35646a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
35656a4d4014SLisandro Dalcin -  time - the time
35666a4d4014SLisandro Dalcin 
35676a4d4014SLisandro Dalcin    Level: intermediate
35686a4d4014SLisandro Dalcin 
35696a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
35706a4d4014SLisandro Dalcin 
35716a4d4014SLisandro Dalcin .keywords: TS, set, time
35726a4d4014SLisandro Dalcin @*/
35737087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
35746a4d4014SLisandro Dalcin {
35756a4d4014SLisandro Dalcin   PetscFunctionBegin;
35760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3577c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
35786a4d4014SLisandro Dalcin   ts->ptime = t;
35796a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
35806a4d4014SLisandro Dalcin }
35816a4d4014SLisandro Dalcin 
35826a4d4014SLisandro Dalcin #undef __FUNCT__
35834a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
3584d763cef2SBarry Smith /*@C
3585d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
3586d763cef2SBarry Smith    TS options in the database.
3587d763cef2SBarry Smith 
35883f9fe445SBarry Smith    Logically Collective on TS
3589d763cef2SBarry Smith 
3590d763cef2SBarry Smith    Input Parameter:
3591d763cef2SBarry Smith +  ts     - The TS context
3592d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
3593d763cef2SBarry Smith 
3594d763cef2SBarry Smith    Notes:
3595d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3596d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3597d763cef2SBarry Smith    hyphen.
3598d763cef2SBarry Smith 
3599d763cef2SBarry Smith    Level: advanced
3600d763cef2SBarry Smith 
3601d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
3602d763cef2SBarry Smith 
3603d763cef2SBarry Smith .seealso: TSSetFromOptions()
3604d763cef2SBarry Smith 
3605d763cef2SBarry Smith @*/
36067087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
3607d763cef2SBarry Smith {
3608dfbe8321SBarry Smith   PetscErrorCode ierr;
3609089b2837SJed Brown   SNES           snes;
3610d763cef2SBarry Smith 
3611d763cef2SBarry Smith   PetscFunctionBegin;
36120700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3613d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3614089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3615089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3616d763cef2SBarry Smith   PetscFunctionReturn(0);
3617d763cef2SBarry Smith }
3618d763cef2SBarry Smith 
3619d763cef2SBarry Smith 
36204a2ae208SSatish Balay #undef __FUNCT__
36214a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
3622d763cef2SBarry Smith /*@C
3623d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
3624d763cef2SBarry Smith    TS options in the database.
3625d763cef2SBarry Smith 
36263f9fe445SBarry Smith    Logically Collective on TS
3627d763cef2SBarry Smith 
3628d763cef2SBarry Smith    Input Parameter:
3629d763cef2SBarry Smith +  ts     - The TS context
3630d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
3631d763cef2SBarry Smith 
3632d763cef2SBarry Smith    Notes:
3633d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3634d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3635d763cef2SBarry Smith    hyphen.
3636d763cef2SBarry Smith 
3637d763cef2SBarry Smith    Level: advanced
3638d763cef2SBarry Smith 
3639d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
3640d763cef2SBarry Smith 
3641d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
3642d763cef2SBarry Smith 
3643d763cef2SBarry Smith @*/
36447087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
3645d763cef2SBarry Smith {
3646dfbe8321SBarry Smith   PetscErrorCode ierr;
3647089b2837SJed Brown   SNES           snes;
3648d763cef2SBarry Smith 
3649d763cef2SBarry Smith   PetscFunctionBegin;
36500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3651d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3652089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3653089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3654d763cef2SBarry Smith   PetscFunctionReturn(0);
3655d763cef2SBarry Smith }
3656d763cef2SBarry Smith 
36574a2ae208SSatish Balay #undef __FUNCT__
36584a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
3659d763cef2SBarry Smith /*@C
3660d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
3661d763cef2SBarry Smith    TS options in the database.
3662d763cef2SBarry Smith 
3663d763cef2SBarry Smith    Not Collective
3664d763cef2SBarry Smith 
3665d763cef2SBarry Smith    Input Parameter:
3666d763cef2SBarry Smith .  ts - The TS context
3667d763cef2SBarry Smith 
3668d763cef2SBarry Smith    Output Parameter:
3669d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
3670d763cef2SBarry Smith 
3671d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
3672d763cef2SBarry Smith    sufficient length to hold the prefix.
3673d763cef2SBarry Smith 
3674d763cef2SBarry Smith    Level: intermediate
3675d763cef2SBarry Smith 
3676d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
3677d763cef2SBarry Smith 
3678d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
3679d763cef2SBarry Smith @*/
36807087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
3681d763cef2SBarry Smith {
3682dfbe8321SBarry Smith   PetscErrorCode ierr;
3683d763cef2SBarry Smith 
3684d763cef2SBarry Smith   PetscFunctionBegin;
36850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36864482741eSBarry Smith   PetscValidPointer(prefix,2);
3687d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3688d763cef2SBarry Smith   PetscFunctionReturn(0);
3689d763cef2SBarry Smith }
3690d763cef2SBarry Smith 
36914a2ae208SSatish Balay #undef __FUNCT__
36924a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
3693d763cef2SBarry Smith /*@C
3694d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
3695d763cef2SBarry Smith 
3696d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
3697d763cef2SBarry Smith 
3698d763cef2SBarry Smith    Input Parameter:
3699d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
3700d763cef2SBarry Smith 
3701d763cef2SBarry Smith    Output Parameters:
3702e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
3703e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
3704e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
3705e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
3706d763cef2SBarry Smith 
37070298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
3708d763cef2SBarry Smith 
3709d763cef2SBarry Smith    Level: intermediate
3710d763cef2SBarry Smith 
371126d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
3712d763cef2SBarry Smith 
3713d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
3714d763cef2SBarry Smith @*/
3715e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
3716d763cef2SBarry Smith {
3717089b2837SJed Brown   PetscErrorCode ierr;
3718089b2837SJed Brown   SNES           snes;
371924989b8cSPeter Brune   DM             dm;
3720089b2837SJed Brown 
3721d763cef2SBarry Smith   PetscFunctionBegin;
3722089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3723e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
372424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
372524989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
3726d763cef2SBarry Smith   PetscFunctionReturn(0);
3727d763cef2SBarry Smith }
3728d763cef2SBarry Smith 
37291713a123SBarry Smith #undef __FUNCT__
37302eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
37312eca1d9cSJed Brown /*@C
37322eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
37332eca1d9cSJed Brown 
37342eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
37352eca1d9cSJed Brown 
37362eca1d9cSJed Brown    Input Parameter:
37372eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
37382eca1d9cSJed Brown 
37392eca1d9cSJed Brown    Output Parameters:
3740e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
3741e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
37422eca1d9cSJed Brown .  f   - The function to compute the matrices
37432eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
37442eca1d9cSJed Brown 
37450298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
37462eca1d9cSJed Brown 
37472eca1d9cSJed Brown    Level: advanced
37482eca1d9cSJed Brown 
37492eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
37502eca1d9cSJed Brown 
37512eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
37522eca1d9cSJed Brown @*/
3753e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
37542eca1d9cSJed Brown {
3755089b2837SJed Brown   PetscErrorCode ierr;
3756089b2837SJed Brown   SNES           snes;
375724989b8cSPeter Brune   DM             dm;
37580910c330SBarry Smith 
37592eca1d9cSJed Brown   PetscFunctionBegin;
3760089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3761f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
3762e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
376324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
376424989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
37652eca1d9cSJed Brown   PetscFunctionReturn(0);
37662eca1d9cSJed Brown }
37672eca1d9cSJed Brown 
37686083293cSBarry Smith 
37692eca1d9cSJed Brown #undef __FUNCT__
377083a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
37711713a123SBarry Smith /*@C
377283a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
37731713a123SBarry Smith    VecView() for the solution at each timestep
37741713a123SBarry Smith 
37751713a123SBarry Smith    Collective on TS
37761713a123SBarry Smith 
37771713a123SBarry Smith    Input Parameters:
37781713a123SBarry Smith +  ts - the TS context
37791713a123SBarry Smith .  step - current time-step
3780142b95e3SSatish Balay .  ptime - current time
37810298fd71SBarry Smith -  dummy - either a viewer or NULL
37821713a123SBarry Smith 
378399fdda47SBarry Smith    Options Database:
378499fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
378599fdda47SBarry Smith 
378699fdda47SBarry Smith    Notes: the initial solution and current solution are not displayed with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
378799fdda47SBarry Smith        will look bad
378899fdda47SBarry Smith 
37891713a123SBarry Smith    Level: intermediate
37901713a123SBarry Smith 
37911713a123SBarry Smith .keywords: TS,  vector, monitor, view
37921713a123SBarry Smith 
3793a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
37941713a123SBarry Smith @*/
37950910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
37961713a123SBarry Smith {
3797dfbe8321SBarry Smith   PetscErrorCode   ierr;
379883a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
3799473a3ab2SBarry Smith   PetscDraw        draw;
38001713a123SBarry Smith 
38011713a123SBarry Smith   PetscFunctionBegin;
38026083293cSBarry Smith   if (!step && ictx->showinitial) {
38036083293cSBarry Smith     if (!ictx->initialsolution) {
38040910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
38051713a123SBarry Smith     }
38060910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
38076083293cSBarry Smith   }
3808b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
38090dcf80beSBarry Smith 
38106083293cSBarry Smith   if (ictx->showinitial) {
38116083293cSBarry Smith     PetscReal pause;
38126083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
38136083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
38146083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
38156083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
38166083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
38176083293cSBarry Smith   }
38180910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
3819473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
3820473a3ab2SBarry Smith     PetscReal xl,yl,xr,yr,tw,w,h;
3821473a3ab2SBarry Smith     char      time[32];
3822473a3ab2SBarry Smith     size_t    len;
3823473a3ab2SBarry Smith 
3824473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
382585b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
3826473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
3827473a3ab2SBarry Smith     ierr =  PetscStrlen(time,&len);CHKERRQ(ierr);
38280298fd71SBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
3829473a3ab2SBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
3830473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
3831473a3ab2SBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
3832473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
3833473a3ab2SBarry Smith   }
3834473a3ab2SBarry Smith 
38356083293cSBarry Smith   if (ictx->showinitial) {
38366083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
38376083293cSBarry Smith   }
38381713a123SBarry Smith   PetscFunctionReturn(0);
38391713a123SBarry Smith }
38401713a123SBarry Smith 
38412d5ee99bSBarry Smith #undef __FUNCT__
38422d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase"
38432d5ee99bSBarry Smith /*@C
38442d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
38452d5ee99bSBarry Smith 
38462d5ee99bSBarry Smith    Collective on TS
38472d5ee99bSBarry Smith 
38482d5ee99bSBarry Smith    Input Parameters:
38492d5ee99bSBarry Smith +  ts - the TS context
38502d5ee99bSBarry Smith .  step - current time-step
38512d5ee99bSBarry Smith .  ptime - current time
38522d5ee99bSBarry Smith -  dummy - either a viewer or NULL
38532d5ee99bSBarry Smith 
38542d5ee99bSBarry Smith    Level: intermediate
38552d5ee99bSBarry Smith 
38562d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
38572d5ee99bSBarry Smith 
38582d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
38592d5ee99bSBarry Smith @*/
38602d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
38612d5ee99bSBarry Smith {
38622d5ee99bSBarry Smith   PetscErrorCode    ierr;
38632d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
38642d5ee99bSBarry Smith   PetscDraw         draw;
38652d5ee99bSBarry Smith   MPI_Comm          comm;
38662d5ee99bSBarry Smith   PetscInt          n;
38672d5ee99bSBarry Smith   PetscMPIInt       size;
38682d5ee99bSBarry Smith   PetscReal         xl,yl,xr,yr,tw,w,h;
38692d5ee99bSBarry Smith   char              time[32];
38702d5ee99bSBarry Smith   size_t            len;
38712d5ee99bSBarry Smith   const PetscScalar *U;
38722d5ee99bSBarry Smith 
38732d5ee99bSBarry Smith   PetscFunctionBegin;
38742d5ee99bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
38752d5ee99bSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
38762d5ee99bSBarry Smith   if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs");
38772d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
38782d5ee99bSBarry Smith   if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns");
38792d5ee99bSBarry Smith 
38802d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
38812d5ee99bSBarry Smith 
38822d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
38834b363babSBarry Smith   ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
3884ba5783adSMatthew G Knepley   if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) {
3885a4494fc1SBarry Smith       ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
3886a4494fc1SBarry Smith       PetscFunctionReturn(0);
3887a4494fc1SBarry Smith   }
38882d5ee99bSBarry Smith   if (!step) ictx->color++;
38892d5ee99bSBarry Smith   ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr);
38902d5ee99bSBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
38912d5ee99bSBarry Smith 
38922d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
38934b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
389485b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
38952d5ee99bSBarry Smith     ierr = PetscStrlen(time,&len);CHKERRQ(ierr);
38962d5ee99bSBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
38972d5ee99bSBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
38982d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
38992d5ee99bSBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
39002d5ee99bSBarry Smith   }
39012d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
39022d5ee99bSBarry Smith   PetscFunctionReturn(0);
39032d5ee99bSBarry Smith }
39042d5ee99bSBarry Smith 
39051713a123SBarry Smith 
39066c699258SBarry Smith #undef __FUNCT__
390783a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
39086083293cSBarry Smith /*@C
390983a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
39106083293cSBarry Smith 
39116083293cSBarry Smith    Collective on TS
39126083293cSBarry Smith 
39136083293cSBarry Smith    Input Parameters:
39146083293cSBarry Smith .    ctx - the monitor context
39156083293cSBarry Smith 
39166083293cSBarry Smith    Level: intermediate
39176083293cSBarry Smith 
39186083293cSBarry Smith .keywords: TS,  vector, monitor, view
39196083293cSBarry Smith 
392083a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
39216083293cSBarry Smith @*/
392283a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
39236083293cSBarry Smith {
39246083293cSBarry Smith   PetscErrorCode ierr;
39256083293cSBarry Smith 
39266083293cSBarry Smith   PetscFunctionBegin;
39274b363babSBarry Smith   ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr);
392883a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
392983a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
393083a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
39316083293cSBarry Smith   PetscFunctionReturn(0);
39326083293cSBarry Smith }
39336083293cSBarry Smith 
39346083293cSBarry Smith #undef __FUNCT__
393583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
39366083293cSBarry Smith /*@C
393783a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
39386083293cSBarry Smith 
39396083293cSBarry Smith    Collective on TS
39406083293cSBarry Smith 
39416083293cSBarry Smith    Input Parameter:
39426083293cSBarry Smith .    ts - time-step context
39436083293cSBarry Smith 
39446083293cSBarry Smith    Output Patameter:
39456083293cSBarry Smith .    ctx - the monitor context
39466083293cSBarry Smith 
394799fdda47SBarry Smith    Options Database:
394899fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
394999fdda47SBarry Smith 
39506083293cSBarry Smith    Level: intermediate
39516083293cSBarry Smith 
39526083293cSBarry Smith .keywords: TS,  vector, monitor, view
39536083293cSBarry Smith 
395483a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
39556083293cSBarry Smith @*/
395683a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
39576083293cSBarry Smith {
39586083293cSBarry Smith   PetscErrorCode   ierr;
39596083293cSBarry Smith 
39606083293cSBarry Smith   PetscFunctionBegin;
3961b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
396283a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
3963e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
3964bbd56ea5SKarl Rupp 
396583a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
3966473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
39670298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
3968473a3ab2SBarry Smith 
3969473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
39700298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
39712d5ee99bSBarry Smith   (*ctx)->color = PETSC_DRAW_WHITE;
39726083293cSBarry Smith   PetscFunctionReturn(0);
39736083293cSBarry Smith }
39746083293cSBarry Smith 
39756083293cSBarry Smith #undef __FUNCT__
397683a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
39773a471f94SBarry Smith /*@C
397883a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
39793a471f94SBarry Smith    VecView() for the error at each timestep
39803a471f94SBarry Smith 
39813a471f94SBarry Smith    Collective on TS
39823a471f94SBarry Smith 
39833a471f94SBarry Smith    Input Parameters:
39843a471f94SBarry Smith +  ts - the TS context
39853a471f94SBarry Smith .  step - current time-step
39863a471f94SBarry Smith .  ptime - current time
39870298fd71SBarry Smith -  dummy - either a viewer or NULL
39883a471f94SBarry Smith 
39893a471f94SBarry Smith    Level: intermediate
39903a471f94SBarry Smith 
39913a471f94SBarry Smith .keywords: TS,  vector, monitor, view
39923a471f94SBarry Smith 
39933a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
39943a471f94SBarry Smith @*/
39950910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
39963a471f94SBarry Smith {
39973a471f94SBarry Smith   PetscErrorCode   ierr;
399883a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
399983a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
40003a471f94SBarry Smith   Vec              work;
40013a471f94SBarry Smith 
40023a471f94SBarry Smith   PetscFunctionBegin;
4003b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
40040910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
40053a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
40060910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
40073a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
40083a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
40093a471f94SBarry Smith   PetscFunctionReturn(0);
40103a471f94SBarry Smith }
40113a471f94SBarry Smith 
40122a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
40133a471f94SBarry Smith #undef __FUNCT__
40146c699258SBarry Smith #define __FUNCT__ "TSSetDM"
40156c699258SBarry Smith /*@
40166c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
40176c699258SBarry Smith 
40183f9fe445SBarry Smith    Logically Collective on TS and DM
40196c699258SBarry Smith 
40206c699258SBarry Smith    Input Parameters:
40216c699258SBarry Smith +  ts - the preconditioner context
40226c699258SBarry Smith -  dm - the dm
40236c699258SBarry Smith 
40246c699258SBarry Smith    Level: intermediate
40256c699258SBarry Smith 
40266c699258SBarry Smith 
40276c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
40286c699258SBarry Smith @*/
40297087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
40306c699258SBarry Smith {
40316c699258SBarry Smith   PetscErrorCode ierr;
4032089b2837SJed Brown   SNES           snes;
4033942e3340SBarry Smith   DMTS           tsdm;
40346c699258SBarry Smith 
40356c699258SBarry Smith   PetscFunctionBegin;
40360700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
403770663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4038942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
40392a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4040942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4041942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
404224989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
404324989b8cSPeter Brune         tsdm->originaldm = dm;
404424989b8cSPeter Brune       }
404524989b8cSPeter Brune     }
40466bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
404724989b8cSPeter Brune   }
40486c699258SBarry Smith   ts->dm = dm;
4049bbd56ea5SKarl Rupp 
4050089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4051089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
40526c699258SBarry Smith   PetscFunctionReturn(0);
40536c699258SBarry Smith }
40546c699258SBarry Smith 
40556c699258SBarry Smith #undef __FUNCT__
40566c699258SBarry Smith #define __FUNCT__ "TSGetDM"
40576c699258SBarry Smith /*@
40586c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
40596c699258SBarry Smith 
40603f9fe445SBarry Smith    Not Collective
40616c699258SBarry Smith 
40626c699258SBarry Smith    Input Parameter:
40636c699258SBarry Smith . ts - the preconditioner context
40646c699258SBarry Smith 
40656c699258SBarry Smith    Output Parameter:
40666c699258SBarry Smith .  dm - the dm
40676c699258SBarry Smith 
40686c699258SBarry Smith    Level: intermediate
40696c699258SBarry Smith 
40706c699258SBarry Smith 
40716c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
40726c699258SBarry Smith @*/
40737087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
40746c699258SBarry Smith {
4075496e6a7aSJed Brown   PetscErrorCode ierr;
4076496e6a7aSJed Brown 
40776c699258SBarry Smith   PetscFunctionBegin;
40780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4079496e6a7aSJed Brown   if (!ts->dm) {
4080ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4081496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4082496e6a7aSJed Brown   }
40836c699258SBarry Smith   *dm = ts->dm;
40846c699258SBarry Smith   PetscFunctionReturn(0);
40856c699258SBarry Smith }
40861713a123SBarry Smith 
40870f5c6efeSJed Brown #undef __FUNCT__
40880f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
40890f5c6efeSJed Brown /*@
40900f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
40910f5c6efeSJed Brown 
40923f9fe445SBarry Smith    Logically Collective on SNES
40930f5c6efeSJed Brown 
40940f5c6efeSJed Brown    Input Parameter:
4095d42a1c89SJed Brown + snes - nonlinear solver
40960910c330SBarry Smith . U - the current state at which to evaluate the residual
4097d42a1c89SJed Brown - ctx - user context, must be a TS
40980f5c6efeSJed Brown 
40990f5c6efeSJed Brown    Output Parameter:
41000f5c6efeSJed Brown . F - the nonlinear residual
41010f5c6efeSJed Brown 
41020f5c6efeSJed Brown    Notes:
41030f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
41040f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
41050f5c6efeSJed Brown 
41060f5c6efeSJed Brown    Level: advanced
41070f5c6efeSJed Brown 
41080f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
41090f5c6efeSJed Brown @*/
41100910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
41110f5c6efeSJed Brown {
41120f5c6efeSJed Brown   TS             ts = (TS)ctx;
41130f5c6efeSJed Brown   PetscErrorCode ierr;
41140f5c6efeSJed Brown 
41150f5c6efeSJed Brown   PetscFunctionBegin;
41160f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
41170910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
41180f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
41190f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
41200910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
41210f5c6efeSJed Brown   PetscFunctionReturn(0);
41220f5c6efeSJed Brown }
41230f5c6efeSJed Brown 
41240f5c6efeSJed Brown #undef __FUNCT__
41250f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
41260f5c6efeSJed Brown /*@
41270f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
41280f5c6efeSJed Brown 
41290f5c6efeSJed Brown    Collective on SNES
41300f5c6efeSJed Brown 
41310f5c6efeSJed Brown    Input Parameter:
41320f5c6efeSJed Brown + snes - nonlinear solver
41330910c330SBarry Smith . U - the current state at which to evaluate the residual
41340f5c6efeSJed Brown - ctx - user context, must be a TS
41350f5c6efeSJed Brown 
41360f5c6efeSJed Brown    Output Parameter:
41370f5c6efeSJed Brown + A - the Jacobian
41380f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
41390f5c6efeSJed Brown - flag - indicates any structure change in the matrix
41400f5c6efeSJed Brown 
41410f5c6efeSJed Brown    Notes:
41420f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
41430f5c6efeSJed Brown 
41440f5c6efeSJed Brown    Level: developer
41450f5c6efeSJed Brown 
41460f5c6efeSJed Brown .seealso: SNESSetJacobian()
41470f5c6efeSJed Brown @*/
4148d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
41490f5c6efeSJed Brown {
41500f5c6efeSJed Brown   TS             ts = (TS)ctx;
41510f5c6efeSJed Brown   PetscErrorCode ierr;
41520f5c6efeSJed Brown 
41530f5c6efeSJed Brown   PetscFunctionBegin;
41540f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
41550910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
41560f5c6efeSJed Brown   PetscValidPointer(A,3);
415794ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
41580f5c6efeSJed Brown   PetscValidPointer(B,4);
415994ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
41600f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4161d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
41620f5c6efeSJed Brown   PetscFunctionReturn(0);
41630f5c6efeSJed Brown }
4164325fc9f4SBarry Smith 
41650e4ef248SJed Brown #undef __FUNCT__
41660e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
41670e4ef248SJed Brown /*@C
41680e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
41690e4ef248SJed Brown 
41700e4ef248SJed Brown    Collective on TS
41710e4ef248SJed Brown 
41720e4ef248SJed Brown    Input Arguments:
41730e4ef248SJed Brown +  ts - time stepping context
41740e4ef248SJed Brown .  t - time at which to evaluate
41750910c330SBarry Smith .  U - state at which to evaluate
41760e4ef248SJed Brown -  ctx - context
41770e4ef248SJed Brown 
41780e4ef248SJed Brown    Output Arguments:
41790e4ef248SJed Brown .  F - right hand side
41800e4ef248SJed Brown 
41810e4ef248SJed Brown    Level: intermediate
41820e4ef248SJed Brown 
41830e4ef248SJed Brown    Notes:
41840e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
41850e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
41860e4ef248SJed Brown 
41870e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
41880e4ef248SJed Brown @*/
41890910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
41900e4ef248SJed Brown {
41910e4ef248SJed Brown   PetscErrorCode ierr;
41920e4ef248SJed Brown   Mat            Arhs,Brhs;
41930e4ef248SJed Brown 
41940e4ef248SJed Brown   PetscFunctionBegin;
41950e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
4196d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
41970910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
41980e4ef248SJed Brown   PetscFunctionReturn(0);
41990e4ef248SJed Brown }
42000e4ef248SJed Brown 
42010e4ef248SJed Brown #undef __FUNCT__
42020e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
42030e4ef248SJed Brown /*@C
42040e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
42050e4ef248SJed Brown 
42060e4ef248SJed Brown    Collective on TS
42070e4ef248SJed Brown 
42080e4ef248SJed Brown    Input Arguments:
42090e4ef248SJed Brown +  ts - time stepping context
42100e4ef248SJed Brown .  t - time at which to evaluate
42110910c330SBarry Smith .  U - state at which to evaluate
42120e4ef248SJed Brown -  ctx - context
42130e4ef248SJed Brown 
42140e4ef248SJed Brown    Output Arguments:
42150e4ef248SJed Brown +  A - pointer to operator
42160e4ef248SJed Brown .  B - pointer to preconditioning matrix
42170e4ef248SJed Brown -  flg - matrix structure flag
42180e4ef248SJed Brown 
42190e4ef248SJed Brown    Level: intermediate
42200e4ef248SJed Brown 
42210e4ef248SJed Brown    Notes:
42220e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
42230e4ef248SJed Brown 
42240e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
42250e4ef248SJed Brown @*/
4226d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
42270e4ef248SJed Brown {
42280e4ef248SJed Brown   PetscFunctionBegin;
42290e4ef248SJed Brown   PetscFunctionReturn(0);
42300e4ef248SJed Brown }
42310e4ef248SJed Brown 
42320026cea9SSean Farley #undef __FUNCT__
42330026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
42340026cea9SSean Farley /*@C
42350026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
42360026cea9SSean Farley 
42370026cea9SSean Farley    Collective on TS
42380026cea9SSean Farley 
42390026cea9SSean Farley    Input Arguments:
42400026cea9SSean Farley +  ts - time stepping context
42410026cea9SSean Farley .  t - time at which to evaluate
42420910c330SBarry Smith .  U - state at which to evaluate
42430910c330SBarry Smith .  Udot - time derivative of state vector
42440026cea9SSean Farley -  ctx - context
42450026cea9SSean Farley 
42460026cea9SSean Farley    Output Arguments:
42470026cea9SSean Farley .  F - left hand side
42480026cea9SSean Farley 
42490026cea9SSean Farley    Level: intermediate
42500026cea9SSean Farley 
42510026cea9SSean Farley    Notes:
42520910c330SBarry 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
42530026cea9SSean Farley    user is required to write their own TSComputeIFunction.
42540026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
42550026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
42560026cea9SSean Farley 
42570026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
42580026cea9SSean Farley @*/
42590910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
42600026cea9SSean Farley {
42610026cea9SSean Farley   PetscErrorCode ierr;
42620026cea9SSean Farley   Mat            A,B;
42630026cea9SSean Farley 
42640026cea9SSean Farley   PetscFunctionBegin;
42650298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
4266d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
42670910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
42680026cea9SSean Farley   PetscFunctionReturn(0);
42690026cea9SSean Farley }
42700026cea9SSean Farley 
42710026cea9SSean Farley #undef __FUNCT__
42720026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
42730026cea9SSean Farley /*@C
4274b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
42750026cea9SSean Farley 
42760026cea9SSean Farley    Collective on TS
42770026cea9SSean Farley 
42780026cea9SSean Farley    Input Arguments:
42790026cea9SSean Farley +  ts - time stepping context
42800026cea9SSean Farley .  t - time at which to evaluate
42810910c330SBarry Smith .  U - state at which to evaluate
42820910c330SBarry Smith .  Udot - time derivative of state vector
42830026cea9SSean Farley .  shift - shift to apply
42840026cea9SSean Farley -  ctx - context
42850026cea9SSean Farley 
42860026cea9SSean Farley    Output Arguments:
42870026cea9SSean Farley +  A - pointer to operator
42880026cea9SSean Farley .  B - pointer to preconditioning matrix
42890026cea9SSean Farley -  flg - matrix structure flag
42900026cea9SSean Farley 
4291b41af12eSJed Brown    Level: advanced
42920026cea9SSean Farley 
42930026cea9SSean Farley    Notes:
42940026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
42950026cea9SSean Farley 
4296b41af12eSJed Brown    It is only appropriate for problems of the form
4297b41af12eSJed Brown 
4298b41af12eSJed Brown $     M Udot = F(U,t)
4299b41af12eSJed Brown 
4300b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4301b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4302b41af12eSJed Brown   an implicit operator of the form
4303b41af12eSJed Brown 
4304b41af12eSJed Brown $    shift*M + J
4305b41af12eSJed Brown 
4306b41af12eSJed 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
4307b41af12eSJed Brown   a copy of M or reassemble it when requested.
4308b41af12eSJed Brown 
43090026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
43100026cea9SSean Farley @*/
4311d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
43120026cea9SSean Farley {
4313b41af12eSJed Brown   PetscErrorCode ierr;
4314b41af12eSJed Brown 
43150026cea9SSean Farley   PetscFunctionBegin;
431694ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4317b41af12eSJed Brown   ts->ijacobian.shift = shift;
43180026cea9SSean Farley   PetscFunctionReturn(0);
43190026cea9SSean Farley }
4320b41af12eSJed Brown 
4321e817cc15SEmil Constantinescu #undef __FUNCT__
4322e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
4323e817cc15SEmil Constantinescu /*@
4324e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4325e817cc15SEmil Constantinescu 
4326e817cc15SEmil Constantinescu    Not Collective
4327e817cc15SEmil Constantinescu 
4328e817cc15SEmil Constantinescu    Input Parameter:
4329e817cc15SEmil Constantinescu .  ts - the TS context
4330e817cc15SEmil Constantinescu 
4331e817cc15SEmil Constantinescu    Output Parameter:
43324e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4333e817cc15SEmil Constantinescu 
4334e817cc15SEmil Constantinescu    Level: beginner
4335e817cc15SEmil Constantinescu 
4336e817cc15SEmil Constantinescu .keywords: TS, equation type
4337e817cc15SEmil Constantinescu 
4338e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4339e817cc15SEmil Constantinescu @*/
4340e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4341e817cc15SEmil Constantinescu {
4342e817cc15SEmil Constantinescu   PetscFunctionBegin;
4343e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4344e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4345e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4346e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4347e817cc15SEmil Constantinescu }
4348e817cc15SEmil Constantinescu 
4349e817cc15SEmil Constantinescu #undef __FUNCT__
4350e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
4351e817cc15SEmil Constantinescu /*@
4352e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4353e817cc15SEmil Constantinescu 
4354e817cc15SEmil Constantinescu    Not Collective
4355e817cc15SEmil Constantinescu 
4356e817cc15SEmil Constantinescu    Input Parameter:
4357e817cc15SEmil Constantinescu +  ts - the TS context
43584e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4359e817cc15SEmil Constantinescu 
4360e817cc15SEmil Constantinescu    Level: advanced
4361e817cc15SEmil Constantinescu 
4362e817cc15SEmil Constantinescu .keywords: TS, equation type
4363e817cc15SEmil Constantinescu 
4364e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4365e817cc15SEmil Constantinescu @*/
4366e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4367e817cc15SEmil Constantinescu {
4368e817cc15SEmil Constantinescu   PetscFunctionBegin;
4369e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4370e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4371e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4372e817cc15SEmil Constantinescu }
43730026cea9SSean Farley 
43744af1b03aSJed Brown #undef __FUNCT__
43754af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
43764af1b03aSJed Brown /*@
43774af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
43784af1b03aSJed Brown 
43794af1b03aSJed Brown    Not Collective
43804af1b03aSJed Brown 
43814af1b03aSJed Brown    Input Parameter:
43824af1b03aSJed Brown .  ts - the TS context
43834af1b03aSJed Brown 
43844af1b03aSJed Brown    Output Parameter:
43854af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
43864af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
43874af1b03aSJed Brown 
4388487e0bb9SJed Brown    Level: beginner
43894af1b03aSJed Brown 
4390cd652676SJed Brown    Notes:
4391cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
43924af1b03aSJed Brown 
43934af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
43944af1b03aSJed Brown 
43954af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
43964af1b03aSJed Brown @*/
43974af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
43984af1b03aSJed Brown {
43994af1b03aSJed Brown   PetscFunctionBegin;
44004af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44014af1b03aSJed Brown   PetscValidPointer(reason,2);
44024af1b03aSJed Brown   *reason = ts->reason;
44034af1b03aSJed Brown   PetscFunctionReturn(0);
44044af1b03aSJed Brown }
44054af1b03aSJed Brown 
4406fb1732b5SBarry Smith #undef __FUNCT__
4407d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
4408d6ad946cSShri Abhyankar /*@
4409d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
4410d6ad946cSShri Abhyankar 
4411d6ad946cSShri Abhyankar    Not Collective
4412d6ad946cSShri Abhyankar 
4413d6ad946cSShri Abhyankar    Input Parameter:
4414d6ad946cSShri Abhyankar +  ts - the TS context
4415d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
4416d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4417d6ad946cSShri Abhyankar 
4418f5abba47SShri Abhyankar    Level: advanced
4419d6ad946cSShri Abhyankar 
4420d6ad946cSShri Abhyankar    Notes:
4421d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
4422d6ad946cSShri Abhyankar 
4423d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
4424d6ad946cSShri Abhyankar 
4425d6ad946cSShri Abhyankar .seealso: TSConvergedReason
4426d6ad946cSShri Abhyankar @*/
4427d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
4428d6ad946cSShri Abhyankar {
4429d6ad946cSShri Abhyankar   PetscFunctionBegin;
4430d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4431d6ad946cSShri Abhyankar   ts->reason = reason;
4432d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
4433d6ad946cSShri Abhyankar }
4434d6ad946cSShri Abhyankar 
4435d6ad946cSShri Abhyankar #undef __FUNCT__
4436cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
4437cc708dedSBarry Smith /*@
4438cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
4439cc708dedSBarry Smith 
4440cc708dedSBarry Smith    Not Collective
4441cc708dedSBarry Smith 
4442cc708dedSBarry Smith    Input Parameter:
4443cc708dedSBarry Smith .  ts - the TS context
4444cc708dedSBarry Smith 
4445cc708dedSBarry Smith    Output Parameter:
4446cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
4447cc708dedSBarry Smith 
4448487e0bb9SJed Brown    Level: beginner
4449cc708dedSBarry Smith 
4450cc708dedSBarry Smith    Notes:
4451cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
4452cc708dedSBarry Smith 
4453cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
4454cc708dedSBarry Smith 
4455cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
4456cc708dedSBarry Smith @*/
4457cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
4458cc708dedSBarry Smith {
4459cc708dedSBarry Smith   PetscFunctionBegin;
4460cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4461cc708dedSBarry Smith   PetscValidPointer(ftime,2);
4462cc708dedSBarry Smith   *ftime = ts->solvetime;
4463cc708dedSBarry Smith   PetscFunctionReturn(0);
4464cc708dedSBarry Smith }
4465cc708dedSBarry Smith 
4466cc708dedSBarry Smith #undef __FUNCT__
44672c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps"
44682c18e0fdSBarry Smith /*@
44692c18e0fdSBarry Smith    TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate()
44702c18e0fdSBarry Smith 
44712c18e0fdSBarry Smith    Not Collective
44722c18e0fdSBarry Smith 
44732c18e0fdSBarry Smith    Input Parameter:
44742c18e0fdSBarry Smith .  ts - the TS context
44752c18e0fdSBarry Smith 
44762c18e0fdSBarry Smith    Output Parameter:
44772c18e0fdSBarry Smith .  steps - the number of steps
44782c18e0fdSBarry Smith 
44792c18e0fdSBarry Smith    Level: beginner
44802c18e0fdSBarry Smith 
44812c18e0fdSBarry Smith    Notes:
44822c18e0fdSBarry Smith    Includes the number of steps for all calls to TSSolve() since TSSetUp() was called
44832c18e0fdSBarry Smith 
44842c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test
44852c18e0fdSBarry Smith 
44862c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
44872c18e0fdSBarry Smith @*/
44882c18e0fdSBarry Smith PetscErrorCode  TSGetTotalSteps(TS ts,PetscInt *steps)
44892c18e0fdSBarry Smith {
44902c18e0fdSBarry Smith   PetscFunctionBegin;
44912c18e0fdSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44922c18e0fdSBarry Smith   PetscValidPointer(steps,2);
44932c18e0fdSBarry Smith   *steps = ts->total_steps;
44942c18e0fdSBarry Smith   PetscFunctionReturn(0);
44952c18e0fdSBarry Smith }
44962c18e0fdSBarry Smith 
44972c18e0fdSBarry Smith #undef __FUNCT__
44985ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
44999f67acb7SJed Brown /*@
45005ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
45019f67acb7SJed Brown    used by the time integrator.
45029f67acb7SJed Brown 
45039f67acb7SJed Brown    Not Collective
45049f67acb7SJed Brown 
45059f67acb7SJed Brown    Input Parameter:
45069f67acb7SJed Brown .  ts - TS context
45079f67acb7SJed Brown 
45089f67acb7SJed Brown    Output Parameter:
45099f67acb7SJed Brown .  nits - number of nonlinear iterations
45109f67acb7SJed Brown 
45119f67acb7SJed Brown    Notes:
45129f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
45139f67acb7SJed Brown 
45149f67acb7SJed Brown    Level: intermediate
45159f67acb7SJed Brown 
45169f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
45179f67acb7SJed Brown 
45185ef26d82SJed Brown .seealso:  TSGetKSPIterations()
45199f67acb7SJed Brown @*/
45205ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
45219f67acb7SJed Brown {
45229f67acb7SJed Brown   PetscFunctionBegin;
45239f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45249f67acb7SJed Brown   PetscValidIntPointer(nits,2);
45255ef26d82SJed Brown   *nits = ts->snes_its;
45269f67acb7SJed Brown   PetscFunctionReturn(0);
45279f67acb7SJed Brown }
45289f67acb7SJed Brown 
45299f67acb7SJed Brown #undef __FUNCT__
45305ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
45319f67acb7SJed Brown /*@
45325ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
45339f67acb7SJed Brown    used by the time integrator.
45349f67acb7SJed Brown 
45359f67acb7SJed Brown    Not Collective
45369f67acb7SJed Brown 
45379f67acb7SJed Brown    Input Parameter:
45389f67acb7SJed Brown .  ts - TS context
45399f67acb7SJed Brown 
45409f67acb7SJed Brown    Output Parameter:
45419f67acb7SJed Brown .  lits - number of linear iterations
45429f67acb7SJed Brown 
45439f67acb7SJed Brown    Notes:
45449f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
45459f67acb7SJed Brown 
45469f67acb7SJed Brown    Level: intermediate
45479f67acb7SJed Brown 
45489f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
45499f67acb7SJed Brown 
45505ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
45519f67acb7SJed Brown @*/
45525ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
45539f67acb7SJed Brown {
45549f67acb7SJed Brown   PetscFunctionBegin;
45559f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45569f67acb7SJed Brown   PetscValidIntPointer(lits,2);
45575ef26d82SJed Brown   *lits = ts->ksp_its;
45589f67acb7SJed Brown   PetscFunctionReturn(0);
45599f67acb7SJed Brown }
45609f67acb7SJed Brown 
45619f67acb7SJed Brown #undef __FUNCT__
4562cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
4563cef5090cSJed Brown /*@
4564cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
4565cef5090cSJed Brown 
4566cef5090cSJed Brown    Not Collective
4567cef5090cSJed Brown 
4568cef5090cSJed Brown    Input Parameter:
4569cef5090cSJed Brown .  ts - TS context
4570cef5090cSJed Brown 
4571cef5090cSJed Brown    Output Parameter:
4572cef5090cSJed Brown .  rejects - number of steps rejected
4573cef5090cSJed Brown 
4574cef5090cSJed Brown    Notes:
4575cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4576cef5090cSJed Brown 
4577cef5090cSJed Brown    Level: intermediate
4578cef5090cSJed Brown 
4579cef5090cSJed Brown .keywords: TS, get, number
4580cef5090cSJed Brown 
45815ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
4582cef5090cSJed Brown @*/
4583cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
4584cef5090cSJed Brown {
4585cef5090cSJed Brown   PetscFunctionBegin;
4586cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4587cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
4588cef5090cSJed Brown   *rejects = ts->reject;
4589cef5090cSJed Brown   PetscFunctionReturn(0);
4590cef5090cSJed Brown }
4591cef5090cSJed Brown 
4592cef5090cSJed Brown #undef __FUNCT__
4593cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
4594cef5090cSJed Brown /*@
4595cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
4596cef5090cSJed Brown 
4597cef5090cSJed Brown    Not Collective
4598cef5090cSJed Brown 
4599cef5090cSJed Brown    Input Parameter:
4600cef5090cSJed Brown .  ts - TS context
4601cef5090cSJed Brown 
4602cef5090cSJed Brown    Output Parameter:
4603cef5090cSJed Brown .  fails - number of failed nonlinear solves
4604cef5090cSJed Brown 
4605cef5090cSJed Brown    Notes:
4606cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4607cef5090cSJed Brown 
4608cef5090cSJed Brown    Level: intermediate
4609cef5090cSJed Brown 
4610cef5090cSJed Brown .keywords: TS, get, number
4611cef5090cSJed Brown 
46125ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
4613cef5090cSJed Brown @*/
4614cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
4615cef5090cSJed Brown {
4616cef5090cSJed Brown   PetscFunctionBegin;
4617cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4618cef5090cSJed Brown   PetscValidIntPointer(fails,2);
4619cef5090cSJed Brown   *fails = ts->num_snes_failures;
4620cef5090cSJed Brown   PetscFunctionReturn(0);
4621cef5090cSJed Brown }
4622cef5090cSJed Brown 
4623cef5090cSJed Brown #undef __FUNCT__
4624cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
4625cef5090cSJed Brown /*@
4626cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
4627cef5090cSJed Brown 
4628cef5090cSJed Brown    Not Collective
4629cef5090cSJed Brown 
4630cef5090cSJed Brown    Input Parameter:
4631cef5090cSJed Brown +  ts - TS context
4632cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
4633cef5090cSJed Brown 
4634cef5090cSJed Brown    Notes:
4635cef5090cSJed Brown    The counter is reset to zero for each step
4636cef5090cSJed Brown 
4637cef5090cSJed Brown    Options Database Key:
4638cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
4639cef5090cSJed Brown 
4640cef5090cSJed Brown    Level: intermediate
4641cef5090cSJed Brown 
4642cef5090cSJed Brown .keywords: TS, set, maximum, number
4643cef5090cSJed Brown 
46445ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4645cef5090cSJed Brown @*/
4646cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
4647cef5090cSJed Brown {
4648cef5090cSJed Brown   PetscFunctionBegin;
4649cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4650cef5090cSJed Brown   ts->max_reject = rejects;
4651cef5090cSJed Brown   PetscFunctionReturn(0);
4652cef5090cSJed Brown }
4653cef5090cSJed Brown 
4654cef5090cSJed Brown #undef __FUNCT__
4655cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
4656cef5090cSJed Brown /*@
4657cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
4658cef5090cSJed Brown 
4659cef5090cSJed Brown    Not Collective
4660cef5090cSJed Brown 
4661cef5090cSJed Brown    Input Parameter:
4662cef5090cSJed Brown +  ts - TS context
4663cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
4664cef5090cSJed Brown 
4665cef5090cSJed Brown    Notes:
4666cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
4667cef5090cSJed Brown 
4668cef5090cSJed Brown    Options Database Key:
4669cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
4670cef5090cSJed Brown 
4671cef5090cSJed Brown    Level: intermediate
4672cef5090cSJed Brown 
4673cef5090cSJed Brown .keywords: TS, set, maximum, number
4674cef5090cSJed Brown 
46755ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
4676cef5090cSJed Brown @*/
4677cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
4678cef5090cSJed Brown {
4679cef5090cSJed Brown   PetscFunctionBegin;
4680cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4681cef5090cSJed Brown   ts->max_snes_failures = fails;
4682cef5090cSJed Brown   PetscFunctionReturn(0);
4683cef5090cSJed Brown }
4684cef5090cSJed Brown 
4685cef5090cSJed Brown #undef __FUNCT__
46864e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails"
4687cef5090cSJed Brown /*@
4688cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
4689cef5090cSJed Brown 
4690cef5090cSJed Brown    Not Collective
4691cef5090cSJed Brown 
4692cef5090cSJed Brown    Input Parameter:
4693cef5090cSJed Brown +  ts - TS context
4694cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
4695cef5090cSJed Brown 
4696cef5090cSJed Brown    Options Database Key:
4697cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
4698cef5090cSJed Brown 
4699cef5090cSJed Brown    Level: intermediate
4700cef5090cSJed Brown 
4701cef5090cSJed Brown .keywords: TS, set, error
4702cef5090cSJed Brown 
47035ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4704cef5090cSJed Brown @*/
4705cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
4706cef5090cSJed Brown {
4707cef5090cSJed Brown   PetscFunctionBegin;
4708cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4709cef5090cSJed Brown   ts->errorifstepfailed = err;
4710cef5090cSJed Brown   PetscFunctionReturn(0);
4711cef5090cSJed Brown }
4712cef5090cSJed Brown 
4713cef5090cSJed Brown #undef __FUNCT__
4714fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
4715fb1732b5SBarry Smith /*@C
4716fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
4717fb1732b5SBarry Smith 
4718fb1732b5SBarry Smith    Collective on TS
4719fb1732b5SBarry Smith 
4720fb1732b5SBarry Smith    Input Parameters:
4721fb1732b5SBarry Smith +  ts - the TS context
4722fb1732b5SBarry Smith .  step - current time-step
4723fb1732b5SBarry Smith .  ptime - current time
47240910c330SBarry Smith .  u - current state
4725fb1732b5SBarry Smith -  viewer - binary viewer
4726fb1732b5SBarry Smith 
4727fb1732b5SBarry Smith    Level: intermediate
4728fb1732b5SBarry Smith 
4729fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
4730fb1732b5SBarry Smith 
4731fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4732fb1732b5SBarry Smith @*/
47330910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
4734fb1732b5SBarry Smith {
4735fb1732b5SBarry Smith   PetscErrorCode ierr;
4736ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
4737fb1732b5SBarry Smith 
4738fb1732b5SBarry Smith   PetscFunctionBegin;
47390910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
4740ed81e22dSJed Brown   PetscFunctionReturn(0);
4741ed81e22dSJed Brown }
4742ed81e22dSJed Brown 
4743ed81e22dSJed Brown #undef __FUNCT__
4744ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
4745ed81e22dSJed Brown /*@C
4746ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
4747ed81e22dSJed Brown 
4748ed81e22dSJed Brown    Collective on TS
4749ed81e22dSJed Brown 
4750ed81e22dSJed Brown    Input Parameters:
4751ed81e22dSJed Brown +  ts - the TS context
4752ed81e22dSJed Brown .  step - current time-step
4753ed81e22dSJed Brown .  ptime - current time
47540910c330SBarry Smith .  u - current state
4755ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4756ed81e22dSJed Brown 
4757ed81e22dSJed Brown    Level: intermediate
4758ed81e22dSJed Brown 
4759ed81e22dSJed Brown    Notes:
4760ed81e22dSJed 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.
4761ed81e22dSJed Brown    These are named according to the file name template.
4762ed81e22dSJed Brown 
4763ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
4764ed81e22dSJed Brown 
4765ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4766ed81e22dSJed Brown 
4767ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4768ed81e22dSJed Brown @*/
47690910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
4770ed81e22dSJed Brown {
4771ed81e22dSJed Brown   PetscErrorCode ierr;
4772ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
4773ed81e22dSJed Brown   PetscViewer    viewer;
4774ed81e22dSJed Brown 
4775ed81e22dSJed Brown   PetscFunctionBegin;
47768caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
4777ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
47780910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
4779ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
4780ed81e22dSJed Brown   PetscFunctionReturn(0);
4781ed81e22dSJed Brown }
4782ed81e22dSJed Brown 
4783ed81e22dSJed Brown #undef __FUNCT__
4784ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
4785ed81e22dSJed Brown /*@C
4786ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
4787ed81e22dSJed Brown 
4788ed81e22dSJed Brown    Collective on TS
4789ed81e22dSJed Brown 
4790ed81e22dSJed Brown    Input Parameters:
4791ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4792ed81e22dSJed Brown 
4793ed81e22dSJed Brown    Level: intermediate
4794ed81e22dSJed Brown 
4795ed81e22dSJed Brown    Note:
4796ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
4797ed81e22dSJed Brown 
4798ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4799ed81e22dSJed Brown 
4800ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
4801ed81e22dSJed Brown @*/
4802ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
4803ed81e22dSJed Brown {
4804ed81e22dSJed Brown   PetscErrorCode ierr;
4805ed81e22dSJed Brown 
4806ed81e22dSJed Brown   PetscFunctionBegin;
4807ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
4808fb1732b5SBarry Smith   PetscFunctionReturn(0);
4809fb1732b5SBarry Smith }
4810fb1732b5SBarry Smith 
481184df9cb4SJed Brown #undef __FUNCT__
4812552698daSJed Brown #define __FUNCT__ "TSGetAdapt"
481384df9cb4SJed Brown /*@
4814552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
481584df9cb4SJed Brown 
4816ed81e22dSJed Brown    Collective on TS if controller has not been created yet
481784df9cb4SJed Brown 
481884df9cb4SJed Brown    Input Arguments:
4819ed81e22dSJed Brown .  ts - time stepping context
482084df9cb4SJed Brown 
482184df9cb4SJed Brown    Output Arguments:
4822ed81e22dSJed Brown .  adapt - adaptive controller
482384df9cb4SJed Brown 
482484df9cb4SJed Brown    Level: intermediate
482584df9cb4SJed Brown 
4826ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
482784df9cb4SJed Brown @*/
4828552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
482984df9cb4SJed Brown {
483084df9cb4SJed Brown   PetscErrorCode ierr;
483184df9cb4SJed Brown 
483284df9cb4SJed Brown   PetscFunctionBegin;
483384df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
483484df9cb4SJed Brown   PetscValidPointer(adapt,2);
483584df9cb4SJed Brown   if (!ts->adapt) {
4836ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
48373bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
48381c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
483984df9cb4SJed Brown   }
484084df9cb4SJed Brown   *adapt = ts->adapt;
484184df9cb4SJed Brown   PetscFunctionReturn(0);
484284df9cb4SJed Brown }
4843d6ebe24aSShri Abhyankar 
4844d6ebe24aSShri Abhyankar #undef __FUNCT__
48451c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
48461c3436cfSJed Brown /*@
48471c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
48481c3436cfSJed Brown 
48491c3436cfSJed Brown    Logically Collective
48501c3436cfSJed Brown 
48511c3436cfSJed Brown    Input Arguments:
48521c3436cfSJed Brown +  ts - time integration context
48531c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
48540298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
48551c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
48560298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
48571c3436cfSJed Brown 
4858a3cdaa26SBarry Smith    Options Database keys:
4859a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
4860a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
4861a3cdaa26SBarry Smith 
48621c3436cfSJed Brown    Level: beginner
48631c3436cfSJed Brown 
4864c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
48651c3436cfSJed Brown @*/
48661c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
48671c3436cfSJed Brown {
48681c3436cfSJed Brown   PetscErrorCode ierr;
48691c3436cfSJed Brown 
48701c3436cfSJed Brown   PetscFunctionBegin;
4871c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
48721c3436cfSJed Brown   if (vatol) {
48731c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
48741c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
4875bbd56ea5SKarl Rupp 
48761c3436cfSJed Brown     ts->vatol = vatol;
48771c3436cfSJed Brown   }
4878c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
48791c3436cfSJed Brown   if (vrtol) {
48801c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
48811c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
4882bbd56ea5SKarl Rupp 
48831c3436cfSJed Brown     ts->vrtol = vrtol;
48841c3436cfSJed Brown   }
48851c3436cfSJed Brown   PetscFunctionReturn(0);
48861c3436cfSJed Brown }
48871c3436cfSJed Brown 
48881c3436cfSJed Brown #undef __FUNCT__
4889c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
4890c5033834SJed Brown /*@
4891c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
4892c5033834SJed Brown 
4893c5033834SJed Brown    Logically Collective
4894c5033834SJed Brown 
4895c5033834SJed Brown    Input Arguments:
4896c5033834SJed Brown .  ts - time integration context
4897c5033834SJed Brown 
4898c5033834SJed Brown    Output Arguments:
48990298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
49000298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
49010298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
49020298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
4903c5033834SJed Brown 
4904c5033834SJed Brown    Level: beginner
4905c5033834SJed Brown 
4906c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
4907c5033834SJed Brown @*/
4908c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
4909c5033834SJed Brown {
4910c5033834SJed Brown   PetscFunctionBegin;
4911c5033834SJed Brown   if (atol)  *atol  = ts->atol;
4912c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
4913c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
4914c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
4915c5033834SJed Brown   PetscFunctionReturn(0);
4916c5033834SJed Brown }
4917c5033834SJed Brown 
4918c5033834SJed Brown #undef __FUNCT__
49191c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
49201c3436cfSJed Brown /*@
49211c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
49221c3436cfSJed Brown 
49231c3436cfSJed Brown    Collective on TS
49241c3436cfSJed Brown 
49251c3436cfSJed Brown    Input Arguments:
49261c3436cfSJed Brown +  ts - time stepping context
49271c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
49281c3436cfSJed Brown 
49291c3436cfSJed Brown    Output Arguments:
49301c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
49311c3436cfSJed Brown 
49321c3436cfSJed Brown    Level: developer
49331c3436cfSJed Brown 
49341c3436cfSJed Brown .seealso: TSSetTolerances()
49351c3436cfSJed Brown @*/
49361c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
49371c3436cfSJed Brown {
49381c3436cfSJed Brown   PetscErrorCode    ierr;
49391c3436cfSJed Brown   PetscInt          i,n,N;
49400910c330SBarry Smith   const PetscScalar *u,*y;
49410910c330SBarry Smith   Vec               U;
49421c3436cfSJed Brown   PetscReal         sum,gsum;
49431c3436cfSJed Brown 
49441c3436cfSJed Brown   PetscFunctionBegin;
49451c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49461c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
49471c3436cfSJed Brown   PetscValidPointer(norm,3);
49480910c330SBarry Smith   U = ts->vec_sol;
49490910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
4950ce94432eSBarry Smith   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
49511c3436cfSJed Brown 
49520910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
49530910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
49540910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
49551c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
49561c3436cfSJed Brown   sum  = 0.;
4957ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
4958ceefc113SJed Brown     const PetscScalar *atol,*rtol;
4959ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4960ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4961ceefc113SJed Brown     for (i=0; i<n; i++) {
49620910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
49630910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4964ceefc113SJed Brown     }
4965ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4966ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4967ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
4968ceefc113SJed Brown     const PetscScalar *atol;
4969ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4970ceefc113SJed Brown     for (i=0; i<n; i++) {
49710910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
49720910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4973ceefc113SJed Brown     }
4974ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4975ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
4976ceefc113SJed Brown     const PetscScalar *rtol;
4977ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4978ceefc113SJed Brown     for (i=0; i<n; i++) {
49790910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
49800910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4981ceefc113SJed Brown     }
4982ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4983ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
49841c3436cfSJed Brown     for (i=0; i<n; i++) {
49850910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
49860910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
49871c3436cfSJed Brown     }
4988ceefc113SJed Brown   }
49890910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
49901c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
49911c3436cfSJed Brown 
4992ce94432eSBarry Smith   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
49931c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
4994620fc8aaSSatish Balay   if (PetscIsInfOrNanReal(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
49951c3436cfSJed Brown   PetscFunctionReturn(0);
49961c3436cfSJed Brown }
49971c3436cfSJed Brown 
49981c3436cfSJed Brown #undef __FUNCT__
49998d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
50008d59e960SJed Brown /*@
50018d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
50028d59e960SJed Brown 
50038d59e960SJed Brown    Logically Collective on TS
50048d59e960SJed Brown 
50058d59e960SJed Brown    Input Arguments:
50068d59e960SJed Brown +  ts - time stepping context
50078d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
50088d59e960SJed Brown 
50098d59e960SJed Brown    Note:
50108d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
50118d59e960SJed Brown 
50128d59e960SJed Brown    Level: intermediate
50138d59e960SJed Brown 
50148d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
50158d59e960SJed Brown @*/
50168d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
50178d59e960SJed Brown {
50188d59e960SJed Brown   PetscFunctionBegin;
50198d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
50208d59e960SJed Brown   ts->cfltime_local = cfltime;
50218d59e960SJed Brown   ts->cfltime       = -1.;
50228d59e960SJed Brown   PetscFunctionReturn(0);
50238d59e960SJed Brown }
50248d59e960SJed Brown 
50258d59e960SJed Brown #undef __FUNCT__
50268d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
50278d59e960SJed Brown /*@
50288d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
50298d59e960SJed Brown 
50308d59e960SJed Brown    Collective on TS
50318d59e960SJed Brown 
50328d59e960SJed Brown    Input Arguments:
50338d59e960SJed Brown .  ts - time stepping context
50348d59e960SJed Brown 
50358d59e960SJed Brown    Output Arguments:
50368d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
50378d59e960SJed Brown 
50388d59e960SJed Brown    Level: advanced
50398d59e960SJed Brown 
50408d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
50418d59e960SJed Brown @*/
50428d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
50438d59e960SJed Brown {
50448d59e960SJed Brown   PetscErrorCode ierr;
50458d59e960SJed Brown 
50468d59e960SJed Brown   PetscFunctionBegin;
50478d59e960SJed Brown   if (ts->cfltime < 0) {
5048ce94432eSBarry Smith     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
50498d59e960SJed Brown   }
50508d59e960SJed Brown   *cfltime = ts->cfltime;
50518d59e960SJed Brown   PetscFunctionReturn(0);
50528d59e960SJed Brown }
50538d59e960SJed Brown 
50548d59e960SJed Brown #undef __FUNCT__
5055d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
5056d6ebe24aSShri Abhyankar /*@
5057d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
5058d6ebe24aSShri Abhyankar 
5059d6ebe24aSShri Abhyankar    Input Parameters:
5060d6ebe24aSShri Abhyankar .  ts   - the TS context.
5061d6ebe24aSShri Abhyankar .  xl   - lower bound.
5062d6ebe24aSShri Abhyankar .  xu   - upper bound.
5063d6ebe24aSShri Abhyankar 
5064d6ebe24aSShri Abhyankar    Notes:
5065d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
5066e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
5067d6ebe24aSShri Abhyankar 
50682bd2b0e6SSatish Balay    Level: advanced
50692bd2b0e6SSatish Balay 
5070d6ebe24aSShri Abhyankar @*/
5071d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
5072d6ebe24aSShri Abhyankar {
5073d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
5074d6ebe24aSShri Abhyankar   SNES           snes;
5075d6ebe24aSShri Abhyankar 
5076d6ebe24aSShri Abhyankar   PetscFunctionBegin;
5077d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
5078d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
5079d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
5080d6ebe24aSShri Abhyankar }
5081d6ebe24aSShri Abhyankar 
5082325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
5083c6db04a5SJed Brown #include <mex.h>
5084325fc9f4SBarry Smith 
5085325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
5086325fc9f4SBarry Smith 
5087325fc9f4SBarry Smith #undef __FUNCT__
5088325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
5089325fc9f4SBarry Smith /*
5090325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
5091325fc9f4SBarry Smith                          TSSetFunctionMatlab().
5092325fc9f4SBarry Smith 
5093325fc9f4SBarry Smith    Collective on TS
5094325fc9f4SBarry Smith 
5095325fc9f4SBarry Smith    Input Parameters:
5096325fc9f4SBarry Smith +  snes - the TS context
50970910c330SBarry Smith -  u - input vector
5098325fc9f4SBarry Smith 
5099325fc9f4SBarry Smith    Output Parameter:
5100325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
5101325fc9f4SBarry Smith 
5102325fc9f4SBarry Smith    Notes:
5103325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
5104325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
5105325fc9f4SBarry Smith    themselves.
5106325fc9f4SBarry Smith 
5107325fc9f4SBarry Smith    Level: developer
5108325fc9f4SBarry Smith 
5109325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5110325fc9f4SBarry Smith 
5111325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5112325fc9f4SBarry Smith */
51130910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
5114325fc9f4SBarry Smith {
5115325fc9f4SBarry Smith   PetscErrorCode  ierr;
5116325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5117325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
5118325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
5119325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
5120325fc9f4SBarry Smith 
5121325fc9f4SBarry Smith   PetscFunctionBegin;
5122325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
51230910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
51240910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
5125325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
51260910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
5127325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
5128325fc9f4SBarry Smith 
5129325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
51300910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
51310910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
51320910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
5133bbd56ea5SKarl Rupp 
5134325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5135325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
5136325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5137325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5138325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
5139325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
5140325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
5141325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
5142325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5143325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5144325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5145325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5146325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5147325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5148325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5149325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5150325fc9f4SBarry Smith   PetscFunctionReturn(0);
5151325fc9f4SBarry Smith }
5152325fc9f4SBarry Smith 
5153325fc9f4SBarry Smith 
5154325fc9f4SBarry Smith #undef __FUNCT__
5155325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
5156325fc9f4SBarry Smith /*
5157325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
5158325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
5159e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
5160325fc9f4SBarry Smith 
5161325fc9f4SBarry Smith    Logically Collective on TS
5162325fc9f4SBarry Smith 
5163325fc9f4SBarry Smith    Input Parameters:
5164325fc9f4SBarry Smith +  ts - the TS context
5165325fc9f4SBarry Smith -  func - function evaluation routine
5166325fc9f4SBarry Smith 
5167325fc9f4SBarry Smith    Calling sequence of func:
51680910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
5169325fc9f4SBarry Smith 
5170325fc9f4SBarry Smith    Level: beginner
5171325fc9f4SBarry Smith 
5172325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5173325fc9f4SBarry Smith 
5174325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5175325fc9f4SBarry Smith */
5176cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
5177325fc9f4SBarry Smith {
5178325fc9f4SBarry Smith   PetscErrorCode  ierr;
5179325fc9f4SBarry Smith   TSMatlabContext *sctx;
5180325fc9f4SBarry Smith 
5181325fc9f4SBarry Smith   PetscFunctionBegin;
5182325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5183325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5184325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5185325fc9f4SBarry Smith   /*
5186325fc9f4SBarry Smith      This should work, but it doesn't
5187325fc9f4SBarry Smith   sctx->ctx = ctx;
5188325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5189325fc9f4SBarry Smith   */
5190325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5191bbd56ea5SKarl Rupp 
51920298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
5193325fc9f4SBarry Smith   PetscFunctionReturn(0);
5194325fc9f4SBarry Smith }
5195325fc9f4SBarry Smith 
5196325fc9f4SBarry Smith #undef __FUNCT__
5197325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
5198325fc9f4SBarry Smith /*
5199325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
5200325fc9f4SBarry Smith                          TSSetJacobianMatlab().
5201325fc9f4SBarry Smith 
5202325fc9f4SBarry Smith    Collective on TS
5203325fc9f4SBarry Smith 
5204325fc9f4SBarry Smith    Input Parameters:
5205cdcf91faSSean Farley +  ts - the TS context
52060910c330SBarry Smith .  u - input vector
5207325fc9f4SBarry Smith .  A, B - the matrices
5208325fc9f4SBarry Smith -  ctx - user context
5209325fc9f4SBarry Smith 
5210325fc9f4SBarry Smith    Level: developer
5211325fc9f4SBarry Smith 
5212325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5213325fc9f4SBarry Smith 
5214325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5215325fc9f4SBarry Smith @*/
5216f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
5217325fc9f4SBarry Smith {
5218325fc9f4SBarry Smith   PetscErrorCode  ierr;
5219325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5220325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
5221325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
5222325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
5223325fc9f4SBarry Smith 
5224325fc9f4SBarry Smith   PetscFunctionBegin;
5225cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
52260910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
5227325fc9f4SBarry Smith 
52280910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
5229325fc9f4SBarry Smith 
5230cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
52310910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
52320910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
52330910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
52340910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
5235bbd56ea5SKarl Rupp 
5236325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5237325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
5238325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5239325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5240325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
5241325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
5242325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
5243325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
5244325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
5245325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
5246325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5247325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5248325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5249325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5250325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5251325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5252325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5253325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
5254325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
5255325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5256325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
5257325fc9f4SBarry Smith   PetscFunctionReturn(0);
5258325fc9f4SBarry Smith }
5259325fc9f4SBarry Smith 
5260325fc9f4SBarry Smith 
5261325fc9f4SBarry Smith #undef __FUNCT__
5262325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
5263325fc9f4SBarry Smith /*
5264325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
5265e3c5b3baSBarry 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
5266325fc9f4SBarry Smith 
5267325fc9f4SBarry Smith    Logically Collective on TS
5268325fc9f4SBarry Smith 
5269325fc9f4SBarry Smith    Input Parameters:
5270cdcf91faSSean Farley +  ts - the TS context
5271325fc9f4SBarry Smith .  A,B - Jacobian matrices
5272325fc9f4SBarry Smith .  func - function evaluation routine
5273325fc9f4SBarry Smith -  ctx - user context
5274325fc9f4SBarry Smith 
5275325fc9f4SBarry Smith    Calling sequence of func:
52760910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
5277325fc9f4SBarry Smith 
5278325fc9f4SBarry Smith 
5279325fc9f4SBarry Smith    Level: developer
5280325fc9f4SBarry Smith 
5281325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5282325fc9f4SBarry Smith 
5283325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5284325fc9f4SBarry Smith */
5285cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
5286325fc9f4SBarry Smith {
5287325fc9f4SBarry Smith   PetscErrorCode  ierr;
5288325fc9f4SBarry Smith   TSMatlabContext *sctx;
5289325fc9f4SBarry Smith 
5290325fc9f4SBarry Smith   PetscFunctionBegin;
5291325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5292325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5293325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5294325fc9f4SBarry Smith   /*
5295325fc9f4SBarry Smith      This should work, but it doesn't
5296325fc9f4SBarry Smith   sctx->ctx = ctx;
5297325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5298325fc9f4SBarry Smith   */
5299325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5300bbd56ea5SKarl Rupp 
5301cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
5302325fc9f4SBarry Smith   PetscFunctionReturn(0);
5303325fc9f4SBarry Smith }
5304325fc9f4SBarry Smith 
5305b5b1a830SBarry Smith #undef __FUNCT__
5306b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
5307b5b1a830SBarry Smith /*
5308b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
5309b5b1a830SBarry Smith 
5310b5b1a830SBarry Smith    Collective on TS
5311b5b1a830SBarry Smith 
5312b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5313b5b1a830SBarry Smith @*/
53140910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
5315b5b1a830SBarry Smith {
5316b5b1a830SBarry Smith   PetscErrorCode  ierr;
5317b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5318a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
5319b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
5320b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
5321b5b1a830SBarry Smith 
5322b5b1a830SBarry Smith   PetscFunctionBegin;
5323cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53240910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
5325b5b1a830SBarry Smith 
5326cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
53270910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
5328bbd56ea5SKarl Rupp 
5329b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5330b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
5331b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
5332b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
5333b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
5334b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
5335b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
5336b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5337b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
5338b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
5339b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
5340b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
5341b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
5342b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
5343b5b1a830SBarry Smith   PetscFunctionReturn(0);
5344b5b1a830SBarry Smith }
5345b5b1a830SBarry Smith 
5346b5b1a830SBarry Smith 
5347b5b1a830SBarry Smith #undef __FUNCT__
5348b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
5349b5b1a830SBarry Smith /*
5350b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
5351b5b1a830SBarry Smith 
5352b5b1a830SBarry Smith    Level: developer
5353b5b1a830SBarry Smith 
5354b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
5355b5b1a830SBarry Smith 
5356b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5357b5b1a830SBarry Smith */
5358cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
5359b5b1a830SBarry Smith {
5360b5b1a830SBarry Smith   PetscErrorCode  ierr;
5361b5b1a830SBarry Smith   TSMatlabContext *sctx;
5362b5b1a830SBarry Smith 
5363b5b1a830SBarry Smith   PetscFunctionBegin;
5364b5b1a830SBarry Smith   /* currently sctx is memory bleed */
5365b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5366b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5367b5b1a830SBarry Smith   /*
5368b5b1a830SBarry Smith      This should work, but it doesn't
5369b5b1a830SBarry Smith   sctx->ctx = ctx;
5370b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5371b5b1a830SBarry Smith   */
5372b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5373bbd56ea5SKarl Rupp 
53740298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
5375b5b1a830SBarry Smith   PetscFunctionReturn(0);
5376b5b1a830SBarry Smith }
5377325fc9f4SBarry Smith #endif
5378b3603a34SBarry Smith 
53790b039ecaSBarry Smith 
53800b039ecaSBarry Smith 
5381b3603a34SBarry Smith #undef __FUNCT__
53824f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
5383b3603a34SBarry Smith /*@C
53844f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
5385b3603a34SBarry Smith        in a time based line graph
5386b3603a34SBarry Smith 
5387b3603a34SBarry Smith    Collective on TS
5388b3603a34SBarry Smith 
5389b3603a34SBarry Smith    Input Parameters:
5390b3603a34SBarry Smith +  ts - the TS context
5391b3603a34SBarry Smith .  step - current time-step
5392b3603a34SBarry Smith .  ptime - current time
5393b3603a34SBarry Smith -  lg - a line graph object
5394b3603a34SBarry Smith 
5395b3603a34SBarry Smith    Level: intermediate
5396b3603a34SBarry Smith 
53970b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
5398b3603a34SBarry Smith 
5399b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
5400b3603a34SBarry Smith 
5401b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5402b3603a34SBarry Smith @*/
54030910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
5404b3603a34SBarry Smith {
5405b3603a34SBarry Smith   PetscErrorCode    ierr;
54060b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
5407b3603a34SBarry Smith   const PetscScalar *yy;
540858ff32f7SBarry Smith   PetscInt          dim;
5409b3603a34SBarry Smith 
5410b3603a34SBarry Smith   PetscFunctionBegin;
541158ff32f7SBarry Smith   if (!step) {
5412a9f9c1f6SBarry Smith     PetscDrawAxis axis;
5413a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5414a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
54150910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
54160b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
54170b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
541858ff32f7SBarry Smith   }
54190910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
5420e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
5421e3efe391SJed Brown   {
5422e3efe391SJed Brown     PetscReal *yreal;
5423e3efe391SJed Brown     PetscInt  i,n;
54240910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
5425785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
5426e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
54270b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
5428e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
5429e3efe391SJed Brown   }
5430e3efe391SJed Brown #else
54310b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
5432e3efe391SJed Brown #endif
54330910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
5434b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
54350b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
54363923b477SBarry Smith   }
5437b3603a34SBarry Smith   PetscFunctionReturn(0);
5438b3603a34SBarry Smith }
5439b3603a34SBarry Smith 
5440b3603a34SBarry Smith #undef __FUNCT__
54414f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
5442ef20d060SBarry Smith /*@C
54434f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
5444ef20d060SBarry Smith        in a time based line graph
5445ef20d060SBarry Smith 
5446ef20d060SBarry Smith    Collective on TS
5447ef20d060SBarry Smith 
5448ef20d060SBarry Smith    Input Parameters:
5449ef20d060SBarry Smith +  ts - the TS context
5450ef20d060SBarry Smith .  step - current time-step
5451ef20d060SBarry Smith .  ptime - current time
5452ef20d060SBarry Smith -  lg - a line graph object
5453ef20d060SBarry Smith 
5454ef20d060SBarry Smith    Level: intermediate
5455ef20d060SBarry Smith 
5456abd5a294SJed Brown    Notes:
5457abd5a294SJed Brown    Only for sequential solves.
5458abd5a294SJed Brown 
5459abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
5460abd5a294SJed Brown 
5461abd5a294SJed Brown    Options Database Keys:
54624f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
5463ef20d060SBarry Smith 
5464ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
5465ef20d060SBarry Smith 
5466abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
5467ef20d060SBarry Smith @*/
54680910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
5469ef20d060SBarry Smith {
5470ef20d060SBarry Smith   PetscErrorCode    ierr;
54710b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
5472ef20d060SBarry Smith   const PetscScalar *yy;
5473ef20d060SBarry Smith   Vec               y;
5474a9f9c1f6SBarry Smith   PetscInt          dim;
5475ef20d060SBarry Smith 
5476ef20d060SBarry Smith   PetscFunctionBegin;
5477a9f9c1f6SBarry Smith   if (!step) {
5478a9f9c1f6SBarry Smith     PetscDrawAxis axis;
5479a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
54801ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
54810910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
5482a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
5483a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5484a9f9c1f6SBarry Smith   }
54850910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
5486ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
54870910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
5488ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
5489e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
5490e3efe391SJed Brown   {
5491e3efe391SJed Brown     PetscReal *yreal;
5492e3efe391SJed Brown     PetscInt  i,n;
5493e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
5494785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
5495e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
54960b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
5497e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
5498e3efe391SJed Brown   }
5499e3efe391SJed Brown #else
55000b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
5501e3efe391SJed Brown #endif
5502ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
5503ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
5504b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
55050b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
55063923b477SBarry Smith   }
5507ef20d060SBarry Smith   PetscFunctionReturn(0);
5508ef20d060SBarry Smith }
5509ef20d060SBarry Smith 
5510201da799SBarry Smith #undef __FUNCT__
5511201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
5512201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
5513201da799SBarry Smith {
5514201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
5515201da799SBarry Smith   PetscReal      x   = ptime,y;
5516201da799SBarry Smith   PetscErrorCode ierr;
5517201da799SBarry Smith   PetscInt       its;
5518201da799SBarry Smith 
5519201da799SBarry Smith   PetscFunctionBegin;
5520201da799SBarry Smith   if (!n) {
5521201da799SBarry Smith     PetscDrawAxis axis;
5522bbd56ea5SKarl Rupp 
5523201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5524201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
5525201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5526bbd56ea5SKarl Rupp 
5527201da799SBarry Smith     ctx->snes_its = 0;
5528201da799SBarry Smith   }
5529201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
5530201da799SBarry Smith   y    = its - ctx->snes_its;
5531201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
55323fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
5533201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
5534201da799SBarry Smith   }
5535201da799SBarry Smith   ctx->snes_its = its;
5536201da799SBarry Smith   PetscFunctionReturn(0);
5537201da799SBarry Smith }
5538201da799SBarry Smith 
5539201da799SBarry Smith #undef __FUNCT__
5540201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
5541201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
5542201da799SBarry Smith {
5543201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
5544201da799SBarry Smith   PetscReal      x   = ptime,y;
5545201da799SBarry Smith   PetscErrorCode ierr;
5546201da799SBarry Smith   PetscInt       its;
5547201da799SBarry Smith 
5548201da799SBarry Smith   PetscFunctionBegin;
5549201da799SBarry Smith   if (!n) {
5550201da799SBarry Smith     PetscDrawAxis axis;
5551bbd56ea5SKarl Rupp 
5552201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5553201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
5554201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5555bbd56ea5SKarl Rupp 
5556201da799SBarry Smith     ctx->ksp_its = 0;
5557201da799SBarry Smith   }
5558201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
5559201da799SBarry Smith   y    = its - ctx->ksp_its;
5560201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
556199fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
5562201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
5563201da799SBarry Smith   }
5564201da799SBarry Smith   ctx->ksp_its = its;
5565201da799SBarry Smith   PetscFunctionReturn(0);
5566201da799SBarry Smith }
5567f9c1d6abSBarry Smith 
5568f9c1d6abSBarry Smith #undef __FUNCT__
5569f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
5570f9c1d6abSBarry Smith /*@
5571f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
5572f9c1d6abSBarry Smith 
5573f9c1d6abSBarry Smith    Collective on TS and Vec
5574f9c1d6abSBarry Smith 
5575f9c1d6abSBarry Smith    Input Parameters:
5576f9c1d6abSBarry Smith +  ts - the TS context
5577f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
5578f9c1d6abSBarry Smith 
5579f9c1d6abSBarry Smith    Output Parameters:
5580f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
5581f9c1d6abSBarry Smith 
5582f9c1d6abSBarry Smith    Level: developer
5583f9c1d6abSBarry Smith 
5584f9c1d6abSBarry Smith .keywords: TS, compute
5585f9c1d6abSBarry Smith 
5586f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
5587f9c1d6abSBarry Smith @*/
5588f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
5589f9c1d6abSBarry Smith {
5590f9c1d6abSBarry Smith   PetscErrorCode ierr;
5591f9c1d6abSBarry Smith 
5592f9c1d6abSBarry Smith   PetscFunctionBegin;
5593f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5594ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
5595f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
5596f9c1d6abSBarry Smith   PetscFunctionReturn(0);
5597f9c1d6abSBarry Smith }
559824655328SShri 
559924655328SShri #undef __FUNCT__
560024655328SShri #define __FUNCT__ "TSRollBack"
560124655328SShri /*@
560224655328SShri    TSRollBack - Rolls back one time step
560324655328SShri 
560424655328SShri    Collective on TS
560524655328SShri 
560624655328SShri    Input Parameter:
560724655328SShri .  ts - the TS context obtained from TSCreate()
560824655328SShri 
560924655328SShri    Level: advanced
561024655328SShri 
561124655328SShri .keywords: TS, timestep, rollback
561224655328SShri 
561324655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
561424655328SShri @*/
561524655328SShri PetscErrorCode  TSRollBack(TS ts)
561624655328SShri {
561724655328SShri   PetscErrorCode ierr;
561824655328SShri 
561924655328SShri   PetscFunctionBegin;
562024655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
562124655328SShri 
562224655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
562324655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
562424655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
562524655328SShri   ts->ptime = ts->ptime_prev;
562624655328SShri   PetscFunctionReturn(0);
562724655328SShri }
5628aeb4809dSShri Abhyankar 
5629ff22ae23SHong Zhang #undef __FUNCT__
5630ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages"
5631ff22ae23SHong Zhang /*@
5632ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
5633ff22ae23SHong Zhang 
5634ff22ae23SHong Zhang    Input Parameter:
5635ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
5636ff22ae23SHong Zhang 
5637ff22ae23SHong Zhang    Level: advanced
5638ff22ae23SHong Zhang 
5639ff22ae23SHong Zhang .keywords: TS, getstages
5640ff22ae23SHong Zhang 
5641ff22ae23SHong Zhang .seealso: TSCreate()
5642ff22ae23SHong Zhang @*/
5643ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns, Vec **Y)
5644ff22ae23SHong Zhang {
5645ff22ae23SHong Zhang   PetscErrorCode ierr;
5646ff22ae23SHong Zhang 
5647ff22ae23SHong Zhang   PetscFunctionBegin;
5648ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
5649ff22ae23SHong Zhang   PetscValidPointer(ns,2);
5650ff22ae23SHong Zhang 
5651ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
5652ff22ae23SHong Zhang   else {
5653ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
5654ff22ae23SHong Zhang   }
5655ff22ae23SHong Zhang   PetscFunctionReturn(0);
5656ff22ae23SHong Zhang }
5657ff22ae23SHong Zhang 
5658