xref: /petsc/src/ts/interface/ts.c (revision c235aa8dcc08e09f20ce58350062050a1d3caf1f)
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 
40607a6623SBarry Smith   if (!TSRegisterAllCalled) {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 {
105ace3abfcSBarry Smith   PetscBool              opt,flg;
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 */
122d2daff3dSHong Zhang   ierr = PetscOptionsBool("-ts_checkpoint","Checkpoint for adjoint sensitivity analysis","TSSetCheckpoint",ts->checkpoint,&ts->checkpoint,NULL);CHKERRQ(ierr);
1230298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1240298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
1250298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
12631748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
12731748224SBarry Smith   if (flg) {
12831748224SBarry Smith     ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
12931748224SBarry Smith   }
13049354f04SShri 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);
13149354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1320298fd71SBarry 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);
1330298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1340298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1350298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1360298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
137bdad233fSMatthew Knepley 
13856f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
13956f85f32SBarry Smith   {
14056f85f32SBarry Smith   PetscBool set;
14156f85f32SBarry Smith   flg  = PETSC_FALSE;
14256f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
14356f85f32SBarry Smith   if (set) {
14456f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
14556f85f32SBarry Smith   }
14656f85f32SBarry Smith   }
14756f85f32SBarry Smith #endif
14856f85f32SBarry Smith 
149bdad233fSMatthew Knepley   /* Monitor options */
150a6570f20SBarry Smith   ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
151eabae89aSBarry Smith   if (flg) {
152ce94432eSBarry Smith     ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr);
153649052a6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
154bdad233fSMatthew Knepley   }
1555180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1565180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1575180491cSLisandro Dalcin 
1584f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
159a7cc72afSBarry Smith   if (opt) {
1600b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1613923b477SBarry Smith     PetscInt       howoften = 1;
162a80ad3e0SBarry Smith 
1630298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
164ce94432eSBarry Smith     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
1654f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
166b3603a34SBarry Smith   }
1674f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
168b3603a34SBarry Smith   if (opt) {
1690b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1703923b477SBarry Smith     PetscInt       howoften = 1;
171b3603a34SBarry Smith 
1720298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
17322d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1744f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
175bdad233fSMatthew Knepley   }
1764f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
177ef20d060SBarry Smith   if (opt) {
1780b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1793923b477SBarry Smith     PetscInt       howoften = 1;
180ef20d060SBarry Smith 
1810298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
18222d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1834f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
184ef20d060SBarry Smith   }
185201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
186201da799SBarry Smith   if (opt) {
187201da799SBarry Smith     TSMonitorLGCtx ctx;
188201da799SBarry Smith     PetscInt       howoften = 1;
189201da799SBarry Smith 
1900298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
19122d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
192201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
193201da799SBarry Smith   }
194201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
195201da799SBarry Smith   if (opt) {
196201da799SBarry Smith     TSMonitorLGCtx ctx;
197201da799SBarry Smith     PetscInt       howoften = 1;
198201da799SBarry Smith 
1990298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
20022d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
201201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
202201da799SBarry Smith   }
2038189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
2048189c53fSBarry Smith   if (opt) {
2058189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2068189c53fSBarry Smith     PetscInt          howoften = 1;
2078189c53fSBarry Smith 
2080298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
20922d28d08SBarry Smith     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
2108189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2118189c53fSBarry Smith   }
212ef20d060SBarry Smith   opt  = PETSC_FALSE;
2130dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
214a7cc72afSBarry Smith   if (opt) {
21583a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
21683a4ac43SBarry Smith     PetscInt         howoften = 1;
217a80ad3e0SBarry Smith 
2180298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
219ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
22083a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
221bdad233fSMatthew Knepley   }
222fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2232d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2242d5ee99bSBarry Smith   if (opt) {
2252d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2262d5ee99bSBarry Smith     PetscReal        bounds[4];
2272d5ee99bSBarry Smith     PetscInt         n = 4;
2282d5ee99bSBarry Smith     PetscDraw        draw;
2292d5ee99bSBarry Smith 
2302d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
2312d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
2322d5ee99bSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr);
2332d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
2342d5ee99bSBarry Smith     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
2354b363babSBarry Smith     ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr);
2364b363babSBarry Smith     ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
237f54adfc0SBarry Smith     ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
2384b363babSBarry Smith     ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr);
23907995780SPeter Brune     /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */
2402d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2412d5ee99bSBarry Smith   }
2422d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2430dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
2443a471f94SBarry Smith   if (opt) {
24583a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
24683a4ac43SBarry Smith     PetscInt         howoften = 1;
2473a471f94SBarry Smith 
2480298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
249ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
25083a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2513a471f94SBarry Smith   }
2523a471f94SBarry Smith   opt  = PETSC_FALSE;
25391b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
254fb1732b5SBarry Smith   if (flg) {
255fb1732b5SBarry Smith     PetscViewer ctx;
256fb1732b5SBarry Smith     if (monfilename[0]) {
257ce94432eSBarry Smith       ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
258c2fbc07fSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
259fb1732b5SBarry Smith     } else {
260ce94432eSBarry Smith       ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts));
2610298fd71SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr);
262fb1732b5SBarry Smith     }
263fb1732b5SBarry Smith   }
264ed81e22dSJed Brown   opt  = PETSC_FALSE;
26591b97e58SBarry 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);
266ed81e22dSJed Brown   if (flg) {
267ed81e22dSJed Brown     const char *ptr,*ptr2;
268ed81e22dSJed Brown     char       *filetemplate;
269ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
270ed81e22dSJed Brown     /* Do some cursory validation of the input. */
271ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
272ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
273ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
274ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
275ce94432eSBarry 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");
276ed81e22dSJed Brown       if (ptr2) break;
277ed81e22dSJed Brown     }
278ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
279ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
280ed81e22dSJed Brown   }
281bdad233fSMatthew Knepley 
282d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
283d1212d36SBarry Smith   if (flg) {
284d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
285d1212d36SBarry Smith     int                  ray = 0;
286d1212d36SBarry Smith     DMDADirection        ddir;
287d1212d36SBarry Smith     DM                   da;
288d1212d36SBarry Smith     PetscMPIInt          rank;
289d1212d36SBarry Smith 
290ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
291d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
292d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
293ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
294d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
295d1212d36SBarry Smith 
296d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
297b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
298d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
299d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
300ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
301d1212d36SBarry Smith     if (!rank) {
302d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
303d1212d36SBarry Smith     }
30451b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
305d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
306d1212d36SBarry Smith   }
30751b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
30851b4a12fSMatthew G. Knepley   if (flg) {
30951b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
31051b4a12fSMatthew G. Knepley     int                 ray = 0;
31151b4a12fSMatthew G. Knepley     DMDADirection       ddir;
31251b4a12fSMatthew G. Knepley     DM                  da;
31351b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
314d1212d36SBarry Smith 
31551b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
31651b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
31751b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
31851b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
31951b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
32051b4a12fSMatthew G. Knepley 
32151b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
322b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
32351b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
32451b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
32551b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
32651b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
32751b4a12fSMatthew G. Knepley   }
328a7a1495cSBarry Smith 
3297781c08eSBarry Smith   /*
3307781c08eSBarry Smith      This code is all wrong. One is creating objects inside the TSSetFromOptions() so if run with the options gui
3317781c08eSBarry Smith      will bleed memory. Also one is using a PetscOptionsBegin() inside a PetscOptionsBegin()
3327781c08eSBarry Smith   */
333552698daSJed Brown   ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
334e55864a3SBarry Smith   ierr = TSAdaptSetFromOptions(PetscOptionsObject,adapt);CHKERRQ(ierr);
335d763cef2SBarry Smith 
336d763cef2SBarry Smith   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
337d763cef2SBarry Smith   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
338d763cef2SBarry Smith 
339d763cef2SBarry Smith   /* Handle specific TS options */
340d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
341e55864a3SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
342d763cef2SBarry Smith   }
343d763cef2SBarry Smith 
344d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
345d763cef2SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
346d763cef2SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
347d763cef2SBarry Smith   PetscFunctionReturn(0);
348d763cef2SBarry Smith }
349d763cef2SBarry Smith 
350d763cef2SBarry Smith #undef __FUNCT__
351d2daff3dSHong Zhang #define __FUNCT__ "TSSetCheckpoint"
352d2daff3dSHong Zhang /*@
353d2daff3dSHong Zhang    TSSetCheckpoint - Allows one to checkpoint the forward run,
354d2daff3dSHong Zhang    useful for adjoint sensitivity analysis.
355d2daff3dSHong Zhang 
356d2daff3dSHong Zhang    Collective on TS
357d2daff3dSHong Zhang 
358d2daff3dSHong Zhang    Input Parameters:
359d2daff3dSHong Zhang +  ts - the TS context obtained from TSCreate()
360d2daff3dSHong Zhang -  checkpoint - flag that indicates if the forward solution will be checkpointed
361d2daff3dSHong Zhang 
362d2daff3dSHong Zhang    Level: intermediate
363d2daff3dSHong Zhang 
364d2daff3dSHong Zhang .seealso:
365d2daff3dSHong Zhang 
366d2daff3dSHong Zhang .keywords: TS, set, checkpoint
367d2daff3dSHong Zhang @*/
368d2daff3dSHong Zhang PetscErrorCode  TSSetCheckpoint(TS ts,PetscBool checkpoint)
369d2daff3dSHong Zhang {
370d2daff3dSHong Zhang   PetscFunctionBegin;
371d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
372d2daff3dSHong Zhang   ts->checkpoint = checkpoint;
373d2daff3dSHong Zhang   PetscFunctionReturn(0);
374d2daff3dSHong Zhang }
375d2daff3dSHong Zhang 
376d2daff3dSHong Zhang #undef __FUNCT__
377d2daff3dSHong Zhang #define __FUNCT__ "TSSetReverseMode"
378d2daff3dSHong Zhang /*@
379d2daff3dSHong Zhang    TSSetRunMode - Allows one to reset the run mode,
380d2daff3dSHong Zhang    useful for adjoint sensitivity analysis.
381d2daff3dSHong Zhang 
382d2daff3dSHong Zhang    Collective on TS
383d2daff3dSHong Zhang 
384d2daff3dSHong Zhang    Input Parameters:
385d2daff3dSHong Zhang +  ts - the TS context obtained from TSCreate()
386d2daff3dSHong Zhang -  reverse_mode - run in forward (default) or reverse mode
387d2daff3dSHong Zhang 
388d2daff3dSHong Zhang    Level: intermediate
389d2daff3dSHong Zhang 
390d2daff3dSHong Zhang .seealso:
391d2daff3dSHong Zhang 
392d2daff3dSHong Zhang .keywords: TS, set, reverse
393d2daff3dSHong Zhang @*/
394d2daff3dSHong Zhang PetscErrorCode  TSSetReverseMode(TS ts,PetscBool reverse_mode)
395d2daff3dSHong Zhang {
396d2daff3dSHong Zhang   PetscFunctionBegin;
397d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
398d2daff3dSHong Zhang   ts->reverse_mode = reverse_mode;
399d2daff3dSHong Zhang   PetscFunctionReturn(0);
400d2daff3dSHong Zhang }
401d2daff3dSHong Zhang 
402a7a1495cSBarry Smith #undef __FUNCT__
403a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian"
404a7a1495cSBarry Smith /*@
405a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
406a7a1495cSBarry Smith       set with TSSetRHSJacobian().
407a7a1495cSBarry Smith 
408a7a1495cSBarry Smith    Collective on TS and Vec
409a7a1495cSBarry Smith 
410a7a1495cSBarry Smith    Input Parameters:
411316643e7SJed Brown +  ts - the TS context
412a7a1495cSBarry Smith .  t - current timestep
4130910c330SBarry Smith -  U - input vector
414a7a1495cSBarry Smith 
415a7a1495cSBarry Smith    Output Parameters:
416a7a1495cSBarry Smith +  A - Jacobian matrix
417a7a1495cSBarry Smith .  B - optional preconditioning matrix
418a7a1495cSBarry Smith -  flag - flag indicating matrix structure
419a7a1495cSBarry Smith 
420a7a1495cSBarry Smith    Notes:
421a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
422a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
423a7a1495cSBarry Smith 
42494b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
425a7a1495cSBarry Smith    flag parameter.
426a7a1495cSBarry Smith 
427a7a1495cSBarry Smith    Level: developer
428a7a1495cSBarry Smith 
429a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
430a7a1495cSBarry Smith 
43194b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
432a7a1495cSBarry Smith @*/
433d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
434a7a1495cSBarry Smith {
435dfbe8321SBarry Smith   PetscErrorCode ierr;
436270bf2e7SJed Brown   PetscObjectState Ustate;
43724989b8cSPeter Brune   DM             dm;
438942e3340SBarry Smith   DMTS           tsdm;
43924989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
44024989b8cSPeter Brune   void           *ctx;
44124989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
442a7a1495cSBarry Smith 
443a7a1495cSBarry Smith   PetscFunctionBegin;
4440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4450910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4460910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
44724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
448942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
44924989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
4500298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
45159e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
4520910c330SBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) {
4530e4ef248SJed Brown     PetscFunctionReturn(0);
454f8ede8e7SJed Brown   }
455d90be118SSean Farley 
456ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
457d90be118SSean Farley 
458e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
45994ab13aaSBarry Smith     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
46094ab13aaSBarry Smith     ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
46194ab13aaSBarry Smith     if (A != B) {
46294ab13aaSBarry Smith       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
46394ab13aaSBarry Smith       ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
464e1244c69SJed Brown     }
465e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
466e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
467e1244c69SJed Brown   }
468e1244c69SJed Brown 
46924989b8cSPeter Brune   if (rhsjacobianfunc) {
47094ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
471a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
472d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
473a7a1495cSBarry Smith     PetscStackPop;
47494ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
475a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
47694ab13aaSBarry Smith     PetscValidHeaderSpecific(A,MAT_CLASSID,4);
47794ab13aaSBarry Smith     PetscValidHeaderSpecific(B,MAT_CLASSID,5);
478ef66eb69SBarry Smith   } else {
47994ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
48094ab13aaSBarry Smith     if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
481ef66eb69SBarry Smith   }
4820e4ef248SJed Brown   ts->rhsjacobian.time       = t;
4830910c330SBarry Smith   ts->rhsjacobian.X          = U;
48459e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
485a7a1495cSBarry Smith   PetscFunctionReturn(0);
486a7a1495cSBarry Smith }
487a7a1495cSBarry Smith 
4884a2ae208SSatish Balay #undef __FUNCT__
4894a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
490316643e7SJed Brown /*@
491d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
492d763cef2SBarry Smith 
493316643e7SJed Brown    Collective on TS and Vec
494316643e7SJed Brown 
495316643e7SJed Brown    Input Parameters:
496316643e7SJed Brown +  ts - the TS context
497316643e7SJed Brown .  t - current time
4980910c330SBarry Smith -  U - state vector
499316643e7SJed Brown 
500316643e7SJed Brown    Output Parameter:
501316643e7SJed Brown .  y - right hand side
502316643e7SJed Brown 
503316643e7SJed Brown    Note:
504316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
505316643e7SJed Brown    is used internally within the nonlinear solvers.
506316643e7SJed Brown 
507316643e7SJed Brown    Level: developer
508316643e7SJed Brown 
509316643e7SJed Brown .keywords: TS, compute
510316643e7SJed Brown 
511316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
512316643e7SJed Brown @*/
5130910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
514d763cef2SBarry Smith {
515dfbe8321SBarry Smith   PetscErrorCode ierr;
51624989b8cSPeter Brune   TSRHSFunction  rhsfunction;
51724989b8cSPeter Brune   TSIFunction    ifunction;
51824989b8cSPeter Brune   void           *ctx;
51924989b8cSPeter Brune   DM             dm;
52024989b8cSPeter Brune 
521d763cef2SBarry Smith   PetscFunctionBegin;
5220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5230910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5240700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
52524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
52624989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
5270298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
528d763cef2SBarry Smith 
529ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
530d763cef2SBarry Smith 
5310910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
53224989b8cSPeter Brune   if (rhsfunction) {
533d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
5340910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
535d763cef2SBarry Smith     PetscStackPop;
536214bc6a2SJed Brown   } else {
537214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
538b2cd27e8SJed Brown   }
53944a41b28SSean Farley 
5400910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
541d763cef2SBarry Smith   PetscFunctionReturn(0);
542d763cef2SBarry Smith }
543d763cef2SBarry Smith 
5444a2ae208SSatish Balay #undef __FUNCT__
545ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
546ef20d060SBarry Smith /*@
547ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
548ef20d060SBarry Smith 
549ef20d060SBarry Smith    Collective on TS and Vec
550ef20d060SBarry Smith 
551ef20d060SBarry Smith    Input Parameters:
552ef20d060SBarry Smith +  ts - the TS context
553ef20d060SBarry Smith -  t - current time
554ef20d060SBarry Smith 
555ef20d060SBarry Smith    Output Parameter:
5560910c330SBarry Smith .  U - the solution
557ef20d060SBarry Smith 
558ef20d060SBarry Smith    Note:
559ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
560ef20d060SBarry Smith    is used internally within the nonlinear solvers.
561ef20d060SBarry Smith 
562ef20d060SBarry Smith    Level: developer
563ef20d060SBarry Smith 
564ef20d060SBarry Smith .keywords: TS, compute
565ef20d060SBarry Smith 
566abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
567ef20d060SBarry Smith @*/
5680910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
569ef20d060SBarry Smith {
570ef20d060SBarry Smith   PetscErrorCode     ierr;
571ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
572ef20d060SBarry Smith   void               *ctx;
573ef20d060SBarry Smith   DM                 dm;
574ef20d060SBarry Smith 
575ef20d060SBarry Smith   PetscFunctionBegin;
576ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5770910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
578ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
579ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
580ef20d060SBarry Smith 
581ef20d060SBarry Smith   if (solutionfunction) {
5829b7cd975SBarry Smith     PetscStackPush("TS user solution function");
5830910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
584ef20d060SBarry Smith     PetscStackPop;
585ef20d060SBarry Smith   }
586ef20d060SBarry Smith   PetscFunctionReturn(0);
587ef20d060SBarry Smith }
5889b7cd975SBarry Smith #undef __FUNCT__
5899b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
5909b7cd975SBarry Smith /*@
5919b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
5929b7cd975SBarry Smith 
5939b7cd975SBarry Smith    Collective on TS and Vec
5949b7cd975SBarry Smith 
5959b7cd975SBarry Smith    Input Parameters:
5969b7cd975SBarry Smith +  ts - the TS context
5979b7cd975SBarry Smith -  t - current time
5989b7cd975SBarry Smith 
5999b7cd975SBarry Smith    Output Parameter:
6009b7cd975SBarry Smith .  U - the function value
6019b7cd975SBarry Smith 
6029b7cd975SBarry Smith    Note:
6039b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
6049b7cd975SBarry Smith    is used internally within the nonlinear solvers.
6059b7cd975SBarry Smith 
6069b7cd975SBarry Smith    Level: developer
6079b7cd975SBarry Smith 
6089b7cd975SBarry Smith .keywords: TS, compute
6099b7cd975SBarry Smith 
6109b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
6119b7cd975SBarry Smith @*/
6129b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
6139b7cd975SBarry Smith {
6149b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
6159b7cd975SBarry Smith   void               *ctx;
6169b7cd975SBarry Smith   DM                 dm;
6179b7cd975SBarry Smith 
6189b7cd975SBarry Smith   PetscFunctionBegin;
6199b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6209b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6219b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
6229b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
6239b7cd975SBarry Smith 
6249b7cd975SBarry Smith   if (forcing) {
6259b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
6269b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
6279b7cd975SBarry Smith     PetscStackPop;
6289b7cd975SBarry Smith   }
6299b7cd975SBarry Smith   PetscFunctionReturn(0);
6309b7cd975SBarry Smith }
631ef20d060SBarry Smith 
632ef20d060SBarry Smith #undef __FUNCT__
633214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
634214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
635214bc6a2SJed Brown {
6362dd45cf8SJed Brown   Vec            F;
637214bc6a2SJed Brown   PetscErrorCode ierr;
638214bc6a2SJed Brown 
639214bc6a2SJed Brown   PetscFunctionBegin;
6400298fd71SBarry Smith   *Frhs = NULL;
6410298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
642214bc6a2SJed Brown   if (!ts->Frhs) {
6432dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
644214bc6a2SJed Brown   }
645214bc6a2SJed Brown   *Frhs = ts->Frhs;
646214bc6a2SJed Brown   PetscFunctionReturn(0);
647214bc6a2SJed Brown }
648214bc6a2SJed Brown 
649214bc6a2SJed Brown #undef __FUNCT__
650214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
651214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
652214bc6a2SJed Brown {
653214bc6a2SJed Brown   Mat            A,B;
6542dd45cf8SJed Brown   PetscErrorCode ierr;
655214bc6a2SJed Brown 
656214bc6a2SJed Brown   PetscFunctionBegin;
657c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
658c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
6590298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
660214bc6a2SJed Brown   if (Arhs) {
661214bc6a2SJed Brown     if (!ts->Arhs) {
662214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
663214bc6a2SJed Brown     }
664214bc6a2SJed Brown     *Arhs = ts->Arhs;
665214bc6a2SJed Brown   }
666214bc6a2SJed Brown   if (Brhs) {
667214bc6a2SJed Brown     if (!ts->Brhs) {
668bdb70873SJed Brown       if (A != B) {
669214bc6a2SJed Brown         ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
670bdb70873SJed Brown       } else {
671bdb70873SJed Brown         ts->Brhs = ts->Arhs;
672bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
673bdb70873SJed Brown       }
674214bc6a2SJed Brown     }
675214bc6a2SJed Brown     *Brhs = ts->Brhs;
676214bc6a2SJed Brown   }
677214bc6a2SJed Brown   PetscFunctionReturn(0);
678214bc6a2SJed Brown }
679214bc6a2SJed Brown 
680214bc6a2SJed Brown #undef __FUNCT__
681316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
682316643e7SJed Brown /*@
6830910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
684316643e7SJed Brown 
685316643e7SJed Brown    Collective on TS and Vec
686316643e7SJed Brown 
687316643e7SJed Brown    Input Parameters:
688316643e7SJed Brown +  ts - the TS context
689316643e7SJed Brown .  t - current time
6900910c330SBarry Smith .  U - state vector
6910910c330SBarry Smith .  Udot - time derivative of state vector
692214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
693316643e7SJed Brown 
694316643e7SJed Brown    Output Parameter:
695316643e7SJed Brown .  Y - right hand side
696316643e7SJed Brown 
697316643e7SJed Brown    Note:
698316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
699316643e7SJed Brown    is used internally within the nonlinear solvers.
700316643e7SJed Brown 
701316643e7SJed Brown    If the user did did not write their equations in implicit form, this
702316643e7SJed Brown    function recasts them in implicit form.
703316643e7SJed Brown 
704316643e7SJed Brown    Level: developer
705316643e7SJed Brown 
706316643e7SJed Brown .keywords: TS, compute
707316643e7SJed Brown 
708316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
709316643e7SJed Brown @*/
7100910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
711316643e7SJed Brown {
712316643e7SJed Brown   PetscErrorCode ierr;
71324989b8cSPeter Brune   TSIFunction    ifunction;
71424989b8cSPeter Brune   TSRHSFunction  rhsfunction;
71524989b8cSPeter Brune   void           *ctx;
71624989b8cSPeter Brune   DM             dm;
717316643e7SJed Brown 
718316643e7SJed Brown   PetscFunctionBegin;
7190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7200910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7210910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
7220700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
723316643e7SJed Brown 
72424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
72524989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
7260298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
72724989b8cSPeter Brune 
728ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
729d90be118SSean Farley 
7300910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
73124989b8cSPeter Brune   if (ifunction) {
732316643e7SJed Brown     PetscStackPush("TS user implicit function");
7330910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
734316643e7SJed Brown     PetscStackPop;
735214bc6a2SJed Brown   }
736214bc6a2SJed Brown   if (imex) {
73724989b8cSPeter Brune     if (!ifunction) {
7380910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
7392dd45cf8SJed Brown     }
74024989b8cSPeter Brune   } else if (rhsfunction) {
74124989b8cSPeter Brune     if (ifunction) {
742214bc6a2SJed Brown       Vec Frhs;
743214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
7440910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
745214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
7462dd45cf8SJed Brown     } else {
7470910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
7480910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
749316643e7SJed Brown     }
7504a6899ffSJed Brown   }
7510910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
752316643e7SJed Brown   PetscFunctionReturn(0);
753316643e7SJed Brown }
754316643e7SJed Brown 
755316643e7SJed Brown #undef __FUNCT__
756316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
757316643e7SJed Brown /*@
758316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
759316643e7SJed Brown 
760316643e7SJed Brown    Collective on TS and Vec
761316643e7SJed Brown 
762316643e7SJed Brown    Input
763316643e7SJed Brown       Input Parameters:
764316643e7SJed Brown +  ts - the TS context
765316643e7SJed Brown .  t - current timestep
7660910c330SBarry Smith .  U - state vector
7670910c330SBarry Smith .  Udot - time derivative of state vector
768214bc6a2SJed Brown .  shift - shift to apply, see note below
769214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
770316643e7SJed Brown 
771316643e7SJed Brown    Output Parameters:
772316643e7SJed Brown +  A - Jacobian matrix
773316643e7SJed Brown .  B - optional preconditioning matrix
774316643e7SJed Brown -  flag - flag indicating matrix structure
775316643e7SJed Brown 
776316643e7SJed Brown    Notes:
7770910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
778316643e7SJed Brown 
7790910c330SBarry Smith    dF/dU + shift*dF/dUdot
780316643e7SJed Brown 
781316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
782316643e7SJed Brown    is used internally within the nonlinear solvers.
783316643e7SJed Brown 
784316643e7SJed Brown    Level: developer
785316643e7SJed Brown 
786316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
787316643e7SJed Brown 
788316643e7SJed Brown .seealso:  TSSetIJacobian()
78963495f91SJed Brown @*/
790d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
791316643e7SJed Brown {
792316643e7SJed Brown   PetscErrorCode ierr;
79324989b8cSPeter Brune   TSIJacobian    ijacobian;
79424989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
79524989b8cSPeter Brune   DM             dm;
79624989b8cSPeter Brune   void           *ctx;
797316643e7SJed Brown 
798316643e7SJed Brown   PetscFunctionBegin;
7990700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8000910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8010910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
802316643e7SJed Brown   PetscValidPointer(A,6);
80394ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
804316643e7SJed Brown   PetscValidPointer(B,7);
80594ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
80624989b8cSPeter Brune 
80724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
80824989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
8090298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
81024989b8cSPeter Brune 
811ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
812316643e7SJed Brown 
81394ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
81424989b8cSPeter Brune   if (ijacobian) {
815316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
816d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
817316643e7SJed Brown     PetscStackPop;
818214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
81994ab13aaSBarry Smith     PetscValidHeaderSpecific(A,MAT_CLASSID,4);
82094ab13aaSBarry Smith     PetscValidHeaderSpecific(B,MAT_CLASSID,5);
8214a6899ffSJed Brown   }
822214bc6a2SJed Brown   if (imex) {
823b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
82494ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
82594ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
82694ab13aaSBarry Smith       if (A != B) {
82794ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
82894ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
829214bc6a2SJed Brown       }
830214bc6a2SJed Brown     }
831214bc6a2SJed Brown   } else {
832e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
833e1244c69SJed Brown     if (rhsjacobian) {
834bd373395SBarry Smith       if (ijacobian) {
835e1244c69SJed Brown         ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
836bd373395SBarry Smith       } else {
837bd373395SBarry Smith         ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr);
838e1244c69SJed Brown       }
839d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
840e1244c69SJed Brown     }
84194ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
842e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
843e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
84494ab13aaSBarry Smith       ierr = MatScale(A,-1);CHKERRQ(ierr);
84594ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
84694ab13aaSBarry Smith       if (A != B) {
84794ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
84894ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
849316643e7SJed Brown       }
850e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
851e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
852e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
85394ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
85494ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
85594ab13aaSBarry Smith         if (A != B) {
85694ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
85794ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
858e1244c69SJed Brown         }
859e1244c69SJed Brown       }
86094ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
86194ab13aaSBarry Smith       if (A != B) {
86294ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
863214bc6a2SJed Brown       }
864316643e7SJed Brown     }
865316643e7SJed Brown   }
86694ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
867316643e7SJed Brown   PetscFunctionReturn(0);
868316643e7SJed Brown }
869316643e7SJed Brown 
870316643e7SJed Brown #undef __FUNCT__
8714a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
872d763cef2SBarry Smith /*@C
873d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
874b5abc632SBarry Smith     where U_t = G(t,u).
875d763cef2SBarry Smith 
8763f9fe445SBarry Smith     Logically Collective on TS
877d763cef2SBarry Smith 
878d763cef2SBarry Smith     Input Parameters:
879d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
8800298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
881d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
882d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
8830298fd71SBarry Smith           function evaluation routine (may be NULL)
884d763cef2SBarry Smith 
885d763cef2SBarry Smith     Calling sequence of func:
88687828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
887d763cef2SBarry Smith 
888d763cef2SBarry Smith +   t - current timestep
889d763cef2SBarry Smith .   u - input vector
890d763cef2SBarry Smith .   F - function vector
891d763cef2SBarry Smith -   ctx - [optional] user-defined function context
892d763cef2SBarry Smith 
893d763cef2SBarry Smith     Level: beginner
894d763cef2SBarry Smith 
895d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
896d763cef2SBarry Smith 
897ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
898d763cef2SBarry Smith @*/
899089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
900d763cef2SBarry Smith {
901089b2837SJed Brown   PetscErrorCode ierr;
902089b2837SJed Brown   SNES           snes;
9030298fd71SBarry Smith   Vec            ralloc = NULL;
90424989b8cSPeter Brune   DM             dm;
905d763cef2SBarry Smith 
906089b2837SJed Brown   PetscFunctionBegin;
9070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
908ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
90924989b8cSPeter Brune 
91024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
91124989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
912089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
913e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
914e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
915e856ceecSJed Brown     r    = ralloc;
916e856ceecSJed Brown   }
917089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
918e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
919d763cef2SBarry Smith   PetscFunctionReturn(0);
920d763cef2SBarry Smith }
921d763cef2SBarry Smith 
9224a2ae208SSatish Balay #undef __FUNCT__
923ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
924ef20d060SBarry Smith /*@C
925abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
926ef20d060SBarry Smith 
927ef20d060SBarry Smith     Logically Collective on TS
928ef20d060SBarry Smith 
929ef20d060SBarry Smith     Input Parameters:
930ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
931ef20d060SBarry Smith .   f - routine for evaluating the solution
932ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
9330298fd71SBarry Smith           function evaluation routine (may be NULL)
934ef20d060SBarry Smith 
935ef20d060SBarry Smith     Calling sequence of func:
936ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
937ef20d060SBarry Smith 
938ef20d060SBarry Smith +   t - current timestep
939ef20d060SBarry Smith .   u - output vector
940ef20d060SBarry Smith -   ctx - [optional] user-defined function context
941ef20d060SBarry Smith 
942abd5a294SJed Brown     Notes:
943abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
944abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
945abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
946abd5a294SJed Brown 
9474f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
948abd5a294SJed Brown 
949ef20d060SBarry Smith     Level: beginner
950ef20d060SBarry Smith 
951ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
952ef20d060SBarry Smith 
9539b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
954ef20d060SBarry Smith @*/
955ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
956ef20d060SBarry Smith {
957ef20d060SBarry Smith   PetscErrorCode ierr;
958ef20d060SBarry Smith   DM             dm;
959ef20d060SBarry Smith 
960ef20d060SBarry Smith   PetscFunctionBegin;
961ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
962ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
963ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
964ef20d060SBarry Smith   PetscFunctionReturn(0);
965ef20d060SBarry Smith }
966ef20d060SBarry Smith 
967ef20d060SBarry Smith #undef __FUNCT__
9689b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
9699b7cd975SBarry Smith /*@C
9709b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
9719b7cd975SBarry Smith 
9729b7cd975SBarry Smith     Logically Collective on TS
9739b7cd975SBarry Smith 
9749b7cd975SBarry Smith     Input Parameters:
9759b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
9769b7cd975SBarry Smith .   f - routine for evaluating the forcing function
9779b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
9780298fd71SBarry Smith           function evaluation routine (may be NULL)
9799b7cd975SBarry Smith 
9809b7cd975SBarry Smith     Calling sequence of func:
9819b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
9829b7cd975SBarry Smith 
9839b7cd975SBarry Smith +   t - current timestep
9849b7cd975SBarry Smith .   u - output vector
9859b7cd975SBarry Smith -   ctx - [optional] user-defined function context
9869b7cd975SBarry Smith 
9879b7cd975SBarry Smith     Notes:
9889b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
9899b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
9909b7cd975SBarry Smith 
9919b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
9929b7cd975SBarry Smith 
9939b7cd975SBarry Smith     Level: beginner
9949b7cd975SBarry Smith 
9959b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
9969b7cd975SBarry Smith 
9979b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
9989b7cd975SBarry Smith @*/
9999b7cd975SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
10009b7cd975SBarry Smith {
10019b7cd975SBarry Smith   PetscErrorCode ierr;
10029b7cd975SBarry Smith   DM             dm;
10039b7cd975SBarry Smith 
10049b7cd975SBarry Smith   PetscFunctionBegin;
10059b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10069b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
10079b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
10089b7cd975SBarry Smith   PetscFunctionReturn(0);
10099b7cd975SBarry Smith }
10109b7cd975SBarry Smith 
10119b7cd975SBarry Smith #undef __FUNCT__
10124a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
1013d763cef2SBarry Smith /*@C
1014d763cef2SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of F,
1015b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1016d763cef2SBarry Smith 
10173f9fe445SBarry Smith    Logically Collective on TS
1018d763cef2SBarry Smith 
1019d763cef2SBarry Smith    Input Parameters:
1020d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1021e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1022e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1023d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1024d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
10250298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1026d763cef2SBarry Smith 
1027d763cef2SBarry Smith    Calling sequence of func:
1028ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1029d763cef2SBarry Smith 
1030d763cef2SBarry Smith +  t - current timestep
1031d763cef2SBarry Smith .  u - input vector
1032e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1033e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1034d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1035d763cef2SBarry Smith 
1036d763cef2SBarry Smith 
1037d763cef2SBarry Smith    Level: beginner
1038d763cef2SBarry Smith 
1039d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1040d763cef2SBarry Smith 
1041ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1042d763cef2SBarry Smith 
1043d763cef2SBarry Smith @*/
1044e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1045d763cef2SBarry Smith {
1046277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1047089b2837SJed Brown   SNES           snes;
104824989b8cSPeter Brune   DM             dm;
104924989b8cSPeter Brune   TSIJacobian    ijacobian;
1050277b19d0SLisandro Dalcin 
1051d763cef2SBarry Smith   PetscFunctionBegin;
10520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1053e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1054e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1055e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1056e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1057d763cef2SBarry Smith 
105824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
105924989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1060e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1061e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1062e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1063e1244c69SJed Brown   }
10640298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1065089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
10665f659677SPeter Brune   if (!ijacobian) {
1067e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
10680e4ef248SJed Brown   }
1069e5d3d808SBarry Smith   if (Amat) {
1070e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
10710e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1072bbd56ea5SKarl Rupp 
1073e5d3d808SBarry Smith     ts->Arhs = Amat;
10740e4ef248SJed Brown   }
1075e5d3d808SBarry Smith   if (Pmat) {
1076e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
10770e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1078bbd56ea5SKarl Rupp 
1079e5d3d808SBarry Smith     ts->Brhs = Pmat;
10800e4ef248SJed Brown   }
1081d763cef2SBarry Smith   PetscFunctionReturn(0);
1082d763cef2SBarry Smith }
1083d763cef2SBarry Smith 
1084316643e7SJed Brown 
1085316643e7SJed Brown #undef __FUNCT__
1086316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
1087316643e7SJed Brown /*@C
1088b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1089316643e7SJed Brown 
10903f9fe445SBarry Smith    Logically Collective on TS
1091316643e7SJed Brown 
1092316643e7SJed Brown    Input Parameters:
1093316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
10940298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1095316643e7SJed Brown .  f   - the function evaluation routine
10960298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1097316643e7SJed Brown 
1098316643e7SJed Brown    Calling sequence of f:
1099316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1100316643e7SJed Brown 
1101316643e7SJed Brown +  t   - time at step/stage being solved
1102316643e7SJed Brown .  u   - state vector
1103316643e7SJed Brown .  u_t - time derivative of state vector
1104316643e7SJed Brown .  F   - function vector
1105316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1106316643e7SJed Brown 
1107316643e7SJed Brown    Important:
1108d6cbdb99SBarry Smith    The user MUST call either this routine, TSSetRHSFunction().  This routine must be used when not solving an ODE, for example a DAE.
1109316643e7SJed Brown 
1110316643e7SJed Brown    Level: beginner
1111316643e7SJed Brown 
1112316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1113316643e7SJed Brown 
1114d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1115316643e7SJed Brown @*/
1116089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
1117316643e7SJed Brown {
1118089b2837SJed Brown   PetscErrorCode ierr;
1119089b2837SJed Brown   SNES           snes;
11200298fd71SBarry Smith   Vec            resalloc = NULL;
112124989b8cSPeter Brune   DM             dm;
1122316643e7SJed Brown 
1123316643e7SJed Brown   PetscFunctionBegin;
11240700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1125ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
112624989b8cSPeter Brune 
112724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
112824989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
112924989b8cSPeter Brune 
1130089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1131e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
1132e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
1133e856ceecSJed Brown     res  = resalloc;
1134e856ceecSJed Brown   }
1135089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
1136e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
1137089b2837SJed Brown   PetscFunctionReturn(0);
1138089b2837SJed Brown }
1139089b2837SJed Brown 
1140089b2837SJed Brown #undef __FUNCT__
1141089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1142089b2837SJed Brown /*@C
1143089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1144089b2837SJed Brown 
1145089b2837SJed Brown    Not Collective
1146089b2837SJed Brown 
1147089b2837SJed Brown    Input Parameter:
1148089b2837SJed Brown .  ts - the TS context
1149089b2837SJed Brown 
1150089b2837SJed Brown    Output Parameter:
11510298fd71SBarry Smith +  r - vector to hold residual (or NULL)
11520298fd71SBarry Smith .  func - the function to compute residual (or NULL)
11530298fd71SBarry Smith -  ctx - the function context (or NULL)
1154089b2837SJed Brown 
1155089b2837SJed Brown    Level: advanced
1156089b2837SJed Brown 
1157089b2837SJed Brown .keywords: TS, nonlinear, get, function
1158089b2837SJed Brown 
1159089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1160089b2837SJed Brown @*/
1161089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1162089b2837SJed Brown {
1163089b2837SJed Brown   PetscErrorCode ierr;
1164089b2837SJed Brown   SNES           snes;
116524989b8cSPeter Brune   DM             dm;
1166089b2837SJed Brown 
1167089b2837SJed Brown   PetscFunctionBegin;
1168089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1169089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11700298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
117124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
117224989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1173089b2837SJed Brown   PetscFunctionReturn(0);
1174089b2837SJed Brown }
1175089b2837SJed Brown 
1176089b2837SJed Brown #undef __FUNCT__
1177089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1178089b2837SJed Brown /*@C
1179089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1180089b2837SJed Brown 
1181089b2837SJed Brown    Not Collective
1182089b2837SJed Brown 
1183089b2837SJed Brown    Input Parameter:
1184089b2837SJed Brown .  ts - the TS context
1185089b2837SJed Brown 
1186089b2837SJed Brown    Output Parameter:
11870298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
11880298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
11890298fd71SBarry Smith -  ctx - the function context (or NULL)
1190089b2837SJed Brown 
1191089b2837SJed Brown    Level: advanced
1192089b2837SJed Brown 
1193089b2837SJed Brown .keywords: TS, nonlinear, get, function
1194089b2837SJed Brown 
1195089b2837SJed Brown .seealso: TSSetRhsfunction(), SNESGetFunction()
1196089b2837SJed Brown @*/
1197089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1198089b2837SJed Brown {
1199089b2837SJed Brown   PetscErrorCode ierr;
1200089b2837SJed Brown   SNES           snes;
120124989b8cSPeter Brune   DM             dm;
1202089b2837SJed Brown 
1203089b2837SJed Brown   PetscFunctionBegin;
1204089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1205089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12060298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
120724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
120824989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1209316643e7SJed Brown   PetscFunctionReturn(0);
1210316643e7SJed Brown }
1211316643e7SJed Brown 
1212316643e7SJed Brown #undef __FUNCT__
1213316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1214316643e7SJed Brown /*@C
1215a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1216ae8867d6SBarry Smith         provided with TSSetIFunction().
1217316643e7SJed Brown 
12183f9fe445SBarry Smith    Logically Collective on TS
1219316643e7SJed Brown 
1220316643e7SJed Brown    Input Parameters:
1221316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1222e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1223e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1224316643e7SJed Brown .  f   - the Jacobian evaluation routine
12250298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1226316643e7SJed Brown 
1227316643e7SJed Brown    Calling sequence of f:
1228ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1229316643e7SJed Brown 
1230316643e7SJed Brown +  t    - time at step/stage being solved
12311b4a444bSJed Brown .  U    - state vector
12321b4a444bSJed Brown .  U_t  - time derivative of state vector
1233316643e7SJed Brown .  a    - shift
1234e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1235e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1236316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1237316643e7SJed Brown 
1238316643e7SJed Brown    Notes:
1239e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1240316643e7SJed Brown 
1241a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1242b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1243a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1244a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1245a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1246a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1247a4f0a591SBarry Smith 
1248316643e7SJed Brown    Level: beginner
1249316643e7SJed Brown 
1250316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1251316643e7SJed Brown 
1252ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1253316643e7SJed Brown 
1254316643e7SJed Brown @*/
1255e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1256316643e7SJed Brown {
1257316643e7SJed Brown   PetscErrorCode ierr;
1258089b2837SJed Brown   SNES           snes;
125924989b8cSPeter Brune   DM             dm;
1260316643e7SJed Brown 
1261316643e7SJed Brown   PetscFunctionBegin;
12620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1263e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1264e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1265e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1266e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
126724989b8cSPeter Brune 
126824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
126924989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
127024989b8cSPeter Brune 
1271089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1272e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1273316643e7SJed Brown   PetscFunctionReturn(0);
1274316643e7SJed Brown }
1275316643e7SJed Brown 
12764a2ae208SSatish Balay #undef __FUNCT__
1277e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse"
1278e1244c69SJed Brown /*@
1279e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1280e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1281e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1282e1244c69SJed Brown    not been changed by the TS.
1283e1244c69SJed Brown 
1284e1244c69SJed Brown    Logically Collective
1285e1244c69SJed Brown 
1286e1244c69SJed Brown    Input Arguments:
1287e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1288e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1289e1244c69SJed Brown 
1290e1244c69SJed Brown    Level: intermediate
1291e1244c69SJed Brown 
1292e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1293e1244c69SJed Brown @*/
1294e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1295e1244c69SJed Brown {
1296e1244c69SJed Brown   PetscFunctionBegin;
1297e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1298e1244c69SJed Brown   PetscFunctionReturn(0);
1299e1244c69SJed Brown }
1300e1244c69SJed Brown 
1301e1244c69SJed Brown #undef __FUNCT__
130255849f57SBarry Smith #define __FUNCT__ "TSLoad"
130355849f57SBarry Smith /*@C
130455849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
130555849f57SBarry Smith 
130655849f57SBarry Smith   Collective on PetscViewer
130755849f57SBarry Smith 
130855849f57SBarry Smith   Input Parameters:
130955849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
131055849f57SBarry Smith            some related function before a call to TSLoad().
131155849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
131255849f57SBarry Smith 
131355849f57SBarry Smith    Level: intermediate
131455849f57SBarry Smith 
131555849f57SBarry Smith   Notes:
131655849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
131755849f57SBarry Smith 
131855849f57SBarry Smith   Notes for advanced users:
131955849f57SBarry Smith   Most users should not need to know the details of the binary storage
132055849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
132155849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
132255849f57SBarry Smith   format is
132355849f57SBarry Smith .vb
132455849f57SBarry Smith      has not yet been determined
132555849f57SBarry Smith .ve
132655849f57SBarry Smith 
132755849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
132855849f57SBarry Smith @*/
1329f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
133055849f57SBarry Smith {
133155849f57SBarry Smith   PetscErrorCode ierr;
133255849f57SBarry Smith   PetscBool      isbinary;
133355849f57SBarry Smith   PetscInt       classid;
133455849f57SBarry Smith   char           type[256];
13352d53ad75SBarry Smith   DMTS           sdm;
1336ad6bc421SBarry Smith   DM             dm;
133755849f57SBarry Smith 
133855849f57SBarry Smith   PetscFunctionBegin;
1339f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
134055849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
134155849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
134255849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
134355849f57SBarry Smith 
134455849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
1345ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
134655849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
1347f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1348f2c2a1b9SBarry Smith   if (ts->ops->load) {
1349f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1350f2c2a1b9SBarry Smith   }
1351ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1352ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1353ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1354f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1355f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
13562d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13572d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
135855849f57SBarry Smith   PetscFunctionReturn(0);
135955849f57SBarry Smith }
136055849f57SBarry Smith 
13619804daf3SBarry Smith #include <petscdraw.h>
1362e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1363e04113cfSBarry Smith #include <petscviewersaws.h>
1364f05ece33SBarry Smith #endif
136555849f57SBarry Smith #undef __FUNCT__
13664a2ae208SSatish Balay #define __FUNCT__ "TSView"
13677e2c5f70SBarry Smith /*@C
1368d763cef2SBarry Smith     TSView - Prints the TS data structure.
1369d763cef2SBarry Smith 
13704c49b128SBarry Smith     Collective on TS
1371d763cef2SBarry Smith 
1372d763cef2SBarry Smith     Input Parameters:
1373d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1374d763cef2SBarry Smith -   viewer - visualization context
1375d763cef2SBarry Smith 
1376d763cef2SBarry Smith     Options Database Key:
1377d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1378d763cef2SBarry Smith 
1379d763cef2SBarry Smith     Notes:
1380d763cef2SBarry Smith     The available visualization contexts include
1381b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1382b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1383d763cef2SBarry Smith          output where only the first processor opens
1384d763cef2SBarry Smith          the file.  All other processors send their
1385d763cef2SBarry Smith          data to the first processor to print.
1386d763cef2SBarry Smith 
1387d763cef2SBarry Smith     The user can open an alternative visualization context with
1388b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1389d763cef2SBarry Smith 
1390d763cef2SBarry Smith     Level: beginner
1391d763cef2SBarry Smith 
1392d763cef2SBarry Smith .keywords: TS, timestep, view
1393d763cef2SBarry Smith 
1394b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1395d763cef2SBarry Smith @*/
13967087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1397d763cef2SBarry Smith {
1398dfbe8321SBarry Smith   PetscErrorCode ierr;
139919fd82e9SBarry Smith   TSType         type;
14002b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
14012d53ad75SBarry Smith   DMTS           sdm;
1402e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1403f05ece33SBarry Smith   PetscBool      isams;
1404f05ece33SBarry Smith #endif
1405d763cef2SBarry Smith 
1406d763cef2SBarry Smith   PetscFunctionBegin;
14070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14083050cee2SBarry Smith   if (!viewer) {
1409ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
14103050cee2SBarry Smith   }
14110700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1412c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1413fd16b177SBarry Smith 
1414251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1415251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
141655849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
14172b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1418e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1419e04113cfSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&isams);CHKERRQ(ierr);
1420f05ece33SBarry Smith #endif
142132077d6dSBarry Smith   if (iascii) {
1422dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
142377431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
14247c8652ddSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1425d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
14265ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1427c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1428d763cef2SBarry Smith     }
14295ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1430193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
14312d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14322d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1433d52bd9f3SBarry Smith     if (ts->ops->view) {
1434d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1435d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1436d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1437d52bd9f3SBarry Smith     }
14380f5bd95cSBarry Smith   } else if (isstring) {
1439a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1440b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
144155849f57SBarry Smith   } else if (isbinary) {
144255849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
144355849f57SBarry Smith     MPI_Comm    comm;
144455849f57SBarry Smith     PetscMPIInt rank;
144555849f57SBarry Smith     char        type[256];
144655849f57SBarry Smith 
144755849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
144855849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
144955849f57SBarry Smith     if (!rank) {
145055849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
145155849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
145255849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
145355849f57SBarry Smith     }
145455849f57SBarry Smith     if (ts->ops->view) {
145555849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
145655849f57SBarry Smith     }
1457f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1458f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
14592d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14602d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
14612b0a91c0SBarry Smith   } else if (isdraw) {
14622b0a91c0SBarry Smith     PetscDraw draw;
14632b0a91c0SBarry Smith     char      str[36];
146489fd9fafSBarry Smith     PetscReal x,y,bottom,h;
14652b0a91c0SBarry Smith 
14662b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
14672b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
14682b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
14692b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
14700298fd71SBarry Smith     ierr   = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
147189fd9fafSBarry Smith     bottom = y - h;
14722b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
14732b0a91c0SBarry Smith     if (ts->ops->view) {
14742b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14752b0a91c0SBarry Smith     }
14762b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1477e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1478f05ece33SBarry Smith   } else if (isams) {
1479d45a07a7SBarry Smith     PetscMPIInt rank;
14802657e9d9SBarry Smith     const char  *name;
14812657e9d9SBarry Smith 
14822657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
1483d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1484d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
1485d45a07a7SBarry Smith       char       dir[1024];
1486d45a07a7SBarry Smith 
1487e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
1488a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
14892657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
14902657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
14912657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
1492d763cef2SBarry Smith     }
14930acecf5bSBarry Smith     if (ts->ops->view) {
14940acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14950acecf5bSBarry Smith     }
1496f05ece33SBarry Smith #endif
1497f05ece33SBarry Smith   }
1498f05ece33SBarry Smith 
1499b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1500251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1501b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1502d763cef2SBarry Smith   PetscFunctionReturn(0);
1503d763cef2SBarry Smith }
1504d763cef2SBarry Smith 
1505d763cef2SBarry Smith 
15064a2ae208SSatish Balay #undef __FUNCT__
15074a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1508b07ff414SBarry Smith /*@
1509d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1510d763cef2SBarry Smith    the timesteppers.
1511d763cef2SBarry Smith 
15123f9fe445SBarry Smith    Logically Collective on TS
1513d763cef2SBarry Smith 
1514d763cef2SBarry Smith    Input Parameters:
1515d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1516d763cef2SBarry Smith -  usrP - optional user context
1517d763cef2SBarry Smith 
1518d763cef2SBarry Smith    Level: intermediate
1519d763cef2SBarry Smith 
1520d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1521d763cef2SBarry Smith 
1522d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1523d763cef2SBarry Smith @*/
15247087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1525d763cef2SBarry Smith {
1526d763cef2SBarry Smith   PetscFunctionBegin;
15270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1528d763cef2SBarry Smith   ts->user = usrP;
1529d763cef2SBarry Smith   PetscFunctionReturn(0);
1530d763cef2SBarry Smith }
1531d763cef2SBarry Smith 
15324a2ae208SSatish Balay #undef __FUNCT__
15334a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1534b07ff414SBarry Smith /*@
1535d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1536d763cef2SBarry Smith     timestepper.
1537d763cef2SBarry Smith 
1538d763cef2SBarry Smith     Not Collective
1539d763cef2SBarry Smith 
1540d763cef2SBarry Smith     Input Parameter:
1541d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1542d763cef2SBarry Smith 
1543d763cef2SBarry Smith     Output Parameter:
1544d763cef2SBarry Smith .   usrP - user context
1545d763cef2SBarry Smith 
1546d763cef2SBarry Smith     Level: intermediate
1547d763cef2SBarry Smith 
1548d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1549d763cef2SBarry Smith 
1550d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1551d763cef2SBarry Smith @*/
1552e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1553d763cef2SBarry Smith {
1554d763cef2SBarry Smith   PetscFunctionBegin;
15550700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1556e71120c6SJed Brown   *(void**)usrP = ts->user;
1557d763cef2SBarry Smith   PetscFunctionReturn(0);
1558d763cef2SBarry Smith }
1559d763cef2SBarry Smith 
15604a2ae208SSatish Balay #undef __FUNCT__
15614a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1562d763cef2SBarry Smith /*@
1563b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1564d763cef2SBarry Smith 
1565d763cef2SBarry Smith    Not Collective
1566d763cef2SBarry Smith 
1567d763cef2SBarry Smith    Input Parameter:
1568d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1569d763cef2SBarry Smith 
1570d763cef2SBarry Smith    Output Parameter:
1571b8123daeSJed Brown .  iter - number of steps completed so far
1572d763cef2SBarry Smith 
1573d763cef2SBarry Smith    Level: intermediate
1574d763cef2SBarry Smith 
1575d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
15769be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
1577d763cef2SBarry Smith @*/
15787087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1579d763cef2SBarry Smith {
1580d763cef2SBarry Smith   PetscFunctionBegin;
15810700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15824482741eSBarry Smith   PetscValidIntPointer(iter,2);
1583d763cef2SBarry Smith   *iter = ts->steps;
1584d763cef2SBarry Smith   PetscFunctionReturn(0);
1585d763cef2SBarry Smith }
1586d763cef2SBarry Smith 
15874a2ae208SSatish Balay #undef __FUNCT__
15884a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1589d763cef2SBarry Smith /*@
1590d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1591d763cef2SBarry Smith    as well as the initial time.
1592d763cef2SBarry Smith 
15933f9fe445SBarry Smith    Logically Collective on TS
1594d763cef2SBarry Smith 
1595d763cef2SBarry Smith    Input Parameters:
1596d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1597d763cef2SBarry Smith .  initial_time - the initial time
1598d763cef2SBarry Smith -  time_step - the size of the timestep
1599d763cef2SBarry Smith 
1600d763cef2SBarry Smith    Level: intermediate
1601d763cef2SBarry Smith 
1602d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1603d763cef2SBarry Smith 
1604d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1605d763cef2SBarry Smith @*/
16067087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1607d763cef2SBarry Smith {
1608e144a568SJed Brown   PetscErrorCode ierr;
1609e144a568SJed Brown 
1610d763cef2SBarry Smith   PetscFunctionBegin;
16110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1612e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1613d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1614d763cef2SBarry Smith   PetscFunctionReturn(0);
1615d763cef2SBarry Smith }
1616d763cef2SBarry Smith 
16174a2ae208SSatish Balay #undef __FUNCT__
16184a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1619d763cef2SBarry Smith /*@
1620d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1621d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1622d763cef2SBarry Smith 
16233f9fe445SBarry Smith    Logically Collective on TS
1624d763cef2SBarry Smith 
1625d763cef2SBarry Smith    Input Parameters:
1626d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1627d763cef2SBarry Smith -  time_step - the size of the timestep
1628d763cef2SBarry Smith 
1629d763cef2SBarry Smith    Level: intermediate
1630d763cef2SBarry Smith 
1631d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1632d763cef2SBarry Smith 
1633d763cef2SBarry Smith .keywords: TS, set, timestep
1634d763cef2SBarry Smith @*/
16357087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1636d763cef2SBarry Smith {
1637d763cef2SBarry Smith   PetscFunctionBegin;
16380700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1639c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1640d763cef2SBarry Smith   ts->time_step      = time_step;
164131748224SBarry Smith   ts->time_step_orig = time_step;
1642d763cef2SBarry Smith   PetscFunctionReturn(0);
1643d763cef2SBarry Smith }
1644d763cef2SBarry Smith 
16454a2ae208SSatish Balay #undef __FUNCT__
1646a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1647a43b19c4SJed Brown /*@
164849354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
164949354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
165049354f04SShri Abhyankar      or just return at the final time TS computed.
1651a43b19c4SJed Brown 
1652a43b19c4SJed Brown   Logically Collective on TS
1653a43b19c4SJed Brown 
1654a43b19c4SJed Brown    Input Parameter:
1655a43b19c4SJed Brown +   ts - the time-step context
165649354f04SShri Abhyankar -   eftopt - exact final time option
1657a43b19c4SJed Brown 
1658a43b19c4SJed Brown    Level: beginner
1659a43b19c4SJed Brown 
1660a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1661a43b19c4SJed Brown @*/
166249354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1663a43b19c4SJed Brown {
1664a43b19c4SJed Brown   PetscFunctionBegin;
1665a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
166649354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
166749354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1668a43b19c4SJed Brown   PetscFunctionReturn(0);
1669a43b19c4SJed Brown }
1670a43b19c4SJed Brown 
1671a43b19c4SJed Brown #undef __FUNCT__
16724a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1673d763cef2SBarry Smith /*@
1674d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1675d763cef2SBarry Smith 
1676d763cef2SBarry Smith    Not Collective
1677d763cef2SBarry Smith 
1678d763cef2SBarry Smith    Input Parameter:
1679d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1680d763cef2SBarry Smith 
1681d763cef2SBarry Smith    Output Parameter:
1682d763cef2SBarry Smith .  dt - the current timestep size
1683d763cef2SBarry Smith 
1684d763cef2SBarry Smith    Level: intermediate
1685d763cef2SBarry Smith 
1686d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1687d763cef2SBarry Smith 
1688d763cef2SBarry Smith .keywords: TS, get, timestep
1689d763cef2SBarry Smith @*/
16907087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1691d763cef2SBarry Smith {
1692d763cef2SBarry Smith   PetscFunctionBegin;
16930700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1694f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1695d763cef2SBarry Smith   *dt = ts->time_step;
1696d763cef2SBarry Smith   PetscFunctionReturn(0);
1697d763cef2SBarry Smith }
1698d763cef2SBarry Smith 
16994a2ae208SSatish Balay #undef __FUNCT__
17004a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1701d8e5e3e6SSatish Balay /*@
1702d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1703d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1704d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1705d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1706d763cef2SBarry Smith 
1707d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1708d763cef2SBarry Smith 
1709d763cef2SBarry Smith    Input Parameter:
1710d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1711d763cef2SBarry Smith 
1712d763cef2SBarry Smith    Output Parameter:
1713d763cef2SBarry Smith .  v - the vector containing the solution
1714d763cef2SBarry Smith 
1715d763cef2SBarry Smith    Level: intermediate
1716d763cef2SBarry Smith 
1717d763cef2SBarry Smith .seealso: TSGetTimeStep()
1718d763cef2SBarry Smith 
1719d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1720d763cef2SBarry Smith @*/
17217087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1722d763cef2SBarry Smith {
1723d763cef2SBarry Smith   PetscFunctionBegin;
17240700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17254482741eSBarry Smith   PetscValidPointer(v,2);
17268737fe31SLisandro Dalcin   *v = ts->vec_sol;
1727d763cef2SBarry Smith   PetscFunctionReturn(0);
1728d763cef2SBarry Smith }
1729d763cef2SBarry Smith 
1730*c235aa8dSHong Zhang #undef __FUNCT__
1731*c235aa8dSHong Zhang #define __FUNCT__ "TSGetSensitivity"
1732*c235aa8dSHong Zhang /*@
1733*c235aa8dSHong Zhang    TSGetSensitivity - Returns the sensitivity in the reverse mode.
1734*c235aa8dSHong Zhang 
1735*c235aa8dSHong Zhang    Not Collective, but Vec returned is parallel if TS is parallel
1736*c235aa8dSHong Zhang 
1737*c235aa8dSHong Zhang    Input Parameter:
1738*c235aa8dSHong Zhang .  ts - the TS context obtained from TSCreate()
1739*c235aa8dSHong Zhang 
1740*c235aa8dSHong Zhang    Output Parameter:
1741*c235aa8dSHong Zhang .  v - the vector containing the solution
1742*c235aa8dSHong Zhang 
1743*c235aa8dSHong Zhang    Level: intermediate
1744*c235aa8dSHong Zhang 
1745*c235aa8dSHong Zhang .seealso: TSGetTimeStep()
1746*c235aa8dSHong Zhang 
1747*c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity
1748*c235aa8dSHong Zhang @*/
1749*c235aa8dSHong Zhang PetscErrorCode  TSGetSensitivity(TS ts,Vec *v)
1750*c235aa8dSHong Zhang {
1751*c235aa8dSHong Zhang   PetscFunctionBegin;
1752*c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1753*c235aa8dSHong Zhang   PetscValidPointer(v,2);
1754*c235aa8dSHong Zhang   *v = ts->vec_sensi;
1755*c235aa8dSHong Zhang   PetscFunctionReturn(0);
1756*c235aa8dSHong Zhang }
1757*c235aa8dSHong Zhang 
1758bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
17594a2ae208SSatish Balay #undef __FUNCT__
1760bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1761d8e5e3e6SSatish Balay /*@
1762bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1763d763cef2SBarry Smith 
1764bdad233fSMatthew Knepley   Not collective
1765d763cef2SBarry Smith 
1766bdad233fSMatthew Knepley   Input Parameters:
1767bdad233fSMatthew Knepley + ts   - The TS
1768bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1769d763cef2SBarry Smith .vb
17700910c330SBarry Smith          U_t - A U = 0      (linear)
17710910c330SBarry Smith          U_t - A(t) U = 0   (linear)
17720910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1773d763cef2SBarry Smith .ve
1774d763cef2SBarry Smith 
1775d763cef2SBarry Smith    Level: beginner
1776d763cef2SBarry Smith 
1777bdad233fSMatthew Knepley .keywords: TS, problem type
1778bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1779d763cef2SBarry Smith @*/
17807087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1781a7cc72afSBarry Smith {
17829e2a6581SJed Brown   PetscErrorCode ierr;
17839e2a6581SJed Brown 
1784d763cef2SBarry Smith   PetscFunctionBegin;
17850700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1786bdad233fSMatthew Knepley   ts->problem_type = type;
17879e2a6581SJed Brown   if (type == TS_LINEAR) {
17889e2a6581SJed Brown     SNES snes;
17899e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
17909e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
17919e2a6581SJed Brown   }
1792d763cef2SBarry Smith   PetscFunctionReturn(0);
1793d763cef2SBarry Smith }
1794d763cef2SBarry Smith 
1795bdad233fSMatthew Knepley #undef __FUNCT__
1796bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1797bdad233fSMatthew Knepley /*@C
1798bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1799bdad233fSMatthew Knepley 
1800bdad233fSMatthew Knepley   Not collective
1801bdad233fSMatthew Knepley 
1802bdad233fSMatthew Knepley   Input Parameter:
1803bdad233fSMatthew Knepley . ts   - The TS
1804bdad233fSMatthew Knepley 
1805bdad233fSMatthew Knepley   Output Parameter:
1806bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1807bdad233fSMatthew Knepley .vb
1808089b2837SJed Brown          M U_t = A U
1809089b2837SJed Brown          M(t) U_t = A(t) U
1810b5abc632SBarry Smith          F(t,U,U_t)
1811bdad233fSMatthew Knepley .ve
1812bdad233fSMatthew Knepley 
1813bdad233fSMatthew Knepley    Level: beginner
1814bdad233fSMatthew Knepley 
1815bdad233fSMatthew Knepley .keywords: TS, problem type
1816bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1817bdad233fSMatthew Knepley @*/
18187087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1819a7cc72afSBarry Smith {
1820bdad233fSMatthew Knepley   PetscFunctionBegin;
18210700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
18224482741eSBarry Smith   PetscValidIntPointer(type,2);
1823bdad233fSMatthew Knepley   *type = ts->problem_type;
1824bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1825bdad233fSMatthew Knepley }
1826d763cef2SBarry Smith 
18274a2ae208SSatish Balay #undef __FUNCT__
18284a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1829d763cef2SBarry Smith /*@
1830d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1831d763cef2SBarry Smith    of a timestepper.
1832d763cef2SBarry Smith 
1833d763cef2SBarry Smith    Collective on TS
1834d763cef2SBarry Smith 
1835d763cef2SBarry Smith    Input Parameter:
1836d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1837d763cef2SBarry Smith 
1838d763cef2SBarry Smith    Notes:
1839d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1840d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1841d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1842d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1843d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1844d763cef2SBarry Smith 
1845d763cef2SBarry Smith    Level: advanced
1846d763cef2SBarry Smith 
1847d763cef2SBarry Smith .keywords: TS, timestep, setup
1848d763cef2SBarry Smith 
1849d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1850d763cef2SBarry Smith @*/
18517087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1852d763cef2SBarry Smith {
1853dfbe8321SBarry Smith   PetscErrorCode ierr;
18546c6b9e74SPeter Brune   DM             dm;
18556c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
1856d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
18576c6b9e74SPeter Brune   TSIJacobian    ijac;
18586c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1859d763cef2SBarry Smith 
1860d763cef2SBarry Smith   PetscFunctionBegin;
18610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1862277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1863277b19d0SLisandro Dalcin 
18647adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
18659596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1866d763cef2SBarry Smith   }
1867277b19d0SLisandro Dalcin 
1868277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1869277b19d0SLisandro Dalcin 
1870*c235aa8dSHong Zhang   if (ts->reverse_mode && !ts->vec_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSensitivity() first");
1871*c235aa8dSHong Zhang 
1872552698daSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
18731c3436cfSJed Brown 
1874e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
1875e1244c69SJed Brown     Mat Amat,Pmat;
1876e1244c69SJed Brown     SNES snes;
1877e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1878e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
1879e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
1880e1244c69SJed Brown      * have displaced the RHS matrix */
1881e1244c69SJed Brown     if (Amat == ts->Arhs) {
1882e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
1883e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
1884e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
1885e1244c69SJed Brown     }
1886e1244c69SJed Brown     if (Pmat == ts->Brhs) {
1887e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
1888e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
1889e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
1890e1244c69SJed Brown     }
1891e1244c69SJed Brown   }
1892e1244c69SJed Brown 
1893277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1894000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1895277b19d0SLisandro Dalcin   }
1896277b19d0SLisandro Dalcin 
18976c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
18986c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
18996c6b9e74SPeter Brune    */
19006c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
19010298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
19026c6b9e74SPeter Brune   if (!func) {
19036c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
19046c6b9e74SPeter Brune   }
19056c6b9e74SPeter 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.
19066c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
19076c6b9e74SPeter Brune    */
19080298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
19090298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
19100298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
19116c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
19126c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
19136c6b9e74SPeter Brune   }
1914277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1915277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1916277b19d0SLisandro Dalcin }
1917277b19d0SLisandro Dalcin 
1918277b19d0SLisandro Dalcin #undef __FUNCT__
1919277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1920277b19d0SLisandro Dalcin /*@
1921277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1922277b19d0SLisandro Dalcin 
1923277b19d0SLisandro Dalcin    Collective on TS
1924277b19d0SLisandro Dalcin 
1925277b19d0SLisandro Dalcin    Input Parameter:
1926277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1927277b19d0SLisandro Dalcin 
1928277b19d0SLisandro Dalcin    Level: beginner
1929277b19d0SLisandro Dalcin 
1930277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1931277b19d0SLisandro Dalcin 
1932277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1933277b19d0SLisandro Dalcin @*/
1934277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1935277b19d0SLisandro Dalcin {
1936277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1937277b19d0SLisandro Dalcin 
1938277b19d0SLisandro Dalcin   PetscFunctionBegin;
1939277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1940277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1941277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1942277b19d0SLisandro Dalcin   }
1943277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
1944bbd56ea5SKarl Rupp 
19454e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
19464e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1947214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
19486bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1949e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1950e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
195138637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1952bbd56ea5SKarl Rupp 
1953*c235aa8dSHong Zhang   if (ts->reverse_mode) {ierr = VecDestroy(&ts->vec_sensi);CHKERRQ(ierr);}
1954277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1955d763cef2SBarry Smith   PetscFunctionReturn(0);
1956d763cef2SBarry Smith }
1957d763cef2SBarry Smith 
19584a2ae208SSatish Balay #undef __FUNCT__
19594a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1960d8e5e3e6SSatish Balay /*@
1961d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1962d763cef2SBarry Smith    with TSCreate().
1963d763cef2SBarry Smith 
1964d763cef2SBarry Smith    Collective on TS
1965d763cef2SBarry Smith 
1966d763cef2SBarry Smith    Input Parameter:
1967d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1968d763cef2SBarry Smith 
1969d763cef2SBarry Smith    Level: beginner
1970d763cef2SBarry Smith 
1971d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1972d763cef2SBarry Smith 
1973d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1974d763cef2SBarry Smith @*/
19756bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1976d763cef2SBarry Smith {
19776849ba73SBarry Smith   PetscErrorCode ierr;
1978d763cef2SBarry Smith 
1979d763cef2SBarry Smith   PetscFunctionBegin;
19806bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
19816bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
19826bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1983d763cef2SBarry Smith 
19846bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1985277b19d0SLisandro Dalcin 
1986e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
1987e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
19886bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
19896d4c513bSLisandro Dalcin 
199084df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
1991aeb4809dSShri Abhyankar   if ((*ts)->event) {
1992aeb4809dSShri Abhyankar     ierr = TSEventMonitorDestroy(&(*ts)->event);CHKERRQ(ierr);
1993aeb4809dSShri Abhyankar   }
19946bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
19956bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
19966bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
19976d4c513bSLisandro Dalcin 
1998a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
1999d763cef2SBarry Smith   PetscFunctionReturn(0);
2000d763cef2SBarry Smith }
2001d763cef2SBarry Smith 
20024a2ae208SSatish Balay #undef __FUNCT__
20034a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
2004d8e5e3e6SSatish Balay /*@
2005d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2006d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2007d763cef2SBarry Smith 
2008d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2009d763cef2SBarry Smith 
2010d763cef2SBarry Smith    Input Parameter:
2011d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2012d763cef2SBarry Smith 
2013d763cef2SBarry Smith    Output Parameter:
2014d763cef2SBarry Smith .  snes - the nonlinear solver context
2015d763cef2SBarry Smith 
2016d763cef2SBarry Smith    Notes:
2017d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2018d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
201994b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2020d763cef2SBarry Smith 
2021d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
20220298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2023d763cef2SBarry Smith 
2024d763cef2SBarry Smith    Level: beginner
2025d763cef2SBarry Smith 
2026d763cef2SBarry Smith .keywords: timestep, get, SNES
2027d763cef2SBarry Smith @*/
20287087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2029d763cef2SBarry Smith {
2030d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2031d372ba47SLisandro Dalcin 
2032d763cef2SBarry Smith   PetscFunctionBegin;
20330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20344482741eSBarry Smith   PetscValidPointer(snes,2);
2035d372ba47SLisandro Dalcin   if (!ts->snes) {
2036ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
20370298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
20383bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2039d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2040496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
20419e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
20429e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
20439e2a6581SJed Brown     }
2044d372ba47SLisandro Dalcin   }
2045d763cef2SBarry Smith   *snes = ts->snes;
2046d763cef2SBarry Smith   PetscFunctionReturn(0);
2047d763cef2SBarry Smith }
2048d763cef2SBarry Smith 
20494a2ae208SSatish Balay #undef __FUNCT__
2050deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
2051deb2cd25SJed Brown /*@
2052deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2053deb2cd25SJed Brown 
2054deb2cd25SJed Brown    Collective
2055deb2cd25SJed Brown 
2056deb2cd25SJed Brown    Input Parameter:
2057deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2058deb2cd25SJed Brown -  snes - the nonlinear solver context
2059deb2cd25SJed Brown 
2060deb2cd25SJed Brown    Notes:
2061deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2062deb2cd25SJed Brown 
2063deb2cd25SJed Brown    Level: developer
2064deb2cd25SJed Brown 
2065deb2cd25SJed Brown .keywords: timestep, set, SNES
2066deb2cd25SJed Brown @*/
2067deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2068deb2cd25SJed Brown {
2069deb2cd25SJed Brown   PetscErrorCode ierr;
2070d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2071deb2cd25SJed Brown 
2072deb2cd25SJed Brown   PetscFunctionBegin;
2073deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2074deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2075deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2076deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2077bbd56ea5SKarl Rupp 
2078deb2cd25SJed Brown   ts->snes = snes;
2079bbd56ea5SKarl Rupp 
20800298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
20810298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2082740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
20830298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2084740132f1SEmil Constantinescu   }
2085deb2cd25SJed Brown   PetscFunctionReturn(0);
2086deb2cd25SJed Brown }
2087deb2cd25SJed Brown 
2088deb2cd25SJed Brown #undef __FUNCT__
208994b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
2090d8e5e3e6SSatish Balay /*@
209194b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2092d763cef2SBarry Smith    a TS (timestepper) context.
2093d763cef2SBarry Smith 
209494b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2095d763cef2SBarry Smith 
2096d763cef2SBarry Smith    Input Parameter:
2097d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2098d763cef2SBarry Smith 
2099d763cef2SBarry Smith    Output Parameter:
210094b7f48cSBarry Smith .  ksp - the nonlinear solver context
2101d763cef2SBarry Smith 
2102d763cef2SBarry Smith    Notes:
210394b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2104d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2105d763cef2SBarry Smith    KSP and PC contexts as well.
2106d763cef2SBarry Smith 
210794b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
21080298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2109d763cef2SBarry Smith 
2110d763cef2SBarry Smith    Level: beginner
2111d763cef2SBarry Smith 
211294b7f48cSBarry Smith .keywords: timestep, get, KSP
2113d763cef2SBarry Smith @*/
21147087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2115d763cef2SBarry Smith {
2116d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2117089b2837SJed Brown   SNES           snes;
2118d372ba47SLisandro Dalcin 
2119d763cef2SBarry Smith   PetscFunctionBegin;
21200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21214482741eSBarry Smith   PetscValidPointer(ksp,2);
212217186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2123e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2124089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2125089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2126d763cef2SBarry Smith   PetscFunctionReturn(0);
2127d763cef2SBarry Smith }
2128d763cef2SBarry Smith 
2129d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2130d763cef2SBarry Smith 
21314a2ae208SSatish Balay #undef __FUNCT__
2132adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
2133adb62b0dSMatthew Knepley /*@
2134adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2135adb62b0dSMatthew Knepley    maximum time for iteration.
2136adb62b0dSMatthew Knepley 
21373f9fe445SBarry Smith    Not Collective
2138adb62b0dSMatthew Knepley 
2139adb62b0dSMatthew Knepley    Input Parameters:
2140adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
21410298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
21420298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2143adb62b0dSMatthew Knepley 
2144adb62b0dSMatthew Knepley    Level: intermediate
2145adb62b0dSMatthew Knepley 
2146adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2147adb62b0dSMatthew Knepley @*/
21487087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2149adb62b0dSMatthew Knepley {
2150adb62b0dSMatthew Knepley   PetscFunctionBegin;
21510700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2152abc0a331SBarry Smith   if (maxsteps) {
21534482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2154adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2155adb62b0dSMatthew Knepley   }
2156abc0a331SBarry Smith   if (maxtime) {
21574482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2158adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2159adb62b0dSMatthew Knepley   }
2160adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2161adb62b0dSMatthew Knepley }
2162adb62b0dSMatthew Knepley 
2163adb62b0dSMatthew Knepley #undef __FUNCT__
21644a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
2165d763cef2SBarry Smith /*@
2166d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2167d763cef2SBarry Smith    maximum time for iteration.
2168d763cef2SBarry Smith 
21693f9fe445SBarry Smith    Logically Collective on TS
2170d763cef2SBarry Smith 
2171d763cef2SBarry Smith    Input Parameters:
2172d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2173d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2174d763cef2SBarry Smith -  maxtime - final time to iterate to
2175d763cef2SBarry Smith 
2176d763cef2SBarry Smith    Options Database Keys:
2177d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
21783bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2179d763cef2SBarry Smith 
2180d763cef2SBarry Smith    Notes:
2181d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2182d763cef2SBarry Smith 
2183d763cef2SBarry Smith    Level: intermediate
2184d763cef2SBarry Smith 
2185d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2186a43b19c4SJed Brown 
2187a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2188d763cef2SBarry Smith @*/
21897087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2190d763cef2SBarry Smith {
2191d763cef2SBarry Smith   PetscFunctionBegin;
21920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2193c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2194c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
219539b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
219639b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2197d763cef2SBarry Smith   PetscFunctionReturn(0);
2198d763cef2SBarry Smith }
2199d763cef2SBarry Smith 
22004a2ae208SSatish Balay #undef __FUNCT__
22014a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
2202d763cef2SBarry Smith /*@
2203d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2204d763cef2SBarry Smith    for use by the TS routines.
2205d763cef2SBarry Smith 
22063f9fe445SBarry Smith    Logically Collective on TS and Vec
2207d763cef2SBarry Smith 
2208d763cef2SBarry Smith    Input Parameters:
2209d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
22100910c330SBarry Smith -  u - the solution vector
2211d763cef2SBarry Smith 
2212d763cef2SBarry Smith    Level: beginner
2213d763cef2SBarry Smith 
2214d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2215d763cef2SBarry Smith @*/
22160910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2217d763cef2SBarry Smith {
22188737fe31SLisandro Dalcin   PetscErrorCode ierr;
2219496e6a7aSJed Brown   DM             dm;
22208737fe31SLisandro Dalcin 
2221d763cef2SBarry Smith   PetscFunctionBegin;
22220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22230910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
22240910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
22256bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2226bbd56ea5SKarl Rupp 
22270910c330SBarry Smith   ts->vec_sol = u;
2228bbd56ea5SKarl Rupp 
2229496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
22300910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2231d763cef2SBarry Smith   PetscFunctionReturn(0);
2232d763cef2SBarry Smith }
2233d763cef2SBarry Smith 
2234e74ef692SMatthew Knepley #undef __FUNCT__
2235*c235aa8dSHong Zhang #define __FUNCT__ "TSSetSensitivity"
2236*c235aa8dSHong Zhang /*@
2237*c235aa8dSHong Zhang    TSSetSensitivity - Sets the initial solution vector
2238*c235aa8dSHong Zhang    for use by the TS routines.
2239*c235aa8dSHong Zhang 
2240*c235aa8dSHong Zhang    Logically Collective on TS and Vec
2241*c235aa8dSHong Zhang 
2242*c235aa8dSHong Zhang    Input Parameters:
2243*c235aa8dSHong Zhang +  ts - the TS context obtained from TSCreate()
2244*c235aa8dSHong Zhang -  u - the solution vector
2245*c235aa8dSHong Zhang 
2246*c235aa8dSHong Zhang    Level: beginner
2247*c235aa8dSHong Zhang 
2248*c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions
2249*c235aa8dSHong Zhang @*/
2250*c235aa8dSHong Zhang PetscErrorCode  TSSetSensitivity(TS ts,Vec u)
2251*c235aa8dSHong Zhang {
2252*c235aa8dSHong Zhang   PetscErrorCode ierr;
2253*c235aa8dSHong Zhang   DM             dm;
2254*c235aa8dSHong Zhang 
2255*c235aa8dSHong Zhang   PetscFunctionBegin;
2256*c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2257*c235aa8dSHong Zhang   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
2258*c235aa8dSHong Zhang   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
2259*c235aa8dSHong Zhang   ierr = VecDestroy(&ts->vec_sensi);CHKERRQ(ierr);
2260*c235aa8dSHong Zhang 
2261*c235aa8dSHong Zhang   ts->vec_sensi = u;
2262*c235aa8dSHong Zhang 
2263*c235aa8dSHong Zhang   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
2264*c235aa8dSHong Zhang   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2265*c235aa8dSHong Zhang   PetscFunctionReturn(0);
2266*c235aa8dSHong Zhang }
2267*c235aa8dSHong Zhang 
2268*c235aa8dSHong Zhang #undef __FUNCT__
2269e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2270ac226902SBarry Smith /*@C
2271000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
22723f2090d5SJed Brown   called once at the beginning of each time step.
2273000e7ae3SMatthew Knepley 
22743f9fe445SBarry Smith   Logically Collective on TS
2275000e7ae3SMatthew Knepley 
2276000e7ae3SMatthew Knepley   Input Parameters:
2277000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2278000e7ae3SMatthew Knepley - func - The function
2279000e7ae3SMatthew Knepley 
2280000e7ae3SMatthew Knepley   Calling sequence of func:
2281000e7ae3SMatthew Knepley . func (TS ts);
2282000e7ae3SMatthew Knepley 
2283000e7ae3SMatthew Knepley   Level: intermediate
2284000e7ae3SMatthew Knepley 
2285b8123daeSJed Brown   Note:
2286b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2287b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2288b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2289b8123daeSJed Brown 
2290000e7ae3SMatthew Knepley .keywords: TS, timestep
22919be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep()
2292000e7ae3SMatthew Knepley @*/
22937087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2294000e7ae3SMatthew Knepley {
2295000e7ae3SMatthew Knepley   PetscFunctionBegin;
22960700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2297ae60f76fSBarry Smith   ts->prestep = func;
2298000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2299000e7ae3SMatthew Knepley }
2300000e7ae3SMatthew Knepley 
2301e74ef692SMatthew Knepley #undef __FUNCT__
23023f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
230309ee8438SJed Brown /*@
23043f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
23053f2090d5SJed Brown 
23063f2090d5SJed Brown   Collective on TS
23073f2090d5SJed Brown 
23083f2090d5SJed Brown   Input Parameters:
23093f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
23103f2090d5SJed Brown 
23113f2090d5SJed Brown   Notes:
23123f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
23133f2090d5SJed Brown   so most users would not generally call this routine themselves.
23143f2090d5SJed Brown 
23153f2090d5SJed Brown   Level: developer
23163f2090d5SJed Brown 
23173f2090d5SJed Brown .keywords: TS, timestep
23189be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
23193f2090d5SJed Brown @*/
23207087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
23213f2090d5SJed Brown {
23223f2090d5SJed Brown   PetscErrorCode ierr;
23233f2090d5SJed Brown 
23243f2090d5SJed Brown   PetscFunctionBegin;
23250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2326ae60f76fSBarry Smith   if (ts->prestep) {
2327ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
2328312ce896SJed Brown   }
23293f2090d5SJed Brown   PetscFunctionReturn(0);
23303f2090d5SJed Brown }
23313f2090d5SJed Brown 
23323f2090d5SJed Brown #undef __FUNCT__
2333b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2334b8123daeSJed Brown /*@C
2335b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2336b8123daeSJed Brown   called once at the beginning of each stage.
2337b8123daeSJed Brown 
2338b8123daeSJed Brown   Logically Collective on TS
2339b8123daeSJed Brown 
2340b8123daeSJed Brown   Input Parameters:
2341b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2342b8123daeSJed Brown - func - The function
2343b8123daeSJed Brown 
2344b8123daeSJed Brown   Calling sequence of func:
2345b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2346b8123daeSJed Brown 
2347b8123daeSJed Brown   Level: intermediate
2348b8123daeSJed Brown 
2349b8123daeSJed Brown   Note:
2350b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2351b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2352b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2353b8123daeSJed Brown 
2354b8123daeSJed Brown .keywords: TS, timestep
23559be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2356b8123daeSJed Brown @*/
2357b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2358b8123daeSJed Brown {
2359b8123daeSJed Brown   PetscFunctionBegin;
2360b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2361ae60f76fSBarry Smith   ts->prestage = func;
2362b8123daeSJed Brown   PetscFunctionReturn(0);
2363b8123daeSJed Brown }
2364b8123daeSJed Brown 
2365b8123daeSJed Brown #undef __FUNCT__
23669be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage"
23679be3e283SDebojyoti Ghosh /*@C
23689be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
23699be3e283SDebojyoti Ghosh   called once at the end of each stage.
23709be3e283SDebojyoti Ghosh 
23719be3e283SDebojyoti Ghosh   Logically Collective on TS
23729be3e283SDebojyoti Ghosh 
23739be3e283SDebojyoti Ghosh   Input Parameters:
23749be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
23759be3e283SDebojyoti Ghosh - func - The function
23769be3e283SDebojyoti Ghosh 
23779be3e283SDebojyoti Ghosh   Calling sequence of func:
23789be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
23799be3e283SDebojyoti Ghosh 
23809be3e283SDebojyoti Ghosh   Level: intermediate
23819be3e283SDebojyoti Ghosh 
23829be3e283SDebojyoti Ghosh   Note:
23839be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
23849be3e283SDebojyoti Ghosh   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
23859be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
23869be3e283SDebojyoti Ghosh 
23879be3e283SDebojyoti Ghosh .keywords: TS, timestep
23889be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
23899be3e283SDebojyoti Ghosh @*/
23909be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
23919be3e283SDebojyoti Ghosh {
23929be3e283SDebojyoti Ghosh   PetscFunctionBegin;
23939be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
23949be3e283SDebojyoti Ghosh   ts->poststage = func;
23959be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
23969be3e283SDebojyoti Ghosh }
23979be3e283SDebojyoti Ghosh 
23989be3e283SDebojyoti Ghosh #undef __FUNCT__
2399b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2400b8123daeSJed Brown /*@
2401b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2402b8123daeSJed Brown 
2403b8123daeSJed Brown   Collective on TS
2404b8123daeSJed Brown 
2405b8123daeSJed Brown   Input Parameters:
2406b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
24079be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
2408b8123daeSJed Brown 
2409b8123daeSJed Brown   Notes:
2410b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2411b8123daeSJed Brown   most users would not generally call this routine themselves.
2412b8123daeSJed Brown 
2413b8123daeSJed Brown   Level: developer
2414b8123daeSJed Brown 
2415b8123daeSJed Brown .keywords: TS, timestep
24169be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
2417b8123daeSJed Brown @*/
2418b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2419b8123daeSJed Brown {
2420b8123daeSJed Brown   PetscErrorCode ierr;
2421b8123daeSJed Brown 
2422b8123daeSJed Brown   PetscFunctionBegin;
2423b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2424ae60f76fSBarry Smith   if (ts->prestage) {
2425ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
2426b8123daeSJed Brown   }
2427b8123daeSJed Brown   PetscFunctionReturn(0);
2428b8123daeSJed Brown }
2429b8123daeSJed Brown 
2430b8123daeSJed Brown #undef __FUNCT__
24319be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage"
24329be3e283SDebojyoti Ghosh /*@
24339be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
24349be3e283SDebojyoti Ghosh 
24359be3e283SDebojyoti Ghosh   Collective on TS
24369be3e283SDebojyoti Ghosh 
24379be3e283SDebojyoti Ghosh   Input Parameters:
24389be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
24399be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
24409be3e283SDebojyoti Ghosh   stageindex  - Stage number
24419be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
24429be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
24439be3e283SDebojyoti Ghosh 
24449be3e283SDebojyoti Ghosh   Notes:
24459be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
24469be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
24479be3e283SDebojyoti Ghosh 
24489be3e283SDebojyoti Ghosh   Level: developer
24499be3e283SDebojyoti Ghosh 
24509be3e283SDebojyoti Ghosh .keywords: TS, timestep
24519be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
24529be3e283SDebojyoti Ghosh @*/
24539be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
24549be3e283SDebojyoti Ghosh {
24559be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
24569be3e283SDebojyoti Ghosh 
24579be3e283SDebojyoti Ghosh   PetscFunctionBegin;
24589be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24594beae5d8SLisandro Dalcin   if (ts->poststage) {
24609be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
24619be3e283SDebojyoti Ghosh   }
24629be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
24639be3e283SDebojyoti Ghosh }
24649be3e283SDebojyoti Ghosh 
24659be3e283SDebojyoti Ghosh #undef __FUNCT__
2466e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2467ac226902SBarry Smith /*@C
2468000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
24693f2090d5SJed Brown   called once at the end of each time step.
2470000e7ae3SMatthew Knepley 
24713f9fe445SBarry Smith   Logically Collective on TS
2472000e7ae3SMatthew Knepley 
2473000e7ae3SMatthew Knepley   Input Parameters:
2474000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2475000e7ae3SMatthew Knepley - func - The function
2476000e7ae3SMatthew Knepley 
2477000e7ae3SMatthew Knepley   Calling sequence of func:
2478b8123daeSJed Brown $ func (TS ts);
2479000e7ae3SMatthew Knepley 
2480000e7ae3SMatthew Knepley   Level: intermediate
2481000e7ae3SMatthew Knepley 
2482000e7ae3SMatthew Knepley .keywords: TS, timestep
2483b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2484000e7ae3SMatthew Knepley @*/
24857087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2486000e7ae3SMatthew Knepley {
2487000e7ae3SMatthew Knepley   PetscFunctionBegin;
24880700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2489ae60f76fSBarry Smith   ts->poststep = func;
2490000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2491000e7ae3SMatthew Knepley }
2492000e7ae3SMatthew Knepley 
2493e74ef692SMatthew Knepley #undef __FUNCT__
24943f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
249509ee8438SJed Brown /*@
24963f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
24973f2090d5SJed Brown 
24983f2090d5SJed Brown   Collective on TS
24993f2090d5SJed Brown 
25003f2090d5SJed Brown   Input Parameters:
25013f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
25023f2090d5SJed Brown 
25033f2090d5SJed Brown   Notes:
25043f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
25053f2090d5SJed Brown   so most users would not generally call this routine themselves.
25063f2090d5SJed Brown 
25073f2090d5SJed Brown   Level: developer
25083f2090d5SJed Brown 
25093f2090d5SJed Brown .keywords: TS, timestep
25103f2090d5SJed Brown @*/
25117087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
25123f2090d5SJed Brown {
25133f2090d5SJed Brown   PetscErrorCode ierr;
25143f2090d5SJed Brown 
25153f2090d5SJed Brown   PetscFunctionBegin;
25160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2517ae60f76fSBarry Smith   if (ts->poststep) {
2518ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
251972ac3e02SJed Brown   }
25203f2090d5SJed Brown   PetscFunctionReturn(0);
25213f2090d5SJed Brown }
25223f2090d5SJed Brown 
2523d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2524d763cef2SBarry Smith 
25254a2ae208SSatish Balay #undef __FUNCT__
2526a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2527d763cef2SBarry Smith /*@C
2528a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2529d763cef2SBarry Smith    timestep to display the iteration's  progress.
2530d763cef2SBarry Smith 
25313f9fe445SBarry Smith    Logically Collective on TS
2532d763cef2SBarry Smith 
2533d763cef2SBarry Smith    Input Parameters:
2534d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2535e213d8f1SJed Brown .  monitor - monitoring routine
2536329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
25370298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
2538b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
25390298fd71SBarry Smith           (may be NULL)
2540d763cef2SBarry Smith 
2541e213d8f1SJed Brown    Calling sequence of monitor:
25420910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2543d763cef2SBarry Smith 
2544d763cef2SBarry Smith +    ts - the TS context
254588c05cc5SBarry 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
254688c05cc5SBarry Smith                                been interpolated to)
25471f06c33eSBarry Smith .    time - current time
25480910c330SBarry Smith .    u - current iterate
2549d763cef2SBarry Smith -    mctx - [optional] monitoring context
2550d763cef2SBarry Smith 
2551d763cef2SBarry Smith    Notes:
2552d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2553d763cef2SBarry Smith    already has been loaded.
2554d763cef2SBarry Smith 
2555025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2556025f1a04SBarry Smith 
2557d763cef2SBarry Smith    Level: intermediate
2558d763cef2SBarry Smith 
2559d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2560d763cef2SBarry Smith 
2561a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2562d763cef2SBarry Smith @*/
2563c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2564d763cef2SBarry Smith {
2565d763cef2SBarry Smith   PetscFunctionBegin;
25660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
256717186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2568d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
25698704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2570d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2571d763cef2SBarry Smith   PetscFunctionReturn(0);
2572d763cef2SBarry Smith }
2573d763cef2SBarry Smith 
25744a2ae208SSatish Balay #undef __FUNCT__
2575a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2576d763cef2SBarry Smith /*@C
2577a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2578d763cef2SBarry Smith 
25793f9fe445SBarry Smith    Logically Collective on TS
2580d763cef2SBarry Smith 
2581d763cef2SBarry Smith    Input Parameters:
2582d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2583d763cef2SBarry Smith 
2584d763cef2SBarry Smith    Notes:
2585d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2586d763cef2SBarry Smith 
2587d763cef2SBarry Smith    Level: intermediate
2588d763cef2SBarry Smith 
2589d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2590d763cef2SBarry Smith 
2591a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2592d763cef2SBarry Smith @*/
25937087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2594d763cef2SBarry Smith {
2595d952e501SBarry Smith   PetscErrorCode ierr;
2596d952e501SBarry Smith   PetscInt       i;
2597d952e501SBarry Smith 
2598d763cef2SBarry Smith   PetscFunctionBegin;
25990700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2600d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
26018704b422SBarry Smith     if (ts->monitordestroy[i]) {
26028704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2603d952e501SBarry Smith     }
2604d952e501SBarry Smith   }
2605d763cef2SBarry Smith   ts->numbermonitors = 0;
2606d763cef2SBarry Smith   PetscFunctionReturn(0);
2607d763cef2SBarry Smith }
2608d763cef2SBarry Smith 
26094a2ae208SSatish Balay #undef __FUNCT__
2610a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2611d8e5e3e6SSatish Balay /*@
2612a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
26135516499fSSatish Balay 
26145516499fSSatish Balay    Level: intermediate
261541251cbbSSatish Balay 
26165516499fSSatish Balay .keywords: TS, set, monitor
26175516499fSSatish Balay 
261841251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
261941251cbbSSatish Balay @*/
2620649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2621d763cef2SBarry Smith {
2622dfbe8321SBarry Smith   PetscErrorCode ierr;
2623ce94432eSBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts));
2624d132466eSBarry Smith 
2625d763cef2SBarry Smith   PetscFunctionBegin;
2626649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2627971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2628649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2629d763cef2SBarry Smith   PetscFunctionReturn(0);
2630d763cef2SBarry Smith }
2631d763cef2SBarry Smith 
26324a2ae208SSatish Balay #undef __FUNCT__
2633cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2634cd652676SJed Brown /*@
2635cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2636cd652676SJed Brown 
2637cd652676SJed Brown    Logically Collective on TS
2638cd652676SJed Brown 
2639cd652676SJed Brown    Input Argument:
2640cd652676SJed Brown .  ts - time stepping context
2641cd652676SJed Brown 
2642cd652676SJed Brown    Output Argument:
2643cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2644cd652676SJed Brown 
2645cd652676SJed Brown    Level: intermediate
2646cd652676SJed Brown 
2647cd652676SJed Brown .keywords: TS, set
2648cd652676SJed Brown 
2649cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2650cd652676SJed Brown @*/
2651cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2652cd652676SJed Brown {
2653cd652676SJed Brown   PetscFunctionBegin;
2654cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2655cd652676SJed Brown   ts->retain_stages = flg;
2656cd652676SJed Brown   PetscFunctionReturn(0);
2657cd652676SJed Brown }
2658cd652676SJed Brown 
2659cd652676SJed Brown #undef __FUNCT__
2660cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2661cd652676SJed Brown /*@
2662cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2663cd652676SJed Brown 
2664cd652676SJed Brown    Collective on TS
2665cd652676SJed Brown 
2666cd652676SJed Brown    Input Argument:
2667cd652676SJed Brown +  ts - time stepping context
2668cd652676SJed Brown -  t - time to interpolate to
2669cd652676SJed Brown 
2670cd652676SJed Brown    Output Argument:
26710910c330SBarry Smith .  U - state at given time
2672cd652676SJed Brown 
2673cd652676SJed Brown    Notes:
2674cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2675cd652676SJed Brown 
2676cd652676SJed Brown    Level: intermediate
2677cd652676SJed Brown 
2678cd652676SJed Brown    Developer Notes:
2679cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2680cd652676SJed Brown 
2681cd652676SJed Brown .keywords: TS, set
2682cd652676SJed Brown 
2683cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2684cd652676SJed Brown @*/
26850910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
2686cd652676SJed Brown {
2687cd652676SJed Brown   PetscErrorCode ierr;
2688cd652676SJed Brown 
2689cd652676SJed Brown   PetscFunctionBegin;
2690cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2691b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
26926712e2f1SBarry 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);
2693ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
26940910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
2695cd652676SJed Brown   PetscFunctionReturn(0);
2696cd652676SJed Brown }
2697cd652676SJed Brown 
2698cd652676SJed Brown #undef __FUNCT__
26994a2ae208SSatish Balay #define __FUNCT__ "TSStep"
2700d763cef2SBarry Smith /*@
27016d9e5789SSean Farley    TSStep - Steps one time step
2702d763cef2SBarry Smith 
2703d763cef2SBarry Smith    Collective on TS
2704d763cef2SBarry Smith 
2705d763cef2SBarry Smith    Input Parameter:
2706d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2707d763cef2SBarry Smith 
27086d9e5789SSean Farley    Level: intermediate
2709d763cef2SBarry Smith 
2710b8123daeSJed Brown    Notes:
2711b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
2712b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
2713b8123daeSJed Brown 
271425cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
271525cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
271625cb2221SBarry Smith 
2717d763cef2SBarry Smith .keywords: TS, timestep, solve
2718d763cef2SBarry Smith 
27199be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
2720d763cef2SBarry Smith @*/
2721193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
2722d763cef2SBarry Smith {
27232fb8446cSMatthew G. Knepley   DM               dm;
2724dfbe8321SBarry Smith   PetscErrorCode   ierr;
2725fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
2726d763cef2SBarry Smith 
2727d763cef2SBarry Smith   PetscFunctionBegin;
27280700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2729fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
2730fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
2731fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
2732fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
2733fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
2734fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
2735fffbeea8SBarry Smith                                 "  year        = {2014}\n}\n",&cite);
2736fffbeea8SBarry Smith 
27372fb8446cSMatthew G. Knepley   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
2738d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2739d405a339SMatthew Knepley 
2740362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
274124655328SShri   ts->ptime_prev = ts->ptime;
2742cdb7a50dSMatthew G. Knepley   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
27434e9ba7eeSMatthew G. Knepley   ierr = VecViewFromOptions(ts->vec_sol, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr);
2744bbd56ea5SKarl Rupp 
2745e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
2746d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2747d2daff3dSHong Zhang   if(ts->reverse_mode) {
2748d2daff3dSHong Zhang     if(!ts->ops->stepadj) {
2749d2daff3dSHong Zhang       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);
2750d2daff3dSHong Zhang     }else {
2751d2daff3dSHong Zhang       ierr = (*ts->ops->stepadj)(ts);CHKERRQ(ierr);
2752d2daff3dSHong Zhang     }
2753d2daff3dSHong Zhang   }else {
2754193ac0bcSJed Brown     ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
2755d2daff3dSHong Zhang   }
2756d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
2757bbd56ea5SKarl Rupp 
275824655328SShri   ts->time_step_prev = ts->ptime - ts->ptime_prev;
2759cdb7a50dSMatthew G. Knepley   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
2760362cd11cSLisandro Dalcin 
2761362cd11cSLisandro Dalcin   if (ts->reason < 0) {
2762cef5090cSJed Brown     if (ts->errorifstepfailed) {
2763cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
2764ce94432eSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]);
27652a3a10ceSLisandro Dalcin       } else if (ts->reason == TS_DIVERGED_STEP_REJECTED) {
27662a3a10ceSLisandro 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]);
2767ce94432eSBarry Smith       } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
2768cef5090cSJed Brown     }
2769362cd11cSLisandro Dalcin   } else if (!ts->reason) {
2770db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2771db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2772362cd11cSLisandro Dalcin   }
2773d763cef2SBarry Smith   PetscFunctionReturn(0);
2774d763cef2SBarry Smith }
2775d763cef2SBarry Smith 
27764a2ae208SSatish Balay #undef __FUNCT__
277705175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
277805175c85SJed Brown /*@
277905175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
278005175c85SJed Brown 
27811c3436cfSJed Brown    Collective on TS
278205175c85SJed Brown 
278305175c85SJed Brown    Input Arguments:
27841c3436cfSJed Brown +  ts - time stepping context
27851c3436cfSJed Brown .  order - desired order of accuracy
27860298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
278705175c85SJed Brown 
278805175c85SJed Brown    Output Arguments:
27890910c330SBarry Smith .  U - state at the end of the current step
279005175c85SJed Brown 
279105175c85SJed Brown    Level: advanced
279205175c85SJed Brown 
2793108c343cSJed Brown    Notes:
2794108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
2795108c343cSJed 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.
2796108c343cSJed Brown 
27971c3436cfSJed Brown .seealso: TSStep(), TSAdapt
279805175c85SJed Brown @*/
27990910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
280005175c85SJed Brown {
280105175c85SJed Brown   PetscErrorCode ierr;
280205175c85SJed Brown 
280305175c85SJed Brown   PetscFunctionBegin;
280405175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
280505175c85SJed Brown   PetscValidType(ts,1);
28060910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
2807ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
28080910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
280905175c85SJed Brown   PetscFunctionReturn(0);
281005175c85SJed Brown }
281105175c85SJed Brown 
281205175c85SJed Brown #undef __FUNCT__
28136a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
28146a4d4014SLisandro Dalcin /*@
28156a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
28166a4d4014SLisandro Dalcin 
28176a4d4014SLisandro Dalcin    Collective on TS
28186a4d4014SLisandro Dalcin 
28196a4d4014SLisandro Dalcin    Input Parameter:
28206a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2821cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
28225a3a76d0SJed Brown 
28236a4d4014SLisandro Dalcin    Level: beginner
28246a4d4014SLisandro Dalcin 
28255a3a76d0SJed Brown    Notes:
28265a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
28275a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
28285a3a76d0SJed Brown    stepped over the final time.
28295a3a76d0SJed Brown 
28306a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
28316a4d4014SLisandro Dalcin 
28326a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
28336a4d4014SLisandro Dalcin @*/
2834cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
28356a4d4014SLisandro Dalcin {
2836b06615a5SLisandro Dalcin   Vec               solution;
28376a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
2838f22f69f0SBarry Smith 
28396a4d4014SLisandro Dalcin   PetscFunctionBegin;
28400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2841f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
2842d2daff3dSHong Zhang   if (!ts->reverse_mode && 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 */
2843b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
28440910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
2845b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
2846b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
2847b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
28485a3a76d0SJed Brown     }
28490910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
2850bbd56ea5SKarl Rupp   } else if (u) {
28510910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
28525a3a76d0SJed Brown   }
2853d2daff3dSHong Zhang   ierr = TSSetUp(ts);CHKERRQ(ierr); /*compute adj coefficients if the reverse mode is on*/
28546a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
2855193ac0bcSJed Brown   ts->steps             = 0;
28565ef26d82SJed Brown   ts->ksp_its           = 0;
28575ef26d82SJed Brown   ts->snes_its          = 0;
2858c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
2859c610991cSLisandro Dalcin   ts->reject            = 0;
2860193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
2861193ac0bcSJed Brown 
2862ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
2863f05ece33SBarry Smith 
2864193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
2865193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
28660910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
2867cc708dedSBarry Smith     ts->solvetime = ts->ptime;
2868193ac0bcSJed Brown   } else {
28696a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
2870db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
2871d2daff3dSHong Zhang     else if (!ts->reverse_mode && ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
2872e1a7a14fSJed Brown     while (!ts->reason) {
2873d2daff3dSHong Zhang       if(!ts->reverse_mode) {
2874b06615a5SLisandro Dalcin         ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2875d2daff3dSHong Zhang       }else {
2876d2daff3dSHong Zhang         ierr = TSMonitor(ts,ts->max_steps-ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
2877d2daff3dSHong Zhang       }
2878193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
2879aeb4809dSShri Abhyankar       if (ts->event) {
2880aeb4809dSShri Abhyankar 	ierr = TSEventMonitor(ts);CHKERRQ(ierr);
28811eda64f1SShri Abhyankar 	if (ts->event->status != TSEVENT_PROCESSING) {
28821eda64f1SShri Abhyankar 	  ierr = TSPostStep(ts);CHKERRQ(ierr);
28831eda64f1SShri Abhyankar 	}
28841eda64f1SShri Abhyankar       } else {
28851eda64f1SShri Abhyankar 	ierr = TSPostStep(ts);CHKERRQ(ierr);
2886aeb4809dSShri Abhyankar       }
2887193ac0bcSJed Brown     }
2888d2daff3dSHong Zhang     if (!ts->reverse_mode && ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
28890910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
2890cc708dedSBarry Smith       ts->solvetime = ts->max_time;
2891b06615a5SLisandro Dalcin       solution = u;
28920574a7fbSJed Brown     } else {
2893ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
2894cc708dedSBarry Smith       ts->solvetime = ts->ptime;
2895b06615a5SLisandro Dalcin       solution = ts->vec_sol;
28960574a7fbSJed Brown     }
2897d2daff3dSHong Zhang     if(!ts->reverse_mode) {
2898b06615a5SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr);
2899d2daff3dSHong Zhang     }
29004e9ba7eeSMatthew G. Knepley     ierr = VecViewFromOptions(u, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr);
2901193ac0bcSJed Brown   }
2902d2daff3dSHong Zhang 
2903ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
290456f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
29056a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
29066a4d4014SLisandro Dalcin }
29076a4d4014SLisandro Dalcin 
29086a4d4014SLisandro Dalcin #undef __FUNCT__
29094a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
2910228d79bcSJed Brown /*@
2911228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
2912228d79bcSJed Brown 
2913228d79bcSJed Brown    Collective on TS
2914228d79bcSJed Brown 
2915228d79bcSJed Brown    Input Parameters:
2916228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
2917228d79bcSJed Brown .  step - step number that has just completed
2918228d79bcSJed Brown .  ptime - model time of the state
29190910c330SBarry Smith -  u - state at the current model time
2920228d79bcSJed Brown 
2921228d79bcSJed Brown    Notes:
2922228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
2923228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
2924228d79bcSJed Brown 
2925228d79bcSJed Brown    Level: advanced
2926228d79bcSJed Brown 
2927228d79bcSJed Brown .keywords: TS, timestep
2928228d79bcSJed Brown @*/
29290910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
2930d763cef2SBarry Smith {
29316849ba73SBarry Smith   PetscErrorCode ierr;
2932a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
2933d763cef2SBarry Smith 
2934d763cef2SBarry Smith   PetscFunctionBegin;
2935b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2936b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
29375edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
2938d763cef2SBarry Smith   for (i=0; i<n; i++) {
29390910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
2940d763cef2SBarry Smith   }
29415edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
2942d763cef2SBarry Smith   PetscFunctionReturn(0);
2943d763cef2SBarry Smith }
2944d763cef2SBarry Smith 
2945d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
29464a2ae208SSatish Balay #undef __FUNCT__
2947a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
2948d763cef2SBarry Smith /*@C
2949a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
2950a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
2951d763cef2SBarry Smith 
2952d763cef2SBarry Smith    Collective on TS
2953d763cef2SBarry Smith 
2954d763cef2SBarry Smith    Input Parameters:
2955d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
2956d763cef2SBarry Smith .  label - the title to put in the title bar
29577c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
2958a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
2959a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
2960d763cef2SBarry Smith 
2961d763cef2SBarry Smith    Output Parameter:
29620b039ecaSBarry Smith .  ctx - the context
2963d763cef2SBarry Smith 
2964d763cef2SBarry Smith    Options Database Key:
29654f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
29664f09c107SBarry Smith .  -ts_monitor_lg_solution -
296799fdda47SBarry Smith .  -ts_monitor_lg_error -
296899fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
296999fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
297099fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
2971d763cef2SBarry Smith 
2972d763cef2SBarry Smith    Notes:
2973a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
2974d763cef2SBarry Smith 
2975d763cef2SBarry Smith    Level: intermediate
2976d763cef2SBarry Smith 
29777c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
2978d763cef2SBarry Smith 
29794f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
29807c922b88SBarry Smith 
2981d763cef2SBarry Smith @*/
2982a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
2983d763cef2SBarry Smith {
2984b0a32e0cSBarry Smith   PetscDraw      win;
2985dfbe8321SBarry Smith   PetscErrorCode ierr;
2986d763cef2SBarry Smith 
2987d763cef2SBarry Smith   PetscFunctionBegin;
2988b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
2989a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
29903fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
29910b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
29923bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)(*ctx)->lg,(PetscObject)win);CHKERRQ(ierr);
2993287de1a7SBarry Smith   ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr);
2994287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
2995a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
2996d763cef2SBarry Smith   PetscFunctionReturn(0);
2997d763cef2SBarry Smith }
2998d763cef2SBarry Smith 
29994a2ae208SSatish Balay #undef __FUNCT__
30004f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
3001b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
3002d763cef2SBarry Smith {
30030b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
3004c32365f1SBarry Smith   PetscReal      x   = ptime,y;
3005dfbe8321SBarry Smith   PetscErrorCode ierr;
3006d763cef2SBarry Smith 
3007d763cef2SBarry Smith   PetscFunctionBegin;
3008b06615a5SLisandro Dalcin   if (!step) {
3009a9f9c1f6SBarry Smith     PetscDrawAxis axis;
3010a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
3011a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
3012a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
3013287de1a7SBarry Smith     ierr = PetscDrawLGIndicateDataPoints(ctx->lg,PETSC_TRUE);CHKERRQ(ierr);
3014a9f9c1f6SBarry Smith   }
3015c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
30160b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
3017b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
30180b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
30193923b477SBarry Smith   }
3020d763cef2SBarry Smith   PetscFunctionReturn(0);
3021d763cef2SBarry Smith }
3022d763cef2SBarry Smith 
30234a2ae208SSatish Balay #undef __FUNCT__
3024a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
3025d763cef2SBarry Smith /*@C
3026a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
3027a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
3028d763cef2SBarry Smith 
30290b039ecaSBarry Smith    Collective on TSMonitorLGCtx
3030d763cef2SBarry Smith 
3031d763cef2SBarry Smith    Input Parameter:
30320b039ecaSBarry Smith .  ctx - the monitor context
3033d763cef2SBarry Smith 
3034d763cef2SBarry Smith    Level: intermediate
3035d763cef2SBarry Smith 
3036d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
3037d763cef2SBarry Smith 
30384f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
3039d763cef2SBarry Smith @*/
3040a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
3041d763cef2SBarry Smith {
3042b0a32e0cSBarry Smith   PetscDraw      draw;
3043dfbe8321SBarry Smith   PetscErrorCode ierr;
3044d763cef2SBarry Smith 
3045d763cef2SBarry Smith   PetscFunctionBegin;
30460b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
30476bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
30480b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
30490b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
3050d763cef2SBarry Smith   PetscFunctionReturn(0);
3051d763cef2SBarry Smith }
3052d763cef2SBarry Smith 
30534a2ae208SSatish Balay #undef __FUNCT__
30544a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
3055d763cef2SBarry Smith /*@
3056b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
3057d763cef2SBarry Smith 
3058d763cef2SBarry Smith    Not Collective
3059d763cef2SBarry Smith 
3060d763cef2SBarry Smith    Input Parameter:
3061d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3062d763cef2SBarry Smith 
3063d763cef2SBarry Smith    Output Parameter:
3064d763cef2SBarry Smith .  t  - the current time
3065d763cef2SBarry Smith 
3066d763cef2SBarry Smith    Level: beginner
3067d763cef2SBarry Smith 
3068b8123daeSJed Brown    Note:
3069b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
30709be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
3071b8123daeSJed Brown 
3072d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3073d763cef2SBarry Smith 
3074d763cef2SBarry Smith .keywords: TS, get, time
3075d763cef2SBarry Smith @*/
30767087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
3077d763cef2SBarry Smith {
3078d763cef2SBarry Smith   PetscFunctionBegin;
30790700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3080f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
3081d763cef2SBarry Smith   *t = ts->ptime;
3082d763cef2SBarry Smith   PetscFunctionReturn(0);
3083d763cef2SBarry Smith }
3084d763cef2SBarry Smith 
30854a2ae208SSatish Balay #undef __FUNCT__
3086e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime"
3087e5e524a1SHong Zhang /*@
3088e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
3089e5e524a1SHong Zhang 
3090e5e524a1SHong Zhang    Not Collective
3091e5e524a1SHong Zhang 
3092e5e524a1SHong Zhang    Input Parameter:
3093e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
3094e5e524a1SHong Zhang 
3095e5e524a1SHong Zhang    Output Parameter:
3096e5e524a1SHong Zhang .  t  - the previous time
3097e5e524a1SHong Zhang 
3098e5e524a1SHong Zhang    Level: beginner
3099e5e524a1SHong Zhang 
3100e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3101e5e524a1SHong Zhang 
3102e5e524a1SHong Zhang .keywords: TS, get, time
3103e5e524a1SHong Zhang @*/
3104e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
3105e5e524a1SHong Zhang {
3106e5e524a1SHong Zhang   PetscFunctionBegin;
3107e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3108e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
3109e5e524a1SHong Zhang   *t = ts->ptime_prev;
3110e5e524a1SHong Zhang   PetscFunctionReturn(0);
3111e5e524a1SHong Zhang }
3112e5e524a1SHong Zhang 
3113e5e524a1SHong Zhang #undef __FUNCT__
31146a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
31156a4d4014SLisandro Dalcin /*@
31166a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
31176a4d4014SLisandro Dalcin 
31183f9fe445SBarry Smith    Logically Collective on TS
31196a4d4014SLisandro Dalcin 
31206a4d4014SLisandro Dalcin    Input Parameters:
31216a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
31226a4d4014SLisandro Dalcin -  time - the time
31236a4d4014SLisandro Dalcin 
31246a4d4014SLisandro Dalcin    Level: intermediate
31256a4d4014SLisandro Dalcin 
31266a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
31276a4d4014SLisandro Dalcin 
31286a4d4014SLisandro Dalcin .keywords: TS, set, time
31296a4d4014SLisandro Dalcin @*/
31307087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
31316a4d4014SLisandro Dalcin {
31326a4d4014SLisandro Dalcin   PetscFunctionBegin;
31330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3134c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
31356a4d4014SLisandro Dalcin   ts->ptime = t;
31366a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
31376a4d4014SLisandro Dalcin }
31386a4d4014SLisandro Dalcin 
31396a4d4014SLisandro Dalcin #undef __FUNCT__
31404a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
3141d763cef2SBarry Smith /*@C
3142d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
3143d763cef2SBarry Smith    TS options in the database.
3144d763cef2SBarry Smith 
31453f9fe445SBarry Smith    Logically Collective on TS
3146d763cef2SBarry Smith 
3147d763cef2SBarry Smith    Input Parameter:
3148d763cef2SBarry Smith +  ts     - The TS context
3149d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
3150d763cef2SBarry Smith 
3151d763cef2SBarry Smith    Notes:
3152d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3153d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3154d763cef2SBarry Smith    hyphen.
3155d763cef2SBarry Smith 
3156d763cef2SBarry Smith    Level: advanced
3157d763cef2SBarry Smith 
3158d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
3159d763cef2SBarry Smith 
3160d763cef2SBarry Smith .seealso: TSSetFromOptions()
3161d763cef2SBarry Smith 
3162d763cef2SBarry Smith @*/
31637087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
3164d763cef2SBarry Smith {
3165dfbe8321SBarry Smith   PetscErrorCode ierr;
3166089b2837SJed Brown   SNES           snes;
3167d763cef2SBarry Smith 
3168d763cef2SBarry Smith   PetscFunctionBegin;
31690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3170d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3171089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3172089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3173d763cef2SBarry Smith   PetscFunctionReturn(0);
3174d763cef2SBarry Smith }
3175d763cef2SBarry Smith 
3176d763cef2SBarry Smith 
31774a2ae208SSatish Balay #undef __FUNCT__
31784a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
3179d763cef2SBarry Smith /*@C
3180d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
3181d763cef2SBarry Smith    TS options in the database.
3182d763cef2SBarry Smith 
31833f9fe445SBarry Smith    Logically Collective on TS
3184d763cef2SBarry Smith 
3185d763cef2SBarry Smith    Input Parameter:
3186d763cef2SBarry Smith +  ts     - The TS context
3187d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
3188d763cef2SBarry Smith 
3189d763cef2SBarry Smith    Notes:
3190d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3191d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3192d763cef2SBarry Smith    hyphen.
3193d763cef2SBarry Smith 
3194d763cef2SBarry Smith    Level: advanced
3195d763cef2SBarry Smith 
3196d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
3197d763cef2SBarry Smith 
3198d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
3199d763cef2SBarry Smith 
3200d763cef2SBarry Smith @*/
32017087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
3202d763cef2SBarry Smith {
3203dfbe8321SBarry Smith   PetscErrorCode ierr;
3204089b2837SJed Brown   SNES           snes;
3205d763cef2SBarry Smith 
3206d763cef2SBarry Smith   PetscFunctionBegin;
32070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3208d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3209089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3210089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3211d763cef2SBarry Smith   PetscFunctionReturn(0);
3212d763cef2SBarry Smith }
3213d763cef2SBarry Smith 
32144a2ae208SSatish Balay #undef __FUNCT__
32154a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
3216d763cef2SBarry Smith /*@C
3217d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
3218d763cef2SBarry Smith    TS options in the database.
3219d763cef2SBarry Smith 
3220d763cef2SBarry Smith    Not Collective
3221d763cef2SBarry Smith 
3222d763cef2SBarry Smith    Input Parameter:
3223d763cef2SBarry Smith .  ts - The TS context
3224d763cef2SBarry Smith 
3225d763cef2SBarry Smith    Output Parameter:
3226d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
3227d763cef2SBarry Smith 
3228d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
3229d763cef2SBarry Smith    sufficient length to hold the prefix.
3230d763cef2SBarry Smith 
3231d763cef2SBarry Smith    Level: intermediate
3232d763cef2SBarry Smith 
3233d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
3234d763cef2SBarry Smith 
3235d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
3236d763cef2SBarry Smith @*/
32377087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
3238d763cef2SBarry Smith {
3239dfbe8321SBarry Smith   PetscErrorCode ierr;
3240d763cef2SBarry Smith 
3241d763cef2SBarry Smith   PetscFunctionBegin;
32420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32434482741eSBarry Smith   PetscValidPointer(prefix,2);
3244d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3245d763cef2SBarry Smith   PetscFunctionReturn(0);
3246d763cef2SBarry Smith }
3247d763cef2SBarry Smith 
32484a2ae208SSatish Balay #undef __FUNCT__
32494a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
3250d763cef2SBarry Smith /*@C
3251d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
3252d763cef2SBarry Smith 
3253d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
3254d763cef2SBarry Smith 
3255d763cef2SBarry Smith    Input Parameter:
3256d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
3257d763cef2SBarry Smith 
3258d763cef2SBarry Smith    Output Parameters:
3259e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
3260e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
3261e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
3262e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
3263d763cef2SBarry Smith 
32640298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
3265d763cef2SBarry Smith 
3266d763cef2SBarry Smith    Level: intermediate
3267d763cef2SBarry Smith 
326826d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
3269d763cef2SBarry Smith 
3270d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
3271d763cef2SBarry Smith @*/
3272e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
3273d763cef2SBarry Smith {
3274089b2837SJed Brown   PetscErrorCode ierr;
3275089b2837SJed Brown   SNES           snes;
327624989b8cSPeter Brune   DM             dm;
3277089b2837SJed Brown 
3278d763cef2SBarry Smith   PetscFunctionBegin;
3279089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3280e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
328124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
328224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
3283d763cef2SBarry Smith   PetscFunctionReturn(0);
3284d763cef2SBarry Smith }
3285d763cef2SBarry Smith 
32861713a123SBarry Smith #undef __FUNCT__
32872eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
32882eca1d9cSJed Brown /*@C
32892eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
32902eca1d9cSJed Brown 
32912eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
32922eca1d9cSJed Brown 
32932eca1d9cSJed Brown    Input Parameter:
32942eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
32952eca1d9cSJed Brown 
32962eca1d9cSJed Brown    Output Parameters:
3297e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
3298e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
32992eca1d9cSJed Brown .  f   - The function to compute the matrices
33002eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
33012eca1d9cSJed Brown 
33020298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
33032eca1d9cSJed Brown 
33042eca1d9cSJed Brown    Level: advanced
33052eca1d9cSJed Brown 
33062eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
33072eca1d9cSJed Brown 
33082eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
33092eca1d9cSJed Brown @*/
3310e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
33112eca1d9cSJed Brown {
3312089b2837SJed Brown   PetscErrorCode ierr;
3313089b2837SJed Brown   SNES           snes;
331424989b8cSPeter Brune   DM             dm;
33150910c330SBarry Smith 
33162eca1d9cSJed Brown   PetscFunctionBegin;
3317089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3318f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
3319e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
332024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
332124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
33222eca1d9cSJed Brown   PetscFunctionReturn(0);
33232eca1d9cSJed Brown }
33242eca1d9cSJed Brown 
33256083293cSBarry Smith 
33262eca1d9cSJed Brown #undef __FUNCT__
332783a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
33281713a123SBarry Smith /*@C
332983a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
33301713a123SBarry Smith    VecView() for the solution at each timestep
33311713a123SBarry Smith 
33321713a123SBarry Smith    Collective on TS
33331713a123SBarry Smith 
33341713a123SBarry Smith    Input Parameters:
33351713a123SBarry Smith +  ts - the TS context
33361713a123SBarry Smith .  step - current time-step
3337142b95e3SSatish Balay .  ptime - current time
33380298fd71SBarry Smith -  dummy - either a viewer or NULL
33391713a123SBarry Smith 
334099fdda47SBarry Smith    Options Database:
334199fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
334299fdda47SBarry Smith 
334399fdda47SBarry 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
334499fdda47SBarry Smith        will look bad
334599fdda47SBarry Smith 
33461713a123SBarry Smith    Level: intermediate
33471713a123SBarry Smith 
33481713a123SBarry Smith .keywords: TS,  vector, monitor, view
33491713a123SBarry Smith 
3350a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
33511713a123SBarry Smith @*/
33520910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
33531713a123SBarry Smith {
3354dfbe8321SBarry Smith   PetscErrorCode   ierr;
335583a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
3356473a3ab2SBarry Smith   PetscDraw        draw;
33571713a123SBarry Smith 
33581713a123SBarry Smith   PetscFunctionBegin;
33596083293cSBarry Smith   if (!step && ictx->showinitial) {
33606083293cSBarry Smith     if (!ictx->initialsolution) {
33610910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
33621713a123SBarry Smith     }
33630910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
33646083293cSBarry Smith   }
3365b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
33660dcf80beSBarry Smith 
33676083293cSBarry Smith   if (ictx->showinitial) {
33686083293cSBarry Smith     PetscReal pause;
33696083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
33706083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
33716083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
33726083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
33736083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
33746083293cSBarry Smith   }
33750910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
3376473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
3377473a3ab2SBarry Smith     PetscReal xl,yl,xr,yr,tw,w,h;
3378473a3ab2SBarry Smith     char      time[32];
3379473a3ab2SBarry Smith     size_t    len;
3380473a3ab2SBarry Smith 
3381473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
3382473a3ab2SBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
3383473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
3384473a3ab2SBarry Smith     ierr =  PetscStrlen(time,&len);CHKERRQ(ierr);
33850298fd71SBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
3386473a3ab2SBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
3387473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
3388473a3ab2SBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
3389473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
3390473a3ab2SBarry Smith   }
3391473a3ab2SBarry Smith 
33926083293cSBarry Smith   if (ictx->showinitial) {
33936083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
33946083293cSBarry Smith   }
33951713a123SBarry Smith   PetscFunctionReturn(0);
33961713a123SBarry Smith }
33971713a123SBarry Smith 
33982d5ee99bSBarry Smith #undef __FUNCT__
33992d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase"
34002d5ee99bSBarry Smith /*@C
34012d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
34022d5ee99bSBarry Smith 
34032d5ee99bSBarry Smith    Collective on TS
34042d5ee99bSBarry Smith 
34052d5ee99bSBarry Smith    Input Parameters:
34062d5ee99bSBarry Smith +  ts - the TS context
34072d5ee99bSBarry Smith .  step - current time-step
34082d5ee99bSBarry Smith .  ptime - current time
34092d5ee99bSBarry Smith -  dummy - either a viewer or NULL
34102d5ee99bSBarry Smith 
34112d5ee99bSBarry Smith    Level: intermediate
34122d5ee99bSBarry Smith 
34132d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
34142d5ee99bSBarry Smith 
34152d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
34162d5ee99bSBarry Smith @*/
34172d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
34182d5ee99bSBarry Smith {
34192d5ee99bSBarry Smith   PetscErrorCode    ierr;
34202d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
34212d5ee99bSBarry Smith   PetscDraw         draw;
34222d5ee99bSBarry Smith   MPI_Comm          comm;
34232d5ee99bSBarry Smith   PetscInt          n;
34242d5ee99bSBarry Smith   PetscMPIInt       size;
34252d5ee99bSBarry Smith   PetscReal         xl,yl,xr,yr,tw,w,h;
34262d5ee99bSBarry Smith   char              time[32];
34272d5ee99bSBarry Smith   size_t            len;
34282d5ee99bSBarry Smith   const PetscScalar *U;
34292d5ee99bSBarry Smith 
34302d5ee99bSBarry Smith   PetscFunctionBegin;
34312d5ee99bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
34322d5ee99bSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
34332d5ee99bSBarry Smith   if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs");
34342d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
34352d5ee99bSBarry Smith   if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns");
34362d5ee99bSBarry Smith 
34372d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
34382d5ee99bSBarry Smith 
34392d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
34404b363babSBarry Smith   ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
3441ba5783adSMatthew G Knepley   if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) {
3442a4494fc1SBarry Smith       ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
3443a4494fc1SBarry Smith       PetscFunctionReturn(0);
3444a4494fc1SBarry Smith   }
34452d5ee99bSBarry Smith   if (!step) ictx->color++;
34462d5ee99bSBarry Smith   ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr);
34472d5ee99bSBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
34482d5ee99bSBarry Smith 
34492d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
34504b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
34512d5ee99bSBarry Smith     ierr = PetscSNPrintf(time,32,"Timestep %d Time %f",(int)step,(double)ptime);CHKERRQ(ierr);
34522d5ee99bSBarry Smith     ierr = PetscStrlen(time,&len);CHKERRQ(ierr);
34532d5ee99bSBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
34542d5ee99bSBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
34552d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
34562d5ee99bSBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
34572d5ee99bSBarry Smith   }
34582d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
34592d5ee99bSBarry Smith   PetscFunctionReturn(0);
34602d5ee99bSBarry Smith }
34612d5ee99bSBarry Smith 
34621713a123SBarry Smith 
34636c699258SBarry Smith #undef __FUNCT__
346483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
34656083293cSBarry Smith /*@C
346683a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
34676083293cSBarry Smith 
34686083293cSBarry Smith    Collective on TS
34696083293cSBarry Smith 
34706083293cSBarry Smith    Input Parameters:
34716083293cSBarry Smith .    ctx - the monitor context
34726083293cSBarry Smith 
34736083293cSBarry Smith    Level: intermediate
34746083293cSBarry Smith 
34756083293cSBarry Smith .keywords: TS,  vector, monitor, view
34766083293cSBarry Smith 
347783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
34786083293cSBarry Smith @*/
347983a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
34806083293cSBarry Smith {
34816083293cSBarry Smith   PetscErrorCode ierr;
34826083293cSBarry Smith 
34836083293cSBarry Smith   PetscFunctionBegin;
34844b363babSBarry Smith   ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr);
348583a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
348683a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
348783a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
34886083293cSBarry Smith   PetscFunctionReturn(0);
34896083293cSBarry Smith }
34906083293cSBarry Smith 
34916083293cSBarry Smith #undef __FUNCT__
349283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
34936083293cSBarry Smith /*@C
349483a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
34956083293cSBarry Smith 
34966083293cSBarry Smith    Collective on TS
34976083293cSBarry Smith 
34986083293cSBarry Smith    Input Parameter:
34996083293cSBarry Smith .    ts - time-step context
35006083293cSBarry Smith 
35016083293cSBarry Smith    Output Patameter:
35026083293cSBarry Smith .    ctx - the monitor context
35036083293cSBarry Smith 
350499fdda47SBarry Smith    Options Database:
350599fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
350699fdda47SBarry Smith 
35076083293cSBarry Smith    Level: intermediate
35086083293cSBarry Smith 
35096083293cSBarry Smith .keywords: TS,  vector, monitor, view
35106083293cSBarry Smith 
351183a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
35126083293cSBarry Smith @*/
351383a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
35146083293cSBarry Smith {
35156083293cSBarry Smith   PetscErrorCode   ierr;
35166083293cSBarry Smith 
35176083293cSBarry Smith   PetscFunctionBegin;
3518b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
351983a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
3520e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
3521bbd56ea5SKarl Rupp 
352283a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
3523473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
35240298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
3525473a3ab2SBarry Smith 
3526473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
35270298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
35282d5ee99bSBarry Smith   (*ctx)->color = PETSC_DRAW_WHITE;
35296083293cSBarry Smith   PetscFunctionReturn(0);
35306083293cSBarry Smith }
35316083293cSBarry Smith 
35326083293cSBarry Smith #undef __FUNCT__
353383a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
35343a471f94SBarry Smith /*@C
353583a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
35363a471f94SBarry Smith    VecView() for the error at each timestep
35373a471f94SBarry Smith 
35383a471f94SBarry Smith    Collective on TS
35393a471f94SBarry Smith 
35403a471f94SBarry Smith    Input Parameters:
35413a471f94SBarry Smith +  ts - the TS context
35423a471f94SBarry Smith .  step - current time-step
35433a471f94SBarry Smith .  ptime - current time
35440298fd71SBarry Smith -  dummy - either a viewer or NULL
35453a471f94SBarry Smith 
35463a471f94SBarry Smith    Level: intermediate
35473a471f94SBarry Smith 
35483a471f94SBarry Smith .keywords: TS,  vector, monitor, view
35493a471f94SBarry Smith 
35503a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
35513a471f94SBarry Smith @*/
35520910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
35533a471f94SBarry Smith {
35543a471f94SBarry Smith   PetscErrorCode   ierr;
355583a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
355683a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
35573a471f94SBarry Smith   Vec              work;
35583a471f94SBarry Smith 
35593a471f94SBarry Smith   PetscFunctionBegin;
3560b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
35610910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
35623a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
35630910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
35643a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
35653a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
35663a471f94SBarry Smith   PetscFunctionReturn(0);
35673a471f94SBarry Smith }
35683a471f94SBarry Smith 
35692a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
35703a471f94SBarry Smith #undef __FUNCT__
35716c699258SBarry Smith #define __FUNCT__ "TSSetDM"
35726c699258SBarry Smith /*@
35736c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
35746c699258SBarry Smith 
35753f9fe445SBarry Smith    Logically Collective on TS and DM
35766c699258SBarry Smith 
35776c699258SBarry Smith    Input Parameters:
35786c699258SBarry Smith +  ts - the preconditioner context
35796c699258SBarry Smith -  dm - the dm
35806c699258SBarry Smith 
35816c699258SBarry Smith    Level: intermediate
35826c699258SBarry Smith 
35836c699258SBarry Smith 
35846c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
35856c699258SBarry Smith @*/
35867087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
35876c699258SBarry Smith {
35886c699258SBarry Smith   PetscErrorCode ierr;
3589089b2837SJed Brown   SNES           snes;
3590942e3340SBarry Smith   DMTS           tsdm;
35916c699258SBarry Smith 
35926c699258SBarry Smith   PetscFunctionBegin;
35930700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
359470663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
3595942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
35962a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
3597942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
3598942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
359924989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
360024989b8cSPeter Brune         tsdm->originaldm = dm;
360124989b8cSPeter Brune       }
360224989b8cSPeter Brune     }
36036bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
360424989b8cSPeter Brune   }
36056c699258SBarry Smith   ts->dm = dm;
3606bbd56ea5SKarl Rupp 
3607089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3608089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
36096c699258SBarry Smith   PetscFunctionReturn(0);
36106c699258SBarry Smith }
36116c699258SBarry Smith 
36126c699258SBarry Smith #undef __FUNCT__
36136c699258SBarry Smith #define __FUNCT__ "TSGetDM"
36146c699258SBarry Smith /*@
36156c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
36166c699258SBarry Smith 
36173f9fe445SBarry Smith    Not Collective
36186c699258SBarry Smith 
36196c699258SBarry Smith    Input Parameter:
36206c699258SBarry Smith . ts - the preconditioner context
36216c699258SBarry Smith 
36226c699258SBarry Smith    Output Parameter:
36236c699258SBarry Smith .  dm - the dm
36246c699258SBarry Smith 
36256c699258SBarry Smith    Level: intermediate
36266c699258SBarry Smith 
36276c699258SBarry Smith 
36286c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
36296c699258SBarry Smith @*/
36307087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
36316c699258SBarry Smith {
3632496e6a7aSJed Brown   PetscErrorCode ierr;
3633496e6a7aSJed Brown 
36346c699258SBarry Smith   PetscFunctionBegin;
36350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3636496e6a7aSJed Brown   if (!ts->dm) {
3637ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
3638496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
3639496e6a7aSJed Brown   }
36406c699258SBarry Smith   *dm = ts->dm;
36416c699258SBarry Smith   PetscFunctionReturn(0);
36426c699258SBarry Smith }
36431713a123SBarry Smith 
36440f5c6efeSJed Brown #undef __FUNCT__
36450f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
36460f5c6efeSJed Brown /*@
36470f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
36480f5c6efeSJed Brown 
36493f9fe445SBarry Smith    Logically Collective on SNES
36500f5c6efeSJed Brown 
36510f5c6efeSJed Brown    Input Parameter:
3652d42a1c89SJed Brown + snes - nonlinear solver
36530910c330SBarry Smith . U - the current state at which to evaluate the residual
3654d42a1c89SJed Brown - ctx - user context, must be a TS
36550f5c6efeSJed Brown 
36560f5c6efeSJed Brown    Output Parameter:
36570f5c6efeSJed Brown . F - the nonlinear residual
36580f5c6efeSJed Brown 
36590f5c6efeSJed Brown    Notes:
36600f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
36610f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
36620f5c6efeSJed Brown 
36630f5c6efeSJed Brown    Level: advanced
36640f5c6efeSJed Brown 
36650f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
36660f5c6efeSJed Brown @*/
36670910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
36680f5c6efeSJed Brown {
36690f5c6efeSJed Brown   TS             ts = (TS)ctx;
36700f5c6efeSJed Brown   PetscErrorCode ierr;
36710f5c6efeSJed Brown 
36720f5c6efeSJed Brown   PetscFunctionBegin;
36730f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
36740910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
36750f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
36760f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
36770910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
36780f5c6efeSJed Brown   PetscFunctionReturn(0);
36790f5c6efeSJed Brown }
36800f5c6efeSJed Brown 
36810f5c6efeSJed Brown #undef __FUNCT__
36820f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
36830f5c6efeSJed Brown /*@
36840f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
36850f5c6efeSJed Brown 
36860f5c6efeSJed Brown    Collective on SNES
36870f5c6efeSJed Brown 
36880f5c6efeSJed Brown    Input Parameter:
36890f5c6efeSJed Brown + snes - nonlinear solver
36900910c330SBarry Smith . U - the current state at which to evaluate the residual
36910f5c6efeSJed Brown - ctx - user context, must be a TS
36920f5c6efeSJed Brown 
36930f5c6efeSJed Brown    Output Parameter:
36940f5c6efeSJed Brown + A - the Jacobian
36950f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
36960f5c6efeSJed Brown - flag - indicates any structure change in the matrix
36970f5c6efeSJed Brown 
36980f5c6efeSJed Brown    Notes:
36990f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
37000f5c6efeSJed Brown 
37010f5c6efeSJed Brown    Level: developer
37020f5c6efeSJed Brown 
37030f5c6efeSJed Brown .seealso: SNESSetJacobian()
37040f5c6efeSJed Brown @*/
3705d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
37060f5c6efeSJed Brown {
37070f5c6efeSJed Brown   TS             ts = (TS)ctx;
37080f5c6efeSJed Brown   PetscErrorCode ierr;
37090f5c6efeSJed Brown 
37100f5c6efeSJed Brown   PetscFunctionBegin;
37110f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
37120910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
37130f5c6efeSJed Brown   PetscValidPointer(A,3);
371494ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
37150f5c6efeSJed Brown   PetscValidPointer(B,4);
371694ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
37170f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
3718d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
37190f5c6efeSJed Brown   PetscFunctionReturn(0);
37200f5c6efeSJed Brown }
3721325fc9f4SBarry Smith 
37220e4ef248SJed Brown #undef __FUNCT__
37230e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
37240e4ef248SJed Brown /*@C
37250e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
37260e4ef248SJed Brown 
37270e4ef248SJed Brown    Collective on TS
37280e4ef248SJed Brown 
37290e4ef248SJed Brown    Input Arguments:
37300e4ef248SJed Brown +  ts - time stepping context
37310e4ef248SJed Brown .  t - time at which to evaluate
37320910c330SBarry Smith .  U - state at which to evaluate
37330e4ef248SJed Brown -  ctx - context
37340e4ef248SJed Brown 
37350e4ef248SJed Brown    Output Arguments:
37360e4ef248SJed Brown .  F - right hand side
37370e4ef248SJed Brown 
37380e4ef248SJed Brown    Level: intermediate
37390e4ef248SJed Brown 
37400e4ef248SJed Brown    Notes:
37410e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
37420e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
37430e4ef248SJed Brown 
37440e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
37450e4ef248SJed Brown @*/
37460910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
37470e4ef248SJed Brown {
37480e4ef248SJed Brown   PetscErrorCode ierr;
37490e4ef248SJed Brown   Mat            Arhs,Brhs;
37500e4ef248SJed Brown 
37510e4ef248SJed Brown   PetscFunctionBegin;
37520e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
3753d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
37540910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
37550e4ef248SJed Brown   PetscFunctionReturn(0);
37560e4ef248SJed Brown }
37570e4ef248SJed Brown 
37580e4ef248SJed Brown #undef __FUNCT__
37590e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
37600e4ef248SJed Brown /*@C
37610e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
37620e4ef248SJed Brown 
37630e4ef248SJed Brown    Collective on TS
37640e4ef248SJed Brown 
37650e4ef248SJed Brown    Input Arguments:
37660e4ef248SJed Brown +  ts - time stepping context
37670e4ef248SJed Brown .  t - time at which to evaluate
37680910c330SBarry Smith .  U - state at which to evaluate
37690e4ef248SJed Brown -  ctx - context
37700e4ef248SJed Brown 
37710e4ef248SJed Brown    Output Arguments:
37720e4ef248SJed Brown +  A - pointer to operator
37730e4ef248SJed Brown .  B - pointer to preconditioning matrix
37740e4ef248SJed Brown -  flg - matrix structure flag
37750e4ef248SJed Brown 
37760e4ef248SJed Brown    Level: intermediate
37770e4ef248SJed Brown 
37780e4ef248SJed Brown    Notes:
37790e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
37800e4ef248SJed Brown 
37810e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
37820e4ef248SJed Brown @*/
3783d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
37840e4ef248SJed Brown {
37850e4ef248SJed Brown   PetscFunctionBegin;
37860e4ef248SJed Brown   PetscFunctionReturn(0);
37870e4ef248SJed Brown }
37880e4ef248SJed Brown 
37890026cea9SSean Farley #undef __FUNCT__
37900026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
37910026cea9SSean Farley /*@C
37920026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
37930026cea9SSean Farley 
37940026cea9SSean Farley    Collective on TS
37950026cea9SSean Farley 
37960026cea9SSean Farley    Input Arguments:
37970026cea9SSean Farley +  ts - time stepping context
37980026cea9SSean Farley .  t - time at which to evaluate
37990910c330SBarry Smith .  U - state at which to evaluate
38000910c330SBarry Smith .  Udot - time derivative of state vector
38010026cea9SSean Farley -  ctx - context
38020026cea9SSean Farley 
38030026cea9SSean Farley    Output Arguments:
38040026cea9SSean Farley .  F - left hand side
38050026cea9SSean Farley 
38060026cea9SSean Farley    Level: intermediate
38070026cea9SSean Farley 
38080026cea9SSean Farley    Notes:
38090910c330SBarry 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
38100026cea9SSean Farley    user is required to write their own TSComputeIFunction.
38110026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
38120026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
38130026cea9SSean Farley 
38140026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
38150026cea9SSean Farley @*/
38160910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
38170026cea9SSean Farley {
38180026cea9SSean Farley   PetscErrorCode ierr;
38190026cea9SSean Farley   Mat            A,B;
38200026cea9SSean Farley 
38210026cea9SSean Farley   PetscFunctionBegin;
38220298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
3823d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
38240910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
38250026cea9SSean Farley   PetscFunctionReturn(0);
38260026cea9SSean Farley }
38270026cea9SSean Farley 
38280026cea9SSean Farley #undef __FUNCT__
38290026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
38300026cea9SSean Farley /*@C
3831b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
38320026cea9SSean Farley 
38330026cea9SSean Farley    Collective on TS
38340026cea9SSean Farley 
38350026cea9SSean Farley    Input Arguments:
38360026cea9SSean Farley +  ts - time stepping context
38370026cea9SSean Farley .  t - time at which to evaluate
38380910c330SBarry Smith .  U - state at which to evaluate
38390910c330SBarry Smith .  Udot - time derivative of state vector
38400026cea9SSean Farley .  shift - shift to apply
38410026cea9SSean Farley -  ctx - context
38420026cea9SSean Farley 
38430026cea9SSean Farley    Output Arguments:
38440026cea9SSean Farley +  A - pointer to operator
38450026cea9SSean Farley .  B - pointer to preconditioning matrix
38460026cea9SSean Farley -  flg - matrix structure flag
38470026cea9SSean Farley 
3848b41af12eSJed Brown    Level: advanced
38490026cea9SSean Farley 
38500026cea9SSean Farley    Notes:
38510026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
38520026cea9SSean Farley 
3853b41af12eSJed Brown    It is only appropriate for problems of the form
3854b41af12eSJed Brown 
3855b41af12eSJed Brown $     M Udot = F(U,t)
3856b41af12eSJed Brown 
3857b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
3858b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
3859b41af12eSJed Brown   an implicit operator of the form
3860b41af12eSJed Brown 
3861b41af12eSJed Brown $    shift*M + J
3862b41af12eSJed Brown 
3863b41af12eSJed 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
3864b41af12eSJed Brown   a copy of M or reassemble it when requested.
3865b41af12eSJed Brown 
38660026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
38670026cea9SSean Farley @*/
3868d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
38690026cea9SSean Farley {
3870b41af12eSJed Brown   PetscErrorCode ierr;
3871b41af12eSJed Brown 
38720026cea9SSean Farley   PetscFunctionBegin;
387394ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
3874b41af12eSJed Brown   ts->ijacobian.shift = shift;
38750026cea9SSean Farley   PetscFunctionReturn(0);
38760026cea9SSean Farley }
3877b41af12eSJed Brown 
3878e817cc15SEmil Constantinescu #undef __FUNCT__
3879e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
3880e817cc15SEmil Constantinescu /*@
3881e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
3882e817cc15SEmil Constantinescu 
3883e817cc15SEmil Constantinescu    Not Collective
3884e817cc15SEmil Constantinescu 
3885e817cc15SEmil Constantinescu    Input Parameter:
3886e817cc15SEmil Constantinescu .  ts - the TS context
3887e817cc15SEmil Constantinescu 
3888e817cc15SEmil Constantinescu    Output Parameter:
38894e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
3890e817cc15SEmil Constantinescu 
3891e817cc15SEmil Constantinescu    Level: beginner
3892e817cc15SEmil Constantinescu 
3893e817cc15SEmil Constantinescu .keywords: TS, equation type
3894e817cc15SEmil Constantinescu 
3895e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
3896e817cc15SEmil Constantinescu @*/
3897e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
3898e817cc15SEmil Constantinescu {
3899e817cc15SEmil Constantinescu   PetscFunctionBegin;
3900e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3901e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
3902e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
3903e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3904e817cc15SEmil Constantinescu }
3905e817cc15SEmil Constantinescu 
3906e817cc15SEmil Constantinescu #undef __FUNCT__
3907e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
3908e817cc15SEmil Constantinescu /*@
3909e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
3910e817cc15SEmil Constantinescu 
3911e817cc15SEmil Constantinescu    Not Collective
3912e817cc15SEmil Constantinescu 
3913e817cc15SEmil Constantinescu    Input Parameter:
3914e817cc15SEmil Constantinescu +  ts - the TS context
39154e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
3916e817cc15SEmil Constantinescu 
3917e817cc15SEmil Constantinescu    Level: advanced
3918e817cc15SEmil Constantinescu 
3919e817cc15SEmil Constantinescu .keywords: TS, equation type
3920e817cc15SEmil Constantinescu 
3921e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
3922e817cc15SEmil Constantinescu @*/
3923e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
3924e817cc15SEmil Constantinescu {
3925e817cc15SEmil Constantinescu   PetscFunctionBegin;
3926e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3927e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
3928e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
3929e817cc15SEmil Constantinescu }
39300026cea9SSean Farley 
39314af1b03aSJed Brown #undef __FUNCT__
39324af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
39334af1b03aSJed Brown /*@
39344af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
39354af1b03aSJed Brown 
39364af1b03aSJed Brown    Not Collective
39374af1b03aSJed Brown 
39384af1b03aSJed Brown    Input Parameter:
39394af1b03aSJed Brown .  ts - the TS context
39404af1b03aSJed Brown 
39414af1b03aSJed Brown    Output Parameter:
39424af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
39434af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
39444af1b03aSJed Brown 
3945487e0bb9SJed Brown    Level: beginner
39464af1b03aSJed Brown 
3947cd652676SJed Brown    Notes:
3948cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
39494af1b03aSJed Brown 
39504af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
39514af1b03aSJed Brown 
39524af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
39534af1b03aSJed Brown @*/
39544af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
39554af1b03aSJed Brown {
39564af1b03aSJed Brown   PetscFunctionBegin;
39574af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
39584af1b03aSJed Brown   PetscValidPointer(reason,2);
39594af1b03aSJed Brown   *reason = ts->reason;
39604af1b03aSJed Brown   PetscFunctionReturn(0);
39614af1b03aSJed Brown }
39624af1b03aSJed Brown 
3963fb1732b5SBarry Smith #undef __FUNCT__
3964d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
3965d6ad946cSShri Abhyankar /*@
3966d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
3967d6ad946cSShri Abhyankar 
3968d6ad946cSShri Abhyankar    Not Collective
3969d6ad946cSShri Abhyankar 
3970d6ad946cSShri Abhyankar    Input Parameter:
3971d6ad946cSShri Abhyankar +  ts - the TS context
3972d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
3973d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
3974d6ad946cSShri Abhyankar 
3975f5abba47SShri Abhyankar    Level: advanced
3976d6ad946cSShri Abhyankar 
3977d6ad946cSShri Abhyankar    Notes:
3978d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
3979d6ad946cSShri Abhyankar 
3980d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
3981d6ad946cSShri Abhyankar 
3982d6ad946cSShri Abhyankar .seealso: TSConvergedReason
3983d6ad946cSShri Abhyankar @*/
3984d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
3985d6ad946cSShri Abhyankar {
3986d6ad946cSShri Abhyankar   PetscFunctionBegin;
3987d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3988d6ad946cSShri Abhyankar   ts->reason = reason;
3989d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
3990d6ad946cSShri Abhyankar }
3991d6ad946cSShri Abhyankar 
3992d6ad946cSShri Abhyankar #undef __FUNCT__
3993cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
3994cc708dedSBarry Smith /*@
3995cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
3996cc708dedSBarry Smith 
3997cc708dedSBarry Smith    Not Collective
3998cc708dedSBarry Smith 
3999cc708dedSBarry Smith    Input Parameter:
4000cc708dedSBarry Smith .  ts - the TS context
4001cc708dedSBarry Smith 
4002cc708dedSBarry Smith    Output Parameter:
4003cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
4004cc708dedSBarry Smith 
4005487e0bb9SJed Brown    Level: beginner
4006cc708dedSBarry Smith 
4007cc708dedSBarry Smith    Notes:
4008cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
4009cc708dedSBarry Smith 
4010cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
4011cc708dedSBarry Smith 
4012cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
4013cc708dedSBarry Smith @*/
4014cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
4015cc708dedSBarry Smith {
4016cc708dedSBarry Smith   PetscFunctionBegin;
4017cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4018cc708dedSBarry Smith   PetscValidPointer(ftime,2);
4019cc708dedSBarry Smith   *ftime = ts->solvetime;
4020cc708dedSBarry Smith   PetscFunctionReturn(0);
4021cc708dedSBarry Smith }
4022cc708dedSBarry Smith 
4023cc708dedSBarry Smith #undef __FUNCT__
40245ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
40259f67acb7SJed Brown /*@
40265ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
40279f67acb7SJed Brown    used by the time integrator.
40289f67acb7SJed Brown 
40299f67acb7SJed Brown    Not Collective
40309f67acb7SJed Brown 
40319f67acb7SJed Brown    Input Parameter:
40329f67acb7SJed Brown .  ts - TS context
40339f67acb7SJed Brown 
40349f67acb7SJed Brown    Output Parameter:
40359f67acb7SJed Brown .  nits - number of nonlinear iterations
40369f67acb7SJed Brown 
40379f67acb7SJed Brown    Notes:
40389f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
40399f67acb7SJed Brown 
40409f67acb7SJed Brown    Level: intermediate
40419f67acb7SJed Brown 
40429f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
40439f67acb7SJed Brown 
40445ef26d82SJed Brown .seealso:  TSGetKSPIterations()
40459f67acb7SJed Brown @*/
40465ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
40479f67acb7SJed Brown {
40489f67acb7SJed Brown   PetscFunctionBegin;
40499f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
40509f67acb7SJed Brown   PetscValidIntPointer(nits,2);
40515ef26d82SJed Brown   *nits = ts->snes_its;
40529f67acb7SJed Brown   PetscFunctionReturn(0);
40539f67acb7SJed Brown }
40549f67acb7SJed Brown 
40559f67acb7SJed Brown #undef __FUNCT__
40565ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
40579f67acb7SJed Brown /*@
40585ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
40599f67acb7SJed Brown    used by the time integrator.
40609f67acb7SJed Brown 
40619f67acb7SJed Brown    Not Collective
40629f67acb7SJed Brown 
40639f67acb7SJed Brown    Input Parameter:
40649f67acb7SJed Brown .  ts - TS context
40659f67acb7SJed Brown 
40669f67acb7SJed Brown    Output Parameter:
40679f67acb7SJed Brown .  lits - number of linear iterations
40689f67acb7SJed Brown 
40699f67acb7SJed Brown    Notes:
40709f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
40719f67acb7SJed Brown 
40729f67acb7SJed Brown    Level: intermediate
40739f67acb7SJed Brown 
40749f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
40759f67acb7SJed Brown 
40765ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
40779f67acb7SJed Brown @*/
40785ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
40799f67acb7SJed Brown {
40809f67acb7SJed Brown   PetscFunctionBegin;
40819f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
40829f67acb7SJed Brown   PetscValidIntPointer(lits,2);
40835ef26d82SJed Brown   *lits = ts->ksp_its;
40849f67acb7SJed Brown   PetscFunctionReturn(0);
40859f67acb7SJed Brown }
40869f67acb7SJed Brown 
40879f67acb7SJed Brown #undef __FUNCT__
4088cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
4089cef5090cSJed Brown /*@
4090cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
4091cef5090cSJed Brown 
4092cef5090cSJed Brown    Not Collective
4093cef5090cSJed Brown 
4094cef5090cSJed Brown    Input Parameter:
4095cef5090cSJed Brown .  ts - TS context
4096cef5090cSJed Brown 
4097cef5090cSJed Brown    Output Parameter:
4098cef5090cSJed Brown .  rejects - number of steps rejected
4099cef5090cSJed Brown 
4100cef5090cSJed Brown    Notes:
4101cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4102cef5090cSJed Brown 
4103cef5090cSJed Brown    Level: intermediate
4104cef5090cSJed Brown 
4105cef5090cSJed Brown .keywords: TS, get, number
4106cef5090cSJed Brown 
41075ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
4108cef5090cSJed Brown @*/
4109cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
4110cef5090cSJed Brown {
4111cef5090cSJed Brown   PetscFunctionBegin;
4112cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4113cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
4114cef5090cSJed Brown   *rejects = ts->reject;
4115cef5090cSJed Brown   PetscFunctionReturn(0);
4116cef5090cSJed Brown }
4117cef5090cSJed Brown 
4118cef5090cSJed Brown #undef __FUNCT__
4119cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
4120cef5090cSJed Brown /*@
4121cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
4122cef5090cSJed Brown 
4123cef5090cSJed Brown    Not Collective
4124cef5090cSJed Brown 
4125cef5090cSJed Brown    Input Parameter:
4126cef5090cSJed Brown .  ts - TS context
4127cef5090cSJed Brown 
4128cef5090cSJed Brown    Output Parameter:
4129cef5090cSJed Brown .  fails - number of failed nonlinear solves
4130cef5090cSJed Brown 
4131cef5090cSJed Brown    Notes:
4132cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4133cef5090cSJed Brown 
4134cef5090cSJed Brown    Level: intermediate
4135cef5090cSJed Brown 
4136cef5090cSJed Brown .keywords: TS, get, number
4137cef5090cSJed Brown 
41385ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
4139cef5090cSJed Brown @*/
4140cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
4141cef5090cSJed Brown {
4142cef5090cSJed Brown   PetscFunctionBegin;
4143cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4144cef5090cSJed Brown   PetscValidIntPointer(fails,2);
4145cef5090cSJed Brown   *fails = ts->num_snes_failures;
4146cef5090cSJed Brown   PetscFunctionReturn(0);
4147cef5090cSJed Brown }
4148cef5090cSJed Brown 
4149cef5090cSJed Brown #undef __FUNCT__
4150cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
4151cef5090cSJed Brown /*@
4152cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
4153cef5090cSJed Brown 
4154cef5090cSJed Brown    Not Collective
4155cef5090cSJed Brown 
4156cef5090cSJed Brown    Input Parameter:
4157cef5090cSJed Brown +  ts - TS context
4158cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
4159cef5090cSJed Brown 
4160cef5090cSJed Brown    Notes:
4161cef5090cSJed Brown    The counter is reset to zero for each step
4162cef5090cSJed Brown 
4163cef5090cSJed Brown    Options Database Key:
4164cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
4165cef5090cSJed Brown 
4166cef5090cSJed Brown    Level: intermediate
4167cef5090cSJed Brown 
4168cef5090cSJed Brown .keywords: TS, set, maximum, number
4169cef5090cSJed Brown 
41705ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4171cef5090cSJed Brown @*/
4172cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
4173cef5090cSJed Brown {
4174cef5090cSJed Brown   PetscFunctionBegin;
4175cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4176cef5090cSJed Brown   ts->max_reject = rejects;
4177cef5090cSJed Brown   PetscFunctionReturn(0);
4178cef5090cSJed Brown }
4179cef5090cSJed Brown 
4180cef5090cSJed Brown #undef __FUNCT__
4181cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
4182cef5090cSJed Brown /*@
4183cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
4184cef5090cSJed Brown 
4185cef5090cSJed Brown    Not Collective
4186cef5090cSJed Brown 
4187cef5090cSJed Brown    Input Parameter:
4188cef5090cSJed Brown +  ts - TS context
4189cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
4190cef5090cSJed Brown 
4191cef5090cSJed Brown    Notes:
4192cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
4193cef5090cSJed Brown 
4194cef5090cSJed Brown    Options Database Key:
4195cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
4196cef5090cSJed Brown 
4197cef5090cSJed Brown    Level: intermediate
4198cef5090cSJed Brown 
4199cef5090cSJed Brown .keywords: TS, set, maximum, number
4200cef5090cSJed Brown 
42015ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
4202cef5090cSJed Brown @*/
4203cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
4204cef5090cSJed Brown {
4205cef5090cSJed Brown   PetscFunctionBegin;
4206cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4207cef5090cSJed Brown   ts->max_snes_failures = fails;
4208cef5090cSJed Brown   PetscFunctionReturn(0);
4209cef5090cSJed Brown }
4210cef5090cSJed Brown 
4211cef5090cSJed Brown #undef __FUNCT__
42124e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails"
4213cef5090cSJed Brown /*@
4214cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
4215cef5090cSJed Brown 
4216cef5090cSJed Brown    Not Collective
4217cef5090cSJed Brown 
4218cef5090cSJed Brown    Input Parameter:
4219cef5090cSJed Brown +  ts - TS context
4220cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
4221cef5090cSJed Brown 
4222cef5090cSJed Brown    Options Database Key:
4223cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
4224cef5090cSJed Brown 
4225cef5090cSJed Brown    Level: intermediate
4226cef5090cSJed Brown 
4227cef5090cSJed Brown .keywords: TS, set, error
4228cef5090cSJed Brown 
42295ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4230cef5090cSJed Brown @*/
4231cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
4232cef5090cSJed Brown {
4233cef5090cSJed Brown   PetscFunctionBegin;
4234cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4235cef5090cSJed Brown   ts->errorifstepfailed = err;
4236cef5090cSJed Brown   PetscFunctionReturn(0);
4237cef5090cSJed Brown }
4238cef5090cSJed Brown 
4239cef5090cSJed Brown #undef __FUNCT__
4240fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
4241fb1732b5SBarry Smith /*@C
4242fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
4243fb1732b5SBarry Smith 
4244fb1732b5SBarry Smith    Collective on TS
4245fb1732b5SBarry Smith 
4246fb1732b5SBarry Smith    Input Parameters:
4247fb1732b5SBarry Smith +  ts - the TS context
4248fb1732b5SBarry Smith .  step - current time-step
4249fb1732b5SBarry Smith .  ptime - current time
42500910c330SBarry Smith .  u - current state
4251fb1732b5SBarry Smith -  viewer - binary viewer
4252fb1732b5SBarry Smith 
4253fb1732b5SBarry Smith    Level: intermediate
4254fb1732b5SBarry Smith 
4255fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
4256fb1732b5SBarry Smith 
4257fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4258fb1732b5SBarry Smith @*/
42590910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
4260fb1732b5SBarry Smith {
4261fb1732b5SBarry Smith   PetscErrorCode ierr;
4262ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
4263fb1732b5SBarry Smith 
4264fb1732b5SBarry Smith   PetscFunctionBegin;
42650910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
4266ed81e22dSJed Brown   PetscFunctionReturn(0);
4267ed81e22dSJed Brown }
4268ed81e22dSJed Brown 
4269ed81e22dSJed Brown #undef __FUNCT__
4270ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
4271ed81e22dSJed Brown /*@C
4272ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
4273ed81e22dSJed Brown 
4274ed81e22dSJed Brown    Collective on TS
4275ed81e22dSJed Brown 
4276ed81e22dSJed Brown    Input Parameters:
4277ed81e22dSJed Brown +  ts - the TS context
4278ed81e22dSJed Brown .  step - current time-step
4279ed81e22dSJed Brown .  ptime - current time
42800910c330SBarry Smith .  u - current state
4281ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4282ed81e22dSJed Brown 
4283ed81e22dSJed Brown    Level: intermediate
4284ed81e22dSJed Brown 
4285ed81e22dSJed Brown    Notes:
4286ed81e22dSJed 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.
4287ed81e22dSJed Brown    These are named according to the file name template.
4288ed81e22dSJed Brown 
4289ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
4290ed81e22dSJed Brown 
4291ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4292ed81e22dSJed Brown 
4293ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4294ed81e22dSJed Brown @*/
42950910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
4296ed81e22dSJed Brown {
4297ed81e22dSJed Brown   PetscErrorCode ierr;
4298ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
4299ed81e22dSJed Brown   PetscViewer    viewer;
4300ed81e22dSJed Brown 
4301ed81e22dSJed Brown   PetscFunctionBegin;
43028caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
4303ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
43040910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
4305ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
4306ed81e22dSJed Brown   PetscFunctionReturn(0);
4307ed81e22dSJed Brown }
4308ed81e22dSJed Brown 
4309ed81e22dSJed Brown #undef __FUNCT__
4310ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
4311ed81e22dSJed Brown /*@C
4312ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
4313ed81e22dSJed Brown 
4314ed81e22dSJed Brown    Collective on TS
4315ed81e22dSJed Brown 
4316ed81e22dSJed Brown    Input Parameters:
4317ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4318ed81e22dSJed Brown 
4319ed81e22dSJed Brown    Level: intermediate
4320ed81e22dSJed Brown 
4321ed81e22dSJed Brown    Note:
4322ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
4323ed81e22dSJed Brown 
4324ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4325ed81e22dSJed Brown 
4326ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
4327ed81e22dSJed Brown @*/
4328ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
4329ed81e22dSJed Brown {
4330ed81e22dSJed Brown   PetscErrorCode ierr;
4331ed81e22dSJed Brown 
4332ed81e22dSJed Brown   PetscFunctionBegin;
4333ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
4334fb1732b5SBarry Smith   PetscFunctionReturn(0);
4335fb1732b5SBarry Smith }
4336fb1732b5SBarry Smith 
433784df9cb4SJed Brown #undef __FUNCT__
4338552698daSJed Brown #define __FUNCT__ "TSGetAdapt"
433984df9cb4SJed Brown /*@
4340552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
434184df9cb4SJed Brown 
4342ed81e22dSJed Brown    Collective on TS if controller has not been created yet
434384df9cb4SJed Brown 
434484df9cb4SJed Brown    Input Arguments:
4345ed81e22dSJed Brown .  ts - time stepping context
434684df9cb4SJed Brown 
434784df9cb4SJed Brown    Output Arguments:
4348ed81e22dSJed Brown .  adapt - adaptive controller
434984df9cb4SJed Brown 
435084df9cb4SJed Brown    Level: intermediate
435184df9cb4SJed Brown 
4352ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
435384df9cb4SJed Brown @*/
4354552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
435584df9cb4SJed Brown {
435684df9cb4SJed Brown   PetscErrorCode ierr;
435784df9cb4SJed Brown 
435884df9cb4SJed Brown   PetscFunctionBegin;
435984df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
436084df9cb4SJed Brown   PetscValidPointer(adapt,2);
436184df9cb4SJed Brown   if (!ts->adapt) {
4362ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
43633bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
43641c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
436584df9cb4SJed Brown   }
436684df9cb4SJed Brown   *adapt = ts->adapt;
436784df9cb4SJed Brown   PetscFunctionReturn(0);
436884df9cb4SJed Brown }
4369d6ebe24aSShri Abhyankar 
4370d6ebe24aSShri Abhyankar #undef __FUNCT__
43711c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
43721c3436cfSJed Brown /*@
43731c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
43741c3436cfSJed Brown 
43751c3436cfSJed Brown    Logically Collective
43761c3436cfSJed Brown 
43771c3436cfSJed Brown    Input Arguments:
43781c3436cfSJed Brown +  ts - time integration context
43791c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
43800298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
43811c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
43820298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
43831c3436cfSJed Brown 
4384a3cdaa26SBarry Smith    Options Database keys:
4385a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
4386a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
4387a3cdaa26SBarry Smith 
43881c3436cfSJed Brown    Level: beginner
43891c3436cfSJed Brown 
4390c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
43911c3436cfSJed Brown @*/
43921c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
43931c3436cfSJed Brown {
43941c3436cfSJed Brown   PetscErrorCode ierr;
43951c3436cfSJed Brown 
43961c3436cfSJed Brown   PetscFunctionBegin;
4397c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
43981c3436cfSJed Brown   if (vatol) {
43991c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
44001c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
4401bbd56ea5SKarl Rupp 
44021c3436cfSJed Brown     ts->vatol = vatol;
44031c3436cfSJed Brown   }
4404c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
44051c3436cfSJed Brown   if (vrtol) {
44061c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
44071c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
4408bbd56ea5SKarl Rupp 
44091c3436cfSJed Brown     ts->vrtol = vrtol;
44101c3436cfSJed Brown   }
44111c3436cfSJed Brown   PetscFunctionReturn(0);
44121c3436cfSJed Brown }
44131c3436cfSJed Brown 
44141c3436cfSJed Brown #undef __FUNCT__
4415c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
4416c5033834SJed Brown /*@
4417c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
4418c5033834SJed Brown 
4419c5033834SJed Brown    Logically Collective
4420c5033834SJed Brown 
4421c5033834SJed Brown    Input Arguments:
4422c5033834SJed Brown .  ts - time integration context
4423c5033834SJed Brown 
4424c5033834SJed Brown    Output Arguments:
44250298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
44260298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
44270298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
44280298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
4429c5033834SJed Brown 
4430c5033834SJed Brown    Level: beginner
4431c5033834SJed Brown 
4432c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
4433c5033834SJed Brown @*/
4434c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
4435c5033834SJed Brown {
4436c5033834SJed Brown   PetscFunctionBegin;
4437c5033834SJed Brown   if (atol)  *atol  = ts->atol;
4438c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
4439c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
4440c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
4441c5033834SJed Brown   PetscFunctionReturn(0);
4442c5033834SJed Brown }
4443c5033834SJed Brown 
4444c5033834SJed Brown #undef __FUNCT__
44451c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
44461c3436cfSJed Brown /*@
44471c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
44481c3436cfSJed Brown 
44491c3436cfSJed Brown    Collective on TS
44501c3436cfSJed Brown 
44511c3436cfSJed Brown    Input Arguments:
44521c3436cfSJed Brown +  ts - time stepping context
44531c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
44541c3436cfSJed Brown 
44551c3436cfSJed Brown    Output Arguments:
44561c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
44571c3436cfSJed Brown 
44581c3436cfSJed Brown    Level: developer
44591c3436cfSJed Brown 
44601c3436cfSJed Brown .seealso: TSSetTolerances()
44611c3436cfSJed Brown @*/
44621c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
44631c3436cfSJed Brown {
44641c3436cfSJed Brown   PetscErrorCode    ierr;
44651c3436cfSJed Brown   PetscInt          i,n,N;
44660910c330SBarry Smith   const PetscScalar *u,*y;
44670910c330SBarry Smith   Vec               U;
44681c3436cfSJed Brown   PetscReal         sum,gsum;
44691c3436cfSJed Brown 
44701c3436cfSJed Brown   PetscFunctionBegin;
44711c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44721c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
44731c3436cfSJed Brown   PetscValidPointer(norm,3);
44740910c330SBarry Smith   U = ts->vec_sol;
44750910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
4476ce94432eSBarry Smith   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
44771c3436cfSJed Brown 
44780910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
44790910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
44800910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
44811c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
44821c3436cfSJed Brown   sum  = 0.;
4483ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
4484ceefc113SJed Brown     const PetscScalar *atol,*rtol;
4485ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4486ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4487ceefc113SJed Brown     for (i=0; i<n; i++) {
44880910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
44890910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4490ceefc113SJed Brown     }
4491ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4492ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4493ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
4494ceefc113SJed Brown     const PetscScalar *atol;
4495ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4496ceefc113SJed Brown     for (i=0; i<n; i++) {
44970910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
44980910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4499ceefc113SJed Brown     }
4500ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4501ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
4502ceefc113SJed Brown     const PetscScalar *rtol;
4503ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4504ceefc113SJed Brown     for (i=0; i<n; i++) {
45050910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
45060910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4507ceefc113SJed Brown     }
4508ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4509ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
45101c3436cfSJed Brown     for (i=0; i<n; i++) {
45110910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
45120910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
45131c3436cfSJed Brown     }
4514ceefc113SJed Brown   }
45150910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
45161c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
45171c3436cfSJed Brown 
4518ce94432eSBarry Smith   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
45191c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
4520620fc8aaSSatish Balay   if (PetscIsInfOrNanReal(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
45211c3436cfSJed Brown   PetscFunctionReturn(0);
45221c3436cfSJed Brown }
45231c3436cfSJed Brown 
45241c3436cfSJed Brown #undef __FUNCT__
45258d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
45268d59e960SJed Brown /*@
45278d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
45288d59e960SJed Brown 
45298d59e960SJed Brown    Logically Collective on TS
45308d59e960SJed Brown 
45318d59e960SJed Brown    Input Arguments:
45328d59e960SJed Brown +  ts - time stepping context
45338d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
45348d59e960SJed Brown 
45358d59e960SJed Brown    Note:
45368d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
45378d59e960SJed Brown 
45388d59e960SJed Brown    Level: intermediate
45398d59e960SJed Brown 
45408d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
45418d59e960SJed Brown @*/
45428d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
45438d59e960SJed Brown {
45448d59e960SJed Brown   PetscFunctionBegin;
45458d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45468d59e960SJed Brown   ts->cfltime_local = cfltime;
45478d59e960SJed Brown   ts->cfltime       = -1.;
45488d59e960SJed Brown   PetscFunctionReturn(0);
45498d59e960SJed Brown }
45508d59e960SJed Brown 
45518d59e960SJed Brown #undef __FUNCT__
45528d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
45538d59e960SJed Brown /*@
45548d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
45558d59e960SJed Brown 
45568d59e960SJed Brown    Collective on TS
45578d59e960SJed Brown 
45588d59e960SJed Brown    Input Arguments:
45598d59e960SJed Brown .  ts - time stepping context
45608d59e960SJed Brown 
45618d59e960SJed Brown    Output Arguments:
45628d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
45638d59e960SJed Brown 
45648d59e960SJed Brown    Level: advanced
45658d59e960SJed Brown 
45668d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
45678d59e960SJed Brown @*/
45688d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
45698d59e960SJed Brown {
45708d59e960SJed Brown   PetscErrorCode ierr;
45718d59e960SJed Brown 
45728d59e960SJed Brown   PetscFunctionBegin;
45738d59e960SJed Brown   if (ts->cfltime < 0) {
4574ce94432eSBarry Smith     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
45758d59e960SJed Brown   }
45768d59e960SJed Brown   *cfltime = ts->cfltime;
45778d59e960SJed Brown   PetscFunctionReturn(0);
45788d59e960SJed Brown }
45798d59e960SJed Brown 
45808d59e960SJed Brown #undef __FUNCT__
4581d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
4582d6ebe24aSShri Abhyankar /*@
4583d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
4584d6ebe24aSShri Abhyankar 
4585d6ebe24aSShri Abhyankar    Input Parameters:
4586d6ebe24aSShri Abhyankar .  ts   - the TS context.
4587d6ebe24aSShri Abhyankar .  xl   - lower bound.
4588d6ebe24aSShri Abhyankar .  xu   - upper bound.
4589d6ebe24aSShri Abhyankar 
4590d6ebe24aSShri Abhyankar    Notes:
4591d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
4592e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
4593d6ebe24aSShri Abhyankar 
45942bd2b0e6SSatish Balay    Level: advanced
45952bd2b0e6SSatish Balay 
4596d6ebe24aSShri Abhyankar @*/
4597d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
4598d6ebe24aSShri Abhyankar {
4599d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
4600d6ebe24aSShri Abhyankar   SNES           snes;
4601d6ebe24aSShri Abhyankar 
4602d6ebe24aSShri Abhyankar   PetscFunctionBegin;
4603d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4604d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
4605d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
4606d6ebe24aSShri Abhyankar }
4607d6ebe24aSShri Abhyankar 
4608325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
4609c6db04a5SJed Brown #include <mex.h>
4610325fc9f4SBarry Smith 
4611325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
4612325fc9f4SBarry Smith 
4613325fc9f4SBarry Smith #undef __FUNCT__
4614325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
4615325fc9f4SBarry Smith /*
4616325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
4617325fc9f4SBarry Smith                          TSSetFunctionMatlab().
4618325fc9f4SBarry Smith 
4619325fc9f4SBarry Smith    Collective on TS
4620325fc9f4SBarry Smith 
4621325fc9f4SBarry Smith    Input Parameters:
4622325fc9f4SBarry Smith +  snes - the TS context
46230910c330SBarry Smith -  u - input vector
4624325fc9f4SBarry Smith 
4625325fc9f4SBarry Smith    Output Parameter:
4626325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
4627325fc9f4SBarry Smith 
4628325fc9f4SBarry Smith    Notes:
4629325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
4630325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
4631325fc9f4SBarry Smith    themselves.
4632325fc9f4SBarry Smith 
4633325fc9f4SBarry Smith    Level: developer
4634325fc9f4SBarry Smith 
4635325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4636325fc9f4SBarry Smith 
4637325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4638325fc9f4SBarry Smith */
46390910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
4640325fc9f4SBarry Smith {
4641325fc9f4SBarry Smith   PetscErrorCode  ierr;
4642325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4643325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
4644325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
4645325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
4646325fc9f4SBarry Smith 
4647325fc9f4SBarry Smith   PetscFunctionBegin;
4648325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
46490910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
46500910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
4651325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
46520910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
4653325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
4654325fc9f4SBarry Smith 
4655325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
46560910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
46570910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
46580910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
4659bbd56ea5SKarl Rupp 
4660325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4661325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
4662325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4663325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4664325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
4665325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
4666325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
4667325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
4668325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4669325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4670325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4671325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4672325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4673325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4674325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4675325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4676325fc9f4SBarry Smith   PetscFunctionReturn(0);
4677325fc9f4SBarry Smith }
4678325fc9f4SBarry Smith 
4679325fc9f4SBarry Smith 
4680325fc9f4SBarry Smith #undef __FUNCT__
4681325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
4682325fc9f4SBarry Smith /*
4683325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
4684325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
4685e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
4686325fc9f4SBarry Smith 
4687325fc9f4SBarry Smith    Logically Collective on TS
4688325fc9f4SBarry Smith 
4689325fc9f4SBarry Smith    Input Parameters:
4690325fc9f4SBarry Smith +  ts - the TS context
4691325fc9f4SBarry Smith -  func - function evaluation routine
4692325fc9f4SBarry Smith 
4693325fc9f4SBarry Smith    Calling sequence of func:
46940910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
4695325fc9f4SBarry Smith 
4696325fc9f4SBarry Smith    Level: beginner
4697325fc9f4SBarry Smith 
4698325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4699325fc9f4SBarry Smith 
4700325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4701325fc9f4SBarry Smith */
4702cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
4703325fc9f4SBarry Smith {
4704325fc9f4SBarry Smith   PetscErrorCode  ierr;
4705325fc9f4SBarry Smith   TSMatlabContext *sctx;
4706325fc9f4SBarry Smith 
4707325fc9f4SBarry Smith   PetscFunctionBegin;
4708325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4709325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4710325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4711325fc9f4SBarry Smith   /*
4712325fc9f4SBarry Smith      This should work, but it doesn't
4713325fc9f4SBarry Smith   sctx->ctx = ctx;
4714325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4715325fc9f4SBarry Smith   */
4716325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4717bbd56ea5SKarl Rupp 
47180298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
4719325fc9f4SBarry Smith   PetscFunctionReturn(0);
4720325fc9f4SBarry Smith }
4721325fc9f4SBarry Smith 
4722325fc9f4SBarry Smith #undef __FUNCT__
4723325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
4724325fc9f4SBarry Smith /*
4725325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
4726325fc9f4SBarry Smith                          TSSetJacobianMatlab().
4727325fc9f4SBarry Smith 
4728325fc9f4SBarry Smith    Collective on TS
4729325fc9f4SBarry Smith 
4730325fc9f4SBarry Smith    Input Parameters:
4731cdcf91faSSean Farley +  ts - the TS context
47320910c330SBarry Smith .  u - input vector
4733325fc9f4SBarry Smith .  A, B - the matrices
4734325fc9f4SBarry Smith -  ctx - user context
4735325fc9f4SBarry Smith 
4736325fc9f4SBarry Smith    Level: developer
4737325fc9f4SBarry Smith 
4738325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
4739325fc9f4SBarry Smith 
4740325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4741325fc9f4SBarry Smith @*/
4742f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
4743325fc9f4SBarry Smith {
4744325fc9f4SBarry Smith   PetscErrorCode  ierr;
4745325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4746325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
4747325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
4748325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
4749325fc9f4SBarry Smith 
4750325fc9f4SBarry Smith   PetscFunctionBegin;
4751cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
47520910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
4753325fc9f4SBarry Smith 
47540910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
4755325fc9f4SBarry Smith 
4756cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
47570910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
47580910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
47590910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
47600910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
4761bbd56ea5SKarl Rupp 
4762325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4763325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
4764325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
4765325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
4766325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
4767325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
4768325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
4769325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
4770325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
4771325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
4772325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4773325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
4774325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
4775325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
4776325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
4777325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
4778325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
4779325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
4780325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
4781325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
4782325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
4783325fc9f4SBarry Smith   PetscFunctionReturn(0);
4784325fc9f4SBarry Smith }
4785325fc9f4SBarry Smith 
4786325fc9f4SBarry Smith 
4787325fc9f4SBarry Smith #undef __FUNCT__
4788325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
4789325fc9f4SBarry Smith /*
4790325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
4791e3c5b3baSBarry 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
4792325fc9f4SBarry Smith 
4793325fc9f4SBarry Smith    Logically Collective on TS
4794325fc9f4SBarry Smith 
4795325fc9f4SBarry Smith    Input Parameters:
4796cdcf91faSSean Farley +  ts - the TS context
4797325fc9f4SBarry Smith .  A,B - Jacobian matrices
4798325fc9f4SBarry Smith .  func - function evaluation routine
4799325fc9f4SBarry Smith -  ctx - user context
4800325fc9f4SBarry Smith 
4801325fc9f4SBarry Smith    Calling sequence of func:
48020910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
4803325fc9f4SBarry Smith 
4804325fc9f4SBarry Smith 
4805325fc9f4SBarry Smith    Level: developer
4806325fc9f4SBarry Smith 
4807325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
4808325fc9f4SBarry Smith 
4809325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4810325fc9f4SBarry Smith */
4811cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
4812325fc9f4SBarry Smith {
4813325fc9f4SBarry Smith   PetscErrorCode  ierr;
4814325fc9f4SBarry Smith   TSMatlabContext *sctx;
4815325fc9f4SBarry Smith 
4816325fc9f4SBarry Smith   PetscFunctionBegin;
4817325fc9f4SBarry Smith   /* currently sctx is memory bleed */
4818325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4819325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4820325fc9f4SBarry Smith   /*
4821325fc9f4SBarry Smith      This should work, but it doesn't
4822325fc9f4SBarry Smith   sctx->ctx = ctx;
4823325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4824325fc9f4SBarry Smith   */
4825325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4826bbd56ea5SKarl Rupp 
4827cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
4828325fc9f4SBarry Smith   PetscFunctionReturn(0);
4829325fc9f4SBarry Smith }
4830325fc9f4SBarry Smith 
4831b5b1a830SBarry Smith #undef __FUNCT__
4832b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
4833b5b1a830SBarry Smith /*
4834b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
4835b5b1a830SBarry Smith 
4836b5b1a830SBarry Smith    Collective on TS
4837b5b1a830SBarry Smith 
4838b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
4839b5b1a830SBarry Smith @*/
48400910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
4841b5b1a830SBarry Smith {
4842b5b1a830SBarry Smith   PetscErrorCode  ierr;
4843b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
4844a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
4845b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
4846b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
4847b5b1a830SBarry Smith 
4848b5b1a830SBarry Smith   PetscFunctionBegin;
4849cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
48500910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4851b5b1a830SBarry Smith 
4852cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
48530910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
4854bbd56ea5SKarl Rupp 
4855b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
4856b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
4857b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
4858b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
4859b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
4860b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
4861b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
4862b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
4863b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
4864b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
4865b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
4866b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
4867b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
4868b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
4869b5b1a830SBarry Smith   PetscFunctionReturn(0);
4870b5b1a830SBarry Smith }
4871b5b1a830SBarry Smith 
4872b5b1a830SBarry Smith 
4873b5b1a830SBarry Smith #undef __FUNCT__
4874b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
4875b5b1a830SBarry Smith /*
4876b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
4877b5b1a830SBarry Smith 
4878b5b1a830SBarry Smith    Level: developer
4879b5b1a830SBarry Smith 
4880b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
4881b5b1a830SBarry Smith 
4882b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
4883b5b1a830SBarry Smith */
4884cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
4885b5b1a830SBarry Smith {
4886b5b1a830SBarry Smith   PetscErrorCode  ierr;
4887b5b1a830SBarry Smith   TSMatlabContext *sctx;
4888b5b1a830SBarry Smith 
4889b5b1a830SBarry Smith   PetscFunctionBegin;
4890b5b1a830SBarry Smith   /* currently sctx is memory bleed */
4891b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
4892b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
4893b5b1a830SBarry Smith   /*
4894b5b1a830SBarry Smith      This should work, but it doesn't
4895b5b1a830SBarry Smith   sctx->ctx = ctx;
4896b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
4897b5b1a830SBarry Smith   */
4898b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
4899bbd56ea5SKarl Rupp 
49000298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
4901b5b1a830SBarry Smith   PetscFunctionReturn(0);
4902b5b1a830SBarry Smith }
4903325fc9f4SBarry Smith #endif
4904b3603a34SBarry Smith 
49050b039ecaSBarry Smith 
49060b039ecaSBarry Smith 
4907b3603a34SBarry Smith #undef __FUNCT__
49084f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
4909b3603a34SBarry Smith /*@C
49104f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
4911b3603a34SBarry Smith        in a time based line graph
4912b3603a34SBarry Smith 
4913b3603a34SBarry Smith    Collective on TS
4914b3603a34SBarry Smith 
4915b3603a34SBarry Smith    Input Parameters:
4916b3603a34SBarry Smith +  ts - the TS context
4917b3603a34SBarry Smith .  step - current time-step
4918b3603a34SBarry Smith .  ptime - current time
4919b3603a34SBarry Smith -  lg - a line graph object
4920b3603a34SBarry Smith 
4921b3603a34SBarry Smith    Level: intermediate
4922b3603a34SBarry Smith 
49230b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
4924b3603a34SBarry Smith 
4925b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
4926b3603a34SBarry Smith 
4927b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4928b3603a34SBarry Smith @*/
49290910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4930b3603a34SBarry Smith {
4931b3603a34SBarry Smith   PetscErrorCode    ierr;
49320b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4933b3603a34SBarry Smith   const PetscScalar *yy;
493458ff32f7SBarry Smith   PetscInt          dim;
4935b3603a34SBarry Smith 
4936b3603a34SBarry Smith   PetscFunctionBegin;
493758ff32f7SBarry Smith   if (!step) {
4938a9f9c1f6SBarry Smith     PetscDrawAxis axis;
4939a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
4940a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
49410910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
49420b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
49430b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
494458ff32f7SBarry Smith   }
49450910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
4946e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
4947e3efe391SJed Brown   {
4948e3efe391SJed Brown     PetscReal *yreal;
4949e3efe391SJed Brown     PetscInt  i,n;
49500910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
4951785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
4952e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
49530b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
4954e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
4955e3efe391SJed Brown   }
4956e3efe391SJed Brown #else
49570b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
4958e3efe391SJed Brown #endif
49590910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
4960b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
49610b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
49623923b477SBarry Smith   }
4963b3603a34SBarry Smith   PetscFunctionReturn(0);
4964b3603a34SBarry Smith }
4965b3603a34SBarry Smith 
4966b3603a34SBarry Smith #undef __FUNCT__
49674f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
4968ef20d060SBarry Smith /*@C
49694f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
4970ef20d060SBarry Smith        in a time based line graph
4971ef20d060SBarry Smith 
4972ef20d060SBarry Smith    Collective on TS
4973ef20d060SBarry Smith 
4974ef20d060SBarry Smith    Input Parameters:
4975ef20d060SBarry Smith +  ts - the TS context
4976ef20d060SBarry Smith .  step - current time-step
4977ef20d060SBarry Smith .  ptime - current time
4978ef20d060SBarry Smith -  lg - a line graph object
4979ef20d060SBarry Smith 
4980ef20d060SBarry Smith    Level: intermediate
4981ef20d060SBarry Smith 
4982abd5a294SJed Brown    Notes:
4983abd5a294SJed Brown    Only for sequential solves.
4984abd5a294SJed Brown 
4985abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
4986abd5a294SJed Brown 
4987abd5a294SJed Brown    Options Database Keys:
49884f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
4989ef20d060SBarry Smith 
4990ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
4991ef20d060SBarry Smith 
4992abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
4993ef20d060SBarry Smith @*/
49940910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
4995ef20d060SBarry Smith {
4996ef20d060SBarry Smith   PetscErrorCode    ierr;
49970b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
4998ef20d060SBarry Smith   const PetscScalar *yy;
4999ef20d060SBarry Smith   Vec               y;
5000a9f9c1f6SBarry Smith   PetscInt          dim;
5001ef20d060SBarry Smith 
5002ef20d060SBarry Smith   PetscFunctionBegin;
5003a9f9c1f6SBarry Smith   if (!step) {
5004a9f9c1f6SBarry Smith     PetscDrawAxis axis;
5005a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
50061ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
50070910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
5008a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
5009a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5010a9f9c1f6SBarry Smith   }
50110910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
5012ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
50130910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
5014ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
5015e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
5016e3efe391SJed Brown   {
5017e3efe391SJed Brown     PetscReal *yreal;
5018e3efe391SJed Brown     PetscInt  i,n;
5019e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
5020785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
5021e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
50220b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
5023e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
5024e3efe391SJed Brown   }
5025e3efe391SJed Brown #else
50260b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
5027e3efe391SJed Brown #endif
5028ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
5029ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
5030b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
50310b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
50323923b477SBarry Smith   }
5033ef20d060SBarry Smith   PetscFunctionReturn(0);
5034ef20d060SBarry Smith }
5035ef20d060SBarry Smith 
5036201da799SBarry Smith #undef __FUNCT__
5037201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
5038201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
5039201da799SBarry Smith {
5040201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
5041201da799SBarry Smith   PetscReal      x   = ptime,y;
5042201da799SBarry Smith   PetscErrorCode ierr;
5043201da799SBarry Smith   PetscInt       its;
5044201da799SBarry Smith 
5045201da799SBarry Smith   PetscFunctionBegin;
5046201da799SBarry Smith   if (!n) {
5047201da799SBarry Smith     PetscDrawAxis axis;
5048bbd56ea5SKarl Rupp 
5049201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5050201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
5051201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5052bbd56ea5SKarl Rupp 
5053201da799SBarry Smith     ctx->snes_its = 0;
5054201da799SBarry Smith   }
5055201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
5056201da799SBarry Smith   y    = its - ctx->snes_its;
5057201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
50583fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
5059201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
5060201da799SBarry Smith   }
5061201da799SBarry Smith   ctx->snes_its = its;
5062201da799SBarry Smith   PetscFunctionReturn(0);
5063201da799SBarry Smith }
5064201da799SBarry Smith 
5065201da799SBarry Smith #undef __FUNCT__
5066201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
5067201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
5068201da799SBarry Smith {
5069201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
5070201da799SBarry Smith   PetscReal      x   = ptime,y;
5071201da799SBarry Smith   PetscErrorCode ierr;
5072201da799SBarry Smith   PetscInt       its;
5073201da799SBarry Smith 
5074201da799SBarry Smith   PetscFunctionBegin;
5075201da799SBarry Smith   if (!n) {
5076201da799SBarry Smith     PetscDrawAxis axis;
5077bbd56ea5SKarl Rupp 
5078201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5079201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
5080201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5081bbd56ea5SKarl Rupp 
5082201da799SBarry Smith     ctx->ksp_its = 0;
5083201da799SBarry Smith   }
5084201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
5085201da799SBarry Smith   y    = its - ctx->ksp_its;
5086201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
508799fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
5088201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
5089201da799SBarry Smith   }
5090201da799SBarry Smith   ctx->ksp_its = its;
5091201da799SBarry Smith   PetscFunctionReturn(0);
5092201da799SBarry Smith }
5093f9c1d6abSBarry Smith 
5094f9c1d6abSBarry Smith #undef __FUNCT__
5095f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
5096f9c1d6abSBarry Smith /*@
5097f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
5098f9c1d6abSBarry Smith 
5099f9c1d6abSBarry Smith    Collective on TS and Vec
5100f9c1d6abSBarry Smith 
5101f9c1d6abSBarry Smith    Input Parameters:
5102f9c1d6abSBarry Smith +  ts - the TS context
5103f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
5104f9c1d6abSBarry Smith 
5105f9c1d6abSBarry Smith    Output Parameters:
5106f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
5107f9c1d6abSBarry Smith 
5108f9c1d6abSBarry Smith    Level: developer
5109f9c1d6abSBarry Smith 
5110f9c1d6abSBarry Smith .keywords: TS, compute
5111f9c1d6abSBarry Smith 
5112f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
5113f9c1d6abSBarry Smith @*/
5114f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
5115f9c1d6abSBarry Smith {
5116f9c1d6abSBarry Smith   PetscErrorCode ierr;
5117f9c1d6abSBarry Smith 
5118f9c1d6abSBarry Smith   PetscFunctionBegin;
5119f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5120ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
5121f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
5122f9c1d6abSBarry Smith   PetscFunctionReturn(0);
5123f9c1d6abSBarry Smith }
512424655328SShri 
512524655328SShri #undef __FUNCT__
512624655328SShri #define __FUNCT__ "TSRollBack"
512724655328SShri /*@
512824655328SShri    TSRollBack - Rolls back one time step
512924655328SShri 
513024655328SShri    Collective on TS
513124655328SShri 
513224655328SShri    Input Parameter:
513324655328SShri .  ts - the TS context obtained from TSCreate()
513424655328SShri 
513524655328SShri    Level: advanced
513624655328SShri 
513724655328SShri .keywords: TS, timestep, rollback
513824655328SShri 
513924655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
514024655328SShri @*/
514124655328SShri PetscErrorCode  TSRollBack(TS ts)
514224655328SShri {
514324655328SShri   PetscErrorCode ierr;
514424655328SShri 
514524655328SShri   PetscFunctionBegin;
514624655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
514724655328SShri 
514824655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
514924655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
515024655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
515124655328SShri   ts->ptime = ts->ptime_prev;
515224655328SShri   PetscFunctionReturn(0);
515324655328SShri }
5154aeb4809dSShri Abhyankar 
5155ff22ae23SHong Zhang #undef __FUNCT__
5156ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages"
5157ff22ae23SHong Zhang /*@
5158ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
5159ff22ae23SHong Zhang 
5160ff22ae23SHong Zhang    Input Parameter:
5161ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
5162ff22ae23SHong Zhang 
5163ff22ae23SHong Zhang    Level: advanced
5164ff22ae23SHong Zhang 
5165ff22ae23SHong Zhang .keywords: TS, getstages
5166ff22ae23SHong Zhang 
5167ff22ae23SHong Zhang .seealso: TSCreate()
5168ff22ae23SHong Zhang @*/
5169ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns, Vec **Y)
5170ff22ae23SHong Zhang {
5171ff22ae23SHong Zhang   PetscErrorCode ierr;
5172ff22ae23SHong Zhang 
5173ff22ae23SHong Zhang   PetscFunctionBegin;
5174ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
5175ff22ae23SHong Zhang   PetscValidPointer(ns,2);
5176ff22ae23SHong Zhang 
5177ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
5178ff22ae23SHong Zhang   else {
5179ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
5180ff22ae23SHong Zhang   }
5181ff22ae23SHong Zhang   PetscFunctionReturn(0);
5182ff22ae23SHong Zhang }
5183ff22ae23SHong Zhang 
5184ff22ae23SHong Zhang 
5185ff22ae23SHong Zhang 
5186