xref: /petsc/src/ts/interface/ts.c (revision d91943122d534c48ed2d7c83dcc35eeb359641a2)
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:
79b6a60446SDebojyoti Ghosh +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE
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*d9194312SHong Zhang  TSSetRHSFunctionSplit2w - Set the split right-hand-side functions which are based on two-way component partitioning.
1789*d9194312SHong Zhang 
1790*d9194312SHong Zhang  Logically Collective on TS
1791*d9194312SHong Zhang 
1792*d9194312SHong Zhang  Input Parameters:
1793*d9194312SHong Zhang  +  ts  - the TS context obtained from TSCreate()
1794*d9194312SHong Zhang  .  r   - vector to hold the residual (or NULL to have it created internally)
1795*d9194312SHong Zhang  .  fun1 - the first function evaluation routine corresponding
1796*d9194312SHong Zhang  .  fun2 - the second function evaluation routine corresponding
1797*d9194312SHong Zhang  -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1798*d9194312SHong Zhang 
1799*d9194312SHong Zhang  Calling sequence of fun:
1800*d9194312SHong Zhang  $  fun(TS ts,PetscReal t,Vec u,Vec f,ctx);
1801*d9194312SHong Zhang 
1802*d9194312SHong Zhang  +  t    - time at step/stage being solved
1803*d9194312SHong Zhang  .  u    - state vector
1804*d9194312SHong Zhang  .  f    - function vector
1805*d9194312SHong Zhang  -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1806*d9194312SHong Zhang 
1807*d9194312SHong Zhang  Level: beginner
1808*d9194312SHong Zhang 
1809*d9194312SHong Zhang  .keywords: TS, timestep, set, ODE, Hamiltonian, Function
1810*d9194312SHong Zhang 
1811*d9194312SHong Zhang  .seealso: TSGetRHSFunctionSplit2w()
1812*d9194312SHong Zhang  @*/
1813*d9194312SHong Zhang PetscErrorCode TSSetRHSFunctionSplit2w(TS ts,Vec r,TSRHSFunctionSplit2w fun1,TSRHSFunctionSplit2w fun2,void *ctx)
1814*d9194312SHong Zhang {
1815*d9194312SHong Zhang   DM             dm;
1816*d9194312SHong Zhang   SNES           snes;
1817*d9194312SHong Zhang   Vec            ralloc = NULL;
1818*d9194312SHong Zhang   PetscErrorCode ierr;
1819*d9194312SHong Zhang 
1820*d9194312SHong Zhang   PetscFunctionBegin;
1821*d9194312SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1822*d9194312SHong Zhang   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
1823*d9194312SHong Zhang 
1824*d9194312SHong Zhang   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1825*d9194312SHong Zhang   ierr = DMTSSetRHSFunctionSplit2w(dm,fun1,fun2,ctx);CHKERRQ(ierr);
1826*d9194312SHong Zhang 
1827*d9194312SHong Zhang   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1828*d9194312SHong Zhang   if (!r && !ts->dm && ts->vec_sol) {
1829*d9194312SHong Zhang     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1830*d9194312SHong Zhang     r = ralloc;
1831*d9194312SHong Zhang   }
1832*d9194312SHong Zhang   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1833*d9194312SHong Zhang   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1834*d9194312SHong Zhang   PetscFunctionReturn(0);
1835*d9194312SHong Zhang }
1836*d9194312SHong Zhang 
1837*d9194312SHong Zhang /*@C
1838*d9194312SHong Zhang  TSGetRHSFunctionSplit2w - Returns the vector where the RHS residual is stored and the functions to compute it.
1839*d9194312SHong Zhang 
1840*d9194312SHong Zhang  Not Collective
1841*d9194312SHong Zhang 
1842*d9194312SHong Zhang  Input Parameter:
1843*d9194312SHong Zhang  . ts - the TS context
1844*d9194312SHong Zhang 
1845*d9194312SHong Zhang  Output Parameter:
1846*d9194312SHong Zhang  + r - vector to hold residual (or NULL)
1847*d9194312SHong Zhang  . fun1 - the function to compute part of the residual (or NULL)
1848*d9194312SHong Zhang  . fun2 - the function to compute part of the residual (or NULL)
1849*d9194312SHong Zhang  - ctx - the function context (or NULL)
1850*d9194312SHong Zhang 
1851*d9194312SHong Zhang  Level: advanced
1852*d9194312SHong Zhang 
1853*d9194312SHong Zhang  .keywords: TS, nonlinear, get, function
1854*d9194312SHong Zhang 
1855*d9194312SHong Zhang  .seealso: TSSetRHSFunctionSplit2w(), SNESGetFunction()
1856*d9194312SHong Zhang  @*/
1857*d9194312SHong Zhang PetscErrorCode TSGetRHSFunctionSplit2w(TS ts,Vec *r,TSRHSFunctionSplit2w *fun1,TSRHSFunctionSplit2w *fun2,void **ctx)
1858*d9194312SHong Zhang {
1859*d9194312SHong Zhang   PetscErrorCode ierr;
1860*d9194312SHong Zhang   SNES           snes;
1861*d9194312SHong Zhang   DM             dm;
1862*d9194312SHong Zhang 
1863*d9194312SHong Zhang   PetscFunctionBegin;
1864*d9194312SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1865*d9194312SHong Zhang   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1866*d9194312SHong Zhang   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1867*d9194312SHong Zhang   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1868*d9194312SHong Zhang   ierr = DMTSGetRHSFunctionSplit2w(dm,fun1,fun2,ctx);CHKERRQ(ierr);
1869*d9194312SHong Zhang   PetscFunctionReturn(0);
1870*d9194312SHong Zhang }
1871*d9194312SHong Zhang 
1872*d9194312SHong Zhang /*@
1873*d9194312SHong Zhang  TSComputeRHSFunctionSplit2w - Evaluates the right-hand-side function.
1874*d9194312SHong Zhang 
1875*d9194312SHong Zhang  Collective on TS and Vec
1876*d9194312SHong Zhang 
1877*d9194312SHong Zhang  Input Parameters:
1878*d9194312SHong Zhang  +  ts - the TS context
1879*d9194312SHong Zhang  .  t - current time
1880*d9194312SHong Zhang  -  U - state vector
1881*d9194312SHong Zhang 
1882*d9194312SHong Zhang  Output Parameter:
1883*d9194312SHong Zhang  .  y - right hand side
1884*d9194312SHong Zhang 
1885*d9194312SHong Zhang  Note:
1886*d9194312SHong Zhang  Most users should not need to explicitly call this routine, as it
1887*d9194312SHong Zhang  is used internally within the nonlinear solvers.
1888*d9194312SHong Zhang 
1889*d9194312SHong Zhang  Level: developer
1890*d9194312SHong Zhang 
1891*d9194312SHong Zhang  .keywords: TS, compute
1892*d9194312SHong Zhang 
1893*d9194312SHong Zhang  .seealso: TSSetRHSFunctionSplit2w()
1894*d9194312SHong Zhang  @*/
1895*d9194312SHong Zhang PetscErrorCode TSComputeRHSFunctionSplit2w(TS ts,PetscReal t,Vec U,Vec y,PetscInt component)
1896*d9194312SHong Zhang {
1897*d9194312SHong Zhang   PetscErrorCode       ierr;
1898*d9194312SHong Zhang   TSRHSFunctionSplit2w rhsfunction1,rhsfunction2;
1899*d9194312SHong Zhang   void                 *ctx;
1900*d9194312SHong Zhang   DM                   dm;
1901*d9194312SHong Zhang 
1902*d9194312SHong Zhang   PetscFunctionBegin;
1903*d9194312SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1904*d9194312SHong Zhang   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1905*d9194312SHong Zhang   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
1906*d9194312SHong Zhang   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1907*d9194312SHong Zhang 
1908*d9194312SHong Zhang   ierr = DMTSGetRHSFunctionSplit2w(dm,&rhsfunction1,&rhsfunction2,&ctx);CHKERRQ(ierr);
1909*d9194312SHong Zhang 
1910*d9194312SHong Zhang   if (!rhsfunction1 || !rhsfunction2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunctionSplit2w()");
1911*d9194312SHong Zhang 
1912*d9194312SHong Zhang   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
1913*d9194312SHong Zhang 
1914*d9194312SHong Zhang   PetscStackPush("TS user right-hand-side function");
1915*d9194312SHong Zhang   if (component==1) {
1916*d9194312SHong Zhang     ierr = (*rhsfunction1)(ts,t,U,y,ctx);CHKERRQ(ierr);
1917*d9194312SHong Zhang   } else if (component==2) {
1918*d9194312SHong Zhang     ierr = (*rhsfunction2)(ts,t,U,y,ctx);CHKERRQ(ierr);
1919*d9194312SHong Zhang   }
1920*d9194312SHong Zhang   PetscStackPop;
1921*d9194312SHong Zhang 
1922*d9194312SHong Zhang   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
1923*d9194312SHong Zhang   PetscFunctionReturn(0);
1924*d9194312SHong Zhang }
1925*d9194312SHong Zhang 
1926*d9194312SHong Zhang /*@C
192755849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
192855849f57SBarry Smith 
192955849f57SBarry Smith   Collective on PetscViewer
193055849f57SBarry Smith 
193155849f57SBarry Smith   Input Parameters:
193255849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
193355849f57SBarry Smith            some related function before a call to TSLoad().
193455849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
193555849f57SBarry Smith 
193655849f57SBarry Smith    Level: intermediate
193755849f57SBarry Smith 
193855849f57SBarry Smith   Notes:
193955849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
194055849f57SBarry Smith 
194155849f57SBarry Smith   Notes for advanced users:
194255849f57SBarry Smith   Most users should not need to know the details of the binary storage
194355849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
194455849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
194555849f57SBarry Smith   format is
194655849f57SBarry Smith .vb
194755849f57SBarry Smith      has not yet been determined
194855849f57SBarry Smith .ve
194955849f57SBarry Smith 
195055849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
195155849f57SBarry Smith @*/
1952f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
195355849f57SBarry Smith {
195455849f57SBarry Smith   PetscErrorCode ierr;
195555849f57SBarry Smith   PetscBool      isbinary;
195655849f57SBarry Smith   PetscInt       classid;
195755849f57SBarry Smith   char           type[256];
19582d53ad75SBarry Smith   DMTS           sdm;
1959ad6bc421SBarry Smith   DM             dm;
196055849f57SBarry Smith 
196155849f57SBarry Smith   PetscFunctionBegin;
1962f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
196355849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
196455849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
196555849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
196655849f57SBarry Smith 
1967060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1968ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1969060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1970f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1971f2c2a1b9SBarry Smith   if (ts->ops->load) {
1972f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1973f2c2a1b9SBarry Smith   }
1974ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1975ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1976ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1977f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1978f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
19792d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19802d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
198155849f57SBarry Smith   PetscFunctionReturn(0);
198255849f57SBarry Smith }
198355849f57SBarry Smith 
19849804daf3SBarry Smith #include <petscdraw.h>
1985e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1986e04113cfSBarry Smith #include <petscviewersaws.h>
1987f05ece33SBarry Smith #endif
19887e2c5f70SBarry Smith /*@C
1989d763cef2SBarry Smith     TSView - Prints the TS data structure.
1990d763cef2SBarry Smith 
19914c49b128SBarry Smith     Collective on TS
1992d763cef2SBarry Smith 
1993d763cef2SBarry Smith     Input Parameters:
1994d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1995d763cef2SBarry Smith -   viewer - visualization context
1996d763cef2SBarry Smith 
1997d763cef2SBarry Smith     Options Database Key:
1998d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1999d763cef2SBarry Smith 
2000d763cef2SBarry Smith     Notes:
2001d763cef2SBarry Smith     The available visualization contexts include
2002b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
2003b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
2004d763cef2SBarry Smith          output where only the first processor opens
2005d763cef2SBarry Smith          the file.  All other processors send their
2006d763cef2SBarry Smith          data to the first processor to print.
2007d763cef2SBarry Smith 
2008d763cef2SBarry Smith     The user can open an alternative visualization context with
2009b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
2010d763cef2SBarry Smith 
2011d763cef2SBarry Smith     Level: beginner
2012d763cef2SBarry Smith 
2013d763cef2SBarry Smith .keywords: TS, timestep, view
2014d763cef2SBarry Smith 
2015b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
2016d763cef2SBarry Smith @*/
20177087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
2018d763cef2SBarry Smith {
2019dfbe8321SBarry Smith   PetscErrorCode ierr;
202019fd82e9SBarry Smith   TSType         type;
20212b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
20222d53ad75SBarry Smith   DMTS           sdm;
2023e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2024536b137fSBarry Smith   PetscBool      issaws;
2025f05ece33SBarry Smith #endif
2026d763cef2SBarry Smith 
2027d763cef2SBarry Smith   PetscFunctionBegin;
20280700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20293050cee2SBarry Smith   if (!viewer) {
2030ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
20313050cee2SBarry Smith   }
20320700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
2033c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
2034fd16b177SBarry Smith 
2035251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
2036251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
203755849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
20382b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
2039e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2040536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
2041f05ece33SBarry Smith #endif
204232077d6dSBarry Smith   if (iascii) {
2043dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
2044efd4aadfSBarry Smith     if (ts->ops->view) {
2045efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2046efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
2047efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2048efd4aadfSBarry Smith     }
2049ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
205077431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
2051ef85077eSLisandro Dalcin     }
2052ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
20537c8652ddSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
2054ef85077eSLisandro Dalcin     }
2055efd4aadfSBarry Smith     if (ts->usessnes) {
2056efd4aadfSBarry Smith       PetscBool lin;
2057d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
20585ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
2059d763cef2SBarry Smith       }
20605ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
2061efd4aadfSBarry Smith       ierr = PetscObjectTypeCompare((PetscObject)ts->snes,SNESKSPONLY,&lin);CHKERRQ(ierr);
2062efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
2063efd4aadfSBarry Smith     }
2064193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
2065a0af407cSBarry Smith     if (ts->vrtol) {
2066a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
2067a0af407cSBarry Smith     } else {
2068a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
2069a0af407cSBarry Smith     }
2070a0af407cSBarry Smith     if (ts->vatol) {
2071a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
2072a0af407cSBarry Smith     } else {
2073a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
2074a0af407cSBarry Smith     }
2075825ab935SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2076efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
2077825ab935SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2078825ab935SBarry Smith     if (ts->snes && ts->usessnes)  {
2079825ab935SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2080825ab935SBarry Smith       ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
2081825ab935SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2082825ab935SBarry Smith     }
20832d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
20842d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
20850f5bd95cSBarry Smith   } else if (isstring) {
2086a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
2087b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
208855849f57SBarry Smith   } else if (isbinary) {
208955849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
209055849f57SBarry Smith     MPI_Comm    comm;
209155849f57SBarry Smith     PetscMPIInt rank;
209255849f57SBarry Smith     char        type[256];
209355849f57SBarry Smith 
209455849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
209555849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
209655849f57SBarry Smith     if (!rank) {
209755849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
209855849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
209955849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
210055849f57SBarry Smith     }
210155849f57SBarry Smith     if (ts->ops->view) {
210255849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
210355849f57SBarry Smith     }
2104efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2105f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
2106f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
21072d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
21082d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
21092b0a91c0SBarry Smith   } else if (isdraw) {
21102b0a91c0SBarry Smith     PetscDraw draw;
21112b0a91c0SBarry Smith     char      str[36];
211289fd9fafSBarry Smith     PetscReal x,y,bottom,h;
21132b0a91c0SBarry Smith 
21142b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
21152b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
21162b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
21172b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
211851fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
211989fd9fafSBarry Smith     bottom = y - h;
21202b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
21212b0a91c0SBarry Smith     if (ts->ops->view) {
21222b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
21232b0a91c0SBarry Smith     }
2124efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2125efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
21262b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2127e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2128536b137fSBarry Smith   } else if (issaws) {
2129d45a07a7SBarry Smith     PetscMPIInt rank;
21302657e9d9SBarry Smith     const char  *name;
21312657e9d9SBarry Smith 
21322657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
2133d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
2134d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
2135d45a07a7SBarry Smith       char       dir[1024];
2136d45a07a7SBarry Smith 
2137e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2138a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
21392657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
21402657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
21412657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2142d763cef2SBarry Smith     }
21430acecf5bSBarry Smith     if (ts->ops->view) {
21440acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
21450acecf5bSBarry Smith     }
2146f05ece33SBarry Smith #endif
2147f05ece33SBarry Smith   }
2148f05ece33SBarry Smith 
2149b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2150251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2151b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2152d763cef2SBarry Smith   PetscFunctionReturn(0);
2153d763cef2SBarry Smith }
2154d763cef2SBarry Smith 
2155b07ff414SBarry Smith /*@
2156d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2157d763cef2SBarry Smith    the timesteppers.
2158d763cef2SBarry Smith 
21593f9fe445SBarry Smith    Logically Collective on TS
2160d763cef2SBarry Smith 
2161d763cef2SBarry Smith    Input Parameters:
2162d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2163d763cef2SBarry Smith -  usrP - optional user context
2164d763cef2SBarry Smith 
216595452b02SPatrick Sanan    Fortran Notes:
216695452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2167daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2168daf670e6SBarry Smith 
2169d763cef2SBarry Smith    Level: intermediate
2170d763cef2SBarry Smith 
2171d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
2172d763cef2SBarry Smith 
2173d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2174d763cef2SBarry Smith @*/
21757087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2176d763cef2SBarry Smith {
2177d763cef2SBarry Smith   PetscFunctionBegin;
21780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2179d763cef2SBarry Smith   ts->user = usrP;
2180d763cef2SBarry Smith   PetscFunctionReturn(0);
2181d763cef2SBarry Smith }
2182d763cef2SBarry Smith 
2183b07ff414SBarry Smith /*@
2184d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2185d763cef2SBarry Smith     timestepper.
2186d763cef2SBarry Smith 
2187d763cef2SBarry Smith     Not Collective
2188d763cef2SBarry Smith 
2189d763cef2SBarry Smith     Input Parameter:
2190d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2191d763cef2SBarry Smith 
2192d763cef2SBarry Smith     Output Parameter:
2193d763cef2SBarry Smith .   usrP - user context
2194d763cef2SBarry Smith 
219595452b02SPatrick Sanan    Fortran Notes:
219695452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2197daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2198daf670e6SBarry Smith 
2199d763cef2SBarry Smith     Level: intermediate
2200d763cef2SBarry Smith 
2201d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
2202d763cef2SBarry Smith 
2203d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2204d763cef2SBarry Smith @*/
2205e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2206d763cef2SBarry Smith {
2207d763cef2SBarry Smith   PetscFunctionBegin;
22080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2209e71120c6SJed Brown   *(void**)usrP = ts->user;
2210d763cef2SBarry Smith   PetscFunctionReturn(0);
2211d763cef2SBarry Smith }
2212d763cef2SBarry Smith 
2213d763cef2SBarry Smith /*@
221480275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2215d763cef2SBarry Smith 
2216d763cef2SBarry Smith    Not Collective
2217d763cef2SBarry Smith 
2218d763cef2SBarry Smith    Input Parameter:
2219d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2220d763cef2SBarry Smith 
2221d763cef2SBarry Smith    Output Parameter:
222280275a0aSLisandro Dalcin .  steps - number of steps completed so far
2223d763cef2SBarry Smith 
2224d763cef2SBarry Smith    Level: intermediate
2225d763cef2SBarry Smith 
2226d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
22279be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2228d763cef2SBarry Smith @*/
222980275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2230d763cef2SBarry Smith {
2231d763cef2SBarry Smith   PetscFunctionBegin;
22320700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
223380275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
223480275a0aSLisandro Dalcin   *steps = ts->steps;
223580275a0aSLisandro Dalcin   PetscFunctionReturn(0);
223680275a0aSLisandro Dalcin }
223780275a0aSLisandro Dalcin 
223880275a0aSLisandro Dalcin /*@
223980275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
224080275a0aSLisandro Dalcin 
224180275a0aSLisandro Dalcin    Logically Collective on TS
224280275a0aSLisandro Dalcin 
224380275a0aSLisandro Dalcin    Input Parameters:
224480275a0aSLisandro Dalcin +  ts - the TS context
224580275a0aSLisandro Dalcin -  steps - number of steps completed so far
224680275a0aSLisandro Dalcin 
224780275a0aSLisandro Dalcin    Notes:
224880275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
224980275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
225080275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
225180275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
225280275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
225380275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
225480275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
225580275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
225680275a0aSLisandro Dalcin 
225780275a0aSLisandro Dalcin    Level: advanced
225880275a0aSLisandro Dalcin 
225980275a0aSLisandro Dalcin .keywords: TS, timestep, set, iteration, number
226080275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
226180275a0aSLisandro Dalcin @*/
226280275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
226380275a0aSLisandro Dalcin {
226480275a0aSLisandro Dalcin   PetscFunctionBegin;
226580275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
226680275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
226780275a0aSLisandro Dalcin   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
226880275a0aSLisandro Dalcin   ts->steps = steps;
2269d763cef2SBarry Smith   PetscFunctionReturn(0);
2270d763cef2SBarry Smith }
2271d763cef2SBarry Smith 
2272d763cef2SBarry Smith /*@
2273d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2274d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2275d763cef2SBarry Smith 
22763f9fe445SBarry Smith    Logically Collective on TS
2277d763cef2SBarry Smith 
2278d763cef2SBarry Smith    Input Parameters:
2279d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2280d763cef2SBarry Smith -  time_step - the size of the timestep
2281d763cef2SBarry Smith 
2282d763cef2SBarry Smith    Level: intermediate
2283d763cef2SBarry Smith 
2284aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2285d763cef2SBarry Smith 
2286d763cef2SBarry Smith .keywords: TS, set, timestep
2287d763cef2SBarry Smith @*/
22887087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2289d763cef2SBarry Smith {
2290d763cef2SBarry Smith   PetscFunctionBegin;
22910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2292c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2293d763cef2SBarry Smith   ts->time_step = time_step;
2294d763cef2SBarry Smith   PetscFunctionReturn(0);
2295d763cef2SBarry Smith }
2296d763cef2SBarry Smith 
2297a43b19c4SJed Brown /*@
229849354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
229949354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
230049354f04SShri Abhyankar      or just return at the final time TS computed.
2301a43b19c4SJed Brown 
2302a43b19c4SJed Brown   Logically Collective on TS
2303a43b19c4SJed Brown 
2304a43b19c4SJed Brown    Input Parameter:
2305a43b19c4SJed Brown +   ts - the time-step context
230649354f04SShri Abhyankar -   eftopt - exact final time option
2307a43b19c4SJed Brown 
2308feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2309feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2310feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2311feed9e9dSBarry Smith 
2312feed9e9dSBarry Smith    Options Database:
2313feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2314feed9e9dSBarry Smith 
2315ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2316ee346746SBarry Smith     then the final time you selected.
2317ee346746SBarry Smith 
2318a43b19c4SJed Brown    Level: beginner
2319a43b19c4SJed Brown 
2320f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2321a43b19c4SJed Brown @*/
232249354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2323a43b19c4SJed Brown {
2324a43b19c4SJed Brown   PetscFunctionBegin;
2325a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
232649354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
232749354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2328a43b19c4SJed Brown   PetscFunctionReturn(0);
2329a43b19c4SJed Brown }
2330a43b19c4SJed Brown 
2331d763cef2SBarry Smith /*@
2332f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2333f6953c82SLisandro Dalcin 
2334f6953c82SLisandro Dalcin    Not Collective
2335f6953c82SLisandro Dalcin 
2336f6953c82SLisandro Dalcin    Input Parameter:
2337f6953c82SLisandro Dalcin .  ts - the TS context
2338f6953c82SLisandro Dalcin 
2339f6953c82SLisandro Dalcin    Output Parameter:
2340f6953c82SLisandro Dalcin .  eftopt - exact final time option
2341f6953c82SLisandro Dalcin 
2342f6953c82SLisandro Dalcin    Level: beginner
2343f6953c82SLisandro Dalcin 
2344f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2345f6953c82SLisandro Dalcin @*/
2346f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2347f6953c82SLisandro Dalcin {
2348f6953c82SLisandro Dalcin   PetscFunctionBegin;
2349f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2350f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2351f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2352f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2353f6953c82SLisandro Dalcin }
2354f6953c82SLisandro Dalcin 
2355f6953c82SLisandro Dalcin /*@
2356d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2357d763cef2SBarry Smith 
2358d763cef2SBarry Smith    Not Collective
2359d763cef2SBarry Smith 
2360d763cef2SBarry Smith    Input Parameter:
2361d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2362d763cef2SBarry Smith 
2363d763cef2SBarry Smith    Output Parameter:
2364d763cef2SBarry Smith .  dt - the current timestep size
2365d763cef2SBarry Smith 
2366d763cef2SBarry Smith    Level: intermediate
2367d763cef2SBarry Smith 
2368aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2369d763cef2SBarry Smith 
2370d763cef2SBarry Smith .keywords: TS, get, timestep
2371d763cef2SBarry Smith @*/
23727087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2373d763cef2SBarry Smith {
2374d763cef2SBarry Smith   PetscFunctionBegin;
23750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2376f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2377d763cef2SBarry Smith   *dt = ts->time_step;
2378d763cef2SBarry Smith   PetscFunctionReturn(0);
2379d763cef2SBarry Smith }
2380d763cef2SBarry Smith 
2381d8e5e3e6SSatish Balay /*@
2382d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2383d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2384d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2385d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2386d763cef2SBarry Smith 
2387d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2388d763cef2SBarry Smith 
2389d763cef2SBarry Smith    Input Parameter:
2390d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2391d763cef2SBarry Smith 
2392d763cef2SBarry Smith    Output Parameter:
2393d763cef2SBarry Smith .  v - the vector containing the solution
2394d763cef2SBarry Smith 
239563e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
239663e21af5SBarry Smith    final time. It returns the solution at the next timestep.
239763e21af5SBarry Smith 
2398d763cef2SBarry Smith    Level: intermediate
2399d763cef2SBarry Smith 
24000ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2401d763cef2SBarry Smith 
2402d763cef2SBarry Smith .keywords: TS, timestep, get, solution
2403d763cef2SBarry Smith @*/
24047087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2405d763cef2SBarry Smith {
2406d763cef2SBarry Smith   PetscFunctionBegin;
24070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24084482741eSBarry Smith   PetscValidPointer(v,2);
24098737fe31SLisandro Dalcin   *v = ts->vec_sol;
2410d763cef2SBarry Smith   PetscFunctionReturn(0);
2411d763cef2SBarry Smith }
2412d763cef2SBarry Smith 
241303fe5f5eSDebojyoti Ghosh /*@
2414b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
241503fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2416b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
241703fe5f5eSDebojyoti Ghosh    structure as the solution vector.
241803fe5f5eSDebojyoti Ghosh 
241903fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
242003fe5f5eSDebojyoti Ghosh 
242103fe5f5eSDebojyoti Ghosh    Parameters :
242203fe5f5eSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2423b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2424b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
242503fe5f5eSDebojyoti Ghosh        returned in v.
2426b2bf4f3aSDebojyoti Ghosh .  v - the vector containing the n-th solution component
242703fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2428b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
242903fe5f5eSDebojyoti Ghosh 
24304cdd57e5SDebojyoti Ghosh    Level: advanced
243103fe5f5eSDebojyoti Ghosh 
243203fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
243303fe5f5eSDebojyoti Ghosh 
243403fe5f5eSDebojyoti Ghosh .keywords: TS, timestep, get, solution
243503fe5f5eSDebojyoti Ghosh @*/
2436b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
243703fe5f5eSDebojyoti Ghosh {
243803fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
243903fe5f5eSDebojyoti Ghosh 
244003fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
244103fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2442b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
244303fe5f5eSDebojyoti Ghosh   else {
2444b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
244503fe5f5eSDebojyoti Ghosh   }
244603fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
244703fe5f5eSDebojyoti Ghosh }
244803fe5f5eSDebojyoti Ghosh 
24494cdd57e5SDebojyoti Ghosh /*@
24504cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
24514cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
24524cdd57e5SDebojyoti Ghosh 
24534cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
24544cdd57e5SDebojyoti Ghosh 
24554cdd57e5SDebojyoti Ghosh    Parameters :
24564cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
24574cdd57e5SDebojyoti Ghosh .  v - the vector containing the auxiliary solution
24584cdd57e5SDebojyoti Ghosh 
24594cdd57e5SDebojyoti Ghosh    Level: intermediate
24604cdd57e5SDebojyoti Ghosh 
24614cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
24624cdd57e5SDebojyoti Ghosh 
24634cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, solution
24644cdd57e5SDebojyoti Ghosh @*/
24654cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
24664cdd57e5SDebojyoti Ghosh {
24674cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
24684cdd57e5SDebojyoti Ghosh 
24694cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
24704cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2471f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
24724cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2473f6356ec7SDebojyoti Ghosh   } else {
2474f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v); CHKERRQ(ierr);
2475f6356ec7SDebojyoti Ghosh   }
24764cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
24774cdd57e5SDebojyoti Ghosh }
24784cdd57e5SDebojyoti Ghosh 
24794cdd57e5SDebojyoti Ghosh /*@
24804cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
24814cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
24824cdd57e5SDebojyoti Ghosh 
24834cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
24844cdd57e5SDebojyoti Ghosh 
24859657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
24869657682dSDebojyoti Ghosh 
24874cdd57e5SDebojyoti Ghosh    Parameters :
24884cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2489657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
24904cdd57e5SDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
24914cdd57e5SDebojyoti Ghosh 
24924cdd57e5SDebojyoti Ghosh    Level: intermediate
24934cdd57e5SDebojyoti Ghosh 
249457df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
24954cdd57e5SDebojyoti Ghosh 
24964cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, error
24974cdd57e5SDebojyoti Ghosh @*/
24980a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
24994cdd57e5SDebojyoti Ghosh {
25004cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
25014cdd57e5SDebojyoti Ghosh 
25024cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
25034cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2504f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
25050a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2506f6356ec7SDebojyoti Ghosh   } else {
2507f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2508f6356ec7SDebojyoti Ghosh   }
25094cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
25104cdd57e5SDebojyoti Ghosh }
25114cdd57e5SDebojyoti Ghosh 
251257df6a1bSDebojyoti Ghosh /*@
251357df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
251457df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
251557df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
251657df6a1bSDebojyoti Ghosh 
251757df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
251857df6a1bSDebojyoti Ghosh 
251957df6a1bSDebojyoti Ghosh    Parameters :
252057df6a1bSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
252157df6a1bSDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
252257df6a1bSDebojyoti Ghosh 
252357df6a1bSDebojyoti Ghosh    Level: intermediate
252457df6a1bSDebojyoti Ghosh 
252557df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
252657df6a1bSDebojyoti Ghosh 
252757df6a1bSDebojyoti Ghosh .keywords: TS, timestep, get, error
252857df6a1bSDebojyoti Ghosh @*/
252957df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
253057df6a1bSDebojyoti Ghosh {
253157df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
253257df6a1bSDebojyoti Ghosh 
253357df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
253457df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25359657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
253657df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
253757df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
253857df6a1bSDebojyoti Ghosh   }
253957df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
254057df6a1bSDebojyoti Ghosh }
254157df6a1bSDebojyoti Ghosh 
2542bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2543d8e5e3e6SSatish Balay /*@
2544bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2545d763cef2SBarry Smith 
2546bdad233fSMatthew Knepley   Not collective
2547d763cef2SBarry Smith 
2548bdad233fSMatthew Knepley   Input Parameters:
2549bdad233fSMatthew Knepley + ts   - The TS
2550bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2551d763cef2SBarry Smith .vb
25520910c330SBarry Smith          U_t - A U = 0      (linear)
25530910c330SBarry Smith          U_t - A(t) U = 0   (linear)
25540910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2555d763cef2SBarry Smith .ve
2556d763cef2SBarry Smith 
2557d763cef2SBarry Smith    Level: beginner
2558d763cef2SBarry Smith 
2559bdad233fSMatthew Knepley .keywords: TS, problem type
2560bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2561d763cef2SBarry Smith @*/
25627087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2563a7cc72afSBarry Smith {
25649e2a6581SJed Brown   PetscErrorCode ierr;
25659e2a6581SJed Brown 
2566d763cef2SBarry Smith   PetscFunctionBegin;
25670700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2568bdad233fSMatthew Knepley   ts->problem_type = type;
25699e2a6581SJed Brown   if (type == TS_LINEAR) {
25709e2a6581SJed Brown     SNES snes;
25719e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
25729e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
25739e2a6581SJed Brown   }
2574d763cef2SBarry Smith   PetscFunctionReturn(0);
2575d763cef2SBarry Smith }
2576d763cef2SBarry Smith 
2577bdad233fSMatthew Knepley /*@C
2578bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2579bdad233fSMatthew Knepley 
2580bdad233fSMatthew Knepley   Not collective
2581bdad233fSMatthew Knepley 
2582bdad233fSMatthew Knepley   Input Parameter:
2583bdad233fSMatthew Knepley . ts   - The TS
2584bdad233fSMatthew Knepley 
2585bdad233fSMatthew Knepley   Output Parameter:
2586bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2587bdad233fSMatthew Knepley .vb
2588089b2837SJed Brown          M U_t = A U
2589089b2837SJed Brown          M(t) U_t = A(t) U
2590b5abc632SBarry Smith          F(t,U,U_t)
2591bdad233fSMatthew Knepley .ve
2592bdad233fSMatthew Knepley 
2593bdad233fSMatthew Knepley    Level: beginner
2594bdad233fSMatthew Knepley 
2595bdad233fSMatthew Knepley .keywords: TS, problem type
2596bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2597bdad233fSMatthew Knepley @*/
25987087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2599a7cc72afSBarry Smith {
2600bdad233fSMatthew Knepley   PetscFunctionBegin;
26010700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
26024482741eSBarry Smith   PetscValidIntPointer(type,2);
2603bdad233fSMatthew Knepley   *type = ts->problem_type;
2604bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2605bdad233fSMatthew Knepley }
2606d763cef2SBarry Smith 
2607d763cef2SBarry Smith /*@
2608d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
2609d763cef2SBarry Smith    of a timestepper.
2610d763cef2SBarry Smith 
2611d763cef2SBarry Smith    Collective on TS
2612d763cef2SBarry Smith 
2613d763cef2SBarry Smith    Input Parameter:
2614d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2615d763cef2SBarry Smith 
2616d763cef2SBarry Smith    Notes:
2617d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2618d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2619141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2620d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2621141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2622d763cef2SBarry Smith 
2623d763cef2SBarry Smith    Level: advanced
2624d763cef2SBarry Smith 
2625d763cef2SBarry Smith .keywords: TS, timestep, setup
2626d763cef2SBarry Smith 
2627141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2628d763cef2SBarry Smith @*/
26297087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2630d763cef2SBarry Smith {
2631dfbe8321SBarry Smith   PetscErrorCode ierr;
26326c6b9e74SPeter Brune   DM             dm;
26336c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2634d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2635cd11d68dSLisandro Dalcin   TSIFunction    ifun;
26366c6b9e74SPeter Brune   TSIJacobian    ijac;
2637efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
26386c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
26392ffb9264SLisandro Dalcin   PetscBool      isnone;
2640d763cef2SBarry Smith 
2641d763cef2SBarry Smith   PetscFunctionBegin;
26420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2643277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2644277b19d0SLisandro Dalcin 
26457adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2646cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2647cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2648d763cef2SBarry Smith   }
2649277b19d0SLisandro Dalcin 
2650277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
2651277b19d0SLisandro Dalcin 
2652e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
2653e1244c69SJed Brown     Mat Amat,Pmat;
2654e1244c69SJed Brown     SNES snes;
2655e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2656e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2657e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2658e1244c69SJed Brown      * have displaced the RHS matrix */
2659e1244c69SJed Brown     if (Amat == ts->Arhs) {
2660abc0d4abSBarry 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 */
2661abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2662e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2663e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2664e1244c69SJed Brown     }
2665e1244c69SJed Brown     if (Pmat == ts->Brhs) {
2666abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2667e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2668e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2669e1244c69SJed Brown     }
2670e1244c69SJed Brown   }
26712ffb9264SLisandro Dalcin 
26722ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
26732ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
26742ffb9264SLisandro Dalcin 
2675277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2676000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2677277b19d0SLisandro Dalcin   }
2678277b19d0SLisandro Dalcin 
26792ffb9264SLisandro Dalcin   /* Attempt to check/preset a default value for the exact final time option */
26802ffb9264SLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
26812ffb9264SLisandro Dalcin   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED)
26822ffb9264SLisandro Dalcin     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
26832ffb9264SLisandro Dalcin 
2684a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
26856c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
26866c6b9e74SPeter Brune    */
26876c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
26880298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
26896c6b9e74SPeter Brune   if (!func) {
26906c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
26916c6b9e74SPeter Brune   }
2692a6772fa2SLisandro 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.
26936c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
26946c6b9e74SPeter Brune    */
26950298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
26960298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2697efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
26980298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2699efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
27006c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
27016c6b9e74SPeter Brune   }
2702c0517034SDebojyoti Ghosh 
2703c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2704c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2705c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2706c0517034SDebojyoti Ghosh   }
2707c0517034SDebojyoti Ghosh 
2708277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2709277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2710277b19d0SLisandro Dalcin }
2711277b19d0SLisandro Dalcin 
2712f6a906c0SBarry Smith /*@
2713277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2714277b19d0SLisandro Dalcin 
2715277b19d0SLisandro Dalcin    Collective on TS
2716277b19d0SLisandro Dalcin 
2717277b19d0SLisandro Dalcin    Input Parameter:
2718277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2719277b19d0SLisandro Dalcin 
2720277b19d0SLisandro Dalcin    Level: beginner
2721277b19d0SLisandro Dalcin 
2722277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
2723277b19d0SLisandro Dalcin 
2724277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2725277b19d0SLisandro Dalcin @*/
2726277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2727277b19d0SLisandro Dalcin {
2728277b19d0SLisandro Dalcin   PetscErrorCode ierr;
2729277b19d0SLisandro Dalcin 
2730277b19d0SLisandro Dalcin   PetscFunctionBegin;
2731277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2732b18ea86cSHong Zhang 
2733277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2734277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2735277b19d0SLisandro Dalcin   }
2736277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2737e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2738bbd56ea5SKarl Rupp 
27394e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
27404e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2741214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
27426bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2743efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2744e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2745e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
274638637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2747bbd56ea5SKarl Rupp 
2748d8151eeaSBarry Smith   ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2749d8151eeaSBarry Smith   ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2750715f1b00SHong Zhang 
2751ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
27522c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
275336eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
275413b1b0ffSHong Zhang   ierr = MatDestroy(&ts->mat_sensip);CHKERRQ(ierr);
2755022c081aSHong Zhang 
2756277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2757d763cef2SBarry Smith   PetscFunctionReturn(0);
2758d763cef2SBarry Smith }
2759d763cef2SBarry Smith 
2760d8e5e3e6SSatish Balay /*@
2761d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2762d763cef2SBarry Smith    with TSCreate().
2763d763cef2SBarry Smith 
2764d763cef2SBarry Smith    Collective on TS
2765d763cef2SBarry Smith 
2766d763cef2SBarry Smith    Input Parameter:
2767d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2768d763cef2SBarry Smith 
2769d763cef2SBarry Smith    Level: beginner
2770d763cef2SBarry Smith 
2771d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2772d763cef2SBarry Smith 
2773d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2774d763cef2SBarry Smith @*/
27756bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2776d763cef2SBarry Smith {
27776849ba73SBarry Smith   PetscErrorCode ierr;
2778d763cef2SBarry Smith 
2779d763cef2SBarry Smith   PetscFunctionBegin;
27806bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
27816bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
27826bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2783d763cef2SBarry Smith 
27846bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2785277b19d0SLisandro Dalcin 
2786e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2787e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
27886bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
27896d4c513bSLisandro Dalcin 
2790bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2791bc952696SBarry Smith 
279284df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
27936427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
27946427ac75SLisandro Dalcin 
27956bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
27966bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
27976bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
27980dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
27996d4c513bSLisandro Dalcin 
2800a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2801d763cef2SBarry Smith   PetscFunctionReturn(0);
2802d763cef2SBarry Smith }
2803d763cef2SBarry Smith 
2804d8e5e3e6SSatish Balay /*@
2805d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2806d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2807d763cef2SBarry Smith 
2808d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2809d763cef2SBarry Smith 
2810d763cef2SBarry Smith    Input Parameter:
2811d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2812d763cef2SBarry Smith 
2813d763cef2SBarry Smith    Output Parameter:
2814d763cef2SBarry Smith .  snes - the nonlinear solver context
2815d763cef2SBarry Smith 
2816d763cef2SBarry Smith    Notes:
2817d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2818d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
281994b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2820d763cef2SBarry Smith 
2821d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
28220298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2823d763cef2SBarry Smith 
2824d763cef2SBarry Smith    Level: beginner
2825d763cef2SBarry Smith 
2826d763cef2SBarry Smith .keywords: timestep, get, SNES
2827d763cef2SBarry Smith @*/
28287087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2829d763cef2SBarry Smith {
2830d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2831d372ba47SLisandro Dalcin 
2832d763cef2SBarry Smith   PetscFunctionBegin;
28330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28344482741eSBarry Smith   PetscValidPointer(snes,2);
2835d372ba47SLisandro Dalcin   if (!ts->snes) {
2836ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
28370298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
28383bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2839d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2840496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
28419e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
28429e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
28439e2a6581SJed Brown     }
2844d372ba47SLisandro Dalcin   }
2845d763cef2SBarry Smith   *snes = ts->snes;
2846d763cef2SBarry Smith   PetscFunctionReturn(0);
2847d763cef2SBarry Smith }
2848d763cef2SBarry Smith 
2849deb2cd25SJed Brown /*@
2850deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2851deb2cd25SJed Brown 
2852deb2cd25SJed Brown    Collective
2853deb2cd25SJed Brown 
2854deb2cd25SJed Brown    Input Parameter:
2855deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2856deb2cd25SJed Brown -  snes - the nonlinear solver context
2857deb2cd25SJed Brown 
2858deb2cd25SJed Brown    Notes:
2859deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2860deb2cd25SJed Brown 
2861deb2cd25SJed Brown    Level: developer
2862deb2cd25SJed Brown 
2863deb2cd25SJed Brown .keywords: timestep, set, SNES
2864deb2cd25SJed Brown @*/
2865deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2866deb2cd25SJed Brown {
2867deb2cd25SJed Brown   PetscErrorCode ierr;
2868d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2869deb2cd25SJed Brown 
2870deb2cd25SJed Brown   PetscFunctionBegin;
2871deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2872deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2873deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2874deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2875bbd56ea5SKarl Rupp 
2876deb2cd25SJed Brown   ts->snes = snes;
2877bbd56ea5SKarl Rupp 
28780298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
28790298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2880740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
28810298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2882740132f1SEmil Constantinescu   }
2883deb2cd25SJed Brown   PetscFunctionReturn(0);
2884deb2cd25SJed Brown }
2885deb2cd25SJed Brown 
2886d8e5e3e6SSatish Balay /*@
288794b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2888d763cef2SBarry Smith    a TS (timestepper) context.
2889d763cef2SBarry Smith 
289094b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2891d763cef2SBarry Smith 
2892d763cef2SBarry Smith    Input Parameter:
2893d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2894d763cef2SBarry Smith 
2895d763cef2SBarry Smith    Output Parameter:
289694b7f48cSBarry Smith .  ksp - the nonlinear solver context
2897d763cef2SBarry Smith 
2898d763cef2SBarry Smith    Notes:
289994b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2900d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2901d763cef2SBarry Smith    KSP and PC contexts as well.
2902d763cef2SBarry Smith 
290394b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
29040298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2905d763cef2SBarry Smith 
2906d763cef2SBarry Smith    Level: beginner
2907d763cef2SBarry Smith 
290894b7f48cSBarry Smith .keywords: timestep, get, KSP
2909d763cef2SBarry Smith @*/
29107087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2911d763cef2SBarry Smith {
2912d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2913089b2837SJed Brown   SNES           snes;
2914d372ba47SLisandro Dalcin 
2915d763cef2SBarry Smith   PetscFunctionBegin;
29160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
29174482741eSBarry Smith   PetscValidPointer(ksp,2);
291817186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2919e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2920089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2921089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2922d763cef2SBarry Smith   PetscFunctionReturn(0);
2923d763cef2SBarry Smith }
2924d763cef2SBarry Smith 
2925d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2926d763cef2SBarry Smith 
2927adb62b0dSMatthew Knepley /*@
2928618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
2929618ce8baSLisandro Dalcin 
2930618ce8baSLisandro Dalcin    Logically Collective on TS
2931618ce8baSLisandro Dalcin 
2932618ce8baSLisandro Dalcin    Input Parameters:
2933618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2934618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
2935618ce8baSLisandro Dalcin 
2936618ce8baSLisandro Dalcin    Options Database Keys:
2937618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
2938618ce8baSLisandro Dalcin 
2939618ce8baSLisandro Dalcin    Notes:
2940618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
2941618ce8baSLisandro Dalcin 
2942618ce8baSLisandro Dalcin    Level: intermediate
2943618ce8baSLisandro Dalcin 
2944618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, steps
2945618ce8baSLisandro Dalcin 
2946618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
2947618ce8baSLisandro Dalcin @*/
2948618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
2949618ce8baSLisandro Dalcin {
2950618ce8baSLisandro Dalcin   PetscFunctionBegin;
2951618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2952618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2953618ce8baSLisandro Dalcin   if (maxsteps < 0 ) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
2954618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
2955618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2956618ce8baSLisandro Dalcin }
2957618ce8baSLisandro Dalcin 
2958618ce8baSLisandro Dalcin /*@
2959618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
2960618ce8baSLisandro Dalcin 
2961618ce8baSLisandro Dalcin    Not Collective
2962618ce8baSLisandro Dalcin 
2963618ce8baSLisandro Dalcin    Input Parameters:
2964618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2965618ce8baSLisandro Dalcin 
2966618ce8baSLisandro Dalcin    Output Parameter:
2967618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
2968618ce8baSLisandro Dalcin 
2969618ce8baSLisandro Dalcin    Level: advanced
2970618ce8baSLisandro Dalcin 
2971618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, steps
2972618ce8baSLisandro Dalcin 
2973618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
2974618ce8baSLisandro Dalcin @*/
2975618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
2976618ce8baSLisandro Dalcin {
2977618ce8baSLisandro Dalcin   PetscFunctionBegin;
2978618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2979618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
2980618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
2981618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2982618ce8baSLisandro Dalcin }
2983618ce8baSLisandro Dalcin 
2984618ce8baSLisandro Dalcin /*@
2985618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
2986618ce8baSLisandro Dalcin 
2987618ce8baSLisandro Dalcin    Logically Collective on TS
2988618ce8baSLisandro Dalcin 
2989618ce8baSLisandro Dalcin    Input Parameters:
2990618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2991618ce8baSLisandro Dalcin -  maxtime - final time to step to
2992618ce8baSLisandro Dalcin 
2993618ce8baSLisandro Dalcin    Options Database Keys:
2994ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
2995618ce8baSLisandro Dalcin 
2996618ce8baSLisandro Dalcin    Notes:
2997618ce8baSLisandro Dalcin    The default maximum time is 5.0
2998618ce8baSLisandro Dalcin 
2999618ce8baSLisandro Dalcin    Level: intermediate
3000618ce8baSLisandro Dalcin 
3001618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, time
3002618ce8baSLisandro Dalcin 
3003618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
3004618ce8baSLisandro Dalcin @*/
3005618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
3006618ce8baSLisandro Dalcin {
3007618ce8baSLisandro Dalcin   PetscFunctionBegin;
3008618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3009618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
3010618ce8baSLisandro Dalcin   ts->max_time = maxtime;
3011618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3012618ce8baSLisandro Dalcin }
3013618ce8baSLisandro Dalcin 
3014618ce8baSLisandro Dalcin /*@
3015618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
3016618ce8baSLisandro Dalcin 
3017618ce8baSLisandro Dalcin    Not Collective
3018618ce8baSLisandro Dalcin 
3019618ce8baSLisandro Dalcin    Input Parameters:
3020618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
3021618ce8baSLisandro Dalcin 
3022618ce8baSLisandro Dalcin    Output Parameter:
3023618ce8baSLisandro Dalcin .  maxtime - final time to step to
3024618ce8baSLisandro Dalcin 
3025618ce8baSLisandro Dalcin    Level: advanced
3026618ce8baSLisandro Dalcin 
3027618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, time
3028618ce8baSLisandro Dalcin 
3029618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
3030618ce8baSLisandro Dalcin @*/
3031618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
3032618ce8baSLisandro Dalcin {
3033618ce8baSLisandro Dalcin   PetscFunctionBegin;
3034618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3035618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
3036618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
3037618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
3038618ce8baSLisandro Dalcin }
3039618ce8baSLisandro Dalcin 
3040618ce8baSLisandro Dalcin /*@
3041aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
3042edc382c3SSatish Balay 
3043edc382c3SSatish Balay    Level: deprecated
3044edc382c3SSatish Balay 
3045aaa6c58dSLisandro Dalcin @*/
3046aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
3047aaa6c58dSLisandro Dalcin {
3048aaa6c58dSLisandro Dalcin   PetscErrorCode ierr;
3049aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
3050aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3051aaa6c58dSLisandro Dalcin   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
3052aaa6c58dSLisandro Dalcin   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
3053aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
3054aaa6c58dSLisandro Dalcin }
3055aaa6c58dSLisandro Dalcin 
3056aaa6c58dSLisandro Dalcin /*@
305719eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
3058edc382c3SSatish Balay 
3059edc382c3SSatish Balay    Level: deprecated
3060edc382c3SSatish Balay 
3061adb62b0dSMatthew Knepley @*/
30627087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
3063adb62b0dSMatthew Knepley {
3064adb62b0dSMatthew Knepley   PetscFunctionBegin;
30650700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3066abc0a331SBarry Smith   if (maxsteps) {
30674482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
3068adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
3069adb62b0dSMatthew Knepley   }
3070abc0a331SBarry Smith   if (maxtime) {
30714482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
3072adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
3073adb62b0dSMatthew Knepley   }
3074adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
3075adb62b0dSMatthew Knepley }
3076adb62b0dSMatthew Knepley 
3077d763cef2SBarry Smith /*@
307819eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
3079edc382c3SSatish Balay 
3080edc382c3SSatish Balay    Level: deprecated
3081edc382c3SSatish Balay 
3082d763cef2SBarry Smith @*/
30837087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
3084d763cef2SBarry Smith {
3085d763cef2SBarry Smith   PetscFunctionBegin;
30860700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3087c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3088c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
308939b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
309039b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
3091d763cef2SBarry Smith   PetscFunctionReturn(0);
3092d763cef2SBarry Smith }
3093d763cef2SBarry Smith 
3094d763cef2SBarry Smith /*@
30955c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
3096edc382c3SSatish Balay 
3097edc382c3SSatish Balay    Level: deprecated
3098edc382c3SSatish Balay 
30995c5f5948SLisandro Dalcin @*/
3100e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31015c5f5948SLisandro Dalcin 
310219eac22cSLisandro Dalcin /*@
31034f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
3104edc382c3SSatish Balay 
3105edc382c3SSatish Balay    Level: deprecated
3106edc382c3SSatish Balay 
31074f4e0956SLisandro Dalcin @*/
31084f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
31094f4e0956SLisandro Dalcin 
31104f4e0956SLisandro Dalcin /*@
3111d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
3112d763cef2SBarry Smith    for use by the TS routines.
3113d763cef2SBarry Smith 
31143f9fe445SBarry Smith    Logically Collective on TS and Vec
3115d763cef2SBarry Smith 
3116d763cef2SBarry Smith    Input Parameters:
3117d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
31180910c330SBarry Smith -  u - the solution vector
3119d763cef2SBarry Smith 
3120d763cef2SBarry Smith    Level: beginner
3121d763cef2SBarry Smith 
3122715f1b00SHong Zhang .keywords: TS, timestep, set, solution, initial values
31230ed3bfb6SBarry Smith 
31240ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3125d763cef2SBarry Smith @*/
31260910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3127d763cef2SBarry Smith {
31288737fe31SLisandro Dalcin   PetscErrorCode ierr;
3129496e6a7aSJed Brown   DM             dm;
31308737fe31SLisandro Dalcin 
3131d763cef2SBarry Smith   PetscFunctionBegin;
31320700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
31330910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
31340910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
31356bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
31360910c330SBarry Smith   ts->vec_sol = u;
3137bbd56ea5SKarl Rupp 
3138496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
31390910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
3140d763cef2SBarry Smith   PetscFunctionReturn(0);
3141d763cef2SBarry Smith }
3142d763cef2SBarry Smith 
3143ac226902SBarry Smith /*@C
3144000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
31453f2090d5SJed Brown   called once at the beginning of each time step.
3146000e7ae3SMatthew Knepley 
31473f9fe445SBarry Smith   Logically Collective on TS
3148000e7ae3SMatthew Knepley 
3149000e7ae3SMatthew Knepley   Input Parameters:
3150000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3151000e7ae3SMatthew Knepley - func - The function
3152000e7ae3SMatthew Knepley 
3153000e7ae3SMatthew Knepley   Calling sequence of func:
3154000e7ae3SMatthew Knepley . func (TS ts);
3155000e7ae3SMatthew Knepley 
3156000e7ae3SMatthew Knepley   Level: intermediate
3157000e7ae3SMatthew Knepley 
3158000e7ae3SMatthew Knepley .keywords: TS, timestep
3159dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3160000e7ae3SMatthew Knepley @*/
31617087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3162000e7ae3SMatthew Knepley {
3163000e7ae3SMatthew Knepley   PetscFunctionBegin;
31640700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3165ae60f76fSBarry Smith   ts->prestep = func;
3166000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3167000e7ae3SMatthew Knepley }
3168000e7ae3SMatthew Knepley 
316909ee8438SJed Brown /*@
31703f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
31713f2090d5SJed Brown 
31723f2090d5SJed Brown   Collective on TS
31733f2090d5SJed Brown 
31743f2090d5SJed Brown   Input Parameters:
31753f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
31763f2090d5SJed Brown 
31773f2090d5SJed Brown   Notes:
31783f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
31793f2090d5SJed Brown   so most users would not generally call this routine themselves.
31803f2090d5SJed Brown 
31813f2090d5SJed Brown   Level: developer
31823f2090d5SJed Brown 
31833f2090d5SJed Brown .keywords: TS, timestep
31849be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
31853f2090d5SJed Brown @*/
31867087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
31873f2090d5SJed Brown {
31883f2090d5SJed Brown   PetscErrorCode ierr;
31893f2090d5SJed Brown 
31903f2090d5SJed Brown   PetscFunctionBegin;
31910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3192ae60f76fSBarry Smith   if (ts->prestep) {
31935efd42a4SStefano Zampini     Vec              U;
31945efd42a4SStefano Zampini     PetscObjectState sprev,spost;
31955efd42a4SStefano Zampini 
31965efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
31975efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3198ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
31995efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3200dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3201312ce896SJed Brown   }
32023f2090d5SJed Brown   PetscFunctionReturn(0);
32033f2090d5SJed Brown }
32043f2090d5SJed Brown 
3205b8123daeSJed Brown /*@C
3206b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3207b8123daeSJed Brown   called once at the beginning of each stage.
3208b8123daeSJed Brown 
3209b8123daeSJed Brown   Logically Collective on TS
3210b8123daeSJed Brown 
3211b8123daeSJed Brown   Input Parameters:
3212b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3213b8123daeSJed Brown - func - The function
3214b8123daeSJed Brown 
3215b8123daeSJed Brown   Calling sequence of func:
3216b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
3217b8123daeSJed Brown 
3218b8123daeSJed Brown   Level: intermediate
3219b8123daeSJed Brown 
3220b8123daeSJed Brown   Note:
3221b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
322280275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3223b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3224b8123daeSJed Brown 
3225b8123daeSJed Brown .keywords: TS, timestep
32269be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3227b8123daeSJed Brown @*/
3228b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3229b8123daeSJed Brown {
3230b8123daeSJed Brown   PetscFunctionBegin;
3231b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3232ae60f76fSBarry Smith   ts->prestage = func;
3233b8123daeSJed Brown   PetscFunctionReturn(0);
3234b8123daeSJed Brown }
3235b8123daeSJed Brown 
32369be3e283SDebojyoti Ghosh /*@C
32379be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
32389be3e283SDebojyoti Ghosh   called once at the end of each stage.
32399be3e283SDebojyoti Ghosh 
32409be3e283SDebojyoti Ghosh   Logically Collective on TS
32419be3e283SDebojyoti Ghosh 
32429be3e283SDebojyoti Ghosh   Input Parameters:
32439be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
32449be3e283SDebojyoti Ghosh - func - The function
32459be3e283SDebojyoti Ghosh 
32469be3e283SDebojyoti Ghosh   Calling sequence of func:
32479be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
32489be3e283SDebojyoti Ghosh 
32499be3e283SDebojyoti Ghosh   Level: intermediate
32509be3e283SDebojyoti Ghosh 
32519be3e283SDebojyoti Ghosh   Note:
32529be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
325380275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
32549be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
32559be3e283SDebojyoti Ghosh 
32569be3e283SDebojyoti Ghosh .keywords: TS, timestep
32579be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
32589be3e283SDebojyoti Ghosh @*/
32599be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
32609be3e283SDebojyoti Ghosh {
32619be3e283SDebojyoti Ghosh   PetscFunctionBegin;
32629be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
32639be3e283SDebojyoti Ghosh   ts->poststage = func;
32649be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
32659be3e283SDebojyoti Ghosh }
32669be3e283SDebojyoti Ghosh 
3267c688d042SShri Abhyankar /*@C
3268c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3269c688d042SShri Abhyankar   called once at the end of each step evaluation.
3270c688d042SShri Abhyankar 
3271c688d042SShri Abhyankar   Logically Collective on TS
3272c688d042SShri Abhyankar 
3273c688d042SShri Abhyankar   Input Parameters:
3274c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3275c688d042SShri Abhyankar - func - The function
3276c688d042SShri Abhyankar 
3277c688d042SShri Abhyankar   Calling sequence of func:
3278c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3279c688d042SShri Abhyankar 
3280c688d042SShri Abhyankar   Level: intermediate
3281c688d042SShri Abhyankar 
3282c688d042SShri Abhyankar   Note:
32831785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
32841785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3285e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3286e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3287e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3288c688d042SShri Abhyankar 
3289c688d042SShri Abhyankar .keywords: TS, timestep
3290c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3291c688d042SShri Abhyankar @*/
3292c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3293c688d042SShri Abhyankar {
3294c688d042SShri Abhyankar   PetscFunctionBegin;
3295c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3296c688d042SShri Abhyankar   ts->postevaluate = func;
3297c688d042SShri Abhyankar   PetscFunctionReturn(0);
3298c688d042SShri Abhyankar }
3299c688d042SShri Abhyankar 
3300b8123daeSJed Brown /*@
3301b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3302b8123daeSJed Brown 
3303b8123daeSJed Brown   Collective on TS
3304b8123daeSJed Brown 
3305b8123daeSJed Brown   Input Parameters:
3306b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
33079be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3308b8123daeSJed Brown 
3309b8123daeSJed Brown   Notes:
3310b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3311b8123daeSJed Brown   most users would not generally call this routine themselves.
3312b8123daeSJed Brown 
3313b8123daeSJed Brown   Level: developer
3314b8123daeSJed Brown 
3315b8123daeSJed Brown .keywords: TS, timestep
33169be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3317b8123daeSJed Brown @*/
3318b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3319b8123daeSJed Brown {
3320b8123daeSJed Brown   PetscErrorCode ierr;
3321b8123daeSJed Brown 
3322b8123daeSJed Brown   PetscFunctionBegin;
3323b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3324ae60f76fSBarry Smith   if (ts->prestage) {
3325ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3326b8123daeSJed Brown   }
3327b8123daeSJed Brown   PetscFunctionReturn(0);
3328b8123daeSJed Brown }
3329b8123daeSJed Brown 
33309be3e283SDebojyoti Ghosh /*@
33319be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
33329be3e283SDebojyoti Ghosh 
33339be3e283SDebojyoti Ghosh   Collective on TS
33349be3e283SDebojyoti Ghosh 
33359be3e283SDebojyoti Ghosh   Input Parameters:
33369be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
33379be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
33389be3e283SDebojyoti Ghosh   stageindex  - Stage number
33399be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
33409be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
33419be3e283SDebojyoti Ghosh 
33429be3e283SDebojyoti Ghosh   Notes:
33439be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
33449be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
33459be3e283SDebojyoti Ghosh 
33469be3e283SDebojyoti Ghosh   Level: developer
33479be3e283SDebojyoti Ghosh 
33489be3e283SDebojyoti Ghosh .keywords: TS, timestep
33499be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
33509be3e283SDebojyoti Ghosh @*/
33519be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
33529be3e283SDebojyoti Ghosh {
33539be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
33549be3e283SDebojyoti Ghosh 
33559be3e283SDebojyoti Ghosh   PetscFunctionBegin;
33569be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
33574beae5d8SLisandro Dalcin   if (ts->poststage) {
33589be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
33599be3e283SDebojyoti Ghosh   }
33609be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
33619be3e283SDebojyoti Ghosh }
33629be3e283SDebojyoti Ghosh 
3363c688d042SShri Abhyankar /*@
3364c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3365c688d042SShri Abhyankar 
3366c688d042SShri Abhyankar   Collective on TS
3367c688d042SShri Abhyankar 
3368c688d042SShri Abhyankar   Input Parameters:
3369c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3370c688d042SShri Abhyankar 
3371c688d042SShri Abhyankar   Notes:
3372c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3373c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3374c688d042SShri Abhyankar 
3375c688d042SShri Abhyankar   Level: developer
3376c688d042SShri Abhyankar 
3377c688d042SShri Abhyankar .keywords: TS, timestep
3378c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3379c688d042SShri Abhyankar @*/
3380c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3381c688d042SShri Abhyankar {
3382c688d042SShri Abhyankar   PetscErrorCode ierr;
3383c688d042SShri Abhyankar 
3384c688d042SShri Abhyankar   PetscFunctionBegin;
3385c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3386c688d042SShri Abhyankar   if (ts->postevaluate) {
3387dcb233daSLisandro Dalcin     Vec              U;
3388dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3389dcb233daSLisandro Dalcin 
3390dcb233daSLisandro Dalcin     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3391dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3392c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3393dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3394dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3395c688d042SShri Abhyankar   }
3396c688d042SShri Abhyankar   PetscFunctionReturn(0);
3397c688d042SShri Abhyankar }
3398c688d042SShri Abhyankar 
3399ac226902SBarry Smith /*@C
3400000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
34013f2090d5SJed Brown   called once at the end of each time step.
3402000e7ae3SMatthew Knepley 
34033f9fe445SBarry Smith   Logically Collective on TS
3404000e7ae3SMatthew Knepley 
3405000e7ae3SMatthew Knepley   Input Parameters:
3406000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3407000e7ae3SMatthew Knepley - func - The function
3408000e7ae3SMatthew Knepley 
3409000e7ae3SMatthew Knepley   Calling sequence of func:
3410b8123daeSJed Brown $ func (TS ts);
3411000e7ae3SMatthew Knepley 
34121785ff2aSShri Abhyankar   Notes:
34131785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
34141785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
34151785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
34161785ff2aSShri Abhyankar 
3417000e7ae3SMatthew Knepley   Level: intermediate
3418000e7ae3SMatthew Knepley 
3419000e7ae3SMatthew Knepley .keywords: TS, timestep
3420dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3421000e7ae3SMatthew Knepley @*/
34227087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3423000e7ae3SMatthew Knepley {
3424000e7ae3SMatthew Knepley   PetscFunctionBegin;
34250700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3426ae60f76fSBarry Smith   ts->poststep = func;
3427000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3428000e7ae3SMatthew Knepley }
3429000e7ae3SMatthew Knepley 
343009ee8438SJed Brown /*@
34313f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
34323f2090d5SJed Brown 
34333f2090d5SJed Brown   Collective on TS
34343f2090d5SJed Brown 
34353f2090d5SJed Brown   Input Parameters:
34363f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
34373f2090d5SJed Brown 
34383f2090d5SJed Brown   Notes:
34393f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
34403f2090d5SJed Brown   so most users would not generally call this routine themselves.
34413f2090d5SJed Brown 
34423f2090d5SJed Brown   Level: developer
34433f2090d5SJed Brown 
34443f2090d5SJed Brown .keywords: TS, timestep
34453f2090d5SJed Brown @*/
34467087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
34473f2090d5SJed Brown {
34483f2090d5SJed Brown   PetscErrorCode ierr;
34493f2090d5SJed Brown 
34503f2090d5SJed Brown   PetscFunctionBegin;
34510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3452ae60f76fSBarry Smith   if (ts->poststep) {
34535efd42a4SStefano Zampini     Vec              U;
34545efd42a4SStefano Zampini     PetscObjectState sprev,spost;
34555efd42a4SStefano Zampini 
34565efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
34575efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3458ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
34595efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3460dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
346172ac3e02SJed Brown   }
34623f2090d5SJed Brown   PetscFunctionReturn(0);
34633f2090d5SJed Brown }
34643f2090d5SJed Brown 
3465d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3466d763cef2SBarry Smith 
3467d763cef2SBarry Smith /*@C
3468a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3469d763cef2SBarry Smith    timestep to display the iteration's  progress.
3470d763cef2SBarry Smith 
34713f9fe445SBarry Smith    Logically Collective on TS
3472d763cef2SBarry Smith 
3473d763cef2SBarry Smith    Input Parameters:
3474d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3475e213d8f1SJed Brown .  monitor - monitoring routine
3476329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
34770298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3478b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
34790298fd71SBarry Smith           (may be NULL)
3480d763cef2SBarry Smith 
3481e213d8f1SJed Brown    Calling sequence of monitor:
3482dcb233daSLisandro Dalcin $    PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3483d763cef2SBarry Smith 
3484d763cef2SBarry Smith +    ts - the TS context
348563e21af5SBarry 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)
34861f06c33eSBarry Smith .    time - current time
34870910c330SBarry Smith .    u - current iterate
3488d763cef2SBarry Smith -    mctx - [optional] monitoring context
3489d763cef2SBarry Smith 
3490d763cef2SBarry Smith    Notes:
3491d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3492d763cef2SBarry Smith    already has been loaded.
3493d763cef2SBarry Smith 
349495452b02SPatrick Sanan    Fortran Notes:
349595452b02SPatrick Sanan     Only a single monitor function can be set for each TS object
3496025f1a04SBarry Smith 
3497d763cef2SBarry Smith    Level: intermediate
3498d763cef2SBarry Smith 
3499d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3500d763cef2SBarry Smith 
3501a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3502d763cef2SBarry Smith @*/
3503c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3504d763cef2SBarry Smith {
350578064530SBarry Smith   PetscErrorCode ierr;
350678064530SBarry Smith   PetscInt       i;
350778064530SBarry Smith   PetscBool      identical;
350878064530SBarry Smith 
3509d763cef2SBarry Smith   PetscFunctionBegin;
35100700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
351178064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
351278064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
351378064530SBarry Smith     if (identical) PetscFunctionReturn(0);
351478064530SBarry Smith   }
351517186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3516d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
35178704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3518d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3519d763cef2SBarry Smith   PetscFunctionReturn(0);
3520d763cef2SBarry Smith }
3521d763cef2SBarry Smith 
3522d763cef2SBarry Smith /*@C
3523a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3524d763cef2SBarry Smith 
35253f9fe445SBarry Smith    Logically Collective on TS
3526d763cef2SBarry Smith 
3527d763cef2SBarry Smith    Input Parameters:
3528d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3529d763cef2SBarry Smith 
3530d763cef2SBarry Smith    Notes:
3531d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3532d763cef2SBarry Smith 
3533d763cef2SBarry Smith    Level: intermediate
3534d763cef2SBarry Smith 
3535d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3536d763cef2SBarry Smith 
3537a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3538d763cef2SBarry Smith @*/
35397087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3540d763cef2SBarry Smith {
3541d952e501SBarry Smith   PetscErrorCode ierr;
3542d952e501SBarry Smith   PetscInt       i;
3543d952e501SBarry Smith 
3544d763cef2SBarry Smith   PetscFunctionBegin;
35450700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3546d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
35478704b422SBarry Smith     if (ts->monitordestroy[i]) {
35488704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3549d952e501SBarry Smith     }
3550d952e501SBarry Smith   }
3551d763cef2SBarry Smith   ts->numbermonitors = 0;
3552d763cef2SBarry Smith   PetscFunctionReturn(0);
3553d763cef2SBarry Smith }
3554d763cef2SBarry Smith 
3555721cd6eeSBarry Smith /*@C
3556721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
35575516499fSSatish Balay 
35585516499fSSatish Balay    Level: intermediate
355941251cbbSSatish Balay 
35605516499fSSatish Balay .keywords: TS, set, monitor
35615516499fSSatish Balay 
356263e21af5SBarry Smith .seealso:  TSMonitorSet()
356341251cbbSSatish Balay @*/
3564721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3565d763cef2SBarry Smith {
3566dfbe8321SBarry Smith   PetscErrorCode ierr;
3567721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
356841aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3569d132466eSBarry Smith 
3570d763cef2SBarry Smith   PetscFunctionBegin;
35714d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
357241aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
357341aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3574721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
357541aca3d6SBarry Smith   if (iascii) {
3576649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
357763e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
357863e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
357963e21af5SBarry Smith     } else {
35808392e04aSShri 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);
358163e21af5SBarry Smith     }
3582649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
358341aca3d6SBarry Smith   } else if (ibinary) {
358441aca3d6SBarry Smith     PetscMPIInt rank;
358541aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
358641aca3d6SBarry Smith     if (!rank) {
3587450a797fSBarry Smith       PetscBool skipHeader;
3588450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3589450a797fSBarry Smith 
3590450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3591450a797fSBarry Smith       if (!skipHeader) {
3592450a797fSBarry Smith          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
3593450a797fSBarry Smith        }
359441aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
359541aca3d6SBarry Smith     } else {
359641aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
359741aca3d6SBarry Smith     }
359841aca3d6SBarry Smith   }
3599721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3600d763cef2SBarry Smith   PetscFunctionReturn(0);
3601d763cef2SBarry Smith }
3602d763cef2SBarry Smith 
3603cd652676SJed Brown /*@
3604cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3605cd652676SJed Brown 
3606cd652676SJed Brown    Collective on TS
3607cd652676SJed Brown 
3608cd652676SJed Brown    Input Argument:
3609cd652676SJed Brown +  ts - time stepping context
3610cd652676SJed Brown -  t - time to interpolate to
3611cd652676SJed Brown 
3612cd652676SJed Brown    Output Argument:
36130910c330SBarry Smith .  U - state at given time
3614cd652676SJed Brown 
3615cd652676SJed Brown    Level: intermediate
3616cd652676SJed Brown 
3617cd652676SJed Brown    Developer Notes:
3618cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3619cd652676SJed Brown 
3620cd652676SJed Brown .keywords: TS, set
3621cd652676SJed Brown 
3622874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3623cd652676SJed Brown @*/
36240910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3625cd652676SJed Brown {
3626cd652676SJed Brown   PetscErrorCode ierr;
3627cd652676SJed Brown 
3628cd652676SJed Brown   PetscFunctionBegin;
3629cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3630b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3631be5899b3SLisandro 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);
3632ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
36330910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3634cd652676SJed Brown   PetscFunctionReturn(0);
3635cd652676SJed Brown }
3636cd652676SJed Brown 
3637d763cef2SBarry Smith /*@
36386d9e5789SSean Farley    TSStep - Steps one time step
3639d763cef2SBarry Smith 
3640d763cef2SBarry Smith    Collective on TS
3641d763cef2SBarry Smith 
3642d763cef2SBarry Smith    Input Parameter:
3643d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3644d763cef2SBarry Smith 
364527829d71SBarry Smith    Level: developer
3646d763cef2SBarry Smith 
3647b8123daeSJed Brown    Notes:
364827829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
364927829d71SBarry Smith 
3650b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3651b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3652b8123daeSJed Brown 
365319eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
365419eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
365525cb2221SBarry Smith 
3656d763cef2SBarry Smith .keywords: TS, timestep, solve
3657d763cef2SBarry Smith 
36589be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3659d763cef2SBarry Smith @*/
3660193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3661d763cef2SBarry Smith {
3662dfbe8321SBarry Smith   PetscErrorCode   ierr;
3663fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3664be5899b3SLisandro Dalcin   PetscReal        ptime;
3665d763cef2SBarry Smith 
3666d763cef2SBarry Smith   PetscFunctionBegin;
36670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3668fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3669fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3670fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3671fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3672fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3673fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3674302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3675fffbeea8SBarry Smith 
3676d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
367768bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3678d405a339SMatthew Knepley 
3679ef85077eSLisandro 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>");
3680a6772fa2SLisandro 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()");
3681be5899b3SLisandro 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");
3682a6772fa2SLisandro Dalcin 
3683be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3684be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
36852808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3686e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3687d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3688193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3689d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3690be5899b3SLisandro Dalcin   ts->ptime_prev = ptime;
36912808aa04SLisandro Dalcin   ts->steps++;
3692be5899b3SLisandro Dalcin   ts->steprollback = PETSC_FALSE;
369328d5b5d6SLisandro Dalcin   ts->steprestart  = PETSC_FALSE;
3694362cd11cSLisandro Dalcin 
3695362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3696cef5090cSJed Brown     if (ts->errorifstepfailed) {
369708c7845fSBarry 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]);
369808c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3699d2daff3dSHong Zhang     }
370008c7845fSBarry Smith   } else if (!ts->reason) {
370108c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
370208c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
370308c7845fSBarry Smith   }
370408c7845fSBarry Smith   PetscFunctionReturn(0);
370508c7845fSBarry Smith }
370608c7845fSBarry Smith 
370708c7845fSBarry Smith /*@
37087cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
37097cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
37107cbde773SLisandro Dalcin 
37117cbde773SLisandro Dalcin    Collective on TS
37127cbde773SLisandro Dalcin 
37137cbde773SLisandro Dalcin    Input Arguments:
37147cbde773SLisandro Dalcin +  ts - time stepping context
37157cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
37167cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
37177cbde773SLisandro Dalcin 
37187cbde773SLisandro Dalcin    Output Arguments:
37197cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
37207cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
37217cbde773SLisandro Dalcin 
37227cbde773SLisandro Dalcin    Level: advanced
37237cbde773SLisandro Dalcin 
37247cbde773SLisandro Dalcin    Notes:
37257cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
37267cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
37277cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
37287cbde773SLisandro Dalcin 
37297cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
37307cbde773SLisandro Dalcin @*/
37317cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
37327cbde773SLisandro Dalcin {
37337cbde773SLisandro Dalcin   PetscErrorCode ierr;
37347cbde773SLisandro Dalcin 
37357cbde773SLisandro Dalcin   PetscFunctionBegin;
37367cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37377cbde773SLisandro Dalcin   PetscValidType(ts,1);
37387cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
37397cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
37407cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
37417cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
37427cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
37437cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
37447cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
37457cbde773SLisandro Dalcin   PetscFunctionReturn(0);
37467cbde773SLisandro Dalcin }
37477cbde773SLisandro Dalcin 
374805175c85SJed Brown /*@
374905175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
375005175c85SJed Brown 
37511c3436cfSJed Brown    Collective on TS
375205175c85SJed Brown 
375305175c85SJed Brown    Input Arguments:
37541c3436cfSJed Brown +  ts - time stepping context
37551c3436cfSJed Brown .  order - desired order of accuracy
37560298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
375705175c85SJed Brown 
375805175c85SJed Brown    Output Arguments:
37590910c330SBarry Smith .  U - state at the end of the current step
376005175c85SJed Brown 
376105175c85SJed Brown    Level: advanced
376205175c85SJed Brown 
3763108c343cSJed Brown    Notes:
3764108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3765108c343cSJed 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.
3766108c343cSJed Brown 
37671c3436cfSJed Brown .seealso: TSStep(), TSAdapt
376805175c85SJed Brown @*/
37690910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
377005175c85SJed Brown {
377105175c85SJed Brown   PetscErrorCode ierr;
377205175c85SJed Brown 
377305175c85SJed Brown   PetscFunctionBegin;
377405175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
377505175c85SJed Brown   PetscValidType(ts,1);
37760910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3777ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
37780910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
377905175c85SJed Brown   PetscFunctionReturn(0);
378005175c85SJed Brown }
378105175c85SJed Brown 
3782b1cb36f3SHong Zhang /*@
37836a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
37846a4d4014SLisandro Dalcin 
37856a4d4014SLisandro Dalcin    Collective on TS
37866a4d4014SLisandro Dalcin 
37876a4d4014SLisandro Dalcin    Input Parameter:
37886a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
378963e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
379063e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
37915a3a76d0SJed Brown 
37926a4d4014SLisandro Dalcin    Level: beginner
37936a4d4014SLisandro Dalcin 
37945a3a76d0SJed Brown    Notes:
37955a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
37965a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
37975a3a76d0SJed Brown    stepped over the final time.
37985a3a76d0SJed Brown 
37996a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
38006a4d4014SLisandro Dalcin 
380163e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
38026a4d4014SLisandro Dalcin @*/
3803cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
38046a4d4014SLisandro Dalcin {
3805b06615a5SLisandro Dalcin   Vec               solution;
38066a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
3807f22f69f0SBarry Smith 
38086a4d4014SLisandro Dalcin   PetscFunctionBegin;
38090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3810f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
3811feed9e9dSBarry Smith 
3812ee41a567SStefano 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 */
38130910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3814b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3815b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3816b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
38175a3a76d0SJed Brown     }
38180910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3819715f1b00SHong Zhang     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
3820bbd56ea5SKarl Rupp   } else if (u) {
38210910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
38225a3a76d0SJed Brown   }
3823b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
382468bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3825a6772fa2SLisandro Dalcin 
3826ef85077eSLisandro 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>");
3827a6772fa2SLisandro 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()");
3828a6772fa2SLisandro 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");
3829a6772fa2SLisandro Dalcin 
3830715f1b00SHong Zhang   if (ts->forward_solve) {
3831715f1b00SHong Zhang     ierr = TSForwardSetUp(ts);CHKERRQ(ierr);
3832715f1b00SHong Zhang   }
3833715f1b00SHong Zhang 
3834e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
3835715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
3836e7069c78SShri      TSTrajectory to incorrectly save the output files
3837e7069c78SShri   */
3838715f1b00SHong Zhang   /* reset time step and iteration counters */
38392808aa04SLisandro Dalcin   if (!ts->steps) {
38405ef26d82SJed Brown     ts->ksp_its           = 0;
38415ef26d82SJed Brown     ts->snes_its          = 0;
3842c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
3843c610991cSLisandro Dalcin     ts->reject            = 0;
38442808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
38452808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
38462808aa04SLisandro Dalcin   }
384710bc0e4dSStefano 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;
3848193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
3849193ac0bcSJed Brown 
3850ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3851f05ece33SBarry Smith 
3852193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
3853193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
3854a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3855cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3856a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
3857be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
3858db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
3859db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3860e7069c78SShri 
38612808aa04SLisandro Dalcin     if (!ts->steps) {
38622808aa04SLisandro Dalcin       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
38636427ac75SLisandro Dalcin       ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
38642808aa04SLisandro Dalcin     }
38656427ac75SLisandro Dalcin 
3866e1a7a14fSJed Brown     while (!ts->reason) {
38672808aa04SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
38689687d888SLisandro Dalcin       if (!ts->steprollback) {
38699687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
38709687d888SLisandro Dalcin       }
3871193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
3872f3b1f45cSBarry Smith       if (ts->testjacobian) {
3873f3b1f45cSBarry Smith         ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr);
3874f3b1f45cSBarry Smith       }
3875f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
3876f3b1f45cSBarry Smith         ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr);
3877f3b1f45cSBarry Smith       }
3878e783b05fSHong 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. */
3879b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
3880b1cb36f3SHong Zhang       }
388158818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
3882715f1b00SHong Zhang         ierr = TSForwardStep(ts);CHKERRQ(ierr);
3883715f1b00SHong Zhang       }
388410b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
3885e783b05fSHong 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. */
388658818c2dSLisandro Dalcin       if (ts->steprollback) {
388758818c2dSLisandro Dalcin         ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
388858818c2dSLisandro Dalcin       }
3889e783b05fSHong Zhang       if (!ts->steprollback) {
38902808aa04SLisandro Dalcin         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
38911eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
3892aeb4809dSShri Abhyankar       }
3893193ac0bcSJed Brown     }
38942808aa04SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
38956427ac75SLisandro Dalcin 
389649354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
38970910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
3898cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3899b06615a5SLisandro Dalcin       solution = u;
390063e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
39010574a7fbSJed Brown     } else {
3902ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3903cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3904b06615a5SLisandro Dalcin       solution = ts->vec_sol;
39050574a7fbSJed Brown     }
3906193ac0bcSJed Brown   }
3907d2daff3dSHong Zhang 
3908ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
390963e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
391056f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
3911ef222394SBarry Smith   if (ts->adjoint_solve) {
3912ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
39132b0a91c0SBarry Smith   }
39146a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
39156a4d4014SLisandro Dalcin }
39166a4d4014SLisandro Dalcin 
39177db568b7SBarry Smith /*@C
3918228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
3919228d79bcSJed Brown 
3920228d79bcSJed Brown    Collective on TS
3921228d79bcSJed Brown 
3922228d79bcSJed Brown    Input Parameters:
3923228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
3924228d79bcSJed Brown .  step - step number that has just completed
3925228d79bcSJed Brown .  ptime - model time of the state
39260910c330SBarry Smith -  u - state at the current model time
3927228d79bcSJed Brown 
3928228d79bcSJed Brown    Notes:
39297db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
39307db568b7SBarry Smith    Users would almost never call this routine directly.
3931228d79bcSJed Brown 
393263e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
393363e21af5SBarry Smith 
39347db568b7SBarry Smith    Level: developer
3935228d79bcSJed Brown 
3936228d79bcSJed Brown .keywords: TS, timestep
3937228d79bcSJed Brown @*/
39380910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
3939d763cef2SBarry Smith {
3940d6152f81SLisandro Dalcin   DM             dm;
3941a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
3942d6152f81SLisandro Dalcin   PetscErrorCode ierr;
3943d763cef2SBarry Smith 
3944d763cef2SBarry Smith   PetscFunctionBegin;
3945b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3946b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
3947d6152f81SLisandro Dalcin 
3948d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
3949d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
3950d6152f81SLisandro Dalcin 
39515edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
3952d763cef2SBarry Smith   for (i=0; i<n; i++) {
39530910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
3954d763cef2SBarry Smith   }
39555edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
3956d763cef2SBarry Smith   PetscFunctionReturn(0);
3957d763cef2SBarry Smith }
3958d763cef2SBarry Smith 
3959d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
3960d763cef2SBarry Smith /*@C
39617db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
3962a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
3963d763cef2SBarry Smith 
3964d763cef2SBarry Smith    Collective on TS
3965d763cef2SBarry Smith 
3966d763cef2SBarry Smith    Input Parameters:
3967d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
3968d763cef2SBarry Smith .  label - the title to put in the title bar
39697c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
3970a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
3971a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
3972d763cef2SBarry Smith 
3973d763cef2SBarry Smith    Output Parameter:
39740b039ecaSBarry Smith .  ctx - the context
3975d763cef2SBarry Smith 
3976d763cef2SBarry Smith    Options Database Key:
39774f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
39788b668821SLisandro Dalcin +  -ts_monitor_lg_timestep_log - automatically sets line graph monitor
39797db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
39807db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
39817db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
39827db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
3983b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
3984d763cef2SBarry Smith 
3985d763cef2SBarry Smith    Notes:
3986a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
3987d763cef2SBarry Smith 
39887db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
39897db568b7SBarry Smith 
39907db568b7SBarry 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
39917db568b7SBarry 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
39927db568b7SBarry Smith    as the first argument.
39937db568b7SBarry Smith 
39947db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
39957db568b7SBarry Smith 
3996d763cef2SBarry Smith    Level: intermediate
3997d763cef2SBarry Smith 
39987db568b7SBarry Smith .keywords: TS, monitor, line graph, residual
3999d763cef2SBarry Smith 
40007db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
40017db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
40027db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
40037db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
40047db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
40057c922b88SBarry Smith 
4006d763cef2SBarry Smith @*/
4007a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
4008d763cef2SBarry Smith {
40097f52daa2SLisandro Dalcin   PetscDraw      draw;
4010dfbe8321SBarry Smith   PetscErrorCode ierr;
4011d763cef2SBarry Smith 
4012d763cef2SBarry Smith   PetscFunctionBegin;
4013b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
40147f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
40157f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
40167f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
4017287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
40187f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
4019a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
4020d763cef2SBarry Smith   PetscFunctionReturn(0);
4021d763cef2SBarry Smith }
4022d763cef2SBarry Smith 
4023b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4024d763cef2SBarry Smith {
40250b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4026c32365f1SBarry Smith   PetscReal      x   = ptime,y;
4027dfbe8321SBarry Smith   PetscErrorCode ierr;
4028d763cef2SBarry Smith 
4029d763cef2SBarry Smith   PetscFunctionBegin;
403063e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
4031b06615a5SLisandro Dalcin   if (!step) {
4032a9f9c1f6SBarry Smith     PetscDrawAxis axis;
40338b668821SLisandro Dalcin     const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step";
4034a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
40358b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr);
4036a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4037a9f9c1f6SBarry Smith   }
4038c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
40398b668821SLisandro Dalcin   if (ctx->semilogy) y = PetscLog10Real(y);
40400b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4041b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
40420b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
40436934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
40443923b477SBarry Smith   }
4045d763cef2SBarry Smith   PetscFunctionReturn(0);
4046d763cef2SBarry Smith }
4047d763cef2SBarry Smith 
4048d763cef2SBarry Smith /*@C
4049a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4050a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4051d763cef2SBarry Smith 
40520b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4053d763cef2SBarry Smith 
4054d763cef2SBarry Smith    Input Parameter:
40550b039ecaSBarry Smith .  ctx - the monitor context
4056d763cef2SBarry Smith 
4057d763cef2SBarry Smith    Level: intermediate
4058d763cef2SBarry Smith 
4059d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
4060d763cef2SBarry Smith 
40614f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4062d763cef2SBarry Smith @*/
4063a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4064d763cef2SBarry Smith {
4065dfbe8321SBarry Smith   PetscErrorCode ierr;
4066d763cef2SBarry Smith 
4067d763cef2SBarry Smith   PetscFunctionBegin;
40687684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
40697684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
40707684fa3eSBarry Smith   }
40710b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
407231152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4073387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4074387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4075387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
40760b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4077d763cef2SBarry Smith   PetscFunctionReturn(0);
4078d763cef2SBarry Smith }
4079d763cef2SBarry Smith 
4080d763cef2SBarry Smith /*@
4081b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4082d763cef2SBarry Smith 
4083d763cef2SBarry Smith    Not Collective
4084d763cef2SBarry Smith 
4085d763cef2SBarry Smith    Input Parameter:
4086d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4087d763cef2SBarry Smith 
4088d763cef2SBarry Smith    Output Parameter:
408919eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
4090d763cef2SBarry Smith 
4091d763cef2SBarry Smith    Level: beginner
4092d763cef2SBarry Smith 
4093b8123daeSJed Brown    Note:
4094b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
40959be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4096b8123daeSJed Brown 
4097aaa6c58dSLisandro Dalcin .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep()
4098d763cef2SBarry Smith 
4099d763cef2SBarry Smith .keywords: TS, get, time
4100d763cef2SBarry Smith @*/
41017087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4102d763cef2SBarry Smith {
4103d763cef2SBarry Smith   PetscFunctionBegin;
41040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4105f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4106d763cef2SBarry Smith   *t = ts->ptime;
4107d763cef2SBarry Smith   PetscFunctionReturn(0);
4108d763cef2SBarry Smith }
4109d763cef2SBarry Smith 
4110e5e524a1SHong Zhang /*@
4111e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4112e5e524a1SHong Zhang 
4113e5e524a1SHong Zhang    Not Collective
4114e5e524a1SHong Zhang 
4115e5e524a1SHong Zhang    Input Parameter:
4116e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4117e5e524a1SHong Zhang 
4118e5e524a1SHong Zhang    Output Parameter:
4119e5e524a1SHong Zhang .  t  - the previous time
4120e5e524a1SHong Zhang 
4121e5e524a1SHong Zhang    Level: beginner
4122e5e524a1SHong Zhang 
4123aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4124e5e524a1SHong Zhang 
4125e5e524a1SHong Zhang .keywords: TS, get, time
4126e5e524a1SHong Zhang @*/
4127e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4128e5e524a1SHong Zhang {
4129e5e524a1SHong Zhang   PetscFunctionBegin;
4130e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4131e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4132e5e524a1SHong Zhang   *t = ts->ptime_prev;
4133e5e524a1SHong Zhang   PetscFunctionReturn(0);
4134e5e524a1SHong Zhang }
4135e5e524a1SHong Zhang 
41366a4d4014SLisandro Dalcin /*@
41376a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
41386a4d4014SLisandro Dalcin 
41393f9fe445SBarry Smith    Logically Collective on TS
41406a4d4014SLisandro Dalcin 
41416a4d4014SLisandro Dalcin    Input Parameters:
41426a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
41436a4d4014SLisandro Dalcin -  time - the time
41446a4d4014SLisandro Dalcin 
41456a4d4014SLisandro Dalcin    Level: intermediate
41466a4d4014SLisandro Dalcin 
414719eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
41486a4d4014SLisandro Dalcin 
41496a4d4014SLisandro Dalcin .keywords: TS, set, time
41506a4d4014SLisandro Dalcin @*/
41517087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
41526a4d4014SLisandro Dalcin {
41536a4d4014SLisandro Dalcin   PetscFunctionBegin;
41540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4155c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
41566a4d4014SLisandro Dalcin   ts->ptime = t;
41576a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
41586a4d4014SLisandro Dalcin }
41596a4d4014SLisandro Dalcin 
4160d763cef2SBarry Smith /*@C
4161d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4162d763cef2SBarry Smith    TS options in the database.
4163d763cef2SBarry Smith 
41643f9fe445SBarry Smith    Logically Collective on TS
4165d763cef2SBarry Smith 
4166d763cef2SBarry Smith    Input Parameter:
4167d763cef2SBarry Smith +  ts     - The TS context
4168d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4169d763cef2SBarry Smith 
4170d763cef2SBarry Smith    Notes:
4171d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4172d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4173d763cef2SBarry Smith    hyphen.
4174d763cef2SBarry Smith 
4175d763cef2SBarry Smith    Level: advanced
4176d763cef2SBarry Smith 
4177d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
4178d763cef2SBarry Smith 
4179d763cef2SBarry Smith .seealso: TSSetFromOptions()
4180d763cef2SBarry Smith 
4181d763cef2SBarry Smith @*/
41827087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4183d763cef2SBarry Smith {
4184dfbe8321SBarry Smith   PetscErrorCode ierr;
4185089b2837SJed Brown   SNES           snes;
4186d763cef2SBarry Smith 
4187d763cef2SBarry Smith   PetscFunctionBegin;
41880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4189d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4190089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4191089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4192d763cef2SBarry Smith   PetscFunctionReturn(0);
4193d763cef2SBarry Smith }
4194d763cef2SBarry Smith 
4195d763cef2SBarry Smith /*@C
4196d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4197d763cef2SBarry Smith    TS options in the database.
4198d763cef2SBarry Smith 
41993f9fe445SBarry Smith    Logically Collective on TS
4200d763cef2SBarry Smith 
4201d763cef2SBarry Smith    Input Parameter:
4202d763cef2SBarry Smith +  ts     - The TS context
4203d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4204d763cef2SBarry Smith 
4205d763cef2SBarry Smith    Notes:
4206d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4207d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4208d763cef2SBarry Smith    hyphen.
4209d763cef2SBarry Smith 
4210d763cef2SBarry Smith    Level: advanced
4211d763cef2SBarry Smith 
4212d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
4213d763cef2SBarry Smith 
4214d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4215d763cef2SBarry Smith 
4216d763cef2SBarry Smith @*/
42177087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4218d763cef2SBarry Smith {
4219dfbe8321SBarry Smith   PetscErrorCode ierr;
4220089b2837SJed Brown   SNES           snes;
4221d763cef2SBarry Smith 
4222d763cef2SBarry Smith   PetscFunctionBegin;
42230700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4224d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4225089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4226089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4227d763cef2SBarry Smith   PetscFunctionReturn(0);
4228d763cef2SBarry Smith }
4229d763cef2SBarry Smith 
4230d763cef2SBarry Smith /*@C
4231d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4232d763cef2SBarry Smith    TS options in the database.
4233d763cef2SBarry Smith 
4234d763cef2SBarry Smith    Not Collective
4235d763cef2SBarry Smith 
4236d763cef2SBarry Smith    Input Parameter:
4237d763cef2SBarry Smith .  ts - The TS context
4238d763cef2SBarry Smith 
4239d763cef2SBarry Smith    Output Parameter:
4240d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4241d763cef2SBarry Smith 
424295452b02SPatrick Sanan    Notes:
424395452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prifix' of
4244d763cef2SBarry Smith    sufficient length to hold the prefix.
4245d763cef2SBarry Smith 
4246d763cef2SBarry Smith    Level: intermediate
4247d763cef2SBarry Smith 
4248d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
4249d763cef2SBarry Smith 
4250d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4251d763cef2SBarry Smith @*/
42527087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4253d763cef2SBarry Smith {
4254dfbe8321SBarry Smith   PetscErrorCode ierr;
4255d763cef2SBarry Smith 
4256d763cef2SBarry Smith   PetscFunctionBegin;
42570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42584482741eSBarry Smith   PetscValidPointer(prefix,2);
4259d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4260d763cef2SBarry Smith   PetscFunctionReturn(0);
4261d763cef2SBarry Smith }
4262d763cef2SBarry Smith 
4263d763cef2SBarry Smith /*@C
4264d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4265d763cef2SBarry Smith 
4266d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4267d763cef2SBarry Smith 
4268d763cef2SBarry Smith    Input Parameter:
4269d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4270d763cef2SBarry Smith 
4271d763cef2SBarry Smith    Output Parameters:
4272e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4273e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4274e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4275e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4276d763cef2SBarry Smith 
427795452b02SPatrick Sanan    Notes:
427895452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
4279d763cef2SBarry Smith 
4280d763cef2SBarry Smith    Level: intermediate
4281d763cef2SBarry Smith 
428280275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4283d763cef2SBarry Smith 
4284d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
4285d763cef2SBarry Smith @*/
4286e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4287d763cef2SBarry Smith {
4288089b2837SJed Brown   PetscErrorCode ierr;
428924989b8cSPeter Brune   DM             dm;
4290089b2837SJed Brown 
4291d763cef2SBarry Smith   PetscFunctionBegin;
429223a57915SBarry Smith   if (Amat || Pmat) {
429323a57915SBarry Smith     SNES snes;
4294089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
429523a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4296e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
429723a57915SBarry Smith   }
429824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
429924989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4300d763cef2SBarry Smith   PetscFunctionReturn(0);
4301d763cef2SBarry Smith }
4302d763cef2SBarry Smith 
43032eca1d9cSJed Brown /*@C
43042eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
43052eca1d9cSJed Brown 
43062eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
43072eca1d9cSJed Brown 
43082eca1d9cSJed Brown    Input Parameter:
43092eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
43102eca1d9cSJed Brown 
43112eca1d9cSJed Brown    Output Parameters:
4312e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4313e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
43142eca1d9cSJed Brown .  f   - The function to compute the matrices
43152eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
43162eca1d9cSJed Brown 
431795452b02SPatrick Sanan    Notes:
431895452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
43192eca1d9cSJed Brown 
43202eca1d9cSJed Brown    Level: advanced
43212eca1d9cSJed Brown 
432280275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
43232eca1d9cSJed Brown 
43242eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
43252eca1d9cSJed Brown @*/
4326e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
43272eca1d9cSJed Brown {
4328089b2837SJed Brown   PetscErrorCode ierr;
432924989b8cSPeter Brune   DM             dm;
43300910c330SBarry Smith 
43312eca1d9cSJed Brown   PetscFunctionBegin;
4332c0aab802Sstefano_zampini   if (Amat || Pmat) {
4333c0aab802Sstefano_zampini     SNES snes;
4334089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4335f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4336e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4337c0aab802Sstefano_zampini   }
433824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
433924989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
43402eca1d9cSJed Brown   PetscFunctionReturn(0);
43412eca1d9cSJed Brown }
43422eca1d9cSJed Brown 
43431713a123SBarry Smith /*@C
434483a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
43451713a123SBarry Smith    VecView() for the solution at each timestep
43461713a123SBarry Smith 
43471713a123SBarry Smith    Collective on TS
43481713a123SBarry Smith 
43491713a123SBarry Smith    Input Parameters:
43501713a123SBarry Smith +  ts - the TS context
43511713a123SBarry Smith .  step - current time-step
4352142b95e3SSatish Balay .  ptime - current time
43530298fd71SBarry Smith -  dummy - either a viewer or NULL
43541713a123SBarry Smith 
435599fdda47SBarry Smith    Options Database:
435699fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
435799fdda47SBarry Smith 
435895452b02SPatrick Sanan    Notes:
435995452b02SPatrick Sanan     the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
436099fdda47SBarry Smith        will look bad
436199fdda47SBarry Smith 
43621713a123SBarry Smith    Level: intermediate
43631713a123SBarry Smith 
43641713a123SBarry Smith .keywords: TS,  vector, monitor, view
43651713a123SBarry Smith 
4366a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
43671713a123SBarry Smith @*/
43680910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
43691713a123SBarry Smith {
4370dfbe8321SBarry Smith   PetscErrorCode   ierr;
437183a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4372473a3ab2SBarry Smith   PetscDraw        draw;
43731713a123SBarry Smith 
43741713a123SBarry Smith   PetscFunctionBegin;
43756083293cSBarry Smith   if (!step && ictx->showinitial) {
43766083293cSBarry Smith     if (!ictx->initialsolution) {
43770910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
43781713a123SBarry Smith     }
43790910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
43806083293cSBarry Smith   }
4381b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
43820dcf80beSBarry Smith 
43836083293cSBarry Smith   if (ictx->showinitial) {
43846083293cSBarry Smith     PetscReal pause;
43856083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
43866083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
43876083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
43886083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
43896083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
43906083293cSBarry Smith   }
43910910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4392473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
439351fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4394473a3ab2SBarry Smith     char      time[32];
4395473a3ab2SBarry Smith 
4396473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
439785b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4398473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4399473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
440051fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4401473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4402473a3ab2SBarry Smith   }
4403473a3ab2SBarry Smith 
44046083293cSBarry Smith   if (ictx->showinitial) {
44056083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
44066083293cSBarry Smith   }
44071713a123SBarry Smith   PetscFunctionReturn(0);
44081713a123SBarry Smith }
44091713a123SBarry Smith 
44109110b2e7SHong Zhang /*@C
44112d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
44122d5ee99bSBarry Smith 
44132d5ee99bSBarry Smith    Collective on TS
44142d5ee99bSBarry Smith 
44152d5ee99bSBarry Smith    Input Parameters:
44162d5ee99bSBarry Smith +  ts - the TS context
44172d5ee99bSBarry Smith .  step - current time-step
44182d5ee99bSBarry Smith .  ptime - current time
44192d5ee99bSBarry Smith -  dummy - either a viewer or NULL
44202d5ee99bSBarry Smith 
44212d5ee99bSBarry Smith    Level: intermediate
44222d5ee99bSBarry Smith 
44232d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
44242d5ee99bSBarry Smith 
44252d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
44262d5ee99bSBarry Smith @*/
44272d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
44282d5ee99bSBarry Smith {
44292d5ee99bSBarry Smith   PetscErrorCode    ierr;
44302d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
44312d5ee99bSBarry Smith   PetscDraw         draw;
44326934998bSLisandro Dalcin   PetscDrawAxis     axis;
44332d5ee99bSBarry Smith   PetscInt          n;
44342d5ee99bSBarry Smith   PetscMPIInt       size;
44356934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
44362d5ee99bSBarry Smith   char              time[32];
44372d5ee99bSBarry Smith   const PetscScalar *U;
44382d5ee99bSBarry Smith 
44392d5ee99bSBarry Smith   PetscFunctionBegin;
44406934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
44416934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
44422d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
44436934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
44442d5ee99bSBarry Smith 
44452d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
44466934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
44476934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
44486934998bSLisandro Dalcin   if (!step) {
44496934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
44506934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
44516934998bSLisandro Dalcin   }
44522d5ee99bSBarry Smith 
44532d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
44546934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
44556934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4456a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
44576934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
44582d5ee99bSBarry Smith 
44596934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
44606934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
44612d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
44624b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
446385b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
44642d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
446551fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
44662d5ee99bSBarry Smith   }
44676934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
44682d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
446956d62c8fSLisandro Dalcin   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
44706934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
44712d5ee99bSBarry Smith   PetscFunctionReturn(0);
44722d5ee99bSBarry Smith }
44732d5ee99bSBarry Smith 
44746083293cSBarry Smith /*@C
447583a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
44766083293cSBarry Smith 
44776083293cSBarry Smith    Collective on TS
44786083293cSBarry Smith 
44796083293cSBarry Smith    Input Parameters:
44806083293cSBarry Smith .    ctx - the monitor context
44816083293cSBarry Smith 
44826083293cSBarry Smith    Level: intermediate
44836083293cSBarry Smith 
44846083293cSBarry Smith .keywords: TS,  vector, monitor, view
44856083293cSBarry Smith 
448683a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
44876083293cSBarry Smith @*/
448883a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
44896083293cSBarry Smith {
44906083293cSBarry Smith   PetscErrorCode ierr;
44916083293cSBarry Smith 
44926083293cSBarry Smith   PetscFunctionBegin;
449383a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
449483a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
449583a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
44966083293cSBarry Smith   PetscFunctionReturn(0);
44976083293cSBarry Smith }
44986083293cSBarry Smith 
44996083293cSBarry Smith /*@C
450083a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
45016083293cSBarry Smith 
45026083293cSBarry Smith    Collective on TS
45036083293cSBarry Smith 
45046083293cSBarry Smith    Input Parameter:
45056083293cSBarry Smith .    ts - time-step context
45066083293cSBarry Smith 
45076083293cSBarry Smith    Output Patameter:
45086083293cSBarry Smith .    ctx - the monitor context
45096083293cSBarry Smith 
451099fdda47SBarry Smith    Options Database:
451199fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
451299fdda47SBarry Smith 
45136083293cSBarry Smith    Level: intermediate
45146083293cSBarry Smith 
45156083293cSBarry Smith .keywords: TS,  vector, monitor, view
45166083293cSBarry Smith 
451783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
45186083293cSBarry Smith @*/
451983a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
45206083293cSBarry Smith {
45216083293cSBarry Smith   PetscErrorCode   ierr;
45226083293cSBarry Smith 
45236083293cSBarry Smith   PetscFunctionBegin;
4524b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
452583a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4526e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4527bbd56ea5SKarl Rupp 
452883a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4529473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4530c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4531473a3ab2SBarry Smith 
4532473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4533c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
45346083293cSBarry Smith   PetscFunctionReturn(0);
45356083293cSBarry Smith }
45366083293cSBarry Smith 
45373a471f94SBarry Smith /*@C
45380ed3bfb6SBarry Smith    TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling
45390ed3bfb6SBarry Smith    VecView() for the solution provided by TSSetSolutionFunction() at each timestep
45400ed3bfb6SBarry Smith 
45410ed3bfb6SBarry Smith    Collective on TS
45420ed3bfb6SBarry Smith 
45430ed3bfb6SBarry Smith    Input Parameters:
45440ed3bfb6SBarry Smith +  ts - the TS context
45450ed3bfb6SBarry Smith .  step - current time-step
45460ed3bfb6SBarry Smith .  ptime - current time
45470ed3bfb6SBarry Smith -  dummy - either a viewer or NULL
45480ed3bfb6SBarry Smith 
45490ed3bfb6SBarry Smith    Options Database:
45500ed3bfb6SBarry Smith .  -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
45510ed3bfb6SBarry Smith 
45520ed3bfb6SBarry Smith    Level: intermediate
45530ed3bfb6SBarry Smith 
45540ed3bfb6SBarry Smith .keywords: TS,  vector, monitor, view
45550ed3bfb6SBarry Smith 
45560ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
45570ed3bfb6SBarry Smith @*/
45580ed3bfb6SBarry Smith PetscErrorCode  TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
45590ed3bfb6SBarry Smith {
45600ed3bfb6SBarry Smith   PetscErrorCode   ierr;
45610ed3bfb6SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
45620ed3bfb6SBarry Smith   PetscViewer      viewer = ctx->viewer;
45630ed3bfb6SBarry Smith   Vec              work;
45640ed3bfb6SBarry Smith 
45650ed3bfb6SBarry Smith   PetscFunctionBegin;
45660ed3bfb6SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
45670ed3bfb6SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
45680ed3bfb6SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
45690ed3bfb6SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
45700ed3bfb6SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
45710ed3bfb6SBarry Smith   PetscFunctionReturn(0);
45720ed3bfb6SBarry Smith }
45730ed3bfb6SBarry Smith 
45740ed3bfb6SBarry Smith /*@C
457583a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
45763a471f94SBarry Smith    VecView() for the error at each timestep
45773a471f94SBarry Smith 
45783a471f94SBarry Smith    Collective on TS
45793a471f94SBarry Smith 
45803a471f94SBarry Smith    Input Parameters:
45813a471f94SBarry Smith +  ts - the TS context
45823a471f94SBarry Smith .  step - current time-step
45833a471f94SBarry Smith .  ptime - current time
45840298fd71SBarry Smith -  dummy - either a viewer or NULL
45853a471f94SBarry Smith 
45860ed3bfb6SBarry Smith    Options Database:
45870ed3bfb6SBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
45880ed3bfb6SBarry Smith 
45893a471f94SBarry Smith    Level: intermediate
45903a471f94SBarry Smith 
45913a471f94SBarry Smith .keywords: TS,  vector, monitor, view
45923a471f94SBarry Smith 
45930ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
45943a471f94SBarry Smith @*/
45950910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
45963a471f94SBarry Smith {
45973a471f94SBarry Smith   PetscErrorCode   ierr;
459883a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
459983a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
46003a471f94SBarry Smith   Vec              work;
46013a471f94SBarry Smith 
46023a471f94SBarry Smith   PetscFunctionBegin;
4603b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
46040910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
46053a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
46060910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
46073a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
46083a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
46093a471f94SBarry Smith   PetscFunctionReturn(0);
46103a471f94SBarry Smith }
46113a471f94SBarry Smith 
4612af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
46136c699258SBarry Smith /*@
46142a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
46156c699258SBarry Smith 
46163f9fe445SBarry Smith    Logically Collective on TS and DM
46176c699258SBarry Smith 
46186c699258SBarry Smith    Input Parameters:
46192a808120SBarry Smith +  ts - the ODE integrator object
46202a808120SBarry Smith -  dm - the dm, cannot be NULL
46216c699258SBarry Smith 
46226c699258SBarry Smith    Level: intermediate
46236c699258SBarry Smith 
46246c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
46256c699258SBarry Smith @*/
46267087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
46276c699258SBarry Smith {
46286c699258SBarry Smith   PetscErrorCode ierr;
4629089b2837SJed Brown   SNES           snes;
4630942e3340SBarry Smith   DMTS           tsdm;
46316c699258SBarry Smith 
46326c699258SBarry Smith   PetscFunctionBegin;
46330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
46342a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
463570663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4636942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
46372a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4638942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4639942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
464024989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
464124989b8cSPeter Brune         tsdm->originaldm = dm;
464224989b8cSPeter Brune       }
464324989b8cSPeter Brune     }
46446bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
464524989b8cSPeter Brune   }
46466c699258SBarry Smith   ts->dm = dm;
4647bbd56ea5SKarl Rupp 
4648089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4649089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
46506c699258SBarry Smith   PetscFunctionReturn(0);
46516c699258SBarry Smith }
46526c699258SBarry Smith 
46536c699258SBarry Smith /*@
46546c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
46556c699258SBarry Smith 
46563f9fe445SBarry Smith    Not Collective
46576c699258SBarry Smith 
46586c699258SBarry Smith    Input Parameter:
46596c699258SBarry Smith . ts - the preconditioner context
46606c699258SBarry Smith 
46616c699258SBarry Smith    Output Parameter:
46626c699258SBarry Smith .  dm - the dm
46636c699258SBarry Smith 
46646c699258SBarry Smith    Level: intermediate
46656c699258SBarry Smith 
46666c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
46676c699258SBarry Smith @*/
46687087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
46696c699258SBarry Smith {
4670496e6a7aSJed Brown   PetscErrorCode ierr;
4671496e6a7aSJed Brown 
46726c699258SBarry Smith   PetscFunctionBegin;
46730700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4674496e6a7aSJed Brown   if (!ts->dm) {
4675ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4676496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4677496e6a7aSJed Brown   }
46786c699258SBarry Smith   *dm = ts->dm;
46796c699258SBarry Smith   PetscFunctionReturn(0);
46806c699258SBarry Smith }
46811713a123SBarry Smith 
46820f5c6efeSJed Brown /*@
46830f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
46840f5c6efeSJed Brown 
46853f9fe445SBarry Smith    Logically Collective on SNES
46860f5c6efeSJed Brown 
46870f5c6efeSJed Brown    Input Parameter:
4688d42a1c89SJed Brown + snes - nonlinear solver
46890910c330SBarry Smith . U - the current state at which to evaluate the residual
4690d42a1c89SJed Brown - ctx - user context, must be a TS
46910f5c6efeSJed Brown 
46920f5c6efeSJed Brown    Output Parameter:
46930f5c6efeSJed Brown . F - the nonlinear residual
46940f5c6efeSJed Brown 
46950f5c6efeSJed Brown    Notes:
46960f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
46970f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
46980f5c6efeSJed Brown 
46990f5c6efeSJed Brown    Level: advanced
47000f5c6efeSJed Brown 
47010f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
47020f5c6efeSJed Brown @*/
47030910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
47040f5c6efeSJed Brown {
47050f5c6efeSJed Brown   TS             ts = (TS)ctx;
47060f5c6efeSJed Brown   PetscErrorCode ierr;
47070f5c6efeSJed Brown 
47080f5c6efeSJed Brown   PetscFunctionBegin;
47090f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
47100910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
47110f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
47120f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
47130910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
47140f5c6efeSJed Brown   PetscFunctionReturn(0);
47150f5c6efeSJed Brown }
47160f5c6efeSJed Brown 
47170f5c6efeSJed Brown /*@
47180f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
47190f5c6efeSJed Brown 
47200f5c6efeSJed Brown    Collective on SNES
47210f5c6efeSJed Brown 
47220f5c6efeSJed Brown    Input Parameter:
47230f5c6efeSJed Brown + snes - nonlinear solver
47240910c330SBarry Smith . U - the current state at which to evaluate the residual
47250f5c6efeSJed Brown - ctx - user context, must be a TS
47260f5c6efeSJed Brown 
47270f5c6efeSJed Brown    Output Parameter:
47280f5c6efeSJed Brown + A - the Jacobian
47290f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
47300f5c6efeSJed Brown - flag - indicates any structure change in the matrix
47310f5c6efeSJed Brown 
47320f5c6efeSJed Brown    Notes:
47330f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
47340f5c6efeSJed Brown 
47350f5c6efeSJed Brown    Level: developer
47360f5c6efeSJed Brown 
47370f5c6efeSJed Brown .seealso: SNESSetJacobian()
47380f5c6efeSJed Brown @*/
4739d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
47400f5c6efeSJed Brown {
47410f5c6efeSJed Brown   TS             ts = (TS)ctx;
47420f5c6efeSJed Brown   PetscErrorCode ierr;
47430f5c6efeSJed Brown 
47440f5c6efeSJed Brown   PetscFunctionBegin;
47450f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
47460910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
47470f5c6efeSJed Brown   PetscValidPointer(A,3);
474894ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
47490f5c6efeSJed Brown   PetscValidPointer(B,4);
475094ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
47510f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4752d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
47530f5c6efeSJed Brown   PetscFunctionReturn(0);
47540f5c6efeSJed Brown }
4755325fc9f4SBarry Smith 
47560e4ef248SJed Brown /*@C
47579ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
47580e4ef248SJed Brown 
47590e4ef248SJed Brown    Collective on TS
47600e4ef248SJed Brown 
47610e4ef248SJed Brown    Input Arguments:
47620e4ef248SJed Brown +  ts - time stepping context
47630e4ef248SJed Brown .  t - time at which to evaluate
47640910c330SBarry Smith .  U - state at which to evaluate
47650e4ef248SJed Brown -  ctx - context
47660e4ef248SJed Brown 
47670e4ef248SJed Brown    Output Arguments:
47680e4ef248SJed Brown .  F - right hand side
47690e4ef248SJed Brown 
47700e4ef248SJed Brown    Level: intermediate
47710e4ef248SJed Brown 
47720e4ef248SJed Brown    Notes:
47730e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
47740e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
47750e4ef248SJed Brown 
47760e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
47770e4ef248SJed Brown @*/
47780910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
47790e4ef248SJed Brown {
47800e4ef248SJed Brown   PetscErrorCode ierr;
47810e4ef248SJed Brown   Mat            Arhs,Brhs;
47820e4ef248SJed Brown 
47830e4ef248SJed Brown   PetscFunctionBegin;
47840e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
4785d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
47860910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
47870e4ef248SJed Brown   PetscFunctionReturn(0);
47880e4ef248SJed Brown }
47890e4ef248SJed Brown 
47900e4ef248SJed Brown /*@C
47910e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
47920e4ef248SJed Brown 
47930e4ef248SJed Brown    Collective on TS
47940e4ef248SJed Brown 
47950e4ef248SJed Brown    Input Arguments:
47960e4ef248SJed Brown +  ts - time stepping context
47970e4ef248SJed Brown .  t - time at which to evaluate
47980910c330SBarry Smith .  U - state at which to evaluate
47990e4ef248SJed Brown -  ctx - context
48000e4ef248SJed Brown 
48010e4ef248SJed Brown    Output Arguments:
48020e4ef248SJed Brown +  A - pointer to operator
48030e4ef248SJed Brown .  B - pointer to preconditioning matrix
48040e4ef248SJed Brown -  flg - matrix structure flag
48050e4ef248SJed Brown 
48060e4ef248SJed Brown    Level: intermediate
48070e4ef248SJed Brown 
48080e4ef248SJed Brown    Notes:
48090e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
48100e4ef248SJed Brown 
48110e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
48120e4ef248SJed Brown @*/
4813d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
48140e4ef248SJed Brown {
48150e4ef248SJed Brown   PetscFunctionBegin;
48160e4ef248SJed Brown   PetscFunctionReturn(0);
48170e4ef248SJed Brown }
48180e4ef248SJed Brown 
48190026cea9SSean Farley /*@C
48200026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
48210026cea9SSean Farley 
48220026cea9SSean Farley    Collective on TS
48230026cea9SSean Farley 
48240026cea9SSean Farley    Input Arguments:
48250026cea9SSean Farley +  ts - time stepping context
48260026cea9SSean Farley .  t - time at which to evaluate
48270910c330SBarry Smith .  U - state at which to evaluate
48280910c330SBarry Smith .  Udot - time derivative of state vector
48290026cea9SSean Farley -  ctx - context
48300026cea9SSean Farley 
48310026cea9SSean Farley    Output Arguments:
48320026cea9SSean Farley .  F - left hand side
48330026cea9SSean Farley 
48340026cea9SSean Farley    Level: intermediate
48350026cea9SSean Farley 
48360026cea9SSean Farley    Notes:
48370910c330SBarry 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
48380026cea9SSean Farley    user is required to write their own TSComputeIFunction.
48390026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
48400026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
48410026cea9SSean Farley 
48429ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
48439ae8fd06SBarry Smith 
48449ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
48450026cea9SSean Farley @*/
48460910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
48470026cea9SSean Farley {
48480026cea9SSean Farley   PetscErrorCode ierr;
48490026cea9SSean Farley   Mat            A,B;
48500026cea9SSean Farley 
48510026cea9SSean Farley   PetscFunctionBegin;
48520298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
4853d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
48540910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
48550026cea9SSean Farley   PetscFunctionReturn(0);
48560026cea9SSean Farley }
48570026cea9SSean Farley 
48580026cea9SSean Farley /*@C
4859b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
48600026cea9SSean Farley 
48610026cea9SSean Farley    Collective on TS
48620026cea9SSean Farley 
48630026cea9SSean Farley    Input Arguments:
48640026cea9SSean Farley +  ts - time stepping context
48650026cea9SSean Farley .  t - time at which to evaluate
48660910c330SBarry Smith .  U - state at which to evaluate
48670910c330SBarry Smith .  Udot - time derivative of state vector
48680026cea9SSean Farley .  shift - shift to apply
48690026cea9SSean Farley -  ctx - context
48700026cea9SSean Farley 
48710026cea9SSean Farley    Output Arguments:
48720026cea9SSean Farley +  A - pointer to operator
48730026cea9SSean Farley .  B - pointer to preconditioning matrix
48740026cea9SSean Farley -  flg - matrix structure flag
48750026cea9SSean Farley 
4876b41af12eSJed Brown    Level: advanced
48770026cea9SSean Farley 
48780026cea9SSean Farley    Notes:
48790026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
48800026cea9SSean Farley 
4881b41af12eSJed Brown    It is only appropriate for problems of the form
4882b41af12eSJed Brown 
4883b41af12eSJed Brown $     M Udot = F(U,t)
4884b41af12eSJed Brown 
4885b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4886b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4887b41af12eSJed Brown   an implicit operator of the form
4888b41af12eSJed Brown 
4889b41af12eSJed Brown $    shift*M + J
4890b41af12eSJed Brown 
4891b41af12eSJed 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
4892b41af12eSJed Brown   a copy of M or reassemble it when requested.
4893b41af12eSJed Brown 
48940026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
48950026cea9SSean Farley @*/
4896d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
48970026cea9SSean Farley {
4898b41af12eSJed Brown   PetscErrorCode ierr;
4899b41af12eSJed Brown 
49000026cea9SSean Farley   PetscFunctionBegin;
490194ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4902b41af12eSJed Brown   ts->ijacobian.shift = shift;
49030026cea9SSean Farley   PetscFunctionReturn(0);
49040026cea9SSean Farley }
4905b41af12eSJed Brown 
4906e817cc15SEmil Constantinescu /*@
4907e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4908e817cc15SEmil Constantinescu 
4909e817cc15SEmil Constantinescu    Not Collective
4910e817cc15SEmil Constantinescu 
4911e817cc15SEmil Constantinescu    Input Parameter:
4912e817cc15SEmil Constantinescu .  ts - the TS context
4913e817cc15SEmil Constantinescu 
4914e817cc15SEmil Constantinescu    Output Parameter:
49154e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4916e817cc15SEmil Constantinescu 
4917e817cc15SEmil Constantinescu    Level: beginner
4918e817cc15SEmil Constantinescu 
4919e817cc15SEmil Constantinescu .keywords: TS, equation type
4920e817cc15SEmil Constantinescu 
4921e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4922e817cc15SEmil Constantinescu @*/
4923e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4924e817cc15SEmil Constantinescu {
4925e817cc15SEmil Constantinescu   PetscFunctionBegin;
4926e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4927e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4928e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4929e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4930e817cc15SEmil Constantinescu }
4931e817cc15SEmil Constantinescu 
4932e817cc15SEmil Constantinescu /*@
4933e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4934e817cc15SEmil Constantinescu 
4935e817cc15SEmil Constantinescu    Not Collective
4936e817cc15SEmil Constantinescu 
4937e817cc15SEmil Constantinescu    Input Parameter:
4938e817cc15SEmil Constantinescu +  ts - the TS context
49391297b224SEmil Constantinescu -  equation_type - see TSEquationType
4940e817cc15SEmil Constantinescu 
4941e817cc15SEmil Constantinescu    Level: advanced
4942e817cc15SEmil Constantinescu 
4943e817cc15SEmil Constantinescu .keywords: TS, equation type
4944e817cc15SEmil Constantinescu 
4945e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4946e817cc15SEmil Constantinescu @*/
4947e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4948e817cc15SEmil Constantinescu {
4949e817cc15SEmil Constantinescu   PetscFunctionBegin;
4950e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4951e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4952e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4953e817cc15SEmil Constantinescu }
49540026cea9SSean Farley 
49554af1b03aSJed Brown /*@
49564af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
49574af1b03aSJed Brown 
49584af1b03aSJed Brown    Not Collective
49594af1b03aSJed Brown 
49604af1b03aSJed Brown    Input Parameter:
49614af1b03aSJed Brown .  ts - the TS context
49624af1b03aSJed Brown 
49634af1b03aSJed Brown    Output Parameter:
49644af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
49654af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
49664af1b03aSJed Brown 
4967487e0bb9SJed Brown    Level: beginner
49684af1b03aSJed Brown 
4969cd652676SJed Brown    Notes:
4970cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
49714af1b03aSJed Brown 
49724af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
49734af1b03aSJed Brown 
49744af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
49754af1b03aSJed Brown @*/
49764af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
49774af1b03aSJed Brown {
49784af1b03aSJed Brown   PetscFunctionBegin;
49794af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49804af1b03aSJed Brown   PetscValidPointer(reason,2);
49814af1b03aSJed Brown   *reason = ts->reason;
49824af1b03aSJed Brown   PetscFunctionReturn(0);
49834af1b03aSJed Brown }
49844af1b03aSJed Brown 
4985d6ad946cSShri Abhyankar /*@
4986d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
4987d6ad946cSShri Abhyankar 
4988d6ad946cSShri Abhyankar    Not Collective
4989d6ad946cSShri Abhyankar 
4990d6ad946cSShri Abhyankar    Input Parameter:
4991d6ad946cSShri Abhyankar +  ts - the TS context
4992d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
4993d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4994d6ad946cSShri Abhyankar 
4995f5abba47SShri Abhyankar    Level: advanced
4996d6ad946cSShri Abhyankar 
4997d6ad946cSShri Abhyankar    Notes:
4998d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
4999d6ad946cSShri Abhyankar 
5000d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
5001d6ad946cSShri Abhyankar 
5002d6ad946cSShri Abhyankar .seealso: TSConvergedReason
5003d6ad946cSShri Abhyankar @*/
5004d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5005d6ad946cSShri Abhyankar {
5006d6ad946cSShri Abhyankar   PetscFunctionBegin;
5007d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5008d6ad946cSShri Abhyankar   ts->reason = reason;
5009d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
5010d6ad946cSShri Abhyankar }
5011d6ad946cSShri Abhyankar 
5012cc708dedSBarry Smith /*@
5013cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
5014cc708dedSBarry Smith 
5015cc708dedSBarry Smith    Not Collective
5016cc708dedSBarry Smith 
5017cc708dedSBarry Smith    Input Parameter:
5018cc708dedSBarry Smith .  ts - the TS context
5019cc708dedSBarry Smith 
5020cc708dedSBarry Smith    Output Parameter:
502119eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
5022cc708dedSBarry Smith 
5023487e0bb9SJed Brown    Level: beginner
5024cc708dedSBarry Smith 
5025cc708dedSBarry Smith    Notes:
5026cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
5027cc708dedSBarry Smith 
5028cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
5029cc708dedSBarry Smith 
5030cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
5031cc708dedSBarry Smith @*/
5032cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5033cc708dedSBarry Smith {
5034cc708dedSBarry Smith   PetscFunctionBegin;
5035cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5036cc708dedSBarry Smith   PetscValidPointer(ftime,2);
5037cc708dedSBarry Smith   *ftime = ts->solvetime;
5038cc708dedSBarry Smith   PetscFunctionReturn(0);
5039cc708dedSBarry Smith }
5040cc708dedSBarry Smith 
50412c18e0fdSBarry Smith /*@
50425ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
50439f67acb7SJed Brown    used by the time integrator.
50449f67acb7SJed Brown 
50459f67acb7SJed Brown    Not Collective
50469f67acb7SJed Brown 
50479f67acb7SJed Brown    Input Parameter:
50489f67acb7SJed Brown .  ts - TS context
50499f67acb7SJed Brown 
50509f67acb7SJed Brown    Output Parameter:
50519f67acb7SJed Brown .  nits - number of nonlinear iterations
50529f67acb7SJed Brown 
50539f67acb7SJed Brown    Notes:
50549f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
50559f67acb7SJed Brown 
50569f67acb7SJed Brown    Level: intermediate
50579f67acb7SJed Brown 
50589f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
50599f67acb7SJed Brown 
50605ef26d82SJed Brown .seealso:  TSGetKSPIterations()
50619f67acb7SJed Brown @*/
50625ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
50639f67acb7SJed Brown {
50649f67acb7SJed Brown   PetscFunctionBegin;
50659f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
50669f67acb7SJed Brown   PetscValidIntPointer(nits,2);
50675ef26d82SJed Brown   *nits = ts->snes_its;
50689f67acb7SJed Brown   PetscFunctionReturn(0);
50699f67acb7SJed Brown }
50709f67acb7SJed Brown 
50719f67acb7SJed Brown /*@
50725ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
50739f67acb7SJed Brown    used by the time integrator.
50749f67acb7SJed Brown 
50759f67acb7SJed Brown    Not Collective
50769f67acb7SJed Brown 
50779f67acb7SJed Brown    Input Parameter:
50789f67acb7SJed Brown .  ts - TS context
50799f67acb7SJed Brown 
50809f67acb7SJed Brown    Output Parameter:
50819f67acb7SJed Brown .  lits - number of linear iterations
50829f67acb7SJed Brown 
50839f67acb7SJed Brown    Notes:
50849f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
50859f67acb7SJed Brown 
50869f67acb7SJed Brown    Level: intermediate
50879f67acb7SJed Brown 
50889f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
50899f67acb7SJed Brown 
50905ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
50919f67acb7SJed Brown @*/
50925ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
50939f67acb7SJed Brown {
50949f67acb7SJed Brown   PetscFunctionBegin;
50959f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
50969f67acb7SJed Brown   PetscValidIntPointer(lits,2);
50975ef26d82SJed Brown   *lits = ts->ksp_its;
50989f67acb7SJed Brown   PetscFunctionReturn(0);
50999f67acb7SJed Brown }
51009f67acb7SJed Brown 
5101cef5090cSJed Brown /*@
5102cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5103cef5090cSJed Brown 
5104cef5090cSJed Brown    Not Collective
5105cef5090cSJed Brown 
5106cef5090cSJed Brown    Input Parameter:
5107cef5090cSJed Brown .  ts - TS context
5108cef5090cSJed Brown 
5109cef5090cSJed Brown    Output Parameter:
5110cef5090cSJed Brown .  rejects - number of steps rejected
5111cef5090cSJed Brown 
5112cef5090cSJed Brown    Notes:
5113cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5114cef5090cSJed Brown 
5115cef5090cSJed Brown    Level: intermediate
5116cef5090cSJed Brown 
5117cef5090cSJed Brown .keywords: TS, get, number
5118cef5090cSJed Brown 
51195ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5120cef5090cSJed Brown @*/
5121cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5122cef5090cSJed Brown {
5123cef5090cSJed Brown   PetscFunctionBegin;
5124cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5125cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5126cef5090cSJed Brown   *rejects = ts->reject;
5127cef5090cSJed Brown   PetscFunctionReturn(0);
5128cef5090cSJed Brown }
5129cef5090cSJed Brown 
5130cef5090cSJed Brown /*@
5131cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5132cef5090cSJed Brown 
5133cef5090cSJed Brown    Not Collective
5134cef5090cSJed Brown 
5135cef5090cSJed Brown    Input Parameter:
5136cef5090cSJed Brown .  ts - TS context
5137cef5090cSJed Brown 
5138cef5090cSJed Brown    Output Parameter:
5139cef5090cSJed Brown .  fails - number of failed nonlinear solves
5140cef5090cSJed Brown 
5141cef5090cSJed Brown    Notes:
5142cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5143cef5090cSJed Brown 
5144cef5090cSJed Brown    Level: intermediate
5145cef5090cSJed Brown 
5146cef5090cSJed Brown .keywords: TS, get, number
5147cef5090cSJed Brown 
51485ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5149cef5090cSJed Brown @*/
5150cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5151cef5090cSJed Brown {
5152cef5090cSJed Brown   PetscFunctionBegin;
5153cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5154cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5155cef5090cSJed Brown   *fails = ts->num_snes_failures;
5156cef5090cSJed Brown   PetscFunctionReturn(0);
5157cef5090cSJed Brown }
5158cef5090cSJed Brown 
5159cef5090cSJed Brown /*@
5160cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5161cef5090cSJed Brown 
5162cef5090cSJed Brown    Not Collective
5163cef5090cSJed Brown 
5164cef5090cSJed Brown    Input Parameter:
5165cef5090cSJed Brown +  ts - TS context
5166cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5167cef5090cSJed Brown 
5168cef5090cSJed Brown    Notes:
5169cef5090cSJed Brown    The counter is reset to zero for each step
5170cef5090cSJed Brown 
5171cef5090cSJed Brown    Options Database Key:
5172cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5173cef5090cSJed Brown 
5174cef5090cSJed Brown    Level: intermediate
5175cef5090cSJed Brown 
5176cef5090cSJed Brown .keywords: TS, set, maximum, number
5177cef5090cSJed Brown 
51785ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5179cef5090cSJed Brown @*/
5180cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5181cef5090cSJed Brown {
5182cef5090cSJed Brown   PetscFunctionBegin;
5183cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5184cef5090cSJed Brown   ts->max_reject = rejects;
5185cef5090cSJed Brown   PetscFunctionReturn(0);
5186cef5090cSJed Brown }
5187cef5090cSJed Brown 
5188cef5090cSJed Brown /*@
5189cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5190cef5090cSJed Brown 
5191cef5090cSJed Brown    Not Collective
5192cef5090cSJed Brown 
5193cef5090cSJed Brown    Input Parameter:
5194cef5090cSJed Brown +  ts - TS context
5195cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5196cef5090cSJed Brown 
5197cef5090cSJed Brown    Notes:
5198cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5199cef5090cSJed Brown 
5200cef5090cSJed Brown    Options Database Key:
5201cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5202cef5090cSJed Brown 
5203cef5090cSJed Brown    Level: intermediate
5204cef5090cSJed Brown 
5205cef5090cSJed Brown .keywords: TS, set, maximum, number
5206cef5090cSJed Brown 
52075ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5208cef5090cSJed Brown @*/
5209cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5210cef5090cSJed Brown {
5211cef5090cSJed Brown   PetscFunctionBegin;
5212cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5213cef5090cSJed Brown   ts->max_snes_failures = fails;
5214cef5090cSJed Brown   PetscFunctionReturn(0);
5215cef5090cSJed Brown }
5216cef5090cSJed Brown 
5217cef5090cSJed Brown /*@
5218cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5219cef5090cSJed Brown 
5220cef5090cSJed Brown    Not Collective
5221cef5090cSJed Brown 
5222cef5090cSJed Brown    Input Parameter:
5223cef5090cSJed Brown +  ts - TS context
5224cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5225cef5090cSJed Brown 
5226cef5090cSJed Brown    Options Database Key:
5227cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5228cef5090cSJed Brown 
5229cef5090cSJed Brown    Level: intermediate
5230cef5090cSJed Brown 
5231cef5090cSJed Brown .keywords: TS, set, error
5232cef5090cSJed Brown 
52335ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5234cef5090cSJed Brown @*/
5235cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5236cef5090cSJed Brown {
5237cef5090cSJed Brown   PetscFunctionBegin;
5238cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5239cef5090cSJed Brown   ts->errorifstepfailed = err;
5240cef5090cSJed Brown   PetscFunctionReturn(0);
5241cef5090cSJed Brown }
5242cef5090cSJed Brown 
5243fb1732b5SBarry Smith /*@C
5244fde5950dSBarry 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
5245fb1732b5SBarry Smith 
5246fb1732b5SBarry Smith    Collective on TS
5247fb1732b5SBarry Smith 
5248fb1732b5SBarry Smith    Input Parameters:
5249fb1732b5SBarry Smith +  ts - the TS context
5250fb1732b5SBarry Smith .  step - current time-step
5251fb1732b5SBarry Smith .  ptime - current time
52520910c330SBarry Smith .  u - current state
5253721cd6eeSBarry Smith -  vf - viewer and its format
5254fb1732b5SBarry Smith 
5255fb1732b5SBarry Smith    Level: intermediate
5256fb1732b5SBarry Smith 
5257fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
5258fb1732b5SBarry Smith 
5259fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5260fb1732b5SBarry Smith @*/
5261721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5262fb1732b5SBarry Smith {
5263fb1732b5SBarry Smith   PetscErrorCode ierr;
5264fb1732b5SBarry Smith 
5265fb1732b5SBarry Smith   PetscFunctionBegin;
5266721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5267721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5268721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5269ed81e22dSJed Brown   PetscFunctionReturn(0);
5270ed81e22dSJed Brown }
5271ed81e22dSJed Brown 
5272ed81e22dSJed Brown /*@C
5273ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5274ed81e22dSJed Brown 
5275ed81e22dSJed Brown    Collective on TS
5276ed81e22dSJed Brown 
5277ed81e22dSJed Brown    Input Parameters:
5278ed81e22dSJed Brown +  ts - the TS context
5279ed81e22dSJed Brown .  step - current time-step
5280ed81e22dSJed Brown .  ptime - current time
52810910c330SBarry Smith .  u - current state
5282ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5283ed81e22dSJed Brown 
5284ed81e22dSJed Brown    Level: intermediate
5285ed81e22dSJed Brown 
5286ed81e22dSJed Brown    Notes:
5287ed81e22dSJed 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.
5288ed81e22dSJed Brown    These are named according to the file name template.
5289ed81e22dSJed Brown 
5290ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5291ed81e22dSJed Brown 
5292ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5293ed81e22dSJed Brown 
5294ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5295ed81e22dSJed Brown @*/
52960910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5297ed81e22dSJed Brown {
5298ed81e22dSJed Brown   PetscErrorCode ierr;
5299ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5300ed81e22dSJed Brown   PetscViewer    viewer;
5301ed81e22dSJed Brown 
5302ed81e22dSJed Brown   PetscFunctionBegin;
530363e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
53048caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5305ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
53060910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5307ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5308ed81e22dSJed Brown   PetscFunctionReturn(0);
5309ed81e22dSJed Brown }
5310ed81e22dSJed Brown 
5311ed81e22dSJed Brown /*@C
5312ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5313ed81e22dSJed Brown 
5314ed81e22dSJed Brown    Collective on TS
5315ed81e22dSJed Brown 
5316ed81e22dSJed Brown    Input Parameters:
5317ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5318ed81e22dSJed Brown 
5319ed81e22dSJed Brown    Level: intermediate
5320ed81e22dSJed Brown 
5321ed81e22dSJed Brown    Note:
5322ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5323ed81e22dSJed Brown 
5324ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5325ed81e22dSJed Brown 
5326ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5327ed81e22dSJed Brown @*/
5328ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5329ed81e22dSJed Brown {
5330ed81e22dSJed Brown   PetscErrorCode ierr;
5331ed81e22dSJed Brown 
5332ed81e22dSJed Brown   PetscFunctionBegin;
5333ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5334fb1732b5SBarry Smith   PetscFunctionReturn(0);
5335fb1732b5SBarry Smith }
5336fb1732b5SBarry Smith 
533784df9cb4SJed Brown /*@
5338552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
533984df9cb4SJed Brown 
5340ed81e22dSJed Brown    Collective on TS if controller has not been created yet
534184df9cb4SJed Brown 
534284df9cb4SJed Brown    Input Arguments:
5343ed81e22dSJed Brown .  ts - time stepping context
534484df9cb4SJed Brown 
534584df9cb4SJed Brown    Output Arguments:
5346ed81e22dSJed Brown .  adapt - adaptive controller
534784df9cb4SJed Brown 
534884df9cb4SJed Brown    Level: intermediate
534984df9cb4SJed Brown 
5350ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
535184df9cb4SJed Brown @*/
5352552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
535384df9cb4SJed Brown {
535484df9cb4SJed Brown   PetscErrorCode ierr;
535584df9cb4SJed Brown 
535684df9cb4SJed Brown   PetscFunctionBegin;
535784df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5358bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
535984df9cb4SJed Brown   if (!ts->adapt) {
5360ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
53613bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
53621c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
536384df9cb4SJed Brown   }
5364bec58848SLisandro Dalcin   *adapt = ts->adapt;
536584df9cb4SJed Brown   PetscFunctionReturn(0);
536684df9cb4SJed Brown }
5367d6ebe24aSShri Abhyankar 
53681c3436cfSJed Brown /*@
53691c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
53701c3436cfSJed Brown 
53711c3436cfSJed Brown    Logically Collective
53721c3436cfSJed Brown 
53731c3436cfSJed Brown    Input Arguments:
53741c3436cfSJed Brown +  ts - time integration context
53751c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
53760298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
53771c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
53780298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
53791c3436cfSJed Brown 
5380a3cdaa26SBarry Smith    Options Database keys:
5381a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5382a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5383a3cdaa26SBarry Smith 
53843ff766beSShri Abhyankar    Notes:
53853ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
53863ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
53873ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
53883ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
53893ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
53903ff766beSShri Abhyankar    differential variables.
53913ff766beSShri Abhyankar 
53921c3436cfSJed Brown    Level: beginner
53931c3436cfSJed Brown 
5394c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
53951c3436cfSJed Brown @*/
53961c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
53971c3436cfSJed Brown {
53981c3436cfSJed Brown   PetscErrorCode ierr;
53991c3436cfSJed Brown 
54001c3436cfSJed Brown   PetscFunctionBegin;
5401c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
54021c3436cfSJed Brown   if (vatol) {
54031c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
54041c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
54051c3436cfSJed Brown     ts->vatol = vatol;
54061c3436cfSJed Brown   }
5407c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
54081c3436cfSJed Brown   if (vrtol) {
54091c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
54101c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
54111c3436cfSJed Brown     ts->vrtol = vrtol;
54121c3436cfSJed Brown   }
54131c3436cfSJed Brown   PetscFunctionReturn(0);
54141c3436cfSJed Brown }
54151c3436cfSJed Brown 
5416c5033834SJed Brown /*@
5417c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5418c5033834SJed Brown 
5419c5033834SJed Brown    Logically Collective
5420c5033834SJed Brown 
5421c5033834SJed Brown    Input Arguments:
5422c5033834SJed Brown .  ts - time integration context
5423c5033834SJed Brown 
5424c5033834SJed Brown    Output Arguments:
54250298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
54260298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
54270298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
54280298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5429c5033834SJed Brown 
5430c5033834SJed Brown    Level: beginner
5431c5033834SJed Brown 
5432c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5433c5033834SJed Brown @*/
5434c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5435c5033834SJed Brown {
5436c5033834SJed Brown   PetscFunctionBegin;
5437c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5438c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5439c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5440c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5441c5033834SJed Brown   PetscFunctionReturn(0);
5442c5033834SJed Brown }
5443c5033834SJed Brown 
54449c6b16b5SShri Abhyankar /*@
5445a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
54469c6b16b5SShri Abhyankar 
54479c6b16b5SShri Abhyankar    Collective on TS
54489c6b16b5SShri Abhyankar 
54499c6b16b5SShri Abhyankar    Input Arguments:
54509c6b16b5SShri Abhyankar +  ts - time stepping context
5451a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5452a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
54539c6b16b5SShri Abhyankar 
54549c6b16b5SShri Abhyankar    Output Arguments:
54557453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
54567453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
54577453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
54589c6b16b5SShri Abhyankar 
54599c6b16b5SShri Abhyankar    Level: developer
54609c6b16b5SShri Abhyankar 
5461deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
54629c6b16b5SShri Abhyankar @*/
54637453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
54649c6b16b5SShri Abhyankar {
54659c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
54669c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
54677453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
54687453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
54699c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
54707453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
54717453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
54727453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
54739c6b16b5SShri Abhyankar 
54749c6b16b5SShri Abhyankar   PetscFunctionBegin;
54759c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5476a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5477a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5478a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5479a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5480a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5481a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
54828a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
54838a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5484a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
54859c6b16b5SShri Abhyankar 
54869c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
54879c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
54889c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
54899c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
54909c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
54917453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
54927453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
54937453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
54949c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
54959c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
54969c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54979c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54989c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54997453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55007453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
55017453f775SEmil Constantinescu       if(tola>0.){
55027453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
55037453f775SEmil Constantinescu         na_loc++;
55047453f775SEmil Constantinescu       }
55057453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55067453f775SEmil Constantinescu       if(tolr>0.){
55077453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
55087453f775SEmil Constantinescu         nr_loc++;
55097453f775SEmil Constantinescu       }
55107453f775SEmil Constantinescu       tol=tola+tolr;
55117453f775SEmil Constantinescu       if(tol>0.){
55127453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
55137453f775SEmil Constantinescu         n_loc++;
55147453f775SEmil Constantinescu       }
55159c6b16b5SShri Abhyankar     }
55169c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55179c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55189c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
55199c6b16b5SShri Abhyankar     const PetscScalar *atol;
55209c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55219c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
55227453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55237453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
55247453f775SEmil Constantinescu       if(tola>0.){
55257453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
55267453f775SEmil Constantinescu         na_loc++;
55277453f775SEmil Constantinescu       }
55287453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55297453f775SEmil Constantinescu       if(tolr>0.){
55307453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
55317453f775SEmil Constantinescu         nr_loc++;
55327453f775SEmil Constantinescu       }
55337453f775SEmil Constantinescu       tol=tola+tolr;
55347453f775SEmil Constantinescu       if(tol>0.){
55357453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
55367453f775SEmil Constantinescu         n_loc++;
55377453f775SEmil Constantinescu       }
55389c6b16b5SShri Abhyankar     }
55399c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55409c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
55419c6b16b5SShri Abhyankar     const PetscScalar *rtol;
55429c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55439c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
55447453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55457453f775SEmil Constantinescu       tola = ts->atol;
55467453f775SEmil Constantinescu       if(tola>0.){
55477453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
55487453f775SEmil Constantinescu         na_loc++;
55497453f775SEmil Constantinescu       }
55507453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55517453f775SEmil Constantinescu       if(tolr>0.){
55527453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
55537453f775SEmil Constantinescu         nr_loc++;
55547453f775SEmil Constantinescu       }
55557453f775SEmil Constantinescu       tol=tola+tolr;
55567453f775SEmil Constantinescu       if(tol>0.){
55577453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
55587453f775SEmil Constantinescu         n_loc++;
55597453f775SEmil Constantinescu       }
55609c6b16b5SShri Abhyankar     }
55619c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55629c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
55639c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
55647453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55657453f775SEmil Constantinescu      tola = ts->atol;
55667453f775SEmil Constantinescu       if(tola>0.){
55677453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
55687453f775SEmil Constantinescu         na_loc++;
55697453f775SEmil Constantinescu       }
55707453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55717453f775SEmil Constantinescu       if(tolr>0.){
55727453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
55737453f775SEmil Constantinescu         nr_loc++;
55747453f775SEmil Constantinescu       }
55757453f775SEmil Constantinescu       tol=tola+tolr;
55767453f775SEmil Constantinescu       if(tol>0.){
55777453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
55787453f775SEmil Constantinescu         n_loc++;
55797453f775SEmil Constantinescu       }
55809c6b16b5SShri Abhyankar     }
55819c6b16b5SShri Abhyankar   }
55829c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
55839c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
55849c6b16b5SShri Abhyankar 
55857453f775SEmil Constantinescu   err_loc[0] = sum;
55867453f775SEmil Constantinescu   err_loc[1] = suma;
55877453f775SEmil Constantinescu   err_loc[2] = sumr;
55887453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
55897453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
55907453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
55917453f775SEmil Constantinescu 
5592a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
55937453f775SEmil Constantinescu 
55947453f775SEmil Constantinescu   gsum   = err_glb[0];
55957453f775SEmil Constantinescu   gsuma  = err_glb[1];
55967453f775SEmil Constantinescu   gsumr  = err_glb[2];
55977453f775SEmil Constantinescu   n_glb  = err_glb[3];
55987453f775SEmil Constantinescu   na_glb = err_glb[4];
55997453f775SEmil Constantinescu   nr_glb = err_glb[5];
56007453f775SEmil Constantinescu 
5601b1316ef9SEmil Constantinescu   *norm  = 0.;
5602b1316ef9SEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
5603b1316ef9SEmil Constantinescu   *norma = 0.;
5604b1316ef9SEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5605b1316ef9SEmil Constantinescu   *normr = 0.;
5606b1316ef9SEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
56079c6b16b5SShri Abhyankar 
56089c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
56097453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
56107453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
56119c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
56129c6b16b5SShri Abhyankar }
56139c6b16b5SShri Abhyankar 
56149c6b16b5SShri Abhyankar /*@
5615a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
56169c6b16b5SShri Abhyankar 
56179c6b16b5SShri Abhyankar    Collective on TS
56189c6b16b5SShri Abhyankar 
56199c6b16b5SShri Abhyankar    Input Arguments:
56209c6b16b5SShri Abhyankar +  ts - time stepping context
5621a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5622a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
56239c6b16b5SShri Abhyankar 
56249c6b16b5SShri Abhyankar    Output Arguments:
56257453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
56267453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
56277453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
56289c6b16b5SShri Abhyankar 
56299c6b16b5SShri Abhyankar    Level: developer
56309c6b16b5SShri Abhyankar 
5631deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
56329c6b16b5SShri Abhyankar @*/
56337453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
56349c6b16b5SShri Abhyankar {
56359c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
56367453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
56379c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
56387453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
56397453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
56407453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
56419c6b16b5SShri Abhyankar 
56429c6b16b5SShri Abhyankar   PetscFunctionBegin;
56439c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5644a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5645a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5646a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5647a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5648a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5649a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
56508a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
56518a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5652a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
56539c6b16b5SShri Abhyankar 
56549c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
56559c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
56569c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
56579c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
56589c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
56597453f775SEmil Constantinescu 
56607453f775SEmil Constantinescu   max=0.;
56617453f775SEmil Constantinescu   maxa=0.;
56627453f775SEmil Constantinescu   maxr=0.;
56637453f775SEmil Constantinescu 
56647453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
56659c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
56669c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56679c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56687453f775SEmil Constantinescu 
56697453f775SEmil Constantinescu     for (i=0; i<n; i++) {
56707453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
56717453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
56727453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56737453f775SEmil Constantinescu       tol  = tola+tolr;
56747453f775SEmil Constantinescu       if(tola>0.){
56757453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
56767453f775SEmil Constantinescu       }
56777453f775SEmil Constantinescu       if(tolr>0.){
56787453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
56797453f775SEmil Constantinescu       }
56807453f775SEmil Constantinescu       if(tol>0.){
56817453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
56827453f775SEmil Constantinescu       }
56839c6b16b5SShri Abhyankar     }
56849c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56859c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56869c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
56879c6b16b5SShri Abhyankar     const PetscScalar *atol;
56889c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56897453f775SEmil Constantinescu     for (i=0; i<n; i++) {
56907453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
56917453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
56927453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56937453f775SEmil Constantinescu       tol  = tola+tolr;
56947453f775SEmil Constantinescu       if(tola>0.){
56957453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
56967453f775SEmil Constantinescu       }
56977453f775SEmil Constantinescu       if(tolr>0.){
56987453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
56997453f775SEmil Constantinescu       }
57007453f775SEmil Constantinescu       if(tol>0.){
57017453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
57027453f775SEmil Constantinescu       }
57039c6b16b5SShri Abhyankar     }
57049c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57059c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
57069c6b16b5SShri Abhyankar     const PetscScalar *rtol;
57079c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57087453f775SEmil Constantinescu 
57097453f775SEmil Constantinescu     for (i=0; i<n; i++) {
57107453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57117453f775SEmil Constantinescu       tola = ts->atol;
57127453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57137453f775SEmil Constantinescu       tol  = tola+tolr;
57147453f775SEmil Constantinescu       if(tola>0.){
57157453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
57167453f775SEmil Constantinescu       }
57177453f775SEmil Constantinescu       if(tolr>0.){
57187453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
57197453f775SEmil Constantinescu       }
57207453f775SEmil Constantinescu       if(tol>0.){
57217453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
57227453f775SEmil Constantinescu       }
57239c6b16b5SShri Abhyankar     }
57249c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57259c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
57267453f775SEmil Constantinescu 
57277453f775SEmil Constantinescu     for (i=0; i<n; i++) {
57287453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57297453f775SEmil Constantinescu       tola = ts->atol;
57307453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57317453f775SEmil Constantinescu       tol  = tola+tolr;
57327453f775SEmil Constantinescu       if(tola>0.){
57337453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
57347453f775SEmil Constantinescu       }
57357453f775SEmil Constantinescu       if(tolr>0.){
57367453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
57377453f775SEmil Constantinescu       }
57387453f775SEmil Constantinescu       if(tol>0.){
57397453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
57407453f775SEmil Constantinescu       }
57419c6b16b5SShri Abhyankar     }
57429c6b16b5SShri Abhyankar   }
57439c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
57449c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
57457453f775SEmil Constantinescu   err_loc[0] = max;
57467453f775SEmil Constantinescu   err_loc[1] = maxa;
57477453f775SEmil Constantinescu   err_loc[2] = maxr;
5748a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
57497453f775SEmil Constantinescu   gmax   = err_glb[0];
57507453f775SEmil Constantinescu   gmaxa  = err_glb[1];
57517453f775SEmil Constantinescu   gmaxr  = err_glb[2];
57529c6b16b5SShri Abhyankar 
57539c6b16b5SShri Abhyankar   *norm = gmax;
57547453f775SEmil Constantinescu   *norma = gmaxa;
57557453f775SEmil Constantinescu   *normr = gmaxr;
57569c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
57577453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
57587453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
57599c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
57609c6b16b5SShri Abhyankar }
57619c6b16b5SShri Abhyankar 
57621c3436cfSJed Brown /*@
57638a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
57641c3436cfSJed Brown 
57651c3436cfSJed Brown    Collective on TS
57661c3436cfSJed Brown 
57671c3436cfSJed Brown    Input Arguments:
57681c3436cfSJed Brown +  ts - time stepping context
5769a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5770a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
5771a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
57727619abb3SShri 
57731c3436cfSJed Brown    Output Arguments:
57748a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
57758a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
57768a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
5777a4868fbcSLisandro Dalcin 
5778a4868fbcSLisandro Dalcin    Options Database Keys:
5779a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
5780a4868fbcSLisandro Dalcin 
57811c3436cfSJed Brown    Level: developer
57821c3436cfSJed Brown 
57838a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
57841c3436cfSJed Brown @*/
57857453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
57861c3436cfSJed Brown {
57878beabaa1SBarry Smith   PetscErrorCode ierr;
57881c3436cfSJed Brown 
57891c3436cfSJed Brown   PetscFunctionBegin;
5790a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
57917453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
5792a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
57937453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
5794a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
57951c3436cfSJed Brown   PetscFunctionReturn(0);
57961c3436cfSJed Brown }
57971c3436cfSJed Brown 
57988a175baeSEmil Constantinescu 
57998a175baeSEmil Constantinescu /*@
58008a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
58018a175baeSEmil Constantinescu 
58028a175baeSEmil Constantinescu    Collective on TS
58038a175baeSEmil Constantinescu 
58048a175baeSEmil Constantinescu    Input Arguments:
58058a175baeSEmil Constantinescu +  ts - time stepping context
58068a175baeSEmil Constantinescu .  E - error vector
58078a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
58088a175baeSEmil Constantinescu -  Y - state vector, previous time step
58098a175baeSEmil Constantinescu 
58108a175baeSEmil Constantinescu    Output Arguments:
58118a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
58128a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
58138a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
58148a175baeSEmil Constantinescu 
58158a175baeSEmil Constantinescu    Level: developer
58168a175baeSEmil Constantinescu 
58178a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
58188a175baeSEmil Constantinescu @*/
58198a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
58208a175baeSEmil Constantinescu {
58218a175baeSEmil Constantinescu   PetscErrorCode    ierr;
58228a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
58238a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
58248a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
58258a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
58268a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
58278a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
58288a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
58298a175baeSEmil Constantinescu 
58308a175baeSEmil Constantinescu   PetscFunctionBegin;
58318a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
58328a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
58338a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
58348a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
58358a175baeSEmil Constantinescu   PetscValidType(E,2);
58368a175baeSEmil Constantinescu   PetscValidType(U,3);
58378a175baeSEmil Constantinescu   PetscValidType(Y,4);
58388a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
58398a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
58408a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
58418a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
58428a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
58438a175baeSEmil Constantinescu 
58448a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
58458a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
58468a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
58478a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
58488a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
58498a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
58508a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
58518a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
58528a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
58538a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
58548a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
58558a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58568a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58578a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
58588a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
58598a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
58608a175baeSEmil Constantinescu       if(tola>0.){
58618a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
58628a175baeSEmil Constantinescu         na_loc++;
58638a175baeSEmil Constantinescu       }
58648a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58658a175baeSEmil Constantinescu       if(tolr>0.){
58668a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
58678a175baeSEmil Constantinescu         nr_loc++;
58688a175baeSEmil Constantinescu       }
58698a175baeSEmil Constantinescu       tol=tola+tolr;
58708a175baeSEmil Constantinescu       if(tol>0.){
58718a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
58728a175baeSEmil Constantinescu         n_loc++;
58738a175baeSEmil Constantinescu       }
58748a175baeSEmil Constantinescu     }
58758a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58768a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58778a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
58788a175baeSEmil Constantinescu     const PetscScalar *atol;
58798a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58808a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
58818a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
58828a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
58838a175baeSEmil Constantinescu       if(tola>0.){
58848a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
58858a175baeSEmil Constantinescu         na_loc++;
58868a175baeSEmil Constantinescu       }
58878a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58888a175baeSEmil Constantinescu       if(tolr>0.){
58898a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
58908a175baeSEmil Constantinescu         nr_loc++;
58918a175baeSEmil Constantinescu       }
58928a175baeSEmil Constantinescu       tol=tola+tolr;
58938a175baeSEmil Constantinescu       if(tol>0.){
58948a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
58958a175baeSEmil Constantinescu         n_loc++;
58968a175baeSEmil Constantinescu       }
58978a175baeSEmil Constantinescu     }
58988a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58998a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
59008a175baeSEmil Constantinescu     const PetscScalar *rtol;
59018a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59028a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
59038a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59048a175baeSEmil Constantinescu       tola = ts->atol;
59058a175baeSEmil Constantinescu       if(tola>0.){
59068a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
59078a175baeSEmil Constantinescu         na_loc++;
59088a175baeSEmil Constantinescu       }
59098a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59108a175baeSEmil Constantinescu       if(tolr>0.){
59118a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
59128a175baeSEmil Constantinescu         nr_loc++;
59138a175baeSEmil Constantinescu       }
59148a175baeSEmil Constantinescu       tol=tola+tolr;
59158a175baeSEmil Constantinescu       if(tol>0.){
59168a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
59178a175baeSEmil Constantinescu         n_loc++;
59188a175baeSEmil Constantinescu       }
59198a175baeSEmil Constantinescu     }
59208a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59218a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
59228a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
59238a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59248a175baeSEmil Constantinescu      tola = ts->atol;
59258a175baeSEmil Constantinescu       if(tola>0.){
59268a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
59278a175baeSEmil Constantinescu         na_loc++;
59288a175baeSEmil Constantinescu       }
59298a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59308a175baeSEmil Constantinescu       if(tolr>0.){
59318a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
59328a175baeSEmil Constantinescu         nr_loc++;
59338a175baeSEmil Constantinescu       }
59348a175baeSEmil Constantinescu       tol=tola+tolr;
59358a175baeSEmil Constantinescu       if(tol>0.){
59368a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
59378a175baeSEmil Constantinescu         n_loc++;
59388a175baeSEmil Constantinescu       }
59398a175baeSEmil Constantinescu     }
59408a175baeSEmil Constantinescu   }
59418a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
59428a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
59438a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
59448a175baeSEmil Constantinescu 
59458a175baeSEmil Constantinescu   err_loc[0] = sum;
59468a175baeSEmil Constantinescu   err_loc[1] = suma;
59478a175baeSEmil Constantinescu   err_loc[2] = sumr;
59488a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
59498a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
59508a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
59518a175baeSEmil Constantinescu 
5952a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
59538a175baeSEmil Constantinescu 
59548a175baeSEmil Constantinescu   gsum   = err_glb[0];
59558a175baeSEmil Constantinescu   gsuma  = err_glb[1];
59568a175baeSEmil Constantinescu   gsumr  = err_glb[2];
59578a175baeSEmil Constantinescu   n_glb  = err_glb[3];
59588a175baeSEmil Constantinescu   na_glb = err_glb[4];
59598a175baeSEmil Constantinescu   nr_glb = err_glb[5];
59608a175baeSEmil Constantinescu 
59618a175baeSEmil Constantinescu   *norm  = 0.;
59628a175baeSEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
59638a175baeSEmil Constantinescu   *norma = 0.;
59648a175baeSEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
59658a175baeSEmil Constantinescu   *normr = 0.;
59668a175baeSEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
59678a175baeSEmil Constantinescu 
59688a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
59698a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
59708a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
59718a175baeSEmil Constantinescu   PetscFunctionReturn(0);
59728a175baeSEmil Constantinescu }
59738a175baeSEmil Constantinescu 
59748a175baeSEmil Constantinescu /*@
59758a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
59768a175baeSEmil Constantinescu    Collective on TS
59778a175baeSEmil Constantinescu 
59788a175baeSEmil Constantinescu    Input Arguments:
59798a175baeSEmil Constantinescu +  ts - time stepping context
59808a175baeSEmil Constantinescu .  E - error vector
59818a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
59828a175baeSEmil Constantinescu -  Y - state vector, previous time step
59838a175baeSEmil Constantinescu 
59848a175baeSEmil Constantinescu    Output Arguments:
59858a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
59868a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
59878a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
59888a175baeSEmil Constantinescu 
59898a175baeSEmil Constantinescu    Level: developer
59908a175baeSEmil Constantinescu 
59918a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
59928a175baeSEmil Constantinescu @*/
59938a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
59948a175baeSEmil Constantinescu {
59958a175baeSEmil Constantinescu   PetscErrorCode    ierr;
59968a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
59978a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
59988a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
59998a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
60008a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
60018a175baeSEmil Constantinescu 
60028a175baeSEmil Constantinescu   PetscFunctionBegin;
60038a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
60048a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
60058a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
60068a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
60078a175baeSEmil Constantinescu   PetscValidType(E,2);
60088a175baeSEmil Constantinescu   PetscValidType(U,3);
60098a175baeSEmil Constantinescu   PetscValidType(Y,4);
60108a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
60118a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
60128a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
60138a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
60148a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
60158a175baeSEmil Constantinescu 
60168a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
60178a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
60188a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
60198a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
60208a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
60218a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
60228a175baeSEmil Constantinescu 
60238a175baeSEmil Constantinescu   max=0.;
60248a175baeSEmil Constantinescu   maxa=0.;
60258a175baeSEmil Constantinescu   maxr=0.;
60268a175baeSEmil Constantinescu 
60278a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
60288a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
60298a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60308a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60318a175baeSEmil Constantinescu 
60328a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
60338a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
60348a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
60358a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60368a175baeSEmil Constantinescu       tol  = tola+tolr;
60378a175baeSEmil Constantinescu       if(tola>0.){
60388a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
60398a175baeSEmil Constantinescu       }
60408a175baeSEmil Constantinescu       if(tolr>0.){
60418a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
60428a175baeSEmil Constantinescu       }
60438a175baeSEmil Constantinescu       if(tol>0.){
60448a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
60458a175baeSEmil Constantinescu       }
60468a175baeSEmil Constantinescu     }
60478a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60488a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60498a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
60508a175baeSEmil Constantinescu     const PetscScalar *atol;
60518a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60528a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
60538a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
60548a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
60558a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60568a175baeSEmil Constantinescu       tol  = tola+tolr;
60578a175baeSEmil Constantinescu       if(tola>0.){
60588a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
60598a175baeSEmil Constantinescu       }
60608a175baeSEmil Constantinescu       if(tolr>0.){
60618a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
60628a175baeSEmil Constantinescu       }
60638a175baeSEmil Constantinescu       if(tol>0.){
60648a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
60658a175baeSEmil Constantinescu       }
60668a175baeSEmil Constantinescu     }
60678a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60688a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
60698a175baeSEmil Constantinescu     const PetscScalar *rtol;
60708a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60718a175baeSEmil Constantinescu 
60728a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
60738a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
60748a175baeSEmil Constantinescu       tola = ts->atol;
60758a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60768a175baeSEmil Constantinescu       tol  = tola+tolr;
60778a175baeSEmil Constantinescu       if(tola>0.){
60788a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
60798a175baeSEmil Constantinescu       }
60808a175baeSEmil Constantinescu       if(tolr>0.){
60818a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
60828a175baeSEmil Constantinescu       }
60838a175baeSEmil Constantinescu       if(tol>0.){
60848a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
60858a175baeSEmil Constantinescu       }
60868a175baeSEmil Constantinescu     }
60878a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60888a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
60898a175baeSEmil Constantinescu 
60908a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
60918a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
60928a175baeSEmil Constantinescu       tola = ts->atol;
60938a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60948a175baeSEmil Constantinescu       tol  = tola+tolr;
60958a175baeSEmil Constantinescu       if(tola>0.){
60968a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
60978a175baeSEmil Constantinescu       }
60988a175baeSEmil Constantinescu       if(tolr>0.){
60998a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
61008a175baeSEmil Constantinescu       }
61018a175baeSEmil Constantinescu       if(tol>0.){
61028a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
61038a175baeSEmil Constantinescu       }
61048a175baeSEmil Constantinescu     }
61058a175baeSEmil Constantinescu   }
61068a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
61078a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
61088a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
61098a175baeSEmil Constantinescu   err_loc[0] = max;
61108a175baeSEmil Constantinescu   err_loc[1] = maxa;
61118a175baeSEmil Constantinescu   err_loc[2] = maxr;
6112a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
61138a175baeSEmil Constantinescu   gmax   = err_glb[0];
61148a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
61158a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
61168a175baeSEmil Constantinescu 
61178a175baeSEmil Constantinescu   *norm = gmax;
61188a175baeSEmil Constantinescu   *norma = gmaxa;
61198a175baeSEmil Constantinescu   *normr = gmaxr;
61208a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
61218a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
61228a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
61238a175baeSEmil Constantinescu   PetscFunctionReturn(0);
61248a175baeSEmil Constantinescu }
61258a175baeSEmil Constantinescu 
61268a175baeSEmil Constantinescu /*@
61278a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
61288a175baeSEmil Constantinescu 
61298a175baeSEmil Constantinescu    Collective on TS
61308a175baeSEmil Constantinescu 
61318a175baeSEmil Constantinescu    Input Arguments:
61328a175baeSEmil Constantinescu +  ts - time stepping context
61338a175baeSEmil Constantinescu .  E - error vector
61348a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
61358a175baeSEmil Constantinescu .  Y - state vector, previous time step
61368a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
61378a175baeSEmil Constantinescu 
61388a175baeSEmil Constantinescu    Output Arguments:
61398a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
61408a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
61418a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
61428a175baeSEmil Constantinescu 
61438a175baeSEmil Constantinescu    Options Database Keys:
61448a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
61458a175baeSEmil Constantinescu 
61468a175baeSEmil Constantinescu    Level: developer
61478a175baeSEmil Constantinescu 
61488a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
61498a175baeSEmil Constantinescu @*/
61508a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
61518a175baeSEmil Constantinescu {
61528a175baeSEmil Constantinescu   PetscErrorCode ierr;
61538a175baeSEmil Constantinescu 
61548a175baeSEmil Constantinescu   PetscFunctionBegin;
61558a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
61568a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
61578a175baeSEmil Constantinescu   } else if(wnormtype == NORM_INFINITY) {
61588a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
61598a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
61608a175baeSEmil Constantinescu   PetscFunctionReturn(0);
61618a175baeSEmil Constantinescu }
61628a175baeSEmil Constantinescu 
61638a175baeSEmil Constantinescu 
61648d59e960SJed Brown /*@
61658d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
61668d59e960SJed Brown 
61678d59e960SJed Brown    Logically Collective on TS
61688d59e960SJed Brown 
61698d59e960SJed Brown    Input Arguments:
61708d59e960SJed Brown +  ts - time stepping context
61718d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
61728d59e960SJed Brown 
61738d59e960SJed Brown    Note:
61748d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
61758d59e960SJed Brown 
61768d59e960SJed Brown    Level: intermediate
61778d59e960SJed Brown 
61788d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
61798d59e960SJed Brown @*/
61808d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
61818d59e960SJed Brown {
61828d59e960SJed Brown   PetscFunctionBegin;
61838d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
61848d59e960SJed Brown   ts->cfltime_local = cfltime;
61858d59e960SJed Brown   ts->cfltime       = -1.;
61868d59e960SJed Brown   PetscFunctionReturn(0);
61878d59e960SJed Brown }
61888d59e960SJed Brown 
61898d59e960SJed Brown /*@
61908d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
61918d59e960SJed Brown 
61928d59e960SJed Brown    Collective on TS
61938d59e960SJed Brown 
61948d59e960SJed Brown    Input Arguments:
61958d59e960SJed Brown .  ts - time stepping context
61968d59e960SJed Brown 
61978d59e960SJed Brown    Output Arguments:
61988d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
61998d59e960SJed Brown 
62008d59e960SJed Brown    Level: advanced
62018d59e960SJed Brown 
62028d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
62038d59e960SJed Brown @*/
62048d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
62058d59e960SJed Brown {
62068d59e960SJed Brown   PetscErrorCode ierr;
62078d59e960SJed Brown 
62088d59e960SJed Brown   PetscFunctionBegin;
62098d59e960SJed Brown   if (ts->cfltime < 0) {
6210b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
62118d59e960SJed Brown   }
62128d59e960SJed Brown   *cfltime = ts->cfltime;
62138d59e960SJed Brown   PetscFunctionReturn(0);
62148d59e960SJed Brown }
62158d59e960SJed Brown 
6216d6ebe24aSShri Abhyankar /*@
6217d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6218d6ebe24aSShri Abhyankar 
6219d6ebe24aSShri Abhyankar    Input Parameters:
6220d6ebe24aSShri Abhyankar .  ts   - the TS context.
6221d6ebe24aSShri Abhyankar .  xl   - lower bound.
6222d6ebe24aSShri Abhyankar .  xu   - upper bound.
6223d6ebe24aSShri Abhyankar 
6224d6ebe24aSShri Abhyankar    Notes:
6225d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6226e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6227d6ebe24aSShri Abhyankar 
62282bd2b0e6SSatish Balay    Level: advanced
62292bd2b0e6SSatish Balay 
6230d6ebe24aSShri Abhyankar @*/
6231d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6232d6ebe24aSShri Abhyankar {
6233d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6234d6ebe24aSShri Abhyankar   SNES           snes;
6235d6ebe24aSShri Abhyankar 
6236d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6237d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6238d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6239d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6240d6ebe24aSShri Abhyankar }
6241d6ebe24aSShri Abhyankar 
6242325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
6243c6db04a5SJed Brown #include <mex.h>
6244325fc9f4SBarry Smith 
6245325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
6246325fc9f4SBarry Smith 
6247325fc9f4SBarry Smith /*
6248325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
6249325fc9f4SBarry Smith                          TSSetFunctionMatlab().
6250325fc9f4SBarry Smith 
6251325fc9f4SBarry Smith    Collective on TS
6252325fc9f4SBarry Smith 
6253325fc9f4SBarry Smith    Input Parameters:
6254325fc9f4SBarry Smith +  snes - the TS context
62550910c330SBarry Smith -  u - input vector
6256325fc9f4SBarry Smith 
6257325fc9f4SBarry Smith    Output Parameter:
6258325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
6259325fc9f4SBarry Smith 
6260325fc9f4SBarry Smith    Notes:
6261325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
6262325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
6263325fc9f4SBarry Smith    themselves.
6264325fc9f4SBarry Smith 
6265325fc9f4SBarry Smith    Level: developer
6266325fc9f4SBarry Smith 
6267325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6268325fc9f4SBarry Smith 
6269325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6270325fc9f4SBarry Smith */
62710910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
6272325fc9f4SBarry Smith {
6273325fc9f4SBarry Smith   PetscErrorCode  ierr;
6274325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6275325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
6276325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
6277325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
6278325fc9f4SBarry Smith 
6279325fc9f4SBarry Smith   PetscFunctionBegin;
6280325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
62810910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
62820910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
6283325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
62840910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
6285325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
6286325fc9f4SBarry Smith 
6287325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
62880910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
62890910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
62900910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
6291bbd56ea5SKarl Rupp 
6292325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6293325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
6294325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6295325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6296325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
6297325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
6298325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
6299325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
6300325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6301325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6302325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6303325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6304325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6305325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6306325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6307325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6308325fc9f4SBarry Smith   PetscFunctionReturn(0);
6309325fc9f4SBarry Smith }
6310325fc9f4SBarry Smith 
6311325fc9f4SBarry Smith /*
6312325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
6313325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
6314e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
6315325fc9f4SBarry Smith 
6316325fc9f4SBarry Smith    Logically Collective on TS
6317325fc9f4SBarry Smith 
6318325fc9f4SBarry Smith    Input Parameters:
6319325fc9f4SBarry Smith +  ts - the TS context
6320325fc9f4SBarry Smith -  func - function evaluation routine
6321325fc9f4SBarry Smith 
6322325fc9f4SBarry Smith    Calling sequence of func:
63230910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
6324325fc9f4SBarry Smith 
6325325fc9f4SBarry Smith    Level: beginner
6326325fc9f4SBarry Smith 
6327325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6328325fc9f4SBarry Smith 
6329325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6330325fc9f4SBarry Smith */
6331cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
6332325fc9f4SBarry Smith {
6333325fc9f4SBarry Smith   PetscErrorCode  ierr;
6334325fc9f4SBarry Smith   TSMatlabContext *sctx;
6335325fc9f4SBarry Smith 
6336325fc9f4SBarry Smith   PetscFunctionBegin;
6337325fc9f4SBarry Smith   /* currently sctx is memory bleed */
633895dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6339325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6340325fc9f4SBarry Smith   /*
6341325fc9f4SBarry Smith      This should work, but it doesn't
6342325fc9f4SBarry Smith   sctx->ctx = ctx;
6343325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6344325fc9f4SBarry Smith   */
6345325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6346bbd56ea5SKarl Rupp 
63470298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
6348325fc9f4SBarry Smith   PetscFunctionReturn(0);
6349325fc9f4SBarry Smith }
6350325fc9f4SBarry Smith 
6351325fc9f4SBarry Smith /*
6352325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
6353325fc9f4SBarry Smith                          TSSetJacobianMatlab().
6354325fc9f4SBarry Smith 
6355325fc9f4SBarry Smith    Collective on TS
6356325fc9f4SBarry Smith 
6357325fc9f4SBarry Smith    Input Parameters:
6358cdcf91faSSean Farley +  ts - the TS context
63590910c330SBarry Smith .  u - input vector
6360325fc9f4SBarry Smith .  A, B - the matrices
6361325fc9f4SBarry Smith -  ctx - user context
6362325fc9f4SBarry Smith 
6363325fc9f4SBarry Smith    Level: developer
6364325fc9f4SBarry Smith 
6365325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6366325fc9f4SBarry Smith 
6367325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6368325fc9f4SBarry Smith @*/
6369f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
6370325fc9f4SBarry Smith {
6371325fc9f4SBarry Smith   PetscErrorCode  ierr;
6372325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6373325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
6374325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
6375325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
6376325fc9f4SBarry Smith 
6377325fc9f4SBarry Smith   PetscFunctionBegin;
6378cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
63790910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
6380325fc9f4SBarry Smith 
63810910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
6382325fc9f4SBarry Smith 
6383cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
63840910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
63850910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
63860910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
63870910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
6388bbd56ea5SKarl Rupp 
6389325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6390325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
6391325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6392325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6393325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
6394325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
6395325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
6396325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
6397325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
6398325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
6399325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6400325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6401325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6402325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6403325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6404325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6405325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6406325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
6407325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
6408325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6409325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
6410325fc9f4SBarry Smith   PetscFunctionReturn(0);
6411325fc9f4SBarry Smith }
6412325fc9f4SBarry Smith 
6413325fc9f4SBarry Smith /*
6414325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
6415e3c5b3baSBarry 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
6416325fc9f4SBarry Smith 
6417325fc9f4SBarry Smith    Logically Collective on TS
6418325fc9f4SBarry Smith 
6419325fc9f4SBarry Smith    Input Parameters:
6420cdcf91faSSean Farley +  ts - the TS context
6421325fc9f4SBarry Smith .  A,B - Jacobian matrices
6422325fc9f4SBarry Smith .  func - function evaluation routine
6423325fc9f4SBarry Smith -  ctx - user context
6424325fc9f4SBarry Smith 
6425325fc9f4SBarry Smith    Calling sequence of func:
64260910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
6427325fc9f4SBarry Smith 
6428325fc9f4SBarry Smith    Level: developer
6429325fc9f4SBarry Smith 
6430325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6431325fc9f4SBarry Smith 
6432325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6433325fc9f4SBarry Smith */
6434cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
6435325fc9f4SBarry Smith {
6436325fc9f4SBarry Smith   PetscErrorCode  ierr;
6437325fc9f4SBarry Smith   TSMatlabContext *sctx;
6438325fc9f4SBarry Smith 
6439325fc9f4SBarry Smith   PetscFunctionBegin;
6440325fc9f4SBarry Smith   /* currently sctx is memory bleed */
644195dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6442325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6443325fc9f4SBarry Smith   /*
6444325fc9f4SBarry Smith      This should work, but it doesn't
6445325fc9f4SBarry Smith   sctx->ctx = ctx;
6446325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6447325fc9f4SBarry Smith   */
6448325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6449bbd56ea5SKarl Rupp 
6450cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
6451325fc9f4SBarry Smith   PetscFunctionReturn(0);
6452325fc9f4SBarry Smith }
6453325fc9f4SBarry Smith 
6454b5b1a830SBarry Smith /*
6455b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
6456b5b1a830SBarry Smith 
6457b5b1a830SBarry Smith    Collective on TS
6458b5b1a830SBarry Smith 
6459b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6460b5b1a830SBarry Smith @*/
64610910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
6462b5b1a830SBarry Smith {
6463b5b1a830SBarry Smith   PetscErrorCode  ierr;
6464b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6465a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
6466b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
6467b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
6468b5b1a830SBarry Smith 
6469b5b1a830SBarry Smith   PetscFunctionBegin;
6470cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
64710910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
6472b5b1a830SBarry Smith 
6473cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
64740910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
6475bbd56ea5SKarl Rupp 
6476b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6477b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
6478b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
6479b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
6480b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
6481b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
6482b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
6483b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6484b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
6485b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
6486b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
6487b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
6488b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
6489b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
6490b5b1a830SBarry Smith   PetscFunctionReturn(0);
6491b5b1a830SBarry Smith }
6492b5b1a830SBarry Smith 
6493b5b1a830SBarry Smith /*
6494b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
6495b5b1a830SBarry Smith 
6496b5b1a830SBarry Smith    Level: developer
6497b5b1a830SBarry Smith 
6498b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
6499b5b1a830SBarry Smith 
6500b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6501b5b1a830SBarry Smith */
6502cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
6503b5b1a830SBarry Smith {
6504b5b1a830SBarry Smith   PetscErrorCode  ierr;
6505b5b1a830SBarry Smith   TSMatlabContext *sctx;
6506b5b1a830SBarry Smith 
6507b5b1a830SBarry Smith   PetscFunctionBegin;
6508b5b1a830SBarry Smith   /* currently sctx is memory bleed */
650995dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6510b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6511b5b1a830SBarry Smith   /*
6512b5b1a830SBarry Smith      This should work, but it doesn't
6513b5b1a830SBarry Smith   sctx->ctx = ctx;
6514b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6515b5b1a830SBarry Smith   */
6516b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6517bbd56ea5SKarl Rupp 
65180298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
6519b5b1a830SBarry Smith   PetscFunctionReturn(0);
6520b5b1a830SBarry Smith }
6521325fc9f4SBarry Smith #endif
6522b3603a34SBarry Smith 
6523b3603a34SBarry Smith /*@C
65244f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6525b3603a34SBarry Smith        in a time based line graph
6526b3603a34SBarry Smith 
6527b3603a34SBarry Smith    Collective on TS
6528b3603a34SBarry Smith 
6529b3603a34SBarry Smith    Input Parameters:
6530b3603a34SBarry Smith +  ts - the TS context
6531b3603a34SBarry Smith .  step - current time-step
6532b3603a34SBarry Smith .  ptime - current time
65337db568b7SBarry Smith .  u - current solution
65347db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6535b3603a34SBarry Smith 
6536b3d3934dSBarry Smith    Options Database:
65379ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6538b3d3934dSBarry Smith 
6539b3603a34SBarry Smith    Level: intermediate
6540b3603a34SBarry Smith 
654195452b02SPatrick Sanan    Notes:
654295452b02SPatrick Sanan     Each process in a parallel run displays its component solutions in a separate window
6543b3603a34SBarry Smith 
6544b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
6545b3603a34SBarry Smith 
65467db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
65477db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
65487db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
65497db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6550b3603a34SBarry Smith @*/
65517db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6552b3603a34SBarry Smith {
6553b3603a34SBarry Smith   PetscErrorCode    ierr;
65547db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6555b3603a34SBarry Smith   const PetscScalar *yy;
655680666b62SBarry Smith   Vec               v;
6557b3603a34SBarry Smith 
6558b3603a34SBarry Smith   PetscFunctionBegin;
655963e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
656058ff32f7SBarry Smith   if (!step) {
6561a9f9c1f6SBarry Smith     PetscDrawAxis axis;
65626934998bSLisandro Dalcin     PetscInt      dim;
6563a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6564a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6565bab0a581SBarry Smith     if (!ctx->names) {
6566bab0a581SBarry Smith       PetscBool flg;
6567bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6568bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6569bab0a581SBarry Smith       if (flg) {
6570bab0a581SBarry Smith         PetscInt i,n;
6571bab0a581SBarry Smith         char     **names;
6572bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6573bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6574bab0a581SBarry Smith         for (i=0; i<n; i++) {
6575bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6576bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6577bab0a581SBarry Smith         }
6578bab0a581SBarry Smith         names[n] = NULL;
6579bab0a581SBarry Smith         ctx->names = names;
6580bab0a581SBarry Smith       }
6581bab0a581SBarry Smith     }
6582387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6583387f4636SBarry Smith       char      **displaynames;
6584387f4636SBarry Smith       PetscBool flg;
6585387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
658695dccacaSBarry Smith       ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6587387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
6588c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6589387f4636SBarry Smith       if (flg) {
6590a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6591387f4636SBarry Smith       }
6592387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6593387f4636SBarry Smith     }
6594387f4636SBarry Smith     if (ctx->displaynames) {
6595387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6596387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6597387f4636SBarry Smith     } else if (ctx->names) {
65980910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
65990b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6600387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6601b0bc92c6SBarry Smith     } else {
6602b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6603b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6604387f4636SBarry Smith     }
66050b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
660658ff32f7SBarry Smith   }
66076934998bSLisandro Dalcin 
66086934998bSLisandro Dalcin   if (!ctx->transform) v = u;
66096934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
661080666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
66116934998bSLisandro Dalcin   if (ctx->displaynames) {
66126934998bSLisandro Dalcin     PetscInt i;
66136934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
66146934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
66156934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
66166934998bSLisandro Dalcin   } else {
6617e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6618e3efe391SJed Brown     PetscInt  i,n;
66196934998bSLisandro Dalcin     PetscReal *yreal;
662080666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6621785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6622e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
66230b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6624e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6625e3efe391SJed Brown #else
66260b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6627e3efe391SJed Brown #endif
662880666b62SBarry Smith   }
66296934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
66306934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
66316934998bSLisandro Dalcin 
6632b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
66330b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
66346934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
66353923b477SBarry Smith   }
6636b3603a34SBarry Smith   PetscFunctionReturn(0);
6637b3603a34SBarry Smith }
6638b3603a34SBarry Smith 
6639b037adc7SBarry Smith /*@C
664031152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6641b037adc7SBarry Smith 
6642b037adc7SBarry Smith    Collective on TS
6643b037adc7SBarry Smith 
6644b037adc7SBarry Smith    Input Parameters:
6645b037adc7SBarry Smith +  ts - the TS context
6646b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6647b037adc7SBarry Smith 
6648b037adc7SBarry Smith    Level: intermediate
6649b037adc7SBarry Smith 
665095452b02SPatrick Sanan    Notes:
665195452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66527db568b7SBarry Smith 
6653b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
6654b037adc7SBarry Smith 
6655a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6656b037adc7SBarry Smith @*/
665731152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6658b037adc7SBarry Smith {
6659b037adc7SBarry Smith   PetscErrorCode    ierr;
6660b037adc7SBarry Smith   PetscInt          i;
6661b037adc7SBarry Smith 
6662b037adc7SBarry Smith   PetscFunctionBegin;
6663b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6664b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
66655537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6666b3d3934dSBarry Smith       break;
6667b3d3934dSBarry Smith     }
6668b3d3934dSBarry Smith   }
6669b3d3934dSBarry Smith   PetscFunctionReturn(0);
6670b3d3934dSBarry Smith }
6671b3d3934dSBarry Smith 
6672e673d494SBarry Smith /*@C
6673e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6674e673d494SBarry Smith 
6675e673d494SBarry Smith    Collective on TS
6676e673d494SBarry Smith 
6677e673d494SBarry Smith    Input Parameters:
6678e673d494SBarry Smith +  ts - the TS context
6679e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6680e673d494SBarry Smith 
6681e673d494SBarry Smith    Level: intermediate
6682e673d494SBarry Smith 
6683e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6684e673d494SBarry Smith 
6685a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6686e673d494SBarry Smith @*/
6687e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6688e673d494SBarry Smith {
6689e673d494SBarry Smith   PetscErrorCode    ierr;
6690e673d494SBarry Smith 
6691e673d494SBarry Smith   PetscFunctionBegin;
6692e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6693e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6694e673d494SBarry Smith   PetscFunctionReturn(0);
6695e673d494SBarry Smith }
6696e673d494SBarry Smith 
6697b3d3934dSBarry Smith /*@C
6698b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6699b3d3934dSBarry Smith 
6700b3d3934dSBarry Smith    Collective on TS
6701b3d3934dSBarry Smith 
6702b3d3934dSBarry Smith    Input Parameter:
6703b3d3934dSBarry Smith .  ts - the TS context
6704b3d3934dSBarry Smith 
6705b3d3934dSBarry Smith    Output Parameter:
6706b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6707b3d3934dSBarry Smith 
6708b3d3934dSBarry Smith    Level: intermediate
6709b3d3934dSBarry Smith 
671095452b02SPatrick Sanan    Notes:
671195452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67127db568b7SBarry Smith 
6713b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6714b3d3934dSBarry Smith 
6715b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6716b3d3934dSBarry Smith @*/
6717b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6718b3d3934dSBarry Smith {
6719b3d3934dSBarry Smith   PetscInt       i;
6720b3d3934dSBarry Smith 
6721b3d3934dSBarry Smith   PetscFunctionBegin;
6722b3d3934dSBarry Smith   *names = NULL;
6723b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6724b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67255537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6726b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6727b3d3934dSBarry Smith       break;
6728387f4636SBarry Smith     }
6729387f4636SBarry Smith   }
6730387f4636SBarry Smith   PetscFunctionReturn(0);
6731387f4636SBarry Smith }
6732387f4636SBarry Smith 
6733a66092f1SBarry Smith /*@C
6734a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6735a66092f1SBarry Smith 
6736a66092f1SBarry Smith    Collective on TS
6737a66092f1SBarry Smith 
6738a66092f1SBarry Smith    Input Parameters:
6739a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6740a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
6741a66092f1SBarry Smith 
6742a66092f1SBarry Smith    Level: intermediate
6743a66092f1SBarry Smith 
6744a66092f1SBarry Smith .keywords: TS,  vector, monitor, view
6745a66092f1SBarry Smith 
6746a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6747a66092f1SBarry Smith @*/
6748a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6749a66092f1SBarry Smith {
6750a66092f1SBarry Smith   PetscInt          j = 0,k;
6751a66092f1SBarry Smith   PetscErrorCode    ierr;
6752a66092f1SBarry Smith 
6753a66092f1SBarry Smith   PetscFunctionBegin;
6754a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6755a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6756a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6757a66092f1SBarry Smith   while (displaynames[j]) j++;
6758a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6759a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6760a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6761a66092f1SBarry Smith   j = 0;
6762a66092f1SBarry Smith   while (displaynames[j]) {
6763a66092f1SBarry Smith     k = 0;
6764a66092f1SBarry Smith     while (ctx->names[k]) {
6765a66092f1SBarry Smith       PetscBool flg;
6766a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6767a66092f1SBarry Smith       if (flg) {
6768a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6769a66092f1SBarry Smith         break;
6770a66092f1SBarry Smith       }
6771a66092f1SBarry Smith       k++;
6772a66092f1SBarry Smith     }
6773a66092f1SBarry Smith     j++;
6774a66092f1SBarry Smith   }
6775a66092f1SBarry Smith   PetscFunctionReturn(0);
6776a66092f1SBarry Smith }
6777a66092f1SBarry Smith 
6778387f4636SBarry Smith /*@C
6779387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6780387f4636SBarry Smith 
6781387f4636SBarry Smith    Collective on TS
6782387f4636SBarry Smith 
6783387f4636SBarry Smith    Input Parameters:
6784387f4636SBarry Smith +  ts - the TS context
6785387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
6786387f4636SBarry Smith 
678795452b02SPatrick Sanan    Notes:
678895452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67897db568b7SBarry Smith 
6790387f4636SBarry Smith    Level: intermediate
6791387f4636SBarry Smith 
6792387f4636SBarry Smith .keywords: TS,  vector, monitor, view
6793387f4636SBarry Smith 
6794387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6795387f4636SBarry Smith @*/
6796387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6797387f4636SBarry Smith {
6798a66092f1SBarry Smith   PetscInt          i;
6799387f4636SBarry Smith   PetscErrorCode    ierr;
6800387f4636SBarry Smith 
6801387f4636SBarry Smith   PetscFunctionBegin;
6802387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6803387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
68045537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6805b3d3934dSBarry Smith       break;
6806b037adc7SBarry Smith     }
6807b037adc7SBarry Smith   }
6808b037adc7SBarry Smith   PetscFunctionReturn(0);
6809b037adc7SBarry Smith }
6810b037adc7SBarry Smith 
681180666b62SBarry Smith /*@C
681280666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
681380666b62SBarry Smith 
681480666b62SBarry Smith    Collective on TS
681580666b62SBarry Smith 
681680666b62SBarry Smith    Input Parameters:
681780666b62SBarry Smith +  ts - the TS context
681880666b62SBarry Smith .  transform - the transform function
68197684fa3eSBarry Smith .  destroy - function to destroy the optional context
682080666b62SBarry Smith -  ctx - optional context used by transform function
682180666b62SBarry Smith 
682295452b02SPatrick Sanan    Notes:
682395452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
68247db568b7SBarry Smith 
682580666b62SBarry Smith    Level: intermediate
682680666b62SBarry Smith 
682780666b62SBarry Smith .keywords: TS,  vector, monitor, view
682880666b62SBarry Smith 
6829a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
683080666b62SBarry Smith @*/
68317684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
683280666b62SBarry Smith {
683380666b62SBarry Smith   PetscInt          i;
6834a66092f1SBarry Smith   PetscErrorCode    ierr;
683580666b62SBarry Smith 
683680666b62SBarry Smith   PetscFunctionBegin;
683780666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
683880666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
68395537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
684080666b62SBarry Smith     }
684180666b62SBarry Smith   }
684280666b62SBarry Smith   PetscFunctionReturn(0);
684380666b62SBarry Smith }
684480666b62SBarry Smith 
6845e673d494SBarry Smith /*@C
6846e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6847e673d494SBarry Smith 
6848e673d494SBarry Smith    Collective on TSLGCtx
6849e673d494SBarry Smith 
6850e673d494SBarry Smith    Input Parameters:
6851e673d494SBarry Smith +  ts - the TS context
6852e673d494SBarry Smith .  transform - the transform function
68537684fa3eSBarry Smith .  destroy - function to destroy the optional context
6854e673d494SBarry Smith -  ctx - optional context used by transform function
6855e673d494SBarry Smith 
6856e673d494SBarry Smith    Level: intermediate
6857e673d494SBarry Smith 
6858e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6859e673d494SBarry Smith 
6860a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6861e673d494SBarry Smith @*/
68627684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6863e673d494SBarry Smith {
6864e673d494SBarry Smith   PetscFunctionBegin;
6865e673d494SBarry Smith   ctx->transform    = transform;
68667684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6867e673d494SBarry Smith   ctx->transformctx = tctx;
6868e673d494SBarry Smith   PetscFunctionReturn(0);
6869e673d494SBarry Smith }
6870e673d494SBarry Smith 
6871ef20d060SBarry Smith /*@C
68728b668821SLisandro Dalcin    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error
6873ef20d060SBarry Smith        in a time based line graph
6874ef20d060SBarry Smith 
6875ef20d060SBarry Smith    Collective on TS
6876ef20d060SBarry Smith 
6877ef20d060SBarry Smith    Input Parameters:
6878ef20d060SBarry Smith +  ts - the TS context
6879ef20d060SBarry Smith .  step - current time-step
6880ef20d060SBarry Smith .  ptime - current time
68817db568b7SBarry Smith .  u - current solution
68827db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6883ef20d060SBarry Smith 
6884ef20d060SBarry Smith    Level: intermediate
6885ef20d060SBarry Smith 
688695452b02SPatrick Sanan    Notes:
688795452b02SPatrick Sanan     Each process in a parallel run displays its component errors in a separate window
6888abd5a294SJed Brown 
6889abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6890abd5a294SJed Brown 
6891abd5a294SJed Brown    Options Database Keys:
68924f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6893ef20d060SBarry Smith 
6894ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
6895ef20d060SBarry Smith 
6896abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6897ef20d060SBarry Smith @*/
68980910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6899ef20d060SBarry Smith {
6900ef20d060SBarry Smith   PetscErrorCode    ierr;
69010b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6902ef20d060SBarry Smith   const PetscScalar *yy;
6903ef20d060SBarry Smith   Vec               y;
6904ef20d060SBarry Smith 
6905ef20d060SBarry Smith   PetscFunctionBegin;
6906a9f9c1f6SBarry Smith   if (!step) {
6907a9f9c1f6SBarry Smith     PetscDrawAxis axis;
69086934998bSLisandro Dalcin     PetscInt      dim;
6909a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
69108b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr);
69110910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6912a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6913a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6914a9f9c1f6SBarry Smith   }
69150910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6916ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
69170910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6918ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6919e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6920e3efe391SJed Brown   {
6921e3efe391SJed Brown     PetscReal *yreal;
6922e3efe391SJed Brown     PetscInt  i,n;
6923e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6924785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6925e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
69260b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6927e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6928e3efe391SJed Brown   }
6929e3efe391SJed Brown #else
69300b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6931e3efe391SJed Brown #endif
6932ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6933ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6934b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
69350b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69366934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
69373923b477SBarry Smith   }
6938ef20d060SBarry Smith   PetscFunctionReturn(0);
6939ef20d060SBarry Smith }
6940ef20d060SBarry Smith 
69417cf37e64SBarry Smith /*@C
69427cf37e64SBarry Smith    TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep
69437cf37e64SBarry Smith 
69447cf37e64SBarry Smith    Collective on TS
69457cf37e64SBarry Smith 
69467cf37e64SBarry Smith    Input Parameters:
69477cf37e64SBarry Smith +  ts - the TS context
69487cf37e64SBarry Smith .  step - current time-step
69497cf37e64SBarry Smith .  ptime - current time
69507cf37e64SBarry Smith .  u - current solution
69517cf37e64SBarry Smith -  dctx - unused context
69527cf37e64SBarry Smith 
69537cf37e64SBarry Smith    Level: intermediate
69547cf37e64SBarry Smith 
69557cf37e64SBarry Smith    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
69567cf37e64SBarry Smith 
69577cf37e64SBarry Smith    Options Database Keys:
69587cf37e64SBarry Smith .  -ts_monitor_error - create a graphical monitor of error history
69597cf37e64SBarry Smith 
69607cf37e64SBarry Smith .keywords: TS,  vector, monitor, view
69617cf37e64SBarry Smith 
69627cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
69637cf37e64SBarry Smith @*/
6964edbaebb3SBarry Smith PetscErrorCode  TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
69657cf37e64SBarry Smith {
69667cf37e64SBarry Smith   PetscErrorCode    ierr;
69677cf37e64SBarry Smith   Vec               y;
69687cf37e64SBarry Smith   PetscReal         nrm;
6969edbaebb3SBarry Smith   PetscBool         flg;
69707cf37e64SBarry Smith 
69717cf37e64SBarry Smith   PetscFunctionBegin;
69727cf37e64SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
69737cf37e64SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
69747cf37e64SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6975edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr);
6976edbaebb3SBarry Smith   if (flg) {
69777cf37e64SBarry Smith     ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
6978edbaebb3SBarry Smith     ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr);
6979edbaebb3SBarry Smith   }
6980edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr);
6981edbaebb3SBarry Smith   if (flg) {
6982edbaebb3SBarry Smith     ierr = VecView(y,vf->viewer);CHKERRQ(ierr);
6983edbaebb3SBarry Smith   }
6984edbaebb3SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
69857cf37e64SBarry Smith   PetscFunctionReturn(0);
69867cf37e64SBarry Smith }
69877cf37e64SBarry Smith 
6988201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6989201da799SBarry Smith {
6990201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6991201da799SBarry Smith   PetscReal      x   = ptime,y;
6992201da799SBarry Smith   PetscErrorCode ierr;
6993201da799SBarry Smith   PetscInt       its;
6994201da799SBarry Smith 
6995201da799SBarry Smith   PetscFunctionBegin;
699663e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
6997201da799SBarry Smith   if (!n) {
6998201da799SBarry Smith     PetscDrawAxis axis;
6999201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7000201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
7001201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7002201da799SBarry Smith     ctx->snes_its = 0;
7003201da799SBarry Smith   }
7004201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
7005201da799SBarry Smith   y    = its - ctx->snes_its;
7006201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
70073fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7008201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
70096934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7010201da799SBarry Smith   }
7011201da799SBarry Smith   ctx->snes_its = its;
7012201da799SBarry Smith   PetscFunctionReturn(0);
7013201da799SBarry Smith }
7014201da799SBarry Smith 
7015201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7016201da799SBarry Smith {
7017201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7018201da799SBarry Smith   PetscReal      x   = ptime,y;
7019201da799SBarry Smith   PetscErrorCode ierr;
7020201da799SBarry Smith   PetscInt       its;
7021201da799SBarry Smith 
7022201da799SBarry Smith   PetscFunctionBegin;
702363e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7024201da799SBarry Smith   if (!n) {
7025201da799SBarry Smith     PetscDrawAxis axis;
7026201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7027201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
7028201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7029201da799SBarry Smith     ctx->ksp_its = 0;
7030201da799SBarry Smith   }
7031201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
7032201da799SBarry Smith   y    = its - ctx->ksp_its;
7033201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
703499fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7035201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
70366934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7037201da799SBarry Smith   }
7038201da799SBarry Smith   ctx->ksp_its = its;
7039201da799SBarry Smith   PetscFunctionReturn(0);
7040201da799SBarry Smith }
7041f9c1d6abSBarry Smith 
7042f9c1d6abSBarry Smith /*@
7043f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
7044f9c1d6abSBarry Smith 
7045f9c1d6abSBarry Smith    Collective on TS and Vec
7046f9c1d6abSBarry Smith 
7047f9c1d6abSBarry Smith    Input Parameters:
7048f9c1d6abSBarry Smith +  ts - the TS context
7049f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
7050f9c1d6abSBarry Smith 
7051f9c1d6abSBarry Smith    Output Parameters:
7052f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
7053f9c1d6abSBarry Smith 
7054f9c1d6abSBarry Smith    Level: developer
7055f9c1d6abSBarry Smith 
7056f9c1d6abSBarry Smith .keywords: TS, compute
7057f9c1d6abSBarry Smith 
7058f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
7059f9c1d6abSBarry Smith @*/
7060f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7061f9c1d6abSBarry Smith {
7062f9c1d6abSBarry Smith   PetscErrorCode ierr;
7063f9c1d6abSBarry Smith 
7064f9c1d6abSBarry Smith   PetscFunctionBegin;
7065f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7066ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7067f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
7068f9c1d6abSBarry Smith   PetscFunctionReturn(0);
7069f9c1d6abSBarry Smith }
707024655328SShri 
7071b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7072b3d3934dSBarry Smith /*@C
7073b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7074b3d3934dSBarry Smith 
7075b3d3934dSBarry Smith    Collective on TS
7076b3d3934dSBarry Smith 
7077b3d3934dSBarry Smith    Input Parameters:
7078b3d3934dSBarry Smith .  ts  - the ODE solver object
7079b3d3934dSBarry Smith 
7080b3d3934dSBarry Smith    Output Parameter:
7081b3d3934dSBarry Smith .  ctx - the context
7082b3d3934dSBarry Smith 
7083b3d3934dSBarry Smith    Level: intermediate
7084b3d3934dSBarry Smith 
7085b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
7086b3d3934dSBarry Smith 
7087b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7088b3d3934dSBarry Smith 
7089b3d3934dSBarry Smith @*/
7090b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7091b3d3934dSBarry Smith {
7092b3d3934dSBarry Smith   PetscErrorCode ierr;
7093b3d3934dSBarry Smith 
7094b3d3934dSBarry Smith   PetscFunctionBegin;
7095a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7096b3d3934dSBarry Smith   PetscFunctionReturn(0);
7097b3d3934dSBarry Smith }
7098b3d3934dSBarry Smith 
7099b3d3934dSBarry Smith /*@C
7100b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7101b3d3934dSBarry Smith 
7102b3d3934dSBarry Smith    Collective on TS
7103b3d3934dSBarry Smith 
7104b3d3934dSBarry Smith    Input Parameters:
7105b3d3934dSBarry Smith +  ts - the TS context
7106b3d3934dSBarry Smith .  step - current time-step
7107b3d3934dSBarry Smith .  ptime - current time
71087db568b7SBarry Smith .  u  - current solution
71097db568b7SBarry Smith -  dctx - the envelope context
7110b3d3934dSBarry Smith 
7111b3d3934dSBarry Smith    Options Database:
7112b3d3934dSBarry Smith .  -ts_monitor_envelope
7113b3d3934dSBarry Smith 
7114b3d3934dSBarry Smith    Level: intermediate
7115b3d3934dSBarry Smith 
711695452b02SPatrick Sanan    Notes:
711795452b02SPatrick Sanan     after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7118b3d3934dSBarry Smith 
7119b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7120b3d3934dSBarry Smith 
71217db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7122b3d3934dSBarry Smith @*/
71237db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7124b3d3934dSBarry Smith {
7125b3d3934dSBarry Smith   PetscErrorCode       ierr;
71267db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7127b3d3934dSBarry Smith 
7128b3d3934dSBarry Smith   PetscFunctionBegin;
7129b3d3934dSBarry Smith   if (!ctx->max) {
7130b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7131b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7132b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7133b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7134b3d3934dSBarry Smith   } else {
7135b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7136b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7137b3d3934dSBarry Smith   }
7138b3d3934dSBarry Smith   PetscFunctionReturn(0);
7139b3d3934dSBarry Smith }
7140b3d3934dSBarry Smith 
7141b3d3934dSBarry Smith /*@C
7142b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7143b3d3934dSBarry Smith 
7144b3d3934dSBarry Smith    Collective on TS
7145b3d3934dSBarry Smith 
7146b3d3934dSBarry Smith    Input Parameter:
7147b3d3934dSBarry Smith .  ts - the TS context
7148b3d3934dSBarry Smith 
7149b3d3934dSBarry Smith    Output Parameter:
7150b3d3934dSBarry Smith +  max - the maximum values
7151b3d3934dSBarry Smith -  min - the minimum values
7152b3d3934dSBarry Smith 
715395452b02SPatrick Sanan    Notes:
715495452b02SPatrick Sanan     If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
71557db568b7SBarry Smith 
7156b3d3934dSBarry Smith    Level: intermediate
7157b3d3934dSBarry Smith 
7158b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7159b3d3934dSBarry Smith 
7160b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7161b3d3934dSBarry Smith @*/
7162b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7163b3d3934dSBarry Smith {
7164b3d3934dSBarry Smith   PetscInt i;
7165b3d3934dSBarry Smith 
7166b3d3934dSBarry Smith   PetscFunctionBegin;
7167b3d3934dSBarry Smith   if (max) *max = NULL;
7168b3d3934dSBarry Smith   if (min) *min = NULL;
7169b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7170b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
71715537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7172b3d3934dSBarry Smith       if (max) *max = ctx->max;
7173b3d3934dSBarry Smith       if (min) *min = ctx->min;
7174b3d3934dSBarry Smith       break;
7175b3d3934dSBarry Smith     }
7176b3d3934dSBarry Smith   }
7177b3d3934dSBarry Smith   PetscFunctionReturn(0);
7178b3d3934dSBarry Smith }
7179b3d3934dSBarry Smith 
7180b3d3934dSBarry Smith /*@C
7181b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7182b3d3934dSBarry Smith 
7183b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7184b3d3934dSBarry Smith 
7185b3d3934dSBarry Smith    Input Parameter:
7186b3d3934dSBarry Smith .  ctx - the monitor context
7187b3d3934dSBarry Smith 
7188b3d3934dSBarry Smith    Level: intermediate
7189b3d3934dSBarry Smith 
7190b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
7191b3d3934dSBarry Smith 
71927db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7193b3d3934dSBarry Smith @*/
7194b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7195b3d3934dSBarry Smith {
7196b3d3934dSBarry Smith   PetscErrorCode ierr;
7197b3d3934dSBarry Smith 
7198b3d3934dSBarry Smith   PetscFunctionBegin;
7199b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7200b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7201b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7202b3d3934dSBarry Smith   PetscFunctionReturn(0);
7203b3d3934dSBarry Smith }
7204f2dee214SBarry Smith 
720524655328SShri /*@
7206dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
7207dcb233daSLisandro Dalcin 
7208dcb233daSLisandro Dalcin    Collective on TS
7209dcb233daSLisandro Dalcin 
7210dcb233daSLisandro Dalcin    Input Parameter:
7211dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
7212dcb233daSLisandro Dalcin 
7213dcb233daSLisandro Dalcin    Level: advanced
7214dcb233daSLisandro Dalcin 
7215dcb233daSLisandro Dalcin    Notes:
7216dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
7217dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
7218dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
7219dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
7220dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
7221dcb233daSLisandro Dalcin    discontinuous source terms).
7222dcb233daSLisandro Dalcin 
7223dcb233daSLisandro Dalcin .keywords: TS, timestep, restart
7224dcb233daSLisandro Dalcin 
7225dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
7226dcb233daSLisandro Dalcin @*/
7227dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
7228dcb233daSLisandro Dalcin {
7229dcb233daSLisandro Dalcin   PetscFunctionBegin;
7230dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7231dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
7232dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
7233dcb233daSLisandro Dalcin }
7234dcb233daSLisandro Dalcin 
7235dcb233daSLisandro Dalcin /*@
723624655328SShri    TSRollBack - Rolls back one time step
723724655328SShri 
723824655328SShri    Collective on TS
723924655328SShri 
724024655328SShri    Input Parameter:
724124655328SShri .  ts - the TS context obtained from TSCreate()
724224655328SShri 
724324655328SShri    Level: advanced
724424655328SShri 
724524655328SShri .keywords: TS, timestep, rollback
724624655328SShri 
724724655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
724824655328SShri @*/
724924655328SShri PetscErrorCode  TSRollBack(TS ts)
725024655328SShri {
725124655328SShri   PetscErrorCode ierr;
725224655328SShri 
725324655328SShri   PetscFunctionBegin;
725424655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7255b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
725624655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
725724655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
725824655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
725924655328SShri   ts->ptime = ts->ptime_prev;
7260be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
72612808aa04SLisandro Dalcin   ts->steps--;
7262b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
726324655328SShri   PetscFunctionReturn(0);
726424655328SShri }
7265aeb4809dSShri Abhyankar 
7266ff22ae23SHong Zhang /*@
7267ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7268ff22ae23SHong Zhang 
7269ff22ae23SHong Zhang    Input Parameter:
7270ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7271ff22ae23SHong Zhang 
7272ff22ae23SHong Zhang    Level: advanced
7273ff22ae23SHong Zhang 
7274ff22ae23SHong Zhang .keywords: TS, getstages
7275ff22ae23SHong Zhang 
7276ff22ae23SHong Zhang .seealso: TSCreate()
7277ff22ae23SHong Zhang @*/
7278ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7279ff22ae23SHong Zhang {
7280ff22ae23SHong Zhang   PetscErrorCode ierr;
7281ff22ae23SHong Zhang 
7282ff22ae23SHong Zhang   PetscFunctionBegin;
7283ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7284ff22ae23SHong Zhang   PetscValidPointer(ns,2);
7285ff22ae23SHong Zhang 
7286ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
7287ff22ae23SHong Zhang   else {
7288ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7289ff22ae23SHong Zhang   }
7290ff22ae23SHong Zhang   PetscFunctionReturn(0);
7291ff22ae23SHong Zhang }
7292ff22ae23SHong Zhang 
7293847ff0e1SMatthew G. Knepley /*@C
7294847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7295847ff0e1SMatthew G. Knepley 
7296847ff0e1SMatthew G. Knepley   Collective on SNES
7297847ff0e1SMatthew G. Knepley 
7298847ff0e1SMatthew G. Knepley   Input Parameters:
7299847ff0e1SMatthew G. Knepley + ts - the TS context
7300847ff0e1SMatthew G. Knepley . t - current timestep
7301847ff0e1SMatthew G. Knepley . U - state vector
7302847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7303847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7304847ff0e1SMatthew G. Knepley - ctx - an optional user context
7305847ff0e1SMatthew G. Knepley 
7306847ff0e1SMatthew G. Knepley   Output Parameters:
7307847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7308847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7309847ff0e1SMatthew G. Knepley 
7310847ff0e1SMatthew G. Knepley   Level: intermediate
7311847ff0e1SMatthew G. Knepley 
7312847ff0e1SMatthew G. Knepley   Notes:
7313847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7314847ff0e1SMatthew G. Knepley 
7315847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7316847ff0e1SMatthew G. Knepley 
7317847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7318847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7319847ff0e1SMatthew G. Knepley 
7320847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7321847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7322847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7323847ff0e1SMatthew G. Knepley 
7324847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
7325847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7326847ff0e1SMatthew G. Knepley @*/
7327847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7328847ff0e1SMatthew G. Knepley {
7329847ff0e1SMatthew G. Knepley   SNES           snes;
7330847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7331847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7332847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7333847ff0e1SMatthew G. Knepley 
7334847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7335c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7336847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7337847ff0e1SMatthew G. Knepley   if (!color) {
7338847ff0e1SMatthew G. Knepley     DM         dm;
7339847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7340847ff0e1SMatthew G. Knepley 
7341847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7342847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7343847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7344847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7345847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7346847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7347847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7348847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7349847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7350847ff0e1SMatthew G. Knepley     } else {
7351847ff0e1SMatthew G. Knepley       MatColoring mc;
7352847ff0e1SMatthew G. Knepley 
7353847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7354847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7355847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7356847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7357847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7358847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7359847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7360847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7361847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7362847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7363847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7364847ff0e1SMatthew G. Knepley     }
7365847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7366847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7367847ff0e1SMatthew G. Knepley   }
7368847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7369847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7370847ff0e1SMatthew G. Knepley   if (J != B) {
7371847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7372847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7373847ff0e1SMatthew G. Knepley   }
7374847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7375847ff0e1SMatthew G. Knepley }
737693b34091SDebojyoti Ghosh 
7377cb9d8021SPierre Barbier de Reuille /*@
7378cb9d8021SPierre Barbier de Reuille     TSSetFunctionDomainError - Set the function testing if the current state vector is valid
7379cb9d8021SPierre Barbier de Reuille 
7380cb9d8021SPierre Barbier de Reuille     Input Parameters:
7381cb9d8021SPierre Barbier de Reuille     ts - the TS context
7382cb9d8021SPierre Barbier de Reuille     func - function called within TSFunctionDomainError
7383cb9d8021SPierre Barbier de Reuille 
7384cb9d8021SPierre Barbier de Reuille     Level: intermediate
7385cb9d8021SPierre Barbier de Reuille 
7386cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain
7387cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError()
7388cb9d8021SPierre Barbier de Reuille @*/
7389cb9d8021SPierre Barbier de Reuille 
7390d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7391cb9d8021SPierre Barbier de Reuille {
7392cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7393cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7394cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7395cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7396cb9d8021SPierre Barbier de Reuille }
7397cb9d8021SPierre Barbier de Reuille 
7398cb9d8021SPierre Barbier de Reuille /*@
7399cb9d8021SPierre Barbier de Reuille     TSFunctionDomainError - Check if the current state is valid
7400cb9d8021SPierre Barbier de Reuille 
7401cb9d8021SPierre Barbier de Reuille     Input Parameters:
7402cb9d8021SPierre Barbier de Reuille     ts - the TS context
7403cb9d8021SPierre Barbier de Reuille     stagetime - time of the simulation
7404d183316bSPierre Barbier de Reuille     Y - state vector to check.
7405cb9d8021SPierre Barbier de Reuille 
7406cb9d8021SPierre Barbier de Reuille     Output Parameter:
7407cb9d8021SPierre Barbier de Reuille     accept - Set to PETSC_FALSE if the current state vector is valid.
7408cb9d8021SPierre Barbier de Reuille 
7409cb9d8021SPierre Barbier de Reuille     Note:
7410cb9d8021SPierre Barbier de Reuille     This function should be used to ensure the state is in a valid part of the space.
7411cb9d8021SPierre Barbier de Reuille     For example, one can ensure here all values are positive.
741296a0c994SBarry Smith 
741396a0c994SBarry Smith     Level: advanced
7414cb9d8021SPierre Barbier de Reuille @*/
7415d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7416cb9d8021SPierre Barbier de Reuille {
7417cb9d8021SPierre Barbier de Reuille   PetscErrorCode ierr;
7418cb9d8021SPierre Barbier de Reuille 
7419cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7420cb9d8021SPierre Barbier de Reuille 
7421cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7422cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7423cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7424d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7425cb9d8021SPierre Barbier de Reuille   }
7426cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7427cb9d8021SPierre Barbier de Reuille }
74281ceb14c0SBarry Smith 
742993b34091SDebojyoti Ghosh /*@C
7430e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
743193b34091SDebojyoti Ghosh 
743293b34091SDebojyoti Ghosh   Collective on MPI_Comm
743393b34091SDebojyoti Ghosh 
743493b34091SDebojyoti Ghosh   Input Parameter:
743593b34091SDebojyoti Ghosh . tsin    - The input TS
743693b34091SDebojyoti Ghosh 
743793b34091SDebojyoti Ghosh   Output Parameter:
7438e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
743993b34091SDebojyoti Ghosh 
74405eca1a21SEmil Constantinescu   Notes:
74415eca1a21SEmil 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.
74425eca1a21SEmil Constantinescu 
7443baa10174SEmil 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);
74445eca1a21SEmil Constantinescu 
74455eca1a21SEmil Constantinescu   Level: developer
744693b34091SDebojyoti Ghosh 
7447e5168f73SEmil Constantinescu .keywords: TS, clone
7448e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
744993b34091SDebojyoti Ghosh @*/
7450baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
745193b34091SDebojyoti Ghosh {
745293b34091SDebojyoti Ghosh   TS             t;
745393b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7454dc846ba4SSatish Balay   SNES           snes_start;
7455dc846ba4SSatish Balay   DM             dm;
7456dc846ba4SSatish Balay   TSType         type;
745793b34091SDebojyoti Ghosh 
745893b34091SDebojyoti Ghosh   PetscFunctionBegin;
745993b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
746093b34091SDebojyoti Ghosh   *tsout = NULL;
746193b34091SDebojyoti Ghosh 
74627a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
746393b34091SDebojyoti Ghosh 
746493b34091SDebojyoti Ghosh   /* General TS description */
746593b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
746693b34091SDebojyoti Ghosh   t->setupcalled       = 0;
746793b34091SDebojyoti Ghosh   t->ksp_its           = 0;
746893b34091SDebojyoti Ghosh   t->snes_its          = 0;
746993b34091SDebojyoti Ghosh   t->nwork             = 0;
747093b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
747193b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
747293b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
747393b34091SDebojyoti Ghosh 
747434561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
747534561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7476d15a3a53SEmil Constantinescu 
747793b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
747893b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
747993b34091SDebojyoti Ghosh 
748093b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
748151699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
748293b34091SDebojyoti Ghosh 
7483e7069c78SShri   t->trajectory = tsin->trajectory;
7484e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7485e7069c78SShri 
7486e7069c78SShri   t->event = tsin->event;
74876b10a48eSSatish Balay   if (t->event) t->event->refct++;
7488e7069c78SShri 
748993b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
749093b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7491e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
749293b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
749393b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
749493b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
749593b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
749693b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
749793b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
749893b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
749993b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
750093b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
750193b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
750293b34091SDebojyoti Ghosh 
750393b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
750493b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
750593b34091SDebojyoti Ghosh 
750693b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
750793b34091SDebojyoti Ghosh 
750893b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
750993b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
751093b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
751193b34091SDebojyoti Ghosh 
751293b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
751393b34091SDebojyoti Ghosh 
75140d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
75150d4fed19SBarry Smith     PetscInt i;
75160d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
75170d4fed19SBarry Smith     for (i=0; i<10; i++) {
75180d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
75190d4fed19SBarry Smith     }
75200d4fed19SBarry Smith   }
752193b34091SDebojyoti Ghosh   *tsout = t;
752293b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
752393b34091SDebojyoti Ghosh }
7524f3b1f45cSBarry Smith 
7525f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
7526f3b1f45cSBarry Smith {
7527f3b1f45cSBarry Smith   PetscErrorCode ierr;
7528f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
7529f3b1f45cSBarry Smith 
7530f3b1f45cSBarry Smith   PetscFunctionBegin;
7531f3b1f45cSBarry Smith   ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr);
7532f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7533f3b1f45cSBarry Smith }
7534f3b1f45cSBarry Smith 
7535f3b1f45cSBarry Smith /*@
7536f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
7537f3b1f45cSBarry Smith 
7538f3b1f45cSBarry Smith    Logically Collective on TS and Mat
7539f3b1f45cSBarry Smith 
7540f3b1f45cSBarry Smith     Input Parameters:
7541f3b1f45cSBarry Smith     TS - the time stepping routine
7542f3b1f45cSBarry Smith 
7543f3b1f45cSBarry Smith    Output Parameter:
7544f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7545f3b1f45cSBarry Smith 
7546f3b1f45cSBarry Smith    Options Database:
7547f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
7548f3b1f45cSBarry Smith 
7549f3b1f45cSBarry Smith    Level: advanced
7550f3b1f45cSBarry Smith 
755195452b02SPatrick Sanan    Notes:
755295452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7553f3b1f45cSBarry Smith 
7554f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
7555f3b1f45cSBarry Smith @*/
7556f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
7557f3b1f45cSBarry Smith {
7558f3b1f45cSBarry Smith   Mat            J,B;
7559f3b1f45cSBarry Smith   PetscErrorCode ierr;
7560f3b1f45cSBarry Smith   TSRHSJacobian  func;
7561f3b1f45cSBarry Smith   void*          ctx;
7562f3b1f45cSBarry Smith 
7563f3b1f45cSBarry Smith   PetscFunctionBegin;
7564f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7565f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7566f3b1f45cSBarry Smith   ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7567f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7568f3b1f45cSBarry Smith }
7569f3b1f45cSBarry Smith 
7570f3b1f45cSBarry Smith /*@C
7571f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
7572f3b1f45cSBarry Smith 
7573f3b1f45cSBarry Smith    Logically Collective on TS and Mat
7574f3b1f45cSBarry Smith 
7575f3b1f45cSBarry Smith     Input Parameters:
7576f3b1f45cSBarry Smith     TS - the time stepping routine
7577f3b1f45cSBarry Smith 
7578f3b1f45cSBarry Smith    Output Parameter:
7579f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7580f3b1f45cSBarry Smith 
7581f3b1f45cSBarry Smith    Options Database:
7582f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
7583f3b1f45cSBarry Smith 
758495452b02SPatrick Sanan    Notes:
758595452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7586f3b1f45cSBarry Smith 
7587f3b1f45cSBarry Smith    Level: advanced
7588f3b1f45cSBarry Smith 
7589f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
7590f3b1f45cSBarry Smith @*/
7591f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
7592f3b1f45cSBarry Smith {
7593f3b1f45cSBarry Smith   Mat            J,B;
7594f3b1f45cSBarry Smith   PetscErrorCode ierr;
7595f3b1f45cSBarry Smith   void           *ctx;
7596f3b1f45cSBarry Smith   TSRHSJacobian  func;
7597f3b1f45cSBarry Smith 
7598f3b1f45cSBarry Smith   PetscFunctionBegin;
7599f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7600f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7601f3b1f45cSBarry Smith   ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7602f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7603f3b1f45cSBarry Smith }
7604