xref: /petsc/src/ts/interface/ts.c (revision 87f4e208db4ba8acce282dbb9e30381b0b02445e)
1af0996ceSBarry Smith #include <petsc/private/tsimpl.h>        /*I "petscts.h"  I*/
2496e6a7aSJed Brown #include <petscdmshell.h>
31e25c274SJed Brown #include <petscdmda.h>
42d5ee99bSBarry Smith #include <petscviewer.h>
52d5ee99bSBarry Smith #include <petscdraw.h>
6d763cef2SBarry Smith 
7d5ba7fb7SMatthew Knepley /* Logging support */
8d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
9a05bf03eSHong Zhang PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
10d405a339SMatthew Knepley 
11feed9e9dSBarry Smith const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0};
1249354f04SShri Abhyankar 
13fde5950dSBarry Smith /*@C
14fde5950dSBarry Smith    TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
15fde5950dSBarry Smith 
16fde5950dSBarry Smith    Collective on TS
17fde5950dSBarry Smith 
18fde5950dSBarry Smith    Input Parameters:
19fde5950dSBarry Smith +  ts - TS object you wish to monitor
20fde5950dSBarry Smith .  name - the monitor type one is seeking
21fde5950dSBarry Smith .  help - message indicating what monitoring is done
22fde5950dSBarry Smith .  manual - manual page for the monitor
23fde5950dSBarry Smith .  monitor - the monitor function
24fde5950dSBarry Smith -  monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the TS or PetscViewer objects
25fde5950dSBarry Smith 
26fde5950dSBarry Smith    Level: developer
27fde5950dSBarry Smith 
28fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
29fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
30fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
31fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
32fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
33fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
34fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
35fde5950dSBarry Smith @*/
36721cd6eeSBarry Smith PetscErrorCode  TSMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(TS,PetscViewerAndFormat*))
37fde5950dSBarry Smith {
38fde5950dSBarry Smith   PetscErrorCode    ierr;
39fde5950dSBarry Smith   PetscViewer       viewer;
40fde5950dSBarry Smith   PetscViewerFormat format;
41fde5950dSBarry Smith   PetscBool         flg;
42fde5950dSBarry Smith 
43fde5950dSBarry Smith   PetscFunctionBegin;
44fde5950dSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
45fde5950dSBarry Smith   if (flg) {
46721cd6eeSBarry Smith     PetscViewerAndFormat *vf;
47721cd6eeSBarry Smith     ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr);
48721cd6eeSBarry Smith     ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr);
49fde5950dSBarry Smith     if (monitorsetup) {
50721cd6eeSBarry Smith       ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr);
51fde5950dSBarry Smith     }
52721cd6eeSBarry Smith     ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
53fde5950dSBarry Smith   }
54fde5950dSBarry Smith   PetscFunctionReturn(0);
55fde5950dSBarry Smith }
56fde5950dSBarry Smith 
572ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type)
582ffb9264SLisandro Dalcin {
592ffb9264SLisandro Dalcin   PetscErrorCode ierr;
602ffb9264SLisandro Dalcin 
612ffb9264SLisandro Dalcin   PetscFunctionBegin;
62b92453a8SLisandro Dalcin   PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1);
63b92453a8SLisandro Dalcin   PetscValidCharPointer(default_type,2);
642ffb9264SLisandro Dalcin   if (!((PetscObject)adapt)->type_name) {
652ffb9264SLisandro Dalcin     ierr = TSAdaptSetType(adapt,default_type);CHKERRQ(ierr);
662ffb9264SLisandro Dalcin   }
672ffb9264SLisandro Dalcin   PetscFunctionReturn(0);
682ffb9264SLisandro Dalcin }
692ffb9264SLisandro Dalcin 
70bdad233fSMatthew Knepley /*@
71bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
72bdad233fSMatthew Knepley 
73bdad233fSMatthew Knepley    Collective on TS
74bdad233fSMatthew Knepley 
75bdad233fSMatthew Knepley    Input Parameter:
76bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
77bdad233fSMatthew Knepley 
78bdad233fSMatthew Knepley    Options Database Keys:
796bd7aeb5SHong Zhang +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE, TSBSI
80ef222394SBarry Smith .  -ts_save_trajectory - checkpoint the solution at each time-step
81ef85077eSLisandro Dalcin .  -ts_max_time <time> - maximum time to compute to
82ef85077eSLisandro Dalcin .  -ts_max_steps <steps> - maximum number of time-steps to take
83ef85077eSLisandro Dalcin .  -ts_init_time <time> - initial time to start computation
84ef85077eSLisandro Dalcin .  -ts_final_time <time> - final time to compute to
853e4cdcaaSBarry Smith .  -ts_dt <dt> - initial time step
86a3cdaa26SBarry 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
87a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
88a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
89a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
90a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
91a3cdaa26SBarry Smith .  -ts_atol <atol> Absolute tolerance for local truncation error
92f3b1f45cSBarry Smith .  -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function
93f3b1f45cSBarry Smith .  -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - test the Jacobian at each iteration against finite difference with RHS function
94ef222394SBarry Smith .  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
95847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
96bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
97de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
98de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
997cf37e64SBarry Smith .  -ts_monitor_error - Monitors norm of error
1006934998bSLisandro Dalcin .  -ts_monitor_lg_timestep - Monitor timestep size graphically
1018b668821SLisandro Dalcin .  -ts_monitor_lg_timestep_log - Monitor log timestep size graphically
102de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
103de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
104de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
105de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
1063e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
1073e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
108fde5950dSBarry Smith .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
109e4160dc7SJulian Andrej .  -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu)
110110eb670SHong Zhang .  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
11153ea634cSHong Zhang 
11291b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
113bdad233fSMatthew Knepley 
114bdad233fSMatthew Knepley    Level: beginner
115bdad233fSMatthew Knepley 
116bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
117bdad233fSMatthew Knepley 
118a313700dSBarry Smith .seealso: TSGetType()
119bdad233fSMatthew Knepley @*/
1207087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
121bdad233fSMatthew Knepley {
122bc952696SBarry Smith   PetscBool              opt,flg,tflg;
123dfbe8321SBarry Smith   PetscErrorCode         ierr;
124eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
12531748224SBarry Smith   PetscReal              time_step;
12649354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
127d1212d36SBarry Smith   char                   dir[16];
128cd11d68dSLisandro Dalcin   TSIFunction            ifun;
1296991f827SBarry Smith   const char             *defaultType;
1306991f827SBarry Smith   char                   typeName[256];
131bdad233fSMatthew Knepley 
132bdad233fSMatthew Knepley   PetscFunctionBegin;
1330700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1346991f827SBarry Smith 
1356991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
136cd11d68dSLisandro Dalcin   ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
137cd11d68dSLisandro Dalcin 
138cd11d68dSLisandro Dalcin   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
139cd11d68dSLisandro Dalcin   if (((PetscObject)ts)->type_name)
140cd11d68dSLisandro Dalcin     defaultType = ((PetscObject)ts)->type_name;
141cd11d68dSLisandro Dalcin   else
142cd11d68dSLisandro Dalcin     defaultType = ifun ? TSBEULER : TSEULER;
1436991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr);
1446991f827SBarry Smith   if (opt) {
1456991f827SBarry Smith     ierr = TSSetType(ts,typeName);CHKERRQ(ierr);
1466991f827SBarry Smith   } else {
1476991f827SBarry Smith     ierr = TSSetType(ts,defaultType);CHKERRQ(ierr);
1486991f827SBarry Smith   }
149bdad233fSMatthew Knepley 
150bdad233fSMatthew Knepley   /* Handle generic TS options */
151ef85077eSLisandro Dalcin   ierr = PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
15219eac22cSLisandro Dalcin   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1530298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
154ef85077eSLisandro Dalcin   ierr = PetscOptionsReal("-ts_final_time","Final time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
15531748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
156cd11d68dSLisandro Dalcin   if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);}
15749354f04SShri 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);
15849354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1590298fd71SBarry 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);
1600298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1610298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1620298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1630298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
164bdad233fSMatthew Knepley 
165f3b1f45cSBarry Smith   ierr = PetscOptionsBool("-ts_rhs_jacobian_test_mult","Test the RHS Jacobian for consistency with RHS at each solve ","None",ts->testjacobian,&ts->testjacobian,NULL);CHKERRQ(ierr);
166f3b1f45cSBarry Smith   ierr = PetscOptionsBool("-ts_rhs_jacobian_test_mult_transpose","Test the RHS Jacobian transpose for consistency with RHS at each solve ","None",ts->testjacobiantranspose,&ts->testjacobiantranspose,NULL);CHKERRQ(ierr);
16756f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
16856f85f32SBarry Smith   {
16956f85f32SBarry Smith   PetscBool set;
17056f85f32SBarry Smith   flg  = PETSC_FALSE;
17156f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
17256f85f32SBarry Smith   if (set) {
17356f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
17456f85f32SBarry Smith   }
17556f85f32SBarry Smith   }
17656f85f32SBarry Smith #endif
17756f85f32SBarry Smith 
178bdad233fSMatthew Knepley   /* Monitor options */
179cd11d68dSLisandro Dalcin   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr);
180fde5950dSBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr);
181fde5950dSBarry Smith 
1825180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1835180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1845180491cSLisandro Dalcin 
1854f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
186b3603a34SBarry Smith   if (opt) {
1870b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1883923b477SBarry Smith     PetscInt       howoften = 1;
189b3603a34SBarry Smith 
1900298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
1916ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
1924f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
193bdad233fSMatthew Knepley   }
1946ba87a44SLisandro Dalcin 
1954f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
196ef20d060SBarry Smith   if (opt) {
1970b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1983923b477SBarry Smith     PetscInt       howoften = 1;
199ef20d060SBarry Smith 
2000298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
2016ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2024f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
203ef20d060SBarry Smith   }
204edbaebb3SBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_error","View the error at each timestep","TSMonitorError",TSMonitorError,NULL);CHKERRQ(ierr);
2057cf37e64SBarry Smith 
2066934998bSLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2076934998bSLisandro Dalcin   if (opt) {
2086934998bSLisandro Dalcin     TSMonitorLGCtx ctx;
2096934998bSLisandro Dalcin     PetscInt       howoften = 1;
2106934998bSLisandro Dalcin 
2116934998bSLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2126ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2136934998bSLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2146934998bSLisandro Dalcin   }
2158b668821SLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2168b668821SLisandro Dalcin   if (opt) {
2178b668821SLisandro Dalcin     TSMonitorLGCtx ctx;
2188b668821SLisandro Dalcin     PetscInt       howoften = 1;
2198b668821SLisandro Dalcin 
2208b668821SLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2218b668821SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2228b668821SLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2238b668821SLisandro Dalcin     ctx->semilogy = PETSC_TRUE;
2248b668821SLisandro Dalcin   }
2258b668821SLisandro Dalcin 
226201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
227201da799SBarry Smith   if (opt) {
228201da799SBarry Smith     TSMonitorLGCtx ctx;
229201da799SBarry Smith     PetscInt       howoften = 1;
230201da799SBarry Smith 
2310298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2326ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
233201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
234201da799SBarry Smith   }
235201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
236201da799SBarry Smith   if (opt) {
237201da799SBarry Smith     TSMonitorLGCtx ctx;
238201da799SBarry Smith     PetscInt       howoften = 1;
239201da799SBarry Smith 
2400298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2416ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
242201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
243201da799SBarry Smith   }
2448189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
2458189c53fSBarry Smith   if (opt) {
2468189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2478189c53fSBarry Smith     PetscInt          howoften = 1;
2488189c53fSBarry Smith 
2490298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
2506934998bSLisandro Dalcin     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2518189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2528189c53fSBarry Smith   }
253ef20d060SBarry Smith   opt  = PETSC_FALSE;
2540dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
255a7cc72afSBarry Smith   if (opt) {
25683a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
25783a4ac43SBarry Smith     PetscInt         howoften = 1;
258a80ad3e0SBarry Smith 
2590298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
2600ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
26183a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
262bdad233fSMatthew Knepley   }
263fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2642d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2652d5ee99bSBarry Smith   if (opt) {
2662d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2672d5ee99bSBarry Smith     PetscReal        bounds[4];
2682d5ee99bSBarry Smith     PetscInt         n = 4;
2692d5ee99bSBarry Smith     PetscDraw        draw;
2706934998bSLisandro Dalcin     PetscDrawAxis    axis;
2712d5ee99bSBarry Smith 
2722d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
2732d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
2746934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr);
2752d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
2766934998bSLisandro Dalcin     ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr);
2776934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
2786934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
2792d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2802d5ee99bSBarry Smith   }
2812d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2820dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
2833a471f94SBarry Smith   if (opt) {
28483a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
28583a4ac43SBarry Smith     PetscInt         howoften = 1;
2863a471f94SBarry Smith 
2870298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
2880ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
28983a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2903a471f94SBarry Smith   }
2910ed3bfb6SBarry Smith   opt  = PETSC_FALSE;
2920ed3bfb6SBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt);CHKERRQ(ierr);
2930ed3bfb6SBarry Smith   if (opt) {
2940ed3bfb6SBarry Smith     TSMonitorDrawCtx ctx;
2950ed3bfb6SBarry Smith     PetscInt         howoften = 1;
2960ed3bfb6SBarry Smith 
2970ed3bfb6SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL);CHKERRQ(ierr);
2980ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2990ed3bfb6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3000ed3bfb6SBarry Smith   }
301fde5950dSBarry Smith 
302ed81e22dSJed Brown   opt  = PETSC_FALSE;
30391b97e58SBarry 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);
304ed81e22dSJed Brown   if (flg) {
305ed81e22dSJed Brown     const char *ptr,*ptr2;
306ed81e22dSJed Brown     char       *filetemplate;
307ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
308ed81e22dSJed Brown     /* Do some cursory validation of the input. */
309ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
310ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
311ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
312ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
313ce94432eSBarry 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");
314ed81e22dSJed Brown       if (ptr2) break;
315ed81e22dSJed Brown     }
316ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
317ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
318ed81e22dSJed Brown   }
319bdad233fSMatthew Knepley 
320d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
321d1212d36SBarry Smith   if (flg) {
322d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
323d1212d36SBarry Smith     int                  ray = 0;
324d1212d36SBarry Smith     DMDADirection        ddir;
325d1212d36SBarry Smith     DM                   da;
326d1212d36SBarry Smith     PetscMPIInt          rank;
327d1212d36SBarry Smith 
328ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
329d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
330d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
331ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
332d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
333d1212d36SBarry Smith 
334d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
335b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
336d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
337d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
338ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
339d1212d36SBarry Smith     if (!rank) {
340d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
341d1212d36SBarry Smith     }
34251b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
343d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
344d1212d36SBarry Smith   }
34551b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
34651b4a12fSMatthew G. Knepley   if (flg) {
34751b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
34851b4a12fSMatthew G. Knepley     int                 ray = 0;
34951b4a12fSMatthew G. Knepley     DMDADirection       ddir;
35051b4a12fSMatthew G. Knepley     DM                  da;
35151b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
352d1212d36SBarry Smith 
35351b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
35451b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
35551b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
35651b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
35751b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3581c3436cfSJed Brown 
35951b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
360b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
36151b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
36251b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
36351b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
36451b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
36551b4a12fSMatthew G. Knepley   }
366a7a1495cSBarry Smith 
367b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
368b3d3934dSBarry Smith   if (opt) {
369b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
370b3d3934dSBarry Smith 
371b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
372b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
373b3d3934dSBarry Smith   }
374b3d3934dSBarry Smith 
375847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
376847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
377847ff0e1SMatthew G. Knepley   if (flg) {
378847ff0e1SMatthew G. Knepley     DM   dm;
379847ff0e1SMatthew G. Knepley     DMTS tdm;
380847ff0e1SMatthew G. Knepley 
381847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
382847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
383847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
384847ff0e1SMatthew G. Knepley     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr);
385847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
386847ff0e1SMatthew G. Knepley   }
387847ff0e1SMatthew G. Knepley 
388d763cef2SBarry Smith   /* Handle specific TS options */
389d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
3906991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
391d763cef2SBarry Smith   }
392fbc52257SHong Zhang 
393a7bdc993SLisandro Dalcin   /* Handle TSAdapt options */
394a7bdc993SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
395a7bdc993SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
396a7bdc993SLisandro Dalcin   ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr);
397a7bdc993SLisandro Dalcin 
39868bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
3994f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
40068bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
40168bece0bSHong Zhang   if (tflg) {
40268bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
40368bece0bSHong Zhang   }
404a05bf03eSHong Zhang 
405a05bf03eSHong Zhang   ierr = TSAdjointSetFromOptions(PetscOptionsObject,ts);CHKERRQ(ierr);
406d763cef2SBarry Smith 
407d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4080633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr);
409fbc52257SHong Zhang   ierr = PetscOptionsEnd();CHKERRQ(ierr);
410d763cef2SBarry Smith 
4114f122a70SLisandro Dalcin   if (ts->trajectory) {
4124f122a70SLisandro Dalcin     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
413d763cef2SBarry Smith   }
41468bece0bSHong Zhang 
4154f122a70SLisandro Dalcin   ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr);
4164f122a70SLisandro Dalcin   if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);}
4174f122a70SLisandro Dalcin   ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
418d763cef2SBarry Smith   PetscFunctionReturn(0);
419d763cef2SBarry Smith }
420d763cef2SBarry Smith 
421d2daff3dSHong Zhang /*@
42278fbdcc8SBarry Smith    TSGetTrajectory - Gets the trajectory from a TS if it exists
42378fbdcc8SBarry Smith 
42478fbdcc8SBarry Smith    Collective on TS
42578fbdcc8SBarry Smith 
42678fbdcc8SBarry Smith    Input Parameters:
42778fbdcc8SBarry Smith .  ts - the TS context obtained from TSCreate()
42878fbdcc8SBarry Smith 
42978fbdcc8SBarry Smith    Output Parameters;
43078fbdcc8SBarry Smith .  tr - the TSTrajectory object, if it exists
43178fbdcc8SBarry Smith 
43278fbdcc8SBarry Smith    Note: This routine should be called after all TS options have been set
43378fbdcc8SBarry Smith 
43478fbdcc8SBarry Smith    Level: advanced
43578fbdcc8SBarry Smith 
43678fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate()
43778fbdcc8SBarry Smith 
43878fbdcc8SBarry Smith .keywords: TS, set, checkpoint,
43978fbdcc8SBarry Smith @*/
44078fbdcc8SBarry Smith PetscErrorCode  TSGetTrajectory(TS ts,TSTrajectory *tr)
44178fbdcc8SBarry Smith {
44278fbdcc8SBarry Smith   PetscFunctionBegin;
44378fbdcc8SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44478fbdcc8SBarry Smith   *tr = ts->trajectory;
44578fbdcc8SBarry Smith   PetscFunctionReturn(0);
44678fbdcc8SBarry Smith }
44778fbdcc8SBarry Smith 
44878fbdcc8SBarry Smith /*@
449bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
450d2daff3dSHong Zhang 
451d2daff3dSHong Zhang    Collective on TS
452d2daff3dSHong Zhang 
453d2daff3dSHong Zhang    Input Parameters:
454bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
455bc952696SBarry Smith 
45678fbdcc8SBarry Smith    Options Database:
45778fbdcc8SBarry Smith +  -ts_save_trajectory - saves the trajectory to a file
45878fbdcc8SBarry Smith -  -ts_trajectory_type type
45978fbdcc8SBarry Smith 
46068bece0bSHong Zhang Note: This routine should be called after all TS options have been set
461d2daff3dSHong Zhang 
462c3a89c15SBarry Smith     The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and
46378fbdcc8SBarry Smith    MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m
46478fbdcc8SBarry Smith 
465d2daff3dSHong Zhang    Level: intermediate
466d2daff3dSHong Zhang 
46778fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectoryType, TSSetTrajectoryType()
468d2daff3dSHong Zhang 
469bc952696SBarry Smith .keywords: TS, set, checkpoint,
470d2daff3dSHong Zhang @*/
471bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
472d2daff3dSHong Zhang {
473bc952696SBarry Smith   PetscErrorCode ierr;
474bc952696SBarry Smith 
475d2daff3dSHong Zhang   PetscFunctionBegin;
476d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
477bc952696SBarry Smith   if (!ts->trajectory) {
478bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
479bc952696SBarry Smith   }
480d2daff3dSHong Zhang   PetscFunctionReturn(0);
481d2daff3dSHong Zhang }
482d2daff3dSHong Zhang 
483a7a1495cSBarry Smith /*@
484a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
485a7a1495cSBarry Smith       set with TSSetRHSJacobian().
486a7a1495cSBarry Smith 
487a7a1495cSBarry Smith    Collective on TS and Vec
488a7a1495cSBarry Smith 
489a7a1495cSBarry Smith    Input Parameters:
490316643e7SJed Brown +  ts - the TS context
491a7a1495cSBarry Smith .  t - current timestep
4920910c330SBarry Smith -  U - input vector
493a7a1495cSBarry Smith 
494a7a1495cSBarry Smith    Output Parameters:
495a7a1495cSBarry Smith +  A - Jacobian matrix
496a7a1495cSBarry Smith .  B - optional preconditioning matrix
497a7a1495cSBarry Smith -  flag - flag indicating matrix structure
498a7a1495cSBarry Smith 
499a7a1495cSBarry Smith    Notes:
500a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
501a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
502a7a1495cSBarry Smith 
50394b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
504a7a1495cSBarry Smith    flag parameter.
505a7a1495cSBarry Smith 
506a7a1495cSBarry Smith    Level: developer
507a7a1495cSBarry Smith 
508a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
509a7a1495cSBarry Smith 
51094b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
511a7a1495cSBarry Smith @*/
512d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
513a7a1495cSBarry Smith {
514dfbe8321SBarry Smith   PetscErrorCode   ierr;
515270bf2e7SJed Brown   PetscObjectState Ustate;
5166c1e1eecSBarry Smith   PetscObjectId    Uid;
51724989b8cSPeter Brune   DM               dm;
518942e3340SBarry Smith   DMTS             tsdm;
51924989b8cSPeter Brune   TSRHSJacobian    rhsjacobianfunc;
52024989b8cSPeter Brune   void             *ctx;
52124989b8cSPeter Brune   TSIJacobian      ijacobianfunc;
522b2df71adSDebojyoti Ghosh   TSRHSFunction    rhsfunction;
523a7a1495cSBarry Smith 
524a7a1495cSBarry Smith   PetscFunctionBegin;
5250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5260910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5270910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
52824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
529942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
53024989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
5310298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
532b2df71adSDebojyoti Ghosh   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
53359e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
5346c1e1eecSBarry Smith   ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
5356c1e1eecSBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
5360e4ef248SJed Brown     PetscFunctionReturn(0);
537f8ede8e7SJed Brown   }
538d90be118SSean Farley 
539ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
540d90be118SSean Farley 
541e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
54294ab13aaSBarry Smith     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
54394ab13aaSBarry Smith     ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
544303f9b21SStefano Zampini     if (B && A != B) {
54594ab13aaSBarry Smith       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
54694ab13aaSBarry Smith       ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
547e1244c69SJed Brown     }
548e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
549e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
550e1244c69SJed Brown   }
551e1244c69SJed Brown 
55224989b8cSPeter Brune   if (rhsjacobianfunc) {
5536cd88445SBarry Smith     PetscBool missing;
55494ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
555a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
556d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
557a7a1495cSBarry Smith     PetscStackPop;
55894ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
5596cd88445SBarry Smith     if (A) {
5606cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
5616cd88445SBarry Smith       if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Amat passed to TSSetRHSJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value");
5626cd88445SBarry Smith     }
5636cd88445SBarry Smith     if (B && B != A) {
5646cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
5656cd88445SBarry Smith       if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Bmat passed to TSSetRHSJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value");
5666cd88445SBarry Smith     }
567ef66eb69SBarry Smith   } else {
56894ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
56994ab13aaSBarry Smith     if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
570ef66eb69SBarry Smith   }
5710e4ef248SJed Brown   ts->rhsjacobian.time       = t;
5727f79407eSBarry Smith   ierr                       = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr);
57359e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
574a7a1495cSBarry Smith   PetscFunctionReturn(0);
575a7a1495cSBarry Smith }
576a7a1495cSBarry Smith 
577316643e7SJed Brown /*@
578d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
579d763cef2SBarry Smith 
580316643e7SJed Brown    Collective on TS and Vec
581316643e7SJed Brown 
582316643e7SJed Brown    Input Parameters:
583316643e7SJed Brown +  ts - the TS context
584316643e7SJed Brown .  t - current time
5850910c330SBarry Smith -  U - state vector
586316643e7SJed Brown 
587316643e7SJed Brown    Output Parameter:
588316643e7SJed Brown .  y - right hand side
589316643e7SJed Brown 
590316643e7SJed Brown    Note:
591316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
592316643e7SJed Brown    is used internally within the nonlinear solvers.
593316643e7SJed Brown 
594316643e7SJed Brown    Level: developer
595316643e7SJed Brown 
596316643e7SJed Brown .keywords: TS, compute
597316643e7SJed Brown 
598316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
599316643e7SJed Brown @*/
6000910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
601d763cef2SBarry Smith {
602dfbe8321SBarry Smith   PetscErrorCode ierr;
60324989b8cSPeter Brune   TSRHSFunction  rhsfunction;
60424989b8cSPeter Brune   TSIFunction    ifunction;
60524989b8cSPeter Brune   void           *ctx;
60624989b8cSPeter Brune   DM             dm;
60724989b8cSPeter Brune 
608d763cef2SBarry Smith   PetscFunctionBegin;
6090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6100910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6110700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
61224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
61324989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6140298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
615d763cef2SBarry Smith 
616ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
617d763cef2SBarry Smith 
6180910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
61924989b8cSPeter Brune   if (rhsfunction) {
620d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6210910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
622d763cef2SBarry Smith     PetscStackPop;
623214bc6a2SJed Brown   } else {
624214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
625b2cd27e8SJed Brown   }
62644a41b28SSean Farley 
6270910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
628d763cef2SBarry Smith   PetscFunctionReturn(0);
629d763cef2SBarry Smith }
630d763cef2SBarry Smith 
631ef20d060SBarry Smith /*@
632ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
633ef20d060SBarry Smith 
634ef20d060SBarry Smith    Collective on TS and Vec
635ef20d060SBarry Smith 
636ef20d060SBarry Smith    Input Parameters:
637ef20d060SBarry Smith +  ts - the TS context
638ef20d060SBarry Smith -  t - current time
639ef20d060SBarry Smith 
640ef20d060SBarry Smith    Output Parameter:
6410910c330SBarry Smith .  U - the solution
642ef20d060SBarry Smith 
643ef20d060SBarry Smith    Note:
644ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
645ef20d060SBarry Smith    is used internally within the nonlinear solvers.
646ef20d060SBarry Smith 
647ef20d060SBarry Smith    Level: developer
648ef20d060SBarry Smith 
649ef20d060SBarry Smith .keywords: TS, compute
650ef20d060SBarry Smith 
651abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
652ef20d060SBarry Smith @*/
6530910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
654ef20d060SBarry Smith {
655ef20d060SBarry Smith   PetscErrorCode     ierr;
656ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
657ef20d060SBarry Smith   void               *ctx;
658ef20d060SBarry Smith   DM                 dm;
659ef20d060SBarry Smith 
660ef20d060SBarry Smith   PetscFunctionBegin;
661ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6620910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
663ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
664ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
665ef20d060SBarry Smith 
666ef20d060SBarry Smith   if (solutionfunction) {
6679b7cd975SBarry Smith     PetscStackPush("TS user solution function");
6680910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
669ef20d060SBarry Smith     PetscStackPop;
670ef20d060SBarry Smith   }
671ef20d060SBarry Smith   PetscFunctionReturn(0);
672ef20d060SBarry Smith }
6739b7cd975SBarry Smith /*@
6749b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
6759b7cd975SBarry Smith 
6769b7cd975SBarry Smith    Collective on TS and Vec
6779b7cd975SBarry Smith 
6789b7cd975SBarry Smith    Input Parameters:
6799b7cd975SBarry Smith +  ts - the TS context
6809b7cd975SBarry Smith -  t - current time
6819b7cd975SBarry Smith 
6829b7cd975SBarry Smith    Output Parameter:
6839b7cd975SBarry Smith .  U - the function value
6849b7cd975SBarry Smith 
6859b7cd975SBarry Smith    Note:
6869b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
6879b7cd975SBarry Smith    is used internally within the nonlinear solvers.
6889b7cd975SBarry Smith 
6899b7cd975SBarry Smith    Level: developer
6909b7cd975SBarry Smith 
6919b7cd975SBarry Smith .keywords: TS, compute
6929b7cd975SBarry Smith 
6939b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
6949b7cd975SBarry Smith @*/
6959b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
6969b7cd975SBarry Smith {
6979b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
6989b7cd975SBarry Smith   void               *ctx;
6999b7cd975SBarry Smith   DM                 dm;
7009b7cd975SBarry Smith 
7019b7cd975SBarry Smith   PetscFunctionBegin;
7029b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7039b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7049b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7059b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7069b7cd975SBarry Smith 
7079b7cd975SBarry Smith   if (forcing) {
7089b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7099b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7109b7cd975SBarry Smith     PetscStackPop;
7119b7cd975SBarry Smith   }
7129b7cd975SBarry Smith   PetscFunctionReturn(0);
7139b7cd975SBarry Smith }
714ef20d060SBarry Smith 
715214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
716214bc6a2SJed Brown {
7172dd45cf8SJed Brown   Vec            F;
718214bc6a2SJed Brown   PetscErrorCode ierr;
719214bc6a2SJed Brown 
720214bc6a2SJed Brown   PetscFunctionBegin;
7210298fd71SBarry Smith   *Frhs = NULL;
7220298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
723214bc6a2SJed Brown   if (!ts->Frhs) {
7242dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
725214bc6a2SJed Brown   }
726214bc6a2SJed Brown   *Frhs = ts->Frhs;
727214bc6a2SJed Brown   PetscFunctionReturn(0);
728214bc6a2SJed Brown }
729214bc6a2SJed Brown 
730214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
731214bc6a2SJed Brown {
732214bc6a2SJed Brown   Mat            A,B;
7332dd45cf8SJed Brown   PetscErrorCode ierr;
73441a1d4d2SBarry Smith   TSIJacobian    ijacobian;
735214bc6a2SJed Brown 
736214bc6a2SJed Brown   PetscFunctionBegin;
737c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
738c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
73941a1d4d2SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr);
740214bc6a2SJed Brown   if (Arhs) {
741214bc6a2SJed Brown     if (!ts->Arhs) {
74241a1d4d2SBarry Smith       if (ijacobian) {
743214bc6a2SJed Brown         ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
74441a1d4d2SBarry Smith       } else {
74541a1d4d2SBarry Smith         ts->Arhs = A;
74641a1d4d2SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
74741a1d4d2SBarry Smith       }
7483565c898SBarry Smith     } else {
7493565c898SBarry Smith       PetscBool flg;
7503565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
7513565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
7523565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs){
7533565c898SBarry Smith         ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr);
7543565c898SBarry Smith         ts->Arhs = A;
7553565c898SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
7563565c898SBarry Smith       }
757214bc6a2SJed Brown     }
758214bc6a2SJed Brown     *Arhs = ts->Arhs;
759214bc6a2SJed Brown   }
760214bc6a2SJed Brown   if (Brhs) {
761214bc6a2SJed Brown     if (!ts->Brhs) {
762bdb70873SJed Brown       if (A != B) {
76341a1d4d2SBarry Smith         if (ijacobian) {
764214bc6a2SJed Brown           ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
765bdb70873SJed Brown         } else {
76641a1d4d2SBarry Smith           ts->Brhs = B;
76741a1d4d2SBarry Smith           ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
76841a1d4d2SBarry Smith         }
76941a1d4d2SBarry Smith       } else {
770bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
77151699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
772bdb70873SJed Brown       }
773214bc6a2SJed Brown     }
774214bc6a2SJed Brown     *Brhs = ts->Brhs;
775214bc6a2SJed Brown   }
776214bc6a2SJed Brown   PetscFunctionReturn(0);
777214bc6a2SJed Brown }
778214bc6a2SJed Brown 
779316643e7SJed Brown /*@
7800910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
781316643e7SJed Brown 
782316643e7SJed Brown    Collective on TS and Vec
783316643e7SJed Brown 
784316643e7SJed Brown    Input Parameters:
785316643e7SJed Brown +  ts - the TS context
786316643e7SJed Brown .  t - current time
7870910c330SBarry Smith .  U - state vector
7880910c330SBarry Smith .  Udot - time derivative of state vector
789214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
790316643e7SJed Brown 
791316643e7SJed Brown    Output Parameter:
792316643e7SJed Brown .  Y - right hand side
793316643e7SJed Brown 
794316643e7SJed Brown    Note:
795316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
796316643e7SJed Brown    is used internally within the nonlinear solvers.
797316643e7SJed Brown 
798316643e7SJed Brown    If the user did did not write their equations in implicit form, this
799316643e7SJed Brown    function recasts them in implicit form.
800316643e7SJed Brown 
801316643e7SJed Brown    Level: developer
802316643e7SJed Brown 
803316643e7SJed Brown .keywords: TS, compute
804316643e7SJed Brown 
805316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
806316643e7SJed Brown @*/
8070910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
808316643e7SJed Brown {
809316643e7SJed Brown   PetscErrorCode ierr;
81024989b8cSPeter Brune   TSIFunction    ifunction;
81124989b8cSPeter Brune   TSRHSFunction  rhsfunction;
81224989b8cSPeter Brune   void           *ctx;
81324989b8cSPeter Brune   DM             dm;
814316643e7SJed Brown 
815316643e7SJed Brown   PetscFunctionBegin;
8160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8170910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8180910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8190700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
820316643e7SJed Brown 
82124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
82224989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8230298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
82424989b8cSPeter Brune 
825ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
826d90be118SSean Farley 
8270910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
82824989b8cSPeter Brune   if (ifunction) {
829316643e7SJed Brown     PetscStackPush("TS user implicit function");
8300910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
831316643e7SJed Brown     PetscStackPop;
832214bc6a2SJed Brown   }
833214bc6a2SJed Brown   if (imex) {
83424989b8cSPeter Brune     if (!ifunction) {
8350910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8362dd45cf8SJed Brown     }
83724989b8cSPeter Brune   } else if (rhsfunction) {
83824989b8cSPeter Brune     if (ifunction) {
839214bc6a2SJed Brown       Vec Frhs;
840214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
8410910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
842214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
8432dd45cf8SJed Brown     } else {
8440910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
8450910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
846316643e7SJed Brown     }
8474a6899ffSJed Brown   }
8480910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
849316643e7SJed Brown   PetscFunctionReturn(0);
850316643e7SJed Brown }
851316643e7SJed Brown 
852316643e7SJed Brown /*@
853316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
854316643e7SJed Brown 
855316643e7SJed Brown    Collective on TS and Vec
856316643e7SJed Brown 
857316643e7SJed Brown    Input
858316643e7SJed Brown       Input Parameters:
859316643e7SJed Brown +  ts - the TS context
860316643e7SJed Brown .  t - current timestep
8610910c330SBarry Smith .  U - state vector
8620910c330SBarry Smith .  Udot - time derivative of state vector
863214bc6a2SJed Brown .  shift - shift to apply, see note below
864214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
865316643e7SJed Brown 
866316643e7SJed Brown    Output Parameters:
867316643e7SJed Brown +  A - Jacobian matrix
8683565c898SBarry Smith -  B - matrix from which the preconditioner is constructed; often the same as A
869316643e7SJed Brown 
870316643e7SJed Brown    Notes:
8710910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
872316643e7SJed Brown 
8730910c330SBarry Smith    dF/dU + shift*dF/dUdot
874316643e7SJed Brown 
875316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
876316643e7SJed Brown    is used internally within the nonlinear solvers.
877316643e7SJed Brown 
878316643e7SJed Brown    Level: developer
879316643e7SJed Brown 
880316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
881316643e7SJed Brown 
882316643e7SJed Brown .seealso:  TSSetIJacobian()
88363495f91SJed Brown @*/
884d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
885316643e7SJed Brown {
886316643e7SJed Brown   PetscErrorCode ierr;
88724989b8cSPeter Brune   TSIJacobian    ijacobian;
88824989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
88924989b8cSPeter Brune   DM             dm;
89024989b8cSPeter Brune   void           *ctx;
891316643e7SJed Brown 
892316643e7SJed Brown   PetscFunctionBegin;
8930700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8940910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8950910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
896316643e7SJed Brown   PetscValidPointer(A,6);
89794ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
898316643e7SJed Brown   PetscValidPointer(B,7);
89994ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
90024989b8cSPeter Brune 
90124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
90224989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9030298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
90424989b8cSPeter Brune 
905ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
906316643e7SJed Brown 
90794ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
90824989b8cSPeter Brune   if (ijacobian) {
9096cd88445SBarry Smith     PetscBool missing;
910316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
911d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
912316643e7SJed Brown     PetscStackPop;
9136cd88445SBarry Smith     ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
9146cd88445SBarry Smith     if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Amat passed to TSSetIJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value");
9153565c898SBarry Smith     if (B != A) {
9166cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
9176cd88445SBarry Smith       if (missing) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Bmat passed to TSSetIJacobian() must have all diagonal entries set, if they are zero you must still set them with a zero value");
9186cd88445SBarry Smith     }
9194a6899ffSJed Brown   }
920214bc6a2SJed Brown   if (imex) {
921b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9224c26be97Sstefano_zampini       PetscBool assembled;
92394ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
9244c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
9254c26be97Sstefano_zampini       if (!assembled) {
9264c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9274c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9284c26be97Sstefano_zampini       }
92994ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
93094ab13aaSBarry Smith       if (A != B) {
93194ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
9324c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
9334c26be97Sstefano_zampini         if (!assembled) {
9344c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9354c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9364c26be97Sstefano_zampini         }
93794ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
938214bc6a2SJed Brown       }
939214bc6a2SJed Brown     }
940214bc6a2SJed Brown   } else {
941e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
942e1244c69SJed Brown     if (rhsjacobian) {
943214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
944d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
945e1244c69SJed Brown     }
94694ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
9473565c898SBarry Smith       PetscBool flg;
948e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
949e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
9503565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
9513565c898SBarry Smith       /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
9523565c898SBarry Smith       if (!flg) {
95394ab13aaSBarry Smith         ierr = MatScale(A,-1);CHKERRQ(ierr);
95494ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
9553565c898SBarry Smith       }
95694ab13aaSBarry Smith       if (A != B) {
95794ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
95894ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
959316643e7SJed Brown       }
960e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
961e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
962e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
96394ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
96494ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
96594ab13aaSBarry Smith         if (A != B) {
96694ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
96794ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
968214bc6a2SJed Brown         }
969316643e7SJed Brown       }
97094ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
97194ab13aaSBarry Smith       if (A != B) {
97294ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
973316643e7SJed Brown       }
974316643e7SJed Brown     }
975316643e7SJed Brown   }
97694ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
977316643e7SJed Brown   PetscFunctionReturn(0);
978316643e7SJed Brown }
979316643e7SJed Brown 
980d763cef2SBarry Smith /*@C
981d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
982b5abc632SBarry Smith     where U_t = G(t,u).
983d763cef2SBarry Smith 
9843f9fe445SBarry Smith     Logically Collective on TS
985d763cef2SBarry Smith 
986d763cef2SBarry Smith     Input Parameters:
987d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
9880298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
989d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
990d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
9910298fd71SBarry Smith           function evaluation routine (may be NULL)
992d763cef2SBarry Smith 
993d763cef2SBarry Smith     Calling sequence of func:
99487828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
995d763cef2SBarry Smith 
996d763cef2SBarry Smith +   t - current timestep
997d763cef2SBarry Smith .   u - input vector
998d763cef2SBarry Smith .   F - function vector
999d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1000d763cef2SBarry Smith 
1001d763cef2SBarry Smith     Level: beginner
1002d763cef2SBarry Smith 
100395452b02SPatrick Sanan     Notes:
100495452b02SPatrick Sanan     You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10052bbac0d3SBarry Smith 
1006d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1007d763cef2SBarry Smith 
1008ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1009d763cef2SBarry Smith @*/
1010089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1011d763cef2SBarry Smith {
1012089b2837SJed Brown   PetscErrorCode ierr;
1013089b2837SJed Brown   SNES           snes;
10140298fd71SBarry Smith   Vec            ralloc = NULL;
101524989b8cSPeter Brune   DM             dm;
1016d763cef2SBarry Smith 
1017089b2837SJed Brown   PetscFunctionBegin;
10180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1019ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
102024989b8cSPeter Brune 
102124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
102224989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1023089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1024e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1025e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1026e856ceecSJed Brown     r = ralloc;
1027e856ceecSJed Brown   }
1028089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1029e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1030d763cef2SBarry Smith   PetscFunctionReturn(0);
1031d763cef2SBarry Smith }
1032d763cef2SBarry Smith 
1033ef20d060SBarry Smith /*@C
1034abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1035ef20d060SBarry Smith 
1036ef20d060SBarry Smith     Logically Collective on TS
1037ef20d060SBarry Smith 
1038ef20d060SBarry Smith     Input Parameters:
1039ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1040ef20d060SBarry Smith .   f - routine for evaluating the solution
1041ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
10420298fd71SBarry Smith           function evaluation routine (may be NULL)
1043ef20d060SBarry Smith 
1044ef20d060SBarry Smith     Calling sequence of func:
1045ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
1046ef20d060SBarry Smith 
1047ef20d060SBarry Smith +   t - current timestep
1048ef20d060SBarry Smith .   u - output vector
1049ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1050ef20d060SBarry Smith 
10510ed3bfb6SBarry Smith     Options Database:
10520ed3bfb6SBarry Smith +  -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction()
10530ed3bfb6SBarry Smith -  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
10540ed3bfb6SBarry Smith 
1055abd5a294SJed Brown     Notes:
1056abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1057abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1058abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1059abd5a294SJed Brown 
10604f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1061abd5a294SJed Brown 
1062ef20d060SBarry Smith     Level: beginner
1063ef20d060SBarry Smith 
1064ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1065ef20d060SBarry Smith 
10660ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError()
1067ef20d060SBarry Smith @*/
1068ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1069ef20d060SBarry Smith {
1070ef20d060SBarry Smith   PetscErrorCode ierr;
1071ef20d060SBarry Smith   DM             dm;
1072ef20d060SBarry Smith 
1073ef20d060SBarry Smith   PetscFunctionBegin;
1074ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1075ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1076ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1077ef20d060SBarry Smith   PetscFunctionReturn(0);
1078ef20d060SBarry Smith }
1079ef20d060SBarry Smith 
10809b7cd975SBarry Smith /*@C
10819b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
10829b7cd975SBarry Smith 
10839b7cd975SBarry Smith     Logically Collective on TS
10849b7cd975SBarry Smith 
10859b7cd975SBarry Smith     Input Parameters:
10869b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1087e162b725SBarry Smith .   func - routine for evaluating the forcing function
10889b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
10890298fd71SBarry Smith           function evaluation routine (may be NULL)
10909b7cd975SBarry Smith 
10919b7cd975SBarry Smith     Calling sequence of func:
1092e162b725SBarry Smith $     func (TS ts,PetscReal t,Vec f,void *ctx);
10939b7cd975SBarry Smith 
10949b7cd975SBarry Smith +   t - current timestep
1095e162b725SBarry Smith .   f - output vector
10969b7cd975SBarry Smith -   ctx - [optional] user-defined function context
10979b7cd975SBarry Smith 
10989b7cd975SBarry Smith     Notes:
10999b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1100e162b725SBarry Smith     create closed-form solutions with a non-physical forcing term. It allows you to use the Method of Manufactored Solution without directly editing the
1101e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1102e162b725SBarry Smith 
1103e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1104e162b725SBarry Smith 
1105e162b725SBarry Smith     This forcing function does not depend on the solution to the equations, it can only depend on spatial location, time, and possibly parameters, the
1106e162b725SBarry Smith     parameters can be passed in the ctx variable.
11079b7cd975SBarry Smith 
11089b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
11099b7cd975SBarry Smith 
11109b7cd975SBarry Smith     Level: beginner
11119b7cd975SBarry Smith 
11129b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
11139b7cd975SBarry Smith 
11149b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
11159b7cd975SBarry Smith @*/
1116e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
11179b7cd975SBarry Smith {
11189b7cd975SBarry Smith   PetscErrorCode ierr;
11199b7cd975SBarry Smith   DM             dm;
11209b7cd975SBarry Smith 
11219b7cd975SBarry Smith   PetscFunctionBegin;
11229b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11239b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1124e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
11259b7cd975SBarry Smith   PetscFunctionReturn(0);
11269b7cd975SBarry Smith }
11279b7cd975SBarry Smith 
1128d763cef2SBarry Smith /*@C
1129f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1130b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1131d763cef2SBarry Smith 
11323f9fe445SBarry Smith    Logically Collective on TS
1133d763cef2SBarry Smith 
1134d763cef2SBarry Smith    Input Parameters:
1135d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1136e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1137e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1138d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1139d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
11400298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1141d763cef2SBarry Smith 
1142f7ab8db6SBarry Smith    Calling sequence of f:
1143ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1144d763cef2SBarry Smith 
1145d763cef2SBarry Smith +  t - current timestep
1146d763cef2SBarry Smith .  u - input vector
1147e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1148e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1149d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1150d763cef2SBarry Smith 
11516cd88445SBarry Smith    Notes:
11526cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
11536cd88445SBarry Smith 
11546cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1155ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1156d763cef2SBarry Smith 
1157d763cef2SBarry Smith    Level: beginner
1158d763cef2SBarry Smith 
1159d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1160d763cef2SBarry Smith 
1161ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1162d763cef2SBarry Smith 
1163d763cef2SBarry Smith @*/
1164e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1165d763cef2SBarry Smith {
1166277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1167089b2837SJed Brown   SNES           snes;
116824989b8cSPeter Brune   DM             dm;
116924989b8cSPeter Brune   TSIJacobian    ijacobian;
1170277b19d0SLisandro Dalcin 
1171d763cef2SBarry Smith   PetscFunctionBegin;
11720700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1173e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1174e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1175e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1176e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1177d763cef2SBarry Smith 
117824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
117924989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1180e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1181e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1182e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1183e1244c69SJed Brown   }
11840298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1185089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11865f659677SPeter Brune   if (!ijacobian) {
1187e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
11880e4ef248SJed Brown   }
1189e5d3d808SBarry Smith   if (Amat) {
1190e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
11910e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1192e5d3d808SBarry Smith     ts->Arhs = Amat;
11930e4ef248SJed Brown   }
1194e5d3d808SBarry Smith   if (Pmat) {
1195e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
11960e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1197e5d3d808SBarry Smith     ts->Brhs = Pmat;
11980e4ef248SJed Brown   }
1199d763cef2SBarry Smith   PetscFunctionReturn(0);
1200d763cef2SBarry Smith }
1201d763cef2SBarry Smith 
1202316643e7SJed Brown /*@C
1203b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1204316643e7SJed Brown 
12053f9fe445SBarry Smith    Logically Collective on TS
1206316643e7SJed Brown 
1207316643e7SJed Brown    Input Parameters:
1208316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12090298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1210316643e7SJed Brown .  f   - the function evaluation routine
12110298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1212316643e7SJed Brown 
1213316643e7SJed Brown    Calling sequence of f:
1214316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1215316643e7SJed Brown 
1216316643e7SJed Brown +  t   - time at step/stage being solved
1217316643e7SJed Brown .  u   - state vector
1218316643e7SJed Brown .  u_t - time derivative of state vector
1219316643e7SJed Brown .  F   - function vector
1220316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1221316643e7SJed Brown 
1222316643e7SJed Brown    Important:
12232bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1224316643e7SJed Brown 
1225316643e7SJed Brown    Level: beginner
1226316643e7SJed Brown 
1227316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1228316643e7SJed Brown 
1229d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1230316643e7SJed Brown @*/
123151699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1232316643e7SJed Brown {
1233089b2837SJed Brown   PetscErrorCode ierr;
1234089b2837SJed Brown   SNES           snes;
123551699248SLisandro Dalcin   Vec            ralloc = NULL;
123624989b8cSPeter Brune   DM             dm;
1237316643e7SJed Brown 
1238316643e7SJed Brown   PetscFunctionBegin;
12390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
124051699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
124124989b8cSPeter Brune 
124224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
124324989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
124424989b8cSPeter Brune 
1245089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
124651699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
124751699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
124851699248SLisandro Dalcin     r  = ralloc;
1249e856ceecSJed Brown   }
125051699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
125151699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1252089b2837SJed Brown   PetscFunctionReturn(0);
1253089b2837SJed Brown }
1254089b2837SJed Brown 
1255089b2837SJed Brown /*@C
1256089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1257089b2837SJed Brown 
1258089b2837SJed Brown    Not Collective
1259089b2837SJed Brown 
1260089b2837SJed Brown    Input Parameter:
1261089b2837SJed Brown .  ts - the TS context
1262089b2837SJed Brown 
1263089b2837SJed Brown    Output Parameter:
12640298fd71SBarry Smith +  r - vector to hold residual (or NULL)
12650298fd71SBarry Smith .  func - the function to compute residual (or NULL)
12660298fd71SBarry Smith -  ctx - the function context (or NULL)
1267089b2837SJed Brown 
1268089b2837SJed Brown    Level: advanced
1269089b2837SJed Brown 
1270089b2837SJed Brown .keywords: TS, nonlinear, get, function
1271089b2837SJed Brown 
1272089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1273089b2837SJed Brown @*/
1274089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1275089b2837SJed Brown {
1276089b2837SJed Brown   PetscErrorCode ierr;
1277089b2837SJed Brown   SNES           snes;
127824989b8cSPeter Brune   DM             dm;
1279089b2837SJed Brown 
1280089b2837SJed Brown   PetscFunctionBegin;
1281089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1282089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12830298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
128424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
128524989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1286089b2837SJed Brown   PetscFunctionReturn(0);
1287089b2837SJed Brown }
1288089b2837SJed Brown 
1289089b2837SJed Brown /*@C
1290089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1291089b2837SJed Brown 
1292089b2837SJed Brown    Not Collective
1293089b2837SJed Brown 
1294089b2837SJed Brown    Input Parameter:
1295089b2837SJed Brown .  ts - the TS context
1296089b2837SJed Brown 
1297089b2837SJed Brown    Output Parameter:
12980298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
12990298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13000298fd71SBarry Smith -  ctx - the function context (or NULL)
1301089b2837SJed Brown 
1302089b2837SJed Brown    Level: advanced
1303089b2837SJed Brown 
1304089b2837SJed Brown .keywords: TS, nonlinear, get, function
1305089b2837SJed Brown 
13062bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1307089b2837SJed Brown @*/
1308089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1309089b2837SJed Brown {
1310089b2837SJed Brown   PetscErrorCode ierr;
1311089b2837SJed Brown   SNES           snes;
131224989b8cSPeter Brune   DM             dm;
1313089b2837SJed Brown 
1314089b2837SJed Brown   PetscFunctionBegin;
1315089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1316089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13170298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
131824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
131924989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1320316643e7SJed Brown   PetscFunctionReturn(0);
1321316643e7SJed Brown }
1322316643e7SJed Brown 
1323316643e7SJed Brown /*@C
1324a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1325ae8867d6SBarry Smith         provided with TSSetIFunction().
1326316643e7SJed Brown 
13273f9fe445SBarry Smith    Logically Collective on TS
1328316643e7SJed Brown 
1329316643e7SJed Brown    Input Parameters:
1330316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1331e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1332e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1333316643e7SJed Brown .  f   - the Jacobian evaluation routine
13340298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1335316643e7SJed Brown 
1336316643e7SJed Brown    Calling sequence of f:
1337ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1338316643e7SJed Brown 
1339316643e7SJed Brown +  t    - time at step/stage being solved
13401b4a444bSJed Brown .  U    - state vector
13411b4a444bSJed Brown .  U_t  - time derivative of state vector
1342316643e7SJed Brown .  a    - shift
1343e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1344e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1345316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1346316643e7SJed Brown 
1347316643e7SJed Brown    Notes:
1348e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1349316643e7SJed Brown 
1350895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1351895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1352895c21f2SBarry Smith 
1353a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1354b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1355a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1356a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1357a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1358a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1359a4f0a591SBarry Smith 
13606cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
13616cd88445SBarry Smith 
13626cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1363ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1364ca5f011dSBarry Smith 
1365316643e7SJed Brown    Level: beginner
1366316643e7SJed Brown 
1367316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1368316643e7SJed Brown 
1369ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1370316643e7SJed Brown 
1371316643e7SJed Brown @*/
1372e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1373316643e7SJed Brown {
1374316643e7SJed Brown   PetscErrorCode ierr;
1375089b2837SJed Brown   SNES           snes;
137624989b8cSPeter Brune   DM             dm;
1377316643e7SJed Brown 
1378316643e7SJed Brown   PetscFunctionBegin;
13790700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1380e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1381e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1382e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1383e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
138424989b8cSPeter Brune 
138524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
138624989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
138724989b8cSPeter Brune 
1388089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1389e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1390316643e7SJed Brown   PetscFunctionReturn(0);
1391316643e7SJed Brown }
1392316643e7SJed Brown 
1393e1244c69SJed Brown /*@
1394e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1395e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1396e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1397e1244c69SJed Brown    not been changed by the TS.
1398e1244c69SJed Brown 
1399e1244c69SJed Brown    Logically Collective
1400e1244c69SJed Brown 
1401e1244c69SJed Brown    Input Arguments:
1402e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1403e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1404e1244c69SJed Brown 
1405e1244c69SJed Brown    Level: intermediate
1406e1244c69SJed Brown 
1407e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1408e1244c69SJed Brown @*/
1409e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1410e1244c69SJed Brown {
1411e1244c69SJed Brown   PetscFunctionBegin;
1412e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1413e1244c69SJed Brown   PetscFunctionReturn(0);
1414e1244c69SJed Brown }
1415e1244c69SJed Brown 
1416efe9872eSLisandro Dalcin /*@C
1417efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1418efe9872eSLisandro Dalcin 
1419efe9872eSLisandro Dalcin    Logically Collective on TS
1420efe9872eSLisandro Dalcin 
1421efe9872eSLisandro Dalcin    Input Parameters:
1422efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1423efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1424efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1425efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1426efe9872eSLisandro Dalcin 
1427efe9872eSLisandro Dalcin    Calling sequence of fun:
1428efe9872eSLisandro Dalcin $  fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1429efe9872eSLisandro Dalcin 
1430efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1431efe9872eSLisandro Dalcin .  U    - state vector
1432efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1433efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1434efe9872eSLisandro Dalcin .  F    - function vector
1435efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1436efe9872eSLisandro Dalcin 
1437efe9872eSLisandro Dalcin    Level: beginner
1438efe9872eSLisandro Dalcin 
1439efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function
1440efe9872eSLisandro Dalcin 
1441efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian()
1442efe9872eSLisandro Dalcin @*/
1443efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1444efe9872eSLisandro Dalcin {
1445efe9872eSLisandro Dalcin   DM             dm;
1446efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1447efe9872eSLisandro Dalcin 
1448efe9872eSLisandro Dalcin   PetscFunctionBegin;
1449efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1450efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1451efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1452efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1453efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1454efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1455efe9872eSLisandro Dalcin }
1456efe9872eSLisandro Dalcin 
1457efe9872eSLisandro Dalcin /*@C
1458efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1459efe9872eSLisandro Dalcin 
1460efe9872eSLisandro Dalcin   Not Collective
1461efe9872eSLisandro Dalcin 
1462efe9872eSLisandro Dalcin   Input Parameter:
1463efe9872eSLisandro Dalcin . ts - the TS context
1464efe9872eSLisandro Dalcin 
1465efe9872eSLisandro Dalcin   Output Parameter:
1466efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1467efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1468efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1469efe9872eSLisandro Dalcin 
1470efe9872eSLisandro Dalcin   Level: advanced
1471efe9872eSLisandro Dalcin 
1472efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function
1473efe9872eSLisandro Dalcin 
1474efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction()
1475efe9872eSLisandro Dalcin @*/
1476efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1477efe9872eSLisandro Dalcin {
1478efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1479efe9872eSLisandro Dalcin   SNES           snes;
1480efe9872eSLisandro Dalcin   DM             dm;
1481efe9872eSLisandro Dalcin 
1482efe9872eSLisandro Dalcin   PetscFunctionBegin;
1483efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1484efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1485efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1486efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1487efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1488efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1489efe9872eSLisandro Dalcin }
1490efe9872eSLisandro Dalcin 
1491efe9872eSLisandro Dalcin /*@C
1492bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1493efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1494efe9872eSLisandro Dalcin 
1495efe9872eSLisandro Dalcin    Logically Collective on TS
1496efe9872eSLisandro Dalcin 
1497efe9872eSLisandro Dalcin    Input Parameters:
1498efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1499efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1500efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1501efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1502efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1503efe9872eSLisandro Dalcin 
1504efe9872eSLisandro Dalcin    Calling sequence of jac:
1505bc77d74cSLisandro Dalcin $  jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx);
1506efe9872eSLisandro Dalcin 
1507efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1508efe9872eSLisandro Dalcin .  U    - state vector
1509efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1510efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1511efe9872eSLisandro Dalcin .  v    - shift for U_t
1512efe9872eSLisandro Dalcin .  a    - shift for U_tt
1513efe9872eSLisandro Dalcin .  J    - Jacobian of G(U) = F(t,U,W+v*U,W'+a*U), equivalent to dF/dU + v*dF/dU_t  + a*dF/dU_tt
1514efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1515efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1516efe9872eSLisandro Dalcin 
1517efe9872eSLisandro Dalcin    Notes:
1518efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1519efe9872eSLisandro Dalcin 
1520efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1521efe9872eSLisandro Dalcin    the Jacobian of G(U) = F(t,U,W+v*U,W'+a*U) where F(t,U,U_t,U_tt) = 0 is the DAE to be solved.
1522efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1523bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1524efe9872eSLisandro Dalcin 
1525efe9872eSLisandro Dalcin    Level: beginner
1526efe9872eSLisandro Dalcin 
1527efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian
1528efe9872eSLisandro Dalcin 
1529efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1530efe9872eSLisandro Dalcin @*/
1531efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1532efe9872eSLisandro Dalcin {
1533efe9872eSLisandro Dalcin   DM             dm;
1534efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1535efe9872eSLisandro Dalcin 
1536efe9872eSLisandro Dalcin   PetscFunctionBegin;
1537efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1538efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1539efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1540efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1541efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1542efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1543efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1544efe9872eSLisandro Dalcin }
1545efe9872eSLisandro Dalcin 
1546efe9872eSLisandro Dalcin /*@C
1547efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1548efe9872eSLisandro Dalcin 
1549efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1550efe9872eSLisandro Dalcin 
1551efe9872eSLisandro Dalcin   Input Parameter:
1552efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1553efe9872eSLisandro Dalcin 
1554efe9872eSLisandro Dalcin   Output Parameters:
1555efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1556efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1557efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1558efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1559efe9872eSLisandro Dalcin 
156095452b02SPatrick Sanan   Notes:
156195452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
1562efe9872eSLisandro Dalcin 
1563efe9872eSLisandro Dalcin   Level: advanced
1564efe9872eSLisandro Dalcin 
156580275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
1566efe9872eSLisandro Dalcin 
1567efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian
1568efe9872eSLisandro Dalcin @*/
1569efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1570efe9872eSLisandro Dalcin {
1571efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1572efe9872eSLisandro Dalcin   SNES           snes;
1573efe9872eSLisandro Dalcin   DM             dm;
1574efe9872eSLisandro Dalcin 
1575efe9872eSLisandro Dalcin   PetscFunctionBegin;
1576efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1577efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1578efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1579efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1580efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1581efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1582efe9872eSLisandro Dalcin }
1583efe9872eSLisandro Dalcin 
1584efe9872eSLisandro Dalcin /*@
1585efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1586efe9872eSLisandro Dalcin 
1587efe9872eSLisandro Dalcin   Collective on TS and Vec
1588efe9872eSLisandro Dalcin 
1589efe9872eSLisandro Dalcin   Input Parameters:
1590efe9872eSLisandro Dalcin + ts - the TS context
1591efe9872eSLisandro Dalcin . t - current time
1592efe9872eSLisandro Dalcin . U - state vector
1593efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1594efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1595efe9872eSLisandro Dalcin 
1596efe9872eSLisandro Dalcin   Output Parameter:
1597efe9872eSLisandro Dalcin . F - the residual vector
1598efe9872eSLisandro Dalcin 
1599efe9872eSLisandro Dalcin   Note:
1600efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1601efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1602efe9872eSLisandro Dalcin 
1603efe9872eSLisandro Dalcin   Level: developer
1604efe9872eSLisandro Dalcin 
1605efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector
1606efe9872eSLisandro Dalcin 
1607efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1608efe9872eSLisandro Dalcin @*/
1609efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1610efe9872eSLisandro Dalcin {
1611efe9872eSLisandro Dalcin   DM             dm;
1612efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1613efe9872eSLisandro Dalcin   void           *ctx;
1614efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1615efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1616efe9872eSLisandro Dalcin 
1617efe9872eSLisandro Dalcin   PetscFunctionBegin;
1618efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1619efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1620efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1621efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1622efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1623efe9872eSLisandro Dalcin 
1624efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1625efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1626efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1627efe9872eSLisandro Dalcin 
1628efe9872eSLisandro Dalcin   if (!I2Function) {
1629efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1630efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1631efe9872eSLisandro Dalcin   }
1632efe9872eSLisandro Dalcin 
1633efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1634efe9872eSLisandro Dalcin 
1635efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1636efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1637efe9872eSLisandro Dalcin   PetscStackPop;
1638efe9872eSLisandro Dalcin 
1639efe9872eSLisandro Dalcin   if (rhsfunction) {
1640efe9872eSLisandro Dalcin     Vec Frhs;
1641efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1642efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1643efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1644efe9872eSLisandro Dalcin   }
1645efe9872eSLisandro Dalcin 
1646efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1647efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1648efe9872eSLisandro Dalcin }
1649efe9872eSLisandro Dalcin 
1650efe9872eSLisandro Dalcin /*@
1651efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1652efe9872eSLisandro Dalcin 
1653efe9872eSLisandro Dalcin   Collective on TS and Vec
1654efe9872eSLisandro Dalcin 
1655efe9872eSLisandro Dalcin   Input Parameters:
1656efe9872eSLisandro Dalcin + ts - the TS context
1657efe9872eSLisandro Dalcin . t - current timestep
1658efe9872eSLisandro Dalcin . U - state vector
1659efe9872eSLisandro Dalcin . V - time derivative of state vector
1660efe9872eSLisandro Dalcin . A - second time derivative of state vector
1661efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1662efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1663efe9872eSLisandro Dalcin 
1664efe9872eSLisandro Dalcin   Output Parameters:
1665efe9872eSLisandro Dalcin + J - Jacobian matrix
1666efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1667efe9872eSLisandro Dalcin 
1668efe9872eSLisandro Dalcin   Notes:
1669efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1670efe9872eSLisandro Dalcin 
1671efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1672efe9872eSLisandro Dalcin 
1673efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1674efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1675efe9872eSLisandro Dalcin 
1676efe9872eSLisandro Dalcin   Level: developer
1677efe9872eSLisandro Dalcin 
1678efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix
1679efe9872eSLisandro Dalcin 
1680efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1681efe9872eSLisandro Dalcin @*/
1682efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1683efe9872eSLisandro Dalcin {
1684efe9872eSLisandro Dalcin   DM             dm;
1685efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1686efe9872eSLisandro Dalcin   void           *ctx;
1687efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1688efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1689efe9872eSLisandro Dalcin 
1690efe9872eSLisandro Dalcin   PetscFunctionBegin;
1691efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1692efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1693efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1694efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1695efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1696efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1697efe9872eSLisandro Dalcin 
1698efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1699efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1700efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1701efe9872eSLisandro Dalcin 
1702efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1703efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1704efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1705efe9872eSLisandro Dalcin   }
1706efe9872eSLisandro Dalcin 
1707efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1708efe9872eSLisandro Dalcin 
1709efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1710efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1711efe9872eSLisandro Dalcin   PetscStackPop;
1712efe9872eSLisandro Dalcin 
1713efe9872eSLisandro Dalcin   if (rhsjacobian) {
1714efe9872eSLisandro Dalcin     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1715efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1716efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1717efe9872eSLisandro Dalcin     ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr);
1718efe9872eSLisandro Dalcin     if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);}
1719efe9872eSLisandro Dalcin   }
1720efe9872eSLisandro Dalcin 
1721efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1722efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1723efe9872eSLisandro Dalcin }
1724efe9872eSLisandro Dalcin 
1725efe9872eSLisandro Dalcin /*@
1726efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1727efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1728efe9872eSLisandro Dalcin 
1729efe9872eSLisandro Dalcin    Logically Collective on TS and Vec
1730efe9872eSLisandro Dalcin 
1731efe9872eSLisandro Dalcin    Input Parameters:
1732efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1733efe9872eSLisandro Dalcin .  u - the solution vector
1734efe9872eSLisandro Dalcin -  v - the time derivative vector
1735efe9872eSLisandro Dalcin 
1736efe9872eSLisandro Dalcin    Level: beginner
1737efe9872eSLisandro Dalcin 
1738efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions
1739efe9872eSLisandro Dalcin @*/
1740efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1741efe9872eSLisandro Dalcin {
1742efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1743efe9872eSLisandro Dalcin 
1744efe9872eSLisandro Dalcin   PetscFunctionBegin;
1745efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1746efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1747efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1748efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1749efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1750efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1751efe9872eSLisandro Dalcin   ts->vec_dot = v;
1752efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1753efe9872eSLisandro Dalcin }
1754efe9872eSLisandro Dalcin 
1755efe9872eSLisandro Dalcin /*@
1756efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1757efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1758efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1759efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1760efe9872eSLisandro Dalcin 
1761efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1762efe9872eSLisandro Dalcin 
1763efe9872eSLisandro Dalcin    Input Parameter:
1764efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1765efe9872eSLisandro Dalcin 
1766efe9872eSLisandro Dalcin    Output Parameter:
1767efe9872eSLisandro Dalcin +  u - the vector containing the solution
1768efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1769efe9872eSLisandro Dalcin 
1770efe9872eSLisandro Dalcin    Level: intermediate
1771efe9872eSLisandro Dalcin 
1772efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1773efe9872eSLisandro Dalcin 
1774efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution
1775efe9872eSLisandro Dalcin @*/
1776efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1777efe9872eSLisandro Dalcin {
1778efe9872eSLisandro Dalcin   PetscFunctionBegin;
1779efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1780efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1781efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1782efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1783efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1784efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1785efe9872eSLisandro Dalcin }
1786efe9872eSLisandro Dalcin 
178755849f57SBarry Smith /*@C
1788*87f4e208SHong Zhang    TSSetRHSSplits - Set the number of splits for the RHS function
1789d9194312SHong Zhang 
1790d9194312SHong Zhang    Logically Collective on TS
1791d9194312SHong Zhang 
1792d9194312SHong Zhang    Input Parameters:
1793d9194312SHong Zhang    + ts        - the TS context obtained from TSCreate()
1794*87f4e208SHong Zhang    - numsplits - the number of splits for the RHS function
1795*87f4e208SHong Zhang 
1796*87f4e208SHong Zhang    Level: intermediate
1797*87f4e208SHong Zhang 
1798*87f4e208SHong Zhang .seealso: TSSetRHSSplitFunction, TSGetRHSSplitFunction
1799*87f4e208SHong Zhang 
1800*87f4e208SHong Zhang .keywords: TS, multirate
1801*87f4e208SHong Zhang @*/
1802*87f4e208SHong Zhang PetscErrorCode TSSetRHSSplits(TS ts,PetscInt numsplits)
1803*87f4e208SHong Zhang {
1804*87f4e208SHong Zhang   PetscFunctionBegin;
1805*87f4e208SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1806*87f4e208SHong Zhang   if (numsplits > MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of splits set for RHSFunction exceeds the maximum number allowed");
1807*87f4e208SHong Zhang   ts->num_rhs_splits = numsplits;
1808*87f4e208SHong Zhang   PetscFunctionReturn(0);
1809*87f4e208SHong Zhang }
1810*87f4e208SHong Zhang 
1811*87f4e208SHong Zhang /*@C
1812*87f4e208SHong Zhang    TSSetRHSSplitIS - Set the index set for the specified split
1813*87f4e208SHong Zhang 
1814*87f4e208SHong Zhang    Logically Collective on TS
1815*87f4e208SHong Zhang 
1816*87f4e208SHong Zhang    Input Parameters:
1817*87f4e208SHong Zhang    + ts  - the TS context obtained from TSCreate()
1818*87f4e208SHong Zhang    . ind - the index of the split, starting from 1
1819*87f4e208SHong Zhang    - is  - the index set for part of the solution vector
1820*87f4e208SHong Zhang 
1821*87f4e208SHong Zhang    Level: intermediate
1822*87f4e208SHong Zhang 
1823*87f4e208SHong Zhang .seealso: TSSetRHSSplits
1824*87f4e208SHong Zhang 
1825*87f4e208SHong Zhang .keywords: TS, multirate
1826*87f4e208SHong Zhang @*/
1827*87f4e208SHong Zhang PetscErrorCode TSSetRHSSplitIS(TS ts,PetscInt ind,IS is)
1828*87f4e208SHong Zhang {
1829*87f4e208SHong Zhang   PetscErrorCode ierr;
1830*87f4e208SHong Zhang   PetscFunctionBegin;
1831*87f4e208SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1832*87f4e208SHong Zhang   PetscValidHeaderSpecific(is,IS_CLASSID,3);
1833*87f4e208SHong Zhang   ierr = PetscObjectReference((PetscObject)is);CHKERRQ(ierr);
1834*87f4e208SHong Zhang   if (!ts->num_rhs_splits) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Call TSSetRHSSplits() to set the Number of splits first");
1835*87f4e208SHong Zhang   if (ind > ts->num_rhs_splits || ind <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Second argument is out of range");
1836*87f4e208SHong Zhang   ierr = ISDestroy(&ts->iss[ind-1]);CHKERRQ(ierr);
1837*87f4e208SHong Zhang   ts->iss[ind-1] = is;
1838*87f4e208SHong Zhang   PetscFunctionReturn(0);
1839*87f4e208SHong Zhang }
1840*87f4e208SHong Zhang 
1841*87f4e208SHong Zhang /*@C
1842*87f4e208SHong Zhang  TSSetRHSSplitFunction - Set the split right-hand-side functions.
1843*87f4e208SHong Zhang 
1844*87f4e208SHong Zhang  Logically Collective on TS
1845*87f4e208SHong Zhang 
1846*87f4e208SHong Zhang  Input Parameters:
1847*87f4e208SHong Zhang  +  ts      - the TS context obtained from TSCreate()
1848*87f4e208SHong Zhang  .  ind     - the index of split RHS function
1849d9194312SHong Zhang  .  r       - vector to hold the residual (or NULL to have it created internally)
1850*87f4e208SHong Zhang  .  rhsfunc - the RHS function evaluation routine
1851*87f4e208SHong Zhang  -  ctx     - user-defined context for private data for the split function evaluation routine (may be NULL)
1852d9194312SHong Zhang 
1853d9194312SHong Zhang  Calling sequence of fun:
1854*87f4e208SHong Zhang  $  rhsfunc(TS ts,PetscReal t,Vec u,Vec f,ctx);
1855d9194312SHong Zhang 
1856d9194312SHong Zhang  +  t    - time at step/stage being solved
1857d9194312SHong Zhang  .  u    - state vector
1858d9194312SHong Zhang  .  f    - function vector
1859d9194312SHong Zhang  -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1860d9194312SHong Zhang 
1861d9194312SHong Zhang  Level: beginner
1862d9194312SHong Zhang 
1863d9194312SHong Zhang  .keywords: TS, timestep, set, ODE, Hamiltonian, Function
1864d9194312SHong Zhang 
1865*87f4e208SHong Zhang  .seealso: TSGetRHSSplitFunction()
1866d9194312SHong Zhang  @*/
1867*87f4e208SHong Zhang PetscErrorCode TSSetRHSSplitFunction(TS ts,PetscInt ind,Vec r,TSRHSFunction rhsfunc,void *ctx)
1868d9194312SHong Zhang {
1869d9194312SHong Zhang   DM             dm;
1870d9194312SHong Zhang   SNES           snes;
1871d9194312SHong Zhang   Vec            ralloc = NULL;
1872d9194312SHong Zhang   PetscErrorCode ierr;
1873d9194312SHong Zhang 
1874d9194312SHong Zhang   PetscFunctionBegin;
1875d9194312SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1876d9194312SHong Zhang   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
1877d9194312SHong Zhang 
1878d9194312SHong Zhang   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1879*87f4e208SHong Zhang   ierr = DMTSSetRHSSplitFunction(dm,ind,rhsfunc,ctx);CHKERRQ(ierr);
1880d9194312SHong Zhang 
1881d9194312SHong Zhang   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1882d9194312SHong Zhang   if (!r && !ts->dm && ts->vec_sol) {
1883d9194312SHong Zhang     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1884d9194312SHong Zhang     r = ralloc;
1885d9194312SHong Zhang   }
1886d9194312SHong Zhang   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1887d9194312SHong Zhang   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1888d9194312SHong Zhang   PetscFunctionReturn(0);
1889d9194312SHong Zhang }
1890d9194312SHong Zhang 
1891d9194312SHong Zhang /*@C
1892*87f4e208SHong Zhang  TSGetRHSSplitFunction - Returns the vector where the split RHS residual is stored and the functions to compute it.
1893d9194312SHong Zhang 
1894d9194312SHong Zhang  Not Collective
1895d9194312SHong Zhang 
1896d9194312SHong Zhang  Input Parameter:
1897d9194312SHong Zhang  . ts - the TS context
1898d9194312SHong Zhang 
1899d9194312SHong Zhang  Output Parameter:
1900d9194312SHong Zhang  + r       - vector to hold residual (or NULL)
1901*87f4e208SHong Zhang  . rhsfunc - the function to compute part of the residual (or NULL)
1902d9194312SHong Zhang  - ctx     - the function context (or NULL)
1903d9194312SHong Zhang 
1904d9194312SHong Zhang  Level: advanced
1905d9194312SHong Zhang 
1906d9194312SHong Zhang  .keywords: TS, nonlinear, get, function
1907d9194312SHong Zhang 
1908*87f4e208SHong Zhang  .seealso: TSSetRHSSplitFunction(), SNESGetFunction()
1909d9194312SHong Zhang  @*/
1910*87f4e208SHong Zhang PetscErrorCode TSGetRHSSplitFunction(TS ts,PetscInt ind,Vec *r,TSRHSFunction *rhsfunc,void **ctx)
1911d9194312SHong Zhang {
1912d9194312SHong Zhang   PetscErrorCode ierr;
1913d9194312SHong Zhang   SNES           snes;
1914d9194312SHong Zhang   DM             dm;
1915d9194312SHong Zhang 
1916d9194312SHong Zhang   PetscFunctionBegin;
1917d9194312SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1918d9194312SHong Zhang   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1919d9194312SHong Zhang   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1920d9194312SHong Zhang   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1921*87f4e208SHong Zhang   ierr = DMTSGetRHSSplitFunction(dm,ind,rhsfunc,ctx);CHKERRQ(ierr);
1922d9194312SHong Zhang   PetscFunctionReturn(0);
1923d9194312SHong Zhang }
1924d9194312SHong Zhang 
1925d9194312SHong Zhang /*@
1926*87f4e208SHong Zhang  TSComputeRHSSplitFunction - Evaluates the right-hand-side function.
1927d9194312SHong Zhang 
1928d9194312SHong Zhang  Collective on TS and Vec
1929d9194312SHong Zhang 
1930d9194312SHong Zhang  Input Parameters:
1931d9194312SHong Zhang  +  ts - the TS context
1932d9194312SHong Zhang  .  t - current time
1933d9194312SHong Zhang  -  U - state vector
1934d9194312SHong Zhang 
1935d9194312SHong Zhang  Output Parameter:
1936d9194312SHong Zhang  .  y - right hand side
1937d9194312SHong Zhang 
1938d9194312SHong Zhang  Note:
1939d9194312SHong Zhang  Most users should not need to explicitly call this routine, as it
1940d9194312SHong Zhang  is used internally within the nonlinear solvers.
1941d9194312SHong Zhang 
1942d9194312SHong Zhang  Level: developer
1943d9194312SHong Zhang 
1944d9194312SHong Zhang  .keywords: TS, compute
1945d9194312SHong Zhang 
1946*87f4e208SHong Zhang  .seealso: TSSetRHSSplitFunction()
1947d9194312SHong Zhang  @*/
1948*87f4e208SHong Zhang PetscErrorCode TSComputeRHSSplitFunction(TS ts,PetscInt ind,PetscReal t,Vec U,Vec y)
1949d9194312SHong Zhang {
1950d9194312SHong Zhang   PetscErrorCode ierr;
1951*87f4e208SHong Zhang   TSRHSFunction  rhsfunc;
1952d9194312SHong Zhang   void           *ctx;
1953d9194312SHong Zhang   DM             dm;
1954d9194312SHong Zhang 
1955d9194312SHong Zhang   PetscFunctionBegin;
1956d9194312SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1957d9194312SHong Zhang   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1958d9194312SHong Zhang   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
1959d9194312SHong Zhang   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1960d9194312SHong Zhang 
1961*87f4e208SHong Zhang   ierr = DMTSGetRHSSplitFunction(dm,ind,&rhsfunc,&ctx);CHKERRQ(ierr);
1962d9194312SHong Zhang 
1963*87f4e208SHong Zhang   if (!rhsfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSSplitFunction()");
1964d9194312SHong Zhang 
1965d9194312SHong Zhang   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
1966d9194312SHong Zhang 
1967d9194312SHong Zhang   PetscStackPush("TS user right-hand-side function");
1968*87f4e208SHong Zhang   ierr = (*rhsfunc)(ts,t,U,y,ctx);CHKERRQ(ierr);
1969d9194312SHong Zhang   PetscStackPop;
1970d9194312SHong Zhang 
1971d9194312SHong Zhang   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
1972d9194312SHong Zhang   PetscFunctionReturn(0);
1973d9194312SHong Zhang }
1974d9194312SHong Zhang 
1975d9194312SHong Zhang /*@C
197655849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
197755849f57SBarry Smith 
197855849f57SBarry Smith   Collective on PetscViewer
197955849f57SBarry Smith 
198055849f57SBarry Smith   Input Parameters:
198155849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
198255849f57SBarry Smith            some related function before a call to TSLoad().
198355849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
198455849f57SBarry Smith 
198555849f57SBarry Smith    Level: intermediate
198655849f57SBarry Smith 
198755849f57SBarry Smith   Notes:
198855849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
198955849f57SBarry Smith 
199055849f57SBarry Smith   Notes for advanced users:
199155849f57SBarry Smith   Most users should not need to know the details of the binary storage
199255849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
199355849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
199455849f57SBarry Smith   format is
199555849f57SBarry Smith .vb
199655849f57SBarry Smith      has not yet been determined
199755849f57SBarry Smith .ve
199855849f57SBarry Smith 
199955849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
200055849f57SBarry Smith @*/
2001f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
200255849f57SBarry Smith {
200355849f57SBarry Smith   PetscErrorCode ierr;
200455849f57SBarry Smith   PetscBool      isbinary;
200555849f57SBarry Smith   PetscInt       classid;
200655849f57SBarry Smith   char           type[256];
20072d53ad75SBarry Smith   DMTS           sdm;
2008ad6bc421SBarry Smith   DM             dm;
200955849f57SBarry Smith 
201055849f57SBarry Smith   PetscFunctionBegin;
2011f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
201255849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
201355849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
201455849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
201555849f57SBarry Smith 
2016060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
2017ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
2018060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
2019f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
2020f2c2a1b9SBarry Smith   if (ts->ops->load) {
2021f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
2022f2c2a1b9SBarry Smith   }
2023ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
2024ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
2025ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
2026f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
2027f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
20282d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
20292d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
203055849f57SBarry Smith   PetscFunctionReturn(0);
203155849f57SBarry Smith }
203255849f57SBarry Smith 
20339804daf3SBarry Smith #include <petscdraw.h>
2034e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2035e04113cfSBarry Smith #include <petscviewersaws.h>
2036f05ece33SBarry Smith #endif
20377e2c5f70SBarry Smith /*@C
2038d763cef2SBarry Smith     TSView - Prints the TS data structure.
2039d763cef2SBarry Smith 
20404c49b128SBarry Smith     Collective on TS
2041d763cef2SBarry Smith 
2042d763cef2SBarry Smith     Input Parameters:
2043d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
2044d763cef2SBarry Smith -   viewer - visualization context
2045d763cef2SBarry Smith 
2046d763cef2SBarry Smith     Options Database Key:
2047d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
2048d763cef2SBarry Smith 
2049d763cef2SBarry Smith     Notes:
2050d763cef2SBarry Smith     The available visualization contexts include
2051b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
2052b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
2053d763cef2SBarry Smith          output where only the first processor opens
2054d763cef2SBarry Smith          the file.  All other processors send their
2055d763cef2SBarry Smith          data to the first processor to print.
2056d763cef2SBarry Smith 
2057d763cef2SBarry Smith     The user can open an alternative visualization context with
2058b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
2059d763cef2SBarry Smith 
2060d763cef2SBarry Smith     Level: beginner
2061d763cef2SBarry Smith 
2062d763cef2SBarry Smith .keywords: TS, timestep, view
2063d763cef2SBarry Smith 
2064b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
2065d763cef2SBarry Smith @*/
20667087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
2067d763cef2SBarry Smith {
2068dfbe8321SBarry Smith   PetscErrorCode ierr;
206919fd82e9SBarry Smith   TSType         type;
20702b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
20712d53ad75SBarry Smith   DMTS           sdm;
2072e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2073536b137fSBarry Smith   PetscBool      issaws;
2074f05ece33SBarry Smith #endif
2075d763cef2SBarry Smith 
2076d763cef2SBarry Smith   PetscFunctionBegin;
20770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20783050cee2SBarry Smith   if (!viewer) {
2079ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
20803050cee2SBarry Smith   }
20810700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2082c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
2083fd16b177SBarry Smith 
2084251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
2085251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
208655849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
20872b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
2088e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2089536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
2090f05ece33SBarry Smith #endif
209132077d6dSBarry Smith   if (iascii) {
2092dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
2093efd4aadfSBarry Smith     if (ts->ops->view) {
2094efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2095efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
2096efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2097efd4aadfSBarry Smith     }
2098ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
209977431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
2100ef85077eSLisandro Dalcin     }
2101ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
21027c8652ddSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
2103ef85077eSLisandro Dalcin     }
2104efd4aadfSBarry Smith     if (ts->usessnes) {
2105efd4aadfSBarry Smith       PetscBool lin;
2106d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
21075ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
2108d763cef2SBarry Smith       }
21095ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
2110efd4aadfSBarry Smith       ierr = PetscObjectTypeCompare((PetscObject)ts->snes,SNESKSPONLY,&lin);CHKERRQ(ierr);
2111efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
2112efd4aadfSBarry Smith     }
2113193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
2114a0af407cSBarry Smith     if (ts->vrtol) {
2115a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
2116a0af407cSBarry Smith     } else {
2117a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
2118a0af407cSBarry Smith     }
2119a0af407cSBarry Smith     if (ts->vatol) {
2120a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
2121a0af407cSBarry Smith     } else {
2122a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
2123a0af407cSBarry Smith     }
2124825ab935SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2125efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
2126825ab935SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2127825ab935SBarry Smith     if (ts->snes && ts->usessnes)  {
2128825ab935SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2129825ab935SBarry Smith       ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
2130825ab935SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2131825ab935SBarry Smith     }
21322d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
21332d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
21340f5bd95cSBarry Smith   } else if (isstring) {
2135a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
2136b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
213755849f57SBarry Smith   } else if (isbinary) {
213855849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
213955849f57SBarry Smith     MPI_Comm    comm;
214055849f57SBarry Smith     PetscMPIInt rank;
214155849f57SBarry Smith     char        type[256];
214255849f57SBarry Smith 
214355849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
214455849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
214555849f57SBarry Smith     if (!rank) {
214655849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
214755849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
214855849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
214955849f57SBarry Smith     }
215055849f57SBarry Smith     if (ts->ops->view) {
215155849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
215255849f57SBarry Smith     }
2153efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2154f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
2155f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
21562d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
21572d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
21582b0a91c0SBarry Smith   } else if (isdraw) {
21592b0a91c0SBarry Smith     PetscDraw draw;
21602b0a91c0SBarry Smith     char      str[36];
216189fd9fafSBarry Smith     PetscReal x,y,bottom,h;
21622b0a91c0SBarry Smith 
21632b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
21642b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
21652b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
21662b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
216751fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
216889fd9fafSBarry Smith     bottom = y - h;
21692b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
21702b0a91c0SBarry Smith     if (ts->ops->view) {
21712b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
21722b0a91c0SBarry Smith     }
2173efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2174efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
21752b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2176e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2177536b137fSBarry Smith   } else if (issaws) {
2178d45a07a7SBarry Smith     PetscMPIInt rank;
21792657e9d9SBarry Smith     const char  *name;
21802657e9d9SBarry Smith 
21812657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
2182d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
2183d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
2184d45a07a7SBarry Smith       char       dir[1024];
2185d45a07a7SBarry Smith 
2186e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2187a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
21882657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
21892657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
21902657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2191d763cef2SBarry Smith     }
21920acecf5bSBarry Smith     if (ts->ops->view) {
21930acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
21940acecf5bSBarry Smith     }
2195f05ece33SBarry Smith #endif
2196f05ece33SBarry Smith   }
2197f05ece33SBarry Smith 
2198b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2199251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2200b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2201d763cef2SBarry Smith   PetscFunctionReturn(0);
2202d763cef2SBarry Smith }
2203d763cef2SBarry Smith 
2204b07ff414SBarry Smith /*@
2205d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2206d763cef2SBarry Smith    the timesteppers.
2207d763cef2SBarry Smith 
22083f9fe445SBarry Smith    Logically Collective on TS
2209d763cef2SBarry Smith 
2210d763cef2SBarry Smith    Input Parameters:
2211d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2212d763cef2SBarry Smith -  usrP - optional user context
2213d763cef2SBarry Smith 
221495452b02SPatrick Sanan    Fortran Notes:
221595452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2216daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2217daf670e6SBarry Smith 
2218d763cef2SBarry Smith    Level: intermediate
2219d763cef2SBarry Smith 
2220d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
2221d763cef2SBarry Smith 
2222d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2223d763cef2SBarry Smith @*/
22247087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2225d763cef2SBarry Smith {
2226d763cef2SBarry Smith   PetscFunctionBegin;
22270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2228d763cef2SBarry Smith   ts->user = usrP;
2229d763cef2SBarry Smith   PetscFunctionReturn(0);
2230d763cef2SBarry Smith }
2231d763cef2SBarry Smith 
2232b07ff414SBarry Smith /*@
2233d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2234d763cef2SBarry Smith     timestepper.
2235d763cef2SBarry Smith 
2236d763cef2SBarry Smith     Not Collective
2237d763cef2SBarry Smith 
2238d763cef2SBarry Smith     Input Parameter:
2239d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2240d763cef2SBarry Smith 
2241d763cef2SBarry Smith     Output Parameter:
2242d763cef2SBarry Smith .   usrP - user context
2243d763cef2SBarry Smith 
224495452b02SPatrick Sanan    Fortran Notes:
224595452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2246daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2247daf670e6SBarry Smith 
2248d763cef2SBarry Smith     Level: intermediate
2249d763cef2SBarry Smith 
2250d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
2251d763cef2SBarry Smith 
2252d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2253d763cef2SBarry Smith @*/
2254e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2255d763cef2SBarry Smith {
2256d763cef2SBarry Smith   PetscFunctionBegin;
22570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2258e71120c6SJed Brown   *(void**)usrP = ts->user;
2259d763cef2SBarry Smith   PetscFunctionReturn(0);
2260d763cef2SBarry Smith }
2261d763cef2SBarry Smith 
2262d763cef2SBarry Smith /*@
226380275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2264d763cef2SBarry Smith 
2265d763cef2SBarry Smith    Not Collective
2266d763cef2SBarry Smith 
2267d763cef2SBarry Smith    Input Parameter:
2268d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2269d763cef2SBarry Smith 
2270d763cef2SBarry Smith    Output Parameter:
227180275a0aSLisandro Dalcin .  steps - number of steps completed so far
2272d763cef2SBarry Smith 
2273d763cef2SBarry Smith    Level: intermediate
2274d763cef2SBarry Smith 
2275d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
22769be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2277d763cef2SBarry Smith @*/
227880275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2279d763cef2SBarry Smith {
2280d763cef2SBarry Smith   PetscFunctionBegin;
22810700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
228280275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
228380275a0aSLisandro Dalcin   *steps = ts->steps;
228480275a0aSLisandro Dalcin   PetscFunctionReturn(0);
228580275a0aSLisandro Dalcin }
228680275a0aSLisandro Dalcin 
228780275a0aSLisandro Dalcin /*@
228880275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
228980275a0aSLisandro Dalcin 
229080275a0aSLisandro Dalcin    Logically Collective on TS
229180275a0aSLisandro Dalcin 
229280275a0aSLisandro Dalcin    Input Parameters:
229380275a0aSLisandro Dalcin +  ts - the TS context
229480275a0aSLisandro Dalcin -  steps - number of steps completed so far
229580275a0aSLisandro Dalcin 
229680275a0aSLisandro Dalcin    Notes:
229780275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
229880275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
229980275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
230080275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
230180275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
230280275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
230380275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
230480275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
230580275a0aSLisandro Dalcin 
230680275a0aSLisandro Dalcin    Level: advanced
230780275a0aSLisandro Dalcin 
230880275a0aSLisandro Dalcin .keywords: TS, timestep, set, iteration, number
230980275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
231080275a0aSLisandro Dalcin @*/
231180275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
231280275a0aSLisandro Dalcin {
231380275a0aSLisandro Dalcin   PetscFunctionBegin;
231480275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
231580275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
231680275a0aSLisandro Dalcin   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
231780275a0aSLisandro Dalcin   ts->steps = steps;
2318d763cef2SBarry Smith   PetscFunctionReturn(0);
2319d763cef2SBarry Smith }
2320d763cef2SBarry Smith 
2321d763cef2SBarry Smith /*@
2322d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2323d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2324d763cef2SBarry Smith 
23253f9fe445SBarry Smith    Logically Collective on TS
2326d763cef2SBarry Smith 
2327d763cef2SBarry Smith    Input Parameters:
2328d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2329d763cef2SBarry Smith -  time_step - the size of the timestep
2330d763cef2SBarry Smith 
2331d763cef2SBarry Smith    Level: intermediate
2332d763cef2SBarry Smith 
2333aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2334d763cef2SBarry Smith 
2335d763cef2SBarry Smith .keywords: TS, set, timestep
2336d763cef2SBarry Smith @*/
23377087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2338d763cef2SBarry Smith {
2339d763cef2SBarry Smith   PetscFunctionBegin;
23400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2341c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2342d763cef2SBarry Smith   ts->time_step = time_step;
2343d763cef2SBarry Smith   PetscFunctionReturn(0);
2344d763cef2SBarry Smith }
2345d763cef2SBarry Smith 
2346a43b19c4SJed Brown /*@
234749354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
234849354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
234949354f04SShri Abhyankar      or just return at the final time TS computed.
2350a43b19c4SJed Brown 
2351a43b19c4SJed Brown   Logically Collective on TS
2352a43b19c4SJed Brown 
2353a43b19c4SJed Brown    Input Parameter:
2354a43b19c4SJed Brown +   ts - the time-step context
235549354f04SShri Abhyankar -   eftopt - exact final time option
2356a43b19c4SJed Brown 
2357feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2358feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2359feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2360feed9e9dSBarry Smith 
2361feed9e9dSBarry Smith    Options Database:
2362feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2363feed9e9dSBarry Smith 
2364ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2365ee346746SBarry Smith     then the final time you selected.
2366ee346746SBarry Smith 
2367a43b19c4SJed Brown    Level: beginner
2368a43b19c4SJed Brown 
2369f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2370a43b19c4SJed Brown @*/
237149354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2372a43b19c4SJed Brown {
2373a43b19c4SJed Brown   PetscFunctionBegin;
2374a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
237549354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
237649354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2377a43b19c4SJed Brown   PetscFunctionReturn(0);
2378a43b19c4SJed Brown }
2379a43b19c4SJed Brown 
2380d763cef2SBarry Smith /*@
2381f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2382f6953c82SLisandro Dalcin 
2383f6953c82SLisandro Dalcin    Not Collective
2384f6953c82SLisandro Dalcin 
2385f6953c82SLisandro Dalcin    Input Parameter:
2386f6953c82SLisandro Dalcin .  ts - the TS context
2387f6953c82SLisandro Dalcin 
2388f6953c82SLisandro Dalcin    Output Parameter:
2389f6953c82SLisandro Dalcin .  eftopt - exact final time option
2390f6953c82SLisandro Dalcin 
2391f6953c82SLisandro Dalcin    Level: beginner
2392f6953c82SLisandro Dalcin 
2393f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2394f6953c82SLisandro Dalcin @*/
2395f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2396f6953c82SLisandro Dalcin {
2397f6953c82SLisandro Dalcin   PetscFunctionBegin;
2398f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2399f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2400f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2401f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2402f6953c82SLisandro Dalcin }
2403f6953c82SLisandro Dalcin 
2404f6953c82SLisandro Dalcin /*@
2405d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2406d763cef2SBarry Smith 
2407d763cef2SBarry Smith    Not Collective
2408d763cef2SBarry Smith 
2409d763cef2SBarry Smith    Input Parameter:
2410d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2411d763cef2SBarry Smith 
2412d763cef2SBarry Smith    Output Parameter:
2413d763cef2SBarry Smith .  dt - the current timestep size
2414d763cef2SBarry Smith 
2415d763cef2SBarry Smith    Level: intermediate
2416d763cef2SBarry Smith 
2417aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2418d763cef2SBarry Smith 
2419d763cef2SBarry Smith .keywords: TS, get, timestep
2420d763cef2SBarry Smith @*/
24217087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2422d763cef2SBarry Smith {
2423d763cef2SBarry Smith   PetscFunctionBegin;
24240700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2425f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2426d763cef2SBarry Smith   *dt = ts->time_step;
2427d763cef2SBarry Smith   PetscFunctionReturn(0);
2428d763cef2SBarry Smith }
2429d763cef2SBarry Smith 
2430d8e5e3e6SSatish Balay /*@
2431d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2432d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2433d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2434d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2435d763cef2SBarry Smith 
2436d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2437d763cef2SBarry Smith 
2438d763cef2SBarry Smith    Input Parameter:
2439d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2440d763cef2SBarry Smith 
2441d763cef2SBarry Smith    Output Parameter:
2442d763cef2SBarry Smith .  v - the vector containing the solution
2443d763cef2SBarry Smith 
244463e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
244563e21af5SBarry Smith    final time. It returns the solution at the next timestep.
244663e21af5SBarry Smith 
2447d763cef2SBarry Smith    Level: intermediate
2448d763cef2SBarry Smith 
24490ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2450d763cef2SBarry Smith 
2451d763cef2SBarry Smith .keywords: TS, timestep, get, solution
2452d763cef2SBarry Smith @*/
24537087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2454d763cef2SBarry Smith {
2455d763cef2SBarry Smith   PetscFunctionBegin;
24560700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24574482741eSBarry Smith   PetscValidPointer(v,2);
24588737fe31SLisandro Dalcin   *v = ts->vec_sol;
2459d763cef2SBarry Smith   PetscFunctionReturn(0);
2460d763cef2SBarry Smith }
2461d763cef2SBarry Smith 
246203fe5f5eSDebojyoti Ghosh /*@
2463b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
246403fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2465b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
246603fe5f5eSDebojyoti Ghosh    structure as the solution vector.
246703fe5f5eSDebojyoti Ghosh 
246803fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
246903fe5f5eSDebojyoti Ghosh 
247003fe5f5eSDebojyoti Ghosh    Parameters :
247103fe5f5eSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2472b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2473b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
247403fe5f5eSDebojyoti Ghosh        returned in v.
2475b2bf4f3aSDebojyoti Ghosh .  v - the vector containing the n-th solution component
247603fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2477b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
247803fe5f5eSDebojyoti Ghosh 
24794cdd57e5SDebojyoti Ghosh    Level: advanced
248003fe5f5eSDebojyoti Ghosh 
248103fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
248203fe5f5eSDebojyoti Ghosh 
248303fe5f5eSDebojyoti Ghosh .keywords: TS, timestep, get, solution
248403fe5f5eSDebojyoti Ghosh @*/
2485b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
248603fe5f5eSDebojyoti Ghosh {
248703fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
248803fe5f5eSDebojyoti Ghosh 
248903fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
249003fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2491b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
249203fe5f5eSDebojyoti Ghosh   else {
2493b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
249403fe5f5eSDebojyoti Ghosh   }
249503fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
249603fe5f5eSDebojyoti Ghosh }
249703fe5f5eSDebojyoti Ghosh 
24984cdd57e5SDebojyoti Ghosh /*@
24994cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
25004cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
25014cdd57e5SDebojyoti Ghosh 
25024cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
25034cdd57e5SDebojyoti Ghosh 
25044cdd57e5SDebojyoti Ghosh    Parameters :
25054cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
25064cdd57e5SDebojyoti Ghosh .  v - the vector containing the auxiliary solution
25074cdd57e5SDebojyoti Ghosh 
25084cdd57e5SDebojyoti Ghosh    Level: intermediate
25094cdd57e5SDebojyoti Ghosh 
25104cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
25114cdd57e5SDebojyoti Ghosh 
25124cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, solution
25134cdd57e5SDebojyoti Ghosh @*/
25144cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
25154cdd57e5SDebojyoti Ghosh {
25164cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
25174cdd57e5SDebojyoti Ghosh 
25184cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
25194cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2520f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
25214cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2522f6356ec7SDebojyoti Ghosh   } else {
2523f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v); CHKERRQ(ierr);
2524f6356ec7SDebojyoti Ghosh   }
25254cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
25264cdd57e5SDebojyoti Ghosh }
25274cdd57e5SDebojyoti Ghosh 
25284cdd57e5SDebojyoti Ghosh /*@
25294cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
25304cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
25314cdd57e5SDebojyoti Ghosh 
25324cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
25334cdd57e5SDebojyoti Ghosh 
25349657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
25359657682dSDebojyoti Ghosh 
25364cdd57e5SDebojyoti Ghosh    Parameters :
25374cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2538657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
25394cdd57e5SDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
25404cdd57e5SDebojyoti Ghosh 
25414cdd57e5SDebojyoti Ghosh    Level: intermediate
25424cdd57e5SDebojyoti Ghosh 
254357df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
25444cdd57e5SDebojyoti Ghosh 
25454cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, error
25464cdd57e5SDebojyoti Ghosh @*/
25470a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
25484cdd57e5SDebojyoti Ghosh {
25494cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
25504cdd57e5SDebojyoti Ghosh 
25514cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
25524cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2553f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
25540a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2555f6356ec7SDebojyoti Ghosh   } else {
2556f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2557f6356ec7SDebojyoti Ghosh   }
25584cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
25594cdd57e5SDebojyoti Ghosh }
25604cdd57e5SDebojyoti Ghosh 
256157df6a1bSDebojyoti Ghosh /*@
256257df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
256357df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
256457df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
256557df6a1bSDebojyoti Ghosh 
256657df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
256757df6a1bSDebojyoti Ghosh 
256857df6a1bSDebojyoti Ghosh    Parameters :
256957df6a1bSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
257057df6a1bSDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
257157df6a1bSDebojyoti Ghosh 
257257df6a1bSDebojyoti Ghosh    Level: intermediate
257357df6a1bSDebojyoti Ghosh 
257457df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
257557df6a1bSDebojyoti Ghosh 
257657df6a1bSDebojyoti Ghosh .keywords: TS, timestep, get, error
257757df6a1bSDebojyoti Ghosh @*/
257857df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
257957df6a1bSDebojyoti Ghosh {
258057df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
258157df6a1bSDebojyoti Ghosh 
258257df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
258357df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25849657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
258557df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
258657df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
258757df6a1bSDebojyoti Ghosh   }
258857df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
258957df6a1bSDebojyoti Ghosh }
259057df6a1bSDebojyoti Ghosh 
2591bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2592d8e5e3e6SSatish Balay /*@
2593bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2594d763cef2SBarry Smith 
2595bdad233fSMatthew Knepley   Not collective
2596d763cef2SBarry Smith 
2597bdad233fSMatthew Knepley   Input Parameters:
2598bdad233fSMatthew Knepley + ts   - The TS
2599bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2600d763cef2SBarry Smith .vb
26010910c330SBarry Smith          U_t - A U = 0      (linear)
26020910c330SBarry Smith          U_t - A(t) U = 0   (linear)
26030910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2604d763cef2SBarry Smith .ve
2605d763cef2SBarry Smith 
2606d763cef2SBarry Smith    Level: beginner
2607d763cef2SBarry Smith 
2608bdad233fSMatthew Knepley .keywords: TS, problem type
2609bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2610d763cef2SBarry Smith @*/
26117087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2612a7cc72afSBarry Smith {
26139e2a6581SJed Brown   PetscErrorCode ierr;
26149e2a6581SJed Brown 
2615d763cef2SBarry Smith   PetscFunctionBegin;
26160700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2617bdad233fSMatthew Knepley   ts->problem_type = type;
26189e2a6581SJed Brown   if (type == TS_LINEAR) {
26199e2a6581SJed Brown     SNES snes;
26209e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
26219e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
26229e2a6581SJed Brown   }
2623d763cef2SBarry Smith   PetscFunctionReturn(0);
2624d763cef2SBarry Smith }
2625d763cef2SBarry Smith 
2626bdad233fSMatthew Knepley /*@C
2627bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2628bdad233fSMatthew Knepley 
2629bdad233fSMatthew Knepley   Not collective
2630bdad233fSMatthew Knepley 
2631bdad233fSMatthew Knepley   Input Parameter:
2632bdad233fSMatthew Knepley . ts   - The TS
2633bdad233fSMatthew Knepley 
2634bdad233fSMatthew Knepley   Output Parameter:
2635bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2636bdad233fSMatthew Knepley .vb
2637089b2837SJed Brown          M U_t = A U
2638089b2837SJed Brown          M(t) U_t = A(t) U
2639b5abc632SBarry Smith          F(t,U,U_t)
2640bdad233fSMatthew Knepley .ve
2641bdad233fSMatthew Knepley 
2642bdad233fSMatthew Knepley    Level: beginner
2643bdad233fSMatthew Knepley 
2644bdad233fSMatthew Knepley .keywords: TS, problem type
2645bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2646bdad233fSMatthew Knepley @*/
26477087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2648a7cc72afSBarry Smith {
2649bdad233fSMatthew Knepley   PetscFunctionBegin;
26500700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
26514482741eSBarry Smith   PetscValidIntPointer(type,2);
2652bdad233fSMatthew Knepley   *type = ts->problem_type;
2653bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2654bdad233fSMatthew Knepley }
2655d763cef2SBarry Smith 
2656d763cef2SBarry Smith /*@
2657d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
2658d763cef2SBarry Smith    of a timestepper.
2659d763cef2SBarry Smith 
2660d763cef2SBarry Smith    Collective on TS
2661d763cef2SBarry Smith 
2662d763cef2SBarry Smith    Input Parameter:
2663d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2664d763cef2SBarry Smith 
2665d763cef2SBarry Smith    Notes:
2666d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2667d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2668141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2669d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2670141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2671d763cef2SBarry Smith 
2672d763cef2SBarry Smith    Level: advanced
2673d763cef2SBarry Smith 
2674d763cef2SBarry Smith .keywords: TS, timestep, setup
2675d763cef2SBarry Smith 
2676141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2677d763cef2SBarry Smith @*/
26787087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2679d763cef2SBarry Smith {
2680dfbe8321SBarry Smith   PetscErrorCode ierr;
26816c6b9e74SPeter Brune   DM             dm;
26826c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2683d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2684cd11d68dSLisandro Dalcin   TSIFunction    ifun;
26856c6b9e74SPeter Brune   TSIJacobian    ijac;
2686efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
26876c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
26882ffb9264SLisandro Dalcin   PetscBool      isnone;
2689d763cef2SBarry Smith 
2690d763cef2SBarry Smith   PetscFunctionBegin;
26910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2692277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2693277b19d0SLisandro Dalcin 
26947adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2695cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2696cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2697d763cef2SBarry Smith   }
2698277b19d0SLisandro Dalcin 
2699277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
2700277b19d0SLisandro Dalcin 
2701e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
2702e1244c69SJed Brown     Mat Amat,Pmat;
2703e1244c69SJed Brown     SNES snes;
2704e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2705e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2706e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2707e1244c69SJed Brown      * have displaced the RHS matrix */
2708e1244c69SJed Brown     if (Amat == ts->Arhs) {
2709abc0d4abSBarry Smith       /* we need to copy the values of the matrix because for the constant Jacobian case the user will never set the numerical values in this new location */
2710abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2711e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2712e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2713e1244c69SJed Brown     }
2714e1244c69SJed Brown     if (Pmat == ts->Brhs) {
2715abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2716e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2717e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2718e1244c69SJed Brown     }
2719e1244c69SJed Brown   }
27202ffb9264SLisandro Dalcin 
27212ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
27222ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
27232ffb9264SLisandro Dalcin 
2724277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2725000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2726277b19d0SLisandro Dalcin   }
2727277b19d0SLisandro Dalcin 
27282ffb9264SLisandro Dalcin   /* Attempt to check/preset a default value for the exact final time option */
27292ffb9264SLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
27302ffb9264SLisandro Dalcin   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED)
27312ffb9264SLisandro Dalcin     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
27322ffb9264SLisandro Dalcin 
2733a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
27346c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
27356c6b9e74SPeter Brune    */
27366c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
27370298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
27386c6b9e74SPeter Brune   if (!func) {
27396c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
27406c6b9e74SPeter Brune   }
2741a6772fa2SLisandro Dalcin   /* If the SNES doesn't have a jacobian set and the TS has an ijacobian or rhsjacobian set, set the SNES to use it.
27426c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
27436c6b9e74SPeter Brune    */
27440298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
27450298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2746efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
27470298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2748efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
27496c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
27506c6b9e74SPeter Brune   }
2751c0517034SDebojyoti Ghosh 
2752c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2753c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2754c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2755c0517034SDebojyoti Ghosh   }
2756c0517034SDebojyoti Ghosh 
2757277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2758277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2759277b19d0SLisandro Dalcin }
2760277b19d0SLisandro Dalcin 
2761f6a906c0SBarry Smith /*@
2762277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2763277b19d0SLisandro Dalcin 
2764277b19d0SLisandro Dalcin    Collective on TS
2765277b19d0SLisandro Dalcin 
2766277b19d0SLisandro Dalcin    Input Parameter:
2767277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2768277b19d0SLisandro Dalcin 
2769277b19d0SLisandro Dalcin    Level: beginner
2770277b19d0SLisandro Dalcin 
2771277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
2772277b19d0SLisandro Dalcin 
2773277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2774277b19d0SLisandro Dalcin @*/
2775277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2776277b19d0SLisandro Dalcin {
2777*87f4e208SHong Zhang   PetscInt       i;
2778277b19d0SLisandro Dalcin   PetscErrorCode ierr;
2779277b19d0SLisandro Dalcin 
2780277b19d0SLisandro Dalcin   PetscFunctionBegin;
2781277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2782b18ea86cSHong Zhang 
2783277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2784277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2785277b19d0SLisandro Dalcin   }
2786277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2787e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2788bbd56ea5SKarl Rupp 
27894e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
27904e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2791214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
27926bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2793efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2794e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2795e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
279638637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2797bbd56ea5SKarl Rupp 
2798d8151eeaSBarry Smith   ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2799d8151eeaSBarry Smith   ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2800715f1b00SHong Zhang 
2801ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
28022c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
280336eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
280413b1b0ffSHong Zhang   ierr = MatDestroy(&ts->mat_sensip);CHKERRQ(ierr);
2805022c081aSHong Zhang 
2806*87f4e208SHong Zhang   for (i=0; i<ts->num_rhs_splits; i++) {
2807*87f4e208SHong Zhang     ierr = ISDestroy(&ts->iss[i]);CHKERRQ(ierr);
2808*87f4e208SHong Zhang   }
2809277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2810d763cef2SBarry Smith   PetscFunctionReturn(0);
2811d763cef2SBarry Smith }
2812d763cef2SBarry Smith 
2813d8e5e3e6SSatish Balay /*@
2814d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2815d763cef2SBarry Smith    with TSCreate().
2816d763cef2SBarry Smith 
2817d763cef2SBarry Smith    Collective on TS
2818d763cef2SBarry Smith 
2819d763cef2SBarry Smith    Input Parameter:
2820d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2821d763cef2SBarry Smith 
2822d763cef2SBarry Smith    Level: beginner
2823d763cef2SBarry Smith 
2824d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2825d763cef2SBarry Smith 
2826d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2827d763cef2SBarry Smith @*/
28286bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2829d763cef2SBarry Smith {
28306849ba73SBarry Smith   PetscErrorCode ierr;
2831d763cef2SBarry Smith 
2832d763cef2SBarry Smith   PetscFunctionBegin;
28336bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
28346bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
28356bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2836d763cef2SBarry Smith 
28376bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2838277b19d0SLisandro Dalcin 
2839e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2840e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
28416bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
28426d4c513bSLisandro Dalcin 
2843bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2844bc952696SBarry Smith 
284584df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
28466427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
28476427ac75SLisandro Dalcin 
28486bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
28496bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
28506bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
28510dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
28526d4c513bSLisandro Dalcin 
2853a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2854d763cef2SBarry Smith   PetscFunctionReturn(0);
2855d763cef2SBarry Smith }
2856d763cef2SBarry Smith 
2857d8e5e3e6SSatish Balay /*@
2858d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2859d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2860d763cef2SBarry Smith 
2861d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2862d763cef2SBarry Smith 
2863d763cef2SBarry Smith    Input Parameter:
2864d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2865d763cef2SBarry Smith 
2866d763cef2SBarry Smith    Output Parameter:
2867d763cef2SBarry Smith .  snes - the nonlinear solver context
2868d763cef2SBarry Smith 
2869d763cef2SBarry Smith    Notes:
2870d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2871d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
287294b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2873d763cef2SBarry Smith 
2874d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
28750298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2876d763cef2SBarry Smith 
2877d763cef2SBarry Smith    Level: beginner
2878d763cef2SBarry Smith 
2879d763cef2SBarry Smith .keywords: timestep, get, SNES
2880d763cef2SBarry Smith @*/
28817087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2882d763cef2SBarry Smith {
2883d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2884d372ba47SLisandro Dalcin 
2885d763cef2SBarry Smith   PetscFunctionBegin;
28860700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28874482741eSBarry Smith   PetscValidPointer(snes,2);
2888d372ba47SLisandro Dalcin   if (!ts->snes) {
2889ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
28900298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
28913bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2892d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2893496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
28949e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
28959e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
28969e2a6581SJed Brown     }
2897d372ba47SLisandro Dalcin   }
2898d763cef2SBarry Smith   *snes = ts->snes;
2899d763cef2SBarry Smith   PetscFunctionReturn(0);
2900d763cef2SBarry Smith }
2901d763cef2SBarry Smith 
2902deb2cd25SJed Brown /*@
2903deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2904deb2cd25SJed Brown 
2905deb2cd25SJed Brown    Collective
2906deb2cd25SJed Brown 
2907deb2cd25SJed Brown    Input Parameter:
2908deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2909deb2cd25SJed Brown -  snes - the nonlinear solver context
2910deb2cd25SJed Brown 
2911deb2cd25SJed Brown    Notes:
2912deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2913deb2cd25SJed Brown 
2914deb2cd25SJed Brown    Level: developer
2915deb2cd25SJed Brown 
2916deb2cd25SJed Brown .keywords: timestep, set, SNES
2917deb2cd25SJed Brown @*/
2918deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2919deb2cd25SJed Brown {
2920deb2cd25SJed Brown   PetscErrorCode ierr;
2921d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2922deb2cd25SJed Brown 
2923deb2cd25SJed Brown   PetscFunctionBegin;
2924deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2925deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2926deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2927deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2928bbd56ea5SKarl Rupp 
2929deb2cd25SJed Brown   ts->snes = snes;
2930bbd56ea5SKarl Rupp 
29310298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
29320298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2933740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
29340298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2935740132f1SEmil Constantinescu   }
2936deb2cd25SJed Brown   PetscFunctionReturn(0);
2937deb2cd25SJed Brown }
2938deb2cd25SJed Brown 
2939d8e5e3e6SSatish Balay /*@
294094b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2941d763cef2SBarry Smith    a TS (timestepper) context.
2942d763cef2SBarry Smith 
294394b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2944d763cef2SBarry Smith 
2945d763cef2SBarry Smith    Input Parameter:
2946d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2947d763cef2SBarry Smith 
2948d763cef2SBarry Smith    Output Parameter:
294994b7f48cSBarry Smith .  ksp - the nonlinear solver context
2950d763cef2SBarry Smith 
2951d763cef2SBarry Smith    Notes:
295294b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2953d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2954d763cef2SBarry Smith    KSP and PC contexts as well.
2955d763cef2SBarry Smith 
295694b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
29570298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2958d763cef2SBarry Smith 
2959d763cef2SBarry Smith    Level: beginner
2960d763cef2SBarry Smith 
296194b7f48cSBarry Smith .keywords: timestep, get, KSP
2962d763cef2SBarry Smith @*/
29637087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2964d763cef2SBarry Smith {
2965d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2966089b2837SJed Brown   SNES           snes;
2967d372ba47SLisandro Dalcin 
2968d763cef2SBarry Smith   PetscFunctionBegin;
29690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29704482741eSBarry Smith   PetscValidPointer(ksp,2);
297117186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2972e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2973089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2974089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2975d763cef2SBarry Smith   PetscFunctionReturn(0);
2976d763cef2SBarry Smith }
2977d763cef2SBarry Smith 
2978d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2979d763cef2SBarry Smith 
2980adb62b0dSMatthew Knepley /*@
2981618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
2982618ce8baSLisandro Dalcin 
2983618ce8baSLisandro Dalcin    Logically Collective on TS
2984618ce8baSLisandro Dalcin 
2985618ce8baSLisandro Dalcin    Input Parameters:
2986618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2987618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
2988618ce8baSLisandro Dalcin 
2989618ce8baSLisandro Dalcin    Options Database Keys:
2990618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
2991618ce8baSLisandro Dalcin 
2992618ce8baSLisandro Dalcin    Notes:
2993618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
2994618ce8baSLisandro Dalcin 
2995618ce8baSLisandro Dalcin    Level: intermediate
2996618ce8baSLisandro Dalcin 
2997618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, steps
2998618ce8baSLisandro Dalcin 
2999618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
3000618ce8baSLisandro Dalcin @*/
3001618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
3002618ce8baSLisandro Dalcin {
3003618ce8baSLisandro Dalcin   PetscFunctionBegin;
3004618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3005618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3006618ce8baSLisandro Dalcin   if (maxsteps < 0 ) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
3007618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
3008618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3009618ce8baSLisandro Dalcin }
3010618ce8baSLisandro Dalcin 
3011618ce8baSLisandro Dalcin /*@
3012618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
3013618ce8baSLisandro Dalcin 
3014618ce8baSLisandro Dalcin    Not Collective
3015618ce8baSLisandro Dalcin 
3016618ce8baSLisandro Dalcin    Input Parameters:
3017618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3018618ce8baSLisandro Dalcin 
3019618ce8baSLisandro Dalcin    Output Parameter:
3020618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
3021618ce8baSLisandro Dalcin 
3022618ce8baSLisandro Dalcin    Level: advanced
3023618ce8baSLisandro Dalcin 
3024618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, steps
3025618ce8baSLisandro Dalcin 
3026618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
3027618ce8baSLisandro Dalcin @*/
3028618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
3029618ce8baSLisandro Dalcin {
3030618ce8baSLisandro Dalcin   PetscFunctionBegin;
3031618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3032618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
3033618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
3034618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3035618ce8baSLisandro Dalcin }
3036618ce8baSLisandro Dalcin 
3037618ce8baSLisandro Dalcin /*@
3038618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
3039618ce8baSLisandro Dalcin 
3040618ce8baSLisandro Dalcin    Logically Collective on TS
3041618ce8baSLisandro Dalcin 
3042618ce8baSLisandro Dalcin    Input Parameters:
3043618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3044618ce8baSLisandro Dalcin -  maxtime - final time to step to
3045618ce8baSLisandro Dalcin 
3046618ce8baSLisandro Dalcin    Options Database Keys:
3047ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
3048618ce8baSLisandro Dalcin 
3049618ce8baSLisandro Dalcin    Notes:
3050618ce8baSLisandro Dalcin    The default maximum time is 5.0
3051618ce8baSLisandro Dalcin 
3052618ce8baSLisandro Dalcin    Level: intermediate
3053618ce8baSLisandro Dalcin 
3054618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, time
3055618ce8baSLisandro Dalcin 
3056618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
3057618ce8baSLisandro Dalcin @*/
3058618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
3059618ce8baSLisandro Dalcin {
3060618ce8baSLisandro Dalcin   PetscFunctionBegin;
3061618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3062618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
3063618ce8baSLisandro Dalcin   ts->max_time = maxtime;
3064618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3065618ce8baSLisandro Dalcin }
3066618ce8baSLisandro Dalcin 
3067618ce8baSLisandro Dalcin /*@
3068618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
3069618ce8baSLisandro Dalcin 
3070618ce8baSLisandro Dalcin    Not Collective
3071618ce8baSLisandro Dalcin 
3072618ce8baSLisandro Dalcin    Input Parameters:
3073618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3074618ce8baSLisandro Dalcin 
3075618ce8baSLisandro Dalcin    Output Parameter:
3076618ce8baSLisandro Dalcin .  maxtime - final time to step to
3077618ce8baSLisandro Dalcin 
3078618ce8baSLisandro Dalcin    Level: advanced
3079618ce8baSLisandro Dalcin 
3080618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, time
3081618ce8baSLisandro Dalcin 
3082618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
3083618ce8baSLisandro Dalcin @*/
3084618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
3085618ce8baSLisandro Dalcin {
3086618ce8baSLisandro Dalcin   PetscFunctionBegin;
3087618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3088618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
3089618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
3090618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3091618ce8baSLisandro Dalcin }
3092618ce8baSLisandro Dalcin 
3093618ce8baSLisandro Dalcin /*@
3094aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
3095edc382c3SSatish Balay 
3096edc382c3SSatish Balay    Level: deprecated
3097edc382c3SSatish Balay 
3098aaa6c58dSLisandro Dalcin @*/
3099aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
3100aaa6c58dSLisandro Dalcin {
3101aaa6c58dSLisandro Dalcin   PetscErrorCode ierr;
3102aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
3103aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3104aaa6c58dSLisandro Dalcin   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
3105aaa6c58dSLisandro Dalcin   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
3106aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
3107aaa6c58dSLisandro Dalcin }
3108aaa6c58dSLisandro Dalcin 
3109aaa6c58dSLisandro Dalcin /*@
311019eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
3111edc382c3SSatish Balay 
3112edc382c3SSatish Balay    Level: deprecated
3113edc382c3SSatish Balay 
3114adb62b0dSMatthew Knepley @*/
31157087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
3116adb62b0dSMatthew Knepley {
3117adb62b0dSMatthew Knepley   PetscFunctionBegin;
31180700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3119abc0a331SBarry Smith   if (maxsteps) {
31204482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
3121adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
3122adb62b0dSMatthew Knepley   }
3123abc0a331SBarry Smith   if (maxtime) {
31244482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
3125adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
3126adb62b0dSMatthew Knepley   }
3127adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
3128adb62b0dSMatthew Knepley }
3129adb62b0dSMatthew Knepley 
3130d763cef2SBarry Smith /*@
313119eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
3132edc382c3SSatish Balay 
3133edc382c3SSatish Balay    Level: deprecated
3134edc382c3SSatish Balay 
3135d763cef2SBarry Smith @*/
31367087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
3137d763cef2SBarry Smith {
3138d763cef2SBarry Smith   PetscFunctionBegin;
31390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3140c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3141c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
314239b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
314339b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
3144d763cef2SBarry Smith   PetscFunctionReturn(0);
3145d763cef2SBarry Smith }
3146d763cef2SBarry Smith 
3147d763cef2SBarry Smith /*@
31485c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
3149edc382c3SSatish Balay 
3150edc382c3SSatish Balay    Level: deprecated
3151edc382c3SSatish Balay 
31525c5f5948SLisandro Dalcin @*/
3153e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31545c5f5948SLisandro Dalcin 
315519eac22cSLisandro Dalcin /*@
31564f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
3157edc382c3SSatish Balay 
3158edc382c3SSatish Balay    Level: deprecated
3159edc382c3SSatish Balay 
31604f4e0956SLisandro Dalcin @*/
31614f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31624f4e0956SLisandro Dalcin 
31634f4e0956SLisandro Dalcin /*@
3164d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
3165d763cef2SBarry Smith    for use by the TS routines.
3166d763cef2SBarry Smith 
31673f9fe445SBarry Smith    Logically Collective on TS and Vec
3168d763cef2SBarry Smith 
3169d763cef2SBarry Smith    Input Parameters:
3170d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
31710910c330SBarry Smith -  u - the solution vector
3172d763cef2SBarry Smith 
3173d763cef2SBarry Smith    Level: beginner
3174d763cef2SBarry Smith 
3175715f1b00SHong Zhang .keywords: TS, timestep, set, solution, initial values
31760ed3bfb6SBarry Smith 
31770ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3178d763cef2SBarry Smith @*/
31790910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3180d763cef2SBarry Smith {
31818737fe31SLisandro Dalcin   PetscErrorCode ierr;
3182496e6a7aSJed Brown   DM             dm;
31838737fe31SLisandro Dalcin 
3184d763cef2SBarry Smith   PetscFunctionBegin;
31850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31860910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
31870910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
31886bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
31890910c330SBarry Smith   ts->vec_sol = u;
3190bbd56ea5SKarl Rupp 
3191496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
31920910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
3193d763cef2SBarry Smith   PetscFunctionReturn(0);
3194d763cef2SBarry Smith }
3195d763cef2SBarry Smith 
3196ac226902SBarry Smith /*@C
3197000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
31983f2090d5SJed Brown   called once at the beginning of each time step.
3199000e7ae3SMatthew Knepley 
32003f9fe445SBarry Smith   Logically Collective on TS
3201000e7ae3SMatthew Knepley 
3202000e7ae3SMatthew Knepley   Input Parameters:
3203000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3204000e7ae3SMatthew Knepley - func - The function
3205000e7ae3SMatthew Knepley 
3206000e7ae3SMatthew Knepley   Calling sequence of func:
3207000e7ae3SMatthew Knepley . func (TS ts);
3208000e7ae3SMatthew Knepley 
3209000e7ae3SMatthew Knepley   Level: intermediate
3210000e7ae3SMatthew Knepley 
3211000e7ae3SMatthew Knepley .keywords: TS, timestep
3212dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3213000e7ae3SMatthew Knepley @*/
32147087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3215000e7ae3SMatthew Knepley {
3216000e7ae3SMatthew Knepley   PetscFunctionBegin;
32170700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3218ae60f76fSBarry Smith   ts->prestep = func;
3219000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3220000e7ae3SMatthew Knepley }
3221000e7ae3SMatthew Knepley 
322209ee8438SJed Brown /*@
32233f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
32243f2090d5SJed Brown 
32253f2090d5SJed Brown   Collective on TS
32263f2090d5SJed Brown 
32273f2090d5SJed Brown   Input Parameters:
32283f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
32293f2090d5SJed Brown 
32303f2090d5SJed Brown   Notes:
32313f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
32323f2090d5SJed Brown   so most users would not generally call this routine themselves.
32333f2090d5SJed Brown 
32343f2090d5SJed Brown   Level: developer
32353f2090d5SJed Brown 
32363f2090d5SJed Brown .keywords: TS, timestep
32379be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
32383f2090d5SJed Brown @*/
32397087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
32403f2090d5SJed Brown {
32413f2090d5SJed Brown   PetscErrorCode ierr;
32423f2090d5SJed Brown 
32433f2090d5SJed Brown   PetscFunctionBegin;
32440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3245ae60f76fSBarry Smith   if (ts->prestep) {
32465efd42a4SStefano Zampini     Vec              U;
32475efd42a4SStefano Zampini     PetscObjectState sprev,spost;
32485efd42a4SStefano Zampini 
32495efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
32505efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3251ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
32525efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3253dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3254312ce896SJed Brown   }
32553f2090d5SJed Brown   PetscFunctionReturn(0);
32563f2090d5SJed Brown }
32573f2090d5SJed Brown 
3258b8123daeSJed Brown /*@C
3259b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3260b8123daeSJed Brown   called once at the beginning of each stage.
3261b8123daeSJed Brown 
3262b8123daeSJed Brown   Logically Collective on TS
3263b8123daeSJed Brown 
3264b8123daeSJed Brown   Input Parameters:
3265b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3266b8123daeSJed Brown - func - The function
3267b8123daeSJed Brown 
3268b8123daeSJed Brown   Calling sequence of func:
3269b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
3270b8123daeSJed Brown 
3271b8123daeSJed Brown   Level: intermediate
3272b8123daeSJed Brown 
3273b8123daeSJed Brown   Note:
3274b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
327580275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3276b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3277b8123daeSJed Brown 
3278b8123daeSJed Brown .keywords: TS, timestep
32799be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3280b8123daeSJed Brown @*/
3281b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3282b8123daeSJed Brown {
3283b8123daeSJed Brown   PetscFunctionBegin;
3284b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3285ae60f76fSBarry Smith   ts->prestage = func;
3286b8123daeSJed Brown   PetscFunctionReturn(0);
3287b8123daeSJed Brown }
3288b8123daeSJed Brown 
32899be3e283SDebojyoti Ghosh /*@C
32909be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
32919be3e283SDebojyoti Ghosh   called once at the end of each stage.
32929be3e283SDebojyoti Ghosh 
32939be3e283SDebojyoti Ghosh   Logically Collective on TS
32949be3e283SDebojyoti Ghosh 
32959be3e283SDebojyoti Ghosh   Input Parameters:
32969be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
32979be3e283SDebojyoti Ghosh - func - The function
32989be3e283SDebojyoti Ghosh 
32999be3e283SDebojyoti Ghosh   Calling sequence of func:
33009be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
33019be3e283SDebojyoti Ghosh 
33029be3e283SDebojyoti Ghosh   Level: intermediate
33039be3e283SDebojyoti Ghosh 
33049be3e283SDebojyoti Ghosh   Note:
33059be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
330680275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
33079be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
33089be3e283SDebojyoti Ghosh 
33099be3e283SDebojyoti Ghosh .keywords: TS, timestep
33109be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
33119be3e283SDebojyoti Ghosh @*/
33129be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
33139be3e283SDebojyoti Ghosh {
33149be3e283SDebojyoti Ghosh   PetscFunctionBegin;
33159be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
33169be3e283SDebojyoti Ghosh   ts->poststage = func;
33179be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
33189be3e283SDebojyoti Ghosh }
33199be3e283SDebojyoti Ghosh 
3320c688d042SShri Abhyankar /*@C
3321c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3322c688d042SShri Abhyankar   called once at the end of each step evaluation.
3323c688d042SShri Abhyankar 
3324c688d042SShri Abhyankar   Logically Collective on TS
3325c688d042SShri Abhyankar 
3326c688d042SShri Abhyankar   Input Parameters:
3327c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3328c688d042SShri Abhyankar - func - The function
3329c688d042SShri Abhyankar 
3330c688d042SShri Abhyankar   Calling sequence of func:
3331c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3332c688d042SShri Abhyankar 
3333c688d042SShri Abhyankar   Level: intermediate
3334c688d042SShri Abhyankar 
3335c688d042SShri Abhyankar   Note:
33361785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
33371785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3338e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3339e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3340e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3341c688d042SShri Abhyankar 
3342c688d042SShri Abhyankar .keywords: TS, timestep
3343c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3344c688d042SShri Abhyankar @*/
3345c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3346c688d042SShri Abhyankar {
3347c688d042SShri Abhyankar   PetscFunctionBegin;
3348c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3349c688d042SShri Abhyankar   ts->postevaluate = func;
3350c688d042SShri Abhyankar   PetscFunctionReturn(0);
3351c688d042SShri Abhyankar }
3352c688d042SShri Abhyankar 
3353b8123daeSJed Brown /*@
3354b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3355b8123daeSJed Brown 
3356b8123daeSJed Brown   Collective on TS
3357b8123daeSJed Brown 
3358b8123daeSJed Brown   Input Parameters:
3359b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
33609be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3361b8123daeSJed Brown 
3362b8123daeSJed Brown   Notes:
3363b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3364b8123daeSJed Brown   most users would not generally call this routine themselves.
3365b8123daeSJed Brown 
3366b8123daeSJed Brown   Level: developer
3367b8123daeSJed Brown 
3368b8123daeSJed Brown .keywords: TS, timestep
33699be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3370b8123daeSJed Brown @*/
3371b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3372b8123daeSJed Brown {
3373b8123daeSJed Brown   PetscErrorCode ierr;
3374b8123daeSJed Brown 
3375b8123daeSJed Brown   PetscFunctionBegin;
3376b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3377ae60f76fSBarry Smith   if (ts->prestage) {
3378ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3379b8123daeSJed Brown   }
3380b8123daeSJed Brown   PetscFunctionReturn(0);
3381b8123daeSJed Brown }
3382b8123daeSJed Brown 
33839be3e283SDebojyoti Ghosh /*@
33849be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
33859be3e283SDebojyoti Ghosh 
33869be3e283SDebojyoti Ghosh   Collective on TS
33879be3e283SDebojyoti Ghosh 
33889be3e283SDebojyoti Ghosh   Input Parameters:
33899be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
33909be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
33919be3e283SDebojyoti Ghosh   stageindex  - Stage number
33929be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
33939be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
33949be3e283SDebojyoti Ghosh 
33959be3e283SDebojyoti Ghosh   Notes:
33969be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
33979be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
33989be3e283SDebojyoti Ghosh 
33999be3e283SDebojyoti Ghosh   Level: developer
34009be3e283SDebojyoti Ghosh 
34019be3e283SDebojyoti Ghosh .keywords: TS, timestep
34029be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
34039be3e283SDebojyoti Ghosh @*/
34049be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
34059be3e283SDebojyoti Ghosh {
34069be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
34079be3e283SDebojyoti Ghosh 
34089be3e283SDebojyoti Ghosh   PetscFunctionBegin;
34099be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
34104beae5d8SLisandro Dalcin   if (ts->poststage) {
34119be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
34129be3e283SDebojyoti Ghosh   }
34139be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
34149be3e283SDebojyoti Ghosh }
34159be3e283SDebojyoti Ghosh 
3416c688d042SShri Abhyankar /*@
3417c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3418c688d042SShri Abhyankar 
3419c688d042SShri Abhyankar   Collective on TS
3420c688d042SShri Abhyankar 
3421c688d042SShri Abhyankar   Input Parameters:
3422c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3423c688d042SShri Abhyankar 
3424c688d042SShri Abhyankar   Notes:
3425c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3426c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3427c688d042SShri Abhyankar 
3428c688d042SShri Abhyankar   Level: developer
3429c688d042SShri Abhyankar 
3430c688d042SShri Abhyankar .keywords: TS, timestep
3431c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3432c688d042SShri Abhyankar @*/
3433c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3434c688d042SShri Abhyankar {
3435c688d042SShri Abhyankar   PetscErrorCode ierr;
3436c688d042SShri Abhyankar 
3437c688d042SShri Abhyankar   PetscFunctionBegin;
3438c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3439c688d042SShri Abhyankar   if (ts->postevaluate) {
3440dcb233daSLisandro Dalcin     Vec              U;
3441dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3442dcb233daSLisandro Dalcin 
3443dcb233daSLisandro Dalcin     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3444dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3445c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3446dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3447dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3448c688d042SShri Abhyankar   }
3449c688d042SShri Abhyankar   PetscFunctionReturn(0);
3450c688d042SShri Abhyankar }
3451c688d042SShri Abhyankar 
3452ac226902SBarry Smith /*@C
3453000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
34543f2090d5SJed Brown   called once at the end of each time step.
3455000e7ae3SMatthew Knepley 
34563f9fe445SBarry Smith   Logically Collective on TS
3457000e7ae3SMatthew Knepley 
3458000e7ae3SMatthew Knepley   Input Parameters:
3459000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3460000e7ae3SMatthew Knepley - func - The function
3461000e7ae3SMatthew Knepley 
3462000e7ae3SMatthew Knepley   Calling sequence of func:
3463b8123daeSJed Brown $ func (TS ts);
3464000e7ae3SMatthew Knepley 
34651785ff2aSShri Abhyankar   Notes:
34661785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
34671785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
34681785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
34691785ff2aSShri Abhyankar 
3470000e7ae3SMatthew Knepley   Level: intermediate
3471000e7ae3SMatthew Knepley 
3472000e7ae3SMatthew Knepley .keywords: TS, timestep
3473dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3474000e7ae3SMatthew Knepley @*/
34757087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3476000e7ae3SMatthew Knepley {
3477000e7ae3SMatthew Knepley   PetscFunctionBegin;
34780700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3479ae60f76fSBarry Smith   ts->poststep = func;
3480000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3481000e7ae3SMatthew Knepley }
3482000e7ae3SMatthew Knepley 
348309ee8438SJed Brown /*@
34843f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
34853f2090d5SJed Brown 
34863f2090d5SJed Brown   Collective on TS
34873f2090d5SJed Brown 
34883f2090d5SJed Brown   Input Parameters:
34893f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
34903f2090d5SJed Brown 
34913f2090d5SJed Brown   Notes:
34923f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
34933f2090d5SJed Brown   so most users would not generally call this routine themselves.
34943f2090d5SJed Brown 
34953f2090d5SJed Brown   Level: developer
34963f2090d5SJed Brown 
34973f2090d5SJed Brown .keywords: TS, timestep
34983f2090d5SJed Brown @*/
34997087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
35003f2090d5SJed Brown {
35013f2090d5SJed Brown   PetscErrorCode ierr;
35023f2090d5SJed Brown 
35033f2090d5SJed Brown   PetscFunctionBegin;
35040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3505ae60f76fSBarry Smith   if (ts->poststep) {
35065efd42a4SStefano Zampini     Vec              U;
35075efd42a4SStefano Zampini     PetscObjectState sprev,spost;
35085efd42a4SStefano Zampini 
35095efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
35105efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3511ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
35125efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3513dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
351472ac3e02SJed Brown   }
35153f2090d5SJed Brown   PetscFunctionReturn(0);
35163f2090d5SJed Brown }
35173f2090d5SJed Brown 
3518d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3519d763cef2SBarry Smith 
3520d763cef2SBarry Smith /*@C
3521a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3522d763cef2SBarry Smith    timestep to display the iteration's  progress.
3523d763cef2SBarry Smith 
35243f9fe445SBarry Smith    Logically Collective on TS
3525d763cef2SBarry Smith 
3526d763cef2SBarry Smith    Input Parameters:
3527d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3528e213d8f1SJed Brown .  monitor - monitoring routine
3529329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
35300298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3531b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
35320298fd71SBarry Smith           (may be NULL)
3533d763cef2SBarry Smith 
3534e213d8f1SJed Brown    Calling sequence of monitor:
3535dcb233daSLisandro Dalcin $    PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3536d763cef2SBarry Smith 
3537d763cef2SBarry Smith +    ts - the TS context
353863e21af5SBarry Smith .    steps - iteration number (after the final time step the monitor routine may be called with a step of -1, this indicates the solution has been interpolated to this time)
35391f06c33eSBarry Smith .    time - current time
35400910c330SBarry Smith .    u - current iterate
3541d763cef2SBarry Smith -    mctx - [optional] monitoring context
3542d763cef2SBarry Smith 
3543d763cef2SBarry Smith    Notes:
3544d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3545d763cef2SBarry Smith    already has been loaded.
3546d763cef2SBarry Smith 
354795452b02SPatrick Sanan    Fortran Notes:
354895452b02SPatrick Sanan     Only a single monitor function can be set for each TS object
3549025f1a04SBarry Smith 
3550d763cef2SBarry Smith    Level: intermediate
3551d763cef2SBarry Smith 
3552d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3553d763cef2SBarry Smith 
3554a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3555d763cef2SBarry Smith @*/
3556c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3557d763cef2SBarry Smith {
355878064530SBarry Smith   PetscErrorCode ierr;
355978064530SBarry Smith   PetscInt       i;
356078064530SBarry Smith   PetscBool      identical;
356178064530SBarry Smith 
3562d763cef2SBarry Smith   PetscFunctionBegin;
35630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
356478064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
356578064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
356678064530SBarry Smith     if (identical) PetscFunctionReturn(0);
356778064530SBarry Smith   }
356817186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3569d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
35708704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3571d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3572d763cef2SBarry Smith   PetscFunctionReturn(0);
3573d763cef2SBarry Smith }
3574d763cef2SBarry Smith 
3575d763cef2SBarry Smith /*@C
3576a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3577d763cef2SBarry Smith 
35783f9fe445SBarry Smith    Logically Collective on TS
3579d763cef2SBarry Smith 
3580d763cef2SBarry Smith    Input Parameters:
3581d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3582d763cef2SBarry Smith 
3583d763cef2SBarry Smith    Notes:
3584d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3585d763cef2SBarry Smith 
3586d763cef2SBarry Smith    Level: intermediate
3587d763cef2SBarry Smith 
3588d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3589d763cef2SBarry Smith 
3590a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3591d763cef2SBarry Smith @*/
35927087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3593d763cef2SBarry Smith {
3594d952e501SBarry Smith   PetscErrorCode ierr;
3595d952e501SBarry Smith   PetscInt       i;
3596d952e501SBarry Smith 
3597d763cef2SBarry Smith   PetscFunctionBegin;
35980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3599d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
36008704b422SBarry Smith     if (ts->monitordestroy[i]) {
36018704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3602d952e501SBarry Smith     }
3603d952e501SBarry Smith   }
3604d763cef2SBarry Smith   ts->numbermonitors = 0;
3605d763cef2SBarry Smith   PetscFunctionReturn(0);
3606d763cef2SBarry Smith }
3607d763cef2SBarry Smith 
3608721cd6eeSBarry Smith /*@C
3609721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
36105516499fSSatish Balay 
36115516499fSSatish Balay    Level: intermediate
361241251cbbSSatish Balay 
36135516499fSSatish Balay .keywords: TS, set, monitor
36145516499fSSatish Balay 
361563e21af5SBarry Smith .seealso:  TSMonitorSet()
361641251cbbSSatish Balay @*/
3617721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3618d763cef2SBarry Smith {
3619dfbe8321SBarry Smith   PetscErrorCode ierr;
3620721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
362141aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3622d132466eSBarry Smith 
3623d763cef2SBarry Smith   PetscFunctionBegin;
36244d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
362541aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
362641aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3627721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
362841aca3d6SBarry Smith   if (iascii) {
3629649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
363063e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
363163e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
363263e21af5SBarry Smith     } else {
36338392e04aSShri Abhyankar       ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)\n" : "\n");CHKERRQ(ierr);
363463e21af5SBarry Smith     }
3635649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
363641aca3d6SBarry Smith   } else if (ibinary) {
363741aca3d6SBarry Smith     PetscMPIInt rank;
363841aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
363941aca3d6SBarry Smith     if (!rank) {
3640450a797fSBarry Smith       PetscBool skipHeader;
3641450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3642450a797fSBarry Smith 
3643450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3644450a797fSBarry Smith       if (!skipHeader) {
3645450a797fSBarry Smith          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
3646450a797fSBarry Smith        }
364741aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
364841aca3d6SBarry Smith     } else {
364941aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
365041aca3d6SBarry Smith     }
365141aca3d6SBarry Smith   }
3652721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3653d763cef2SBarry Smith   PetscFunctionReturn(0);
3654d763cef2SBarry Smith }
3655d763cef2SBarry Smith 
3656cd652676SJed Brown /*@
3657cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3658cd652676SJed Brown 
3659cd652676SJed Brown    Collective on TS
3660cd652676SJed Brown 
3661cd652676SJed Brown    Input Argument:
3662cd652676SJed Brown +  ts - time stepping context
3663cd652676SJed Brown -  t - time to interpolate to
3664cd652676SJed Brown 
3665cd652676SJed Brown    Output Argument:
36660910c330SBarry Smith .  U - state at given time
3667cd652676SJed Brown 
3668cd652676SJed Brown    Level: intermediate
3669cd652676SJed Brown 
3670cd652676SJed Brown    Developer Notes:
3671cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3672cd652676SJed Brown 
3673cd652676SJed Brown .keywords: TS, set
3674cd652676SJed Brown 
3675874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3676cd652676SJed Brown @*/
36770910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3678cd652676SJed Brown {
3679cd652676SJed Brown   PetscErrorCode ierr;
3680cd652676SJed Brown 
3681cd652676SJed Brown   PetscFunctionBegin;
3682cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3683b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3684be5899b3SLisandro Dalcin   if (t < ts->ptime_prev || t > ts->ptime) SETERRQ3(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Requested time %g not in last time steps [%g,%g]",t,(double)ts->ptime_prev,(double)ts->ptime);
3685ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
36860910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3687cd652676SJed Brown   PetscFunctionReturn(0);
3688cd652676SJed Brown }
3689cd652676SJed Brown 
3690d763cef2SBarry Smith /*@
36916d9e5789SSean Farley    TSStep - Steps one time step
3692d763cef2SBarry Smith 
3693d763cef2SBarry Smith    Collective on TS
3694d763cef2SBarry Smith 
3695d763cef2SBarry Smith    Input Parameter:
3696d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3697d763cef2SBarry Smith 
369827829d71SBarry Smith    Level: developer
3699d763cef2SBarry Smith 
3700b8123daeSJed Brown    Notes:
370127829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
370227829d71SBarry Smith 
3703b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3704b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3705b8123daeSJed Brown 
370619eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
370719eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
370825cb2221SBarry Smith 
3709d763cef2SBarry Smith .keywords: TS, timestep, solve
3710d763cef2SBarry Smith 
37119be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3712d763cef2SBarry Smith @*/
3713193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3714d763cef2SBarry Smith {
3715dfbe8321SBarry Smith   PetscErrorCode   ierr;
3716fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3717be5899b3SLisandro Dalcin   PetscReal        ptime;
3718d763cef2SBarry Smith 
3719d763cef2SBarry Smith   PetscFunctionBegin;
37200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3721fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3722fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3723fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3724fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3725fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3726fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3727302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3728fffbeea8SBarry Smith 
3729d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
373068bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3731d405a339SMatthew Knepley 
3732ef85077eSLisandro Dalcin   if (ts->max_time >= PETSC_MAX_REAL && ts->max_steps == PETSC_MAX_INT) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetMaxTime() or TSSetMaxSteps(), or use -ts_max_time <time> or -ts_max_steps <steps>");
3733a6772fa2SLisandro Dalcin   if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetExactFinalTime() or use -ts_exact_final_time <stepover,interpolate,matchstep> before calling TSStep()");
3734be5899b3SLisandro Dalcin   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && !ts->adapt) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Since TS is not adaptive you cannot use TS_EXACTFINALTIME_MATCHSTEP, suggest TS_EXACTFINALTIME_INTERPOLATE");
3735a6772fa2SLisandro Dalcin 
3736be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3737be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
37382808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3739e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3740d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3741193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3742d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3743be5899b3SLisandro Dalcin   ts->ptime_prev = ptime;
37442808aa04SLisandro Dalcin   ts->steps++;
3745be5899b3SLisandro Dalcin   ts->steprollback = PETSC_FALSE;
374628d5b5d6SLisandro Dalcin   ts->steprestart  = PETSC_FALSE;
3747362cd11cSLisandro Dalcin 
3748362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3749cef5090cSJed Brown     if (ts->errorifstepfailed) {
375008c7845fSBarry Smith       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]);
375108c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3752d2daff3dSHong Zhang     }
375308c7845fSBarry Smith   } else if (!ts->reason) {
375408c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
375508c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
375608c7845fSBarry Smith   }
375708c7845fSBarry Smith   PetscFunctionReturn(0);
375808c7845fSBarry Smith }
375908c7845fSBarry Smith 
376008c7845fSBarry Smith /*@
37617cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
37627cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
37637cbde773SLisandro Dalcin 
37647cbde773SLisandro Dalcin    Collective on TS
37657cbde773SLisandro Dalcin 
37667cbde773SLisandro Dalcin    Input Arguments:
37677cbde773SLisandro Dalcin +  ts - time stepping context
37687cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
37697cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
37707cbde773SLisandro Dalcin 
37717cbde773SLisandro Dalcin    Output Arguments:
37727cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
37737cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
37747cbde773SLisandro Dalcin 
37757cbde773SLisandro Dalcin    Level: advanced
37767cbde773SLisandro Dalcin 
37777cbde773SLisandro Dalcin    Notes:
37787cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
37797cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
37807cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
37817cbde773SLisandro Dalcin 
37827cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
37837cbde773SLisandro Dalcin @*/
37847cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
37857cbde773SLisandro Dalcin {
37867cbde773SLisandro Dalcin   PetscErrorCode ierr;
37877cbde773SLisandro Dalcin 
37887cbde773SLisandro Dalcin   PetscFunctionBegin;
37897cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37907cbde773SLisandro Dalcin   PetscValidType(ts,1);
37917cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
37927cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
37937cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
37947cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
37957cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
37967cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
37977cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
37987cbde773SLisandro Dalcin   PetscFunctionReturn(0);
37997cbde773SLisandro Dalcin }
38007cbde773SLisandro Dalcin 
380105175c85SJed Brown /*@
380205175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
380305175c85SJed Brown 
38041c3436cfSJed Brown    Collective on TS
380505175c85SJed Brown 
380605175c85SJed Brown    Input Arguments:
38071c3436cfSJed Brown +  ts - time stepping context
38081c3436cfSJed Brown .  order - desired order of accuracy
38090298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
381005175c85SJed Brown 
381105175c85SJed Brown    Output Arguments:
38120910c330SBarry Smith .  U - state at the end of the current step
381305175c85SJed Brown 
381405175c85SJed Brown    Level: advanced
381505175c85SJed Brown 
3816108c343cSJed Brown    Notes:
3817108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3818108c343cSJed 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.
3819108c343cSJed Brown 
38201c3436cfSJed Brown .seealso: TSStep(), TSAdapt
382105175c85SJed Brown @*/
38220910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
382305175c85SJed Brown {
382405175c85SJed Brown   PetscErrorCode ierr;
382505175c85SJed Brown 
382605175c85SJed Brown   PetscFunctionBegin;
382705175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
382805175c85SJed Brown   PetscValidType(ts,1);
38290910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3830ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
38310910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
383205175c85SJed Brown   PetscFunctionReturn(0);
383305175c85SJed Brown }
383405175c85SJed Brown 
3835b1cb36f3SHong Zhang /*@
38366a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
38376a4d4014SLisandro Dalcin 
38386a4d4014SLisandro Dalcin    Collective on TS
38396a4d4014SLisandro Dalcin 
38406a4d4014SLisandro Dalcin    Input Parameter:
38416a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
384263e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
384363e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
38445a3a76d0SJed Brown 
38456a4d4014SLisandro Dalcin    Level: beginner
38466a4d4014SLisandro Dalcin 
38475a3a76d0SJed Brown    Notes:
38485a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
38495a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
38505a3a76d0SJed Brown    stepped over the final time.
38515a3a76d0SJed Brown 
38526a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
38536a4d4014SLisandro Dalcin 
385463e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
38556a4d4014SLisandro Dalcin @*/
3856cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
38576a4d4014SLisandro Dalcin {
3858b06615a5SLisandro Dalcin   Vec               solution;
38596a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
3860f22f69f0SBarry Smith 
38616a4d4014SLisandro Dalcin   PetscFunctionBegin;
38620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3863f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
3864feed9e9dSBarry Smith 
3865ee41a567SStefano Zampini   if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && u) {   /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */
38660910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3867b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3868b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3869b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
38705a3a76d0SJed Brown     }
38710910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3872715f1b00SHong Zhang     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
3873bbd56ea5SKarl Rupp   } else if (u) {
38740910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
38755a3a76d0SJed Brown   }
3876b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
387768bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3878a6772fa2SLisandro Dalcin 
3879ef85077eSLisandro Dalcin   if (ts->max_time >= PETSC_MAX_REAL && ts->max_steps == PETSC_MAX_INT) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetMaxTime() or TSSetMaxSteps(), or use -ts_max_time <time> or -ts_max_steps <steps>");
3880a6772fa2SLisandro Dalcin   if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"You must call TSSetExactFinalTime() or use -ts_exact_final_time <stepover,interpolate,matchstep> before calling TSSolve()");
3881a6772fa2SLisandro Dalcin   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && !ts->adapt) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Since TS is not adaptive you cannot use TS_EXACTFINALTIME_MATCHSTEP, suggest TS_EXACTFINALTIME_INTERPOLATE");
3882a6772fa2SLisandro Dalcin 
3883715f1b00SHong Zhang   if (ts->forward_solve) {
3884715f1b00SHong Zhang     ierr = TSForwardSetUp(ts);CHKERRQ(ierr);
3885715f1b00SHong Zhang   }
3886715f1b00SHong Zhang 
3887e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
3888715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
3889e7069c78SShri      TSTrajectory to incorrectly save the output files
3890e7069c78SShri   */
3891715f1b00SHong Zhang   /* reset time step and iteration counters */
38922808aa04SLisandro Dalcin   if (!ts->steps) {
38935ef26d82SJed Brown     ts->ksp_its           = 0;
38945ef26d82SJed Brown     ts->snes_its          = 0;
3895c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
3896c610991cSLisandro Dalcin     ts->reject            = 0;
38972808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
38982808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
38992808aa04SLisandro Dalcin   }
390010bc0e4dSStefano Zampini   if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP && ts->ptime + ts->time_step > ts->max_time) ts->time_step = ts->max_time - ts->ptime;
3901193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
3902193ac0bcSJed Brown 
3903ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3904f05ece33SBarry Smith 
3905193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
3906193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
3907a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3908cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3909a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
3910be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
3911db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
3912db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3913e7069c78SShri 
39142808aa04SLisandro Dalcin     if (!ts->steps) {
39152808aa04SLisandro Dalcin       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
39166427ac75SLisandro Dalcin       ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
39172808aa04SLisandro Dalcin     }
39186427ac75SLisandro Dalcin 
3919e1a7a14fSJed Brown     while (!ts->reason) {
39202808aa04SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
39219687d888SLisandro Dalcin       if (!ts->steprollback) {
39229687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
39239687d888SLisandro Dalcin       }
3924193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
3925f3b1f45cSBarry Smith       if (ts->testjacobian) {
3926f3b1f45cSBarry Smith         ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr);
3927f3b1f45cSBarry Smith       }
3928f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
3929f3b1f45cSBarry Smith         ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr);
3930f3b1f45cSBarry Smith       }
3931e783b05fSHong Zhang       if (ts->vec_costintegral && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */
3932b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
3933b1cb36f3SHong Zhang       }
393458818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
3935715f1b00SHong Zhang         ierr = TSForwardStep(ts);CHKERRQ(ierr);
3936715f1b00SHong Zhang       }
393710b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
3938e783b05fSHong Zhang       ierr = TSEventHandler(ts);CHKERRQ(ierr); /* The right-hand side may be changed due to event. Be careful with Any computation using the RHS information after this point. */
393958818c2dSLisandro Dalcin       if (ts->steprollback) {
394058818c2dSLisandro Dalcin         ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
394158818c2dSLisandro Dalcin       }
3942e783b05fSHong Zhang       if (!ts->steprollback) {
39432808aa04SLisandro Dalcin         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
39441eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
3945aeb4809dSShri Abhyankar       }
3946193ac0bcSJed Brown     }
39472808aa04SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
39486427ac75SLisandro Dalcin 
394949354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
39500910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
3951cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3952b06615a5SLisandro Dalcin       solution = u;
395363e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
39540574a7fbSJed Brown     } else {
3955ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3956cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3957b06615a5SLisandro Dalcin       solution = ts->vec_sol;
39580574a7fbSJed Brown     }
3959193ac0bcSJed Brown   }
3960d2daff3dSHong Zhang 
3961ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
396263e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
396356f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
3964ef222394SBarry Smith   if (ts->adjoint_solve) {
3965ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
39662b0a91c0SBarry Smith   }
39676a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
39686a4d4014SLisandro Dalcin }
39696a4d4014SLisandro Dalcin 
39707db568b7SBarry Smith /*@C
3971228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
3972228d79bcSJed Brown 
3973228d79bcSJed Brown    Collective on TS
3974228d79bcSJed Brown 
3975228d79bcSJed Brown    Input Parameters:
3976228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
3977228d79bcSJed Brown .  step - step number that has just completed
3978228d79bcSJed Brown .  ptime - model time of the state
39790910c330SBarry Smith -  u - state at the current model time
3980228d79bcSJed Brown 
3981228d79bcSJed Brown    Notes:
39827db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
39837db568b7SBarry Smith    Users would almost never call this routine directly.
3984228d79bcSJed Brown 
398563e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
398663e21af5SBarry Smith 
39877db568b7SBarry Smith    Level: developer
3988228d79bcSJed Brown 
3989228d79bcSJed Brown .keywords: TS, timestep
3990228d79bcSJed Brown @*/
39910910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
3992d763cef2SBarry Smith {
3993d6152f81SLisandro Dalcin   DM             dm;
3994a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
3995d6152f81SLisandro Dalcin   PetscErrorCode ierr;
3996d763cef2SBarry Smith 
3997d763cef2SBarry Smith   PetscFunctionBegin;
3998b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3999b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
4000d6152f81SLisandro Dalcin 
4001d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
4002d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
4003d6152f81SLisandro Dalcin 
40045edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
4005d763cef2SBarry Smith   for (i=0; i<n; i++) {
40060910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
4007d763cef2SBarry Smith   }
40085edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
4009d763cef2SBarry Smith   PetscFunctionReturn(0);
4010d763cef2SBarry Smith }
4011d763cef2SBarry Smith 
4012d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
4013d763cef2SBarry Smith /*@C
40147db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
4015a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
4016d763cef2SBarry Smith 
4017d763cef2SBarry Smith    Collective on TS
4018d763cef2SBarry Smith 
4019d763cef2SBarry Smith    Input Parameters:
4020d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
4021d763cef2SBarry Smith .  label - the title to put in the title bar
40227c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
4023a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
4024a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
4025d763cef2SBarry Smith 
4026d763cef2SBarry Smith    Output Parameter:
40270b039ecaSBarry Smith .  ctx - the context
4028d763cef2SBarry Smith 
4029d763cef2SBarry Smith    Options Database Key:
40304f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
40318b668821SLisandro Dalcin +  -ts_monitor_lg_timestep_log - automatically sets line graph monitor
40327db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
40337db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
40347db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
40357db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
4036b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
4037d763cef2SBarry Smith 
4038d763cef2SBarry Smith    Notes:
4039a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
4040d763cef2SBarry Smith 
40417db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
40427db568b7SBarry Smith 
40437db568b7SBarry Smith    Many of the functions that control the monitoring have two forms: TSMonitorLGSet/GetXXXX() and TSMonitorLGCtxSet/GetXXXX() the first take a TS object as the
40447db568b7SBarry Smith    first argument (if that TS object does not have a TSMonitorLGCtx associated with it the function call is ignored) and the second takes a TSMonitorLGCtx object
40457db568b7SBarry Smith    as the first argument.
40467db568b7SBarry Smith 
40477db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
40487db568b7SBarry Smith 
4049d763cef2SBarry Smith    Level: intermediate
4050d763cef2SBarry Smith 
40517db568b7SBarry Smith .keywords: TS, monitor, line graph, residual
4052d763cef2SBarry Smith 
40537db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
40547db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
40557db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
40567db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
40577db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
40587c922b88SBarry Smith 
4059d763cef2SBarry Smith @*/
4060a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
4061d763cef2SBarry Smith {
40627f52daa2SLisandro Dalcin   PetscDraw      draw;
4063dfbe8321SBarry Smith   PetscErrorCode ierr;
4064d763cef2SBarry Smith 
4065d763cef2SBarry Smith   PetscFunctionBegin;
4066b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
40677f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
40687f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
40697f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
4070287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
40717f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
4072a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
4073d763cef2SBarry Smith   PetscFunctionReturn(0);
4074d763cef2SBarry Smith }
4075d763cef2SBarry Smith 
4076b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4077d763cef2SBarry Smith {
40780b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4079c32365f1SBarry Smith   PetscReal      x   = ptime,y;
4080dfbe8321SBarry Smith   PetscErrorCode ierr;
4081d763cef2SBarry Smith 
4082d763cef2SBarry Smith   PetscFunctionBegin;
408363e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
4084b06615a5SLisandro Dalcin   if (!step) {
4085a9f9c1f6SBarry Smith     PetscDrawAxis axis;
40868b668821SLisandro Dalcin     const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step";
4087a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
40888b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr);
4089a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4090a9f9c1f6SBarry Smith   }
4091c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
40928b668821SLisandro Dalcin   if (ctx->semilogy) y = PetscLog10Real(y);
40930b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4094b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
40950b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
40966934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
40973923b477SBarry Smith   }
4098d763cef2SBarry Smith   PetscFunctionReturn(0);
4099d763cef2SBarry Smith }
4100d763cef2SBarry Smith 
4101d763cef2SBarry Smith /*@C
4102a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4103a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4104d763cef2SBarry Smith 
41050b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4106d763cef2SBarry Smith 
4107d763cef2SBarry Smith    Input Parameter:
41080b039ecaSBarry Smith .  ctx - the monitor context
4109d763cef2SBarry Smith 
4110d763cef2SBarry Smith    Level: intermediate
4111d763cef2SBarry Smith 
4112d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
4113d763cef2SBarry Smith 
41144f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4115d763cef2SBarry Smith @*/
4116a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4117d763cef2SBarry Smith {
4118dfbe8321SBarry Smith   PetscErrorCode ierr;
4119d763cef2SBarry Smith 
4120d763cef2SBarry Smith   PetscFunctionBegin;
41217684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
41227684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
41237684fa3eSBarry Smith   }
41240b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
412531152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4126387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4127387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4128387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
41290b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4130d763cef2SBarry Smith   PetscFunctionReturn(0);
4131d763cef2SBarry Smith }
4132d763cef2SBarry Smith 
4133d763cef2SBarry Smith /*@
4134b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4135d763cef2SBarry Smith 
4136d763cef2SBarry Smith    Not Collective
4137d763cef2SBarry Smith 
4138d763cef2SBarry Smith    Input Parameter:
4139d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4140d763cef2SBarry Smith 
4141d763cef2SBarry Smith    Output Parameter:
414219eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
4143d763cef2SBarry Smith 
4144d763cef2SBarry Smith    Level: beginner
4145d763cef2SBarry Smith 
4146b8123daeSJed Brown    Note:
4147b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
41489be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4149b8123daeSJed Brown 
4150aaa6c58dSLisandro Dalcin .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep()
4151d763cef2SBarry Smith 
4152d763cef2SBarry Smith .keywords: TS, get, time
4153d763cef2SBarry Smith @*/
41547087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4155d763cef2SBarry Smith {
4156d763cef2SBarry Smith   PetscFunctionBegin;
41570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4158f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4159d763cef2SBarry Smith   *t = ts->ptime;
4160d763cef2SBarry Smith   PetscFunctionReturn(0);
4161d763cef2SBarry Smith }
4162d763cef2SBarry Smith 
4163e5e524a1SHong Zhang /*@
4164e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4165e5e524a1SHong Zhang 
4166e5e524a1SHong Zhang    Not Collective
4167e5e524a1SHong Zhang 
4168e5e524a1SHong Zhang    Input Parameter:
4169e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4170e5e524a1SHong Zhang 
4171e5e524a1SHong Zhang    Output Parameter:
4172e5e524a1SHong Zhang .  t  - the previous time
4173e5e524a1SHong Zhang 
4174e5e524a1SHong Zhang    Level: beginner
4175e5e524a1SHong Zhang 
4176aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4177e5e524a1SHong Zhang 
4178e5e524a1SHong Zhang .keywords: TS, get, time
4179e5e524a1SHong Zhang @*/
4180e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4181e5e524a1SHong Zhang {
4182e5e524a1SHong Zhang   PetscFunctionBegin;
4183e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4184e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4185e5e524a1SHong Zhang   *t = ts->ptime_prev;
4186e5e524a1SHong Zhang   PetscFunctionReturn(0);
4187e5e524a1SHong Zhang }
4188e5e524a1SHong Zhang 
41896a4d4014SLisandro Dalcin /*@
41906a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
41916a4d4014SLisandro Dalcin 
41923f9fe445SBarry Smith    Logically Collective on TS
41936a4d4014SLisandro Dalcin 
41946a4d4014SLisandro Dalcin    Input Parameters:
41956a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
41966a4d4014SLisandro Dalcin -  time - the time
41976a4d4014SLisandro Dalcin 
41986a4d4014SLisandro Dalcin    Level: intermediate
41996a4d4014SLisandro Dalcin 
420019eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
42016a4d4014SLisandro Dalcin 
42026a4d4014SLisandro Dalcin .keywords: TS, set, time
42036a4d4014SLisandro Dalcin @*/
42047087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
42056a4d4014SLisandro Dalcin {
42066a4d4014SLisandro Dalcin   PetscFunctionBegin;
42070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4208c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
42096a4d4014SLisandro Dalcin   ts->ptime = t;
42106a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
42116a4d4014SLisandro Dalcin }
42126a4d4014SLisandro Dalcin 
4213d763cef2SBarry Smith /*@C
4214d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4215d763cef2SBarry Smith    TS options in the database.
4216d763cef2SBarry Smith 
42173f9fe445SBarry Smith    Logically Collective on TS
4218d763cef2SBarry Smith 
4219d763cef2SBarry Smith    Input Parameter:
4220d763cef2SBarry Smith +  ts     - The TS context
4221d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4222d763cef2SBarry Smith 
4223d763cef2SBarry Smith    Notes:
4224d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4225d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4226d763cef2SBarry Smith    hyphen.
4227d763cef2SBarry Smith 
4228d763cef2SBarry Smith    Level: advanced
4229d763cef2SBarry Smith 
4230d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
4231d763cef2SBarry Smith 
4232d763cef2SBarry Smith .seealso: TSSetFromOptions()
4233d763cef2SBarry Smith 
4234d763cef2SBarry Smith @*/
42357087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4236d763cef2SBarry Smith {
4237dfbe8321SBarry Smith   PetscErrorCode ierr;
4238089b2837SJed Brown   SNES           snes;
4239d763cef2SBarry Smith 
4240d763cef2SBarry Smith   PetscFunctionBegin;
42410700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4242d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4243089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4244089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4245d763cef2SBarry Smith   PetscFunctionReturn(0);
4246d763cef2SBarry Smith }
4247d763cef2SBarry Smith 
4248d763cef2SBarry Smith /*@C
4249d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4250d763cef2SBarry Smith    TS options in the database.
4251d763cef2SBarry Smith 
42523f9fe445SBarry Smith    Logically Collective on TS
4253d763cef2SBarry Smith 
4254d763cef2SBarry Smith    Input Parameter:
4255d763cef2SBarry Smith +  ts     - The TS context
4256d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4257d763cef2SBarry Smith 
4258d763cef2SBarry Smith    Notes:
4259d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4260d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4261d763cef2SBarry Smith    hyphen.
4262d763cef2SBarry Smith 
4263d763cef2SBarry Smith    Level: advanced
4264d763cef2SBarry Smith 
4265d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
4266d763cef2SBarry Smith 
4267d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4268d763cef2SBarry Smith 
4269d763cef2SBarry Smith @*/
42707087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4271d763cef2SBarry Smith {
4272dfbe8321SBarry Smith   PetscErrorCode ierr;
4273089b2837SJed Brown   SNES           snes;
4274d763cef2SBarry Smith 
4275d763cef2SBarry Smith   PetscFunctionBegin;
42760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4277d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4278089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4279089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4280d763cef2SBarry Smith   PetscFunctionReturn(0);
4281d763cef2SBarry Smith }
4282d763cef2SBarry Smith 
4283d763cef2SBarry Smith /*@C
4284d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4285d763cef2SBarry Smith    TS options in the database.
4286d763cef2SBarry Smith 
4287d763cef2SBarry Smith    Not Collective
4288d763cef2SBarry Smith 
4289d763cef2SBarry Smith    Input Parameter:
4290d763cef2SBarry Smith .  ts - The TS context
4291d763cef2SBarry Smith 
4292d763cef2SBarry Smith    Output Parameter:
4293d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4294d763cef2SBarry Smith 
429595452b02SPatrick Sanan    Notes:
429695452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prifix' of
4297d763cef2SBarry Smith    sufficient length to hold the prefix.
4298d763cef2SBarry Smith 
4299d763cef2SBarry Smith    Level: intermediate
4300d763cef2SBarry Smith 
4301d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
4302d763cef2SBarry Smith 
4303d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4304d763cef2SBarry Smith @*/
43057087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4306d763cef2SBarry Smith {
4307dfbe8321SBarry Smith   PetscErrorCode ierr;
4308d763cef2SBarry Smith 
4309d763cef2SBarry Smith   PetscFunctionBegin;
43100700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
43114482741eSBarry Smith   PetscValidPointer(prefix,2);
4312d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4313d763cef2SBarry Smith   PetscFunctionReturn(0);
4314d763cef2SBarry Smith }
4315d763cef2SBarry Smith 
4316d763cef2SBarry Smith /*@C
4317d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4318d763cef2SBarry Smith 
4319d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4320d763cef2SBarry Smith 
4321d763cef2SBarry Smith    Input Parameter:
4322d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4323d763cef2SBarry Smith 
4324d763cef2SBarry Smith    Output Parameters:
4325e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4326e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4327e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4328e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4329d763cef2SBarry Smith 
433095452b02SPatrick Sanan    Notes:
433195452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
4332d763cef2SBarry Smith 
4333d763cef2SBarry Smith    Level: intermediate
4334d763cef2SBarry Smith 
433580275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4336d763cef2SBarry Smith 
4337d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
4338d763cef2SBarry Smith @*/
4339e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4340d763cef2SBarry Smith {
4341089b2837SJed Brown   PetscErrorCode ierr;
434224989b8cSPeter Brune   DM             dm;
4343089b2837SJed Brown 
4344d763cef2SBarry Smith   PetscFunctionBegin;
434523a57915SBarry Smith   if (Amat || Pmat) {
434623a57915SBarry Smith     SNES snes;
4347089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
434823a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4349e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
435023a57915SBarry Smith   }
435124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
435224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4353d763cef2SBarry Smith   PetscFunctionReturn(0);
4354d763cef2SBarry Smith }
4355d763cef2SBarry Smith 
43562eca1d9cSJed Brown /*@C
43572eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
43582eca1d9cSJed Brown 
43592eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
43602eca1d9cSJed Brown 
43612eca1d9cSJed Brown    Input Parameter:
43622eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
43632eca1d9cSJed Brown 
43642eca1d9cSJed Brown    Output Parameters:
4365e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4366e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
43672eca1d9cSJed Brown .  f   - The function to compute the matrices
43682eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
43692eca1d9cSJed Brown 
437095452b02SPatrick Sanan    Notes:
437195452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
43722eca1d9cSJed Brown 
43732eca1d9cSJed Brown    Level: advanced
43742eca1d9cSJed Brown 
437580275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
43762eca1d9cSJed Brown 
43772eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
43782eca1d9cSJed Brown @*/
4379e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
43802eca1d9cSJed Brown {
4381089b2837SJed Brown   PetscErrorCode ierr;
438224989b8cSPeter Brune   DM             dm;
43830910c330SBarry Smith 
43842eca1d9cSJed Brown   PetscFunctionBegin;
4385c0aab802Sstefano_zampini   if (Amat || Pmat) {
4386c0aab802Sstefano_zampini     SNES snes;
4387089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4388f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4389e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4390c0aab802Sstefano_zampini   }
439124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
439224989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
43932eca1d9cSJed Brown   PetscFunctionReturn(0);
43942eca1d9cSJed Brown }
43952eca1d9cSJed Brown 
43961713a123SBarry Smith /*@C
439783a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
43981713a123SBarry Smith    VecView() for the solution at each timestep
43991713a123SBarry Smith 
44001713a123SBarry Smith    Collective on TS
44011713a123SBarry Smith 
44021713a123SBarry Smith    Input Parameters:
44031713a123SBarry Smith +  ts - the TS context
44041713a123SBarry Smith .  step - current time-step
4405142b95e3SSatish Balay .  ptime - current time
44060298fd71SBarry Smith -  dummy - either a viewer or NULL
44071713a123SBarry Smith 
440899fdda47SBarry Smith    Options Database:
440999fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
441099fdda47SBarry Smith 
441195452b02SPatrick Sanan    Notes:
441295452b02SPatrick Sanan     the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
441399fdda47SBarry Smith        will look bad
441499fdda47SBarry Smith 
44151713a123SBarry Smith    Level: intermediate
44161713a123SBarry Smith 
44171713a123SBarry Smith .keywords: TS,  vector, monitor, view
44181713a123SBarry Smith 
4419a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
44201713a123SBarry Smith @*/
44210910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
44221713a123SBarry Smith {
4423dfbe8321SBarry Smith   PetscErrorCode   ierr;
442483a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4425473a3ab2SBarry Smith   PetscDraw        draw;
44261713a123SBarry Smith 
44271713a123SBarry Smith   PetscFunctionBegin;
44286083293cSBarry Smith   if (!step && ictx->showinitial) {
44296083293cSBarry Smith     if (!ictx->initialsolution) {
44300910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
44311713a123SBarry Smith     }
44320910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
44336083293cSBarry Smith   }
4434b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
44350dcf80beSBarry Smith 
44366083293cSBarry Smith   if (ictx->showinitial) {
44376083293cSBarry Smith     PetscReal pause;
44386083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
44396083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
44406083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
44416083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
44426083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
44436083293cSBarry Smith   }
44440910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4445473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
444651fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4447473a3ab2SBarry Smith     char      time[32];
4448473a3ab2SBarry Smith 
4449473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
445085b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4451473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4452473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
445351fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4454473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4455473a3ab2SBarry Smith   }
4456473a3ab2SBarry Smith 
44576083293cSBarry Smith   if (ictx->showinitial) {
44586083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
44596083293cSBarry Smith   }
44601713a123SBarry Smith   PetscFunctionReturn(0);
44611713a123SBarry Smith }
44621713a123SBarry Smith 
44639110b2e7SHong Zhang /*@C
44642d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
44652d5ee99bSBarry Smith 
44662d5ee99bSBarry Smith    Collective on TS
44672d5ee99bSBarry Smith 
44682d5ee99bSBarry Smith    Input Parameters:
44692d5ee99bSBarry Smith +  ts - the TS context
44702d5ee99bSBarry Smith .  step - current time-step
44712d5ee99bSBarry Smith .  ptime - current time
44722d5ee99bSBarry Smith -  dummy - either a viewer or NULL
44732d5ee99bSBarry Smith 
44742d5ee99bSBarry Smith    Level: intermediate
44752d5ee99bSBarry Smith 
44762d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
44772d5ee99bSBarry Smith 
44782d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
44792d5ee99bSBarry Smith @*/
44802d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
44812d5ee99bSBarry Smith {
44822d5ee99bSBarry Smith   PetscErrorCode    ierr;
44832d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
44842d5ee99bSBarry Smith   PetscDraw         draw;
44856934998bSLisandro Dalcin   PetscDrawAxis     axis;
44862d5ee99bSBarry Smith   PetscInt          n;
44872d5ee99bSBarry Smith   PetscMPIInt       size;
44886934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
44892d5ee99bSBarry Smith   char              time[32];
44902d5ee99bSBarry Smith   const PetscScalar *U;
44912d5ee99bSBarry Smith 
44922d5ee99bSBarry Smith   PetscFunctionBegin;
44936934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
44946934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
44952d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
44966934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
44972d5ee99bSBarry Smith 
44982d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
44996934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
45006934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
45016934998bSLisandro Dalcin   if (!step) {
45026934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
45036934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
45046934998bSLisandro Dalcin   }
45052d5ee99bSBarry Smith 
45062d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
45076934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
45086934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4509a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
45106934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
45112d5ee99bSBarry Smith 
45126934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
45136934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
45142d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
45154b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
451685b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
45172d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
451851fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
45192d5ee99bSBarry Smith   }
45206934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
45212d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
452256d62c8fSLisandro Dalcin   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
45236934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
45242d5ee99bSBarry Smith   PetscFunctionReturn(0);
45252d5ee99bSBarry Smith }
45262d5ee99bSBarry Smith 
45276083293cSBarry Smith /*@C
452883a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
45296083293cSBarry Smith 
45306083293cSBarry Smith    Collective on TS
45316083293cSBarry Smith 
45326083293cSBarry Smith    Input Parameters:
45336083293cSBarry Smith .    ctx - the monitor context
45346083293cSBarry Smith 
45356083293cSBarry Smith    Level: intermediate
45366083293cSBarry Smith 
45376083293cSBarry Smith .keywords: TS,  vector, monitor, view
45386083293cSBarry Smith 
453983a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
45406083293cSBarry Smith @*/
454183a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
45426083293cSBarry Smith {
45436083293cSBarry Smith   PetscErrorCode ierr;
45446083293cSBarry Smith 
45456083293cSBarry Smith   PetscFunctionBegin;
454683a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
454783a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
454883a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
45496083293cSBarry Smith   PetscFunctionReturn(0);
45506083293cSBarry Smith }
45516083293cSBarry Smith 
45526083293cSBarry Smith /*@C
455383a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
45546083293cSBarry Smith 
45556083293cSBarry Smith    Collective on TS
45566083293cSBarry Smith 
45576083293cSBarry Smith    Input Parameter:
45586083293cSBarry Smith .    ts - time-step context
45596083293cSBarry Smith 
45606083293cSBarry Smith    Output Patameter:
45616083293cSBarry Smith .    ctx - the monitor context
45626083293cSBarry Smith 
456399fdda47SBarry Smith    Options Database:
456499fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
456599fdda47SBarry Smith 
45666083293cSBarry Smith    Level: intermediate
45676083293cSBarry Smith 
45686083293cSBarry Smith .keywords: TS,  vector, monitor, view
45696083293cSBarry Smith 
457083a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
45716083293cSBarry Smith @*/
457283a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
45736083293cSBarry Smith {
45746083293cSBarry Smith   PetscErrorCode   ierr;
45756083293cSBarry Smith 
45766083293cSBarry Smith   PetscFunctionBegin;
4577b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
457883a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4579e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4580bbd56ea5SKarl Rupp 
458183a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4582473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4583c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4584473a3ab2SBarry Smith 
4585473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4586c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
45876083293cSBarry Smith   PetscFunctionReturn(0);
45886083293cSBarry Smith }
45896083293cSBarry Smith 
45903a471f94SBarry Smith /*@C
45910ed3bfb6SBarry Smith    TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling
45920ed3bfb6SBarry Smith    VecView() for the solution provided by TSSetSolutionFunction() at each timestep
45930ed3bfb6SBarry Smith 
45940ed3bfb6SBarry Smith    Collective on TS
45950ed3bfb6SBarry Smith 
45960ed3bfb6SBarry Smith    Input Parameters:
45970ed3bfb6SBarry Smith +  ts - the TS context
45980ed3bfb6SBarry Smith .  step - current time-step
45990ed3bfb6SBarry Smith .  ptime - current time
46000ed3bfb6SBarry Smith -  dummy - either a viewer or NULL
46010ed3bfb6SBarry Smith 
46020ed3bfb6SBarry Smith    Options Database:
46030ed3bfb6SBarry Smith .  -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
46040ed3bfb6SBarry Smith 
46050ed3bfb6SBarry Smith    Level: intermediate
46060ed3bfb6SBarry Smith 
46070ed3bfb6SBarry Smith .keywords: TS,  vector, monitor, view
46080ed3bfb6SBarry Smith 
46090ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
46100ed3bfb6SBarry Smith @*/
46110ed3bfb6SBarry Smith PetscErrorCode  TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
46120ed3bfb6SBarry Smith {
46130ed3bfb6SBarry Smith   PetscErrorCode   ierr;
46140ed3bfb6SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
46150ed3bfb6SBarry Smith   PetscViewer      viewer = ctx->viewer;
46160ed3bfb6SBarry Smith   Vec              work;
46170ed3bfb6SBarry Smith 
46180ed3bfb6SBarry Smith   PetscFunctionBegin;
46190ed3bfb6SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
46200ed3bfb6SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
46210ed3bfb6SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
46220ed3bfb6SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
46230ed3bfb6SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
46240ed3bfb6SBarry Smith   PetscFunctionReturn(0);
46250ed3bfb6SBarry Smith }
46260ed3bfb6SBarry Smith 
46270ed3bfb6SBarry Smith /*@C
462883a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
46293a471f94SBarry Smith    VecView() for the error at each timestep
46303a471f94SBarry Smith 
46313a471f94SBarry Smith    Collective on TS
46323a471f94SBarry Smith 
46333a471f94SBarry Smith    Input Parameters:
46343a471f94SBarry Smith +  ts - the TS context
46353a471f94SBarry Smith .  step - current time-step
46363a471f94SBarry Smith .  ptime - current time
46370298fd71SBarry Smith -  dummy - either a viewer or NULL
46383a471f94SBarry Smith 
46390ed3bfb6SBarry Smith    Options Database:
46400ed3bfb6SBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
46410ed3bfb6SBarry Smith 
46423a471f94SBarry Smith    Level: intermediate
46433a471f94SBarry Smith 
46443a471f94SBarry Smith .keywords: TS,  vector, monitor, view
46453a471f94SBarry Smith 
46460ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
46473a471f94SBarry Smith @*/
46480910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
46493a471f94SBarry Smith {
46503a471f94SBarry Smith   PetscErrorCode   ierr;
465183a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
465283a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
46533a471f94SBarry Smith   Vec              work;
46543a471f94SBarry Smith 
46553a471f94SBarry Smith   PetscFunctionBegin;
4656b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
46570910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
46583a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
46590910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
46603a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
46613a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
46623a471f94SBarry Smith   PetscFunctionReturn(0);
46633a471f94SBarry Smith }
46643a471f94SBarry Smith 
4665af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
46666c699258SBarry Smith /*@
46672a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
46686c699258SBarry Smith 
46693f9fe445SBarry Smith    Logically Collective on TS and DM
46706c699258SBarry Smith 
46716c699258SBarry Smith    Input Parameters:
46722a808120SBarry Smith +  ts - the ODE integrator object
46732a808120SBarry Smith -  dm - the dm, cannot be NULL
46746c699258SBarry Smith 
46756c699258SBarry Smith    Level: intermediate
46766c699258SBarry Smith 
46776c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
46786c699258SBarry Smith @*/
46797087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
46806c699258SBarry Smith {
46816c699258SBarry Smith   PetscErrorCode ierr;
4682089b2837SJed Brown   SNES           snes;
4683942e3340SBarry Smith   DMTS           tsdm;
46846c699258SBarry Smith 
46856c699258SBarry Smith   PetscFunctionBegin;
46860700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
46872a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
468870663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4689942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
46902a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4691942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4692942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
469324989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
469424989b8cSPeter Brune         tsdm->originaldm = dm;
469524989b8cSPeter Brune       }
469624989b8cSPeter Brune     }
46976bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
469824989b8cSPeter Brune   }
46996c699258SBarry Smith   ts->dm = dm;
4700bbd56ea5SKarl Rupp 
4701089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4702089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
47036c699258SBarry Smith   PetscFunctionReturn(0);
47046c699258SBarry Smith }
47056c699258SBarry Smith 
47066c699258SBarry Smith /*@
47076c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
47086c699258SBarry Smith 
47093f9fe445SBarry Smith    Not Collective
47106c699258SBarry Smith 
47116c699258SBarry Smith    Input Parameter:
47126c699258SBarry Smith . ts - the preconditioner context
47136c699258SBarry Smith 
47146c699258SBarry Smith    Output Parameter:
47156c699258SBarry Smith .  dm - the dm
47166c699258SBarry Smith 
47176c699258SBarry Smith    Level: intermediate
47186c699258SBarry Smith 
47196c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
47206c699258SBarry Smith @*/
47217087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
47226c699258SBarry Smith {
4723496e6a7aSJed Brown   PetscErrorCode ierr;
4724496e6a7aSJed Brown 
47256c699258SBarry Smith   PetscFunctionBegin;
47260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4727496e6a7aSJed Brown   if (!ts->dm) {
4728ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4729496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4730496e6a7aSJed Brown   }
47316c699258SBarry Smith   *dm = ts->dm;
47326c699258SBarry Smith   PetscFunctionReturn(0);
47336c699258SBarry Smith }
47341713a123SBarry Smith 
47350f5c6efeSJed Brown /*@
47360f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
47370f5c6efeSJed Brown 
47383f9fe445SBarry Smith    Logically Collective on SNES
47390f5c6efeSJed Brown 
47400f5c6efeSJed Brown    Input Parameter:
4741d42a1c89SJed Brown + snes - nonlinear solver
47420910c330SBarry Smith . U - the current state at which to evaluate the residual
4743d42a1c89SJed Brown - ctx - user context, must be a TS
47440f5c6efeSJed Brown 
47450f5c6efeSJed Brown    Output Parameter:
47460f5c6efeSJed Brown . F - the nonlinear residual
47470f5c6efeSJed Brown 
47480f5c6efeSJed Brown    Notes:
47490f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
47500f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
47510f5c6efeSJed Brown 
47520f5c6efeSJed Brown    Level: advanced
47530f5c6efeSJed Brown 
47540f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
47550f5c6efeSJed Brown @*/
47560910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
47570f5c6efeSJed Brown {
47580f5c6efeSJed Brown   TS             ts = (TS)ctx;
47590f5c6efeSJed Brown   PetscErrorCode ierr;
47600f5c6efeSJed Brown 
47610f5c6efeSJed Brown   PetscFunctionBegin;
47620f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
47630910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
47640f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
47650f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
47660910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
47670f5c6efeSJed Brown   PetscFunctionReturn(0);
47680f5c6efeSJed Brown }
47690f5c6efeSJed Brown 
47700f5c6efeSJed Brown /*@
47710f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
47720f5c6efeSJed Brown 
47730f5c6efeSJed Brown    Collective on SNES
47740f5c6efeSJed Brown 
47750f5c6efeSJed Brown    Input Parameter:
47760f5c6efeSJed Brown + snes - nonlinear solver
47770910c330SBarry Smith . U - the current state at which to evaluate the residual
47780f5c6efeSJed Brown - ctx - user context, must be a TS
47790f5c6efeSJed Brown 
47800f5c6efeSJed Brown    Output Parameter:
47810f5c6efeSJed Brown + A - the Jacobian
47820f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
47830f5c6efeSJed Brown - flag - indicates any structure change in the matrix
47840f5c6efeSJed Brown 
47850f5c6efeSJed Brown    Notes:
47860f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
47870f5c6efeSJed Brown 
47880f5c6efeSJed Brown    Level: developer
47890f5c6efeSJed Brown 
47900f5c6efeSJed Brown .seealso: SNESSetJacobian()
47910f5c6efeSJed Brown @*/
4792d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
47930f5c6efeSJed Brown {
47940f5c6efeSJed Brown   TS             ts = (TS)ctx;
47950f5c6efeSJed Brown   PetscErrorCode ierr;
47960f5c6efeSJed Brown 
47970f5c6efeSJed Brown   PetscFunctionBegin;
47980f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
47990910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
48000f5c6efeSJed Brown   PetscValidPointer(A,3);
480194ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
48020f5c6efeSJed Brown   PetscValidPointer(B,4);
480394ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
48040f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4805d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
48060f5c6efeSJed Brown   PetscFunctionReturn(0);
48070f5c6efeSJed Brown }
4808325fc9f4SBarry Smith 
48090e4ef248SJed Brown /*@C
48109ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
48110e4ef248SJed Brown 
48120e4ef248SJed Brown    Collective on TS
48130e4ef248SJed Brown 
48140e4ef248SJed Brown    Input Arguments:
48150e4ef248SJed Brown +  ts - time stepping context
48160e4ef248SJed Brown .  t - time at which to evaluate
48170910c330SBarry Smith .  U - state at which to evaluate
48180e4ef248SJed Brown -  ctx - context
48190e4ef248SJed Brown 
48200e4ef248SJed Brown    Output Arguments:
48210e4ef248SJed Brown .  F - right hand side
48220e4ef248SJed Brown 
48230e4ef248SJed Brown    Level: intermediate
48240e4ef248SJed Brown 
48250e4ef248SJed Brown    Notes:
48260e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
48270e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
48280e4ef248SJed Brown 
48290e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
48300e4ef248SJed Brown @*/
48310910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
48320e4ef248SJed Brown {
48330e4ef248SJed Brown   PetscErrorCode ierr;
48340e4ef248SJed Brown   Mat            Arhs,Brhs;
48350e4ef248SJed Brown 
48360e4ef248SJed Brown   PetscFunctionBegin;
48370e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
4838d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
48390910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
48400e4ef248SJed Brown   PetscFunctionReturn(0);
48410e4ef248SJed Brown }
48420e4ef248SJed Brown 
48430e4ef248SJed Brown /*@C
48440e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
48450e4ef248SJed Brown 
48460e4ef248SJed Brown    Collective on TS
48470e4ef248SJed Brown 
48480e4ef248SJed Brown    Input Arguments:
48490e4ef248SJed Brown +  ts - time stepping context
48500e4ef248SJed Brown .  t - time at which to evaluate
48510910c330SBarry Smith .  U - state at which to evaluate
48520e4ef248SJed Brown -  ctx - context
48530e4ef248SJed Brown 
48540e4ef248SJed Brown    Output Arguments:
48550e4ef248SJed Brown +  A - pointer to operator
48560e4ef248SJed Brown .  B - pointer to preconditioning matrix
48570e4ef248SJed Brown -  flg - matrix structure flag
48580e4ef248SJed Brown 
48590e4ef248SJed Brown    Level: intermediate
48600e4ef248SJed Brown 
48610e4ef248SJed Brown    Notes:
48620e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
48630e4ef248SJed Brown 
48640e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
48650e4ef248SJed Brown @*/
4866d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
48670e4ef248SJed Brown {
48680e4ef248SJed Brown   PetscFunctionBegin;
48690e4ef248SJed Brown   PetscFunctionReturn(0);
48700e4ef248SJed Brown }
48710e4ef248SJed Brown 
48720026cea9SSean Farley /*@C
48730026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
48740026cea9SSean Farley 
48750026cea9SSean Farley    Collective on TS
48760026cea9SSean Farley 
48770026cea9SSean Farley    Input Arguments:
48780026cea9SSean Farley +  ts - time stepping context
48790026cea9SSean Farley .  t - time at which to evaluate
48800910c330SBarry Smith .  U - state at which to evaluate
48810910c330SBarry Smith .  Udot - time derivative of state vector
48820026cea9SSean Farley -  ctx - context
48830026cea9SSean Farley 
48840026cea9SSean Farley    Output Arguments:
48850026cea9SSean Farley .  F - left hand side
48860026cea9SSean Farley 
48870026cea9SSean Farley    Level: intermediate
48880026cea9SSean Farley 
48890026cea9SSean Farley    Notes:
48900910c330SBarry 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
48910026cea9SSean Farley    user is required to write their own TSComputeIFunction.
48920026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
48930026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
48940026cea9SSean Farley 
48959ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
48969ae8fd06SBarry Smith 
48979ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
48980026cea9SSean Farley @*/
48990910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
49000026cea9SSean Farley {
49010026cea9SSean Farley   PetscErrorCode ierr;
49020026cea9SSean Farley   Mat            A,B;
49030026cea9SSean Farley 
49040026cea9SSean Farley   PetscFunctionBegin;
49050298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
4906d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
49070910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
49080026cea9SSean Farley   PetscFunctionReturn(0);
49090026cea9SSean Farley }
49100026cea9SSean Farley 
49110026cea9SSean Farley /*@C
4912b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
49130026cea9SSean Farley 
49140026cea9SSean Farley    Collective on TS
49150026cea9SSean Farley 
49160026cea9SSean Farley    Input Arguments:
49170026cea9SSean Farley +  ts - time stepping context
49180026cea9SSean Farley .  t - time at which to evaluate
49190910c330SBarry Smith .  U - state at which to evaluate
49200910c330SBarry Smith .  Udot - time derivative of state vector
49210026cea9SSean Farley .  shift - shift to apply
49220026cea9SSean Farley -  ctx - context
49230026cea9SSean Farley 
49240026cea9SSean Farley    Output Arguments:
49250026cea9SSean Farley +  A - pointer to operator
49260026cea9SSean Farley .  B - pointer to preconditioning matrix
49270026cea9SSean Farley -  flg - matrix structure flag
49280026cea9SSean Farley 
4929b41af12eSJed Brown    Level: advanced
49300026cea9SSean Farley 
49310026cea9SSean Farley    Notes:
49320026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
49330026cea9SSean Farley 
4934b41af12eSJed Brown    It is only appropriate for problems of the form
4935b41af12eSJed Brown 
4936b41af12eSJed Brown $     M Udot = F(U,t)
4937b41af12eSJed Brown 
4938b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4939b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4940b41af12eSJed Brown   an implicit operator of the form
4941b41af12eSJed Brown 
4942b41af12eSJed Brown $    shift*M + J
4943b41af12eSJed Brown 
4944b41af12eSJed 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
4945b41af12eSJed Brown   a copy of M or reassemble it when requested.
4946b41af12eSJed Brown 
49470026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
49480026cea9SSean Farley @*/
4949d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
49500026cea9SSean Farley {
4951b41af12eSJed Brown   PetscErrorCode ierr;
4952b41af12eSJed Brown 
49530026cea9SSean Farley   PetscFunctionBegin;
495494ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4955b41af12eSJed Brown   ts->ijacobian.shift = shift;
49560026cea9SSean Farley   PetscFunctionReturn(0);
49570026cea9SSean Farley }
4958b41af12eSJed Brown 
4959e817cc15SEmil Constantinescu /*@
4960e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4961e817cc15SEmil Constantinescu 
4962e817cc15SEmil Constantinescu    Not Collective
4963e817cc15SEmil Constantinescu 
4964e817cc15SEmil Constantinescu    Input Parameter:
4965e817cc15SEmil Constantinescu .  ts - the TS context
4966e817cc15SEmil Constantinescu 
4967e817cc15SEmil Constantinescu    Output Parameter:
49684e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4969e817cc15SEmil Constantinescu 
4970e817cc15SEmil Constantinescu    Level: beginner
4971e817cc15SEmil Constantinescu 
4972e817cc15SEmil Constantinescu .keywords: TS, equation type
4973e817cc15SEmil Constantinescu 
4974e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4975e817cc15SEmil Constantinescu @*/
4976e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4977e817cc15SEmil Constantinescu {
4978e817cc15SEmil Constantinescu   PetscFunctionBegin;
4979e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4980e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4981e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4982e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4983e817cc15SEmil Constantinescu }
4984e817cc15SEmil Constantinescu 
4985e817cc15SEmil Constantinescu /*@
4986e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4987e817cc15SEmil Constantinescu 
4988e817cc15SEmil Constantinescu    Not Collective
4989e817cc15SEmil Constantinescu 
4990e817cc15SEmil Constantinescu    Input Parameter:
4991e817cc15SEmil Constantinescu +  ts - the TS context
49921297b224SEmil Constantinescu -  equation_type - see TSEquationType
4993e817cc15SEmil Constantinescu 
4994e817cc15SEmil Constantinescu    Level: advanced
4995e817cc15SEmil Constantinescu 
4996e817cc15SEmil Constantinescu .keywords: TS, equation type
4997e817cc15SEmil Constantinescu 
4998e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4999e817cc15SEmil Constantinescu @*/
5000e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
5001e817cc15SEmil Constantinescu {
5002e817cc15SEmil Constantinescu   PetscFunctionBegin;
5003e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5004e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
5005e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
5006e817cc15SEmil Constantinescu }
50070026cea9SSean Farley 
50084af1b03aSJed Brown /*@
50094af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
50104af1b03aSJed Brown 
50114af1b03aSJed Brown    Not Collective
50124af1b03aSJed Brown 
50134af1b03aSJed Brown    Input Parameter:
50144af1b03aSJed Brown .  ts - the TS context
50154af1b03aSJed Brown 
50164af1b03aSJed Brown    Output Parameter:
50174af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
50184af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
50194af1b03aSJed Brown 
5020487e0bb9SJed Brown    Level: beginner
50214af1b03aSJed Brown 
5022cd652676SJed Brown    Notes:
5023cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
50244af1b03aSJed Brown 
50254af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
50264af1b03aSJed Brown 
50274af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
50284af1b03aSJed Brown @*/
50294af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
50304af1b03aSJed Brown {
50314af1b03aSJed Brown   PetscFunctionBegin;
50324af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
50334af1b03aSJed Brown   PetscValidPointer(reason,2);
50344af1b03aSJed Brown   *reason = ts->reason;
50354af1b03aSJed Brown   PetscFunctionReturn(0);
50364af1b03aSJed Brown }
50374af1b03aSJed Brown 
5038d6ad946cSShri Abhyankar /*@
5039d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
5040d6ad946cSShri Abhyankar 
5041d6ad946cSShri Abhyankar    Not Collective
5042d6ad946cSShri Abhyankar 
5043d6ad946cSShri Abhyankar    Input Parameter:
5044d6ad946cSShri Abhyankar +  ts - the TS context
5045d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5046d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
5047d6ad946cSShri Abhyankar 
5048f5abba47SShri Abhyankar    Level: advanced
5049d6ad946cSShri Abhyankar 
5050d6ad946cSShri Abhyankar    Notes:
5051d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
5052d6ad946cSShri Abhyankar 
5053d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
5054d6ad946cSShri Abhyankar 
5055d6ad946cSShri Abhyankar .seealso: TSConvergedReason
5056d6ad946cSShri Abhyankar @*/
5057d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5058d6ad946cSShri Abhyankar {
5059d6ad946cSShri Abhyankar   PetscFunctionBegin;
5060d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5061d6ad946cSShri Abhyankar   ts->reason = reason;
5062d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
5063d6ad946cSShri Abhyankar }
5064d6ad946cSShri Abhyankar 
5065cc708dedSBarry Smith /*@
5066cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
5067cc708dedSBarry Smith 
5068cc708dedSBarry Smith    Not Collective
5069cc708dedSBarry Smith 
5070cc708dedSBarry Smith    Input Parameter:
5071cc708dedSBarry Smith .  ts - the TS context
5072cc708dedSBarry Smith 
5073cc708dedSBarry Smith    Output Parameter:
507419eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
5075cc708dedSBarry Smith 
5076487e0bb9SJed Brown    Level: beginner
5077cc708dedSBarry Smith 
5078cc708dedSBarry Smith    Notes:
5079cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
5080cc708dedSBarry Smith 
5081cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
5082cc708dedSBarry Smith 
5083cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
5084cc708dedSBarry Smith @*/
5085cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5086cc708dedSBarry Smith {
5087cc708dedSBarry Smith   PetscFunctionBegin;
5088cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5089cc708dedSBarry Smith   PetscValidPointer(ftime,2);
5090cc708dedSBarry Smith   *ftime = ts->solvetime;
5091cc708dedSBarry Smith   PetscFunctionReturn(0);
5092cc708dedSBarry Smith }
5093cc708dedSBarry Smith 
50942c18e0fdSBarry Smith /*@
50955ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
50969f67acb7SJed Brown    used by the time integrator.
50979f67acb7SJed Brown 
50989f67acb7SJed Brown    Not Collective
50999f67acb7SJed Brown 
51009f67acb7SJed Brown    Input Parameter:
51019f67acb7SJed Brown .  ts - TS context
51029f67acb7SJed Brown 
51039f67acb7SJed Brown    Output Parameter:
51049f67acb7SJed Brown .  nits - number of nonlinear iterations
51059f67acb7SJed Brown 
51069f67acb7SJed Brown    Notes:
51079f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
51089f67acb7SJed Brown 
51099f67acb7SJed Brown    Level: intermediate
51109f67acb7SJed Brown 
51119f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
51129f67acb7SJed Brown 
51135ef26d82SJed Brown .seealso:  TSGetKSPIterations()
51149f67acb7SJed Brown @*/
51155ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
51169f67acb7SJed Brown {
51179f67acb7SJed Brown   PetscFunctionBegin;
51189f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
51199f67acb7SJed Brown   PetscValidIntPointer(nits,2);
51205ef26d82SJed Brown   *nits = ts->snes_its;
51219f67acb7SJed Brown   PetscFunctionReturn(0);
51229f67acb7SJed Brown }
51239f67acb7SJed Brown 
51249f67acb7SJed Brown /*@
51255ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
51269f67acb7SJed Brown    used by the time integrator.
51279f67acb7SJed Brown 
51289f67acb7SJed Brown    Not Collective
51299f67acb7SJed Brown 
51309f67acb7SJed Brown    Input Parameter:
51319f67acb7SJed Brown .  ts - TS context
51329f67acb7SJed Brown 
51339f67acb7SJed Brown    Output Parameter:
51349f67acb7SJed Brown .  lits - number of linear iterations
51359f67acb7SJed Brown 
51369f67acb7SJed Brown    Notes:
51379f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
51389f67acb7SJed Brown 
51399f67acb7SJed Brown    Level: intermediate
51409f67acb7SJed Brown 
51419f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
51429f67acb7SJed Brown 
51435ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
51449f67acb7SJed Brown @*/
51455ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
51469f67acb7SJed Brown {
51479f67acb7SJed Brown   PetscFunctionBegin;
51489f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
51499f67acb7SJed Brown   PetscValidIntPointer(lits,2);
51505ef26d82SJed Brown   *lits = ts->ksp_its;
51519f67acb7SJed Brown   PetscFunctionReturn(0);
51529f67acb7SJed Brown }
51539f67acb7SJed Brown 
5154cef5090cSJed Brown /*@
5155cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5156cef5090cSJed Brown 
5157cef5090cSJed Brown    Not Collective
5158cef5090cSJed Brown 
5159cef5090cSJed Brown    Input Parameter:
5160cef5090cSJed Brown .  ts - TS context
5161cef5090cSJed Brown 
5162cef5090cSJed Brown    Output Parameter:
5163cef5090cSJed Brown .  rejects - number of steps rejected
5164cef5090cSJed Brown 
5165cef5090cSJed Brown    Notes:
5166cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5167cef5090cSJed Brown 
5168cef5090cSJed Brown    Level: intermediate
5169cef5090cSJed Brown 
5170cef5090cSJed Brown .keywords: TS, get, number
5171cef5090cSJed Brown 
51725ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5173cef5090cSJed Brown @*/
5174cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5175cef5090cSJed Brown {
5176cef5090cSJed Brown   PetscFunctionBegin;
5177cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5178cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5179cef5090cSJed Brown   *rejects = ts->reject;
5180cef5090cSJed Brown   PetscFunctionReturn(0);
5181cef5090cSJed Brown }
5182cef5090cSJed Brown 
5183cef5090cSJed Brown /*@
5184cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5185cef5090cSJed Brown 
5186cef5090cSJed Brown    Not Collective
5187cef5090cSJed Brown 
5188cef5090cSJed Brown    Input Parameter:
5189cef5090cSJed Brown .  ts - TS context
5190cef5090cSJed Brown 
5191cef5090cSJed Brown    Output Parameter:
5192cef5090cSJed Brown .  fails - number of failed nonlinear solves
5193cef5090cSJed Brown 
5194cef5090cSJed Brown    Notes:
5195cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5196cef5090cSJed Brown 
5197cef5090cSJed Brown    Level: intermediate
5198cef5090cSJed Brown 
5199cef5090cSJed Brown .keywords: TS, get, number
5200cef5090cSJed Brown 
52015ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5202cef5090cSJed Brown @*/
5203cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5204cef5090cSJed Brown {
5205cef5090cSJed Brown   PetscFunctionBegin;
5206cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5207cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5208cef5090cSJed Brown   *fails = ts->num_snes_failures;
5209cef5090cSJed Brown   PetscFunctionReturn(0);
5210cef5090cSJed Brown }
5211cef5090cSJed Brown 
5212cef5090cSJed Brown /*@
5213cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5214cef5090cSJed Brown 
5215cef5090cSJed Brown    Not Collective
5216cef5090cSJed Brown 
5217cef5090cSJed Brown    Input Parameter:
5218cef5090cSJed Brown +  ts - TS context
5219cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5220cef5090cSJed Brown 
5221cef5090cSJed Brown    Notes:
5222cef5090cSJed Brown    The counter is reset to zero for each step
5223cef5090cSJed Brown 
5224cef5090cSJed Brown    Options Database Key:
5225cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5226cef5090cSJed Brown 
5227cef5090cSJed Brown    Level: intermediate
5228cef5090cSJed Brown 
5229cef5090cSJed Brown .keywords: TS, set, maximum, number
5230cef5090cSJed Brown 
52315ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5232cef5090cSJed Brown @*/
5233cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5234cef5090cSJed Brown {
5235cef5090cSJed Brown   PetscFunctionBegin;
5236cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5237cef5090cSJed Brown   ts->max_reject = rejects;
5238cef5090cSJed Brown   PetscFunctionReturn(0);
5239cef5090cSJed Brown }
5240cef5090cSJed Brown 
5241cef5090cSJed Brown /*@
5242cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5243cef5090cSJed Brown 
5244cef5090cSJed Brown    Not Collective
5245cef5090cSJed Brown 
5246cef5090cSJed Brown    Input Parameter:
5247cef5090cSJed Brown +  ts - TS context
5248cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5249cef5090cSJed Brown 
5250cef5090cSJed Brown    Notes:
5251cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5252cef5090cSJed Brown 
5253cef5090cSJed Brown    Options Database Key:
5254cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5255cef5090cSJed Brown 
5256cef5090cSJed Brown    Level: intermediate
5257cef5090cSJed Brown 
5258cef5090cSJed Brown .keywords: TS, set, maximum, number
5259cef5090cSJed Brown 
52605ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5261cef5090cSJed Brown @*/
5262cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5263cef5090cSJed Brown {
5264cef5090cSJed Brown   PetscFunctionBegin;
5265cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5266cef5090cSJed Brown   ts->max_snes_failures = fails;
5267cef5090cSJed Brown   PetscFunctionReturn(0);
5268cef5090cSJed Brown }
5269cef5090cSJed Brown 
5270cef5090cSJed Brown /*@
5271cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5272cef5090cSJed Brown 
5273cef5090cSJed Brown    Not Collective
5274cef5090cSJed Brown 
5275cef5090cSJed Brown    Input Parameter:
5276cef5090cSJed Brown +  ts - TS context
5277cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5278cef5090cSJed Brown 
5279cef5090cSJed Brown    Options Database Key:
5280cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5281cef5090cSJed Brown 
5282cef5090cSJed Brown    Level: intermediate
5283cef5090cSJed Brown 
5284cef5090cSJed Brown .keywords: TS, set, error
5285cef5090cSJed Brown 
52865ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5287cef5090cSJed Brown @*/
5288cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5289cef5090cSJed Brown {
5290cef5090cSJed Brown   PetscFunctionBegin;
5291cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5292cef5090cSJed Brown   ts->errorifstepfailed = err;
5293cef5090cSJed Brown   PetscFunctionReturn(0);
5294cef5090cSJed Brown }
5295cef5090cSJed Brown 
5296fb1732b5SBarry Smith /*@C
5297fde5950dSBarry Smith    TSMonitorSolution - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file or a PetscDraw object
5298fb1732b5SBarry Smith 
5299fb1732b5SBarry Smith    Collective on TS
5300fb1732b5SBarry Smith 
5301fb1732b5SBarry Smith    Input Parameters:
5302fb1732b5SBarry Smith +  ts - the TS context
5303fb1732b5SBarry Smith .  step - current time-step
5304fb1732b5SBarry Smith .  ptime - current time
53050910c330SBarry Smith .  u - current state
5306721cd6eeSBarry Smith -  vf - viewer and its format
5307fb1732b5SBarry Smith 
5308fb1732b5SBarry Smith    Level: intermediate
5309fb1732b5SBarry Smith 
5310fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
5311fb1732b5SBarry Smith 
5312fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5313fb1732b5SBarry Smith @*/
5314721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5315fb1732b5SBarry Smith {
5316fb1732b5SBarry Smith   PetscErrorCode ierr;
5317fb1732b5SBarry Smith 
5318fb1732b5SBarry Smith   PetscFunctionBegin;
5319721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5320721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5321721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5322ed81e22dSJed Brown   PetscFunctionReturn(0);
5323ed81e22dSJed Brown }
5324ed81e22dSJed Brown 
5325ed81e22dSJed Brown /*@C
5326ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5327ed81e22dSJed Brown 
5328ed81e22dSJed Brown    Collective on TS
5329ed81e22dSJed Brown 
5330ed81e22dSJed Brown    Input Parameters:
5331ed81e22dSJed Brown +  ts - the TS context
5332ed81e22dSJed Brown .  step - current time-step
5333ed81e22dSJed Brown .  ptime - current time
53340910c330SBarry Smith .  u - current state
5335ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5336ed81e22dSJed Brown 
5337ed81e22dSJed Brown    Level: intermediate
5338ed81e22dSJed Brown 
5339ed81e22dSJed Brown    Notes:
5340ed81e22dSJed 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.
5341ed81e22dSJed Brown    These are named according to the file name template.
5342ed81e22dSJed Brown 
5343ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5344ed81e22dSJed Brown 
5345ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5346ed81e22dSJed Brown 
5347ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5348ed81e22dSJed Brown @*/
53490910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5350ed81e22dSJed Brown {
5351ed81e22dSJed Brown   PetscErrorCode ierr;
5352ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5353ed81e22dSJed Brown   PetscViewer    viewer;
5354ed81e22dSJed Brown 
5355ed81e22dSJed Brown   PetscFunctionBegin;
535663e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
53578caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5358ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
53590910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5360ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5361ed81e22dSJed Brown   PetscFunctionReturn(0);
5362ed81e22dSJed Brown }
5363ed81e22dSJed Brown 
5364ed81e22dSJed Brown /*@C
5365ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5366ed81e22dSJed Brown 
5367ed81e22dSJed Brown    Collective on TS
5368ed81e22dSJed Brown 
5369ed81e22dSJed Brown    Input Parameters:
5370ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5371ed81e22dSJed Brown 
5372ed81e22dSJed Brown    Level: intermediate
5373ed81e22dSJed Brown 
5374ed81e22dSJed Brown    Note:
5375ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5376ed81e22dSJed Brown 
5377ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5378ed81e22dSJed Brown 
5379ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5380ed81e22dSJed Brown @*/
5381ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5382ed81e22dSJed Brown {
5383ed81e22dSJed Brown   PetscErrorCode ierr;
5384ed81e22dSJed Brown 
5385ed81e22dSJed Brown   PetscFunctionBegin;
5386ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5387fb1732b5SBarry Smith   PetscFunctionReturn(0);
5388fb1732b5SBarry Smith }
5389fb1732b5SBarry Smith 
539084df9cb4SJed Brown /*@
5391552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
539284df9cb4SJed Brown 
5393ed81e22dSJed Brown    Collective on TS if controller has not been created yet
539484df9cb4SJed Brown 
539584df9cb4SJed Brown    Input Arguments:
5396ed81e22dSJed Brown .  ts - time stepping context
539784df9cb4SJed Brown 
539884df9cb4SJed Brown    Output Arguments:
5399ed81e22dSJed Brown .  adapt - adaptive controller
540084df9cb4SJed Brown 
540184df9cb4SJed Brown    Level: intermediate
540284df9cb4SJed Brown 
5403ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
540484df9cb4SJed Brown @*/
5405552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
540684df9cb4SJed Brown {
540784df9cb4SJed Brown   PetscErrorCode ierr;
540884df9cb4SJed Brown 
540984df9cb4SJed Brown   PetscFunctionBegin;
541084df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5411bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
541284df9cb4SJed Brown   if (!ts->adapt) {
5413ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
54143bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
54151c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
541684df9cb4SJed Brown   }
5417bec58848SLisandro Dalcin   *adapt = ts->adapt;
541884df9cb4SJed Brown   PetscFunctionReturn(0);
541984df9cb4SJed Brown }
5420d6ebe24aSShri Abhyankar 
54211c3436cfSJed Brown /*@
54221c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
54231c3436cfSJed Brown 
54241c3436cfSJed Brown    Logically Collective
54251c3436cfSJed Brown 
54261c3436cfSJed Brown    Input Arguments:
54271c3436cfSJed Brown +  ts - time integration context
54281c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
54290298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
54301c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
54310298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
54321c3436cfSJed Brown 
5433a3cdaa26SBarry Smith    Options Database keys:
5434a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5435a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5436a3cdaa26SBarry Smith 
54373ff766beSShri Abhyankar    Notes:
54383ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
54393ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
54403ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
54413ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
54423ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
54433ff766beSShri Abhyankar    differential variables.
54443ff766beSShri Abhyankar 
54451c3436cfSJed Brown    Level: beginner
54461c3436cfSJed Brown 
5447c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
54481c3436cfSJed Brown @*/
54491c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
54501c3436cfSJed Brown {
54511c3436cfSJed Brown   PetscErrorCode ierr;
54521c3436cfSJed Brown 
54531c3436cfSJed Brown   PetscFunctionBegin;
5454c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
54551c3436cfSJed Brown   if (vatol) {
54561c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
54571c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
54581c3436cfSJed Brown     ts->vatol = vatol;
54591c3436cfSJed Brown   }
5460c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
54611c3436cfSJed Brown   if (vrtol) {
54621c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
54631c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
54641c3436cfSJed Brown     ts->vrtol = vrtol;
54651c3436cfSJed Brown   }
54661c3436cfSJed Brown   PetscFunctionReturn(0);
54671c3436cfSJed Brown }
54681c3436cfSJed Brown 
5469c5033834SJed Brown /*@
5470c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5471c5033834SJed Brown 
5472c5033834SJed Brown    Logically Collective
5473c5033834SJed Brown 
5474c5033834SJed Brown    Input Arguments:
5475c5033834SJed Brown .  ts - time integration context
5476c5033834SJed Brown 
5477c5033834SJed Brown    Output Arguments:
54780298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
54790298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
54800298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
54810298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5482c5033834SJed Brown 
5483c5033834SJed Brown    Level: beginner
5484c5033834SJed Brown 
5485c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5486c5033834SJed Brown @*/
5487c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5488c5033834SJed Brown {
5489c5033834SJed Brown   PetscFunctionBegin;
5490c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5491c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5492c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5493c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5494c5033834SJed Brown   PetscFunctionReturn(0);
5495c5033834SJed Brown }
5496c5033834SJed Brown 
54979c6b16b5SShri Abhyankar /*@
5498a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
54999c6b16b5SShri Abhyankar 
55009c6b16b5SShri Abhyankar    Collective on TS
55019c6b16b5SShri Abhyankar 
55029c6b16b5SShri Abhyankar    Input Arguments:
55039c6b16b5SShri Abhyankar +  ts - time stepping context
5504a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5505a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
55069c6b16b5SShri Abhyankar 
55079c6b16b5SShri Abhyankar    Output Arguments:
55087453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
55097453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
55107453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
55119c6b16b5SShri Abhyankar 
55129c6b16b5SShri Abhyankar    Level: developer
55139c6b16b5SShri Abhyankar 
5514deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
55159c6b16b5SShri Abhyankar @*/
55167453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
55179c6b16b5SShri Abhyankar {
55189c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
55199c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
55207453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
55217453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
55229c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
55237453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
55247453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
55257453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
55269c6b16b5SShri Abhyankar 
55279c6b16b5SShri Abhyankar   PetscFunctionBegin;
55289c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5529a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5530a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5531a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5532a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5533a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5534a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
55358a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
55368a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5537a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
55389c6b16b5SShri Abhyankar 
55399c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
55409c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
55419c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
55429c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
55439c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
55447453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
55457453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
55467453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
55479c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
55489c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
55499c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55509c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55519c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
55527453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55537453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
55547453f775SEmil Constantinescu       if(tola>0.){
55557453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
55567453f775SEmil Constantinescu         na_loc++;
55577453f775SEmil Constantinescu       }
55587453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55597453f775SEmil Constantinescu       if(tolr>0.){
55607453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
55617453f775SEmil Constantinescu         nr_loc++;
55627453f775SEmil Constantinescu       }
55637453f775SEmil Constantinescu       tol=tola+tolr;
55647453f775SEmil Constantinescu       if(tol>0.){
55657453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
55667453f775SEmil Constantinescu         n_loc++;
55677453f775SEmil Constantinescu       }
55689c6b16b5SShri Abhyankar     }
55699c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55709c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55719c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
55729c6b16b5SShri Abhyankar     const PetscScalar *atol;
55739c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55749c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
55757453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55767453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
55777453f775SEmil Constantinescu       if(tola>0.){
55787453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
55797453f775SEmil Constantinescu         na_loc++;
55807453f775SEmil Constantinescu       }
55817453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55827453f775SEmil Constantinescu       if(tolr>0.){
55837453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
55847453f775SEmil Constantinescu         nr_loc++;
55857453f775SEmil Constantinescu       }
55867453f775SEmil Constantinescu       tol=tola+tolr;
55877453f775SEmil Constantinescu       if(tol>0.){
55887453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
55897453f775SEmil Constantinescu         n_loc++;
55907453f775SEmil Constantinescu       }
55919c6b16b5SShri Abhyankar     }
55929c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55939c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
55949c6b16b5SShri Abhyankar     const PetscScalar *rtol;
55959c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55969c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
55977453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55987453f775SEmil Constantinescu       tola = ts->atol;
55997453f775SEmil Constantinescu       if(tola>0.){
56007453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
56017453f775SEmil Constantinescu         na_loc++;
56027453f775SEmil Constantinescu       }
56037453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56047453f775SEmil Constantinescu       if(tolr>0.){
56057453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
56067453f775SEmil Constantinescu         nr_loc++;
56077453f775SEmil Constantinescu       }
56087453f775SEmil Constantinescu       tol=tola+tolr;
56097453f775SEmil Constantinescu       if(tol>0.){
56107453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
56117453f775SEmil Constantinescu         n_loc++;
56127453f775SEmil Constantinescu       }
56139c6b16b5SShri Abhyankar     }
56149c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56159c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
56169c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
56177453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
56187453f775SEmil Constantinescu      tola = ts->atol;
56197453f775SEmil Constantinescu       if(tola>0.){
56207453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
56217453f775SEmil Constantinescu         na_loc++;
56227453f775SEmil Constantinescu       }
56237453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56247453f775SEmil Constantinescu       if(tolr>0.){
56257453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
56267453f775SEmil Constantinescu         nr_loc++;
56277453f775SEmil Constantinescu       }
56287453f775SEmil Constantinescu       tol=tola+tolr;
56297453f775SEmil Constantinescu       if(tol>0.){
56307453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
56317453f775SEmil Constantinescu         n_loc++;
56327453f775SEmil Constantinescu       }
56339c6b16b5SShri Abhyankar     }
56349c6b16b5SShri Abhyankar   }
56359c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
56369c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
56379c6b16b5SShri Abhyankar 
56387453f775SEmil Constantinescu   err_loc[0] = sum;
56397453f775SEmil Constantinescu   err_loc[1] = suma;
56407453f775SEmil Constantinescu   err_loc[2] = sumr;
56417453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
56427453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
56437453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
56447453f775SEmil Constantinescu 
5645a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
56467453f775SEmil Constantinescu 
56477453f775SEmil Constantinescu   gsum   = err_glb[0];
56487453f775SEmil Constantinescu   gsuma  = err_glb[1];
56497453f775SEmil Constantinescu   gsumr  = err_glb[2];
56507453f775SEmil Constantinescu   n_glb  = err_glb[3];
56517453f775SEmil Constantinescu   na_glb = err_glb[4];
56527453f775SEmil Constantinescu   nr_glb = err_glb[5];
56537453f775SEmil Constantinescu 
5654b1316ef9SEmil Constantinescu   *norm  = 0.;
5655b1316ef9SEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
5656b1316ef9SEmil Constantinescu   *norma = 0.;
5657b1316ef9SEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5658b1316ef9SEmil Constantinescu   *normr = 0.;
5659b1316ef9SEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
56609c6b16b5SShri Abhyankar 
56619c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
56627453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
56637453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
56649c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
56659c6b16b5SShri Abhyankar }
56669c6b16b5SShri Abhyankar 
56679c6b16b5SShri Abhyankar /*@
5668a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
56699c6b16b5SShri Abhyankar 
56709c6b16b5SShri Abhyankar    Collective on TS
56719c6b16b5SShri Abhyankar 
56729c6b16b5SShri Abhyankar    Input Arguments:
56739c6b16b5SShri Abhyankar +  ts - time stepping context
5674a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5675a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
56769c6b16b5SShri Abhyankar 
56779c6b16b5SShri Abhyankar    Output Arguments:
56787453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
56797453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
56807453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
56819c6b16b5SShri Abhyankar 
56829c6b16b5SShri Abhyankar    Level: developer
56839c6b16b5SShri Abhyankar 
5684deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
56859c6b16b5SShri Abhyankar @*/
56867453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
56879c6b16b5SShri Abhyankar {
56889c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
56897453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
56909c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
56917453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
56927453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
56937453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
56949c6b16b5SShri Abhyankar 
56959c6b16b5SShri Abhyankar   PetscFunctionBegin;
56969c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5697a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5698a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5699a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5700a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5701a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5702a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
57038a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
57048a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5705a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
57069c6b16b5SShri Abhyankar 
57079c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
57089c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
57099c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
57109c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
57119c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
57127453f775SEmil Constantinescu 
57137453f775SEmil Constantinescu   max=0.;
57147453f775SEmil Constantinescu   maxa=0.;
57157453f775SEmil Constantinescu   maxr=0.;
57167453f775SEmil Constantinescu 
57177453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
57189c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
57199c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57209c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57217453f775SEmil Constantinescu 
57227453f775SEmil Constantinescu     for (i=0; i<n; i++) {
57237453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57247453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
57257453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57267453f775SEmil Constantinescu       tol  = tola+tolr;
57277453f775SEmil Constantinescu       if(tola>0.){
57287453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
57297453f775SEmil Constantinescu       }
57307453f775SEmil Constantinescu       if(tolr>0.){
57317453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
57327453f775SEmil Constantinescu       }
57337453f775SEmil Constantinescu       if(tol>0.){
57347453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
57357453f775SEmil Constantinescu       }
57369c6b16b5SShri Abhyankar     }
57379c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57389c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57399c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
57409c6b16b5SShri Abhyankar     const PetscScalar *atol;
57419c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57427453f775SEmil Constantinescu     for (i=0; i<n; i++) {
57437453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57447453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
57457453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57467453f775SEmil Constantinescu       tol  = tola+tolr;
57477453f775SEmil Constantinescu       if(tola>0.){
57487453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
57497453f775SEmil Constantinescu       }
57507453f775SEmil Constantinescu       if(tolr>0.){
57517453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
57527453f775SEmil Constantinescu       }
57537453f775SEmil Constantinescu       if(tol>0.){
57547453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
57557453f775SEmil Constantinescu       }
57569c6b16b5SShri Abhyankar     }
57579c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57589c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
57599c6b16b5SShri Abhyankar     const PetscScalar *rtol;
57609c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57617453f775SEmil Constantinescu 
57627453f775SEmil Constantinescu     for (i=0; i<n; i++) {
57637453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57647453f775SEmil Constantinescu       tola = ts->atol;
57657453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57667453f775SEmil Constantinescu       tol  = tola+tolr;
57677453f775SEmil Constantinescu       if(tola>0.){
57687453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
57697453f775SEmil Constantinescu       }
57707453f775SEmil Constantinescu       if(tolr>0.){
57717453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
57727453f775SEmil Constantinescu       }
57737453f775SEmil Constantinescu       if(tol>0.){
57747453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
57757453f775SEmil Constantinescu       }
57769c6b16b5SShri Abhyankar     }
57779c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57789c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
57797453f775SEmil Constantinescu 
57807453f775SEmil Constantinescu     for (i=0; i<n; i++) {
57817453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57827453f775SEmil Constantinescu       tola = ts->atol;
57837453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57847453f775SEmil Constantinescu       tol  = tola+tolr;
57857453f775SEmil Constantinescu       if(tola>0.){
57867453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
57877453f775SEmil Constantinescu       }
57887453f775SEmil Constantinescu       if(tolr>0.){
57897453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
57907453f775SEmil Constantinescu       }
57917453f775SEmil Constantinescu       if(tol>0.){
57927453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
57937453f775SEmil Constantinescu       }
57949c6b16b5SShri Abhyankar     }
57959c6b16b5SShri Abhyankar   }
57969c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
57979c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
57987453f775SEmil Constantinescu   err_loc[0] = max;
57997453f775SEmil Constantinescu   err_loc[1] = maxa;
58007453f775SEmil Constantinescu   err_loc[2] = maxr;
5801a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
58027453f775SEmil Constantinescu   gmax   = err_glb[0];
58037453f775SEmil Constantinescu   gmaxa  = err_glb[1];
58047453f775SEmil Constantinescu   gmaxr  = err_glb[2];
58059c6b16b5SShri Abhyankar 
58069c6b16b5SShri Abhyankar   *norm = gmax;
58077453f775SEmil Constantinescu   *norma = gmaxa;
58087453f775SEmil Constantinescu   *normr = gmaxr;
58099c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
58107453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
58117453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
58129c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
58139c6b16b5SShri Abhyankar }
58149c6b16b5SShri Abhyankar 
58151c3436cfSJed Brown /*@
58168a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
58171c3436cfSJed Brown 
58181c3436cfSJed Brown    Collective on TS
58191c3436cfSJed Brown 
58201c3436cfSJed Brown    Input Arguments:
58211c3436cfSJed Brown +  ts - time stepping context
5822a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5823a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
5824a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
58257619abb3SShri 
58261c3436cfSJed Brown    Output Arguments:
58278a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
58288a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
58298a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
5830a4868fbcSLisandro Dalcin 
5831a4868fbcSLisandro Dalcin    Options Database Keys:
5832a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
5833a4868fbcSLisandro Dalcin 
58341c3436cfSJed Brown    Level: developer
58351c3436cfSJed Brown 
58368a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
58371c3436cfSJed Brown @*/
58387453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
58391c3436cfSJed Brown {
58408beabaa1SBarry Smith   PetscErrorCode ierr;
58411c3436cfSJed Brown 
58421c3436cfSJed Brown   PetscFunctionBegin;
5843a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
58447453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
5845a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
58467453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
5847a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
58481c3436cfSJed Brown   PetscFunctionReturn(0);
58491c3436cfSJed Brown }
58501c3436cfSJed Brown 
58518a175baeSEmil Constantinescu 
58528a175baeSEmil Constantinescu /*@
58538a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
58548a175baeSEmil Constantinescu 
58558a175baeSEmil Constantinescu    Collective on TS
58568a175baeSEmil Constantinescu 
58578a175baeSEmil Constantinescu    Input Arguments:
58588a175baeSEmil Constantinescu +  ts - time stepping context
58598a175baeSEmil Constantinescu .  E - error vector
58608a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
58618a175baeSEmil Constantinescu -  Y - state vector, previous time step
58628a175baeSEmil Constantinescu 
58638a175baeSEmil Constantinescu    Output Arguments:
58648a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
58658a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
58668a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
58678a175baeSEmil Constantinescu 
58688a175baeSEmil Constantinescu    Level: developer
58698a175baeSEmil Constantinescu 
58708a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
58718a175baeSEmil Constantinescu @*/
58728a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
58738a175baeSEmil Constantinescu {
58748a175baeSEmil Constantinescu   PetscErrorCode    ierr;
58758a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
58768a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
58778a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
58788a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
58798a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
58808a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
58818a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
58828a175baeSEmil Constantinescu 
58838a175baeSEmil Constantinescu   PetscFunctionBegin;
58848a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
58858a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
58868a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
58878a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
58888a175baeSEmil Constantinescu   PetscValidType(E,2);
58898a175baeSEmil Constantinescu   PetscValidType(U,3);
58908a175baeSEmil Constantinescu   PetscValidType(Y,4);
58918a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
58928a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
58938a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
58948a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
58958a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
58968a175baeSEmil Constantinescu 
58978a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
58988a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
58998a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
59008a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
59018a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
59028a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
59038a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
59048a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
59058a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
59068a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
59078a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
59088a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59098a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59108a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
59118a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59128a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
59138a175baeSEmil Constantinescu       if(tola>0.){
59148a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
59158a175baeSEmil Constantinescu         na_loc++;
59168a175baeSEmil Constantinescu       }
59178a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59188a175baeSEmil Constantinescu       if(tolr>0.){
59198a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
59208a175baeSEmil Constantinescu         nr_loc++;
59218a175baeSEmil Constantinescu       }
59228a175baeSEmil Constantinescu       tol=tola+tolr;
59238a175baeSEmil Constantinescu       if(tol>0.){
59248a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
59258a175baeSEmil Constantinescu         n_loc++;
59268a175baeSEmil Constantinescu       }
59278a175baeSEmil Constantinescu     }
59288a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59298a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59308a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
59318a175baeSEmil Constantinescu     const PetscScalar *atol;
59328a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59338a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
59348a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59358a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
59368a175baeSEmil Constantinescu       if(tola>0.){
59378a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
59388a175baeSEmil Constantinescu         na_loc++;
59398a175baeSEmil Constantinescu       }
59408a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59418a175baeSEmil Constantinescu       if(tolr>0.){
59428a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
59438a175baeSEmil Constantinescu         nr_loc++;
59448a175baeSEmil Constantinescu       }
59458a175baeSEmil Constantinescu       tol=tola+tolr;
59468a175baeSEmil Constantinescu       if(tol>0.){
59478a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
59488a175baeSEmil Constantinescu         n_loc++;
59498a175baeSEmil Constantinescu       }
59508a175baeSEmil Constantinescu     }
59518a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59528a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
59538a175baeSEmil Constantinescu     const PetscScalar *rtol;
59548a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59558a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
59568a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59578a175baeSEmil Constantinescu       tola = ts->atol;
59588a175baeSEmil Constantinescu       if(tola>0.){
59598a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
59608a175baeSEmil Constantinescu         na_loc++;
59618a175baeSEmil Constantinescu       }
59628a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59638a175baeSEmil Constantinescu       if(tolr>0.){
59648a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
59658a175baeSEmil Constantinescu         nr_loc++;
59668a175baeSEmil Constantinescu       }
59678a175baeSEmil Constantinescu       tol=tola+tolr;
59688a175baeSEmil Constantinescu       if(tol>0.){
59698a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
59708a175baeSEmil Constantinescu         n_loc++;
59718a175baeSEmil Constantinescu       }
59728a175baeSEmil Constantinescu     }
59738a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59748a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
59758a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
59768a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59778a175baeSEmil Constantinescu      tola = ts->atol;
59788a175baeSEmil Constantinescu       if(tola>0.){
59798a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
59808a175baeSEmil Constantinescu         na_loc++;
59818a175baeSEmil Constantinescu       }
59828a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59838a175baeSEmil Constantinescu       if(tolr>0.){
59848a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
59858a175baeSEmil Constantinescu         nr_loc++;
59868a175baeSEmil Constantinescu       }
59878a175baeSEmil Constantinescu       tol=tola+tolr;
59888a175baeSEmil Constantinescu       if(tol>0.){
59898a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
59908a175baeSEmil Constantinescu         n_loc++;
59918a175baeSEmil Constantinescu       }
59928a175baeSEmil Constantinescu     }
59938a175baeSEmil Constantinescu   }
59948a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
59958a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
59968a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
59978a175baeSEmil Constantinescu 
59988a175baeSEmil Constantinescu   err_loc[0] = sum;
59998a175baeSEmil Constantinescu   err_loc[1] = suma;
60008a175baeSEmil Constantinescu   err_loc[2] = sumr;
60018a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
60028a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
60038a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
60048a175baeSEmil Constantinescu 
6005a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
60068a175baeSEmil Constantinescu 
60078a175baeSEmil Constantinescu   gsum   = err_glb[0];
60088a175baeSEmil Constantinescu   gsuma  = err_glb[1];
60098a175baeSEmil Constantinescu   gsumr  = err_glb[2];
60108a175baeSEmil Constantinescu   n_glb  = err_glb[3];
60118a175baeSEmil Constantinescu   na_glb = err_glb[4];
60128a175baeSEmil Constantinescu   nr_glb = err_glb[5];
60138a175baeSEmil Constantinescu 
60148a175baeSEmil Constantinescu   *norm  = 0.;
60158a175baeSEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
60168a175baeSEmil Constantinescu   *norma = 0.;
60178a175baeSEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
60188a175baeSEmil Constantinescu   *normr = 0.;
60198a175baeSEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
60208a175baeSEmil Constantinescu 
60218a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
60228a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
60238a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
60248a175baeSEmil Constantinescu   PetscFunctionReturn(0);
60258a175baeSEmil Constantinescu }
60268a175baeSEmil Constantinescu 
60278a175baeSEmil Constantinescu /*@
60288a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
60298a175baeSEmil Constantinescu    Collective on TS
60308a175baeSEmil Constantinescu 
60318a175baeSEmil Constantinescu    Input Arguments:
60328a175baeSEmil Constantinescu +  ts - time stepping context
60338a175baeSEmil Constantinescu .  E - error vector
60348a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
60358a175baeSEmil Constantinescu -  Y - state vector, previous time step
60368a175baeSEmil Constantinescu 
60378a175baeSEmil Constantinescu    Output Arguments:
60388a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
60398a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
60408a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
60418a175baeSEmil Constantinescu 
60428a175baeSEmil Constantinescu    Level: developer
60438a175baeSEmil Constantinescu 
60448a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
60458a175baeSEmil Constantinescu @*/
60468a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
60478a175baeSEmil Constantinescu {
60488a175baeSEmil Constantinescu   PetscErrorCode    ierr;
60498a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
60508a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
60518a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
60528a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
60538a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
60548a175baeSEmil Constantinescu 
60558a175baeSEmil Constantinescu   PetscFunctionBegin;
60568a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
60578a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
60588a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
60598a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
60608a175baeSEmil Constantinescu   PetscValidType(E,2);
60618a175baeSEmil Constantinescu   PetscValidType(U,3);
60628a175baeSEmil Constantinescu   PetscValidType(Y,4);
60638a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
60648a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
60658a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
60668a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
60678a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
60688a175baeSEmil Constantinescu 
60698a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
60708a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
60718a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
60728a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
60738a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
60748a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
60758a175baeSEmil Constantinescu 
60768a175baeSEmil Constantinescu   max=0.;
60778a175baeSEmil Constantinescu   maxa=0.;
60788a175baeSEmil Constantinescu   maxr=0.;
60798a175baeSEmil Constantinescu 
60808a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
60818a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
60828a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60838a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60848a175baeSEmil Constantinescu 
60858a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
60868a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
60878a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
60888a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60898a175baeSEmil Constantinescu       tol  = tola+tolr;
60908a175baeSEmil Constantinescu       if(tola>0.){
60918a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
60928a175baeSEmil Constantinescu       }
60938a175baeSEmil Constantinescu       if(tolr>0.){
60948a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
60958a175baeSEmil Constantinescu       }
60968a175baeSEmil Constantinescu       if(tol>0.){
60978a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
60988a175baeSEmil Constantinescu       }
60998a175baeSEmil Constantinescu     }
61008a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61018a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61028a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
61038a175baeSEmil Constantinescu     const PetscScalar *atol;
61048a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61058a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
61068a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61078a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
61088a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61098a175baeSEmil Constantinescu       tol  = tola+tolr;
61108a175baeSEmil Constantinescu       if(tola>0.){
61118a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
61128a175baeSEmil Constantinescu       }
61138a175baeSEmil Constantinescu       if(tolr>0.){
61148a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
61158a175baeSEmil Constantinescu       }
61168a175baeSEmil Constantinescu       if(tol>0.){
61178a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
61188a175baeSEmil Constantinescu       }
61198a175baeSEmil Constantinescu     }
61208a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61218a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
61228a175baeSEmil Constantinescu     const PetscScalar *rtol;
61238a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61248a175baeSEmil Constantinescu 
61258a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
61268a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61278a175baeSEmil Constantinescu       tola = ts->atol;
61288a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61298a175baeSEmil Constantinescu       tol  = tola+tolr;
61308a175baeSEmil Constantinescu       if(tola>0.){
61318a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
61328a175baeSEmil Constantinescu       }
61338a175baeSEmil Constantinescu       if(tolr>0.){
61348a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
61358a175baeSEmil Constantinescu       }
61368a175baeSEmil Constantinescu       if(tol>0.){
61378a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
61388a175baeSEmil Constantinescu       }
61398a175baeSEmil Constantinescu     }
61408a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61418a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
61428a175baeSEmil Constantinescu 
61438a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
61448a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61458a175baeSEmil Constantinescu       tola = ts->atol;
61468a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61478a175baeSEmil Constantinescu       tol  = tola+tolr;
61488a175baeSEmil Constantinescu       if(tola>0.){
61498a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
61508a175baeSEmil Constantinescu       }
61518a175baeSEmil Constantinescu       if(tolr>0.){
61528a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
61538a175baeSEmil Constantinescu       }
61548a175baeSEmil Constantinescu       if(tol>0.){
61558a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
61568a175baeSEmil Constantinescu       }
61578a175baeSEmil Constantinescu     }
61588a175baeSEmil Constantinescu   }
61598a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
61608a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
61618a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
61628a175baeSEmil Constantinescu   err_loc[0] = max;
61638a175baeSEmil Constantinescu   err_loc[1] = maxa;
61648a175baeSEmil Constantinescu   err_loc[2] = maxr;
6165a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
61668a175baeSEmil Constantinescu   gmax   = err_glb[0];
61678a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
61688a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
61698a175baeSEmil Constantinescu 
61708a175baeSEmil Constantinescu   *norm = gmax;
61718a175baeSEmil Constantinescu   *norma = gmaxa;
61728a175baeSEmil Constantinescu   *normr = gmaxr;
61738a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
61748a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
61758a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
61768a175baeSEmil Constantinescu   PetscFunctionReturn(0);
61778a175baeSEmil Constantinescu }
61788a175baeSEmil Constantinescu 
61798a175baeSEmil Constantinescu /*@
61808a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
61818a175baeSEmil Constantinescu 
61828a175baeSEmil Constantinescu    Collective on TS
61838a175baeSEmil Constantinescu 
61848a175baeSEmil Constantinescu    Input Arguments:
61858a175baeSEmil Constantinescu +  ts - time stepping context
61868a175baeSEmil Constantinescu .  E - error vector
61878a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
61888a175baeSEmil Constantinescu .  Y - state vector, previous time step
61898a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
61908a175baeSEmil Constantinescu 
61918a175baeSEmil Constantinescu    Output Arguments:
61928a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
61938a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
61948a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
61958a175baeSEmil Constantinescu 
61968a175baeSEmil Constantinescu    Options Database Keys:
61978a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
61988a175baeSEmil Constantinescu 
61998a175baeSEmil Constantinescu    Level: developer
62008a175baeSEmil Constantinescu 
62018a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
62028a175baeSEmil Constantinescu @*/
62038a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
62048a175baeSEmil Constantinescu {
62058a175baeSEmil Constantinescu   PetscErrorCode ierr;
62068a175baeSEmil Constantinescu 
62078a175baeSEmil Constantinescu   PetscFunctionBegin;
62088a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
62098a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
62108a175baeSEmil Constantinescu   } else if(wnormtype == NORM_INFINITY) {
62118a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
62128a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
62138a175baeSEmil Constantinescu   PetscFunctionReturn(0);
62148a175baeSEmil Constantinescu }
62158a175baeSEmil Constantinescu 
62168a175baeSEmil Constantinescu 
62178d59e960SJed Brown /*@
62188d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
62198d59e960SJed Brown 
62208d59e960SJed Brown    Logically Collective on TS
62218d59e960SJed Brown 
62228d59e960SJed Brown    Input Arguments:
62238d59e960SJed Brown +  ts - time stepping context
62248d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
62258d59e960SJed Brown 
62268d59e960SJed Brown    Note:
62278d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
62288d59e960SJed Brown 
62298d59e960SJed Brown    Level: intermediate
62308d59e960SJed Brown 
62318d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
62328d59e960SJed Brown @*/
62338d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
62348d59e960SJed Brown {
62358d59e960SJed Brown   PetscFunctionBegin;
62368d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
62378d59e960SJed Brown   ts->cfltime_local = cfltime;
62388d59e960SJed Brown   ts->cfltime       = -1.;
62398d59e960SJed Brown   PetscFunctionReturn(0);
62408d59e960SJed Brown }
62418d59e960SJed Brown 
62428d59e960SJed Brown /*@
62438d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
62448d59e960SJed Brown 
62458d59e960SJed Brown    Collective on TS
62468d59e960SJed Brown 
62478d59e960SJed Brown    Input Arguments:
62488d59e960SJed Brown .  ts - time stepping context
62498d59e960SJed Brown 
62508d59e960SJed Brown    Output Arguments:
62518d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
62528d59e960SJed Brown 
62538d59e960SJed Brown    Level: advanced
62548d59e960SJed Brown 
62558d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
62568d59e960SJed Brown @*/
62578d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
62588d59e960SJed Brown {
62598d59e960SJed Brown   PetscErrorCode ierr;
62608d59e960SJed Brown 
62618d59e960SJed Brown   PetscFunctionBegin;
62628d59e960SJed Brown   if (ts->cfltime < 0) {
6263b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
62648d59e960SJed Brown   }
62658d59e960SJed Brown   *cfltime = ts->cfltime;
62668d59e960SJed Brown   PetscFunctionReturn(0);
62678d59e960SJed Brown }
62688d59e960SJed Brown 
6269d6ebe24aSShri Abhyankar /*@
6270d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6271d6ebe24aSShri Abhyankar 
6272d6ebe24aSShri Abhyankar    Input Parameters:
6273d6ebe24aSShri Abhyankar .  ts   - the TS context.
6274d6ebe24aSShri Abhyankar .  xl   - lower bound.
6275d6ebe24aSShri Abhyankar .  xu   - upper bound.
6276d6ebe24aSShri Abhyankar 
6277d6ebe24aSShri Abhyankar    Notes:
6278d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6279e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6280d6ebe24aSShri Abhyankar 
62812bd2b0e6SSatish Balay    Level: advanced
62822bd2b0e6SSatish Balay 
6283d6ebe24aSShri Abhyankar @*/
6284d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6285d6ebe24aSShri Abhyankar {
6286d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6287d6ebe24aSShri Abhyankar   SNES           snes;
6288d6ebe24aSShri Abhyankar 
6289d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6290d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6291d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6292d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6293d6ebe24aSShri Abhyankar }
6294d6ebe24aSShri Abhyankar 
6295325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
6296c6db04a5SJed Brown #include <mex.h>
6297325fc9f4SBarry Smith 
6298325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
6299325fc9f4SBarry Smith 
6300325fc9f4SBarry Smith /*
6301325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
6302325fc9f4SBarry Smith                          TSSetFunctionMatlab().
6303325fc9f4SBarry Smith 
6304325fc9f4SBarry Smith    Collective on TS
6305325fc9f4SBarry Smith 
6306325fc9f4SBarry Smith    Input Parameters:
6307325fc9f4SBarry Smith +  snes - the TS context
63080910c330SBarry Smith -  u - input vector
6309325fc9f4SBarry Smith 
6310325fc9f4SBarry Smith    Output Parameter:
6311325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
6312325fc9f4SBarry Smith 
6313325fc9f4SBarry Smith    Notes:
6314325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
6315325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
6316325fc9f4SBarry Smith    themselves.
6317325fc9f4SBarry Smith 
6318325fc9f4SBarry Smith    Level: developer
6319325fc9f4SBarry Smith 
6320325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6321325fc9f4SBarry Smith 
6322325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6323325fc9f4SBarry Smith */
63240910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
6325325fc9f4SBarry Smith {
6326325fc9f4SBarry Smith   PetscErrorCode  ierr;
6327325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6328325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
6329325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
6330325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
6331325fc9f4SBarry Smith 
6332325fc9f4SBarry Smith   PetscFunctionBegin;
6333325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
63340910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
63350910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
6336325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
63370910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
6338325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
6339325fc9f4SBarry Smith 
6340325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
63410910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
63420910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
63430910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
6344bbd56ea5SKarl Rupp 
6345325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6346325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
6347325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6348325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6349325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
6350325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
6351325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
6352325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
6353325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6354325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6355325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6356325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6357325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6358325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6359325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6360325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6361325fc9f4SBarry Smith   PetscFunctionReturn(0);
6362325fc9f4SBarry Smith }
6363325fc9f4SBarry Smith 
6364325fc9f4SBarry Smith /*
6365325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
6366325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
6367e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
6368325fc9f4SBarry Smith 
6369325fc9f4SBarry Smith    Logically Collective on TS
6370325fc9f4SBarry Smith 
6371325fc9f4SBarry Smith    Input Parameters:
6372325fc9f4SBarry Smith +  ts - the TS context
6373325fc9f4SBarry Smith -  func - function evaluation routine
6374325fc9f4SBarry Smith 
6375325fc9f4SBarry Smith    Calling sequence of func:
63760910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
6377325fc9f4SBarry Smith 
6378325fc9f4SBarry Smith    Level: beginner
6379325fc9f4SBarry Smith 
6380325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6381325fc9f4SBarry Smith 
6382325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6383325fc9f4SBarry Smith */
6384cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
6385325fc9f4SBarry Smith {
6386325fc9f4SBarry Smith   PetscErrorCode  ierr;
6387325fc9f4SBarry Smith   TSMatlabContext *sctx;
6388325fc9f4SBarry Smith 
6389325fc9f4SBarry Smith   PetscFunctionBegin;
6390325fc9f4SBarry Smith   /* currently sctx is memory bleed */
639195dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6392325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6393325fc9f4SBarry Smith   /*
6394325fc9f4SBarry Smith      This should work, but it doesn't
6395325fc9f4SBarry Smith   sctx->ctx = ctx;
6396325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6397325fc9f4SBarry Smith   */
6398325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6399bbd56ea5SKarl Rupp 
64000298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
6401325fc9f4SBarry Smith   PetscFunctionReturn(0);
6402325fc9f4SBarry Smith }
6403325fc9f4SBarry Smith 
6404325fc9f4SBarry Smith /*
6405325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
6406325fc9f4SBarry Smith                          TSSetJacobianMatlab().
6407325fc9f4SBarry Smith 
6408325fc9f4SBarry Smith    Collective on TS
6409325fc9f4SBarry Smith 
6410325fc9f4SBarry Smith    Input Parameters:
6411cdcf91faSSean Farley +  ts - the TS context
64120910c330SBarry Smith .  u - input vector
6413325fc9f4SBarry Smith .  A, B - the matrices
6414325fc9f4SBarry Smith -  ctx - user context
6415325fc9f4SBarry Smith 
6416325fc9f4SBarry Smith    Level: developer
6417325fc9f4SBarry Smith 
6418325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6419325fc9f4SBarry Smith 
6420325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6421325fc9f4SBarry Smith @*/
6422f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
6423325fc9f4SBarry Smith {
6424325fc9f4SBarry Smith   PetscErrorCode  ierr;
6425325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6426325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
6427325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
6428325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
6429325fc9f4SBarry Smith 
6430325fc9f4SBarry Smith   PetscFunctionBegin;
6431cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
64320910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
6433325fc9f4SBarry Smith 
64340910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
6435325fc9f4SBarry Smith 
6436cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
64370910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
64380910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
64390910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
64400910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
6441bbd56ea5SKarl Rupp 
6442325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6443325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
6444325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6445325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6446325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
6447325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
6448325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
6449325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
6450325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
6451325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
6452325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6453325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6454325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6455325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6456325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6457325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6458325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6459325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
6460325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
6461325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6462325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
6463325fc9f4SBarry Smith   PetscFunctionReturn(0);
6464325fc9f4SBarry Smith }
6465325fc9f4SBarry Smith 
6466325fc9f4SBarry Smith /*
6467325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
6468e3c5b3baSBarry 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
6469325fc9f4SBarry Smith 
6470325fc9f4SBarry Smith    Logically Collective on TS
6471325fc9f4SBarry Smith 
6472325fc9f4SBarry Smith    Input Parameters:
6473cdcf91faSSean Farley +  ts - the TS context
6474325fc9f4SBarry Smith .  A,B - Jacobian matrices
6475325fc9f4SBarry Smith .  func - function evaluation routine
6476325fc9f4SBarry Smith -  ctx - user context
6477325fc9f4SBarry Smith 
6478325fc9f4SBarry Smith    Calling sequence of func:
64790910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
6480325fc9f4SBarry Smith 
6481325fc9f4SBarry Smith    Level: developer
6482325fc9f4SBarry Smith 
6483325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6484325fc9f4SBarry Smith 
6485325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6486325fc9f4SBarry Smith */
6487cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
6488325fc9f4SBarry Smith {
6489325fc9f4SBarry Smith   PetscErrorCode  ierr;
6490325fc9f4SBarry Smith   TSMatlabContext *sctx;
6491325fc9f4SBarry Smith 
6492325fc9f4SBarry Smith   PetscFunctionBegin;
6493325fc9f4SBarry Smith   /* currently sctx is memory bleed */
649495dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6495325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6496325fc9f4SBarry Smith   /*
6497325fc9f4SBarry Smith      This should work, but it doesn't
6498325fc9f4SBarry Smith   sctx->ctx = ctx;
6499325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6500325fc9f4SBarry Smith   */
6501325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6502bbd56ea5SKarl Rupp 
6503cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
6504325fc9f4SBarry Smith   PetscFunctionReturn(0);
6505325fc9f4SBarry Smith }
6506325fc9f4SBarry Smith 
6507b5b1a830SBarry Smith /*
6508b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
6509b5b1a830SBarry Smith 
6510b5b1a830SBarry Smith    Collective on TS
6511b5b1a830SBarry Smith 
6512b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6513b5b1a830SBarry Smith @*/
65140910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
6515b5b1a830SBarry Smith {
6516b5b1a830SBarry Smith   PetscErrorCode  ierr;
6517b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6518a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
6519b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
6520b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
6521b5b1a830SBarry Smith 
6522b5b1a830SBarry Smith   PetscFunctionBegin;
6523cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
65240910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
6525b5b1a830SBarry Smith 
6526cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
65270910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
6528bbd56ea5SKarl Rupp 
6529b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6530b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
6531b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
6532b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
6533b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
6534b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
6535b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
6536b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6537b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
6538b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
6539b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
6540b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
6541b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
6542b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
6543b5b1a830SBarry Smith   PetscFunctionReturn(0);
6544b5b1a830SBarry Smith }
6545b5b1a830SBarry Smith 
6546b5b1a830SBarry Smith /*
6547b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
6548b5b1a830SBarry Smith 
6549b5b1a830SBarry Smith    Level: developer
6550b5b1a830SBarry Smith 
6551b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
6552b5b1a830SBarry Smith 
6553b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6554b5b1a830SBarry Smith */
6555cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
6556b5b1a830SBarry Smith {
6557b5b1a830SBarry Smith   PetscErrorCode  ierr;
6558b5b1a830SBarry Smith   TSMatlabContext *sctx;
6559b5b1a830SBarry Smith 
6560b5b1a830SBarry Smith   PetscFunctionBegin;
6561b5b1a830SBarry Smith   /* currently sctx is memory bleed */
656295dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6563b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6564b5b1a830SBarry Smith   /*
6565b5b1a830SBarry Smith      This should work, but it doesn't
6566b5b1a830SBarry Smith   sctx->ctx = ctx;
6567b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6568b5b1a830SBarry Smith   */
6569b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6570bbd56ea5SKarl Rupp 
65710298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
6572b5b1a830SBarry Smith   PetscFunctionReturn(0);
6573b5b1a830SBarry Smith }
6574325fc9f4SBarry Smith #endif
6575b3603a34SBarry Smith 
6576b3603a34SBarry Smith /*@C
65774f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6578b3603a34SBarry Smith        in a time based line graph
6579b3603a34SBarry Smith 
6580b3603a34SBarry Smith    Collective on TS
6581b3603a34SBarry Smith 
6582b3603a34SBarry Smith    Input Parameters:
6583b3603a34SBarry Smith +  ts - the TS context
6584b3603a34SBarry Smith .  step - current time-step
6585b3603a34SBarry Smith .  ptime - current time
65867db568b7SBarry Smith .  u - current solution
65877db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6588b3603a34SBarry Smith 
6589b3d3934dSBarry Smith    Options Database:
65909ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6591b3d3934dSBarry Smith 
6592b3603a34SBarry Smith    Level: intermediate
6593b3603a34SBarry Smith 
659495452b02SPatrick Sanan    Notes:
659595452b02SPatrick Sanan     Each process in a parallel run displays its component solutions in a separate window
6596b3603a34SBarry Smith 
6597b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
6598b3603a34SBarry Smith 
65997db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
66007db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
66017db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
66027db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6603b3603a34SBarry Smith @*/
66047db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6605b3603a34SBarry Smith {
6606b3603a34SBarry Smith   PetscErrorCode    ierr;
66077db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6608b3603a34SBarry Smith   const PetscScalar *yy;
660980666b62SBarry Smith   Vec               v;
6610b3603a34SBarry Smith 
6611b3603a34SBarry Smith   PetscFunctionBegin;
661263e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
661358ff32f7SBarry Smith   if (!step) {
6614a9f9c1f6SBarry Smith     PetscDrawAxis axis;
66156934998bSLisandro Dalcin     PetscInt      dim;
6616a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6617a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6618bab0a581SBarry Smith     if (!ctx->names) {
6619bab0a581SBarry Smith       PetscBool flg;
6620bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6621bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6622bab0a581SBarry Smith       if (flg) {
6623bab0a581SBarry Smith         PetscInt i,n;
6624bab0a581SBarry Smith         char     **names;
6625bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6626bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6627bab0a581SBarry Smith         for (i=0; i<n; i++) {
6628bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6629bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6630bab0a581SBarry Smith         }
6631bab0a581SBarry Smith         names[n] = NULL;
6632bab0a581SBarry Smith         ctx->names = names;
6633bab0a581SBarry Smith       }
6634bab0a581SBarry Smith     }
6635387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6636387f4636SBarry Smith       char      **displaynames;
6637387f4636SBarry Smith       PetscBool flg;
6638387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
663995dccacaSBarry Smith       ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6640387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
6641c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6642387f4636SBarry Smith       if (flg) {
6643a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6644387f4636SBarry Smith       }
6645387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6646387f4636SBarry Smith     }
6647387f4636SBarry Smith     if (ctx->displaynames) {
6648387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6649387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6650387f4636SBarry Smith     } else if (ctx->names) {
66510910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
66520b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6653387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6654b0bc92c6SBarry Smith     } else {
6655b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6656b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6657387f4636SBarry Smith     }
66580b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
665958ff32f7SBarry Smith   }
66606934998bSLisandro Dalcin 
66616934998bSLisandro Dalcin   if (!ctx->transform) v = u;
66626934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
666380666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
66646934998bSLisandro Dalcin   if (ctx->displaynames) {
66656934998bSLisandro Dalcin     PetscInt i;
66666934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
66676934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
66686934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
66696934998bSLisandro Dalcin   } else {
6670e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6671e3efe391SJed Brown     PetscInt  i,n;
66726934998bSLisandro Dalcin     PetscReal *yreal;
667380666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6674785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6675e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
66760b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6677e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6678e3efe391SJed Brown #else
66790b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6680e3efe391SJed Brown #endif
668180666b62SBarry Smith   }
66826934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
66836934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
66846934998bSLisandro Dalcin 
6685b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
66860b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
66876934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
66883923b477SBarry Smith   }
6689b3603a34SBarry Smith   PetscFunctionReturn(0);
6690b3603a34SBarry Smith }
6691b3603a34SBarry Smith 
6692b037adc7SBarry Smith /*@C
669331152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6694b037adc7SBarry Smith 
6695b037adc7SBarry Smith    Collective on TS
6696b037adc7SBarry Smith 
6697b037adc7SBarry Smith    Input Parameters:
6698b037adc7SBarry Smith +  ts - the TS context
6699b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6700b037adc7SBarry Smith 
6701b037adc7SBarry Smith    Level: intermediate
6702b037adc7SBarry Smith 
670395452b02SPatrick Sanan    Notes:
670495452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67057db568b7SBarry Smith 
6706b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
6707b037adc7SBarry Smith 
6708a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6709b037adc7SBarry Smith @*/
671031152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6711b037adc7SBarry Smith {
6712b037adc7SBarry Smith   PetscErrorCode    ierr;
6713b037adc7SBarry Smith   PetscInt          i;
6714b037adc7SBarry Smith 
6715b037adc7SBarry Smith   PetscFunctionBegin;
6716b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6717b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67185537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6719b3d3934dSBarry Smith       break;
6720b3d3934dSBarry Smith     }
6721b3d3934dSBarry Smith   }
6722b3d3934dSBarry Smith   PetscFunctionReturn(0);
6723b3d3934dSBarry Smith }
6724b3d3934dSBarry Smith 
6725e673d494SBarry Smith /*@C
6726e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6727e673d494SBarry Smith 
6728e673d494SBarry Smith    Collective on TS
6729e673d494SBarry Smith 
6730e673d494SBarry Smith    Input Parameters:
6731e673d494SBarry Smith +  ts - the TS context
6732e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6733e673d494SBarry Smith 
6734e673d494SBarry Smith    Level: intermediate
6735e673d494SBarry Smith 
6736e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6737e673d494SBarry Smith 
6738a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6739e673d494SBarry Smith @*/
6740e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6741e673d494SBarry Smith {
6742e673d494SBarry Smith   PetscErrorCode    ierr;
6743e673d494SBarry Smith 
6744e673d494SBarry Smith   PetscFunctionBegin;
6745e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6746e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6747e673d494SBarry Smith   PetscFunctionReturn(0);
6748e673d494SBarry Smith }
6749e673d494SBarry Smith 
6750b3d3934dSBarry Smith /*@C
6751b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6752b3d3934dSBarry Smith 
6753b3d3934dSBarry Smith    Collective on TS
6754b3d3934dSBarry Smith 
6755b3d3934dSBarry Smith    Input Parameter:
6756b3d3934dSBarry Smith .  ts - the TS context
6757b3d3934dSBarry Smith 
6758b3d3934dSBarry Smith    Output Parameter:
6759b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6760b3d3934dSBarry Smith 
6761b3d3934dSBarry Smith    Level: intermediate
6762b3d3934dSBarry Smith 
676395452b02SPatrick Sanan    Notes:
676495452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67657db568b7SBarry Smith 
6766b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6767b3d3934dSBarry Smith 
6768b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6769b3d3934dSBarry Smith @*/
6770b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6771b3d3934dSBarry Smith {
6772b3d3934dSBarry Smith   PetscInt       i;
6773b3d3934dSBarry Smith 
6774b3d3934dSBarry Smith   PetscFunctionBegin;
6775b3d3934dSBarry Smith   *names = NULL;
6776b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6777b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67785537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6779b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6780b3d3934dSBarry Smith       break;
6781387f4636SBarry Smith     }
6782387f4636SBarry Smith   }
6783387f4636SBarry Smith   PetscFunctionReturn(0);
6784387f4636SBarry Smith }
6785387f4636SBarry Smith 
6786a66092f1SBarry Smith /*@C
6787a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6788a66092f1SBarry Smith 
6789a66092f1SBarry Smith    Collective on TS
6790a66092f1SBarry Smith 
6791a66092f1SBarry Smith    Input Parameters:
6792a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6793a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
6794a66092f1SBarry Smith 
6795a66092f1SBarry Smith    Level: intermediate
6796a66092f1SBarry Smith 
6797a66092f1SBarry Smith .keywords: TS,  vector, monitor, view
6798a66092f1SBarry Smith 
6799a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6800a66092f1SBarry Smith @*/
6801a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6802a66092f1SBarry Smith {
6803a66092f1SBarry Smith   PetscInt          j = 0,k;
6804a66092f1SBarry Smith   PetscErrorCode    ierr;
6805a66092f1SBarry Smith 
6806a66092f1SBarry Smith   PetscFunctionBegin;
6807a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6808a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6809a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6810a66092f1SBarry Smith   while (displaynames[j]) j++;
6811a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6812a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6813a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6814a66092f1SBarry Smith   j = 0;
6815a66092f1SBarry Smith   while (displaynames[j]) {
6816a66092f1SBarry Smith     k = 0;
6817a66092f1SBarry Smith     while (ctx->names[k]) {
6818a66092f1SBarry Smith       PetscBool flg;
6819a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6820a66092f1SBarry Smith       if (flg) {
6821a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6822a66092f1SBarry Smith         break;
6823a66092f1SBarry Smith       }
6824a66092f1SBarry Smith       k++;
6825a66092f1SBarry Smith     }
6826a66092f1SBarry Smith     j++;
6827a66092f1SBarry Smith   }
6828a66092f1SBarry Smith   PetscFunctionReturn(0);
6829a66092f1SBarry Smith }
6830a66092f1SBarry Smith 
6831387f4636SBarry Smith /*@C
6832387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6833387f4636SBarry Smith 
6834387f4636SBarry Smith    Collective on TS
6835387f4636SBarry Smith 
6836387f4636SBarry Smith    Input Parameters:
6837387f4636SBarry Smith +  ts - the TS context
6838387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
6839387f4636SBarry Smith 
684095452b02SPatrick Sanan    Notes:
684195452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
68427db568b7SBarry Smith 
6843387f4636SBarry Smith    Level: intermediate
6844387f4636SBarry Smith 
6845387f4636SBarry Smith .keywords: TS,  vector, monitor, view
6846387f4636SBarry Smith 
6847387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6848387f4636SBarry Smith @*/
6849387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6850387f4636SBarry Smith {
6851a66092f1SBarry Smith   PetscInt          i;
6852387f4636SBarry Smith   PetscErrorCode    ierr;
6853387f4636SBarry Smith 
6854387f4636SBarry Smith   PetscFunctionBegin;
6855387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6856387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
68575537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6858b3d3934dSBarry Smith       break;
6859b037adc7SBarry Smith     }
6860b037adc7SBarry Smith   }
6861b037adc7SBarry Smith   PetscFunctionReturn(0);
6862b037adc7SBarry Smith }
6863b037adc7SBarry Smith 
686480666b62SBarry Smith /*@C
686580666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
686680666b62SBarry Smith 
686780666b62SBarry Smith    Collective on TS
686880666b62SBarry Smith 
686980666b62SBarry Smith    Input Parameters:
687080666b62SBarry Smith +  ts - the TS context
687180666b62SBarry Smith .  transform - the transform function
68727684fa3eSBarry Smith .  destroy - function to destroy the optional context
687380666b62SBarry Smith -  ctx - optional context used by transform function
687480666b62SBarry Smith 
687595452b02SPatrick Sanan    Notes:
687695452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
68777db568b7SBarry Smith 
687880666b62SBarry Smith    Level: intermediate
687980666b62SBarry Smith 
688080666b62SBarry Smith .keywords: TS,  vector, monitor, view
688180666b62SBarry Smith 
6882a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
688380666b62SBarry Smith @*/
68847684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
688580666b62SBarry Smith {
688680666b62SBarry Smith   PetscInt          i;
6887a66092f1SBarry Smith   PetscErrorCode    ierr;
688880666b62SBarry Smith 
688980666b62SBarry Smith   PetscFunctionBegin;
689080666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
689180666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
68925537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
689380666b62SBarry Smith     }
689480666b62SBarry Smith   }
689580666b62SBarry Smith   PetscFunctionReturn(0);
689680666b62SBarry Smith }
689780666b62SBarry Smith 
6898e673d494SBarry Smith /*@C
6899e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6900e673d494SBarry Smith 
6901e673d494SBarry Smith    Collective on TSLGCtx
6902e673d494SBarry Smith 
6903e673d494SBarry Smith    Input Parameters:
6904e673d494SBarry Smith +  ts - the TS context
6905e673d494SBarry Smith .  transform - the transform function
69067684fa3eSBarry Smith .  destroy - function to destroy the optional context
6907e673d494SBarry Smith -  ctx - optional context used by transform function
6908e673d494SBarry Smith 
6909e673d494SBarry Smith    Level: intermediate
6910e673d494SBarry Smith 
6911e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6912e673d494SBarry Smith 
6913a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6914e673d494SBarry Smith @*/
69157684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6916e673d494SBarry Smith {
6917e673d494SBarry Smith   PetscFunctionBegin;
6918e673d494SBarry Smith   ctx->transform    = transform;
69197684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6920e673d494SBarry Smith   ctx->transformctx = tctx;
6921e673d494SBarry Smith   PetscFunctionReturn(0);
6922e673d494SBarry Smith }
6923e673d494SBarry Smith 
6924ef20d060SBarry Smith /*@C
69258b668821SLisandro Dalcin    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error
6926ef20d060SBarry Smith        in a time based line graph
6927ef20d060SBarry Smith 
6928ef20d060SBarry Smith    Collective on TS
6929ef20d060SBarry Smith 
6930ef20d060SBarry Smith    Input Parameters:
6931ef20d060SBarry Smith +  ts - the TS context
6932ef20d060SBarry Smith .  step - current time-step
6933ef20d060SBarry Smith .  ptime - current time
69347db568b7SBarry Smith .  u - current solution
69357db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6936ef20d060SBarry Smith 
6937ef20d060SBarry Smith    Level: intermediate
6938ef20d060SBarry Smith 
693995452b02SPatrick Sanan    Notes:
694095452b02SPatrick Sanan     Each process in a parallel run displays its component errors in a separate window
6941abd5a294SJed Brown 
6942abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6943abd5a294SJed Brown 
6944abd5a294SJed Brown    Options Database Keys:
69454f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6946ef20d060SBarry Smith 
6947ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
6948ef20d060SBarry Smith 
6949abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6950ef20d060SBarry Smith @*/
69510910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6952ef20d060SBarry Smith {
6953ef20d060SBarry Smith   PetscErrorCode    ierr;
69540b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6955ef20d060SBarry Smith   const PetscScalar *yy;
6956ef20d060SBarry Smith   Vec               y;
6957ef20d060SBarry Smith 
6958ef20d060SBarry Smith   PetscFunctionBegin;
6959a9f9c1f6SBarry Smith   if (!step) {
6960a9f9c1f6SBarry Smith     PetscDrawAxis axis;
69616934998bSLisandro Dalcin     PetscInt      dim;
6962a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
69638b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr);
69640910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6965a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6966a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6967a9f9c1f6SBarry Smith   }
69680910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6969ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
69700910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6971ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6972e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6973e3efe391SJed Brown   {
6974e3efe391SJed Brown     PetscReal *yreal;
6975e3efe391SJed Brown     PetscInt  i,n;
6976e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6977785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6978e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
69790b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6980e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6981e3efe391SJed Brown   }
6982e3efe391SJed Brown #else
69830b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6984e3efe391SJed Brown #endif
6985ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6986ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6987b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
69880b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69896934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
69903923b477SBarry Smith   }
6991ef20d060SBarry Smith   PetscFunctionReturn(0);
6992ef20d060SBarry Smith }
6993ef20d060SBarry Smith 
69947cf37e64SBarry Smith /*@C
69957cf37e64SBarry Smith    TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep
69967cf37e64SBarry Smith 
69977cf37e64SBarry Smith    Collective on TS
69987cf37e64SBarry Smith 
69997cf37e64SBarry Smith    Input Parameters:
70007cf37e64SBarry Smith +  ts - the TS context
70017cf37e64SBarry Smith .  step - current time-step
70027cf37e64SBarry Smith .  ptime - current time
70037cf37e64SBarry Smith .  u - current solution
70047cf37e64SBarry Smith -  dctx - unused context
70057cf37e64SBarry Smith 
70067cf37e64SBarry Smith    Level: intermediate
70077cf37e64SBarry Smith 
70087cf37e64SBarry Smith    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
70097cf37e64SBarry Smith 
70107cf37e64SBarry Smith    Options Database Keys:
70117cf37e64SBarry Smith .  -ts_monitor_error - create a graphical monitor of error history
70127cf37e64SBarry Smith 
70137cf37e64SBarry Smith .keywords: TS,  vector, monitor, view
70147cf37e64SBarry Smith 
70157cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
70167cf37e64SBarry Smith @*/
7017edbaebb3SBarry Smith PetscErrorCode  TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
70187cf37e64SBarry Smith {
70197cf37e64SBarry Smith   PetscErrorCode    ierr;
70207cf37e64SBarry Smith   Vec               y;
70217cf37e64SBarry Smith   PetscReal         nrm;
7022edbaebb3SBarry Smith   PetscBool         flg;
70237cf37e64SBarry Smith 
70247cf37e64SBarry Smith   PetscFunctionBegin;
70257cf37e64SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
70267cf37e64SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
70277cf37e64SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
7028edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr);
7029edbaebb3SBarry Smith   if (flg) {
70307cf37e64SBarry Smith     ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
7031edbaebb3SBarry Smith     ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr);
7032edbaebb3SBarry Smith   }
7033edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr);
7034edbaebb3SBarry Smith   if (flg) {
7035edbaebb3SBarry Smith     ierr = VecView(y,vf->viewer);CHKERRQ(ierr);
7036edbaebb3SBarry Smith   }
7037edbaebb3SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
70387cf37e64SBarry Smith   PetscFunctionReturn(0);
70397cf37e64SBarry Smith }
70407cf37e64SBarry Smith 
7041201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7042201da799SBarry Smith {
7043201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7044201da799SBarry Smith   PetscReal      x   = ptime,y;
7045201da799SBarry Smith   PetscErrorCode ierr;
7046201da799SBarry Smith   PetscInt       its;
7047201da799SBarry Smith 
7048201da799SBarry Smith   PetscFunctionBegin;
704963e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7050201da799SBarry Smith   if (!n) {
7051201da799SBarry Smith     PetscDrawAxis axis;
7052201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7053201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
7054201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7055201da799SBarry Smith     ctx->snes_its = 0;
7056201da799SBarry Smith   }
7057201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
7058201da799SBarry Smith   y    = its - ctx->snes_its;
7059201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
70603fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7061201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
70626934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7063201da799SBarry Smith   }
7064201da799SBarry Smith   ctx->snes_its = its;
7065201da799SBarry Smith   PetscFunctionReturn(0);
7066201da799SBarry Smith }
7067201da799SBarry Smith 
7068201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7069201da799SBarry Smith {
7070201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7071201da799SBarry Smith   PetscReal      x   = ptime,y;
7072201da799SBarry Smith   PetscErrorCode ierr;
7073201da799SBarry Smith   PetscInt       its;
7074201da799SBarry Smith 
7075201da799SBarry Smith   PetscFunctionBegin;
707663e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7077201da799SBarry Smith   if (!n) {
7078201da799SBarry Smith     PetscDrawAxis axis;
7079201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7080201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
7081201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7082201da799SBarry Smith     ctx->ksp_its = 0;
7083201da799SBarry Smith   }
7084201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
7085201da799SBarry Smith   y    = its - ctx->ksp_its;
7086201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
708799fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7088201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
70896934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7090201da799SBarry Smith   }
7091201da799SBarry Smith   ctx->ksp_its = its;
7092201da799SBarry Smith   PetscFunctionReturn(0);
7093201da799SBarry Smith }
7094f9c1d6abSBarry Smith 
7095f9c1d6abSBarry Smith /*@
7096f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
7097f9c1d6abSBarry Smith 
7098f9c1d6abSBarry Smith    Collective on TS and Vec
7099f9c1d6abSBarry Smith 
7100f9c1d6abSBarry Smith    Input Parameters:
7101f9c1d6abSBarry Smith +  ts - the TS context
7102f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
7103f9c1d6abSBarry Smith 
7104f9c1d6abSBarry Smith    Output Parameters:
7105f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
7106f9c1d6abSBarry Smith 
7107f9c1d6abSBarry Smith    Level: developer
7108f9c1d6abSBarry Smith 
7109f9c1d6abSBarry Smith .keywords: TS, compute
7110f9c1d6abSBarry Smith 
7111f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
7112f9c1d6abSBarry Smith @*/
7113f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7114f9c1d6abSBarry Smith {
7115f9c1d6abSBarry Smith   PetscErrorCode ierr;
7116f9c1d6abSBarry Smith 
7117f9c1d6abSBarry Smith   PetscFunctionBegin;
7118f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7119ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7120f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
7121f9c1d6abSBarry Smith   PetscFunctionReturn(0);
7122f9c1d6abSBarry Smith }
712324655328SShri 
7124b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7125b3d3934dSBarry Smith /*@C
7126b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7127b3d3934dSBarry Smith 
7128b3d3934dSBarry Smith    Collective on TS
7129b3d3934dSBarry Smith 
7130b3d3934dSBarry Smith    Input Parameters:
7131b3d3934dSBarry Smith .  ts  - the ODE solver object
7132b3d3934dSBarry Smith 
7133b3d3934dSBarry Smith    Output Parameter:
7134b3d3934dSBarry Smith .  ctx - the context
7135b3d3934dSBarry Smith 
7136b3d3934dSBarry Smith    Level: intermediate
7137b3d3934dSBarry Smith 
7138b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
7139b3d3934dSBarry Smith 
7140b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7141b3d3934dSBarry Smith 
7142b3d3934dSBarry Smith @*/
7143b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7144b3d3934dSBarry Smith {
7145b3d3934dSBarry Smith   PetscErrorCode ierr;
7146b3d3934dSBarry Smith 
7147b3d3934dSBarry Smith   PetscFunctionBegin;
7148a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7149b3d3934dSBarry Smith   PetscFunctionReturn(0);
7150b3d3934dSBarry Smith }
7151b3d3934dSBarry Smith 
7152b3d3934dSBarry Smith /*@C
7153b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7154b3d3934dSBarry Smith 
7155b3d3934dSBarry Smith    Collective on TS
7156b3d3934dSBarry Smith 
7157b3d3934dSBarry Smith    Input Parameters:
7158b3d3934dSBarry Smith +  ts - the TS context
7159b3d3934dSBarry Smith .  step - current time-step
7160b3d3934dSBarry Smith .  ptime - current time
71617db568b7SBarry Smith .  u  - current solution
71627db568b7SBarry Smith -  dctx - the envelope context
7163b3d3934dSBarry Smith 
7164b3d3934dSBarry Smith    Options Database:
7165b3d3934dSBarry Smith .  -ts_monitor_envelope
7166b3d3934dSBarry Smith 
7167b3d3934dSBarry Smith    Level: intermediate
7168b3d3934dSBarry Smith 
716995452b02SPatrick Sanan    Notes:
717095452b02SPatrick Sanan     after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7171b3d3934dSBarry Smith 
7172b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7173b3d3934dSBarry Smith 
71747db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7175b3d3934dSBarry Smith @*/
71767db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7177b3d3934dSBarry Smith {
7178b3d3934dSBarry Smith   PetscErrorCode       ierr;
71797db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7180b3d3934dSBarry Smith 
7181b3d3934dSBarry Smith   PetscFunctionBegin;
7182b3d3934dSBarry Smith   if (!ctx->max) {
7183b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7184b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7185b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7186b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7187b3d3934dSBarry Smith   } else {
7188b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7189b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7190b3d3934dSBarry Smith   }
7191b3d3934dSBarry Smith   PetscFunctionReturn(0);
7192b3d3934dSBarry Smith }
7193b3d3934dSBarry Smith 
7194b3d3934dSBarry Smith /*@C
7195b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7196b3d3934dSBarry Smith 
7197b3d3934dSBarry Smith    Collective on TS
7198b3d3934dSBarry Smith 
7199b3d3934dSBarry Smith    Input Parameter:
7200b3d3934dSBarry Smith .  ts - the TS context
7201b3d3934dSBarry Smith 
7202b3d3934dSBarry Smith    Output Parameter:
7203b3d3934dSBarry Smith +  max - the maximum values
7204b3d3934dSBarry Smith -  min - the minimum values
7205b3d3934dSBarry Smith 
720695452b02SPatrick Sanan    Notes:
720795452b02SPatrick Sanan     If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
72087db568b7SBarry Smith 
7209b3d3934dSBarry Smith    Level: intermediate
7210b3d3934dSBarry Smith 
7211b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7212b3d3934dSBarry Smith 
7213b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7214b3d3934dSBarry Smith @*/
7215b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7216b3d3934dSBarry Smith {
7217b3d3934dSBarry Smith   PetscInt i;
7218b3d3934dSBarry Smith 
7219b3d3934dSBarry Smith   PetscFunctionBegin;
7220b3d3934dSBarry Smith   if (max) *max = NULL;
7221b3d3934dSBarry Smith   if (min) *min = NULL;
7222b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7223b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
72245537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7225b3d3934dSBarry Smith       if (max) *max = ctx->max;
7226b3d3934dSBarry Smith       if (min) *min = ctx->min;
7227b3d3934dSBarry Smith       break;
7228b3d3934dSBarry Smith     }
7229b3d3934dSBarry Smith   }
7230b3d3934dSBarry Smith   PetscFunctionReturn(0);
7231b3d3934dSBarry Smith }
7232b3d3934dSBarry Smith 
7233b3d3934dSBarry Smith /*@C
7234b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7235b3d3934dSBarry Smith 
7236b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7237b3d3934dSBarry Smith 
7238b3d3934dSBarry Smith    Input Parameter:
7239b3d3934dSBarry Smith .  ctx - the monitor context
7240b3d3934dSBarry Smith 
7241b3d3934dSBarry Smith    Level: intermediate
7242b3d3934dSBarry Smith 
7243b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
7244b3d3934dSBarry Smith 
72457db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7246b3d3934dSBarry Smith @*/
7247b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7248b3d3934dSBarry Smith {
7249b3d3934dSBarry Smith   PetscErrorCode ierr;
7250b3d3934dSBarry Smith 
7251b3d3934dSBarry Smith   PetscFunctionBegin;
7252b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7253b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7254b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7255b3d3934dSBarry Smith   PetscFunctionReturn(0);
7256b3d3934dSBarry Smith }
7257f2dee214SBarry Smith 
725824655328SShri /*@
7259dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
7260dcb233daSLisandro Dalcin 
7261dcb233daSLisandro Dalcin    Collective on TS
7262dcb233daSLisandro Dalcin 
7263dcb233daSLisandro Dalcin    Input Parameter:
7264dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
7265dcb233daSLisandro Dalcin 
7266dcb233daSLisandro Dalcin    Level: advanced
7267dcb233daSLisandro Dalcin 
7268dcb233daSLisandro Dalcin    Notes:
7269dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
7270dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
7271dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
7272dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
7273dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
7274dcb233daSLisandro Dalcin    discontinuous source terms).
7275dcb233daSLisandro Dalcin 
7276dcb233daSLisandro Dalcin .keywords: TS, timestep, restart
7277dcb233daSLisandro Dalcin 
7278dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
7279dcb233daSLisandro Dalcin @*/
7280dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
7281dcb233daSLisandro Dalcin {
7282dcb233daSLisandro Dalcin   PetscFunctionBegin;
7283dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7284dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
7285dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
7286dcb233daSLisandro Dalcin }
7287dcb233daSLisandro Dalcin 
7288dcb233daSLisandro Dalcin /*@
728924655328SShri    TSRollBack - Rolls back one time step
729024655328SShri 
729124655328SShri    Collective on TS
729224655328SShri 
729324655328SShri    Input Parameter:
729424655328SShri .  ts - the TS context obtained from TSCreate()
729524655328SShri 
729624655328SShri    Level: advanced
729724655328SShri 
729824655328SShri .keywords: TS, timestep, rollback
729924655328SShri 
730024655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
730124655328SShri @*/
730224655328SShri PetscErrorCode  TSRollBack(TS ts)
730324655328SShri {
730424655328SShri   PetscErrorCode ierr;
730524655328SShri 
730624655328SShri   PetscFunctionBegin;
730724655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7308b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
730924655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
731024655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
731124655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
731224655328SShri   ts->ptime = ts->ptime_prev;
7313be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
73142808aa04SLisandro Dalcin   ts->steps--;
7315b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
731624655328SShri   PetscFunctionReturn(0);
731724655328SShri }
7318aeb4809dSShri Abhyankar 
7319ff22ae23SHong Zhang /*@
7320ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7321ff22ae23SHong Zhang 
7322ff22ae23SHong Zhang    Input Parameter:
7323ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7324ff22ae23SHong Zhang 
7325ff22ae23SHong Zhang    Level: advanced
7326ff22ae23SHong Zhang 
7327ff22ae23SHong Zhang .keywords: TS, getstages
7328ff22ae23SHong Zhang 
7329ff22ae23SHong Zhang .seealso: TSCreate()
7330ff22ae23SHong Zhang @*/
7331ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7332ff22ae23SHong Zhang {
7333ff22ae23SHong Zhang   PetscErrorCode ierr;
7334ff22ae23SHong Zhang 
7335ff22ae23SHong Zhang   PetscFunctionBegin;
7336ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7337ff22ae23SHong Zhang   PetscValidPointer(ns,2);
7338ff22ae23SHong Zhang 
7339ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
7340ff22ae23SHong Zhang   else {
7341ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7342ff22ae23SHong Zhang   }
7343ff22ae23SHong Zhang   PetscFunctionReturn(0);
7344ff22ae23SHong Zhang }
7345ff22ae23SHong Zhang 
7346847ff0e1SMatthew G. Knepley /*@C
7347847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7348847ff0e1SMatthew G. Knepley 
7349847ff0e1SMatthew G. Knepley   Collective on SNES
7350847ff0e1SMatthew G. Knepley 
7351847ff0e1SMatthew G. Knepley   Input Parameters:
7352847ff0e1SMatthew G. Knepley + ts - the TS context
7353847ff0e1SMatthew G. Knepley . t - current timestep
7354847ff0e1SMatthew G. Knepley . U - state vector
7355847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7356847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7357847ff0e1SMatthew G. Knepley - ctx - an optional user context
7358847ff0e1SMatthew G. Knepley 
7359847ff0e1SMatthew G. Knepley   Output Parameters:
7360847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7361847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7362847ff0e1SMatthew G. Knepley 
7363847ff0e1SMatthew G. Knepley   Level: intermediate
7364847ff0e1SMatthew G. Knepley 
7365847ff0e1SMatthew G. Knepley   Notes:
7366847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7367847ff0e1SMatthew G. Knepley 
7368847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7369847ff0e1SMatthew G. Knepley 
7370847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7371847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7372847ff0e1SMatthew G. Knepley 
7373847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7374847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7375847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7376847ff0e1SMatthew G. Knepley 
7377847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
7378847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7379847ff0e1SMatthew G. Knepley @*/
7380847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7381847ff0e1SMatthew G. Knepley {
7382847ff0e1SMatthew G. Knepley   SNES           snes;
7383847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7384847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7385847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7386847ff0e1SMatthew G. Knepley 
7387847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7388c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7389847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7390847ff0e1SMatthew G. Knepley   if (!color) {
7391847ff0e1SMatthew G. Knepley     DM         dm;
7392847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7393847ff0e1SMatthew G. Knepley 
7394847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7395847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7396847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7397847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7398847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7399847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7400847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7401847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7402847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7403847ff0e1SMatthew G. Knepley     } else {
7404847ff0e1SMatthew G. Knepley       MatColoring mc;
7405847ff0e1SMatthew G. Knepley 
7406847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7407847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7408847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7409847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7410847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7411847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7412847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7413847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7414847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7415847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7416847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7417847ff0e1SMatthew G. Knepley     }
7418847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7419847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7420847ff0e1SMatthew G. Knepley   }
7421847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7422847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7423847ff0e1SMatthew G. Knepley   if (J != B) {
7424847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7425847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7426847ff0e1SMatthew G. Knepley   }
7427847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7428847ff0e1SMatthew G. Knepley }
742993b34091SDebojyoti Ghosh 
7430cb9d8021SPierre Barbier de Reuille /*@
7431cb9d8021SPierre Barbier de Reuille     TSSetFunctionDomainError - Set the function testing if the current state vector is valid
7432cb9d8021SPierre Barbier de Reuille 
7433cb9d8021SPierre Barbier de Reuille     Input Parameters:
7434cb9d8021SPierre Barbier de Reuille     ts - the TS context
7435cb9d8021SPierre Barbier de Reuille     func - function called within TSFunctionDomainError
7436cb9d8021SPierre Barbier de Reuille 
7437cb9d8021SPierre Barbier de Reuille     Level: intermediate
7438cb9d8021SPierre Barbier de Reuille 
7439cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain
7440cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError()
7441cb9d8021SPierre Barbier de Reuille @*/
7442cb9d8021SPierre Barbier de Reuille 
7443d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7444cb9d8021SPierre Barbier de Reuille {
7445cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7446cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7447cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7448cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7449cb9d8021SPierre Barbier de Reuille }
7450cb9d8021SPierre Barbier de Reuille 
7451cb9d8021SPierre Barbier de Reuille /*@
7452cb9d8021SPierre Barbier de Reuille     TSFunctionDomainError - Check if the current state is valid
7453cb9d8021SPierre Barbier de Reuille 
7454cb9d8021SPierre Barbier de Reuille     Input Parameters:
7455cb9d8021SPierre Barbier de Reuille     ts - the TS context
7456cb9d8021SPierre Barbier de Reuille     stagetime - time of the simulation
7457d183316bSPierre Barbier de Reuille     Y - state vector to check.
7458cb9d8021SPierre Barbier de Reuille 
7459cb9d8021SPierre Barbier de Reuille     Output Parameter:
7460cb9d8021SPierre Barbier de Reuille     accept - Set to PETSC_FALSE if the current state vector is valid.
7461cb9d8021SPierre Barbier de Reuille 
7462cb9d8021SPierre Barbier de Reuille     Note:
7463cb9d8021SPierre Barbier de Reuille     This function should be used to ensure the state is in a valid part of the space.
7464cb9d8021SPierre Barbier de Reuille     For example, one can ensure here all values are positive.
746596a0c994SBarry Smith 
746696a0c994SBarry Smith     Level: advanced
7467cb9d8021SPierre Barbier de Reuille @*/
7468d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7469cb9d8021SPierre Barbier de Reuille {
7470cb9d8021SPierre Barbier de Reuille   PetscErrorCode ierr;
7471cb9d8021SPierre Barbier de Reuille 
7472cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7473cb9d8021SPierre Barbier de Reuille 
7474cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7475cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7476cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7477d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7478cb9d8021SPierre Barbier de Reuille   }
7479cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7480cb9d8021SPierre Barbier de Reuille }
74811ceb14c0SBarry Smith 
748293b34091SDebojyoti Ghosh /*@C
7483e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
748493b34091SDebojyoti Ghosh 
748593b34091SDebojyoti Ghosh   Collective on MPI_Comm
748693b34091SDebojyoti Ghosh 
748793b34091SDebojyoti Ghosh   Input Parameter:
748893b34091SDebojyoti Ghosh . tsin    - The input TS
748993b34091SDebojyoti Ghosh 
749093b34091SDebojyoti Ghosh   Output Parameter:
7491e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
749293b34091SDebojyoti Ghosh 
74935eca1a21SEmil Constantinescu   Notes:
74945eca1a21SEmil Constantinescu   This function is used to create a clone of a TS object. It is used in ARKIMEX for initializing the slope for first stage explicit methods. It will likely be replaced in the future with a mechanism of switching methods on the fly.
74955eca1a21SEmil Constantinescu 
7496baa10174SEmil Constantinescu   When using TSDestroy() on a clone the user has to first reset the correct TS reference in the embedded SNES object: e.g.: by running SNES snes_dup=NULL; TSGetSNES(ts,&snes_dup); ierr = TSSetSNES(ts,snes_dup);
74975eca1a21SEmil Constantinescu 
74985eca1a21SEmil Constantinescu   Level: developer
749993b34091SDebojyoti Ghosh 
7500e5168f73SEmil Constantinescu .keywords: TS, clone
7501e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
750293b34091SDebojyoti Ghosh @*/
7503baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
750493b34091SDebojyoti Ghosh {
750593b34091SDebojyoti Ghosh   TS             t;
750693b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7507dc846ba4SSatish Balay   SNES           snes_start;
7508dc846ba4SSatish Balay   DM             dm;
7509dc846ba4SSatish Balay   TSType         type;
751093b34091SDebojyoti Ghosh 
751193b34091SDebojyoti Ghosh   PetscFunctionBegin;
751293b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
751393b34091SDebojyoti Ghosh   *tsout = NULL;
751493b34091SDebojyoti Ghosh 
75157a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
751693b34091SDebojyoti Ghosh 
751793b34091SDebojyoti Ghosh   /* General TS description */
751893b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
751993b34091SDebojyoti Ghosh   t->setupcalled       = 0;
752093b34091SDebojyoti Ghosh   t->ksp_its           = 0;
752193b34091SDebojyoti Ghosh   t->snes_its          = 0;
752293b34091SDebojyoti Ghosh   t->nwork             = 0;
752393b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
752493b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
752593b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
752693b34091SDebojyoti Ghosh 
752734561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
752834561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7529d15a3a53SEmil Constantinescu 
753093b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
753193b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
753293b34091SDebojyoti Ghosh 
753393b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
753451699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
753593b34091SDebojyoti Ghosh 
7536e7069c78SShri   t->trajectory = tsin->trajectory;
7537e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7538e7069c78SShri 
7539e7069c78SShri   t->event = tsin->event;
75406b10a48eSSatish Balay   if (t->event) t->event->refct++;
7541e7069c78SShri 
754293b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
754393b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7544e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
754593b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
754693b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
754793b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
754893b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
754993b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
755093b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
755193b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
755293b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
755393b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
755493b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
755593b34091SDebojyoti Ghosh 
755693b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
755793b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
755893b34091SDebojyoti Ghosh 
755993b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
756093b34091SDebojyoti Ghosh 
756193b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
756293b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
756393b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
756493b34091SDebojyoti Ghosh 
756593b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
756693b34091SDebojyoti Ghosh 
75670d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
75680d4fed19SBarry Smith     PetscInt i;
75690d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
75700d4fed19SBarry Smith     for (i=0; i<10; i++) {
75710d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
75720d4fed19SBarry Smith     }
75730d4fed19SBarry Smith   }
757493b34091SDebojyoti Ghosh   *tsout = t;
757593b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
757693b34091SDebojyoti Ghosh }
7577f3b1f45cSBarry Smith 
7578f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
7579f3b1f45cSBarry Smith {
7580f3b1f45cSBarry Smith   PetscErrorCode ierr;
7581f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
7582f3b1f45cSBarry Smith 
7583f3b1f45cSBarry Smith   PetscFunctionBegin;
7584f3b1f45cSBarry Smith   ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr);
7585f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7586f3b1f45cSBarry Smith }
7587f3b1f45cSBarry Smith 
7588f3b1f45cSBarry Smith /*@
7589f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
7590f3b1f45cSBarry Smith 
7591f3b1f45cSBarry Smith    Logically Collective on TS and Mat
7592f3b1f45cSBarry Smith 
7593f3b1f45cSBarry Smith     Input Parameters:
7594f3b1f45cSBarry Smith     TS - the time stepping routine
7595f3b1f45cSBarry Smith 
7596f3b1f45cSBarry Smith    Output Parameter:
7597f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7598f3b1f45cSBarry Smith 
7599f3b1f45cSBarry Smith    Options Database:
7600f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
7601f3b1f45cSBarry Smith 
7602f3b1f45cSBarry Smith    Level: advanced
7603f3b1f45cSBarry Smith 
760495452b02SPatrick Sanan    Notes:
760595452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7606f3b1f45cSBarry Smith 
7607f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
7608f3b1f45cSBarry Smith @*/
7609f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
7610f3b1f45cSBarry Smith {
7611f3b1f45cSBarry Smith   Mat            J,B;
7612f3b1f45cSBarry Smith   PetscErrorCode ierr;
7613f3b1f45cSBarry Smith   TSRHSJacobian  func;
7614f3b1f45cSBarry Smith   void*          ctx;
7615f3b1f45cSBarry Smith 
7616f3b1f45cSBarry Smith   PetscFunctionBegin;
7617f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7618f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7619f3b1f45cSBarry Smith   ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7620f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7621f3b1f45cSBarry Smith }
7622f3b1f45cSBarry Smith 
7623f3b1f45cSBarry Smith /*@C
7624f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
7625f3b1f45cSBarry Smith 
7626f3b1f45cSBarry Smith    Logically Collective on TS and Mat
7627f3b1f45cSBarry Smith 
7628f3b1f45cSBarry Smith     Input Parameters:
7629f3b1f45cSBarry Smith     TS - the time stepping routine
7630f3b1f45cSBarry Smith 
7631f3b1f45cSBarry Smith    Output Parameter:
7632f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7633f3b1f45cSBarry Smith 
7634f3b1f45cSBarry Smith    Options Database:
7635f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
7636f3b1f45cSBarry Smith 
763795452b02SPatrick Sanan    Notes:
763895452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7639f3b1f45cSBarry Smith 
7640f3b1f45cSBarry Smith    Level: advanced
7641f3b1f45cSBarry Smith 
7642f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
7643f3b1f45cSBarry Smith @*/
7644f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
7645f3b1f45cSBarry Smith {
7646f3b1f45cSBarry Smith   Mat            J,B;
7647f3b1f45cSBarry Smith   PetscErrorCode ierr;
7648f3b1f45cSBarry Smith   void           *ctx;
7649f3b1f45cSBarry Smith   TSRHSJacobian  func;
7650f3b1f45cSBarry Smith 
7651f3b1f45cSBarry Smith   PetscFunctionBegin;
7652f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7653f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7654f3b1f45cSBarry Smith   ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7655f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7656f3b1f45cSBarry Smith }
7657