xref: /petsc/src/ts/interface/ts.c (revision 76cddca14ae96d212ef49f8b4b971d466bb67ce2)
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 
71c167fc2SEmil Constantinescu #define SkipSmallValue(a,b,tol) if(PetscAbsScalar(a)< tol || PetscAbsScalar(b)< tol) continue;
81c167fc2SEmil Constantinescu 
9d5ba7fb7SMatthew Knepley /* Logging support */
10d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
11a05bf03eSHong Zhang PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
12d405a339SMatthew Knepley 
13feed9e9dSBarry Smith const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0};
1449354f04SShri Abhyankar 
151c167fc2SEmil Constantinescu 
16fde5950dSBarry Smith /*@C
17fde5950dSBarry Smith    TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
18fde5950dSBarry Smith 
19fde5950dSBarry Smith    Collective on TS
20fde5950dSBarry Smith 
21fde5950dSBarry Smith    Input Parameters:
22fde5950dSBarry Smith +  ts - TS object you wish to monitor
23fde5950dSBarry Smith .  name - the monitor type one is seeking
24fde5950dSBarry Smith .  help - message indicating what monitoring is done
25fde5950dSBarry Smith .  manual - manual page for the monitor
26fde5950dSBarry Smith .  monitor - the monitor function
27fde5950dSBarry 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
28fde5950dSBarry Smith 
29fde5950dSBarry Smith    Level: developer
30fde5950dSBarry Smith 
31fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
32fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
33fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
34fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
35fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
36fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
37fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
38fde5950dSBarry Smith @*/
39721cd6eeSBarry 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*))
40fde5950dSBarry Smith {
41fde5950dSBarry Smith   PetscErrorCode    ierr;
42fde5950dSBarry Smith   PetscViewer       viewer;
43fde5950dSBarry Smith   PetscViewerFormat format;
44fde5950dSBarry Smith   PetscBool         flg;
45fde5950dSBarry Smith 
46fde5950dSBarry Smith   PetscFunctionBegin;
4716413a6aSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject) ts)->options,((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
48fde5950dSBarry Smith   if (flg) {
49721cd6eeSBarry Smith     PetscViewerAndFormat *vf;
50721cd6eeSBarry Smith     ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr);
51721cd6eeSBarry Smith     ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr);
52fde5950dSBarry Smith     if (monitorsetup) {
53721cd6eeSBarry Smith       ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr);
54fde5950dSBarry Smith     }
55721cd6eeSBarry Smith     ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
56fde5950dSBarry Smith   }
57fde5950dSBarry Smith   PetscFunctionReturn(0);
58fde5950dSBarry Smith }
59fde5950dSBarry Smith 
602ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type)
612ffb9264SLisandro Dalcin {
622ffb9264SLisandro Dalcin   PetscErrorCode ierr;
632ffb9264SLisandro Dalcin 
642ffb9264SLisandro Dalcin   PetscFunctionBegin;
65b92453a8SLisandro Dalcin   PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1);
66b92453a8SLisandro Dalcin   PetscValidCharPointer(default_type,2);
672ffb9264SLisandro Dalcin   if (!((PetscObject)adapt)->type_name) {
682ffb9264SLisandro Dalcin     ierr = TSAdaptSetType(adapt,default_type);CHKERRQ(ierr);
692ffb9264SLisandro Dalcin   }
702ffb9264SLisandro Dalcin   PetscFunctionReturn(0);
712ffb9264SLisandro Dalcin }
722ffb9264SLisandro Dalcin 
73bdad233fSMatthew Knepley /*@
74bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
75bdad233fSMatthew Knepley 
76bdad233fSMatthew Knepley    Collective on TS
77bdad233fSMatthew Knepley 
78bdad233fSMatthew Knepley    Input Parameter:
79bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
80bdad233fSMatthew Knepley 
81bdad233fSMatthew Knepley    Options Database Keys:
82e49d4f37SHong Zhang +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE, TSBSYMP
83ef222394SBarry Smith .  -ts_save_trajectory - checkpoint the solution at each time-step
84ef85077eSLisandro Dalcin .  -ts_max_time <time> - maximum time to compute to
85ef85077eSLisandro Dalcin .  -ts_max_steps <steps> - maximum number of time-steps to take
86ef85077eSLisandro Dalcin .  -ts_init_time <time> - initial time to start computation
874dc72f7fSBarry Smith .  -ts_final_time <time> - final time to compute to (deprecated: use -ts_max_time)
883e4cdcaaSBarry Smith .  -ts_dt <dt> - initial time step
89a3cdaa26SBarry 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
90a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
91a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
92a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
93a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
94a3cdaa26SBarry Smith .  -ts_atol <atol> Absolute tolerance for local truncation error
95f3b1f45cSBarry Smith .  -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function
96f3b1f45cSBarry 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
97ef222394SBarry Smith .  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
98847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
99bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
100de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
101de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
1027cf37e64SBarry Smith .  -ts_monitor_error - Monitors norm of error
1036934998bSLisandro Dalcin .  -ts_monitor_lg_timestep - Monitor timestep size graphically
1048b668821SLisandro Dalcin .  -ts_monitor_lg_timestep_log - Monitor log timestep size graphically
105de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
106de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
107de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
108de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
1093e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
1103e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
111fde5950dSBarry Smith .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
112e4160dc7SJulian Andrej .  -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu)
113110eb670SHong Zhang .  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
11453ea634cSHong Zhang 
11591b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
116bdad233fSMatthew Knepley 
117bdad233fSMatthew Knepley    Level: beginner
118bdad233fSMatthew Knepley 
119bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
120bdad233fSMatthew Knepley 
121a313700dSBarry Smith .seealso: TSGetType()
122bdad233fSMatthew Knepley @*/
1237087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
124bdad233fSMatthew Knepley {
125bc952696SBarry Smith   PetscBool              opt,flg,tflg;
126dfbe8321SBarry Smith   PetscErrorCode         ierr;
127eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
12831748224SBarry Smith   PetscReal              time_step;
12949354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
130d1212d36SBarry Smith   char                   dir[16];
131cd11d68dSLisandro Dalcin   TSIFunction            ifun;
1326991f827SBarry Smith   const char             *defaultType;
1336991f827SBarry Smith   char                   typeName[256];
134bdad233fSMatthew Knepley 
135bdad233fSMatthew Knepley   PetscFunctionBegin;
1360700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1376991f827SBarry Smith 
1386991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
139cd11d68dSLisandro Dalcin   ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
140cd11d68dSLisandro Dalcin 
141cd11d68dSLisandro Dalcin   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
1421ef27442SStefano Zampini   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
1431ef27442SStefano Zampini   else defaultType = ifun ? TSBEULER : TSEULER;
1446991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr);
1456991f827SBarry Smith   if (opt) {
1466991f827SBarry Smith     ierr = TSSetType(ts,typeName);CHKERRQ(ierr);
1476991f827SBarry Smith   } else {
1486991f827SBarry Smith     ierr = TSSetType(ts,defaultType);CHKERRQ(ierr);
1496991f827SBarry Smith   }
150bdad233fSMatthew Knepley 
151bdad233fSMatthew Knepley   /* Handle generic TS options */
1524dc72f7fSBarry Smith   ierr = PetscOptionsDeprecated("-ts_final_time","-ts_max_time","3.10",NULL);CHKERRQ(ierr);
153ef85077eSLisandro Dalcin   ierr = PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
15419eac22cSLisandro Dalcin   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1550298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
15631748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
157cd11d68dSLisandro Dalcin   if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);}
15849354f04SShri 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);
15949354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1600298fd71SBarry 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);
1610298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1620298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1630298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1640298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
165bdad233fSMatthew Knepley 
166f3b1f45cSBarry 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);
167f3b1f45cSBarry 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);
1680fe4d17eSHong Zhang   ierr = PetscOptionsBool("-ts_use_splitrhsfunction","Use the split RHS function for multirate solvers ","TSSetUseSplitRHSFunction",ts->use_splitrhsfunction,&ts->use_splitrhsfunction,NULL);CHKERRQ(ierr);
16956f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
17056f85f32SBarry Smith   {
17156f85f32SBarry Smith   PetscBool set;
17256f85f32SBarry Smith   flg  = PETSC_FALSE;
17356f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
17456f85f32SBarry Smith   if (set) {
17556f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
17656f85f32SBarry Smith   }
17756f85f32SBarry Smith   }
17856f85f32SBarry Smith #endif
17956f85f32SBarry Smith 
180bdad233fSMatthew Knepley   /* Monitor options */
181cd11d68dSLisandro Dalcin   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr);
182cc9c3a59SBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_extreme","Monitor extreme values of the solution","TSMonitorExtreme",TSMonitorExtreme,NULL);CHKERRQ(ierr);
183fde5950dSBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr);
184fde5950dSBarry Smith 
1855180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1865180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1875180491cSLisandro Dalcin 
1884f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
189b3603a34SBarry Smith   if (opt) {
1900b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1913923b477SBarry Smith     PetscInt       howoften = 1;
192b3603a34SBarry Smith 
1930298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
1946ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
1954f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
196bdad233fSMatthew Knepley   }
1976ba87a44SLisandro Dalcin 
1984f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
199ef20d060SBarry Smith   if (opt) {
2000b039ecaSBarry Smith     TSMonitorLGCtx ctx;
2013923b477SBarry Smith     PetscInt       howoften = 1;
202ef20d060SBarry Smith 
2030298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
2046ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2054f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
206ef20d060SBarry Smith   }
207edbaebb3SBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_error","View the error at each timestep","TSMonitorError",TSMonitorError,NULL);CHKERRQ(ierr);
2087cf37e64SBarry Smith 
2096934998bSLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2106934998bSLisandro Dalcin   if (opt) {
2116934998bSLisandro Dalcin     TSMonitorLGCtx ctx;
2126934998bSLisandro Dalcin     PetscInt       howoften = 1;
2136934998bSLisandro Dalcin 
2146934998bSLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2156ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2166934998bSLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2176934998bSLisandro Dalcin   }
2188b668821SLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2198b668821SLisandro Dalcin   if (opt) {
2208b668821SLisandro Dalcin     TSMonitorLGCtx ctx;
2218b668821SLisandro Dalcin     PetscInt       howoften = 1;
2228b668821SLisandro Dalcin 
2238b668821SLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2248b668821SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2258b668821SLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2268b668821SLisandro Dalcin     ctx->semilogy = PETSC_TRUE;
2278b668821SLisandro Dalcin   }
2288b668821SLisandro Dalcin 
229201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
230201da799SBarry Smith   if (opt) {
231201da799SBarry Smith     TSMonitorLGCtx ctx;
232201da799SBarry Smith     PetscInt       howoften = 1;
233201da799SBarry Smith 
2340298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2356ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
236201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
237201da799SBarry Smith   }
238201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
239201da799SBarry Smith   if (opt) {
240201da799SBarry Smith     TSMonitorLGCtx ctx;
241201da799SBarry Smith     PetscInt       howoften = 1;
242201da799SBarry Smith 
2430298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2446ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
245201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
246201da799SBarry Smith   }
2478189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
2488189c53fSBarry Smith   if (opt) {
2498189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2508189c53fSBarry Smith     PetscInt          howoften = 1;
2518189c53fSBarry Smith 
2520298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
2536934998bSLisandro Dalcin     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2548189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2558189c53fSBarry Smith   }
2560ec8ee2bSJoseph Pusztay   ierr = PetscOptionsName("-ts_monitor_sp_swarm","Display particle phase from the DMSwarm","TSMonitorSPSwarm",&opt);CHKERRQ(ierr);
2571b575b74SJoseph Pusztay   if (opt) {
2581b575b74SJoseph Pusztay     TSMonitorSPCtx  ctx;
2591b575b74SJoseph Pusztay     PetscInt        howoften = 1;
2600ec8ee2bSJoseph Pusztay     ierr = PetscOptionsInt("-ts_monitor_sp_swarm","Display particles phase from the DMSwarm","TSMonitorSPSwarm",howoften,&howoften,NULL);CHKERRQ(ierr);
2611b575b74SJoseph Pusztay     ierr = TSMonitorSPCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx);CHKERRQ(ierr);
2620ec8ee2bSJoseph Pusztay     ierr = TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscErrorCode (*)(void**))TSMonitorSPCtxDestroy);CHKERRQ(ierr);
2631b575b74SJoseph Pusztay   }
264ef20d060SBarry Smith   opt  = PETSC_FALSE;
2650dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
266a7cc72afSBarry Smith   if (opt) {
26783a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
26883a4ac43SBarry Smith     PetscInt         howoften = 1;
269a80ad3e0SBarry Smith 
2700298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
2710ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
27283a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
273bdad233fSMatthew Knepley   }
274fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2752d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2762d5ee99bSBarry Smith   if (opt) {
2772d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2782d5ee99bSBarry Smith     PetscReal        bounds[4];
2792d5ee99bSBarry Smith     PetscInt         n = 4;
2802d5ee99bSBarry Smith     PetscDraw        draw;
2816934998bSLisandro Dalcin     PetscDrawAxis    axis;
2822d5ee99bSBarry Smith 
2832d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
2842d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
2856934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr);
2862d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
2876934998bSLisandro Dalcin     ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr);
2886934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
2896934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
2902d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2912d5ee99bSBarry Smith   }
2922d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2930dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
2943a471f94SBarry Smith   if (opt) {
29583a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
29683a4ac43SBarry Smith     PetscInt         howoften = 1;
2973a471f94SBarry Smith 
2980298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
2990ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
30083a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3013a471f94SBarry Smith   }
3020ed3bfb6SBarry Smith   opt  = PETSC_FALSE;
3030ed3bfb6SBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt);CHKERRQ(ierr);
3040ed3bfb6SBarry Smith   if (opt) {
3050ed3bfb6SBarry Smith     TSMonitorDrawCtx ctx;
3060ed3bfb6SBarry Smith     PetscInt         howoften = 1;
3070ed3bfb6SBarry Smith 
3080ed3bfb6SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL);CHKERRQ(ierr);
3090ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
3100ed3bfb6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
3110ed3bfb6SBarry Smith   }
312fde5950dSBarry Smith 
313ed81e22dSJed Brown   opt  = PETSC_FALSE;
31491b97e58SBarry 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);
315ed81e22dSJed Brown   if (flg) {
316ed81e22dSJed Brown     const char *ptr,*ptr2;
317ed81e22dSJed Brown     char       *filetemplate;
318ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
319ed81e22dSJed Brown     /* Do some cursory validation of the input. */
320ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
321ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
322ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
323ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
324ce94432eSBarry 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");
325ed81e22dSJed Brown       if (ptr2) break;
326ed81e22dSJed Brown     }
327ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
328ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
329ed81e22dSJed Brown   }
330bdad233fSMatthew Knepley 
331d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
332d1212d36SBarry Smith   if (flg) {
333d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
334d1212d36SBarry Smith     int                  ray = 0;
335d1212d36SBarry Smith     DMDADirection        ddir;
336d1212d36SBarry Smith     DM                   da;
337d1212d36SBarry Smith     PetscMPIInt          rank;
338d1212d36SBarry Smith 
339ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
340d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
341d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
342ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
343d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
344d1212d36SBarry Smith 
345d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
346b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
347d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
348d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
349ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
350d1212d36SBarry Smith     if (!rank) {
351d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
352d1212d36SBarry Smith     }
35351b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
354d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
355d1212d36SBarry Smith   }
35651b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
35751b4a12fSMatthew G. Knepley   if (flg) {
35851b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
35951b4a12fSMatthew G. Knepley     int                 ray = 0;
36051b4a12fSMatthew G. Knepley     DMDADirection       ddir;
36151b4a12fSMatthew G. Knepley     DM                  da;
36251b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
363d1212d36SBarry Smith 
36451b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
36551b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
36651b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
36751b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
36851b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3691c3436cfSJed Brown 
37051b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
371b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
37251b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
37351b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
37451b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
37551b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
37651b4a12fSMatthew G. Knepley   }
377a7a1495cSBarry Smith 
378b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
379b3d3934dSBarry Smith   if (opt) {
380b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
381b3d3934dSBarry Smith 
382b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
383b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
384b3d3934dSBarry Smith   }
385b3d3934dSBarry Smith 
386847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
387847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
388847ff0e1SMatthew G. Knepley   if (flg) {
389847ff0e1SMatthew G. Knepley     DM   dm;
390847ff0e1SMatthew G. Knepley     DMTS tdm;
391847ff0e1SMatthew G. Knepley 
392847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
393847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
394847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
395847ff0e1SMatthew G. Knepley     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr);
396847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
397847ff0e1SMatthew G. Knepley   }
398847ff0e1SMatthew G. Knepley 
399d763cef2SBarry Smith   /* Handle specific TS options */
400d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
4016991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
402d763cef2SBarry Smith   }
403fbc52257SHong Zhang 
404a7bdc993SLisandro Dalcin   /* Handle TSAdapt options */
405a7bdc993SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
406a7bdc993SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
407a7bdc993SLisandro Dalcin   ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr);
408a7bdc993SLisandro Dalcin 
40968bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
4104f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
41168bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
41268bece0bSHong Zhang   if (tflg) {
41368bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
41468bece0bSHong Zhang   }
415a05bf03eSHong Zhang 
416a05bf03eSHong Zhang   ierr = TSAdjointSetFromOptions(PetscOptionsObject,ts);CHKERRQ(ierr);
417d763cef2SBarry Smith 
418d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4190633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr);
420fbc52257SHong Zhang   ierr = PetscOptionsEnd();CHKERRQ(ierr);
421d763cef2SBarry Smith 
4224f122a70SLisandro Dalcin   if (ts->trajectory) {
4234f122a70SLisandro Dalcin     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
424d763cef2SBarry Smith   }
42568bece0bSHong Zhang 
4261ef27442SStefano Zampini   /* why do we have to do this here and not during TSSetUp? */
4274f122a70SLisandro Dalcin   ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr);
4281ef27442SStefano Zampini   if (ts->problem_type == TS_LINEAR) {
4291ef27442SStefano Zampini     ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&flg,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
4301ef27442SStefano Zampini     if (!flg) { ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); }
4311ef27442SStefano Zampini   }
4324f122a70SLisandro Dalcin   ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
433d763cef2SBarry Smith   PetscFunctionReturn(0);
434d763cef2SBarry Smith }
435d763cef2SBarry Smith 
436d2daff3dSHong Zhang /*@
43778fbdcc8SBarry Smith    TSGetTrajectory - Gets the trajectory from a TS if it exists
43878fbdcc8SBarry Smith 
43978fbdcc8SBarry Smith    Collective on TS
44078fbdcc8SBarry Smith 
44178fbdcc8SBarry Smith    Input Parameters:
44278fbdcc8SBarry Smith .  ts - the TS context obtained from TSCreate()
44378fbdcc8SBarry Smith 
44478fbdcc8SBarry Smith    Output Parameters;
44578fbdcc8SBarry Smith .  tr - the TSTrajectory object, if it exists
44678fbdcc8SBarry Smith 
44778fbdcc8SBarry Smith    Note: This routine should be called after all TS options have been set
44878fbdcc8SBarry Smith 
44978fbdcc8SBarry Smith    Level: advanced
45078fbdcc8SBarry Smith 
45178fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate()
45278fbdcc8SBarry Smith 
45378fbdcc8SBarry Smith .keywords: TS, set, checkpoint,
45478fbdcc8SBarry Smith @*/
45578fbdcc8SBarry Smith PetscErrorCode  TSGetTrajectory(TS ts,TSTrajectory *tr)
45678fbdcc8SBarry Smith {
45778fbdcc8SBarry Smith   PetscFunctionBegin;
45878fbdcc8SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45978fbdcc8SBarry Smith   *tr = ts->trajectory;
46078fbdcc8SBarry Smith   PetscFunctionReturn(0);
46178fbdcc8SBarry Smith }
46278fbdcc8SBarry Smith 
46378fbdcc8SBarry Smith /*@
464bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
465d2daff3dSHong Zhang 
466d2daff3dSHong Zhang    Collective on TS
467d2daff3dSHong Zhang 
468d2daff3dSHong Zhang    Input Parameters:
469bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
470bc952696SBarry Smith 
47178fbdcc8SBarry Smith    Options Database:
47278fbdcc8SBarry Smith +  -ts_save_trajectory - saves the trajectory to a file
47378fbdcc8SBarry Smith -  -ts_trajectory_type type
47478fbdcc8SBarry Smith 
47568bece0bSHong Zhang Note: This routine should be called after all TS options have been set
476d2daff3dSHong Zhang 
477c3a89c15SBarry Smith     The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and
47878fbdcc8SBarry Smith    MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m
47978fbdcc8SBarry Smith 
480d2daff3dSHong Zhang    Level: intermediate
481d2daff3dSHong Zhang 
4822d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve()
483d2daff3dSHong Zhang 
484bc952696SBarry Smith .keywords: TS, set, checkpoint,
485d2daff3dSHong Zhang @*/
486bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
487d2daff3dSHong Zhang {
488bc952696SBarry Smith   PetscErrorCode ierr;
489bc952696SBarry Smith 
490d2daff3dSHong Zhang   PetscFunctionBegin;
491d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
492bc952696SBarry Smith   if (!ts->trajectory) {
493bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
494bc952696SBarry Smith   }
495d2daff3dSHong Zhang   PetscFunctionReturn(0);
496d2daff3dSHong Zhang }
497d2daff3dSHong Zhang 
498a7a1495cSBarry Smith /*@
4992d29f1f2SStefano Zampini    TSResetTrajectory - Destroys and recreates the internal TSTrajectory object
5002d29f1f2SStefano Zampini 
5012d29f1f2SStefano Zampini    Collective on TS
5022d29f1f2SStefano Zampini 
5032d29f1f2SStefano Zampini    Input Parameters:
5042d29f1f2SStefano Zampini .  ts - the TS context obtained from TSCreate()
5052d29f1f2SStefano Zampini 
5062d29f1f2SStefano Zampini    Level: intermediate
5072d29f1f2SStefano Zampini 
5082d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve()
5092d29f1f2SStefano Zampini 
5102d29f1f2SStefano Zampini .keywords: TS, set, checkpoint,
5112d29f1f2SStefano Zampini @*/
5122d29f1f2SStefano Zampini PetscErrorCode  TSResetTrajectory(TS ts)
5132d29f1f2SStefano Zampini {
5142d29f1f2SStefano Zampini   PetscErrorCode ierr;
5152d29f1f2SStefano Zampini 
5162d29f1f2SStefano Zampini   PetscFunctionBegin;
5172d29f1f2SStefano Zampini   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5182d29f1f2SStefano Zampini   if (ts->trajectory) {
5192d29f1f2SStefano Zampini     ierr = TSTrajectoryDestroy(&ts->trajectory);CHKERRQ(ierr);
5202d29f1f2SStefano Zampini     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
5212d29f1f2SStefano Zampini   }
5222d29f1f2SStefano Zampini   PetscFunctionReturn(0);
5232d29f1f2SStefano Zampini }
5242d29f1f2SStefano Zampini 
5252d29f1f2SStefano Zampini /*@
526a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
527a7a1495cSBarry Smith       set with TSSetRHSJacobian().
528a7a1495cSBarry Smith 
529a7a1495cSBarry Smith    Collective on TS and Vec
530a7a1495cSBarry Smith 
531a7a1495cSBarry Smith    Input Parameters:
532316643e7SJed Brown +  ts - the TS context
533a7a1495cSBarry Smith .  t - current timestep
5340910c330SBarry Smith -  U - input vector
535a7a1495cSBarry Smith 
536a7a1495cSBarry Smith    Output Parameters:
537a7a1495cSBarry Smith +  A - Jacobian matrix
538a7a1495cSBarry Smith .  B - optional preconditioning matrix
539a7a1495cSBarry Smith -  flag - flag indicating matrix structure
540a7a1495cSBarry Smith 
541a7a1495cSBarry Smith    Notes:
542a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
543a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
544a7a1495cSBarry Smith 
54594b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
546a7a1495cSBarry Smith    flag parameter.
547a7a1495cSBarry Smith 
548a7a1495cSBarry Smith    Level: developer
549a7a1495cSBarry Smith 
550a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
551a7a1495cSBarry Smith 
55294b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
553a7a1495cSBarry Smith @*/
554d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
555a7a1495cSBarry Smith {
556dfbe8321SBarry Smith   PetscErrorCode   ierr;
557270bf2e7SJed Brown   PetscObjectState Ustate;
5586c1e1eecSBarry Smith   PetscObjectId    Uid;
55924989b8cSPeter Brune   DM               dm;
560942e3340SBarry Smith   DMTS             tsdm;
56124989b8cSPeter Brune   TSRHSJacobian    rhsjacobianfunc;
56224989b8cSPeter Brune   void             *ctx;
56324989b8cSPeter Brune   TSIJacobian      ijacobianfunc;
564b2df71adSDebojyoti Ghosh   TSRHSFunction    rhsfunction;
565a7a1495cSBarry Smith 
566a7a1495cSBarry Smith   PetscFunctionBegin;
5670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5680910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5690910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
57024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
571942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
57224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
5730298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
574b2df71adSDebojyoti Ghosh   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
57559e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
5766c1e1eecSBarry Smith   ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
577971015bcSStefano Zampini 
5786c1e1eecSBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
579971015bcSStefano Zampini     /* restore back RHS Jacobian matrices if they have been shifted/scaled */
580971015bcSStefano Zampini     if (A == ts->Arhs) {
581971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
582971015bcSStefano Zampini         ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
583971015bcSStefano Zampini       }
584971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
585971015bcSStefano Zampini         ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
586971015bcSStefano Zampini       }
587971015bcSStefano Zampini     }
588971015bcSStefano Zampini     if (B && B == ts->Brhs && A != B) {
589971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
590971015bcSStefano Zampini         ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
591971015bcSStefano Zampini       }
592971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
593971015bcSStefano Zampini         ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
594971015bcSStefano Zampini       }
595971015bcSStefano Zampini     }
596971015bcSStefano Zampini     ts->rhsjacobian.shift = 0;
597971015bcSStefano Zampini     ts->rhsjacobian.scale = 1.;
5980e4ef248SJed Brown     PetscFunctionReturn(0);
599f8ede8e7SJed Brown   }
600d90be118SSean Farley 
601ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
602d90be118SSean Farley 
603e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
604971015bcSStefano Zampini     if (A == ts->Arhs) {
605971015bcSStefano Zampini       /* MatScale has a short path for this case.
606971015bcSStefano Zampini          However, this code path is taken the first time TSComputeRHSJacobian is called
607971015bcSStefano Zampini          and the matrices have not assembled yet */
608971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
60994ab13aaSBarry Smith         ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
610971015bcSStefano Zampini       }
611971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
61294ab13aaSBarry Smith         ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
613971015bcSStefano Zampini       }
614971015bcSStefano Zampini     }
615971015bcSStefano Zampini     if (B && B == ts->Brhs && A != B) {
616971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
61794ab13aaSBarry Smith         ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
618971015bcSStefano Zampini       }
619971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
62094ab13aaSBarry Smith         ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
621e1244c69SJed Brown       }
622971015bcSStefano Zampini     }
623e1244c69SJed Brown   }
624e1244c69SJed Brown 
62524989b8cSPeter Brune   if (rhsjacobianfunc) {
6266cd88445SBarry Smith     PetscBool missing;
62794ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
628a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
629d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
630a7a1495cSBarry Smith     PetscStackPop;
63194ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
6326cd88445SBarry Smith     if (A) {
6336cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
6346cd88445SBarry 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");
6356cd88445SBarry Smith     }
6366cd88445SBarry Smith     if (B && B != A) {
6376cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
6386cd88445SBarry 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");
6396cd88445SBarry Smith     }
640ef66eb69SBarry Smith   } else {
64194ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
642971015bcSStefano Zampini     if (B && A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
643ef66eb69SBarry Smith   }
6440e4ef248SJed Brown   ts->rhsjacobian.time  = t;
645971015bcSStefano Zampini   ts->rhsjacobian.shift = 0;
646971015bcSStefano Zampini   ts->rhsjacobian.scale = 1.;
6477f79407eSBarry Smith   ierr                  = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr);
64859e4f3c8SBarry Smith   ierr                  = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
649a7a1495cSBarry Smith   PetscFunctionReturn(0);
650a7a1495cSBarry Smith }
651a7a1495cSBarry Smith 
652316643e7SJed Brown /*@
653d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
654d763cef2SBarry Smith 
655316643e7SJed Brown    Collective on TS and Vec
656316643e7SJed Brown 
657316643e7SJed Brown    Input Parameters:
658316643e7SJed Brown +  ts - the TS context
659316643e7SJed Brown .  t - current time
6600910c330SBarry Smith -  U - state vector
661316643e7SJed Brown 
662316643e7SJed Brown    Output Parameter:
663316643e7SJed Brown .  y - right hand side
664316643e7SJed Brown 
665316643e7SJed Brown    Note:
666316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
667316643e7SJed Brown    is used internally within the nonlinear solvers.
668316643e7SJed Brown 
669316643e7SJed Brown    Level: developer
670316643e7SJed Brown 
671316643e7SJed Brown .keywords: TS, compute
672316643e7SJed Brown 
673316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
674316643e7SJed Brown @*/
6750910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
676d763cef2SBarry Smith {
677dfbe8321SBarry Smith   PetscErrorCode ierr;
67824989b8cSPeter Brune   TSRHSFunction  rhsfunction;
67924989b8cSPeter Brune   TSIFunction    ifunction;
68024989b8cSPeter Brune   void           *ctx;
68124989b8cSPeter Brune   DM             dm;
68224989b8cSPeter Brune 
683d763cef2SBarry Smith   PetscFunctionBegin;
6840700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6850910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6860700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
68724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
68824989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6890298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
690d763cef2SBarry Smith 
691ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
692d763cef2SBarry Smith 
6930910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
69424989b8cSPeter Brune   if (rhsfunction) {
695d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6960910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
697d763cef2SBarry Smith     PetscStackPop;
698214bc6a2SJed Brown   } else {
699214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
700b2cd27e8SJed Brown   }
70144a41b28SSean Farley 
7020910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
703d763cef2SBarry Smith   PetscFunctionReturn(0);
704d763cef2SBarry Smith }
705d763cef2SBarry Smith 
706ef20d060SBarry Smith /*@
707ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
708ef20d060SBarry Smith 
709ef20d060SBarry Smith    Collective on TS and Vec
710ef20d060SBarry Smith 
711ef20d060SBarry Smith    Input Parameters:
712ef20d060SBarry Smith +  ts - the TS context
713ef20d060SBarry Smith -  t - current time
714ef20d060SBarry Smith 
715ef20d060SBarry Smith    Output Parameter:
7160910c330SBarry Smith .  U - the solution
717ef20d060SBarry Smith 
718ef20d060SBarry Smith    Note:
719ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
720ef20d060SBarry Smith    is used internally within the nonlinear solvers.
721ef20d060SBarry Smith 
722ef20d060SBarry Smith    Level: developer
723ef20d060SBarry Smith 
724ef20d060SBarry Smith .keywords: TS, compute
725ef20d060SBarry Smith 
726abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
727ef20d060SBarry Smith @*/
7280910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
729ef20d060SBarry Smith {
730ef20d060SBarry Smith   PetscErrorCode     ierr;
731ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
732ef20d060SBarry Smith   void               *ctx;
733ef20d060SBarry Smith   DM                 dm;
734ef20d060SBarry Smith 
735ef20d060SBarry Smith   PetscFunctionBegin;
736ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7370910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
738ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
739ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
740ef20d060SBarry Smith 
741ef20d060SBarry Smith   if (solutionfunction) {
7429b7cd975SBarry Smith     PetscStackPush("TS user solution function");
7430910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
744ef20d060SBarry Smith     PetscStackPop;
745ef20d060SBarry Smith   }
746ef20d060SBarry Smith   PetscFunctionReturn(0);
747ef20d060SBarry Smith }
7489b7cd975SBarry Smith /*@
7499b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
7509b7cd975SBarry Smith 
7519b7cd975SBarry Smith    Collective on TS and Vec
7529b7cd975SBarry Smith 
7539b7cd975SBarry Smith    Input Parameters:
7549b7cd975SBarry Smith +  ts - the TS context
7559b7cd975SBarry Smith -  t - current time
7569b7cd975SBarry Smith 
7579b7cd975SBarry Smith    Output Parameter:
7589b7cd975SBarry Smith .  U - the function value
7599b7cd975SBarry Smith 
7609b7cd975SBarry Smith    Note:
7619b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
7629b7cd975SBarry Smith    is used internally within the nonlinear solvers.
7639b7cd975SBarry Smith 
7649b7cd975SBarry Smith    Level: developer
7659b7cd975SBarry Smith 
7669b7cd975SBarry Smith .keywords: TS, compute
7679b7cd975SBarry Smith 
7689b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7699b7cd975SBarry Smith @*/
7709b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7719b7cd975SBarry Smith {
7729b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7739b7cd975SBarry Smith   void               *ctx;
7749b7cd975SBarry Smith   DM                 dm;
7759b7cd975SBarry Smith 
7769b7cd975SBarry Smith   PetscFunctionBegin;
7779b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7789b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7799b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7809b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7819b7cd975SBarry Smith 
7829b7cd975SBarry Smith   if (forcing) {
7839b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7849b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7859b7cd975SBarry Smith     PetscStackPop;
7869b7cd975SBarry Smith   }
7879b7cd975SBarry Smith   PetscFunctionReturn(0);
7889b7cd975SBarry Smith }
789ef20d060SBarry Smith 
790214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
791214bc6a2SJed Brown {
7922dd45cf8SJed Brown   Vec            F;
793214bc6a2SJed Brown   PetscErrorCode ierr;
794214bc6a2SJed Brown 
795214bc6a2SJed Brown   PetscFunctionBegin;
7960298fd71SBarry Smith   *Frhs = NULL;
7970298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
798214bc6a2SJed Brown   if (!ts->Frhs) {
7992dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
800214bc6a2SJed Brown   }
801214bc6a2SJed Brown   *Frhs = ts->Frhs;
802214bc6a2SJed Brown   PetscFunctionReturn(0);
803214bc6a2SJed Brown }
804214bc6a2SJed Brown 
805971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
806214bc6a2SJed Brown {
807214bc6a2SJed Brown   Mat            A,B;
8082dd45cf8SJed Brown   PetscErrorCode ierr;
80941a1d4d2SBarry Smith   TSIJacobian    ijacobian;
810214bc6a2SJed Brown 
811214bc6a2SJed Brown   PetscFunctionBegin;
812c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
813c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
81441a1d4d2SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr);
815214bc6a2SJed Brown   if (Arhs) {
816214bc6a2SJed Brown     if (!ts->Arhs) {
81741a1d4d2SBarry Smith       if (ijacobian) {
818214bc6a2SJed Brown         ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
81941a1d4d2SBarry Smith       } else {
82041a1d4d2SBarry Smith         ts->Arhs = A;
82141a1d4d2SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
82241a1d4d2SBarry Smith       }
8233565c898SBarry Smith     } else {
8243565c898SBarry Smith       PetscBool flg;
8253565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
8263565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
8273565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs){
8283565c898SBarry Smith         ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr);
8293565c898SBarry Smith         ts->Arhs = A;
8303565c898SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
8313565c898SBarry Smith       }
832214bc6a2SJed Brown     }
833214bc6a2SJed Brown     *Arhs = ts->Arhs;
834214bc6a2SJed Brown   }
835214bc6a2SJed Brown   if (Brhs) {
836214bc6a2SJed Brown     if (!ts->Brhs) {
837bdb70873SJed Brown       if (A != B) {
83841a1d4d2SBarry Smith         if (ijacobian) {
839214bc6a2SJed Brown           ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
840bdb70873SJed Brown         } else {
84141a1d4d2SBarry Smith           ts->Brhs = B;
84241a1d4d2SBarry Smith           ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
84341a1d4d2SBarry Smith         }
84441a1d4d2SBarry Smith       } else {
845bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
84651699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
847bdb70873SJed Brown       }
848214bc6a2SJed Brown     }
849214bc6a2SJed Brown     *Brhs = ts->Brhs;
850214bc6a2SJed Brown   }
851214bc6a2SJed Brown   PetscFunctionReturn(0);
852214bc6a2SJed Brown }
853214bc6a2SJed Brown 
854316643e7SJed Brown /*@
8550910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
856316643e7SJed Brown 
857316643e7SJed Brown    Collective on TS and Vec
858316643e7SJed Brown 
859316643e7SJed Brown    Input Parameters:
860316643e7SJed Brown +  ts - the TS context
861316643e7SJed Brown .  t - current time
8620910c330SBarry Smith .  U - state vector
8630910c330SBarry Smith .  Udot - time derivative of state vector
864214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
865316643e7SJed Brown 
866316643e7SJed Brown    Output Parameter:
867316643e7SJed Brown .  Y - right hand side
868316643e7SJed Brown 
869316643e7SJed Brown    Note:
870316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
871316643e7SJed Brown    is used internally within the nonlinear solvers.
872316643e7SJed Brown 
873316643e7SJed Brown    If the user did did not write their equations in implicit form, this
874316643e7SJed Brown    function recasts them in implicit form.
875316643e7SJed Brown 
876316643e7SJed Brown    Level: developer
877316643e7SJed Brown 
878316643e7SJed Brown .keywords: TS, compute
879316643e7SJed Brown 
880316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
881316643e7SJed Brown @*/
8820910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
883316643e7SJed Brown {
884316643e7SJed Brown   PetscErrorCode ierr;
88524989b8cSPeter Brune   TSIFunction    ifunction;
88624989b8cSPeter Brune   TSRHSFunction  rhsfunction;
88724989b8cSPeter Brune   void           *ctx;
88824989b8cSPeter Brune   DM             dm;
889316643e7SJed Brown 
890316643e7SJed Brown   PetscFunctionBegin;
8910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8920910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8930910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8940700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
895316643e7SJed Brown 
89624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
89724989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8980298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
89924989b8cSPeter Brune 
900ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
901d90be118SSean Farley 
9020910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
90324989b8cSPeter Brune   if (ifunction) {
904316643e7SJed Brown     PetscStackPush("TS user implicit function");
9050910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
906316643e7SJed Brown     PetscStackPop;
907214bc6a2SJed Brown   }
908214bc6a2SJed Brown   if (imex) {
90924989b8cSPeter Brune     if (!ifunction) {
9100910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
9112dd45cf8SJed Brown     }
91224989b8cSPeter Brune   } else if (rhsfunction) {
91324989b8cSPeter Brune     if (ifunction) {
914214bc6a2SJed Brown       Vec Frhs;
915214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
9160910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
917214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
9182dd45cf8SJed Brown     } else {
9190910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
9200910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
921316643e7SJed Brown     }
9224a6899ffSJed Brown   }
9230910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
924316643e7SJed Brown   PetscFunctionReturn(0);
925316643e7SJed Brown }
926316643e7SJed Brown 
927316643e7SJed Brown /*@
928316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
929316643e7SJed Brown 
930316643e7SJed Brown    Collective on TS and Vec
931316643e7SJed Brown 
932316643e7SJed Brown    Input
933316643e7SJed Brown       Input Parameters:
934316643e7SJed Brown +  ts - the TS context
935316643e7SJed Brown .  t - current timestep
9360910c330SBarry Smith .  U - state vector
9370910c330SBarry Smith .  Udot - time derivative of state vector
938214bc6a2SJed Brown .  shift - shift to apply, see note below
939214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
940316643e7SJed Brown 
941316643e7SJed Brown    Output Parameters:
942316643e7SJed Brown +  A - Jacobian matrix
9433565c898SBarry Smith -  B - matrix from which the preconditioner is constructed; often the same as A
944316643e7SJed Brown 
945316643e7SJed Brown    Notes:
9460910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
947316643e7SJed Brown 
9480910c330SBarry Smith    dF/dU + shift*dF/dUdot
949316643e7SJed Brown 
950316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
951316643e7SJed Brown    is used internally within the nonlinear solvers.
952316643e7SJed Brown 
953316643e7SJed Brown    Level: developer
954316643e7SJed Brown 
955316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
956316643e7SJed Brown 
957316643e7SJed Brown .seealso:  TSSetIJacobian()
95863495f91SJed Brown @*/
959d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
960316643e7SJed Brown {
961316643e7SJed Brown   PetscErrorCode ierr;
96224989b8cSPeter Brune   TSIJacobian    ijacobian;
96324989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
96424989b8cSPeter Brune   DM             dm;
96524989b8cSPeter Brune   void           *ctx;
966316643e7SJed Brown 
967316643e7SJed Brown   PetscFunctionBegin;
9680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9690910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
9700910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
971316643e7SJed Brown   PetscValidPointer(A,6);
97294ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
973316643e7SJed Brown   PetscValidPointer(B,7);
97494ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
97524989b8cSPeter Brune 
97624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
97724989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9780298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
97924989b8cSPeter Brune 
980ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
981316643e7SJed Brown 
98294ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
98324989b8cSPeter Brune   if (ijacobian) {
9846cd88445SBarry Smith     PetscBool missing;
985316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
986d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
987316643e7SJed Brown     PetscStackPop;
9886cd88445SBarry Smith     ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
9896cd88445SBarry 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");
9903565c898SBarry Smith     if (B != A) {
9916cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
9926cd88445SBarry 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");
9936cd88445SBarry Smith     }
9944a6899ffSJed Brown   }
995214bc6a2SJed Brown   if (imex) {
996b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9974c26be97Sstefano_zampini       PetscBool assembled;
998971015bcSStefano Zampini       if (rhsjacobian) {
999971015bcSStefano Zampini         Mat Arhs = NULL;
1000971015bcSStefano Zampini         ierr = TSGetRHSMats_Private(ts,&Arhs,NULL);CHKERRQ(ierr);
1001971015bcSStefano Zampini         if (A == Arhs) {
1002971015bcSStefano Zampini           if (rhsjacobian == TSComputeRHSJacobianConstant) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Unsupported operation! cannot use TSComputeRHSJacobianConstant");
1003971015bcSStefano Zampini           ts->rhsjacobian.time = PETSC_MIN_REAL;
1004971015bcSStefano Zampini         }
1005971015bcSStefano Zampini       }
100694ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
10074c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
10084c26be97Sstefano_zampini       if (!assembled) {
10094c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10104c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10114c26be97Sstefano_zampini       }
101294ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
101394ab13aaSBarry Smith       if (A != B) {
101494ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
10154c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
10164c26be97Sstefano_zampini         if (!assembled) {
10174c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10184c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10194c26be97Sstefano_zampini         }
102094ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
1021214bc6a2SJed Brown       }
1022214bc6a2SJed Brown     }
1023214bc6a2SJed Brown   } else {
1024e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
1025e1244c69SJed Brown     if (rhsjacobian) {
1026214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
1027d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
1028e1244c69SJed Brown     }
102994ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
10303565c898SBarry Smith       PetscBool flg;
1031e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
1032e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
10333565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
10343565c898SBarry Smith       /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
10353565c898SBarry Smith       if (!flg) {
103694ab13aaSBarry Smith         ierr = MatScale(A,-1);CHKERRQ(ierr);
103794ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
10383565c898SBarry Smith       }
103994ab13aaSBarry Smith       if (A != B) {
104094ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
104194ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
1042316643e7SJed Brown       }
1043e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
1044e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1045e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
104694ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
104794ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
104894ab13aaSBarry Smith         if (A != B) {
104994ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
105094ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1051214bc6a2SJed Brown         }
1052316643e7SJed Brown       }
105394ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
105494ab13aaSBarry Smith       if (A != B) {
105594ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
1056316643e7SJed Brown       }
1057316643e7SJed Brown     }
1058316643e7SJed Brown   }
105994ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
1060316643e7SJed Brown   PetscFunctionReturn(0);
1061316643e7SJed Brown }
1062316643e7SJed Brown 
1063d763cef2SBarry Smith /*@C
1064d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
1065b5abc632SBarry Smith     where U_t = G(t,u).
1066d763cef2SBarry Smith 
10673f9fe445SBarry Smith     Logically Collective on TS
1068d763cef2SBarry Smith 
1069d763cef2SBarry Smith     Input Parameters:
1070d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
10710298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
1072d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
1073d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
10740298fd71SBarry Smith           function evaluation routine (may be NULL)
1075d763cef2SBarry Smith 
1076d763cef2SBarry Smith     Calling sequence of func:
107787828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
1078d763cef2SBarry Smith 
1079d763cef2SBarry Smith +   t - current timestep
1080d763cef2SBarry Smith .   u - input vector
1081d763cef2SBarry Smith .   F - function vector
1082d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1083d763cef2SBarry Smith 
1084d763cef2SBarry Smith     Level: beginner
1085d763cef2SBarry Smith 
108695452b02SPatrick Sanan     Notes:
108795452b02SPatrick Sanan     You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10882bbac0d3SBarry Smith 
1089d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1090d763cef2SBarry Smith 
1091ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1092d763cef2SBarry Smith @*/
1093089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1094d763cef2SBarry Smith {
1095089b2837SJed Brown   PetscErrorCode ierr;
1096089b2837SJed Brown   SNES           snes;
10970298fd71SBarry Smith   Vec            ralloc = NULL;
109824989b8cSPeter Brune   DM             dm;
1099d763cef2SBarry Smith 
1100089b2837SJed Brown   PetscFunctionBegin;
11010700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1102ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
110324989b8cSPeter Brune 
110424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
110524989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1106089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1107e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1108e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1109e856ceecSJed Brown     r = ralloc;
1110e856ceecSJed Brown   }
1111089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1112e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1113d763cef2SBarry Smith   PetscFunctionReturn(0);
1114d763cef2SBarry Smith }
1115d763cef2SBarry Smith 
1116ef20d060SBarry Smith /*@C
1117abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1118ef20d060SBarry Smith 
1119ef20d060SBarry Smith     Logically Collective on TS
1120ef20d060SBarry Smith 
1121ef20d060SBarry Smith     Input Parameters:
1122ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1123ef20d060SBarry Smith .   f - routine for evaluating the solution
1124ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
11250298fd71SBarry Smith           function evaluation routine (may be NULL)
1126ef20d060SBarry Smith 
1127ef20d060SBarry Smith     Calling sequence of func:
1128ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
1129ef20d060SBarry Smith 
1130ef20d060SBarry Smith +   t - current timestep
1131ef20d060SBarry Smith .   u - output vector
1132ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1133ef20d060SBarry Smith 
11340ed3bfb6SBarry Smith     Options Database:
11350ed3bfb6SBarry Smith +  -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction()
11360ed3bfb6SBarry Smith -  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
11370ed3bfb6SBarry Smith 
1138abd5a294SJed Brown     Notes:
1139abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1140abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1141abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1142abd5a294SJed Brown 
11434f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1144abd5a294SJed Brown 
1145ef20d060SBarry Smith     Level: beginner
1146ef20d060SBarry Smith 
1147ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1148ef20d060SBarry Smith 
11490ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError()
1150ef20d060SBarry Smith @*/
1151ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1152ef20d060SBarry Smith {
1153ef20d060SBarry Smith   PetscErrorCode ierr;
1154ef20d060SBarry Smith   DM             dm;
1155ef20d060SBarry Smith 
1156ef20d060SBarry Smith   PetscFunctionBegin;
1157ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1158ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1159ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1160ef20d060SBarry Smith   PetscFunctionReturn(0);
1161ef20d060SBarry Smith }
1162ef20d060SBarry Smith 
11639b7cd975SBarry Smith /*@C
11649b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
11659b7cd975SBarry Smith 
11669b7cd975SBarry Smith     Logically Collective on TS
11679b7cd975SBarry Smith 
11689b7cd975SBarry Smith     Input Parameters:
11699b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1170e162b725SBarry Smith .   func - routine for evaluating the forcing function
11719b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
11720298fd71SBarry Smith           function evaluation routine (may be NULL)
11739b7cd975SBarry Smith 
11749b7cd975SBarry Smith     Calling sequence of func:
1175e162b725SBarry Smith $     func (TS ts,PetscReal t,Vec f,void *ctx);
11769b7cd975SBarry Smith 
11779b7cd975SBarry Smith +   t - current timestep
1178e162b725SBarry Smith .   f - output vector
11799b7cd975SBarry Smith -   ctx - [optional] user-defined function context
11809b7cd975SBarry Smith 
11819b7cd975SBarry Smith     Notes:
11829b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1183e162b725SBarry 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
1184e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1185e162b725SBarry Smith 
1186e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1187e162b725SBarry Smith 
1188e162b725SBarry 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
1189e162b725SBarry Smith     parameters can be passed in the ctx variable.
11909b7cd975SBarry Smith 
11919b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
11929b7cd975SBarry Smith 
11939b7cd975SBarry Smith     Level: beginner
11949b7cd975SBarry Smith 
11959b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
11969b7cd975SBarry Smith 
11979b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
11989b7cd975SBarry Smith @*/
1199e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
12009b7cd975SBarry Smith {
12019b7cd975SBarry Smith   PetscErrorCode ierr;
12029b7cd975SBarry Smith   DM             dm;
12039b7cd975SBarry Smith 
12049b7cd975SBarry Smith   PetscFunctionBegin;
12059b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
12069b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1207e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
12089b7cd975SBarry Smith   PetscFunctionReturn(0);
12099b7cd975SBarry Smith }
12109b7cd975SBarry Smith 
1211d763cef2SBarry Smith /*@C
1212f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1213b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1214d763cef2SBarry Smith 
12153f9fe445SBarry Smith    Logically Collective on TS
1216d763cef2SBarry Smith 
1217d763cef2SBarry Smith    Input Parameters:
1218d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1219e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1220e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1221d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1222d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
12230298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1224d763cef2SBarry Smith 
1225f7ab8db6SBarry Smith    Calling sequence of f:
1226ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1227d763cef2SBarry Smith 
1228d763cef2SBarry Smith +  t - current timestep
1229d763cef2SBarry Smith .  u - input vector
1230e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1231e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1232d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1233d763cef2SBarry Smith 
12346cd88445SBarry Smith    Notes:
12356cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
12366cd88445SBarry Smith 
12376cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1238ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1239d763cef2SBarry Smith 
1240d763cef2SBarry Smith    Level: beginner
1241d763cef2SBarry Smith 
1242d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1243d763cef2SBarry Smith 
1244ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1245d763cef2SBarry Smith 
1246d763cef2SBarry Smith @*/
1247e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1248d763cef2SBarry Smith {
1249277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1250089b2837SJed Brown   SNES           snes;
125124989b8cSPeter Brune   DM             dm;
125224989b8cSPeter Brune   TSIJacobian    ijacobian;
1253277b19d0SLisandro Dalcin 
1254d763cef2SBarry Smith   PetscFunctionBegin;
12550700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1256e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1257e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1258e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1259e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1260d763cef2SBarry Smith 
126124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
126224989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1263e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1264e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1265e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1266e1244c69SJed Brown   }
12670298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1268089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12695f659677SPeter Brune   if (!ijacobian) {
1270e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
12710e4ef248SJed Brown   }
1272e5d3d808SBarry Smith   if (Amat) {
1273e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
12740e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1275e5d3d808SBarry Smith     ts->Arhs = Amat;
12760e4ef248SJed Brown   }
1277e5d3d808SBarry Smith   if (Pmat) {
1278e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
12790e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1280e5d3d808SBarry Smith     ts->Brhs = Pmat;
12810e4ef248SJed Brown   }
1282d763cef2SBarry Smith   PetscFunctionReturn(0);
1283d763cef2SBarry Smith }
1284d763cef2SBarry Smith 
1285316643e7SJed Brown /*@C
1286b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1287316643e7SJed Brown 
12883f9fe445SBarry Smith    Logically Collective on TS
1289316643e7SJed Brown 
1290316643e7SJed Brown    Input Parameters:
1291316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12920298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1293316643e7SJed Brown .  f   - the function evaluation routine
12940298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1295316643e7SJed Brown 
1296316643e7SJed Brown    Calling sequence of f:
1297316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1298316643e7SJed Brown 
1299316643e7SJed Brown +  t   - time at step/stage being solved
1300316643e7SJed Brown .  u   - state vector
1301316643e7SJed Brown .  u_t - time derivative of state vector
1302316643e7SJed Brown .  F   - function vector
1303316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1304316643e7SJed Brown 
1305316643e7SJed Brown    Important:
13062bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1307316643e7SJed Brown 
1308316643e7SJed Brown    Level: beginner
1309316643e7SJed Brown 
1310316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1311316643e7SJed Brown 
1312d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1313316643e7SJed Brown @*/
131451699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1315316643e7SJed Brown {
1316089b2837SJed Brown   PetscErrorCode ierr;
1317089b2837SJed Brown   SNES           snes;
131851699248SLisandro Dalcin   Vec            ralloc = NULL;
131924989b8cSPeter Brune   DM             dm;
1320316643e7SJed Brown 
1321316643e7SJed Brown   PetscFunctionBegin;
13220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
132351699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
132424989b8cSPeter Brune 
132524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
132624989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
132724989b8cSPeter Brune 
1328089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
132951699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
133051699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
133151699248SLisandro Dalcin     r  = ralloc;
1332e856ceecSJed Brown   }
133351699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
133451699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1335089b2837SJed Brown   PetscFunctionReturn(0);
1336089b2837SJed Brown }
1337089b2837SJed Brown 
1338089b2837SJed Brown /*@C
1339089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1340089b2837SJed Brown 
1341089b2837SJed Brown    Not Collective
1342089b2837SJed Brown 
1343089b2837SJed Brown    Input Parameter:
1344089b2837SJed Brown .  ts - the TS context
1345089b2837SJed Brown 
1346089b2837SJed Brown    Output Parameter:
13470298fd71SBarry Smith +  r - vector to hold residual (or NULL)
13480298fd71SBarry Smith .  func - the function to compute residual (or NULL)
13490298fd71SBarry Smith -  ctx - the function context (or NULL)
1350089b2837SJed Brown 
1351089b2837SJed Brown    Level: advanced
1352089b2837SJed Brown 
1353089b2837SJed Brown .keywords: TS, nonlinear, get, function
1354089b2837SJed Brown 
1355089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1356089b2837SJed Brown @*/
1357089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1358089b2837SJed Brown {
1359089b2837SJed Brown   PetscErrorCode ierr;
1360089b2837SJed Brown   SNES           snes;
136124989b8cSPeter Brune   DM             dm;
1362089b2837SJed Brown 
1363089b2837SJed Brown   PetscFunctionBegin;
1364089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1365089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13660298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
136724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
136824989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1369089b2837SJed Brown   PetscFunctionReturn(0);
1370089b2837SJed Brown }
1371089b2837SJed Brown 
1372089b2837SJed Brown /*@C
1373089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1374089b2837SJed Brown 
1375089b2837SJed Brown    Not Collective
1376089b2837SJed Brown 
1377089b2837SJed Brown    Input Parameter:
1378089b2837SJed Brown .  ts - the TS context
1379089b2837SJed Brown 
1380089b2837SJed Brown    Output Parameter:
13810298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13820298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13830298fd71SBarry Smith -  ctx - the function context (or NULL)
1384089b2837SJed Brown 
1385089b2837SJed Brown    Level: advanced
1386089b2837SJed Brown 
1387089b2837SJed Brown .keywords: TS, nonlinear, get, function
1388089b2837SJed Brown 
13892bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1390089b2837SJed Brown @*/
1391089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1392089b2837SJed Brown {
1393089b2837SJed Brown   PetscErrorCode ierr;
1394089b2837SJed Brown   SNES           snes;
139524989b8cSPeter Brune   DM             dm;
1396089b2837SJed Brown 
1397089b2837SJed Brown   PetscFunctionBegin;
1398089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1399089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
14000298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
140124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
140224989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1403316643e7SJed Brown   PetscFunctionReturn(0);
1404316643e7SJed Brown }
1405316643e7SJed Brown 
1406316643e7SJed Brown /*@C
1407a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1408ae8867d6SBarry Smith         provided with TSSetIFunction().
1409316643e7SJed Brown 
14103f9fe445SBarry Smith    Logically Collective on TS
1411316643e7SJed Brown 
1412316643e7SJed Brown    Input Parameters:
1413316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1414e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1415e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1416316643e7SJed Brown .  f   - the Jacobian evaluation routine
14170298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1418316643e7SJed Brown 
1419316643e7SJed Brown    Calling sequence of f:
1420ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1421316643e7SJed Brown 
1422316643e7SJed Brown +  t    - time at step/stage being solved
14231b4a444bSJed Brown .  U    - state vector
14241b4a444bSJed Brown .  U_t  - time derivative of state vector
1425316643e7SJed Brown .  a    - shift
1426e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1427e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1428316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1429316643e7SJed Brown 
1430316643e7SJed Brown    Notes:
1431e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1432316643e7SJed Brown 
1433895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1434895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1435895c21f2SBarry Smith 
1436a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1437b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1438a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1439a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1440a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1441a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1442a4f0a591SBarry Smith 
14436cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
14446cd88445SBarry Smith 
14456cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1446ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1447ca5f011dSBarry Smith 
1448316643e7SJed Brown    Level: beginner
1449316643e7SJed Brown 
1450316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1451316643e7SJed Brown 
1452ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1453316643e7SJed Brown 
1454316643e7SJed Brown @*/
1455e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1456316643e7SJed Brown {
1457316643e7SJed Brown   PetscErrorCode ierr;
1458089b2837SJed Brown   SNES           snes;
145924989b8cSPeter Brune   DM             dm;
1460316643e7SJed Brown 
1461316643e7SJed Brown   PetscFunctionBegin;
14620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1463e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1464e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1465e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1466e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
146724989b8cSPeter Brune 
146824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
146924989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
147024989b8cSPeter Brune 
1471089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1472e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1473316643e7SJed Brown   PetscFunctionReturn(0);
1474316643e7SJed Brown }
1475316643e7SJed Brown 
1476e1244c69SJed Brown /*@
1477e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1478e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1479e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1480e1244c69SJed Brown    not been changed by the TS.
1481e1244c69SJed Brown 
1482e1244c69SJed Brown    Logically Collective
1483e1244c69SJed Brown 
1484e1244c69SJed Brown    Input Arguments:
1485e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1486e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1487e1244c69SJed Brown 
1488e1244c69SJed Brown    Level: intermediate
1489e1244c69SJed Brown 
1490e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1491e1244c69SJed Brown @*/
1492e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1493e1244c69SJed Brown {
1494e1244c69SJed Brown   PetscFunctionBegin;
1495e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1496e1244c69SJed Brown   PetscFunctionReturn(0);
1497e1244c69SJed Brown }
1498e1244c69SJed Brown 
1499efe9872eSLisandro Dalcin /*@C
1500efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1501efe9872eSLisandro Dalcin 
1502efe9872eSLisandro Dalcin    Logically Collective on TS
1503efe9872eSLisandro Dalcin 
1504efe9872eSLisandro Dalcin    Input Parameters:
1505efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1506efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1507efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1508efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1509efe9872eSLisandro Dalcin 
1510efe9872eSLisandro Dalcin    Calling sequence of fun:
1511efe9872eSLisandro Dalcin $  fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1512efe9872eSLisandro Dalcin 
1513efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1514efe9872eSLisandro Dalcin .  U    - state vector
1515efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1516efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1517efe9872eSLisandro Dalcin .  F    - function vector
1518efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1519efe9872eSLisandro Dalcin 
1520efe9872eSLisandro Dalcin    Level: beginner
1521efe9872eSLisandro Dalcin 
1522efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function
1523efe9872eSLisandro Dalcin 
1524efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian()
1525efe9872eSLisandro Dalcin @*/
1526efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1527efe9872eSLisandro Dalcin {
1528efe9872eSLisandro Dalcin   DM             dm;
1529efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1530efe9872eSLisandro Dalcin 
1531efe9872eSLisandro Dalcin   PetscFunctionBegin;
1532efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1533efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1534efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1535efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1536efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1537efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1538efe9872eSLisandro Dalcin }
1539efe9872eSLisandro Dalcin 
1540efe9872eSLisandro Dalcin /*@C
1541efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1542efe9872eSLisandro Dalcin 
1543efe9872eSLisandro Dalcin   Not Collective
1544efe9872eSLisandro Dalcin 
1545efe9872eSLisandro Dalcin   Input Parameter:
1546efe9872eSLisandro Dalcin . ts - the TS context
1547efe9872eSLisandro Dalcin 
1548efe9872eSLisandro Dalcin   Output Parameter:
1549efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1550efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1551efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1552efe9872eSLisandro Dalcin 
1553efe9872eSLisandro Dalcin   Level: advanced
1554efe9872eSLisandro Dalcin 
1555efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function
1556efe9872eSLisandro Dalcin 
1557efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction()
1558efe9872eSLisandro Dalcin @*/
1559efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1560efe9872eSLisandro Dalcin {
1561efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1562efe9872eSLisandro Dalcin   SNES           snes;
1563efe9872eSLisandro Dalcin   DM             dm;
1564efe9872eSLisandro Dalcin 
1565efe9872eSLisandro Dalcin   PetscFunctionBegin;
1566efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1567efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1568efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1569efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1570efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1571efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1572efe9872eSLisandro Dalcin }
1573efe9872eSLisandro Dalcin 
1574efe9872eSLisandro Dalcin /*@C
1575bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1576efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1577efe9872eSLisandro Dalcin 
1578efe9872eSLisandro Dalcin    Logically Collective on TS
1579efe9872eSLisandro Dalcin 
1580efe9872eSLisandro Dalcin    Input Parameters:
1581efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1582efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1583efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1584efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1585efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1586efe9872eSLisandro Dalcin 
1587efe9872eSLisandro Dalcin    Calling sequence of jac:
1588bc77d74cSLisandro Dalcin $  jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx);
1589efe9872eSLisandro Dalcin 
1590efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1591efe9872eSLisandro Dalcin .  U    - state vector
1592efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1593efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1594efe9872eSLisandro Dalcin .  v    - shift for U_t
1595efe9872eSLisandro Dalcin .  a    - shift for U_tt
1596efe9872eSLisandro 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
1597efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1598efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1599efe9872eSLisandro Dalcin 
1600efe9872eSLisandro Dalcin    Notes:
1601efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1602efe9872eSLisandro Dalcin 
1603efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1604efe9872eSLisandro 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.
1605efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1606bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1607efe9872eSLisandro Dalcin 
1608efe9872eSLisandro Dalcin    Level: beginner
1609efe9872eSLisandro Dalcin 
1610efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian
1611efe9872eSLisandro Dalcin 
1612efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1613efe9872eSLisandro Dalcin @*/
1614efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1615efe9872eSLisandro Dalcin {
1616efe9872eSLisandro Dalcin   DM             dm;
1617efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1618efe9872eSLisandro Dalcin 
1619efe9872eSLisandro Dalcin   PetscFunctionBegin;
1620efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1621efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1622efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1623efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1624efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1625efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1626efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1627efe9872eSLisandro Dalcin }
1628efe9872eSLisandro Dalcin 
1629efe9872eSLisandro Dalcin /*@C
1630efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1631efe9872eSLisandro Dalcin 
1632efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1633efe9872eSLisandro Dalcin 
1634efe9872eSLisandro Dalcin   Input Parameter:
1635efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1636efe9872eSLisandro Dalcin 
1637efe9872eSLisandro Dalcin   Output Parameters:
1638efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1639efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1640efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1641efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1642efe9872eSLisandro Dalcin 
164395452b02SPatrick Sanan   Notes:
164495452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
1645efe9872eSLisandro Dalcin 
1646efe9872eSLisandro Dalcin   Level: advanced
1647efe9872eSLisandro Dalcin 
164880275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
1649efe9872eSLisandro Dalcin 
1650efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian
1651efe9872eSLisandro Dalcin @*/
1652efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1653efe9872eSLisandro Dalcin {
1654efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1655efe9872eSLisandro Dalcin   SNES           snes;
1656efe9872eSLisandro Dalcin   DM             dm;
1657efe9872eSLisandro Dalcin 
1658efe9872eSLisandro Dalcin   PetscFunctionBegin;
1659efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1660efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1661efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1662efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1663efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1664efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1665efe9872eSLisandro Dalcin }
1666efe9872eSLisandro Dalcin 
1667efe9872eSLisandro Dalcin /*@
1668efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1669efe9872eSLisandro Dalcin 
1670efe9872eSLisandro Dalcin   Collective on TS and Vec
1671efe9872eSLisandro Dalcin 
1672efe9872eSLisandro Dalcin   Input Parameters:
1673efe9872eSLisandro Dalcin + ts - the TS context
1674efe9872eSLisandro Dalcin . t - current time
1675efe9872eSLisandro Dalcin . U - state vector
1676efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1677efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1678efe9872eSLisandro Dalcin 
1679efe9872eSLisandro Dalcin   Output Parameter:
1680efe9872eSLisandro Dalcin . F - the residual vector
1681efe9872eSLisandro Dalcin 
1682efe9872eSLisandro Dalcin   Note:
1683efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1684efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1685efe9872eSLisandro Dalcin 
1686efe9872eSLisandro Dalcin   Level: developer
1687efe9872eSLisandro Dalcin 
1688efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector
1689efe9872eSLisandro Dalcin 
1690efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1691efe9872eSLisandro Dalcin @*/
1692efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1693efe9872eSLisandro Dalcin {
1694efe9872eSLisandro Dalcin   DM             dm;
1695efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1696efe9872eSLisandro Dalcin   void           *ctx;
1697efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1698efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1699efe9872eSLisandro Dalcin 
1700efe9872eSLisandro Dalcin   PetscFunctionBegin;
1701efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1702efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1703efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1704efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1705efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1706efe9872eSLisandro Dalcin 
1707efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1708efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1709efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1710efe9872eSLisandro Dalcin 
1711efe9872eSLisandro Dalcin   if (!I2Function) {
1712efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1713efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1714efe9872eSLisandro Dalcin   }
1715efe9872eSLisandro Dalcin 
1716efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1717efe9872eSLisandro Dalcin 
1718efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1719efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1720efe9872eSLisandro Dalcin   PetscStackPop;
1721efe9872eSLisandro Dalcin 
1722efe9872eSLisandro Dalcin   if (rhsfunction) {
1723efe9872eSLisandro Dalcin     Vec Frhs;
1724efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1725efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1726efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1727efe9872eSLisandro Dalcin   }
1728efe9872eSLisandro Dalcin 
1729efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1730efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1731efe9872eSLisandro Dalcin }
1732efe9872eSLisandro Dalcin 
1733efe9872eSLisandro Dalcin /*@
1734efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1735efe9872eSLisandro Dalcin 
1736efe9872eSLisandro Dalcin   Collective on TS and Vec
1737efe9872eSLisandro Dalcin 
1738efe9872eSLisandro Dalcin   Input Parameters:
1739efe9872eSLisandro Dalcin + ts - the TS context
1740efe9872eSLisandro Dalcin . t - current timestep
1741efe9872eSLisandro Dalcin . U - state vector
1742efe9872eSLisandro Dalcin . V - time derivative of state vector
1743efe9872eSLisandro Dalcin . A - second time derivative of state vector
1744efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1745efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1746efe9872eSLisandro Dalcin 
1747efe9872eSLisandro Dalcin   Output Parameters:
1748efe9872eSLisandro Dalcin + J - Jacobian matrix
1749efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1750efe9872eSLisandro Dalcin 
1751efe9872eSLisandro Dalcin   Notes:
1752efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1753efe9872eSLisandro Dalcin 
1754efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1755efe9872eSLisandro Dalcin 
1756efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1757efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1758efe9872eSLisandro Dalcin 
1759efe9872eSLisandro Dalcin   Level: developer
1760efe9872eSLisandro Dalcin 
1761efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix
1762efe9872eSLisandro Dalcin 
1763efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1764efe9872eSLisandro Dalcin @*/
1765efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1766efe9872eSLisandro Dalcin {
1767efe9872eSLisandro Dalcin   DM             dm;
1768efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1769efe9872eSLisandro Dalcin   void           *ctx;
1770efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1771efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1772efe9872eSLisandro Dalcin 
1773efe9872eSLisandro Dalcin   PetscFunctionBegin;
1774efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1775efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1776efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1777efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1778efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1779efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1780efe9872eSLisandro Dalcin 
1781efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1782efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1783efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1784efe9872eSLisandro Dalcin 
1785efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1786efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1787efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1788efe9872eSLisandro Dalcin   }
1789efe9872eSLisandro Dalcin 
1790efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1791efe9872eSLisandro Dalcin 
1792efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1793efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1794efe9872eSLisandro Dalcin   PetscStackPop;
1795efe9872eSLisandro Dalcin 
1796efe9872eSLisandro Dalcin   if (rhsjacobian) {
1797efe9872eSLisandro Dalcin     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1798efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1799efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1800efe9872eSLisandro Dalcin     ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr);
1801efe9872eSLisandro Dalcin     if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);}
1802efe9872eSLisandro Dalcin   }
1803efe9872eSLisandro Dalcin 
1804efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1805efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1806efe9872eSLisandro Dalcin }
1807efe9872eSLisandro Dalcin 
1808efe9872eSLisandro Dalcin /*@
1809efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1810efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1811efe9872eSLisandro Dalcin 
1812efe9872eSLisandro Dalcin    Logically Collective on TS and Vec
1813efe9872eSLisandro Dalcin 
1814efe9872eSLisandro Dalcin    Input Parameters:
1815efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1816efe9872eSLisandro Dalcin .  u - the solution vector
1817efe9872eSLisandro Dalcin -  v - the time derivative vector
1818efe9872eSLisandro Dalcin 
1819efe9872eSLisandro Dalcin    Level: beginner
1820efe9872eSLisandro Dalcin 
1821efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions
1822efe9872eSLisandro Dalcin @*/
1823efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1824efe9872eSLisandro Dalcin {
1825efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1826efe9872eSLisandro Dalcin 
1827efe9872eSLisandro Dalcin   PetscFunctionBegin;
1828efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1829efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1830efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1831efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1832efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1833efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1834efe9872eSLisandro Dalcin   ts->vec_dot = v;
1835efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1836efe9872eSLisandro Dalcin }
1837efe9872eSLisandro Dalcin 
1838efe9872eSLisandro Dalcin /*@
1839efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1840efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1841efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1842efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1843efe9872eSLisandro Dalcin 
1844efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1845efe9872eSLisandro Dalcin 
1846efe9872eSLisandro Dalcin    Input Parameter:
1847efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1848efe9872eSLisandro Dalcin 
1849efe9872eSLisandro Dalcin    Output Parameter:
1850efe9872eSLisandro Dalcin +  u - the vector containing the solution
1851efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1852efe9872eSLisandro Dalcin 
1853efe9872eSLisandro Dalcin    Level: intermediate
1854efe9872eSLisandro Dalcin 
1855efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1856efe9872eSLisandro Dalcin 
1857efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution
1858efe9872eSLisandro Dalcin @*/
1859efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1860efe9872eSLisandro Dalcin {
1861efe9872eSLisandro Dalcin   PetscFunctionBegin;
1862efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1863efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1864efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1865efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1866efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1867efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1868efe9872eSLisandro Dalcin }
1869efe9872eSLisandro Dalcin 
187055849f57SBarry Smith /*@C
187155849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
187255849f57SBarry Smith 
187355849f57SBarry Smith   Collective on PetscViewer
187455849f57SBarry Smith 
187555849f57SBarry Smith   Input Parameters:
187655849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
187755849f57SBarry Smith            some related function before a call to TSLoad().
187855849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
187955849f57SBarry Smith 
188055849f57SBarry Smith    Level: intermediate
188155849f57SBarry Smith 
188255849f57SBarry Smith   Notes:
188355849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
188455849f57SBarry Smith 
188555849f57SBarry Smith   Notes for advanced users:
188655849f57SBarry Smith   Most users should not need to know the details of the binary storage
188755849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
188855849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
188955849f57SBarry Smith   format is
189055849f57SBarry Smith .vb
189155849f57SBarry Smith      has not yet been determined
189255849f57SBarry Smith .ve
189355849f57SBarry Smith 
189455849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
189555849f57SBarry Smith @*/
1896f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
189755849f57SBarry Smith {
189855849f57SBarry Smith   PetscErrorCode ierr;
189955849f57SBarry Smith   PetscBool      isbinary;
190055849f57SBarry Smith   PetscInt       classid;
190155849f57SBarry Smith   char           type[256];
19022d53ad75SBarry Smith   DMTS           sdm;
1903ad6bc421SBarry Smith   DM             dm;
190455849f57SBarry Smith 
190555849f57SBarry Smith   PetscFunctionBegin;
1906f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
190755849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
190855849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
190955849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
191055849f57SBarry Smith 
1911060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1912ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1913060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1914f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1915f2c2a1b9SBarry Smith   if (ts->ops->load) {
1916f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1917f2c2a1b9SBarry Smith   }
1918ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1919ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1920ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1921f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1922f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
19232d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19242d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
192555849f57SBarry Smith   PetscFunctionReturn(0);
192655849f57SBarry Smith }
192755849f57SBarry Smith 
19289804daf3SBarry Smith #include <petscdraw.h>
1929e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1930e04113cfSBarry Smith #include <petscviewersaws.h>
1931f05ece33SBarry Smith #endif
19327e2c5f70SBarry Smith /*@C
1933d763cef2SBarry Smith     TSView - Prints the TS data structure.
1934d763cef2SBarry Smith 
19354c49b128SBarry Smith     Collective on TS
1936d763cef2SBarry Smith 
1937d763cef2SBarry Smith     Input Parameters:
1938d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1939d763cef2SBarry Smith -   viewer - visualization context
1940d763cef2SBarry Smith 
1941d763cef2SBarry Smith     Options Database Key:
1942d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1943d763cef2SBarry Smith 
1944d763cef2SBarry Smith     Notes:
1945d763cef2SBarry Smith     The available visualization contexts include
1946b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1947b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1948d763cef2SBarry Smith          output where only the first processor opens
1949d763cef2SBarry Smith          the file.  All other processors send their
1950d763cef2SBarry Smith          data to the first processor to print.
1951d763cef2SBarry Smith 
1952d763cef2SBarry Smith     The user can open an alternative visualization context with
1953b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1954d763cef2SBarry Smith 
1955d763cef2SBarry Smith     Level: beginner
1956d763cef2SBarry Smith 
1957d763cef2SBarry Smith .keywords: TS, timestep, view
1958d763cef2SBarry Smith 
1959b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1960d763cef2SBarry Smith @*/
19617087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1962d763cef2SBarry Smith {
1963dfbe8321SBarry Smith   PetscErrorCode ierr;
196419fd82e9SBarry Smith   TSType         type;
19652b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
19662d53ad75SBarry Smith   DMTS           sdm;
1967e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1968536b137fSBarry Smith   PetscBool      issaws;
1969f05ece33SBarry Smith #endif
1970d763cef2SBarry Smith 
1971d763cef2SBarry Smith   PetscFunctionBegin;
19720700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19733050cee2SBarry Smith   if (!viewer) {
1974ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
19753050cee2SBarry Smith   }
19760700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1977c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1978fd16b177SBarry Smith 
1979251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1980251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
198155849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
19822b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1983e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1984536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1985f05ece33SBarry Smith #endif
198632077d6dSBarry Smith   if (iascii) {
1987dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
1988efd4aadfSBarry Smith     if (ts->ops->view) {
1989efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1990efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1991efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1992efd4aadfSBarry Smith     }
1993ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
199477431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1995ef85077eSLisandro Dalcin     }
1996ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
19977c8652ddSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1998ef85077eSLisandro Dalcin     }
1999efd4aadfSBarry Smith     if (ts->usessnes) {
2000efd4aadfSBarry Smith       PetscBool lin;
2001d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
20025ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
2003d763cef2SBarry Smith       }
20045ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
20051ef27442SStefano Zampini       ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
2006efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
2007efd4aadfSBarry Smith     }
2008193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
2009a0af407cSBarry Smith     if (ts->vrtol) {
2010a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
2011a0af407cSBarry Smith     } else {
2012a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
2013a0af407cSBarry Smith     }
2014a0af407cSBarry Smith     if (ts->vatol) {
2015a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
2016a0af407cSBarry Smith     } else {
2017a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
2018a0af407cSBarry Smith     }
2019825ab935SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2020efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
2021825ab935SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2022825ab935SBarry Smith     if (ts->snes && ts->usessnes)  {
2023825ab935SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2024825ab935SBarry Smith       ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
2025825ab935SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2026825ab935SBarry Smith     }
20272d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
20282d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
20290f5bd95cSBarry Smith   } else if (isstring) {
2030a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
2031b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
203255849f57SBarry Smith   } else if (isbinary) {
203355849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
203455849f57SBarry Smith     MPI_Comm    comm;
203555849f57SBarry Smith     PetscMPIInt rank;
203655849f57SBarry Smith     char        type[256];
203755849f57SBarry Smith 
203855849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
203955849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
204055849f57SBarry Smith     if (!rank) {
204155849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
204255849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
204355849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
204455849f57SBarry Smith     }
204555849f57SBarry Smith     if (ts->ops->view) {
204655849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
204755849f57SBarry Smith     }
2048efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2049f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
2050f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
20512d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
20522d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
20532b0a91c0SBarry Smith   } else if (isdraw) {
20542b0a91c0SBarry Smith     PetscDraw draw;
20552b0a91c0SBarry Smith     char      str[36];
205689fd9fafSBarry Smith     PetscReal x,y,bottom,h;
20572b0a91c0SBarry Smith 
20582b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
20592b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
20602b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
20612b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
206251fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
206389fd9fafSBarry Smith     bottom = y - h;
20642b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
20652b0a91c0SBarry Smith     if (ts->ops->view) {
20662b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
20672b0a91c0SBarry Smith     }
2068efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2069efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
20702b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2071e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2072536b137fSBarry Smith   } else if (issaws) {
2073d45a07a7SBarry Smith     PetscMPIInt rank;
20742657e9d9SBarry Smith     const char  *name;
20752657e9d9SBarry Smith 
20762657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
2077d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
2078d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
2079d45a07a7SBarry Smith       char       dir[1024];
2080d45a07a7SBarry Smith 
2081e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2082a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
20832657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
20842657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
20852657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2086d763cef2SBarry Smith     }
20870acecf5bSBarry Smith     if (ts->ops->view) {
20880acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
20890acecf5bSBarry Smith     }
2090f05ece33SBarry Smith #endif
2091f05ece33SBarry Smith   }
2092f05ece33SBarry Smith 
2093b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2094251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2095b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2096d763cef2SBarry Smith   PetscFunctionReturn(0);
2097d763cef2SBarry Smith }
2098d763cef2SBarry Smith 
2099b07ff414SBarry Smith /*@
2100d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2101d763cef2SBarry Smith    the timesteppers.
2102d763cef2SBarry Smith 
21033f9fe445SBarry Smith    Logically Collective on TS
2104d763cef2SBarry Smith 
2105d763cef2SBarry Smith    Input Parameters:
2106d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2107d763cef2SBarry Smith -  usrP - optional user context
2108d763cef2SBarry Smith 
210995452b02SPatrick Sanan    Fortran Notes:
211095452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2111daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2112daf670e6SBarry Smith 
2113d763cef2SBarry Smith    Level: intermediate
2114d763cef2SBarry Smith 
2115d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
2116d763cef2SBarry Smith 
2117d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2118d763cef2SBarry Smith @*/
21197087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2120d763cef2SBarry Smith {
2121d763cef2SBarry Smith   PetscFunctionBegin;
21220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2123d763cef2SBarry Smith   ts->user = usrP;
2124d763cef2SBarry Smith   PetscFunctionReturn(0);
2125d763cef2SBarry Smith }
2126d763cef2SBarry Smith 
2127b07ff414SBarry Smith /*@
2128d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2129d763cef2SBarry Smith     timestepper.
2130d763cef2SBarry Smith 
2131d763cef2SBarry Smith     Not Collective
2132d763cef2SBarry Smith 
2133d763cef2SBarry Smith     Input Parameter:
2134d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2135d763cef2SBarry Smith 
2136d763cef2SBarry Smith     Output Parameter:
2137d763cef2SBarry Smith .   usrP - user context
2138d763cef2SBarry Smith 
213995452b02SPatrick Sanan    Fortran Notes:
214095452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2141daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2142daf670e6SBarry Smith 
2143d763cef2SBarry Smith     Level: intermediate
2144d763cef2SBarry Smith 
2145d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
2146d763cef2SBarry Smith 
2147d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2148d763cef2SBarry Smith @*/
2149e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2150d763cef2SBarry Smith {
2151d763cef2SBarry Smith   PetscFunctionBegin;
21520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2153e71120c6SJed Brown   *(void**)usrP = ts->user;
2154d763cef2SBarry Smith   PetscFunctionReturn(0);
2155d763cef2SBarry Smith }
2156d763cef2SBarry Smith 
2157d763cef2SBarry Smith /*@
215880275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2159d763cef2SBarry Smith 
2160d763cef2SBarry Smith    Not Collective
2161d763cef2SBarry Smith 
2162d763cef2SBarry Smith    Input Parameter:
2163d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2164d763cef2SBarry Smith 
2165d763cef2SBarry Smith    Output Parameter:
216680275a0aSLisandro Dalcin .  steps - number of steps completed so far
2167d763cef2SBarry Smith 
2168d763cef2SBarry Smith    Level: intermediate
2169d763cef2SBarry Smith 
2170d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
21719be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2172d763cef2SBarry Smith @*/
217380275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2174d763cef2SBarry Smith {
2175d763cef2SBarry Smith   PetscFunctionBegin;
21760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
217780275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
217880275a0aSLisandro Dalcin   *steps = ts->steps;
217980275a0aSLisandro Dalcin   PetscFunctionReturn(0);
218080275a0aSLisandro Dalcin }
218180275a0aSLisandro Dalcin 
218280275a0aSLisandro Dalcin /*@
218380275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
218480275a0aSLisandro Dalcin 
218580275a0aSLisandro Dalcin    Logically Collective on TS
218680275a0aSLisandro Dalcin 
218780275a0aSLisandro Dalcin    Input Parameters:
218880275a0aSLisandro Dalcin +  ts - the TS context
218980275a0aSLisandro Dalcin -  steps - number of steps completed so far
219080275a0aSLisandro Dalcin 
219180275a0aSLisandro Dalcin    Notes:
219280275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
219380275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
219480275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
219580275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
219680275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
219780275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
219880275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
219980275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
220080275a0aSLisandro Dalcin 
220180275a0aSLisandro Dalcin    Level: advanced
220280275a0aSLisandro Dalcin 
220380275a0aSLisandro Dalcin .keywords: TS, timestep, set, iteration, number
220480275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
220580275a0aSLisandro Dalcin @*/
220680275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
220780275a0aSLisandro Dalcin {
220880275a0aSLisandro Dalcin   PetscFunctionBegin;
220980275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
221080275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
221180275a0aSLisandro Dalcin   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
221280275a0aSLisandro Dalcin   ts->steps = steps;
2213d763cef2SBarry Smith   PetscFunctionReturn(0);
2214d763cef2SBarry Smith }
2215d763cef2SBarry Smith 
2216d763cef2SBarry Smith /*@
2217d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2218d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2219d763cef2SBarry Smith 
22203f9fe445SBarry Smith    Logically Collective on TS
2221d763cef2SBarry Smith 
2222d763cef2SBarry Smith    Input Parameters:
2223d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2224d763cef2SBarry Smith -  time_step - the size of the timestep
2225d763cef2SBarry Smith 
2226d763cef2SBarry Smith    Level: intermediate
2227d763cef2SBarry Smith 
2228aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2229d763cef2SBarry Smith 
2230d763cef2SBarry Smith .keywords: TS, set, timestep
2231d763cef2SBarry Smith @*/
22327087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2233d763cef2SBarry Smith {
2234d763cef2SBarry Smith   PetscFunctionBegin;
22350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2236c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2237d763cef2SBarry Smith   ts->time_step = time_step;
2238d763cef2SBarry Smith   PetscFunctionReturn(0);
2239d763cef2SBarry Smith }
2240d763cef2SBarry Smith 
2241a43b19c4SJed Brown /*@
224249354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
224349354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
224449354f04SShri Abhyankar      or just return at the final time TS computed.
2245a43b19c4SJed Brown 
2246a43b19c4SJed Brown   Logically Collective on TS
2247a43b19c4SJed Brown 
2248a43b19c4SJed Brown    Input Parameter:
2249a43b19c4SJed Brown +   ts - the time-step context
225049354f04SShri Abhyankar -   eftopt - exact final time option
2251a43b19c4SJed Brown 
2252feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2253feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2254feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2255feed9e9dSBarry Smith 
2256feed9e9dSBarry Smith    Options Database:
2257feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2258feed9e9dSBarry Smith 
2259ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2260ee346746SBarry Smith     then the final time you selected.
2261ee346746SBarry Smith 
2262a43b19c4SJed Brown    Level: beginner
2263a43b19c4SJed Brown 
2264f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2265a43b19c4SJed Brown @*/
226649354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2267a43b19c4SJed Brown {
2268a43b19c4SJed Brown   PetscFunctionBegin;
2269a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
227049354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
227149354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2272a43b19c4SJed Brown   PetscFunctionReturn(0);
2273a43b19c4SJed Brown }
2274a43b19c4SJed Brown 
2275d763cef2SBarry Smith /*@
2276f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2277f6953c82SLisandro Dalcin 
2278f6953c82SLisandro Dalcin    Not Collective
2279f6953c82SLisandro Dalcin 
2280f6953c82SLisandro Dalcin    Input Parameter:
2281f6953c82SLisandro Dalcin .  ts - the TS context
2282f6953c82SLisandro Dalcin 
2283f6953c82SLisandro Dalcin    Output Parameter:
2284f6953c82SLisandro Dalcin .  eftopt - exact final time option
2285f6953c82SLisandro Dalcin 
2286f6953c82SLisandro Dalcin    Level: beginner
2287f6953c82SLisandro Dalcin 
2288f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2289f6953c82SLisandro Dalcin @*/
2290f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2291f6953c82SLisandro Dalcin {
2292f6953c82SLisandro Dalcin   PetscFunctionBegin;
2293f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2294f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2295f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2296f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2297f6953c82SLisandro Dalcin }
2298f6953c82SLisandro Dalcin 
2299f6953c82SLisandro Dalcin /*@
2300d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2301d763cef2SBarry Smith 
2302d763cef2SBarry Smith    Not Collective
2303d763cef2SBarry Smith 
2304d763cef2SBarry Smith    Input Parameter:
2305d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2306d763cef2SBarry Smith 
2307d763cef2SBarry Smith    Output Parameter:
2308d763cef2SBarry Smith .  dt - the current timestep size
2309d763cef2SBarry Smith 
2310d763cef2SBarry Smith    Level: intermediate
2311d763cef2SBarry Smith 
2312aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2313d763cef2SBarry Smith 
2314d763cef2SBarry Smith .keywords: TS, get, timestep
2315d763cef2SBarry Smith @*/
23167087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2317d763cef2SBarry Smith {
2318d763cef2SBarry Smith   PetscFunctionBegin;
23190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2320f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2321d763cef2SBarry Smith   *dt = ts->time_step;
2322d763cef2SBarry Smith   PetscFunctionReturn(0);
2323d763cef2SBarry Smith }
2324d763cef2SBarry Smith 
2325d8e5e3e6SSatish Balay /*@
2326d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2327d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2328d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2329d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2330d763cef2SBarry Smith 
2331d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2332d763cef2SBarry Smith 
2333d763cef2SBarry Smith    Input Parameter:
2334d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2335d763cef2SBarry Smith 
2336d763cef2SBarry Smith    Output Parameter:
2337d763cef2SBarry Smith .  v - the vector containing the solution
2338d763cef2SBarry Smith 
233963e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
234063e21af5SBarry Smith    final time. It returns the solution at the next timestep.
234163e21af5SBarry Smith 
2342d763cef2SBarry Smith    Level: intermediate
2343d763cef2SBarry Smith 
23440ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2345d763cef2SBarry Smith 
2346d763cef2SBarry Smith .keywords: TS, timestep, get, solution
2347d763cef2SBarry Smith @*/
23487087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2349d763cef2SBarry Smith {
2350d763cef2SBarry Smith   PetscFunctionBegin;
23510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23524482741eSBarry Smith   PetscValidPointer(v,2);
23538737fe31SLisandro Dalcin   *v = ts->vec_sol;
2354d763cef2SBarry Smith   PetscFunctionReturn(0);
2355d763cef2SBarry Smith }
2356d763cef2SBarry Smith 
235703fe5f5eSDebojyoti Ghosh /*@
2358b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
235903fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2360b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
236103fe5f5eSDebojyoti Ghosh    structure as the solution vector.
236203fe5f5eSDebojyoti Ghosh 
236303fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
236403fe5f5eSDebojyoti Ghosh 
236503fe5f5eSDebojyoti Ghosh    Parameters :
236603fe5f5eSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2367b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2368b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
236903fe5f5eSDebojyoti Ghosh        returned in v.
2370b2bf4f3aSDebojyoti Ghosh .  v - the vector containing the n-th solution component
237103fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2372b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
237303fe5f5eSDebojyoti Ghosh 
23744cdd57e5SDebojyoti Ghosh    Level: advanced
237503fe5f5eSDebojyoti Ghosh 
237603fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
237703fe5f5eSDebojyoti Ghosh 
237803fe5f5eSDebojyoti Ghosh .keywords: TS, timestep, get, solution
237903fe5f5eSDebojyoti Ghosh @*/
2380b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
238103fe5f5eSDebojyoti Ghosh {
238203fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
238303fe5f5eSDebojyoti Ghosh 
238403fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
238503fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2386b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
238703fe5f5eSDebojyoti Ghosh   else {
2388b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
238903fe5f5eSDebojyoti Ghosh   }
239003fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
239103fe5f5eSDebojyoti Ghosh }
239203fe5f5eSDebojyoti Ghosh 
23934cdd57e5SDebojyoti Ghosh /*@
23944cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
23954cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
23964cdd57e5SDebojyoti Ghosh 
23974cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
23984cdd57e5SDebojyoti Ghosh 
23994cdd57e5SDebojyoti Ghosh    Parameters :
24004cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
24014cdd57e5SDebojyoti Ghosh .  v - the vector containing the auxiliary solution
24024cdd57e5SDebojyoti Ghosh 
24034cdd57e5SDebojyoti Ghosh    Level: intermediate
24044cdd57e5SDebojyoti Ghosh 
24054cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
24064cdd57e5SDebojyoti Ghosh 
24074cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, solution
24084cdd57e5SDebojyoti Ghosh @*/
24094cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
24104cdd57e5SDebojyoti Ghosh {
24114cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
24124cdd57e5SDebojyoti Ghosh 
24134cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
24144cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2415f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
24164cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2417f6356ec7SDebojyoti Ghosh   } else {
2418f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v); CHKERRQ(ierr);
2419f6356ec7SDebojyoti Ghosh   }
24204cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
24214cdd57e5SDebojyoti Ghosh }
24224cdd57e5SDebojyoti Ghosh 
24234cdd57e5SDebojyoti Ghosh /*@
24244cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
24254cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
24264cdd57e5SDebojyoti Ghosh 
24274cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
24284cdd57e5SDebojyoti Ghosh 
24299657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
24309657682dSDebojyoti Ghosh 
24314cdd57e5SDebojyoti Ghosh    Parameters :
24324cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2433657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
24344cdd57e5SDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
24354cdd57e5SDebojyoti Ghosh 
24364cdd57e5SDebojyoti Ghosh    Level: intermediate
24374cdd57e5SDebojyoti Ghosh 
243857df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
24394cdd57e5SDebojyoti Ghosh 
24404cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, error
24414cdd57e5SDebojyoti Ghosh @*/
24420a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
24434cdd57e5SDebojyoti Ghosh {
24444cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
24454cdd57e5SDebojyoti Ghosh 
24464cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
24474cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2448f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
24490a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2450f6356ec7SDebojyoti Ghosh   } else {
2451f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2452f6356ec7SDebojyoti Ghosh   }
24534cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
24544cdd57e5SDebojyoti Ghosh }
24554cdd57e5SDebojyoti Ghosh 
245657df6a1bSDebojyoti Ghosh /*@
245757df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
245857df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
245957df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
246057df6a1bSDebojyoti Ghosh 
246157df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
246257df6a1bSDebojyoti Ghosh 
246357df6a1bSDebojyoti Ghosh    Parameters :
246457df6a1bSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
246557df6a1bSDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
246657df6a1bSDebojyoti Ghosh 
246757df6a1bSDebojyoti Ghosh    Level: intermediate
246857df6a1bSDebojyoti Ghosh 
246957df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
247057df6a1bSDebojyoti Ghosh 
247157df6a1bSDebojyoti Ghosh .keywords: TS, timestep, get, error
247257df6a1bSDebojyoti Ghosh @*/
247357df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
247457df6a1bSDebojyoti Ghosh {
247557df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
247657df6a1bSDebojyoti Ghosh 
247757df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
247857df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24799657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
248057df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
248157df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
248257df6a1bSDebojyoti Ghosh   }
248357df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
248457df6a1bSDebojyoti Ghosh }
248557df6a1bSDebojyoti Ghosh 
2486bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2487d8e5e3e6SSatish Balay /*@
2488bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2489d763cef2SBarry Smith 
2490bdad233fSMatthew Knepley   Not collective
2491d763cef2SBarry Smith 
2492bdad233fSMatthew Knepley   Input Parameters:
2493bdad233fSMatthew Knepley + ts   - The TS
2494bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2495d763cef2SBarry Smith .vb
24960910c330SBarry Smith          U_t - A U = 0      (linear)
24970910c330SBarry Smith          U_t - A(t) U = 0   (linear)
24980910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2499d763cef2SBarry Smith .ve
2500d763cef2SBarry Smith 
2501d763cef2SBarry Smith    Level: beginner
2502d763cef2SBarry Smith 
2503bdad233fSMatthew Knepley .keywords: TS, problem type
2504bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2505d763cef2SBarry Smith @*/
25067087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2507a7cc72afSBarry Smith {
25089e2a6581SJed Brown   PetscErrorCode ierr;
25099e2a6581SJed Brown 
2510d763cef2SBarry Smith   PetscFunctionBegin;
25110700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2512bdad233fSMatthew Knepley   ts->problem_type = type;
25139e2a6581SJed Brown   if (type == TS_LINEAR) {
25149e2a6581SJed Brown     SNES snes;
25159e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
25169e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
25179e2a6581SJed Brown   }
2518d763cef2SBarry Smith   PetscFunctionReturn(0);
2519d763cef2SBarry Smith }
2520d763cef2SBarry Smith 
2521bdad233fSMatthew Knepley /*@C
2522bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2523bdad233fSMatthew Knepley 
2524bdad233fSMatthew Knepley   Not collective
2525bdad233fSMatthew Knepley 
2526bdad233fSMatthew Knepley   Input Parameter:
2527bdad233fSMatthew Knepley . ts   - The TS
2528bdad233fSMatthew Knepley 
2529bdad233fSMatthew Knepley   Output Parameter:
2530bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2531bdad233fSMatthew Knepley .vb
2532089b2837SJed Brown          M U_t = A U
2533089b2837SJed Brown          M(t) U_t = A(t) U
2534b5abc632SBarry Smith          F(t,U,U_t)
2535bdad233fSMatthew Knepley .ve
2536bdad233fSMatthew Knepley 
2537bdad233fSMatthew Knepley    Level: beginner
2538bdad233fSMatthew Knepley 
2539bdad233fSMatthew Knepley .keywords: TS, problem type
2540bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2541bdad233fSMatthew Knepley @*/
25427087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2543a7cc72afSBarry Smith {
2544bdad233fSMatthew Knepley   PetscFunctionBegin;
25450700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
25464482741eSBarry Smith   PetscValidIntPointer(type,2);
2547bdad233fSMatthew Knepley   *type = ts->problem_type;
2548bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2549bdad233fSMatthew Knepley }
2550d763cef2SBarry Smith 
2551d763cef2SBarry Smith /*@
2552d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
2553d763cef2SBarry Smith    of a timestepper.
2554d763cef2SBarry Smith 
2555d763cef2SBarry Smith    Collective on TS
2556d763cef2SBarry Smith 
2557d763cef2SBarry Smith    Input Parameter:
2558d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2559d763cef2SBarry Smith 
2560d763cef2SBarry Smith    Notes:
2561d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2562d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2563141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2564d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2565141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2566d763cef2SBarry Smith 
2567d763cef2SBarry Smith    Level: advanced
2568d763cef2SBarry Smith 
2569d763cef2SBarry Smith .keywords: TS, timestep, setup
2570d763cef2SBarry Smith 
2571141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2572d763cef2SBarry Smith @*/
25737087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2574d763cef2SBarry Smith {
2575dfbe8321SBarry Smith   PetscErrorCode ierr;
25766c6b9e74SPeter Brune   DM             dm;
25776c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2578d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2579cd11d68dSLisandro Dalcin   TSIFunction    ifun;
25806c6b9e74SPeter Brune   TSIJacobian    ijac;
2581efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
25826c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
25832ffb9264SLisandro Dalcin   PetscBool      isnone;
2584d763cef2SBarry Smith 
2585d763cef2SBarry Smith   PetscFunctionBegin;
25860700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2587277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2588277b19d0SLisandro Dalcin 
25897adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2590cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2591cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2592d763cef2SBarry Smith   }
2593277b19d0SLisandro Dalcin 
2594277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
2595277b19d0SLisandro Dalcin 
2596971015bcSStefano Zampini   ierr = TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL);CHKERRQ(ierr);
2597971015bcSStefano Zampini   if (ts->rhsjacobian.reuse && rhsjac == TSComputeRHSJacobianConstant) {
2598e1244c69SJed Brown     Mat Amat,Pmat;
2599e1244c69SJed Brown     SNES snes;
2600e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2601e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2602e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2603e1244c69SJed Brown      * have displaced the RHS matrix */
2604971015bcSStefano Zampini     if (Amat && Amat == ts->Arhs) {
2605abc0d4abSBarry 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 */
2606abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2607e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2608e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2609e1244c69SJed Brown     }
2610971015bcSStefano Zampini     if (Pmat && Pmat == ts->Brhs) {
2611abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2612e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2613e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2614e1244c69SJed Brown     }
2615e1244c69SJed Brown   }
26162ffb9264SLisandro Dalcin 
26172ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
26182ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
26192ffb9264SLisandro Dalcin 
2620277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2621000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2622277b19d0SLisandro Dalcin   }
2623277b19d0SLisandro Dalcin 
26242ffb9264SLisandro Dalcin   /* Attempt to check/preset a default value for the exact final time option */
26252ffb9264SLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
26262ffb9264SLisandro Dalcin   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED)
26272ffb9264SLisandro Dalcin     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
26282ffb9264SLisandro Dalcin 
2629a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
26306c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
26316c6b9e74SPeter Brune    */
26326c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
26330298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
26346c6b9e74SPeter Brune   if (!func) {
26356c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
26366c6b9e74SPeter Brune   }
2637a6772fa2SLisandro 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.
26386c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
26396c6b9e74SPeter Brune    */
26400298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
26410298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2642efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
26430298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2644efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
26456c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
26466c6b9e74SPeter Brune   }
2647c0517034SDebojyoti Ghosh 
2648c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2649c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2650c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2651c0517034SDebojyoti Ghosh   }
2652c0517034SDebojyoti Ghosh 
2653277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2654277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2655277b19d0SLisandro Dalcin }
2656277b19d0SLisandro Dalcin 
2657f6a906c0SBarry Smith /*@
2658277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2659277b19d0SLisandro Dalcin 
2660277b19d0SLisandro Dalcin    Collective on TS
2661277b19d0SLisandro Dalcin 
2662277b19d0SLisandro Dalcin    Input Parameter:
2663277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2664277b19d0SLisandro Dalcin 
2665277b19d0SLisandro Dalcin    Level: beginner
2666277b19d0SLisandro Dalcin 
2667277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
2668277b19d0SLisandro Dalcin 
2669277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2670277b19d0SLisandro Dalcin @*/
2671277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2672277b19d0SLisandro Dalcin {
26731d06f6b3SHong Zhang   TS_RHSSplitLink ilink = ts->tsrhssplit,next;
2674277b19d0SLisandro Dalcin   PetscErrorCode  ierr;
2675277b19d0SLisandro Dalcin 
2676277b19d0SLisandro Dalcin   PetscFunctionBegin;
2677277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2678b18ea86cSHong Zhang 
2679277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2680277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2681277b19d0SLisandro Dalcin   }
2682277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2683e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2684bbd56ea5SKarl Rupp 
26854e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
26864e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2687214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
26886bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2689efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2690e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2691e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
269238637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2693bbd56ea5SKarl Rupp 
2694d8151eeaSBarry Smith   ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2695d8151eeaSBarry Smith   ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2696715f1b00SHong Zhang 
2697ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
26982c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
269936eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
270013b1b0ffSHong Zhang   ierr = MatDestroy(&ts->mat_sensip);CHKERRQ(ierr);
2701022c081aSHong Zhang 
27021d06f6b3SHong Zhang   while (ilink) {
27031d06f6b3SHong Zhang     next = ilink->next;
27041d06f6b3SHong Zhang     ierr = TSDestroy(&ilink->ts);CHKERRQ(ierr);
27051d06f6b3SHong Zhang     ierr = PetscFree(ilink->splitname);CHKERRQ(ierr);
27061d06f6b3SHong Zhang     ierr = ISDestroy(&ilink->is);CHKERRQ(ierr);
27071d06f6b3SHong Zhang     ierr = PetscFree(ilink);CHKERRQ(ierr);
27081d06f6b3SHong Zhang     ilink = next;
270987f4e208SHong Zhang   }
2710545aaa6fSHong Zhang   ts->num_rhs_splits = 0;
2711277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2712d763cef2SBarry Smith   PetscFunctionReturn(0);
2713d763cef2SBarry Smith }
2714d763cef2SBarry Smith 
2715d8e5e3e6SSatish Balay /*@
2716d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2717d763cef2SBarry Smith    with TSCreate().
2718d763cef2SBarry Smith 
2719d763cef2SBarry Smith    Collective on TS
2720d763cef2SBarry Smith 
2721d763cef2SBarry Smith    Input Parameter:
2722d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2723d763cef2SBarry Smith 
2724d763cef2SBarry Smith    Level: beginner
2725d763cef2SBarry Smith 
2726d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2727d763cef2SBarry Smith 
2728d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2729d763cef2SBarry Smith @*/
27306bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2731d763cef2SBarry Smith {
27326849ba73SBarry Smith   PetscErrorCode ierr;
2733d763cef2SBarry Smith 
2734d763cef2SBarry Smith   PetscFunctionBegin;
27356bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
27366bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
27376bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2738d763cef2SBarry Smith 
27396bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2740277b19d0SLisandro Dalcin 
2741e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2742e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
27436bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
27446d4c513bSLisandro Dalcin 
2745bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2746bc952696SBarry Smith 
274784df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
27486427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
27496427ac75SLisandro Dalcin 
27506bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
27516bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
27526bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
27530dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
27546d4c513bSLisandro Dalcin 
2755a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2756d763cef2SBarry Smith   PetscFunctionReturn(0);
2757d763cef2SBarry Smith }
2758d763cef2SBarry Smith 
2759d8e5e3e6SSatish Balay /*@
2760d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2761d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2762d763cef2SBarry Smith 
2763d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2764d763cef2SBarry Smith 
2765d763cef2SBarry Smith    Input Parameter:
2766d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2767d763cef2SBarry Smith 
2768d763cef2SBarry Smith    Output Parameter:
2769d763cef2SBarry Smith .  snes - the nonlinear solver context
2770d763cef2SBarry Smith 
2771d763cef2SBarry Smith    Notes:
2772d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2773d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
277494b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2775d763cef2SBarry Smith 
2776d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
27770298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2778d763cef2SBarry Smith 
2779d763cef2SBarry Smith    Level: beginner
2780d763cef2SBarry Smith 
2781d763cef2SBarry Smith .keywords: timestep, get, SNES
2782d763cef2SBarry Smith @*/
27837087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2784d763cef2SBarry Smith {
2785d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2786d372ba47SLisandro Dalcin 
2787d763cef2SBarry Smith   PetscFunctionBegin;
27880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27894482741eSBarry Smith   PetscValidPointer(snes,2);
2790d372ba47SLisandro Dalcin   if (!ts->snes) {
2791ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
279216413a6aSBarry Smith     ierr = PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options);CHKERRQ(ierr);
27930298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27943bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2795d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2796496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
27979e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
27989e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
27999e2a6581SJed Brown     }
2800d372ba47SLisandro Dalcin   }
2801d763cef2SBarry Smith   *snes = ts->snes;
2802d763cef2SBarry Smith   PetscFunctionReturn(0);
2803d763cef2SBarry Smith }
2804d763cef2SBarry Smith 
2805deb2cd25SJed Brown /*@
2806deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2807deb2cd25SJed Brown 
2808deb2cd25SJed Brown    Collective
2809deb2cd25SJed Brown 
2810deb2cd25SJed Brown    Input Parameter:
2811deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2812deb2cd25SJed Brown -  snes - the nonlinear solver context
2813deb2cd25SJed Brown 
2814deb2cd25SJed Brown    Notes:
2815deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2816deb2cd25SJed Brown 
2817deb2cd25SJed Brown    Level: developer
2818deb2cd25SJed Brown 
2819deb2cd25SJed Brown .keywords: timestep, set, SNES
2820deb2cd25SJed Brown @*/
2821deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2822deb2cd25SJed Brown {
2823deb2cd25SJed Brown   PetscErrorCode ierr;
2824d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2825deb2cd25SJed Brown 
2826deb2cd25SJed Brown   PetscFunctionBegin;
2827deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2828deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2829deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2830deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2831bbd56ea5SKarl Rupp 
2832deb2cd25SJed Brown   ts->snes = snes;
2833bbd56ea5SKarl Rupp 
28340298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
28350298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2836740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
28370298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2838740132f1SEmil Constantinescu   }
2839deb2cd25SJed Brown   PetscFunctionReturn(0);
2840deb2cd25SJed Brown }
2841deb2cd25SJed Brown 
2842d8e5e3e6SSatish Balay /*@
284394b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2844d763cef2SBarry Smith    a TS (timestepper) context.
2845d763cef2SBarry Smith 
284694b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2847d763cef2SBarry Smith 
2848d763cef2SBarry Smith    Input Parameter:
2849d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2850d763cef2SBarry Smith 
2851d763cef2SBarry Smith    Output Parameter:
285294b7f48cSBarry Smith .  ksp - the nonlinear solver context
2853d763cef2SBarry Smith 
2854d763cef2SBarry Smith    Notes:
285594b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2856d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2857d763cef2SBarry Smith    KSP and PC contexts as well.
2858d763cef2SBarry Smith 
285994b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
28600298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2861d763cef2SBarry Smith 
2862d763cef2SBarry Smith    Level: beginner
2863d763cef2SBarry Smith 
286494b7f48cSBarry Smith .keywords: timestep, get, KSP
2865d763cef2SBarry Smith @*/
28667087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2867d763cef2SBarry Smith {
2868d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2869089b2837SJed Brown   SNES           snes;
2870d372ba47SLisandro Dalcin 
2871d763cef2SBarry Smith   PetscFunctionBegin;
28720700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28734482741eSBarry Smith   PetscValidPointer(ksp,2);
287417186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2875e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2876089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2877089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2878d763cef2SBarry Smith   PetscFunctionReturn(0);
2879d763cef2SBarry Smith }
2880d763cef2SBarry Smith 
2881d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2882d763cef2SBarry Smith 
2883adb62b0dSMatthew Knepley /*@
2884618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
2885618ce8baSLisandro Dalcin 
2886618ce8baSLisandro Dalcin    Logically Collective on TS
2887618ce8baSLisandro Dalcin 
2888618ce8baSLisandro Dalcin    Input Parameters:
2889618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2890618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
2891618ce8baSLisandro Dalcin 
2892618ce8baSLisandro Dalcin    Options Database Keys:
2893618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
2894618ce8baSLisandro Dalcin 
2895618ce8baSLisandro Dalcin    Notes:
2896618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
2897618ce8baSLisandro Dalcin 
2898618ce8baSLisandro Dalcin    Level: intermediate
2899618ce8baSLisandro Dalcin 
2900618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, steps
2901618ce8baSLisandro Dalcin 
2902618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
2903618ce8baSLisandro Dalcin @*/
2904618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
2905618ce8baSLisandro Dalcin {
2906618ce8baSLisandro Dalcin   PetscFunctionBegin;
2907618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2908618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2909618ce8baSLisandro Dalcin   if (maxsteps < 0 ) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
2910618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
2911618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2912618ce8baSLisandro Dalcin }
2913618ce8baSLisandro Dalcin 
2914618ce8baSLisandro Dalcin /*@
2915618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
2916618ce8baSLisandro Dalcin 
2917618ce8baSLisandro Dalcin    Not Collective
2918618ce8baSLisandro Dalcin 
2919618ce8baSLisandro Dalcin    Input Parameters:
2920618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2921618ce8baSLisandro Dalcin 
2922618ce8baSLisandro Dalcin    Output Parameter:
2923618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
2924618ce8baSLisandro Dalcin 
2925618ce8baSLisandro Dalcin    Level: advanced
2926618ce8baSLisandro Dalcin 
2927618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, steps
2928618ce8baSLisandro Dalcin 
2929618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
2930618ce8baSLisandro Dalcin @*/
2931618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
2932618ce8baSLisandro Dalcin {
2933618ce8baSLisandro Dalcin   PetscFunctionBegin;
2934618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2935618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
2936618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
2937618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2938618ce8baSLisandro Dalcin }
2939618ce8baSLisandro Dalcin 
2940618ce8baSLisandro Dalcin /*@
2941618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
2942618ce8baSLisandro Dalcin 
2943618ce8baSLisandro Dalcin    Logically Collective on TS
2944618ce8baSLisandro Dalcin 
2945618ce8baSLisandro Dalcin    Input Parameters:
2946618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2947618ce8baSLisandro Dalcin -  maxtime - final time to step to
2948618ce8baSLisandro Dalcin 
2949618ce8baSLisandro Dalcin    Options Database Keys:
2950ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
2951618ce8baSLisandro Dalcin 
2952618ce8baSLisandro Dalcin    Notes:
2953618ce8baSLisandro Dalcin    The default maximum time is 5.0
2954618ce8baSLisandro Dalcin 
2955618ce8baSLisandro Dalcin    Level: intermediate
2956618ce8baSLisandro Dalcin 
2957618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, time
2958618ce8baSLisandro Dalcin 
2959618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
2960618ce8baSLisandro Dalcin @*/
2961618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
2962618ce8baSLisandro Dalcin {
2963618ce8baSLisandro Dalcin   PetscFunctionBegin;
2964618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2965618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
2966618ce8baSLisandro Dalcin   ts->max_time = maxtime;
2967618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2968618ce8baSLisandro Dalcin }
2969618ce8baSLisandro Dalcin 
2970618ce8baSLisandro Dalcin /*@
2971618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
2972618ce8baSLisandro Dalcin 
2973618ce8baSLisandro Dalcin    Not Collective
2974618ce8baSLisandro Dalcin 
2975618ce8baSLisandro Dalcin    Input Parameters:
2976618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2977618ce8baSLisandro Dalcin 
2978618ce8baSLisandro Dalcin    Output Parameter:
2979618ce8baSLisandro Dalcin .  maxtime - final time to step to
2980618ce8baSLisandro Dalcin 
2981618ce8baSLisandro Dalcin    Level: advanced
2982618ce8baSLisandro Dalcin 
2983618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, time
2984618ce8baSLisandro Dalcin 
2985618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
2986618ce8baSLisandro Dalcin @*/
2987618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
2988618ce8baSLisandro Dalcin {
2989618ce8baSLisandro Dalcin   PetscFunctionBegin;
2990618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2991618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
2992618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
2993618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2994618ce8baSLisandro Dalcin }
2995618ce8baSLisandro Dalcin 
2996618ce8baSLisandro Dalcin /*@
2997aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
2998edc382c3SSatish Balay 
2999edc382c3SSatish Balay    Level: deprecated
3000edc382c3SSatish Balay 
3001aaa6c58dSLisandro Dalcin @*/
3002aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
3003aaa6c58dSLisandro Dalcin {
3004aaa6c58dSLisandro Dalcin   PetscErrorCode ierr;
3005aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
3006aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3007aaa6c58dSLisandro Dalcin   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
3008aaa6c58dSLisandro Dalcin   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
3009aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
3010aaa6c58dSLisandro Dalcin }
3011aaa6c58dSLisandro Dalcin 
3012aaa6c58dSLisandro Dalcin /*@
301319eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
3014edc382c3SSatish Balay 
3015edc382c3SSatish Balay    Level: deprecated
3016edc382c3SSatish Balay 
3017adb62b0dSMatthew Knepley @*/
30187087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
3019adb62b0dSMatthew Knepley {
3020adb62b0dSMatthew Knepley   PetscFunctionBegin;
30210700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3022abc0a331SBarry Smith   if (maxsteps) {
30234482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
3024adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
3025adb62b0dSMatthew Knepley   }
3026abc0a331SBarry Smith   if (maxtime) {
30274482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
3028adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
3029adb62b0dSMatthew Knepley   }
3030adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
3031adb62b0dSMatthew Knepley }
3032adb62b0dSMatthew Knepley 
3033d763cef2SBarry Smith /*@
303419eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
3035edc382c3SSatish Balay 
3036edc382c3SSatish Balay    Level: deprecated
3037edc382c3SSatish Balay 
3038d763cef2SBarry Smith @*/
30397087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
3040d763cef2SBarry Smith {
3041d763cef2SBarry Smith   PetscFunctionBegin;
30420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3043c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3044c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
304539b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
304639b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
3047d763cef2SBarry Smith   PetscFunctionReturn(0);
3048d763cef2SBarry Smith }
3049d763cef2SBarry Smith 
3050d763cef2SBarry Smith /*@
30515c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
3052edc382c3SSatish Balay 
3053edc382c3SSatish Balay    Level: deprecated
3054edc382c3SSatish Balay 
30555c5f5948SLisandro Dalcin @*/
3056e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
30575c5f5948SLisandro Dalcin 
305819eac22cSLisandro Dalcin /*@
30594f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
3060edc382c3SSatish Balay 
3061edc382c3SSatish Balay    Level: deprecated
3062edc382c3SSatish Balay 
30634f4e0956SLisandro Dalcin @*/
30644f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
30654f4e0956SLisandro Dalcin 
30664f4e0956SLisandro Dalcin /*@
3067d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
3068d763cef2SBarry Smith    for use by the TS routines.
3069d763cef2SBarry Smith 
30703f9fe445SBarry Smith    Logically Collective on TS and Vec
3071d763cef2SBarry Smith 
3072d763cef2SBarry Smith    Input Parameters:
3073d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
30740910c330SBarry Smith -  u - the solution vector
3075d763cef2SBarry Smith 
3076d763cef2SBarry Smith    Level: beginner
3077d763cef2SBarry Smith 
3078715f1b00SHong Zhang .keywords: TS, timestep, set, solution, initial values
30790ed3bfb6SBarry Smith 
30800ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3081d763cef2SBarry Smith @*/
30820910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3083d763cef2SBarry Smith {
30848737fe31SLisandro Dalcin   PetscErrorCode ierr;
3085496e6a7aSJed Brown   DM             dm;
30868737fe31SLisandro Dalcin 
3087d763cef2SBarry Smith   PetscFunctionBegin;
30880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
30890910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
30900910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
30916bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
30920910c330SBarry Smith   ts->vec_sol = u;
3093bbd56ea5SKarl Rupp 
3094496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
30950910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
3096d763cef2SBarry Smith   PetscFunctionReturn(0);
3097d763cef2SBarry Smith }
3098d763cef2SBarry Smith 
3099ac226902SBarry Smith /*@C
3100000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
31013f2090d5SJed Brown   called once at the beginning of each time step.
3102000e7ae3SMatthew Knepley 
31033f9fe445SBarry Smith   Logically Collective on TS
3104000e7ae3SMatthew Knepley 
3105000e7ae3SMatthew Knepley   Input Parameters:
3106000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3107000e7ae3SMatthew Knepley - func - The function
3108000e7ae3SMatthew Knepley 
3109000e7ae3SMatthew Knepley   Calling sequence of func:
3110000e7ae3SMatthew Knepley . func (TS ts);
3111000e7ae3SMatthew Knepley 
3112000e7ae3SMatthew Knepley   Level: intermediate
3113000e7ae3SMatthew Knepley 
3114000e7ae3SMatthew Knepley .keywords: TS, timestep
3115dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3116000e7ae3SMatthew Knepley @*/
31177087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3118000e7ae3SMatthew Knepley {
3119000e7ae3SMatthew Knepley   PetscFunctionBegin;
31200700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3121ae60f76fSBarry Smith   ts->prestep = func;
3122000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3123000e7ae3SMatthew Knepley }
3124000e7ae3SMatthew Knepley 
312509ee8438SJed Brown /*@
31263f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
31273f2090d5SJed Brown 
31283f2090d5SJed Brown   Collective on TS
31293f2090d5SJed Brown 
31303f2090d5SJed Brown   Input Parameters:
31313f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
31323f2090d5SJed Brown 
31333f2090d5SJed Brown   Notes:
31343f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
31353f2090d5SJed Brown   so most users would not generally call this routine themselves.
31363f2090d5SJed Brown 
31373f2090d5SJed Brown   Level: developer
31383f2090d5SJed Brown 
31393f2090d5SJed Brown .keywords: TS, timestep
31409be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
31413f2090d5SJed Brown @*/
31427087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
31433f2090d5SJed Brown {
31443f2090d5SJed Brown   PetscErrorCode ierr;
31453f2090d5SJed Brown 
31463f2090d5SJed Brown   PetscFunctionBegin;
31470700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3148ae60f76fSBarry Smith   if (ts->prestep) {
31495efd42a4SStefano Zampini     Vec              U;
31505efd42a4SStefano Zampini     PetscObjectState sprev,spost;
31515efd42a4SStefano Zampini 
31525efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
31535efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3154ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
31555efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3156dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3157312ce896SJed Brown   }
31583f2090d5SJed Brown   PetscFunctionReturn(0);
31593f2090d5SJed Brown }
31603f2090d5SJed Brown 
3161b8123daeSJed Brown /*@C
3162b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3163b8123daeSJed Brown   called once at the beginning of each stage.
3164b8123daeSJed Brown 
3165b8123daeSJed Brown   Logically Collective on TS
3166b8123daeSJed Brown 
3167b8123daeSJed Brown   Input Parameters:
3168b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3169b8123daeSJed Brown - func - The function
3170b8123daeSJed Brown 
3171b8123daeSJed Brown   Calling sequence of func:
3172b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
3173b8123daeSJed Brown 
3174b8123daeSJed Brown   Level: intermediate
3175b8123daeSJed Brown 
3176b8123daeSJed Brown   Note:
3177b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
317880275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3179b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3180b8123daeSJed Brown 
3181b8123daeSJed Brown .keywords: TS, timestep
31829be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3183b8123daeSJed Brown @*/
3184b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3185b8123daeSJed Brown {
3186b8123daeSJed Brown   PetscFunctionBegin;
3187b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3188ae60f76fSBarry Smith   ts->prestage = func;
3189b8123daeSJed Brown   PetscFunctionReturn(0);
3190b8123daeSJed Brown }
3191b8123daeSJed Brown 
31929be3e283SDebojyoti Ghosh /*@C
31939be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
31949be3e283SDebojyoti Ghosh   called once at the end of each stage.
31959be3e283SDebojyoti Ghosh 
31969be3e283SDebojyoti Ghosh   Logically Collective on TS
31979be3e283SDebojyoti Ghosh 
31989be3e283SDebojyoti Ghosh   Input Parameters:
31999be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
32009be3e283SDebojyoti Ghosh - func - The function
32019be3e283SDebojyoti Ghosh 
32029be3e283SDebojyoti Ghosh   Calling sequence of func:
32039be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
32049be3e283SDebojyoti Ghosh 
32059be3e283SDebojyoti Ghosh   Level: intermediate
32069be3e283SDebojyoti Ghosh 
32079be3e283SDebojyoti Ghosh   Note:
32089be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
320980275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
32109be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
32119be3e283SDebojyoti Ghosh 
32129be3e283SDebojyoti Ghosh .keywords: TS, timestep
32139be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
32149be3e283SDebojyoti Ghosh @*/
32159be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
32169be3e283SDebojyoti Ghosh {
32179be3e283SDebojyoti Ghosh   PetscFunctionBegin;
32189be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
32199be3e283SDebojyoti Ghosh   ts->poststage = func;
32209be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
32219be3e283SDebojyoti Ghosh }
32229be3e283SDebojyoti Ghosh 
3223c688d042SShri Abhyankar /*@C
3224c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3225c688d042SShri Abhyankar   called once at the end of each step evaluation.
3226c688d042SShri Abhyankar 
3227c688d042SShri Abhyankar   Logically Collective on TS
3228c688d042SShri Abhyankar 
3229c688d042SShri Abhyankar   Input Parameters:
3230c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3231c688d042SShri Abhyankar - func - The function
3232c688d042SShri Abhyankar 
3233c688d042SShri Abhyankar   Calling sequence of func:
3234c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3235c688d042SShri Abhyankar 
3236c688d042SShri Abhyankar   Level: intermediate
3237c688d042SShri Abhyankar 
3238c688d042SShri Abhyankar   Note:
32391785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
32401785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3241e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3242e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3243e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3244c688d042SShri Abhyankar 
3245c688d042SShri Abhyankar .keywords: TS, timestep
3246c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3247c688d042SShri Abhyankar @*/
3248c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3249c688d042SShri Abhyankar {
3250c688d042SShri Abhyankar   PetscFunctionBegin;
3251c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3252c688d042SShri Abhyankar   ts->postevaluate = func;
3253c688d042SShri Abhyankar   PetscFunctionReturn(0);
3254c688d042SShri Abhyankar }
3255c688d042SShri Abhyankar 
3256b8123daeSJed Brown /*@
3257b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3258b8123daeSJed Brown 
3259b8123daeSJed Brown   Collective on TS
3260b8123daeSJed Brown 
3261b8123daeSJed Brown   Input Parameters:
3262b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
32639be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3264b8123daeSJed Brown 
3265b8123daeSJed Brown   Notes:
3266b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3267b8123daeSJed Brown   most users would not generally call this routine themselves.
3268b8123daeSJed Brown 
3269b8123daeSJed Brown   Level: developer
3270b8123daeSJed Brown 
3271b8123daeSJed Brown .keywords: TS, timestep
32729be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3273b8123daeSJed Brown @*/
3274b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3275b8123daeSJed Brown {
3276b8123daeSJed Brown   PetscFunctionBegin;
3277b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3278ae60f76fSBarry Smith   if (ts->prestage) {
3279ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3280b8123daeSJed Brown   }
3281b8123daeSJed Brown   PetscFunctionReturn(0);
3282b8123daeSJed Brown }
3283b8123daeSJed Brown 
32849be3e283SDebojyoti Ghosh /*@
32859be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
32869be3e283SDebojyoti Ghosh 
32879be3e283SDebojyoti Ghosh   Collective on TS
32889be3e283SDebojyoti Ghosh 
32899be3e283SDebojyoti Ghosh   Input Parameters:
32909be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
32919be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
32929be3e283SDebojyoti Ghosh   stageindex  - Stage number
32939be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
32949be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
32959be3e283SDebojyoti Ghosh 
32969be3e283SDebojyoti Ghosh   Notes:
32979be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
32989be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
32999be3e283SDebojyoti Ghosh 
33009be3e283SDebojyoti Ghosh   Level: developer
33019be3e283SDebojyoti Ghosh 
33029be3e283SDebojyoti Ghosh .keywords: TS, timestep
33039be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
33049be3e283SDebojyoti Ghosh @*/
33059be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
33069be3e283SDebojyoti Ghosh {
33079be3e283SDebojyoti Ghosh   PetscFunctionBegin;
33089be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
33094beae5d8SLisandro Dalcin   if (ts->poststage) {
33109be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
33119be3e283SDebojyoti Ghosh   }
33129be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
33139be3e283SDebojyoti Ghosh }
33149be3e283SDebojyoti Ghosh 
3315c688d042SShri Abhyankar /*@
3316c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3317c688d042SShri Abhyankar 
3318c688d042SShri Abhyankar   Collective on TS
3319c688d042SShri Abhyankar 
3320c688d042SShri Abhyankar   Input Parameters:
3321c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3322c688d042SShri Abhyankar 
3323c688d042SShri Abhyankar   Notes:
3324c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3325c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3326c688d042SShri Abhyankar 
3327c688d042SShri Abhyankar   Level: developer
3328c688d042SShri Abhyankar 
3329c688d042SShri Abhyankar .keywords: TS, timestep
3330c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3331c688d042SShri Abhyankar @*/
3332c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3333c688d042SShri Abhyankar {
3334c688d042SShri Abhyankar   PetscErrorCode ierr;
3335c688d042SShri Abhyankar 
3336c688d042SShri Abhyankar   PetscFunctionBegin;
3337c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3338c688d042SShri Abhyankar   if (ts->postevaluate) {
3339dcb233daSLisandro Dalcin     Vec              U;
3340dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3341dcb233daSLisandro Dalcin 
3342dcb233daSLisandro Dalcin     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3343dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3344c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3345dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3346dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3347c688d042SShri Abhyankar   }
3348c688d042SShri Abhyankar   PetscFunctionReturn(0);
3349c688d042SShri Abhyankar }
3350c688d042SShri Abhyankar 
3351ac226902SBarry Smith /*@C
3352000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
33533f2090d5SJed Brown   called once at the end of each time step.
3354000e7ae3SMatthew Knepley 
33553f9fe445SBarry Smith   Logically Collective on TS
3356000e7ae3SMatthew Knepley 
3357000e7ae3SMatthew Knepley   Input Parameters:
3358000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3359000e7ae3SMatthew Knepley - func - The function
3360000e7ae3SMatthew Knepley 
3361000e7ae3SMatthew Knepley   Calling sequence of func:
3362b8123daeSJed Brown $ func (TS ts);
3363000e7ae3SMatthew Knepley 
33641785ff2aSShri Abhyankar   Notes:
33651785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
33661785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
33671785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
33681785ff2aSShri Abhyankar 
3369000e7ae3SMatthew Knepley   Level: intermediate
3370000e7ae3SMatthew Knepley 
3371000e7ae3SMatthew Knepley .keywords: TS, timestep
3372dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3373000e7ae3SMatthew Knepley @*/
33747087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3375000e7ae3SMatthew Knepley {
3376000e7ae3SMatthew Knepley   PetscFunctionBegin;
33770700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3378ae60f76fSBarry Smith   ts->poststep = func;
3379000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3380000e7ae3SMatthew Knepley }
3381000e7ae3SMatthew Knepley 
338209ee8438SJed Brown /*@
33833f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
33843f2090d5SJed Brown 
33853f2090d5SJed Brown   Collective on TS
33863f2090d5SJed Brown 
33873f2090d5SJed Brown   Input Parameters:
33883f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
33893f2090d5SJed Brown 
33903f2090d5SJed Brown   Notes:
33913f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
33923f2090d5SJed Brown   so most users would not generally call this routine themselves.
33933f2090d5SJed Brown 
33943f2090d5SJed Brown   Level: developer
33953f2090d5SJed Brown 
33963f2090d5SJed Brown .keywords: TS, timestep
33973f2090d5SJed Brown @*/
33987087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
33993f2090d5SJed Brown {
34003f2090d5SJed Brown   PetscErrorCode ierr;
34013f2090d5SJed Brown 
34023f2090d5SJed Brown   PetscFunctionBegin;
34030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3404ae60f76fSBarry Smith   if (ts->poststep) {
34055efd42a4SStefano Zampini     Vec              U;
34065efd42a4SStefano Zampini     PetscObjectState sprev,spost;
34075efd42a4SStefano Zampini 
34085efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
34095efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3410ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
34115efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3412dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
341372ac3e02SJed Brown   }
34143f2090d5SJed Brown   PetscFunctionReturn(0);
34153f2090d5SJed Brown }
34163f2090d5SJed Brown 
3417d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3418d763cef2SBarry Smith 
3419d763cef2SBarry Smith /*@C
3420a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3421d763cef2SBarry Smith    timestep to display the iteration's  progress.
3422d763cef2SBarry Smith 
34233f9fe445SBarry Smith    Logically Collective on TS
3424d763cef2SBarry Smith 
3425d763cef2SBarry Smith    Input Parameters:
3426d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3427e213d8f1SJed Brown .  monitor - monitoring routine
3428329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
34290298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3430b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
34310298fd71SBarry Smith           (may be NULL)
3432d763cef2SBarry Smith 
3433e213d8f1SJed Brown    Calling sequence of monitor:
3434dcb233daSLisandro Dalcin $    PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3435d763cef2SBarry Smith 
3436d763cef2SBarry Smith +    ts - the TS context
343763e21af5SBarry 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)
34381f06c33eSBarry Smith .    time - current time
34390910c330SBarry Smith .    u - current iterate
3440d763cef2SBarry Smith -    mctx - [optional] monitoring context
3441d763cef2SBarry Smith 
3442d763cef2SBarry Smith    Notes:
3443d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3444d763cef2SBarry Smith    already has been loaded.
3445d763cef2SBarry Smith 
344695452b02SPatrick Sanan    Fortran Notes:
344795452b02SPatrick Sanan     Only a single monitor function can be set for each TS object
3448025f1a04SBarry Smith 
3449d763cef2SBarry Smith    Level: intermediate
3450d763cef2SBarry Smith 
3451d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3452d763cef2SBarry Smith 
3453a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3454d763cef2SBarry Smith @*/
3455c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3456d763cef2SBarry Smith {
345778064530SBarry Smith   PetscErrorCode ierr;
345878064530SBarry Smith   PetscInt       i;
345978064530SBarry Smith   PetscBool      identical;
346078064530SBarry Smith 
3461d763cef2SBarry Smith   PetscFunctionBegin;
34620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
346378064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
346478064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
346578064530SBarry Smith     if (identical) PetscFunctionReturn(0);
346678064530SBarry Smith   }
346717186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3468d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
34698704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3470d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3471d763cef2SBarry Smith   PetscFunctionReturn(0);
3472d763cef2SBarry Smith }
3473d763cef2SBarry Smith 
3474d763cef2SBarry Smith /*@C
3475a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3476d763cef2SBarry Smith 
34773f9fe445SBarry Smith    Logically Collective on TS
3478d763cef2SBarry Smith 
3479d763cef2SBarry Smith    Input Parameters:
3480d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3481d763cef2SBarry Smith 
3482d763cef2SBarry Smith    Notes:
3483d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3484d763cef2SBarry Smith 
3485d763cef2SBarry Smith    Level: intermediate
3486d763cef2SBarry Smith 
3487d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3488d763cef2SBarry Smith 
3489a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3490d763cef2SBarry Smith @*/
34917087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3492d763cef2SBarry Smith {
3493d952e501SBarry Smith   PetscErrorCode ierr;
3494d952e501SBarry Smith   PetscInt       i;
3495d952e501SBarry Smith 
3496d763cef2SBarry Smith   PetscFunctionBegin;
34970700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3498d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
34998704b422SBarry Smith     if (ts->monitordestroy[i]) {
35008704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3501d952e501SBarry Smith     }
3502d952e501SBarry Smith   }
3503d763cef2SBarry Smith   ts->numbermonitors = 0;
3504d763cef2SBarry Smith   PetscFunctionReturn(0);
3505d763cef2SBarry Smith }
3506d763cef2SBarry Smith 
3507721cd6eeSBarry Smith /*@C
3508721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
35095516499fSSatish Balay 
35105516499fSSatish Balay    Level: intermediate
351141251cbbSSatish Balay 
35125516499fSSatish Balay .keywords: TS, set, monitor
35135516499fSSatish Balay 
351463e21af5SBarry Smith .seealso:  TSMonitorSet()
351541251cbbSSatish Balay @*/
3516721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3517d763cef2SBarry Smith {
3518dfbe8321SBarry Smith   PetscErrorCode ierr;
3519721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
352041aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3521d132466eSBarry Smith 
3522d763cef2SBarry Smith   PetscFunctionBegin;
35234d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
352441aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
352541aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3526721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
352741aca3d6SBarry Smith   if (iascii) {
3528649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
352963e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
353063e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
353163e21af5SBarry Smith     } else {
35328392e04aSShri 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);
353363e21af5SBarry Smith     }
3534649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
353541aca3d6SBarry Smith   } else if (ibinary) {
353641aca3d6SBarry Smith     PetscMPIInt rank;
353741aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
353841aca3d6SBarry Smith     if (!rank) {
3539450a797fSBarry Smith       PetscBool skipHeader;
3540450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3541450a797fSBarry Smith 
3542450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3543450a797fSBarry Smith       if (!skipHeader) {
3544450a797fSBarry Smith          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
3545450a797fSBarry Smith        }
354641aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
354741aca3d6SBarry Smith     } else {
354841aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
354941aca3d6SBarry Smith     }
355041aca3d6SBarry Smith   }
3551721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3552d763cef2SBarry Smith   PetscFunctionReturn(0);
3553d763cef2SBarry Smith }
3554d763cef2SBarry Smith 
3555cc9c3a59SBarry Smith /*@C
3556cc9c3a59SBarry Smith    TSMonitorExtreme - Prints the extreme values of the solution at each timestep
3557cc9c3a59SBarry Smith 
3558cc9c3a59SBarry Smith    Level: intermediate
3559cc9c3a59SBarry Smith 
3560cc9c3a59SBarry Smith .keywords: TS, set, monitor
3561cc9c3a59SBarry Smith 
3562cc9c3a59SBarry Smith .seealso:  TSMonitorSet()
3563cc9c3a59SBarry Smith @*/
3564cc9c3a59SBarry Smith PetscErrorCode TSMonitorExtreme(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3565cc9c3a59SBarry Smith {
3566cc9c3a59SBarry Smith   PetscErrorCode ierr;
3567cc9c3a59SBarry Smith   PetscViewer    viewer =  vf->viewer;
3568cc9c3a59SBarry Smith   PetscBool      iascii;
3569cc9c3a59SBarry Smith   PetscReal      max,min;
3570cc9c3a59SBarry Smith 
3571cc9c3a59SBarry Smith 
3572cc9c3a59SBarry Smith   PetscFunctionBegin;
3573cc9c3a59SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3574cc9c3a59SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
3575cc9c3a59SBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3576cc9c3a59SBarry Smith   if (iascii) {
3577cc9c3a59SBarry Smith     ierr = VecMax(v,NULL,&max);CHKERRQ(ierr);
3578cc9c3a59SBarry Smith     ierr = VecMin(v,NULL,&min);CHKERRQ(ierr);
3579cc9c3a59SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
35805132926bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s max %g min %g\n",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)" : "",(double)max,(double)min);CHKERRQ(ierr);
3581cc9c3a59SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3582cc9c3a59SBarry Smith   }
3583cc9c3a59SBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3584cc9c3a59SBarry Smith   PetscFunctionReturn(0);
3585cc9c3a59SBarry Smith }
3586cc9c3a59SBarry Smith 
3587cd652676SJed Brown /*@
3588cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3589cd652676SJed Brown 
3590cd652676SJed Brown    Collective on TS
3591cd652676SJed Brown 
3592cd652676SJed Brown    Input Argument:
3593cd652676SJed Brown +  ts - time stepping context
3594cd652676SJed Brown -  t - time to interpolate to
3595cd652676SJed Brown 
3596cd652676SJed Brown    Output Argument:
35970910c330SBarry Smith .  U - state at given time
3598cd652676SJed Brown 
3599cd652676SJed Brown    Level: intermediate
3600cd652676SJed Brown 
3601cd652676SJed Brown    Developer Notes:
3602cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3603cd652676SJed Brown 
3604cd652676SJed Brown .keywords: TS, set
3605cd652676SJed Brown 
3606874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3607cd652676SJed Brown @*/
36080910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3609cd652676SJed Brown {
3610cd652676SJed Brown   PetscErrorCode ierr;
3611cd652676SJed Brown 
3612cd652676SJed Brown   PetscFunctionBegin;
3613cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3614b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3615be5899b3SLisandro 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);
3616ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
36170910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3618cd652676SJed Brown   PetscFunctionReturn(0);
3619cd652676SJed Brown }
3620cd652676SJed Brown 
3621d763cef2SBarry Smith /*@
36226d9e5789SSean Farley    TSStep - Steps one time step
3623d763cef2SBarry Smith 
3624d763cef2SBarry Smith    Collective on TS
3625d763cef2SBarry Smith 
3626d763cef2SBarry Smith    Input Parameter:
3627d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3628d763cef2SBarry Smith 
362927829d71SBarry Smith    Level: developer
3630d763cef2SBarry Smith 
3631b8123daeSJed Brown    Notes:
363227829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
363327829d71SBarry Smith 
3634b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3635b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3636b8123daeSJed Brown 
363719eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
363819eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
363925cb2221SBarry Smith 
3640d763cef2SBarry Smith .keywords: TS, timestep, solve
3641d763cef2SBarry Smith 
36429be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3643d763cef2SBarry Smith @*/
3644193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3645d763cef2SBarry Smith {
3646dfbe8321SBarry Smith   PetscErrorCode   ierr;
3647fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3648be5899b3SLisandro Dalcin   PetscReal        ptime;
3649d763cef2SBarry Smith 
3650d763cef2SBarry Smith   PetscFunctionBegin;
36510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3652fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3653fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3654fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3655fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3656fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3657fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3658302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3659fffbeea8SBarry Smith 
3660d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
366168bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3662d405a339SMatthew Knepley 
3663ef85077eSLisandro 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>");
3664a6772fa2SLisandro 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()");
3665be5899b3SLisandro 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");
3666a6772fa2SLisandro Dalcin 
3667be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3668be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
36692808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3670e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3671d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3672193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3673d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3674be5899b3SLisandro Dalcin   ts->ptime_prev = ptime;
36752808aa04SLisandro Dalcin   ts->steps++;
3676be5899b3SLisandro Dalcin   ts->steprollback = PETSC_FALSE;
367728d5b5d6SLisandro Dalcin   ts->steprestart  = PETSC_FALSE;
3678362cd11cSLisandro Dalcin 
3679362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3680cef5090cSJed Brown     if (ts->errorifstepfailed) {
368108c7845fSBarry 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]);
368208c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3683d2daff3dSHong Zhang     }
368408c7845fSBarry Smith   } else if (!ts->reason) {
368508c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
368608c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
368708c7845fSBarry Smith   }
368808c7845fSBarry Smith   PetscFunctionReturn(0);
368908c7845fSBarry Smith }
369008c7845fSBarry Smith 
369108c7845fSBarry Smith /*@
36927cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
36937cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
36947cbde773SLisandro Dalcin 
36957cbde773SLisandro Dalcin    Collective on TS
36967cbde773SLisandro Dalcin 
36977cbde773SLisandro Dalcin    Input Arguments:
36987cbde773SLisandro Dalcin +  ts - time stepping context
36997cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
37007cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
37017cbde773SLisandro Dalcin 
37027cbde773SLisandro Dalcin    Output Arguments:
37037cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
37047cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
37057cbde773SLisandro Dalcin 
37067cbde773SLisandro Dalcin    Level: advanced
37077cbde773SLisandro Dalcin 
37087cbde773SLisandro Dalcin    Notes:
37097cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
37107cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
37117cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
37127cbde773SLisandro Dalcin 
37137cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
37147cbde773SLisandro Dalcin @*/
37157cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
37167cbde773SLisandro Dalcin {
37177cbde773SLisandro Dalcin   PetscErrorCode ierr;
37187cbde773SLisandro Dalcin 
37197cbde773SLisandro Dalcin   PetscFunctionBegin;
37207cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37217cbde773SLisandro Dalcin   PetscValidType(ts,1);
37227cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
37237cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
37247cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
37257cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
37267cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
37277cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
37287cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
37297cbde773SLisandro Dalcin   PetscFunctionReturn(0);
37307cbde773SLisandro Dalcin }
37317cbde773SLisandro Dalcin 
373205175c85SJed Brown /*@
373305175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
373405175c85SJed Brown 
37351c3436cfSJed Brown    Collective on TS
373605175c85SJed Brown 
373705175c85SJed Brown    Input Arguments:
37381c3436cfSJed Brown +  ts - time stepping context
37391c3436cfSJed Brown .  order - desired order of accuracy
37400298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
374105175c85SJed Brown 
374205175c85SJed Brown    Output Arguments:
37430910c330SBarry Smith .  U - state at the end of the current step
374405175c85SJed Brown 
374505175c85SJed Brown    Level: advanced
374605175c85SJed Brown 
3747108c343cSJed Brown    Notes:
3748108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3749108c343cSJed 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.
3750108c343cSJed Brown 
37511c3436cfSJed Brown .seealso: TSStep(), TSAdapt
375205175c85SJed Brown @*/
37530910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
375405175c85SJed Brown {
375505175c85SJed Brown   PetscErrorCode ierr;
375605175c85SJed Brown 
375705175c85SJed Brown   PetscFunctionBegin;
375805175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
375905175c85SJed Brown   PetscValidType(ts,1);
37600910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3761ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
37620910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
376305175c85SJed Brown   PetscFunctionReturn(0);
376405175c85SJed Brown }
376505175c85SJed Brown 
3766b1cb36f3SHong Zhang /*@
37676a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
37686a4d4014SLisandro Dalcin 
37696a4d4014SLisandro Dalcin    Collective on TS
37706a4d4014SLisandro Dalcin 
37716a4d4014SLisandro Dalcin    Input Parameter:
37726a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
377363e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
377463e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
37755a3a76d0SJed Brown 
37766a4d4014SLisandro Dalcin    Level: beginner
37776a4d4014SLisandro Dalcin 
37785a3a76d0SJed Brown    Notes:
37795a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
37805a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
37815a3a76d0SJed Brown    stepped over the final time.
37825a3a76d0SJed Brown 
37836a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
37846a4d4014SLisandro Dalcin 
378563e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
37866a4d4014SLisandro Dalcin @*/
3787cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
37886a4d4014SLisandro Dalcin {
3789b06615a5SLisandro Dalcin   Vec               solution;
37906a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
3791f22f69f0SBarry Smith 
37926a4d4014SLisandro Dalcin   PetscFunctionBegin;
37930700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3794f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
3795feed9e9dSBarry Smith 
3796ee41a567SStefano 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 */
37970910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3798b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3799b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3800b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
38015a3a76d0SJed Brown     }
38020910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3803715f1b00SHong Zhang     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
3804bbd56ea5SKarl Rupp   } else if (u) {
38050910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
38065a3a76d0SJed Brown   }
3807b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
380868bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3809a6772fa2SLisandro Dalcin 
3810ef85077eSLisandro 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>");
3811a6772fa2SLisandro 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()");
3812a6772fa2SLisandro 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");
3813a6772fa2SLisandro Dalcin 
3814715f1b00SHong Zhang   if (ts->forward_solve) {
3815715f1b00SHong Zhang     ierr = TSForwardSetUp(ts);CHKERRQ(ierr);
3816715f1b00SHong Zhang   }
3817715f1b00SHong Zhang 
3818e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
3819715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
3820e7069c78SShri      TSTrajectory to incorrectly save the output files
3821e7069c78SShri   */
3822715f1b00SHong Zhang   /* reset time step and iteration counters */
38232808aa04SLisandro Dalcin   if (!ts->steps) {
38245ef26d82SJed Brown     ts->ksp_its           = 0;
38255ef26d82SJed Brown     ts->snes_its          = 0;
3826c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
3827c610991cSLisandro Dalcin     ts->reject            = 0;
38282808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
38292808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
38302808aa04SLisandro Dalcin   }
383110bc0e4dSStefano 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;
3832193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
3833193ac0bcSJed Brown 
3834ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3835f05ece33SBarry Smith 
3836193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
3837193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
3838a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3839cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3840a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
3841be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
3842db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
3843db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3844e7069c78SShri 
38452808aa04SLisandro Dalcin     if (!ts->steps) {
38462808aa04SLisandro Dalcin       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
38476427ac75SLisandro Dalcin       ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
38482808aa04SLisandro Dalcin     }
38496427ac75SLisandro Dalcin 
3850e1a7a14fSJed Brown     while (!ts->reason) {
38512808aa04SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
38529687d888SLisandro Dalcin       if (!ts->steprollback) {
38539687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
38549687d888SLisandro Dalcin       }
3855193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
3856f3b1f45cSBarry Smith       if (ts->testjacobian) {
3857f3b1f45cSBarry Smith         ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr);
3858f3b1f45cSBarry Smith       }
3859f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
3860f3b1f45cSBarry Smith         ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr);
3861f3b1f45cSBarry Smith       }
3862e783b05fSHong 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. */
3863b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
3864b1cb36f3SHong Zhang       }
386558818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
3866715f1b00SHong Zhang         ierr = TSForwardStep(ts);CHKERRQ(ierr);
3867715f1b00SHong Zhang       }
386810b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
3869e783b05fSHong 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. */
387058818c2dSLisandro Dalcin       if (ts->steprollback) {
387158818c2dSLisandro Dalcin         ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
387258818c2dSLisandro Dalcin       }
3873e783b05fSHong Zhang       if (!ts->steprollback) {
38742808aa04SLisandro Dalcin         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
38751eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
3876aeb4809dSShri Abhyankar       }
3877193ac0bcSJed Brown     }
38782808aa04SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
38796427ac75SLisandro Dalcin 
388049354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
38810910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
3882cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3883b06615a5SLisandro Dalcin       solution = u;
388463e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
38850574a7fbSJed Brown     } else {
3886ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3887cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3888b06615a5SLisandro Dalcin       solution = ts->vec_sol;
38890574a7fbSJed Brown     }
3890193ac0bcSJed Brown   }
3891d2daff3dSHong Zhang 
3892ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
389363e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
389456f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
3895ef222394SBarry Smith   if (ts->adjoint_solve) {
3896ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
38972b0a91c0SBarry Smith   }
38986a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
38996a4d4014SLisandro Dalcin }
39006a4d4014SLisandro Dalcin 
39017db568b7SBarry Smith /*@C
3902228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
3903228d79bcSJed Brown 
3904228d79bcSJed Brown    Collective on TS
3905228d79bcSJed Brown 
3906228d79bcSJed Brown    Input Parameters:
3907228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
3908228d79bcSJed Brown .  step - step number that has just completed
3909228d79bcSJed Brown .  ptime - model time of the state
39100910c330SBarry Smith -  u - state at the current model time
3911228d79bcSJed Brown 
3912228d79bcSJed Brown    Notes:
39137db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
39147db568b7SBarry Smith    Users would almost never call this routine directly.
3915228d79bcSJed Brown 
391663e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
391763e21af5SBarry Smith 
39187db568b7SBarry Smith    Level: developer
3919228d79bcSJed Brown 
3920228d79bcSJed Brown .keywords: TS, timestep
3921228d79bcSJed Brown @*/
39220910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
3923d763cef2SBarry Smith {
3924d6152f81SLisandro Dalcin   DM             dm;
3925a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
3926d6152f81SLisandro Dalcin   PetscErrorCode ierr;
3927d763cef2SBarry Smith 
3928d763cef2SBarry Smith   PetscFunctionBegin;
3929b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3930b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
3931d6152f81SLisandro Dalcin 
3932d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
3933d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
3934d6152f81SLisandro Dalcin 
39358860a134SJunchao Zhang   ierr = VecLockReadPush(u);CHKERRQ(ierr);
3936d763cef2SBarry Smith   for (i=0; i<n; i++) {
39370910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
3938d763cef2SBarry Smith   }
39398860a134SJunchao Zhang   ierr = VecLockReadPop(u);CHKERRQ(ierr);
3940d763cef2SBarry Smith   PetscFunctionReturn(0);
3941d763cef2SBarry Smith }
3942d763cef2SBarry Smith 
3943d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
3944d763cef2SBarry Smith /*@C
39457db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
3946a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
3947d763cef2SBarry Smith 
3948d763cef2SBarry Smith    Collective on TS
3949d763cef2SBarry Smith 
3950d763cef2SBarry Smith    Input Parameters:
3951d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
3952d763cef2SBarry Smith .  label - the title to put in the title bar
39537c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
3954a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
3955a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
3956d763cef2SBarry Smith 
3957d763cef2SBarry Smith    Output Parameter:
39580b039ecaSBarry Smith .  ctx - the context
3959d763cef2SBarry Smith 
3960d763cef2SBarry Smith    Options Database Key:
39614f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
39628b668821SLisandro Dalcin +  -ts_monitor_lg_timestep_log - automatically sets line graph monitor
39637db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
39647db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
39657db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
39667db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
3967b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
3968d763cef2SBarry Smith 
3969d763cef2SBarry Smith    Notes:
3970a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
3971d763cef2SBarry Smith 
39727db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
39737db568b7SBarry Smith 
39747db568b7SBarry 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
39757db568b7SBarry 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
39767db568b7SBarry Smith    as the first argument.
39777db568b7SBarry Smith 
39787db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
39797db568b7SBarry Smith 
3980d763cef2SBarry Smith    Level: intermediate
3981d763cef2SBarry Smith 
39827db568b7SBarry Smith .keywords: TS, monitor, line graph, residual
3983d763cef2SBarry Smith 
39847db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
39857db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
39867db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
39877db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
39887db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
39897c922b88SBarry Smith 
3990d763cef2SBarry Smith @*/
3991a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
3992d763cef2SBarry Smith {
39937f52daa2SLisandro Dalcin   PetscDraw      draw;
3994dfbe8321SBarry Smith   PetscErrorCode ierr;
3995d763cef2SBarry Smith 
3996d763cef2SBarry Smith   PetscFunctionBegin;
3997b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
39987f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
39997f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
40007f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
4001287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
40027f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
4003a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
4004d763cef2SBarry Smith   PetscFunctionReturn(0);
4005d763cef2SBarry Smith }
4006d763cef2SBarry Smith 
4007b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4008d763cef2SBarry Smith {
40090b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4010c32365f1SBarry Smith   PetscReal      x   = ptime,y;
4011dfbe8321SBarry Smith   PetscErrorCode ierr;
4012d763cef2SBarry Smith 
4013d763cef2SBarry Smith   PetscFunctionBegin;
401463e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
4015b06615a5SLisandro Dalcin   if (!step) {
4016a9f9c1f6SBarry Smith     PetscDrawAxis axis;
40178b668821SLisandro Dalcin     const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step";
4018a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
40198b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr);
4020a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4021a9f9c1f6SBarry Smith   }
4022c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
40238b668821SLisandro Dalcin   if (ctx->semilogy) y = PetscLog10Real(y);
40240b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4025b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
40260b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
40276934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
40283923b477SBarry Smith   }
4029d763cef2SBarry Smith   PetscFunctionReturn(0);
4030d763cef2SBarry Smith }
4031d763cef2SBarry Smith 
4032d763cef2SBarry Smith /*@C
4033a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4034a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4035d763cef2SBarry Smith 
40360b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4037d763cef2SBarry Smith 
4038d763cef2SBarry Smith    Input Parameter:
40390b039ecaSBarry Smith .  ctx - the monitor context
4040d763cef2SBarry Smith 
4041d763cef2SBarry Smith    Level: intermediate
4042d763cef2SBarry Smith 
4043d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
4044d763cef2SBarry Smith 
40454f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4046d763cef2SBarry Smith @*/
4047a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4048d763cef2SBarry Smith {
4049dfbe8321SBarry Smith   PetscErrorCode ierr;
4050d763cef2SBarry Smith 
4051d763cef2SBarry Smith   PetscFunctionBegin;
40527684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
40537684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
40547684fa3eSBarry Smith   }
40550b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
405631152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4057387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4058387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4059387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
40600b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4061d763cef2SBarry Smith   PetscFunctionReturn(0);
4062d763cef2SBarry Smith }
4063d763cef2SBarry Smith 
40641b575b74SJoseph Pusztay /*
40651b575b74SJoseph Pusztay 
40661b575b74SJoseph Pusztay   Creates a TS Monitor SPCtx for use with DM Swarm particle visualizations
40671b575b74SJoseph Pusztay 
40681b575b74SJoseph Pusztay */
40691b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorSPCtx *ctx)
40701b575b74SJoseph Pusztay {
40711b575b74SJoseph Pusztay   PetscDraw      draw;
40721b575b74SJoseph Pusztay   PetscErrorCode ierr;
40731b575b74SJoseph Pusztay 
40741b575b74SJoseph Pusztay   PetscFunctionBegin;
40751b575b74SJoseph Pusztay   ierr = PetscNew(ctx);CHKERRQ(ierr);
40761b575b74SJoseph Pusztay   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
40771b575b74SJoseph Pusztay   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
40781b575b74SJoseph Pusztay   ierr = PetscDrawSPCreate(draw,1,&(*ctx)->sp);CHKERRQ(ierr);
40791b575b74SJoseph Pusztay   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
40801b575b74SJoseph Pusztay   (*ctx)->howoften = howoften;
40811b575b74SJoseph Pusztay   PetscFunctionReturn(0);
40821b575b74SJoseph Pusztay 
40831b575b74SJoseph Pusztay }
40841b575b74SJoseph Pusztay 
40851b575b74SJoseph Pusztay /*
40861b575b74SJoseph Pusztay   Destroys a TSMonitorSPCtx that was created with TSMonitorSPCtxCreate
40871b575b74SJoseph Pusztay */
40881b575b74SJoseph Pusztay PetscErrorCode TSMonitorSPCtxDestroy(TSMonitorSPCtx *ctx)
40891b575b74SJoseph Pusztay {
40901b575b74SJoseph Pusztay   PetscErrorCode ierr;
40911b575b74SJoseph Pusztay 
40921b575b74SJoseph Pusztay   PetscFunctionBegin;
40931b575b74SJoseph Pusztay 
40941b575b74SJoseph Pusztay   ierr = PetscDrawSPDestroy(&(*ctx)->sp);CHKERRQ(ierr);
40951b575b74SJoseph Pusztay   ierr = PetscFree(*ctx);CHKERRQ(ierr);
40961b575b74SJoseph Pusztay 
40971b575b74SJoseph Pusztay   PetscFunctionReturn(0);
40981b575b74SJoseph Pusztay 
40991b575b74SJoseph Pusztay }
41001b575b74SJoseph Pusztay 
4101d763cef2SBarry Smith /*@
4102b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4103d763cef2SBarry Smith 
4104d763cef2SBarry Smith    Not Collective
4105d763cef2SBarry Smith 
4106d763cef2SBarry Smith    Input Parameter:
4107d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4108d763cef2SBarry Smith 
4109d763cef2SBarry Smith    Output Parameter:
411019eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
4111d763cef2SBarry Smith 
4112d763cef2SBarry Smith    Level: beginner
4113d763cef2SBarry Smith 
4114b8123daeSJed Brown    Note:
4115b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
41169be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4117b8123daeSJed Brown 
4118aaa6c58dSLisandro Dalcin .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep()
4119d763cef2SBarry Smith 
4120d763cef2SBarry Smith .keywords: TS, get, time
4121d763cef2SBarry Smith @*/
41227087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4123d763cef2SBarry Smith {
4124d763cef2SBarry Smith   PetscFunctionBegin;
41250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4126f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4127d763cef2SBarry Smith   *t = ts->ptime;
4128d763cef2SBarry Smith   PetscFunctionReturn(0);
4129d763cef2SBarry Smith }
4130d763cef2SBarry Smith 
4131e5e524a1SHong Zhang /*@
4132e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4133e5e524a1SHong Zhang 
4134e5e524a1SHong Zhang    Not Collective
4135e5e524a1SHong Zhang 
4136e5e524a1SHong Zhang    Input Parameter:
4137e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4138e5e524a1SHong Zhang 
4139e5e524a1SHong Zhang    Output Parameter:
4140e5e524a1SHong Zhang .  t  - the previous time
4141e5e524a1SHong Zhang 
4142e5e524a1SHong Zhang    Level: beginner
4143e5e524a1SHong Zhang 
4144aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4145e5e524a1SHong Zhang 
4146e5e524a1SHong Zhang .keywords: TS, get, time
4147e5e524a1SHong Zhang @*/
4148e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4149e5e524a1SHong Zhang {
4150e5e524a1SHong Zhang   PetscFunctionBegin;
4151e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4152e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4153e5e524a1SHong Zhang   *t = ts->ptime_prev;
4154e5e524a1SHong Zhang   PetscFunctionReturn(0);
4155e5e524a1SHong Zhang }
4156e5e524a1SHong Zhang 
41576a4d4014SLisandro Dalcin /*@
41586a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
41596a4d4014SLisandro Dalcin 
41603f9fe445SBarry Smith    Logically Collective on TS
41616a4d4014SLisandro Dalcin 
41626a4d4014SLisandro Dalcin    Input Parameters:
41636a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
41646a4d4014SLisandro Dalcin -  time - the time
41656a4d4014SLisandro Dalcin 
41666a4d4014SLisandro Dalcin    Level: intermediate
41676a4d4014SLisandro Dalcin 
416819eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
41696a4d4014SLisandro Dalcin 
41706a4d4014SLisandro Dalcin .keywords: TS, set, time
41716a4d4014SLisandro Dalcin @*/
41727087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
41736a4d4014SLisandro Dalcin {
41746a4d4014SLisandro Dalcin   PetscFunctionBegin;
41750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4176c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
41776a4d4014SLisandro Dalcin   ts->ptime = t;
41786a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
41796a4d4014SLisandro Dalcin }
41806a4d4014SLisandro Dalcin 
4181d763cef2SBarry Smith /*@C
4182d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4183d763cef2SBarry Smith    TS options in the database.
4184d763cef2SBarry Smith 
41853f9fe445SBarry Smith    Logically Collective on TS
4186d763cef2SBarry Smith 
4187d763cef2SBarry Smith    Input Parameter:
4188d763cef2SBarry Smith +  ts     - The TS context
4189d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4190d763cef2SBarry Smith 
4191d763cef2SBarry Smith    Notes:
4192d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4193d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4194d763cef2SBarry Smith    hyphen.
4195d763cef2SBarry Smith 
4196d763cef2SBarry Smith    Level: advanced
4197d763cef2SBarry Smith 
4198d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
4199d763cef2SBarry Smith 
4200d763cef2SBarry Smith .seealso: TSSetFromOptions()
4201d763cef2SBarry Smith 
4202d763cef2SBarry Smith @*/
42037087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4204d763cef2SBarry Smith {
4205dfbe8321SBarry Smith   PetscErrorCode ierr;
4206089b2837SJed Brown   SNES           snes;
4207d763cef2SBarry Smith 
4208d763cef2SBarry Smith   PetscFunctionBegin;
42090700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4210d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4211089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4212089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4213d763cef2SBarry Smith   PetscFunctionReturn(0);
4214d763cef2SBarry Smith }
4215d763cef2SBarry Smith 
4216d763cef2SBarry Smith /*@C
4217d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4218d763cef2SBarry Smith    TS options in the database.
4219d763cef2SBarry Smith 
42203f9fe445SBarry Smith    Logically Collective on TS
4221d763cef2SBarry Smith 
4222d763cef2SBarry Smith    Input Parameter:
4223d763cef2SBarry Smith +  ts     - The TS context
4224d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4225d763cef2SBarry Smith 
4226d763cef2SBarry Smith    Notes:
4227d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4228d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4229d763cef2SBarry Smith    hyphen.
4230d763cef2SBarry Smith 
4231d763cef2SBarry Smith    Level: advanced
4232d763cef2SBarry Smith 
4233d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
4234d763cef2SBarry Smith 
4235d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4236d763cef2SBarry Smith 
4237d763cef2SBarry Smith @*/
42387087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4239d763cef2SBarry Smith {
4240dfbe8321SBarry Smith   PetscErrorCode ierr;
4241089b2837SJed Brown   SNES           snes;
4242d763cef2SBarry Smith 
4243d763cef2SBarry Smith   PetscFunctionBegin;
42440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4245d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4246089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4247089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4248d763cef2SBarry Smith   PetscFunctionReturn(0);
4249d763cef2SBarry Smith }
4250d763cef2SBarry Smith 
4251d763cef2SBarry Smith /*@C
4252d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4253d763cef2SBarry Smith    TS options in the database.
4254d763cef2SBarry Smith 
4255d763cef2SBarry Smith    Not Collective
4256d763cef2SBarry Smith 
4257d763cef2SBarry Smith    Input Parameter:
4258d763cef2SBarry Smith .  ts - The TS context
4259d763cef2SBarry Smith 
4260d763cef2SBarry Smith    Output Parameter:
4261d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4262d763cef2SBarry Smith 
426395452b02SPatrick Sanan    Notes:
426495452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prifix' of
4265d763cef2SBarry Smith    sufficient length to hold the prefix.
4266d763cef2SBarry Smith 
4267d763cef2SBarry Smith    Level: intermediate
4268d763cef2SBarry Smith 
4269d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
4270d763cef2SBarry Smith 
4271d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4272d763cef2SBarry Smith @*/
42737087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4274d763cef2SBarry Smith {
4275dfbe8321SBarry Smith   PetscErrorCode ierr;
4276d763cef2SBarry Smith 
4277d763cef2SBarry Smith   PetscFunctionBegin;
42780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42794482741eSBarry Smith   PetscValidPointer(prefix,2);
4280d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4281d763cef2SBarry Smith   PetscFunctionReturn(0);
4282d763cef2SBarry Smith }
4283d763cef2SBarry Smith 
4284d763cef2SBarry Smith /*@C
4285d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4286d763cef2SBarry Smith 
4287d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4288d763cef2SBarry Smith 
4289d763cef2SBarry Smith    Input Parameter:
4290d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4291d763cef2SBarry Smith 
4292d763cef2SBarry Smith    Output Parameters:
4293e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4294e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4295e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4296e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4297d763cef2SBarry Smith 
429895452b02SPatrick Sanan    Notes:
429995452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
4300d763cef2SBarry Smith 
4301d763cef2SBarry Smith    Level: intermediate
4302d763cef2SBarry Smith 
430380275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4304d763cef2SBarry Smith 
4305d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
4306d763cef2SBarry Smith @*/
4307e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4308d763cef2SBarry Smith {
4309089b2837SJed Brown   PetscErrorCode ierr;
431024989b8cSPeter Brune   DM             dm;
4311089b2837SJed Brown 
4312d763cef2SBarry Smith   PetscFunctionBegin;
431323a57915SBarry Smith   if (Amat || Pmat) {
431423a57915SBarry Smith     SNES snes;
4315089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
431623a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4317e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
431823a57915SBarry Smith   }
431924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
432024989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4321d763cef2SBarry Smith   PetscFunctionReturn(0);
4322d763cef2SBarry Smith }
4323d763cef2SBarry Smith 
43242eca1d9cSJed Brown /*@C
43252eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
43262eca1d9cSJed Brown 
43272eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
43282eca1d9cSJed Brown 
43292eca1d9cSJed Brown    Input Parameter:
43302eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
43312eca1d9cSJed Brown 
43322eca1d9cSJed Brown    Output Parameters:
4333e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4334e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
43352eca1d9cSJed Brown .  f   - The function to compute the matrices
43362eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
43372eca1d9cSJed Brown 
433895452b02SPatrick Sanan    Notes:
433995452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
43402eca1d9cSJed Brown 
43412eca1d9cSJed Brown    Level: advanced
43422eca1d9cSJed Brown 
434380275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
43442eca1d9cSJed Brown 
43452eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
43462eca1d9cSJed Brown @*/
4347e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
43482eca1d9cSJed Brown {
4349089b2837SJed Brown   PetscErrorCode ierr;
435024989b8cSPeter Brune   DM             dm;
43510910c330SBarry Smith 
43522eca1d9cSJed Brown   PetscFunctionBegin;
4353c0aab802Sstefano_zampini   if (Amat || Pmat) {
4354c0aab802Sstefano_zampini     SNES snes;
4355089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4356f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4357e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4358c0aab802Sstefano_zampini   }
435924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
436024989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
43612eca1d9cSJed Brown   PetscFunctionReturn(0);
43622eca1d9cSJed Brown }
43632eca1d9cSJed Brown 
43641713a123SBarry Smith /*@C
436583a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
43661713a123SBarry Smith    VecView() for the solution at each timestep
43671713a123SBarry Smith 
43681713a123SBarry Smith    Collective on TS
43691713a123SBarry Smith 
43701713a123SBarry Smith    Input Parameters:
43711713a123SBarry Smith +  ts - the TS context
43721713a123SBarry Smith .  step - current time-step
4373142b95e3SSatish Balay .  ptime - current time
43740298fd71SBarry Smith -  dummy - either a viewer or NULL
43751713a123SBarry Smith 
437699fdda47SBarry Smith    Options Database:
437799fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
437899fdda47SBarry Smith 
437995452b02SPatrick Sanan    Notes:
438095452b02SPatrick Sanan     the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
438199fdda47SBarry Smith        will look bad
438299fdda47SBarry Smith 
43831713a123SBarry Smith    Level: intermediate
43841713a123SBarry Smith 
43851713a123SBarry Smith .keywords: TS,  vector, monitor, view
43861713a123SBarry Smith 
4387a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
43881713a123SBarry Smith @*/
43890910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
43901713a123SBarry Smith {
4391dfbe8321SBarry Smith   PetscErrorCode   ierr;
439283a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4393473a3ab2SBarry Smith   PetscDraw        draw;
43941713a123SBarry Smith 
43951713a123SBarry Smith   PetscFunctionBegin;
43966083293cSBarry Smith   if (!step && ictx->showinitial) {
43976083293cSBarry Smith     if (!ictx->initialsolution) {
43980910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
43991713a123SBarry Smith     }
44000910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
44016083293cSBarry Smith   }
4402b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
44030dcf80beSBarry Smith 
44046083293cSBarry Smith   if (ictx->showinitial) {
44056083293cSBarry Smith     PetscReal pause;
44066083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
44076083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
44086083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
44096083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
44106083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
44116083293cSBarry Smith   }
44120910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4413473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
441451fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4415473a3ab2SBarry Smith     char      time[32];
4416473a3ab2SBarry Smith 
4417473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
441885b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4419473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4420473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
442151fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4422473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4423473a3ab2SBarry Smith   }
4424473a3ab2SBarry Smith 
44256083293cSBarry Smith   if (ictx->showinitial) {
44266083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
44276083293cSBarry Smith   }
44281713a123SBarry Smith   PetscFunctionReturn(0);
44291713a123SBarry Smith }
44301713a123SBarry Smith 
44319110b2e7SHong Zhang /*@C
44322d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
44332d5ee99bSBarry Smith 
44342d5ee99bSBarry Smith    Collective on TS
44352d5ee99bSBarry Smith 
44362d5ee99bSBarry Smith    Input Parameters:
44372d5ee99bSBarry Smith +  ts - the TS context
44382d5ee99bSBarry Smith .  step - current time-step
44392d5ee99bSBarry Smith .  ptime - current time
44402d5ee99bSBarry Smith -  dummy - either a viewer or NULL
44412d5ee99bSBarry Smith 
44422d5ee99bSBarry Smith    Level: intermediate
44432d5ee99bSBarry Smith 
44442d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
44452d5ee99bSBarry Smith 
44462d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
44472d5ee99bSBarry Smith @*/
44482d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
44492d5ee99bSBarry Smith {
44502d5ee99bSBarry Smith   PetscErrorCode    ierr;
44512d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
44522d5ee99bSBarry Smith   PetscDraw         draw;
44536934998bSLisandro Dalcin   PetscDrawAxis     axis;
44542d5ee99bSBarry Smith   PetscInt          n;
44552d5ee99bSBarry Smith   PetscMPIInt       size;
44566934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
44572d5ee99bSBarry Smith   char              time[32];
44582d5ee99bSBarry Smith   const PetscScalar *U;
44592d5ee99bSBarry Smith 
44602d5ee99bSBarry Smith   PetscFunctionBegin;
44616934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
44626934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
44632d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
44646934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
44652d5ee99bSBarry Smith 
44662d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
44676934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
44686934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
44696934998bSLisandro Dalcin   if (!step) {
44706934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
44716934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
44726934998bSLisandro Dalcin   }
44732d5ee99bSBarry Smith 
44742d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
44756934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
44766934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4477a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
44786934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
44792d5ee99bSBarry Smith 
44806934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
44816934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
44822d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
44834b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
448485b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
44852d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
448651fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
44872d5ee99bSBarry Smith   }
44886934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
44892d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
449056d62c8fSLisandro Dalcin   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
44916934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
44922d5ee99bSBarry Smith   PetscFunctionReturn(0);
44932d5ee99bSBarry Smith }
44942d5ee99bSBarry Smith 
44956083293cSBarry Smith /*@C
449683a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
44976083293cSBarry Smith 
44986083293cSBarry Smith    Collective on TS
44996083293cSBarry Smith 
45006083293cSBarry Smith    Input Parameters:
45016083293cSBarry Smith .    ctx - the monitor context
45026083293cSBarry Smith 
45036083293cSBarry Smith    Level: intermediate
45046083293cSBarry Smith 
45056083293cSBarry Smith .keywords: TS,  vector, monitor, view
45066083293cSBarry Smith 
450783a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
45086083293cSBarry Smith @*/
450983a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
45106083293cSBarry Smith {
45116083293cSBarry Smith   PetscErrorCode ierr;
45126083293cSBarry Smith 
45136083293cSBarry Smith   PetscFunctionBegin;
451483a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
451583a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
451683a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
45176083293cSBarry Smith   PetscFunctionReturn(0);
45186083293cSBarry Smith }
45196083293cSBarry Smith 
45206083293cSBarry Smith /*@C
452183a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
45226083293cSBarry Smith 
45236083293cSBarry Smith    Collective on TS
45246083293cSBarry Smith 
45256083293cSBarry Smith    Input Parameter:
45266083293cSBarry Smith .    ts - time-step context
45276083293cSBarry Smith 
45286083293cSBarry Smith    Output Patameter:
45296083293cSBarry Smith .    ctx - the monitor context
45306083293cSBarry Smith 
453199fdda47SBarry Smith    Options Database:
453299fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
453399fdda47SBarry Smith 
45346083293cSBarry Smith    Level: intermediate
45356083293cSBarry Smith 
45366083293cSBarry Smith .keywords: TS,  vector, monitor, view
45376083293cSBarry Smith 
453883a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
45396083293cSBarry Smith @*/
454083a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
45416083293cSBarry Smith {
45426083293cSBarry Smith   PetscErrorCode   ierr;
45436083293cSBarry Smith 
45446083293cSBarry Smith   PetscFunctionBegin;
4545b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
454683a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4547e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4548bbd56ea5SKarl Rupp 
454983a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4550473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4551c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4552473a3ab2SBarry Smith 
4553473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4554c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
45556083293cSBarry Smith   PetscFunctionReturn(0);
45566083293cSBarry Smith }
45576083293cSBarry Smith 
45583a471f94SBarry Smith /*@C
45590ed3bfb6SBarry Smith    TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling
45600ed3bfb6SBarry Smith    VecView() for the solution provided by TSSetSolutionFunction() at each timestep
45610ed3bfb6SBarry Smith 
45620ed3bfb6SBarry Smith    Collective on TS
45630ed3bfb6SBarry Smith 
45640ed3bfb6SBarry Smith    Input Parameters:
45650ed3bfb6SBarry Smith +  ts - the TS context
45660ed3bfb6SBarry Smith .  step - current time-step
45670ed3bfb6SBarry Smith .  ptime - current time
45680ed3bfb6SBarry Smith -  dummy - either a viewer or NULL
45690ed3bfb6SBarry Smith 
45700ed3bfb6SBarry Smith    Options Database:
45710ed3bfb6SBarry Smith .  -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
45720ed3bfb6SBarry Smith 
45730ed3bfb6SBarry Smith    Level: intermediate
45740ed3bfb6SBarry Smith 
45750ed3bfb6SBarry Smith .keywords: TS,  vector, monitor, view
45760ed3bfb6SBarry Smith 
45770ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
45780ed3bfb6SBarry Smith @*/
45790ed3bfb6SBarry Smith PetscErrorCode  TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
45800ed3bfb6SBarry Smith {
45810ed3bfb6SBarry Smith   PetscErrorCode   ierr;
45820ed3bfb6SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
45830ed3bfb6SBarry Smith   PetscViewer      viewer = ctx->viewer;
45840ed3bfb6SBarry Smith   Vec              work;
45850ed3bfb6SBarry Smith 
45860ed3bfb6SBarry Smith   PetscFunctionBegin;
45870ed3bfb6SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
45880ed3bfb6SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
45890ed3bfb6SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
45900ed3bfb6SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
45910ed3bfb6SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
45920ed3bfb6SBarry Smith   PetscFunctionReturn(0);
45930ed3bfb6SBarry Smith }
45940ed3bfb6SBarry Smith 
45950ed3bfb6SBarry Smith /*@C
459683a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
45973a471f94SBarry Smith    VecView() for the error at each timestep
45983a471f94SBarry Smith 
45993a471f94SBarry Smith    Collective on TS
46003a471f94SBarry Smith 
46013a471f94SBarry Smith    Input Parameters:
46023a471f94SBarry Smith +  ts - the TS context
46033a471f94SBarry Smith .  step - current time-step
46043a471f94SBarry Smith .  ptime - current time
46050298fd71SBarry Smith -  dummy - either a viewer or NULL
46063a471f94SBarry Smith 
46070ed3bfb6SBarry Smith    Options Database:
46080ed3bfb6SBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
46090ed3bfb6SBarry Smith 
46103a471f94SBarry Smith    Level: intermediate
46113a471f94SBarry Smith 
46123a471f94SBarry Smith .keywords: TS,  vector, monitor, view
46133a471f94SBarry Smith 
46140ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
46153a471f94SBarry Smith @*/
46160910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
46173a471f94SBarry Smith {
46183a471f94SBarry Smith   PetscErrorCode   ierr;
461983a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
462083a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
46213a471f94SBarry Smith   Vec              work;
46223a471f94SBarry Smith 
46233a471f94SBarry Smith   PetscFunctionBegin;
4624b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
46250910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
46263a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
46270910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
46283a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
46293a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
46303a471f94SBarry Smith   PetscFunctionReturn(0);
46313a471f94SBarry Smith }
46323a471f94SBarry Smith 
4633af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
46346c699258SBarry Smith /*@
46352a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
46366c699258SBarry Smith 
46373f9fe445SBarry Smith    Logically Collective on TS and DM
46386c699258SBarry Smith 
46396c699258SBarry Smith    Input Parameters:
46402a808120SBarry Smith +  ts - the ODE integrator object
46412a808120SBarry Smith -  dm - the dm, cannot be NULL
46426c699258SBarry Smith 
4643e03a659cSJed Brown    Notes:
4644e03a659cSJed Brown    A DM can only be used for solving one problem at a time because information about the problem is stored on the DM,
4645e03a659cSJed Brown    even when not using interfaces like DMTSSetIFunction().  Use DMClone() to get a distinct DM when solving
4646e03a659cSJed Brown    different problems using the same function space.
4647e03a659cSJed Brown 
46486c699258SBarry Smith    Level: intermediate
46496c699258SBarry Smith 
46506c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
46516c699258SBarry Smith @*/
46527087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
46536c699258SBarry Smith {
46546c699258SBarry Smith   PetscErrorCode ierr;
4655089b2837SJed Brown   SNES           snes;
4656942e3340SBarry Smith   DMTS           tsdm;
46576c699258SBarry Smith 
46586c699258SBarry Smith   PetscFunctionBegin;
46590700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
46602a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
466170663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4662942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
46632a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4664942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4665942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
466624989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
466724989b8cSPeter Brune         tsdm->originaldm = dm;
466824989b8cSPeter Brune       }
466924989b8cSPeter Brune     }
46706bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
467124989b8cSPeter Brune   }
46726c699258SBarry Smith   ts->dm = dm;
4673bbd56ea5SKarl Rupp 
4674089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4675089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
46766c699258SBarry Smith   PetscFunctionReturn(0);
46776c699258SBarry Smith }
46786c699258SBarry Smith 
46796c699258SBarry Smith /*@
46806c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
46816c699258SBarry Smith 
46823f9fe445SBarry Smith    Not Collective
46836c699258SBarry Smith 
46846c699258SBarry Smith    Input Parameter:
46856c699258SBarry Smith . ts - the preconditioner context
46866c699258SBarry Smith 
46876c699258SBarry Smith    Output Parameter:
46886c699258SBarry Smith .  dm - the dm
46896c699258SBarry Smith 
46906c699258SBarry Smith    Level: intermediate
46916c699258SBarry Smith 
46926c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
46936c699258SBarry Smith @*/
46947087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
46956c699258SBarry Smith {
4696496e6a7aSJed Brown   PetscErrorCode ierr;
4697496e6a7aSJed Brown 
46986c699258SBarry Smith   PetscFunctionBegin;
46990700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4700496e6a7aSJed Brown   if (!ts->dm) {
4701ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4702496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4703496e6a7aSJed Brown   }
47046c699258SBarry Smith   *dm = ts->dm;
47056c699258SBarry Smith   PetscFunctionReturn(0);
47066c699258SBarry Smith }
47071713a123SBarry Smith 
47080f5c6efeSJed Brown /*@
47090f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
47100f5c6efeSJed Brown 
47113f9fe445SBarry Smith    Logically Collective on SNES
47120f5c6efeSJed Brown 
47130f5c6efeSJed Brown    Input Parameter:
4714d42a1c89SJed Brown + snes - nonlinear solver
47150910c330SBarry Smith . U - the current state at which to evaluate the residual
4716d42a1c89SJed Brown - ctx - user context, must be a TS
47170f5c6efeSJed Brown 
47180f5c6efeSJed Brown    Output Parameter:
47190f5c6efeSJed Brown . F - the nonlinear residual
47200f5c6efeSJed Brown 
47210f5c6efeSJed Brown    Notes:
47220f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
47230f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
47240f5c6efeSJed Brown 
47250f5c6efeSJed Brown    Level: advanced
47260f5c6efeSJed Brown 
47270f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
47280f5c6efeSJed Brown @*/
47290910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
47300f5c6efeSJed Brown {
47310f5c6efeSJed Brown   TS             ts = (TS)ctx;
47320f5c6efeSJed Brown   PetscErrorCode ierr;
47330f5c6efeSJed Brown 
47340f5c6efeSJed Brown   PetscFunctionBegin;
47350f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
47360910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
47370f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
47380f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
47390910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
47400f5c6efeSJed Brown   PetscFunctionReturn(0);
47410f5c6efeSJed Brown }
47420f5c6efeSJed Brown 
47430f5c6efeSJed Brown /*@
47440f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
47450f5c6efeSJed Brown 
47460f5c6efeSJed Brown    Collective on SNES
47470f5c6efeSJed Brown 
47480f5c6efeSJed Brown    Input Parameter:
47490f5c6efeSJed Brown + snes - nonlinear solver
47500910c330SBarry Smith . U - the current state at which to evaluate the residual
47510f5c6efeSJed Brown - ctx - user context, must be a TS
47520f5c6efeSJed Brown 
47530f5c6efeSJed Brown    Output Parameter:
47540f5c6efeSJed Brown + A - the Jacobian
47550f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
47560f5c6efeSJed Brown - flag - indicates any structure change in the matrix
47570f5c6efeSJed Brown 
47580f5c6efeSJed Brown    Notes:
47590f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
47600f5c6efeSJed Brown 
47610f5c6efeSJed Brown    Level: developer
47620f5c6efeSJed Brown 
47630f5c6efeSJed Brown .seealso: SNESSetJacobian()
47640f5c6efeSJed Brown @*/
4765d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
47660f5c6efeSJed Brown {
47670f5c6efeSJed Brown   TS             ts = (TS)ctx;
47680f5c6efeSJed Brown   PetscErrorCode ierr;
47690f5c6efeSJed Brown 
47700f5c6efeSJed Brown   PetscFunctionBegin;
47710f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
47720910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
47730f5c6efeSJed Brown   PetscValidPointer(A,3);
477494ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
47750f5c6efeSJed Brown   PetscValidPointer(B,4);
477694ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
47770f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4778d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
47790f5c6efeSJed Brown   PetscFunctionReturn(0);
47800f5c6efeSJed Brown }
4781325fc9f4SBarry Smith 
47820e4ef248SJed Brown /*@C
47839ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
47840e4ef248SJed Brown 
47850e4ef248SJed Brown    Collective on TS
47860e4ef248SJed Brown 
47870e4ef248SJed Brown    Input Arguments:
47880e4ef248SJed Brown +  ts - time stepping context
47890e4ef248SJed Brown .  t - time at which to evaluate
47900910c330SBarry Smith .  U - state at which to evaluate
47910e4ef248SJed Brown -  ctx - context
47920e4ef248SJed Brown 
47930e4ef248SJed Brown    Output Arguments:
47940e4ef248SJed Brown .  F - right hand side
47950e4ef248SJed Brown 
47960e4ef248SJed Brown    Level: intermediate
47970e4ef248SJed Brown 
47980e4ef248SJed Brown    Notes:
47990e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
48000e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
48010e4ef248SJed Brown 
48020e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
48030e4ef248SJed Brown @*/
48040910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
48050e4ef248SJed Brown {
48060e4ef248SJed Brown   PetscErrorCode ierr;
48070e4ef248SJed Brown   Mat            Arhs,Brhs;
48080e4ef248SJed Brown 
48090e4ef248SJed Brown   PetscFunctionBegin;
48100e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
4811d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
48120910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
48130e4ef248SJed Brown   PetscFunctionReturn(0);
48140e4ef248SJed Brown }
48150e4ef248SJed Brown 
48160e4ef248SJed Brown /*@C
48170e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
48180e4ef248SJed Brown 
48190e4ef248SJed Brown    Collective on TS
48200e4ef248SJed Brown 
48210e4ef248SJed Brown    Input Arguments:
48220e4ef248SJed Brown +  ts - time stepping context
48230e4ef248SJed Brown .  t - time at which to evaluate
48240910c330SBarry Smith .  U - state at which to evaluate
48250e4ef248SJed Brown -  ctx - context
48260e4ef248SJed Brown 
48270e4ef248SJed Brown    Output Arguments:
48280e4ef248SJed Brown +  A - pointer to operator
48290e4ef248SJed Brown .  B - pointer to preconditioning matrix
48300e4ef248SJed Brown -  flg - matrix structure flag
48310e4ef248SJed Brown 
48320e4ef248SJed Brown    Level: intermediate
48330e4ef248SJed Brown 
48340e4ef248SJed Brown    Notes:
48350e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
48360e4ef248SJed Brown 
48370e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
48380e4ef248SJed Brown @*/
4839d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
48400e4ef248SJed Brown {
48410e4ef248SJed Brown   PetscFunctionBegin;
48420e4ef248SJed Brown   PetscFunctionReturn(0);
48430e4ef248SJed Brown }
48440e4ef248SJed Brown 
48450026cea9SSean Farley /*@C
48460026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
48470026cea9SSean Farley 
48480026cea9SSean Farley    Collective on TS
48490026cea9SSean Farley 
48500026cea9SSean Farley    Input Arguments:
48510026cea9SSean Farley +  ts - time stepping context
48520026cea9SSean Farley .  t - time at which to evaluate
48530910c330SBarry Smith .  U - state at which to evaluate
48540910c330SBarry Smith .  Udot - time derivative of state vector
48550026cea9SSean Farley -  ctx - context
48560026cea9SSean Farley 
48570026cea9SSean Farley    Output Arguments:
48580026cea9SSean Farley .  F - left hand side
48590026cea9SSean Farley 
48600026cea9SSean Farley    Level: intermediate
48610026cea9SSean Farley 
48620026cea9SSean Farley    Notes:
48630910c330SBarry 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
48640026cea9SSean Farley    user is required to write their own TSComputeIFunction.
48650026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
48660026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
48670026cea9SSean Farley 
48689ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
48699ae8fd06SBarry Smith 
48709ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
48710026cea9SSean Farley @*/
48720910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
48730026cea9SSean Farley {
48740026cea9SSean Farley   PetscErrorCode ierr;
48750026cea9SSean Farley   Mat            A,B;
48760026cea9SSean Farley 
48770026cea9SSean Farley   PetscFunctionBegin;
48780298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
4879d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
48800910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
48810026cea9SSean Farley   PetscFunctionReturn(0);
48820026cea9SSean Farley }
48830026cea9SSean Farley 
48840026cea9SSean Farley /*@C
4885b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
48860026cea9SSean Farley 
48870026cea9SSean Farley    Collective on TS
48880026cea9SSean Farley 
48890026cea9SSean Farley    Input Arguments:
48900026cea9SSean Farley +  ts - time stepping context
48910026cea9SSean Farley .  t - time at which to evaluate
48920910c330SBarry Smith .  U - state at which to evaluate
48930910c330SBarry Smith .  Udot - time derivative of state vector
48940026cea9SSean Farley .  shift - shift to apply
48950026cea9SSean Farley -  ctx - context
48960026cea9SSean Farley 
48970026cea9SSean Farley    Output Arguments:
48980026cea9SSean Farley +  A - pointer to operator
48990026cea9SSean Farley .  B - pointer to preconditioning matrix
49000026cea9SSean Farley -  flg - matrix structure flag
49010026cea9SSean Farley 
4902b41af12eSJed Brown    Level: advanced
49030026cea9SSean Farley 
49040026cea9SSean Farley    Notes:
49050026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
49060026cea9SSean Farley 
4907b41af12eSJed Brown    It is only appropriate for problems of the form
4908b41af12eSJed Brown 
4909b41af12eSJed Brown $     M Udot = F(U,t)
4910b41af12eSJed Brown 
4911b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4912b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4913b41af12eSJed Brown   an implicit operator of the form
4914b41af12eSJed Brown 
4915b41af12eSJed Brown $    shift*M + J
4916b41af12eSJed Brown 
4917b41af12eSJed 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
4918b41af12eSJed Brown   a copy of M or reassemble it when requested.
4919b41af12eSJed Brown 
49200026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
49210026cea9SSean Farley @*/
4922d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
49230026cea9SSean Farley {
4924b41af12eSJed Brown   PetscErrorCode ierr;
4925b41af12eSJed Brown 
49260026cea9SSean Farley   PetscFunctionBegin;
492794ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4928b41af12eSJed Brown   ts->ijacobian.shift = shift;
49290026cea9SSean Farley   PetscFunctionReturn(0);
49300026cea9SSean Farley }
4931b41af12eSJed Brown 
4932e817cc15SEmil Constantinescu /*@
4933e817cc15SEmil Constantinescu    TSGetEquationType - Gets 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
4939e817cc15SEmil Constantinescu 
4940e817cc15SEmil Constantinescu    Output Parameter:
49414e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4942e817cc15SEmil Constantinescu 
4943e817cc15SEmil Constantinescu    Level: beginner
4944e817cc15SEmil Constantinescu 
4945e817cc15SEmil Constantinescu .keywords: TS, equation type
4946e817cc15SEmil Constantinescu 
4947e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4948e817cc15SEmil Constantinescu @*/
4949e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4950e817cc15SEmil Constantinescu {
4951e817cc15SEmil Constantinescu   PetscFunctionBegin;
4952e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4953e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4954e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4955e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4956e817cc15SEmil Constantinescu }
4957e817cc15SEmil Constantinescu 
4958e817cc15SEmil Constantinescu /*@
4959e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4960e817cc15SEmil Constantinescu 
4961e817cc15SEmil Constantinescu    Not Collective
4962e817cc15SEmil Constantinescu 
4963e817cc15SEmil Constantinescu    Input Parameter:
4964e817cc15SEmil Constantinescu +  ts - the TS context
49651297b224SEmil Constantinescu -  equation_type - see TSEquationType
4966e817cc15SEmil Constantinescu 
4967e817cc15SEmil Constantinescu    Level: advanced
4968e817cc15SEmil Constantinescu 
4969e817cc15SEmil Constantinescu .keywords: TS, equation type
4970e817cc15SEmil Constantinescu 
4971e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4972e817cc15SEmil Constantinescu @*/
4973e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4974e817cc15SEmil Constantinescu {
4975e817cc15SEmil Constantinescu   PetscFunctionBegin;
4976e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4977e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4978e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4979e817cc15SEmil Constantinescu }
49800026cea9SSean Farley 
49814af1b03aSJed Brown /*@
49824af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
49834af1b03aSJed Brown 
49844af1b03aSJed Brown    Not Collective
49854af1b03aSJed Brown 
49864af1b03aSJed Brown    Input Parameter:
49874af1b03aSJed Brown .  ts - the TS context
49884af1b03aSJed Brown 
49894af1b03aSJed Brown    Output Parameter:
49904af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
49914af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
49924af1b03aSJed Brown 
4993487e0bb9SJed Brown    Level: beginner
49944af1b03aSJed Brown 
4995cd652676SJed Brown    Notes:
4996cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
49974af1b03aSJed Brown 
49984af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
49994af1b03aSJed Brown 
50004af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
50014af1b03aSJed Brown @*/
50024af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
50034af1b03aSJed Brown {
50044af1b03aSJed Brown   PetscFunctionBegin;
50054af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
50064af1b03aSJed Brown   PetscValidPointer(reason,2);
50074af1b03aSJed Brown   *reason = ts->reason;
50084af1b03aSJed Brown   PetscFunctionReturn(0);
50094af1b03aSJed Brown }
50104af1b03aSJed Brown 
5011d6ad946cSShri Abhyankar /*@
5012d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
5013d6ad946cSShri Abhyankar 
5014d6ad946cSShri Abhyankar    Not Collective
5015d6ad946cSShri Abhyankar 
5016d6ad946cSShri Abhyankar    Input Parameter:
5017d6ad946cSShri Abhyankar +  ts - the TS context
5018d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
5019d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
5020d6ad946cSShri Abhyankar 
5021f5abba47SShri Abhyankar    Level: advanced
5022d6ad946cSShri Abhyankar 
5023d6ad946cSShri Abhyankar    Notes:
5024d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
5025d6ad946cSShri Abhyankar 
5026d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
5027d6ad946cSShri Abhyankar 
5028d6ad946cSShri Abhyankar .seealso: TSConvergedReason
5029d6ad946cSShri Abhyankar @*/
5030d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
5031d6ad946cSShri Abhyankar {
5032d6ad946cSShri Abhyankar   PetscFunctionBegin;
5033d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5034d6ad946cSShri Abhyankar   ts->reason = reason;
5035d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
5036d6ad946cSShri Abhyankar }
5037d6ad946cSShri Abhyankar 
5038cc708dedSBarry Smith /*@
5039cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
5040cc708dedSBarry Smith 
5041cc708dedSBarry Smith    Not Collective
5042cc708dedSBarry Smith 
5043cc708dedSBarry Smith    Input Parameter:
5044cc708dedSBarry Smith .  ts - the TS context
5045cc708dedSBarry Smith 
5046cc708dedSBarry Smith    Output Parameter:
504719eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
5048cc708dedSBarry Smith 
5049487e0bb9SJed Brown    Level: beginner
5050cc708dedSBarry Smith 
5051cc708dedSBarry Smith    Notes:
5052cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
5053cc708dedSBarry Smith 
5054cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
5055cc708dedSBarry Smith 
5056cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
5057cc708dedSBarry Smith @*/
5058cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5059cc708dedSBarry Smith {
5060cc708dedSBarry Smith   PetscFunctionBegin;
5061cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5062cc708dedSBarry Smith   PetscValidPointer(ftime,2);
5063cc708dedSBarry Smith   *ftime = ts->solvetime;
5064cc708dedSBarry Smith   PetscFunctionReturn(0);
5065cc708dedSBarry Smith }
5066cc708dedSBarry Smith 
50672c18e0fdSBarry Smith /*@
50685ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
50699f67acb7SJed Brown    used by the time integrator.
50709f67acb7SJed Brown 
50719f67acb7SJed Brown    Not Collective
50729f67acb7SJed Brown 
50739f67acb7SJed Brown    Input Parameter:
50749f67acb7SJed Brown .  ts - TS context
50759f67acb7SJed Brown 
50769f67acb7SJed Brown    Output Parameter:
50779f67acb7SJed Brown .  nits - number of nonlinear iterations
50789f67acb7SJed Brown 
50799f67acb7SJed Brown    Notes:
50809f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
50819f67acb7SJed Brown 
50829f67acb7SJed Brown    Level: intermediate
50839f67acb7SJed Brown 
50849f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
50859f67acb7SJed Brown 
50865ef26d82SJed Brown .seealso:  TSGetKSPIterations()
50879f67acb7SJed Brown @*/
50885ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
50899f67acb7SJed Brown {
50909f67acb7SJed Brown   PetscFunctionBegin;
50919f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
50929f67acb7SJed Brown   PetscValidIntPointer(nits,2);
50935ef26d82SJed Brown   *nits = ts->snes_its;
50949f67acb7SJed Brown   PetscFunctionReturn(0);
50959f67acb7SJed Brown }
50969f67acb7SJed Brown 
50979f67acb7SJed Brown /*@
50985ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
50999f67acb7SJed Brown    used by the time integrator.
51009f67acb7SJed Brown 
51019f67acb7SJed Brown    Not Collective
51029f67acb7SJed Brown 
51039f67acb7SJed Brown    Input Parameter:
51049f67acb7SJed Brown .  ts - TS context
51059f67acb7SJed Brown 
51069f67acb7SJed Brown    Output Parameter:
51079f67acb7SJed Brown .  lits - number of linear iterations
51089f67acb7SJed Brown 
51099f67acb7SJed Brown    Notes:
51109f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
51119f67acb7SJed Brown 
51129f67acb7SJed Brown    Level: intermediate
51139f67acb7SJed Brown 
51149f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
51159f67acb7SJed Brown 
51165ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
51179f67acb7SJed Brown @*/
51185ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
51199f67acb7SJed Brown {
51209f67acb7SJed Brown   PetscFunctionBegin;
51219f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
51229f67acb7SJed Brown   PetscValidIntPointer(lits,2);
51235ef26d82SJed Brown   *lits = ts->ksp_its;
51249f67acb7SJed Brown   PetscFunctionReturn(0);
51259f67acb7SJed Brown }
51269f67acb7SJed Brown 
5127cef5090cSJed Brown /*@
5128cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5129cef5090cSJed Brown 
5130cef5090cSJed Brown    Not Collective
5131cef5090cSJed Brown 
5132cef5090cSJed Brown    Input Parameter:
5133cef5090cSJed Brown .  ts - TS context
5134cef5090cSJed Brown 
5135cef5090cSJed Brown    Output Parameter:
5136cef5090cSJed Brown .  rejects - number of steps rejected
5137cef5090cSJed Brown 
5138cef5090cSJed Brown    Notes:
5139cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5140cef5090cSJed Brown 
5141cef5090cSJed Brown    Level: intermediate
5142cef5090cSJed Brown 
5143cef5090cSJed Brown .keywords: TS, get, number
5144cef5090cSJed Brown 
51455ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5146cef5090cSJed Brown @*/
5147cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5148cef5090cSJed Brown {
5149cef5090cSJed Brown   PetscFunctionBegin;
5150cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5151cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5152cef5090cSJed Brown   *rejects = ts->reject;
5153cef5090cSJed Brown   PetscFunctionReturn(0);
5154cef5090cSJed Brown }
5155cef5090cSJed Brown 
5156cef5090cSJed Brown /*@
5157cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5158cef5090cSJed Brown 
5159cef5090cSJed Brown    Not Collective
5160cef5090cSJed Brown 
5161cef5090cSJed Brown    Input Parameter:
5162cef5090cSJed Brown .  ts - TS context
5163cef5090cSJed Brown 
5164cef5090cSJed Brown    Output Parameter:
5165cef5090cSJed Brown .  fails - number of failed nonlinear solves
5166cef5090cSJed Brown 
5167cef5090cSJed Brown    Notes:
5168cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5169cef5090cSJed Brown 
5170cef5090cSJed Brown    Level: intermediate
5171cef5090cSJed Brown 
5172cef5090cSJed Brown .keywords: TS, get, number
5173cef5090cSJed Brown 
51745ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5175cef5090cSJed Brown @*/
5176cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5177cef5090cSJed Brown {
5178cef5090cSJed Brown   PetscFunctionBegin;
5179cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5180cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5181cef5090cSJed Brown   *fails = ts->num_snes_failures;
5182cef5090cSJed Brown   PetscFunctionReturn(0);
5183cef5090cSJed Brown }
5184cef5090cSJed Brown 
5185cef5090cSJed Brown /*@
5186cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5187cef5090cSJed Brown 
5188cef5090cSJed Brown    Not Collective
5189cef5090cSJed Brown 
5190cef5090cSJed Brown    Input Parameter:
5191cef5090cSJed Brown +  ts - TS context
5192cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5193cef5090cSJed Brown 
5194cef5090cSJed Brown    Notes:
5195cef5090cSJed Brown    The counter is reset to zero for each step
5196cef5090cSJed Brown 
5197cef5090cSJed Brown    Options Database Key:
5198cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5199cef5090cSJed Brown 
5200cef5090cSJed Brown    Level: intermediate
5201cef5090cSJed Brown 
5202cef5090cSJed Brown .keywords: TS, set, maximum, number
5203cef5090cSJed Brown 
52045ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5205cef5090cSJed Brown @*/
5206cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5207cef5090cSJed Brown {
5208cef5090cSJed Brown   PetscFunctionBegin;
5209cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5210cef5090cSJed Brown   ts->max_reject = rejects;
5211cef5090cSJed Brown   PetscFunctionReturn(0);
5212cef5090cSJed Brown }
5213cef5090cSJed Brown 
5214cef5090cSJed Brown /*@
5215cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5216cef5090cSJed Brown 
5217cef5090cSJed Brown    Not Collective
5218cef5090cSJed Brown 
5219cef5090cSJed Brown    Input Parameter:
5220cef5090cSJed Brown +  ts - TS context
5221cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5222cef5090cSJed Brown 
5223cef5090cSJed Brown    Notes:
5224cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5225cef5090cSJed Brown 
5226cef5090cSJed Brown    Options Database Key:
5227cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5228cef5090cSJed Brown 
5229cef5090cSJed Brown    Level: intermediate
5230cef5090cSJed Brown 
5231cef5090cSJed Brown .keywords: TS, set, maximum, number
5232cef5090cSJed Brown 
52335ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5234cef5090cSJed Brown @*/
5235cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5236cef5090cSJed Brown {
5237cef5090cSJed Brown   PetscFunctionBegin;
5238cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5239cef5090cSJed Brown   ts->max_snes_failures = fails;
5240cef5090cSJed Brown   PetscFunctionReturn(0);
5241cef5090cSJed Brown }
5242cef5090cSJed Brown 
5243cef5090cSJed Brown /*@
5244cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5245cef5090cSJed Brown 
5246cef5090cSJed Brown    Not Collective
5247cef5090cSJed Brown 
5248cef5090cSJed Brown    Input Parameter:
5249cef5090cSJed Brown +  ts - TS context
5250cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5251cef5090cSJed Brown 
5252cef5090cSJed Brown    Options Database Key:
5253cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5254cef5090cSJed Brown 
5255cef5090cSJed Brown    Level: intermediate
5256cef5090cSJed Brown 
5257cef5090cSJed Brown .keywords: TS, set, error
5258cef5090cSJed Brown 
52595ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5260cef5090cSJed Brown @*/
5261cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5262cef5090cSJed Brown {
5263cef5090cSJed Brown   PetscFunctionBegin;
5264cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5265cef5090cSJed Brown   ts->errorifstepfailed = err;
5266cef5090cSJed Brown   PetscFunctionReturn(0);
5267cef5090cSJed Brown }
5268cef5090cSJed Brown 
5269fb1732b5SBarry Smith /*@C
5270fde5950dSBarry 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
5271fb1732b5SBarry Smith 
5272fb1732b5SBarry Smith    Collective on TS
5273fb1732b5SBarry Smith 
5274fb1732b5SBarry Smith    Input Parameters:
5275fb1732b5SBarry Smith +  ts - the TS context
5276fb1732b5SBarry Smith .  step - current time-step
5277fb1732b5SBarry Smith .  ptime - current time
52780910c330SBarry Smith .  u - current state
5279721cd6eeSBarry Smith -  vf - viewer and its format
5280fb1732b5SBarry Smith 
5281fb1732b5SBarry Smith    Level: intermediate
5282fb1732b5SBarry Smith 
5283fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
5284fb1732b5SBarry Smith 
5285fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5286fb1732b5SBarry Smith @*/
5287721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5288fb1732b5SBarry Smith {
5289fb1732b5SBarry Smith   PetscErrorCode ierr;
5290fb1732b5SBarry Smith 
5291fb1732b5SBarry Smith   PetscFunctionBegin;
5292721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5293721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5294721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5295ed81e22dSJed Brown   PetscFunctionReturn(0);
5296ed81e22dSJed Brown }
5297ed81e22dSJed Brown 
5298ed81e22dSJed Brown /*@C
5299ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5300ed81e22dSJed Brown 
5301ed81e22dSJed Brown    Collective on TS
5302ed81e22dSJed Brown 
5303ed81e22dSJed Brown    Input Parameters:
5304ed81e22dSJed Brown +  ts - the TS context
5305ed81e22dSJed Brown .  step - current time-step
5306ed81e22dSJed Brown .  ptime - current time
53070910c330SBarry Smith .  u - current state
5308ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5309ed81e22dSJed Brown 
5310ed81e22dSJed Brown    Level: intermediate
5311ed81e22dSJed Brown 
5312ed81e22dSJed Brown    Notes:
5313ed81e22dSJed 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.
5314ed81e22dSJed Brown    These are named according to the file name template.
5315ed81e22dSJed Brown 
5316ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5317ed81e22dSJed Brown 
5318ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5319ed81e22dSJed Brown 
5320ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5321ed81e22dSJed Brown @*/
53220910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5323ed81e22dSJed Brown {
5324ed81e22dSJed Brown   PetscErrorCode ierr;
5325ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5326ed81e22dSJed Brown   PetscViewer    viewer;
5327ed81e22dSJed Brown 
5328ed81e22dSJed Brown   PetscFunctionBegin;
532963e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
53308caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5331ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
53320910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5333ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5334ed81e22dSJed Brown   PetscFunctionReturn(0);
5335ed81e22dSJed Brown }
5336ed81e22dSJed Brown 
5337ed81e22dSJed Brown /*@C
5338ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5339ed81e22dSJed Brown 
5340ed81e22dSJed Brown    Collective on TS
5341ed81e22dSJed Brown 
5342ed81e22dSJed Brown    Input Parameters:
5343ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5344ed81e22dSJed Brown 
5345ed81e22dSJed Brown    Level: intermediate
5346ed81e22dSJed Brown 
5347ed81e22dSJed Brown    Note:
5348ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5349ed81e22dSJed Brown 
5350ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5351ed81e22dSJed Brown 
5352ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5353ed81e22dSJed Brown @*/
5354ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5355ed81e22dSJed Brown {
5356ed81e22dSJed Brown   PetscErrorCode ierr;
5357ed81e22dSJed Brown 
5358ed81e22dSJed Brown   PetscFunctionBegin;
5359ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5360fb1732b5SBarry Smith   PetscFunctionReturn(0);
5361fb1732b5SBarry Smith }
5362fb1732b5SBarry Smith 
536384df9cb4SJed Brown /*@
5364552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
536584df9cb4SJed Brown 
5366ed81e22dSJed Brown    Collective on TS if controller has not been created yet
536784df9cb4SJed Brown 
536884df9cb4SJed Brown    Input Arguments:
5369ed81e22dSJed Brown .  ts - time stepping context
537084df9cb4SJed Brown 
537184df9cb4SJed Brown    Output Arguments:
5372ed81e22dSJed Brown .  adapt - adaptive controller
537384df9cb4SJed Brown 
537484df9cb4SJed Brown    Level: intermediate
537584df9cb4SJed Brown 
5376ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
537784df9cb4SJed Brown @*/
5378552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
537984df9cb4SJed Brown {
538084df9cb4SJed Brown   PetscErrorCode ierr;
538184df9cb4SJed Brown 
538284df9cb4SJed Brown   PetscFunctionBegin;
538384df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5384bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
538584df9cb4SJed Brown   if (!ts->adapt) {
5386ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
53873bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
53881c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
538984df9cb4SJed Brown   }
5390bec58848SLisandro Dalcin   *adapt = ts->adapt;
539184df9cb4SJed Brown   PetscFunctionReturn(0);
539284df9cb4SJed Brown }
5393d6ebe24aSShri Abhyankar 
53941c3436cfSJed Brown /*@
53951c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
53961c3436cfSJed Brown 
53971c3436cfSJed Brown    Logically Collective
53981c3436cfSJed Brown 
53991c3436cfSJed Brown    Input Arguments:
54001c3436cfSJed Brown +  ts - time integration context
54011c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
54020298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
54031c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
54040298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
54051c3436cfSJed Brown 
5406a3cdaa26SBarry Smith    Options Database keys:
5407a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5408a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5409a3cdaa26SBarry Smith 
54103ff766beSShri Abhyankar    Notes:
54113ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
54123ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
54133ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
54143ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
54153ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
54163ff766beSShri Abhyankar    differential variables.
54173ff766beSShri Abhyankar 
54181c3436cfSJed Brown    Level: beginner
54191c3436cfSJed Brown 
5420c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
54211c3436cfSJed Brown @*/
54221c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
54231c3436cfSJed Brown {
54241c3436cfSJed Brown   PetscErrorCode ierr;
54251c3436cfSJed Brown 
54261c3436cfSJed Brown   PetscFunctionBegin;
5427c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
54281c3436cfSJed Brown   if (vatol) {
54291c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
54301c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
54311c3436cfSJed Brown     ts->vatol = vatol;
54321c3436cfSJed Brown   }
5433c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
54341c3436cfSJed Brown   if (vrtol) {
54351c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
54361c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
54371c3436cfSJed Brown     ts->vrtol = vrtol;
54381c3436cfSJed Brown   }
54391c3436cfSJed Brown   PetscFunctionReturn(0);
54401c3436cfSJed Brown }
54411c3436cfSJed Brown 
5442c5033834SJed Brown /*@
5443c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5444c5033834SJed Brown 
5445c5033834SJed Brown    Logically Collective
5446c5033834SJed Brown 
5447c5033834SJed Brown    Input Arguments:
5448c5033834SJed Brown .  ts - time integration context
5449c5033834SJed Brown 
5450c5033834SJed Brown    Output Arguments:
54510298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
54520298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
54530298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
54540298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5455c5033834SJed Brown 
5456c5033834SJed Brown    Level: beginner
5457c5033834SJed Brown 
5458c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5459c5033834SJed Brown @*/
5460c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5461c5033834SJed Brown {
5462c5033834SJed Brown   PetscFunctionBegin;
5463c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5464c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5465c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5466c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5467c5033834SJed Brown   PetscFunctionReturn(0);
5468c5033834SJed Brown }
5469c5033834SJed Brown 
54709c6b16b5SShri Abhyankar /*@
5471a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
54729c6b16b5SShri Abhyankar 
54739c6b16b5SShri Abhyankar    Collective on TS
54749c6b16b5SShri Abhyankar 
54759c6b16b5SShri Abhyankar    Input Arguments:
54769c6b16b5SShri Abhyankar +  ts - time stepping context
5477a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5478a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
54799c6b16b5SShri Abhyankar 
54809c6b16b5SShri Abhyankar    Output Arguments:
54817453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
54827453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
54837453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
54849c6b16b5SShri Abhyankar 
54859c6b16b5SShri Abhyankar    Level: developer
54869c6b16b5SShri Abhyankar 
5487deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
54889c6b16b5SShri Abhyankar @*/
54897453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
54909c6b16b5SShri Abhyankar {
54919c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
54929c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
54937453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
54947453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
54959c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
54967453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
54977453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
54987453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
54999c6b16b5SShri Abhyankar 
55009c6b16b5SShri Abhyankar   PetscFunctionBegin;
55019c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5502a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5503a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5504a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5505a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5506a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5507a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
55088a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
55098a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5510a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
55119c6b16b5SShri Abhyankar 
55129c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
55139c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
55149c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
55159c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
55169c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
55177453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
55187453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
55197453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
55209c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
55219c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
55229c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55239c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55249c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
5525*76cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
55267453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55277453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
55287453f775SEmil Constantinescu       if(tola>0.){
55297453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
55307453f775SEmil Constantinescu         na_loc++;
55317453f775SEmil Constantinescu       }
55327453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55337453f775SEmil Constantinescu       if(tolr>0.){
55347453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
55357453f775SEmil Constantinescu         nr_loc++;
55367453f775SEmil Constantinescu       }
55377453f775SEmil Constantinescu       tol=tola+tolr;
55387453f775SEmil Constantinescu       if(tol>0.){
55397453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
55407453f775SEmil Constantinescu         n_loc++;
55417453f775SEmil Constantinescu       }
55429c6b16b5SShri Abhyankar     }
55439c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55449c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55459c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
55469c6b16b5SShri Abhyankar     const PetscScalar *atol;
55479c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55489c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
5549*76cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
55507453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55517453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
55527453f775SEmil Constantinescu       if(tola>0.){
55537453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
55547453f775SEmil Constantinescu         na_loc++;
55557453f775SEmil Constantinescu       }
55567453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55577453f775SEmil Constantinescu       if(tolr>0.){
55587453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
55597453f775SEmil Constantinescu         nr_loc++;
55607453f775SEmil Constantinescu       }
55617453f775SEmil Constantinescu       tol=tola+tolr;
55627453f775SEmil Constantinescu       if(tol>0.){
55637453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
55647453f775SEmil Constantinescu         n_loc++;
55657453f775SEmil Constantinescu       }
55669c6b16b5SShri Abhyankar     }
55679c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55689c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
55699c6b16b5SShri Abhyankar     const PetscScalar *rtol;
55709c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55719c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
5572*76cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
55737453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55747453f775SEmil Constantinescu       tola = ts->atol;
55757453f775SEmil Constantinescu       if(tola>0.){
55767453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
55777453f775SEmil Constantinescu         na_loc++;
55787453f775SEmil Constantinescu       }
55797453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55807453f775SEmil Constantinescu       if(tolr>0.){
55817453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
55827453f775SEmil Constantinescu         nr_loc++;
55837453f775SEmil Constantinescu       }
55847453f775SEmil Constantinescu       tol=tola+tolr;
55857453f775SEmil Constantinescu       if(tol>0.){
55867453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
55877453f775SEmil Constantinescu         n_loc++;
55887453f775SEmil Constantinescu       }
55899c6b16b5SShri Abhyankar     }
55909c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55919c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
55929c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
5593*76cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
55947453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55957453f775SEmil Constantinescu       tola = ts->atol;
55967453f775SEmil Constantinescu       if(tola>0.){
55977453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
55987453f775SEmil Constantinescu         na_loc++;
55997453f775SEmil Constantinescu       }
56007453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56017453f775SEmil Constantinescu       if(tolr>0.){
56027453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
56037453f775SEmil Constantinescu         nr_loc++;
56047453f775SEmil Constantinescu       }
56057453f775SEmil Constantinescu       tol=tola+tolr;
56067453f775SEmil Constantinescu       if(tol>0.){
56077453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
56087453f775SEmil Constantinescu         n_loc++;
56097453f775SEmil Constantinescu       }
56109c6b16b5SShri Abhyankar     }
56119c6b16b5SShri Abhyankar   }
56129c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
56139c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
56149c6b16b5SShri Abhyankar 
56157453f775SEmil Constantinescu   err_loc[0] = sum;
56167453f775SEmil Constantinescu   err_loc[1] = suma;
56177453f775SEmil Constantinescu   err_loc[2] = sumr;
56187453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
56197453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
56207453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
56217453f775SEmil Constantinescu 
5622a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
56237453f775SEmil Constantinescu 
56247453f775SEmil Constantinescu   gsum   = err_glb[0];
56257453f775SEmil Constantinescu   gsuma  = err_glb[1];
56267453f775SEmil Constantinescu   gsumr  = err_glb[2];
56277453f775SEmil Constantinescu   n_glb  = err_glb[3];
56287453f775SEmil Constantinescu   na_glb = err_glb[4];
56297453f775SEmil Constantinescu   nr_glb = err_glb[5];
56307453f775SEmil Constantinescu 
5631b1316ef9SEmil Constantinescu   *norm  = 0.;
5632b1316ef9SEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
5633b1316ef9SEmil Constantinescu   *norma = 0.;
5634b1316ef9SEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5635b1316ef9SEmil Constantinescu   *normr = 0.;
5636b1316ef9SEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
56379c6b16b5SShri Abhyankar 
56389c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
56397453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
56407453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
56419c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
56429c6b16b5SShri Abhyankar }
56439c6b16b5SShri Abhyankar 
56449c6b16b5SShri Abhyankar /*@
5645a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
56469c6b16b5SShri Abhyankar 
56479c6b16b5SShri Abhyankar    Collective on TS
56489c6b16b5SShri Abhyankar 
56499c6b16b5SShri Abhyankar    Input Arguments:
56509c6b16b5SShri Abhyankar +  ts - time stepping context
5651a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5652a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
56539c6b16b5SShri Abhyankar 
56549c6b16b5SShri Abhyankar    Output Arguments:
56557453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
56567453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
56577453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
56589c6b16b5SShri Abhyankar 
56599c6b16b5SShri Abhyankar    Level: developer
56609c6b16b5SShri Abhyankar 
5661deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
56629c6b16b5SShri Abhyankar @*/
56637453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
56649c6b16b5SShri Abhyankar {
56659c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
56667453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
56679c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
56687453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
56697453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
56707453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
56719c6b16b5SShri Abhyankar 
56729c6b16b5SShri Abhyankar   PetscFunctionBegin;
56739c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5674a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5675a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5676a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5677a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5678a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5679a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
56808a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
56818a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5682a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
56839c6b16b5SShri Abhyankar 
56849c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
56859c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
56869c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
56879c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
56889c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
56897453f775SEmil Constantinescu 
56907453f775SEmil Constantinescu   max=0.;
56917453f775SEmil Constantinescu   maxa=0.;
56927453f775SEmil Constantinescu   maxr=0.;
56937453f775SEmil Constantinescu 
56947453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
56959c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
56969c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56979c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56987453f775SEmil Constantinescu 
56997453f775SEmil Constantinescu     for (i=0; i<n; i++) {
5700*76cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57017453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57027453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
57037453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57047453f775SEmil Constantinescu       tol  = tola+tolr;
57057453f775SEmil Constantinescu       if(tola>0.){
57067453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
57077453f775SEmil Constantinescu       }
57087453f775SEmil Constantinescu       if(tolr>0.){
57097453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
57107453f775SEmil Constantinescu       }
57117453f775SEmil Constantinescu       if(tol>0.){
57127453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
57137453f775SEmil Constantinescu       }
57149c6b16b5SShri Abhyankar     }
57159c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57169c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57179c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
57189c6b16b5SShri Abhyankar     const PetscScalar *atol;
57199c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57207453f775SEmil Constantinescu     for (i=0; i<n; i++) {
5721*76cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57227453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57237453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
57247453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57257453f775SEmil Constantinescu       tol  = tola+tolr;
57267453f775SEmil Constantinescu       if(tola>0.){
57277453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
57287453f775SEmil Constantinescu       }
57297453f775SEmil Constantinescu       if(tolr>0.){
57307453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
57317453f775SEmil Constantinescu       }
57327453f775SEmil Constantinescu       if(tol>0.){
57337453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
57347453f775SEmil Constantinescu       }
57359c6b16b5SShri Abhyankar     }
57369c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57379c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
57389c6b16b5SShri Abhyankar     const PetscScalar *rtol;
57399c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57407453f775SEmil Constantinescu 
57417453f775SEmil Constantinescu     for (i=0; i<n; i++) {
5742*76cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57437453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57447453f775SEmil Constantinescu       tola = ts->atol;
57457453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57467453f775SEmil Constantinescu       tol  = tola+tolr;
57477453f775SEmil Constantinescu       if(tola>0.){
57487453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
57497453f775SEmil Constantinescu       }
57507453f775SEmil Constantinescu       if(tolr>0.){
57517453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
57527453f775SEmil Constantinescu       }
57537453f775SEmil Constantinescu       if(tol>0.){
57547453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
57557453f775SEmil Constantinescu       }
57569c6b16b5SShri Abhyankar     }
57579c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57589c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
57597453f775SEmil Constantinescu 
57607453f775SEmil Constantinescu     for (i=0; i<n; i++) {
5761*76cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
57627453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57637453f775SEmil Constantinescu       tola = ts->atol;
57647453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57657453f775SEmil Constantinescu       tol  = tola+tolr;
57667453f775SEmil Constantinescu       if(tola>0.){
57677453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
57687453f775SEmil Constantinescu       }
57697453f775SEmil Constantinescu       if(tolr>0.){
57707453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
57717453f775SEmil Constantinescu       }
57727453f775SEmil Constantinescu       if(tol>0.){
57737453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
57747453f775SEmil Constantinescu       }
57759c6b16b5SShri Abhyankar     }
57769c6b16b5SShri Abhyankar   }
57779c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
57789c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
57797453f775SEmil Constantinescu   err_loc[0] = max;
57807453f775SEmil Constantinescu   err_loc[1] = maxa;
57817453f775SEmil Constantinescu   err_loc[2] = maxr;
5782a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
57837453f775SEmil Constantinescu   gmax   = err_glb[0];
57847453f775SEmil Constantinescu   gmaxa  = err_glb[1];
57857453f775SEmil Constantinescu   gmaxr  = err_glb[2];
57869c6b16b5SShri Abhyankar 
57879c6b16b5SShri Abhyankar   *norm = gmax;
57887453f775SEmil Constantinescu   *norma = gmaxa;
57897453f775SEmil Constantinescu   *normr = gmaxr;
57909c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
57917453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
57927453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
57939c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
57949c6b16b5SShri Abhyankar }
57959c6b16b5SShri Abhyankar 
57961c3436cfSJed Brown /*@
57978a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
57981c3436cfSJed Brown 
57991c3436cfSJed Brown    Collective on TS
58001c3436cfSJed Brown 
58011c3436cfSJed Brown    Input Arguments:
58021c3436cfSJed Brown +  ts - time stepping context
5803a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5804a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
5805a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
58067619abb3SShri 
58071c3436cfSJed Brown    Output Arguments:
58088a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
58098a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
58108a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
5811a4868fbcSLisandro Dalcin 
5812a4868fbcSLisandro Dalcin    Options Database Keys:
5813a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
5814a4868fbcSLisandro Dalcin 
58151c3436cfSJed Brown    Level: developer
58161c3436cfSJed Brown 
58178a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
58181c3436cfSJed Brown @*/
58197453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
58201c3436cfSJed Brown {
58218beabaa1SBarry Smith   PetscErrorCode ierr;
58221c3436cfSJed Brown 
58231c3436cfSJed Brown   PetscFunctionBegin;
5824a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
58257453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
5826a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
58277453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
5828a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
58291c3436cfSJed Brown   PetscFunctionReturn(0);
58301c3436cfSJed Brown }
58311c3436cfSJed Brown 
58328a175baeSEmil Constantinescu 
58338a175baeSEmil Constantinescu /*@
58348a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
58358a175baeSEmil Constantinescu 
58368a175baeSEmil Constantinescu    Collective on TS
58378a175baeSEmil Constantinescu 
58388a175baeSEmil Constantinescu    Input Arguments:
58398a175baeSEmil Constantinescu +  ts - time stepping context
58408a175baeSEmil Constantinescu .  E - error vector
58418a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
58428a175baeSEmil Constantinescu -  Y - state vector, previous time step
58438a175baeSEmil Constantinescu 
58448a175baeSEmil Constantinescu    Output Arguments:
58458a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
58468a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
58478a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
58488a175baeSEmil Constantinescu 
58498a175baeSEmil Constantinescu    Level: developer
58508a175baeSEmil Constantinescu 
58518a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
58528a175baeSEmil Constantinescu @*/
58538a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
58548a175baeSEmil Constantinescu {
58558a175baeSEmil Constantinescu   PetscErrorCode    ierr;
58568a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
58578a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
58588a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
58598a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
58608a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
58618a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
58628a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
58638a175baeSEmil Constantinescu 
58648a175baeSEmil Constantinescu   PetscFunctionBegin;
58658a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
58668a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
58678a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
58688a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
58698a175baeSEmil Constantinescu   PetscValidType(E,2);
58708a175baeSEmil Constantinescu   PetscValidType(U,3);
58718a175baeSEmil Constantinescu   PetscValidType(Y,4);
58728a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
58738a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
58748a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
58758a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
58768a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
58778a175baeSEmil Constantinescu 
58788a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
58798a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
58808a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
58818a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
58828a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
58838a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
58848a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
58858a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
58868a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
58878a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
58888a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
58898a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58908a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58918a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
5892*76cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
58938a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
58948a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
58958a175baeSEmil Constantinescu       if(tola>0.){
58968a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
58978a175baeSEmil Constantinescu         na_loc++;
58988a175baeSEmil Constantinescu       }
58998a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59008a175baeSEmil Constantinescu       if(tolr>0.){
59018a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
59028a175baeSEmil Constantinescu         nr_loc++;
59038a175baeSEmil Constantinescu       }
59048a175baeSEmil Constantinescu       tol=tola+tolr;
59058a175baeSEmil Constantinescu       if(tol>0.){
59068a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
59078a175baeSEmil Constantinescu         n_loc++;
59088a175baeSEmil Constantinescu       }
59098a175baeSEmil Constantinescu     }
59108a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59118a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59128a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
59138a175baeSEmil Constantinescu     const PetscScalar *atol;
59148a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59158a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
5916*76cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59178a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59188a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
59198a175baeSEmil Constantinescu       if(tola>0.){
59208a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
59218a175baeSEmil Constantinescu         na_loc++;
59228a175baeSEmil Constantinescu       }
59238a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59248a175baeSEmil Constantinescu       if(tolr>0.){
59258a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
59268a175baeSEmil Constantinescu         nr_loc++;
59278a175baeSEmil Constantinescu       }
59288a175baeSEmil Constantinescu       tol=tola+tolr;
59298a175baeSEmil Constantinescu       if(tol>0.){
59308a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
59318a175baeSEmil Constantinescu         n_loc++;
59328a175baeSEmil Constantinescu       }
59338a175baeSEmil Constantinescu     }
59348a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59358a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
59368a175baeSEmil Constantinescu     const PetscScalar *rtol;
59378a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59388a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
5939*76cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59408a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59418a175baeSEmil Constantinescu       tola = ts->atol;
59428a175baeSEmil Constantinescu       if(tola>0.){
59438a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
59448a175baeSEmil Constantinescu         na_loc++;
59458a175baeSEmil Constantinescu       }
59468a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59478a175baeSEmil Constantinescu       if(tolr>0.){
59488a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
59498a175baeSEmil Constantinescu         nr_loc++;
59508a175baeSEmil Constantinescu       }
59518a175baeSEmil Constantinescu       tol=tola+tolr;
59528a175baeSEmil Constantinescu       if(tol>0.){
59538a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
59548a175baeSEmil Constantinescu         n_loc++;
59558a175baeSEmil Constantinescu       }
59568a175baeSEmil Constantinescu     }
59578a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59588a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
59598a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
5960*76cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
59618a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59628a175baeSEmil Constantinescu       tola = ts->atol;
59638a175baeSEmil Constantinescu       if(tola>0.){
59648a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
59658a175baeSEmil Constantinescu         na_loc++;
59668a175baeSEmil Constantinescu       }
59678a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59688a175baeSEmil Constantinescu       if(tolr>0.){
59698a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
59708a175baeSEmil Constantinescu         nr_loc++;
59718a175baeSEmil Constantinescu       }
59728a175baeSEmil Constantinescu       tol=tola+tolr;
59738a175baeSEmil Constantinescu       if(tol>0.){
59748a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
59758a175baeSEmil Constantinescu         n_loc++;
59768a175baeSEmil Constantinescu       }
59778a175baeSEmil Constantinescu     }
59788a175baeSEmil Constantinescu   }
59798a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
59808a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
59818a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
59828a175baeSEmil Constantinescu 
59838a175baeSEmil Constantinescu   err_loc[0] = sum;
59848a175baeSEmil Constantinescu   err_loc[1] = suma;
59858a175baeSEmil Constantinescu   err_loc[2] = sumr;
59868a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
59878a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
59888a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
59898a175baeSEmil Constantinescu 
5990a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
59918a175baeSEmil Constantinescu 
59928a175baeSEmil Constantinescu   gsum   = err_glb[0];
59938a175baeSEmil Constantinescu   gsuma  = err_glb[1];
59948a175baeSEmil Constantinescu   gsumr  = err_glb[2];
59958a175baeSEmil Constantinescu   n_glb  = err_glb[3];
59968a175baeSEmil Constantinescu   na_glb = err_glb[4];
59978a175baeSEmil Constantinescu   nr_glb = err_glb[5];
59988a175baeSEmil Constantinescu 
59998a175baeSEmil Constantinescu   *norm  = 0.;
60008a175baeSEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
60018a175baeSEmil Constantinescu   *norma = 0.;
60028a175baeSEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
60038a175baeSEmil Constantinescu   *normr = 0.;
60048a175baeSEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
60058a175baeSEmil Constantinescu 
60068a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
60078a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
60088a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
60098a175baeSEmil Constantinescu   PetscFunctionReturn(0);
60108a175baeSEmil Constantinescu }
60118a175baeSEmil Constantinescu 
60128a175baeSEmil Constantinescu /*@
60138a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
60148a175baeSEmil Constantinescu    Collective on TS
60158a175baeSEmil Constantinescu 
60168a175baeSEmil Constantinescu    Input Arguments:
60178a175baeSEmil Constantinescu +  ts - time stepping context
60188a175baeSEmil Constantinescu .  E - error vector
60198a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
60208a175baeSEmil Constantinescu -  Y - state vector, previous time step
60218a175baeSEmil Constantinescu 
60228a175baeSEmil Constantinescu    Output Arguments:
60238a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
60248a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
60258a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
60268a175baeSEmil Constantinescu 
60278a175baeSEmil Constantinescu    Level: developer
60288a175baeSEmil Constantinescu 
60298a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
60308a175baeSEmil Constantinescu @*/
60318a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
60328a175baeSEmil Constantinescu {
60338a175baeSEmil Constantinescu   PetscErrorCode    ierr;
60348a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
60358a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
60368a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
60378a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
60388a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
60398a175baeSEmil Constantinescu 
60408a175baeSEmil Constantinescu   PetscFunctionBegin;
60418a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
60428a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
60438a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
60448a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
60458a175baeSEmil Constantinescu   PetscValidType(E,2);
60468a175baeSEmil Constantinescu   PetscValidType(U,3);
60478a175baeSEmil Constantinescu   PetscValidType(Y,4);
60488a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
60498a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
60508a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
60518a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
60528a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
60538a175baeSEmil Constantinescu 
60548a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
60558a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
60568a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
60578a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
60588a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
60598a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
60608a175baeSEmil Constantinescu 
60618a175baeSEmil Constantinescu   max=0.;
60628a175baeSEmil Constantinescu   maxa=0.;
60638a175baeSEmil Constantinescu   maxr=0.;
60648a175baeSEmil Constantinescu 
60658a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
60668a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
60678a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60688a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60698a175baeSEmil Constantinescu 
60708a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
6071*76cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
60728a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
60738a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
60748a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60758a175baeSEmil Constantinescu       tol  = tola+tolr;
60768a175baeSEmil Constantinescu       if(tola>0.){
60778a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
60788a175baeSEmil Constantinescu       }
60798a175baeSEmil Constantinescu       if(tolr>0.){
60808a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
60818a175baeSEmil Constantinescu       }
60828a175baeSEmil Constantinescu       if(tol>0.){
60838a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
60848a175baeSEmil Constantinescu       }
60858a175baeSEmil Constantinescu     }
60868a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60878a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60888a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
60898a175baeSEmil Constantinescu     const PetscScalar *atol;
60908a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60918a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
6092*76cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
60938a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
60948a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
60958a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60968a175baeSEmil Constantinescu       tol  = tola+tolr;
60978a175baeSEmil Constantinescu       if(tola>0.){
60988a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
60998a175baeSEmil Constantinescu       }
61008a175baeSEmil Constantinescu       if(tolr>0.){
61018a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
61028a175baeSEmil Constantinescu       }
61038a175baeSEmil Constantinescu       if(tol>0.){
61048a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
61058a175baeSEmil Constantinescu       }
61068a175baeSEmil Constantinescu     }
61078a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
61088a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
61098a175baeSEmil Constantinescu     const PetscScalar *rtol;
61108a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61118a175baeSEmil Constantinescu 
61128a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
6113*76cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61148a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61158a175baeSEmil Constantinescu       tola = ts->atol;
61168a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61178a175baeSEmil Constantinescu       tol  = tola+tolr;
61188a175baeSEmil Constantinescu       if(tola>0.){
61198a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
61208a175baeSEmil Constantinescu       }
61218a175baeSEmil Constantinescu       if(tolr>0.){
61228a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
61238a175baeSEmil Constantinescu       }
61248a175baeSEmil Constantinescu       if(tol>0.){
61258a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
61268a175baeSEmil Constantinescu       }
61278a175baeSEmil Constantinescu     }
61288a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
61298a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
61308a175baeSEmil Constantinescu 
61318a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
6132*76cddca1SEmil Constantinescu       SkipSmallValue(y[i],u[i],ts->adapt->ignore_max);
61338a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
61348a175baeSEmil Constantinescu       tola = ts->atol;
61358a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
61368a175baeSEmil Constantinescu       tol  = tola+tolr;
61378a175baeSEmil Constantinescu       if(tola>0.){
61388a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
61398a175baeSEmil Constantinescu       }
61408a175baeSEmil Constantinescu       if(tolr>0.){
61418a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
61428a175baeSEmil Constantinescu       }
61438a175baeSEmil Constantinescu       if(tol>0.){
61448a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
61458a175baeSEmil Constantinescu       }
61468a175baeSEmil Constantinescu     }
61478a175baeSEmil Constantinescu   }
61488a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
61498a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
61508a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
61518a175baeSEmil Constantinescu   err_loc[0] = max;
61528a175baeSEmil Constantinescu   err_loc[1] = maxa;
61538a175baeSEmil Constantinescu   err_loc[2] = maxr;
6154a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
61558a175baeSEmil Constantinescu   gmax   = err_glb[0];
61568a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
61578a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
61588a175baeSEmil Constantinescu 
61598a175baeSEmil Constantinescu   *norm = gmax;
61608a175baeSEmil Constantinescu   *norma = gmaxa;
61618a175baeSEmil Constantinescu   *normr = gmaxr;
61628a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
61638a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
61648a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
61658a175baeSEmil Constantinescu   PetscFunctionReturn(0);
61668a175baeSEmil Constantinescu }
61678a175baeSEmil Constantinescu 
61688a175baeSEmil Constantinescu /*@
61698a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
61708a175baeSEmil Constantinescu 
61718a175baeSEmil Constantinescu    Collective on TS
61728a175baeSEmil Constantinescu 
61738a175baeSEmil Constantinescu    Input Arguments:
61748a175baeSEmil Constantinescu +  ts - time stepping context
61758a175baeSEmil Constantinescu .  E - error vector
61768a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
61778a175baeSEmil Constantinescu .  Y - state vector, previous time step
61788a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
61798a175baeSEmil Constantinescu 
61808a175baeSEmil Constantinescu    Output Arguments:
61818a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
61828a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
61838a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
61848a175baeSEmil Constantinescu 
61858a175baeSEmil Constantinescu    Options Database Keys:
61868a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
61878a175baeSEmil Constantinescu 
61888a175baeSEmil Constantinescu    Level: developer
61898a175baeSEmil Constantinescu 
61908a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
61918a175baeSEmil Constantinescu @*/
61928a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
61938a175baeSEmil Constantinescu {
61948a175baeSEmil Constantinescu   PetscErrorCode ierr;
61958a175baeSEmil Constantinescu 
61968a175baeSEmil Constantinescu   PetscFunctionBegin;
61978a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
61988a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
61998a175baeSEmil Constantinescu   } else if(wnormtype == NORM_INFINITY) {
62008a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
62018a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
62028a175baeSEmil Constantinescu   PetscFunctionReturn(0);
62038a175baeSEmil Constantinescu }
62048a175baeSEmil Constantinescu 
62058a175baeSEmil Constantinescu 
62068d59e960SJed Brown /*@
62078d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
62088d59e960SJed Brown 
62098d59e960SJed Brown    Logically Collective on TS
62108d59e960SJed Brown 
62118d59e960SJed Brown    Input Arguments:
62128d59e960SJed Brown +  ts - time stepping context
62138d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
62148d59e960SJed Brown 
62158d59e960SJed Brown    Note:
62168d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
62178d59e960SJed Brown 
62188d59e960SJed Brown    Level: intermediate
62198d59e960SJed Brown 
62208d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
62218d59e960SJed Brown @*/
62228d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
62238d59e960SJed Brown {
62248d59e960SJed Brown   PetscFunctionBegin;
62258d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
62268d59e960SJed Brown   ts->cfltime_local = cfltime;
62278d59e960SJed Brown   ts->cfltime       = -1.;
62288d59e960SJed Brown   PetscFunctionReturn(0);
62298d59e960SJed Brown }
62308d59e960SJed Brown 
62318d59e960SJed Brown /*@
62328d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
62338d59e960SJed Brown 
62348d59e960SJed Brown    Collective on TS
62358d59e960SJed Brown 
62368d59e960SJed Brown    Input Arguments:
62378d59e960SJed Brown .  ts - time stepping context
62388d59e960SJed Brown 
62398d59e960SJed Brown    Output Arguments:
62408d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
62418d59e960SJed Brown 
62428d59e960SJed Brown    Level: advanced
62438d59e960SJed Brown 
62448d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
62458d59e960SJed Brown @*/
62468d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
62478d59e960SJed Brown {
62488d59e960SJed Brown   PetscErrorCode ierr;
62498d59e960SJed Brown 
62508d59e960SJed Brown   PetscFunctionBegin;
62518d59e960SJed Brown   if (ts->cfltime < 0) {
6252b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
62538d59e960SJed Brown   }
62548d59e960SJed Brown   *cfltime = ts->cfltime;
62558d59e960SJed Brown   PetscFunctionReturn(0);
62568d59e960SJed Brown }
62578d59e960SJed Brown 
6258d6ebe24aSShri Abhyankar /*@
6259d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6260d6ebe24aSShri Abhyankar 
6261d6ebe24aSShri Abhyankar    Input Parameters:
6262d6ebe24aSShri Abhyankar .  ts   - the TS context.
6263d6ebe24aSShri Abhyankar .  xl   - lower bound.
6264d6ebe24aSShri Abhyankar .  xu   - upper bound.
6265d6ebe24aSShri Abhyankar 
6266d6ebe24aSShri Abhyankar    Notes:
6267d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6268e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6269d6ebe24aSShri Abhyankar 
62702bd2b0e6SSatish Balay    Level: advanced
62712bd2b0e6SSatish Balay 
6272d6ebe24aSShri Abhyankar @*/
6273d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6274d6ebe24aSShri Abhyankar {
6275d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6276d6ebe24aSShri Abhyankar   SNES           snes;
6277d6ebe24aSShri Abhyankar 
6278d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6279d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6280d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6281d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6282d6ebe24aSShri Abhyankar }
6283d6ebe24aSShri Abhyankar 
6284325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
6285c6db04a5SJed Brown #include <mex.h>
6286325fc9f4SBarry Smith 
6287325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
6288325fc9f4SBarry Smith 
6289325fc9f4SBarry Smith /*
6290325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
6291325fc9f4SBarry Smith                          TSSetFunctionMatlab().
6292325fc9f4SBarry Smith 
6293325fc9f4SBarry Smith    Collective on TS
6294325fc9f4SBarry Smith 
6295325fc9f4SBarry Smith    Input Parameters:
6296325fc9f4SBarry Smith +  snes - the TS context
62970910c330SBarry Smith -  u - input vector
6298325fc9f4SBarry Smith 
6299325fc9f4SBarry Smith    Output Parameter:
6300325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
6301325fc9f4SBarry Smith 
6302325fc9f4SBarry Smith    Notes:
6303325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
6304325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
6305325fc9f4SBarry Smith    themselves.
6306325fc9f4SBarry Smith 
6307325fc9f4SBarry Smith    Level: developer
6308325fc9f4SBarry Smith 
6309325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6310325fc9f4SBarry Smith 
6311325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6312325fc9f4SBarry Smith */
63130910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
6314325fc9f4SBarry Smith {
6315325fc9f4SBarry Smith   PetscErrorCode  ierr;
6316325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6317325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
6318325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
6319325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
6320325fc9f4SBarry Smith 
6321325fc9f4SBarry Smith   PetscFunctionBegin;
6322325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
63230910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
63240910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
6325325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
63260910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
6327325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
6328325fc9f4SBarry Smith 
6329325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
63300910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
63310910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
63320910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
6333bbd56ea5SKarl Rupp 
6334325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6335325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
6336325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6337325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6338325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
6339325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
6340325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
6341325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
6342325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6343325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6344325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6345325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6346325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6347325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6348325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6349325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6350325fc9f4SBarry Smith   PetscFunctionReturn(0);
6351325fc9f4SBarry Smith }
6352325fc9f4SBarry Smith 
6353325fc9f4SBarry Smith /*
6354325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
6355325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
6356e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
6357325fc9f4SBarry Smith 
6358325fc9f4SBarry Smith    Logically Collective on TS
6359325fc9f4SBarry Smith 
6360325fc9f4SBarry Smith    Input Parameters:
6361325fc9f4SBarry Smith +  ts - the TS context
6362325fc9f4SBarry Smith -  func - function evaluation routine
6363325fc9f4SBarry Smith 
6364325fc9f4SBarry Smith    Calling sequence of func:
63650910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
6366325fc9f4SBarry Smith 
6367325fc9f4SBarry Smith    Level: beginner
6368325fc9f4SBarry Smith 
6369325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6370325fc9f4SBarry Smith 
6371325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6372325fc9f4SBarry Smith */
6373cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
6374325fc9f4SBarry Smith {
6375325fc9f4SBarry Smith   PetscErrorCode  ierr;
6376325fc9f4SBarry Smith   TSMatlabContext *sctx;
6377325fc9f4SBarry Smith 
6378325fc9f4SBarry Smith   PetscFunctionBegin;
6379325fc9f4SBarry Smith   /* currently sctx is memory bleed */
638095dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6381325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6382325fc9f4SBarry Smith   /*
6383325fc9f4SBarry Smith      This should work, but it doesn't
6384325fc9f4SBarry Smith   sctx->ctx = ctx;
6385325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6386325fc9f4SBarry Smith   */
6387325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6388bbd56ea5SKarl Rupp 
63890298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
6390325fc9f4SBarry Smith   PetscFunctionReturn(0);
6391325fc9f4SBarry Smith }
6392325fc9f4SBarry Smith 
6393325fc9f4SBarry Smith /*
6394325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
6395325fc9f4SBarry Smith                          TSSetJacobianMatlab().
6396325fc9f4SBarry Smith 
6397325fc9f4SBarry Smith    Collective on TS
6398325fc9f4SBarry Smith 
6399325fc9f4SBarry Smith    Input Parameters:
6400cdcf91faSSean Farley +  ts - the TS context
64010910c330SBarry Smith .  u - input vector
6402325fc9f4SBarry Smith .  A, B - the matrices
6403325fc9f4SBarry Smith -  ctx - user context
6404325fc9f4SBarry Smith 
6405325fc9f4SBarry Smith    Level: developer
6406325fc9f4SBarry Smith 
6407325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6408325fc9f4SBarry Smith 
6409325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6410325fc9f4SBarry Smith @*/
6411f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
6412325fc9f4SBarry Smith {
6413325fc9f4SBarry Smith   PetscErrorCode  ierr;
6414325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6415325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
6416325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
6417325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
6418325fc9f4SBarry Smith 
6419325fc9f4SBarry Smith   PetscFunctionBegin;
6420cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
64210910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
6422325fc9f4SBarry Smith 
64230910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
6424325fc9f4SBarry Smith 
6425cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
64260910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
64270910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
64280910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
64290910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
6430bbd56ea5SKarl Rupp 
6431325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6432325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
6433325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6434325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6435325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
6436325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
6437325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
6438325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
6439325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
6440325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
6441325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6442325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6443325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6444325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6445325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6446325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6447325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6448325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
6449325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
6450325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6451325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
6452325fc9f4SBarry Smith   PetscFunctionReturn(0);
6453325fc9f4SBarry Smith }
6454325fc9f4SBarry Smith 
6455325fc9f4SBarry Smith /*
6456325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
6457e3c5b3baSBarry 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
6458325fc9f4SBarry Smith 
6459325fc9f4SBarry Smith    Logically Collective on TS
6460325fc9f4SBarry Smith 
6461325fc9f4SBarry Smith    Input Parameters:
6462cdcf91faSSean Farley +  ts - the TS context
6463325fc9f4SBarry Smith .  A,B - Jacobian matrices
6464325fc9f4SBarry Smith .  func - function evaluation routine
6465325fc9f4SBarry Smith -  ctx - user context
6466325fc9f4SBarry Smith 
6467325fc9f4SBarry Smith    Calling sequence of func:
64680910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
6469325fc9f4SBarry Smith 
6470325fc9f4SBarry Smith    Level: developer
6471325fc9f4SBarry Smith 
6472325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6473325fc9f4SBarry Smith 
6474325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6475325fc9f4SBarry Smith */
6476cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
6477325fc9f4SBarry Smith {
6478325fc9f4SBarry Smith   PetscErrorCode  ierr;
6479325fc9f4SBarry Smith   TSMatlabContext *sctx;
6480325fc9f4SBarry Smith 
6481325fc9f4SBarry Smith   PetscFunctionBegin;
6482325fc9f4SBarry Smith   /* currently sctx is memory bleed */
648395dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6484325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6485325fc9f4SBarry Smith   /*
6486325fc9f4SBarry Smith      This should work, but it doesn't
6487325fc9f4SBarry Smith   sctx->ctx = ctx;
6488325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6489325fc9f4SBarry Smith   */
6490325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6491bbd56ea5SKarl Rupp 
6492cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
6493325fc9f4SBarry Smith   PetscFunctionReturn(0);
6494325fc9f4SBarry Smith }
6495325fc9f4SBarry Smith 
6496b5b1a830SBarry Smith /*
6497b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
6498b5b1a830SBarry Smith 
6499b5b1a830SBarry Smith    Collective on TS
6500b5b1a830SBarry Smith 
6501b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6502b5b1a830SBarry Smith @*/
65030910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
6504b5b1a830SBarry Smith {
6505b5b1a830SBarry Smith   PetscErrorCode  ierr;
6506b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6507a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
6508b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
6509b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
6510b5b1a830SBarry Smith 
6511b5b1a830SBarry Smith   PetscFunctionBegin;
6512cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
65130910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
6514b5b1a830SBarry Smith 
6515cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
65160910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
6517bbd56ea5SKarl Rupp 
6518b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6519b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
6520b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
6521b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
6522b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
6523b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
6524b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
6525b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6526b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
6527b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
6528b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
6529b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
6530b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
6531b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
6532b5b1a830SBarry Smith   PetscFunctionReturn(0);
6533b5b1a830SBarry Smith }
6534b5b1a830SBarry Smith 
6535b5b1a830SBarry Smith /*
6536b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
6537b5b1a830SBarry Smith 
6538b5b1a830SBarry Smith    Level: developer
6539b5b1a830SBarry Smith 
6540b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
6541b5b1a830SBarry Smith 
6542b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6543b5b1a830SBarry Smith */
6544cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
6545b5b1a830SBarry Smith {
6546b5b1a830SBarry Smith   PetscErrorCode  ierr;
6547b5b1a830SBarry Smith   TSMatlabContext *sctx;
6548b5b1a830SBarry Smith 
6549b5b1a830SBarry Smith   PetscFunctionBegin;
6550b5b1a830SBarry Smith   /* currently sctx is memory bleed */
655195dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6552b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6553b5b1a830SBarry Smith   /*
6554b5b1a830SBarry Smith      This should work, but it doesn't
6555b5b1a830SBarry Smith   sctx->ctx = ctx;
6556b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6557b5b1a830SBarry Smith   */
6558b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6559bbd56ea5SKarl Rupp 
65600298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
6561b5b1a830SBarry Smith   PetscFunctionReturn(0);
6562b5b1a830SBarry Smith }
6563325fc9f4SBarry Smith #endif
6564b3603a34SBarry Smith 
6565b3603a34SBarry Smith /*@C
65664f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6567b3603a34SBarry Smith        in a time based line graph
6568b3603a34SBarry Smith 
6569b3603a34SBarry Smith    Collective on TS
6570b3603a34SBarry Smith 
6571b3603a34SBarry Smith    Input Parameters:
6572b3603a34SBarry Smith +  ts - the TS context
6573b3603a34SBarry Smith .  step - current time-step
6574b3603a34SBarry Smith .  ptime - current time
65757db568b7SBarry Smith .  u - current solution
65767db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6577b3603a34SBarry Smith 
6578b3d3934dSBarry Smith    Options Database:
65799ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6580b3d3934dSBarry Smith 
6581b3603a34SBarry Smith    Level: intermediate
6582b3603a34SBarry Smith 
658395452b02SPatrick Sanan    Notes:
658495452b02SPatrick Sanan     Each process in a parallel run displays its component solutions in a separate window
6585b3603a34SBarry Smith 
6586b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
6587b3603a34SBarry Smith 
65887db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
65897db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
65907db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
65917db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6592b3603a34SBarry Smith @*/
65937db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6594b3603a34SBarry Smith {
6595b3603a34SBarry Smith   PetscErrorCode    ierr;
65967db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6597b3603a34SBarry Smith   const PetscScalar *yy;
659880666b62SBarry Smith   Vec               v;
6599b3603a34SBarry Smith 
6600b3603a34SBarry Smith   PetscFunctionBegin;
660163e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
660258ff32f7SBarry Smith   if (!step) {
6603a9f9c1f6SBarry Smith     PetscDrawAxis axis;
66046934998bSLisandro Dalcin     PetscInt      dim;
6605a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
66060ec8ee2bSJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6607bab0a581SBarry Smith     if (!ctx->names) {
6608bab0a581SBarry Smith       PetscBool flg;
6609bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6610bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6611bab0a581SBarry Smith       if (flg) {
6612bab0a581SBarry Smith         PetscInt i,n;
6613bab0a581SBarry Smith         char     **names;
6614bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6615bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6616bab0a581SBarry Smith         for (i=0; i<n; i++) {
6617bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6618bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6619bab0a581SBarry Smith         }
6620bab0a581SBarry Smith         names[n] = NULL;
6621bab0a581SBarry Smith         ctx->names = names;
6622bab0a581SBarry Smith       }
6623bab0a581SBarry Smith     }
6624387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6625387f4636SBarry Smith       char      **displaynames;
6626387f4636SBarry Smith       PetscBool flg;
6627387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
662895dccacaSBarry Smith       ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6629387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
6630c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6631387f4636SBarry Smith       if (flg) {
6632a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6633387f4636SBarry Smith       }
6634387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6635387f4636SBarry Smith     }
6636387f4636SBarry Smith     if (ctx->displaynames) {
6637387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6638387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6639387f4636SBarry Smith     } else if (ctx->names) {
66400910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
66410b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6642387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6643b0bc92c6SBarry Smith     } else {
6644b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6645b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6646387f4636SBarry Smith     }
66470b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
664858ff32f7SBarry Smith   }
66496934998bSLisandro Dalcin 
66506934998bSLisandro Dalcin   if (!ctx->transform) v = u;
66516934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
665280666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
66536934998bSLisandro Dalcin   if (ctx->displaynames) {
66546934998bSLisandro Dalcin     PetscInt i;
66556934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
66566934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
66576934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
66586934998bSLisandro Dalcin   } else {
6659e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6660e3efe391SJed Brown     PetscInt  i,n;
66616934998bSLisandro Dalcin     PetscReal *yreal;
666280666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6663785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6664e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
66650b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6666e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6667e3efe391SJed Brown #else
66680b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6669e3efe391SJed Brown #endif
667080666b62SBarry Smith   }
66716934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
66726934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
66736934998bSLisandro Dalcin 
6674b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
66750b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
66766934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
66773923b477SBarry Smith   }
6678b3603a34SBarry Smith   PetscFunctionReturn(0);
6679b3603a34SBarry Smith }
6680b3603a34SBarry Smith 
6681b037adc7SBarry Smith /*@C
668231152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6683b037adc7SBarry Smith 
6684b037adc7SBarry Smith    Collective on TS
6685b037adc7SBarry Smith 
6686b037adc7SBarry Smith    Input Parameters:
6687b037adc7SBarry Smith +  ts - the TS context
6688b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6689b037adc7SBarry Smith 
6690b037adc7SBarry Smith    Level: intermediate
6691b037adc7SBarry Smith 
669295452b02SPatrick Sanan    Notes:
669395452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66947db568b7SBarry Smith 
6695b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
6696b037adc7SBarry Smith 
6697a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6698b037adc7SBarry Smith @*/
669931152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6700b037adc7SBarry Smith {
6701b037adc7SBarry Smith   PetscErrorCode    ierr;
6702b037adc7SBarry Smith   PetscInt          i;
6703b037adc7SBarry Smith 
6704b037adc7SBarry Smith   PetscFunctionBegin;
6705b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6706b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67075537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6708b3d3934dSBarry Smith       break;
6709b3d3934dSBarry Smith     }
6710b3d3934dSBarry Smith   }
6711b3d3934dSBarry Smith   PetscFunctionReturn(0);
6712b3d3934dSBarry Smith }
6713b3d3934dSBarry Smith 
6714e673d494SBarry Smith /*@C
6715e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6716e673d494SBarry Smith 
6717e673d494SBarry Smith    Collective on TS
6718e673d494SBarry Smith 
6719e673d494SBarry Smith    Input Parameters:
6720e673d494SBarry Smith +  ts - the TS context
6721e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6722e673d494SBarry Smith 
6723e673d494SBarry Smith    Level: intermediate
6724e673d494SBarry Smith 
6725e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6726e673d494SBarry Smith 
6727a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6728e673d494SBarry Smith @*/
6729e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6730e673d494SBarry Smith {
6731e673d494SBarry Smith   PetscErrorCode    ierr;
6732e673d494SBarry Smith 
6733e673d494SBarry Smith   PetscFunctionBegin;
6734e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6735e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6736e673d494SBarry Smith   PetscFunctionReturn(0);
6737e673d494SBarry Smith }
6738e673d494SBarry Smith 
6739b3d3934dSBarry Smith /*@C
6740b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6741b3d3934dSBarry Smith 
6742b3d3934dSBarry Smith    Collective on TS
6743b3d3934dSBarry Smith 
6744b3d3934dSBarry Smith    Input Parameter:
6745b3d3934dSBarry Smith .  ts - the TS context
6746b3d3934dSBarry Smith 
6747b3d3934dSBarry Smith    Output Parameter:
6748b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6749b3d3934dSBarry Smith 
6750b3d3934dSBarry Smith    Level: intermediate
6751b3d3934dSBarry Smith 
675295452b02SPatrick Sanan    Notes:
675395452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67547db568b7SBarry Smith 
6755b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6756b3d3934dSBarry Smith 
6757b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6758b3d3934dSBarry Smith @*/
6759b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6760b3d3934dSBarry Smith {
6761b3d3934dSBarry Smith   PetscInt       i;
6762b3d3934dSBarry Smith 
6763b3d3934dSBarry Smith   PetscFunctionBegin;
6764b3d3934dSBarry Smith   *names = NULL;
6765b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6766b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67675537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6768b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6769b3d3934dSBarry Smith       break;
6770387f4636SBarry Smith     }
6771387f4636SBarry Smith   }
6772387f4636SBarry Smith   PetscFunctionReturn(0);
6773387f4636SBarry Smith }
6774387f4636SBarry Smith 
6775a66092f1SBarry Smith /*@C
6776a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6777a66092f1SBarry Smith 
6778a66092f1SBarry Smith    Collective on TS
6779a66092f1SBarry Smith 
6780a66092f1SBarry Smith    Input Parameters:
6781a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6782a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
6783a66092f1SBarry Smith 
6784a66092f1SBarry Smith    Level: intermediate
6785a66092f1SBarry Smith 
6786a66092f1SBarry Smith .keywords: TS,  vector, monitor, view
6787a66092f1SBarry Smith 
6788a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6789a66092f1SBarry Smith @*/
6790a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6791a66092f1SBarry Smith {
6792a66092f1SBarry Smith   PetscInt          j = 0,k;
6793a66092f1SBarry Smith   PetscErrorCode    ierr;
6794a66092f1SBarry Smith 
6795a66092f1SBarry Smith   PetscFunctionBegin;
6796a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6797a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6798a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6799a66092f1SBarry Smith   while (displaynames[j]) j++;
6800a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6801a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6802a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6803a66092f1SBarry Smith   j = 0;
6804a66092f1SBarry Smith   while (displaynames[j]) {
6805a66092f1SBarry Smith     k = 0;
6806a66092f1SBarry Smith     while (ctx->names[k]) {
6807a66092f1SBarry Smith       PetscBool flg;
6808a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6809a66092f1SBarry Smith       if (flg) {
6810a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6811a66092f1SBarry Smith         break;
6812a66092f1SBarry Smith       }
6813a66092f1SBarry Smith       k++;
6814a66092f1SBarry Smith     }
6815a66092f1SBarry Smith     j++;
6816a66092f1SBarry Smith   }
6817a66092f1SBarry Smith   PetscFunctionReturn(0);
6818a66092f1SBarry Smith }
6819a66092f1SBarry Smith 
6820387f4636SBarry Smith /*@C
6821387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6822387f4636SBarry Smith 
6823387f4636SBarry Smith    Collective on TS
6824387f4636SBarry Smith 
6825387f4636SBarry Smith    Input Parameters:
6826387f4636SBarry Smith +  ts - the TS context
6827387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
6828387f4636SBarry Smith 
682995452b02SPatrick Sanan    Notes:
683095452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
68317db568b7SBarry Smith 
6832387f4636SBarry Smith    Level: intermediate
6833387f4636SBarry Smith 
6834387f4636SBarry Smith .keywords: TS,  vector, monitor, view
6835387f4636SBarry Smith 
6836387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6837387f4636SBarry Smith @*/
6838387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6839387f4636SBarry Smith {
6840a66092f1SBarry Smith   PetscInt          i;
6841387f4636SBarry Smith   PetscErrorCode    ierr;
6842387f4636SBarry Smith 
6843387f4636SBarry Smith   PetscFunctionBegin;
6844387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6845387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
68465537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6847b3d3934dSBarry Smith       break;
6848b037adc7SBarry Smith     }
6849b037adc7SBarry Smith   }
6850b037adc7SBarry Smith   PetscFunctionReturn(0);
6851b037adc7SBarry Smith }
6852b037adc7SBarry Smith 
685380666b62SBarry Smith /*@C
685480666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
685580666b62SBarry Smith 
685680666b62SBarry Smith    Collective on TS
685780666b62SBarry Smith 
685880666b62SBarry Smith    Input Parameters:
685980666b62SBarry Smith +  ts - the TS context
686080666b62SBarry Smith .  transform - the transform function
68617684fa3eSBarry Smith .  destroy - function to destroy the optional context
686280666b62SBarry Smith -  ctx - optional context used by transform function
686380666b62SBarry Smith 
686495452b02SPatrick Sanan    Notes:
686595452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
68667db568b7SBarry Smith 
686780666b62SBarry Smith    Level: intermediate
686880666b62SBarry Smith 
686980666b62SBarry Smith .keywords: TS,  vector, monitor, view
687080666b62SBarry Smith 
6871a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
687280666b62SBarry Smith @*/
68737684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
687480666b62SBarry Smith {
687580666b62SBarry Smith   PetscInt          i;
6876a66092f1SBarry Smith   PetscErrorCode    ierr;
687780666b62SBarry Smith 
687880666b62SBarry Smith   PetscFunctionBegin;
687980666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
688080666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
68815537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
688280666b62SBarry Smith     }
688380666b62SBarry Smith   }
688480666b62SBarry Smith   PetscFunctionReturn(0);
688580666b62SBarry Smith }
688680666b62SBarry Smith 
6887e673d494SBarry Smith /*@C
6888e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6889e673d494SBarry Smith 
6890e673d494SBarry Smith    Collective on TSLGCtx
6891e673d494SBarry Smith 
6892e673d494SBarry Smith    Input Parameters:
6893e673d494SBarry Smith +  ts - the TS context
6894e673d494SBarry Smith .  transform - the transform function
68957684fa3eSBarry Smith .  destroy - function to destroy the optional context
6896e673d494SBarry Smith -  ctx - optional context used by transform function
6897e673d494SBarry Smith 
6898e673d494SBarry Smith    Level: intermediate
6899e673d494SBarry Smith 
6900e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6901e673d494SBarry Smith 
6902a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6903e673d494SBarry Smith @*/
69047684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6905e673d494SBarry Smith {
6906e673d494SBarry Smith   PetscFunctionBegin;
6907e673d494SBarry Smith   ctx->transform    = transform;
69087684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6909e673d494SBarry Smith   ctx->transformctx = tctx;
6910e673d494SBarry Smith   PetscFunctionReturn(0);
6911e673d494SBarry Smith }
6912e673d494SBarry Smith 
6913ef20d060SBarry Smith /*@C
69148b668821SLisandro Dalcin    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error
6915ef20d060SBarry Smith        in a time based line graph
6916ef20d060SBarry Smith 
6917ef20d060SBarry Smith    Collective on TS
6918ef20d060SBarry Smith 
6919ef20d060SBarry Smith    Input Parameters:
6920ef20d060SBarry Smith +  ts - the TS context
6921ef20d060SBarry Smith .  step - current time-step
6922ef20d060SBarry Smith .  ptime - current time
69237db568b7SBarry Smith .  u - current solution
69247db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6925ef20d060SBarry Smith 
6926ef20d060SBarry Smith    Level: intermediate
6927ef20d060SBarry Smith 
692895452b02SPatrick Sanan    Notes:
692995452b02SPatrick Sanan     Each process in a parallel run displays its component errors in a separate window
6930abd5a294SJed Brown 
6931abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6932abd5a294SJed Brown 
6933abd5a294SJed Brown    Options Database Keys:
69344f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6935ef20d060SBarry Smith 
6936ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
6937ef20d060SBarry Smith 
6938abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6939ef20d060SBarry Smith @*/
69400910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6941ef20d060SBarry Smith {
6942ef20d060SBarry Smith   PetscErrorCode    ierr;
69430b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6944ef20d060SBarry Smith   const PetscScalar *yy;
6945ef20d060SBarry Smith   Vec               y;
6946ef20d060SBarry Smith 
6947ef20d060SBarry Smith   PetscFunctionBegin;
6948a9f9c1f6SBarry Smith   if (!step) {
6949a9f9c1f6SBarry Smith     PetscDrawAxis axis;
69506934998bSLisandro Dalcin     PetscInt      dim;
6951a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
69528b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr);
69530910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6954a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6955a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6956a9f9c1f6SBarry Smith   }
69570910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6958ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
69590910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6960ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6961e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6962e3efe391SJed Brown   {
6963e3efe391SJed Brown     PetscReal *yreal;
6964e3efe391SJed Brown     PetscInt  i,n;
6965e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6966785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6967e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
69680b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6969e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6970e3efe391SJed Brown   }
6971e3efe391SJed Brown #else
69720b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6973e3efe391SJed Brown #endif
6974ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6975ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6976b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
69770b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69786934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
69793923b477SBarry Smith   }
6980ef20d060SBarry Smith   PetscFunctionReturn(0);
6981ef20d060SBarry Smith }
6982ef20d060SBarry Smith 
69835e3b7effSJoseph Pusztay /*@C
69845e3b7effSJoseph Pusztay    TSMonitorSPSwarmSolution - Graphically displays phase plots of DMSwarm particles on a scatter plot
69855e3b7effSJoseph Pusztay 
69865e3b7effSJoseph Pusztay    Input Parameters:
69875e3b7effSJoseph Pusztay +  ts - the TS context
69885e3b7effSJoseph Pusztay .  step - current time-step
69895e3b7effSJoseph Pusztay .  ptime - current time
69905e3b7effSJoseph Pusztay .  u - current solution
69915e3b7effSJoseph Pusztay -  dctx - the TSMonitorSPCtx object that contains all the options for the monitoring, this is created with TSMonitorSPCtxCreate()
69925e3b7effSJoseph Pusztay 
69935e3b7effSJoseph Pusztay    Options Database:
6994918b1d10SJoseph Pusztay .   -ts_monitor_sp_swarm
69955e3b7effSJoseph Pusztay 
69965e3b7effSJoseph Pusztay    Level: intermediate
69975e3b7effSJoseph Pusztay 
69985e3b7effSJoseph Pusztay .keywords: TS,  vector, monitor, view, swarm
69995e3b7effSJoseph Pusztay @*/
70000ec8ee2bSJoseph Pusztay PetscErrorCode TSMonitorSPSwarmSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
70011b575b74SJoseph Pusztay {
70021b575b74SJoseph Pusztay   PetscErrorCode    ierr;
70031b575b74SJoseph Pusztay   TSMonitorSPCtx    ctx = (TSMonitorSPCtx)dctx;
70041b575b74SJoseph Pusztay   const PetscScalar *yy;
7005b1670a61SJoseph Pusztay   PetscReal       *y,*x;
70061b575b74SJoseph Pusztay   PetscInt          Np, p, dim=2;
70071b575b74SJoseph Pusztay   DM                dm;
70081b575b74SJoseph Pusztay 
70091b575b74SJoseph Pusztay   PetscFunctionBegin;
70101b575b74SJoseph Pusztay 
70111b575b74SJoseph Pusztay   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
70121b575b74SJoseph Pusztay   if (!step) {
70131b575b74SJoseph Pusztay     PetscDrawAxis axis;
70141b575b74SJoseph Pusztay     ierr = PetscDrawSPGetAxis(ctx->sp,&axis);CHKERRQ(ierr);
70151b575b74SJoseph Pusztay     ierr = PetscDrawAxisSetLabels(axis,"Particles","X","Y");CHKERRQ(ierr);
7016895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetLimits(axis, -5, 5, -5, 5);CHKERRQ(ierr);
7017895f37d5SJoseph Pusztay     ierr = PetscDrawAxisSetHoldLimits(axis, PETSC_TRUE);CHKERRQ(ierr);
70181b575b74SJoseph Pusztay     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
70191b575b74SJoseph Pusztay     ierr = DMGetDimension(dm, &dim);
70201b575b74SJoseph Pusztay     if(dim!=2) SETERRQ(PETSC_COMM_SELF, ierr, "Dimensions improper for monitor arguments! Current support: two dimensions.");CHKERRQ(ierr);
70211b575b74SJoseph Pusztay     ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
70221b575b74SJoseph Pusztay     Np /= 2*dim;
70231b575b74SJoseph Pusztay     ierr = PetscDrawSPSetDimension(ctx->sp, Np);CHKERRQ(ierr);
70241b575b74SJoseph Pusztay     ierr = PetscDrawSPReset(ctx->sp);CHKERRQ(ierr);
70251b575b74SJoseph Pusztay   }
70261b575b74SJoseph Pusztay 
70271b575b74SJoseph Pusztay   ierr = VecGetLocalSize(u, &Np);CHKERRQ(ierr);
70281b575b74SJoseph Pusztay   Np /= 2*dim;
70291b575b74SJoseph Pusztay   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
7030895f37d5SJoseph Pusztay   ierr = PetscMalloc2(Np, &x, Np, &y);CHKERRQ(ierr);
70311b575b74SJoseph Pusztay   /* get points from solution vector */
70321b575b74SJoseph Pusztay   for (p=0; p<Np; ++p){
7033b1670a61SJoseph Pusztay     x[p] = PetscRealPart(yy[2*dim*p]);
7034b1670a61SJoseph Pusztay     y[p] = PetscRealPart(yy[2*dim*p+1]);
7035895f37d5SJoseph Pusztay   }
70361b575b74SJoseph Pusztay   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
70371b575b74SJoseph Pusztay 
70381b575b74SJoseph Pusztay   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
70391b575b74SJoseph Pusztay     ierr = PetscDrawSPAddPoint(ctx->sp,x,y);CHKERRQ(ierr);
70401b575b74SJoseph Pusztay     ierr = PetscDrawSPDraw(ctx->sp,PETSC_FALSE);CHKERRQ(ierr);
70411b575b74SJoseph Pusztay     ierr = PetscDrawSPSave(ctx->sp);CHKERRQ(ierr);
70421b575b74SJoseph Pusztay   }
70431b575b74SJoseph Pusztay 
7044918b1d10SJoseph Pusztay   ierr = PetscFree2(x, y);CHKERRQ(ierr);
7045918b1d10SJoseph Pusztay 
70461b575b74SJoseph Pusztay   PetscFunctionReturn(0);
70471b575b74SJoseph Pusztay }
70481b575b74SJoseph Pusztay 
70491b575b74SJoseph Pusztay 
70501b575b74SJoseph Pusztay 
70517cf37e64SBarry Smith /*@C
70527cf37e64SBarry Smith    TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep
70537cf37e64SBarry Smith 
70547cf37e64SBarry Smith    Collective on TS
70557cf37e64SBarry Smith 
70567cf37e64SBarry Smith    Input Parameters:
70577cf37e64SBarry Smith +  ts - the TS context
70587cf37e64SBarry Smith .  step - current time-step
70597cf37e64SBarry Smith .  ptime - current time
70607cf37e64SBarry Smith .  u - current solution
70617cf37e64SBarry Smith -  dctx - unused context
70627cf37e64SBarry Smith 
70637cf37e64SBarry Smith    Level: intermediate
70647cf37e64SBarry Smith 
70657cf37e64SBarry Smith    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
70667cf37e64SBarry Smith 
70677cf37e64SBarry Smith    Options Database Keys:
70687cf37e64SBarry Smith .  -ts_monitor_error - create a graphical monitor of error history
70697cf37e64SBarry Smith 
70707cf37e64SBarry Smith .keywords: TS,  vector, monitor, view
70717cf37e64SBarry Smith 
70727cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
70737cf37e64SBarry Smith @*/
7074edbaebb3SBarry Smith PetscErrorCode  TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
70757cf37e64SBarry Smith {
70767cf37e64SBarry Smith   PetscErrorCode    ierr;
70777cf37e64SBarry Smith   Vec               y;
70787cf37e64SBarry Smith   PetscReal         nrm;
7079edbaebb3SBarry Smith   PetscBool         flg;
70807cf37e64SBarry Smith 
70817cf37e64SBarry Smith   PetscFunctionBegin;
70827cf37e64SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
70837cf37e64SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
70847cf37e64SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
7085edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr);
7086edbaebb3SBarry Smith   if (flg) {
70877cf37e64SBarry Smith     ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
7088edbaebb3SBarry Smith     ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr);
7089edbaebb3SBarry Smith   }
7090edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr);
7091edbaebb3SBarry Smith   if (flg) {
7092edbaebb3SBarry Smith     ierr = VecView(y,vf->viewer);CHKERRQ(ierr);
7093edbaebb3SBarry Smith   }
7094edbaebb3SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
70957cf37e64SBarry Smith   PetscFunctionReturn(0);
70967cf37e64SBarry Smith }
70977cf37e64SBarry Smith 
7098201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7099201da799SBarry Smith {
7100201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7101201da799SBarry Smith   PetscReal      x   = ptime,y;
7102201da799SBarry Smith   PetscErrorCode ierr;
7103201da799SBarry Smith   PetscInt       its;
7104201da799SBarry Smith 
7105201da799SBarry Smith   PetscFunctionBegin;
710663e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7107201da799SBarry Smith   if (!n) {
7108201da799SBarry Smith     PetscDrawAxis axis;
7109201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7110201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
7111201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7112201da799SBarry Smith     ctx->snes_its = 0;
7113201da799SBarry Smith   }
7114201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
7115201da799SBarry Smith   y    = its - ctx->snes_its;
7116201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
71173fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7118201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
71196934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7120201da799SBarry Smith   }
7121201da799SBarry Smith   ctx->snes_its = its;
7122201da799SBarry Smith   PetscFunctionReturn(0);
7123201da799SBarry Smith }
7124201da799SBarry Smith 
7125201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
7126201da799SBarry Smith {
7127201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
7128201da799SBarry Smith   PetscReal      x   = ptime,y;
7129201da799SBarry Smith   PetscErrorCode ierr;
7130201da799SBarry Smith   PetscInt       its;
7131201da799SBarry Smith 
7132201da799SBarry Smith   PetscFunctionBegin;
713363e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7134201da799SBarry Smith   if (!n) {
7135201da799SBarry Smith     PetscDrawAxis axis;
7136201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7137201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
7138201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7139201da799SBarry Smith     ctx->ksp_its = 0;
7140201da799SBarry Smith   }
7141201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
7142201da799SBarry Smith   y    = its - ctx->ksp_its;
7143201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
714499fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7145201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
71466934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7147201da799SBarry Smith   }
7148201da799SBarry Smith   ctx->ksp_its = its;
7149201da799SBarry Smith   PetscFunctionReturn(0);
7150201da799SBarry Smith }
7151f9c1d6abSBarry Smith 
7152f9c1d6abSBarry Smith /*@
7153f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
7154f9c1d6abSBarry Smith 
7155f9c1d6abSBarry Smith    Collective on TS and Vec
7156f9c1d6abSBarry Smith 
7157f9c1d6abSBarry Smith    Input Parameters:
7158f9c1d6abSBarry Smith +  ts - the TS context
7159f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
7160f9c1d6abSBarry Smith 
7161f9c1d6abSBarry Smith    Output Parameters:
7162f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
7163f9c1d6abSBarry Smith 
7164f9c1d6abSBarry Smith    Level: developer
7165f9c1d6abSBarry Smith 
7166f9c1d6abSBarry Smith .keywords: TS, compute
7167f9c1d6abSBarry Smith 
7168f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
7169f9c1d6abSBarry Smith @*/
7170f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7171f9c1d6abSBarry Smith {
7172f9c1d6abSBarry Smith   PetscErrorCode ierr;
7173f9c1d6abSBarry Smith 
7174f9c1d6abSBarry Smith   PetscFunctionBegin;
7175f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7176ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7177f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
7178f9c1d6abSBarry Smith   PetscFunctionReturn(0);
7179f9c1d6abSBarry Smith }
718024655328SShri 
7181b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7182b3d3934dSBarry Smith /*@C
7183b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7184b3d3934dSBarry Smith 
7185b3d3934dSBarry Smith    Collective on TS
7186b3d3934dSBarry Smith 
7187b3d3934dSBarry Smith    Input Parameters:
7188b3d3934dSBarry Smith .  ts  - the ODE solver object
7189b3d3934dSBarry Smith 
7190b3d3934dSBarry Smith    Output Parameter:
7191b3d3934dSBarry Smith .  ctx - the context
7192b3d3934dSBarry Smith 
7193b3d3934dSBarry Smith    Level: intermediate
7194b3d3934dSBarry Smith 
7195b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
7196b3d3934dSBarry Smith 
7197b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7198b3d3934dSBarry Smith 
7199b3d3934dSBarry Smith @*/
7200b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7201b3d3934dSBarry Smith {
7202b3d3934dSBarry Smith   PetscErrorCode ierr;
7203b3d3934dSBarry Smith 
7204b3d3934dSBarry Smith   PetscFunctionBegin;
7205a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7206b3d3934dSBarry Smith   PetscFunctionReturn(0);
7207b3d3934dSBarry Smith }
7208b3d3934dSBarry Smith 
7209b3d3934dSBarry Smith /*@C
7210b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7211b3d3934dSBarry Smith 
7212b3d3934dSBarry Smith    Collective on TS
7213b3d3934dSBarry Smith 
7214b3d3934dSBarry Smith    Input Parameters:
7215b3d3934dSBarry Smith +  ts - the TS context
7216b3d3934dSBarry Smith .  step - current time-step
7217b3d3934dSBarry Smith .  ptime - current time
72187db568b7SBarry Smith .  u  - current solution
72197db568b7SBarry Smith -  dctx - the envelope context
7220b3d3934dSBarry Smith 
7221b3d3934dSBarry Smith    Options Database:
7222b3d3934dSBarry Smith .  -ts_monitor_envelope
7223b3d3934dSBarry Smith 
7224b3d3934dSBarry Smith    Level: intermediate
7225b3d3934dSBarry Smith 
722695452b02SPatrick Sanan    Notes:
722795452b02SPatrick Sanan     after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7228b3d3934dSBarry Smith 
7229b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7230b3d3934dSBarry Smith 
72317db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7232b3d3934dSBarry Smith @*/
72337db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7234b3d3934dSBarry Smith {
7235b3d3934dSBarry Smith   PetscErrorCode       ierr;
72367db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7237b3d3934dSBarry Smith 
7238b3d3934dSBarry Smith   PetscFunctionBegin;
7239b3d3934dSBarry Smith   if (!ctx->max) {
7240b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7241b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7242b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7243b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7244b3d3934dSBarry Smith   } else {
7245b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7246b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7247b3d3934dSBarry Smith   }
7248b3d3934dSBarry Smith   PetscFunctionReturn(0);
7249b3d3934dSBarry Smith }
7250b3d3934dSBarry Smith 
7251b3d3934dSBarry Smith /*@C
7252b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7253b3d3934dSBarry Smith 
7254b3d3934dSBarry Smith    Collective on TS
7255b3d3934dSBarry Smith 
7256b3d3934dSBarry Smith    Input Parameter:
7257b3d3934dSBarry Smith .  ts - the TS context
7258b3d3934dSBarry Smith 
7259b3d3934dSBarry Smith    Output Parameter:
7260b3d3934dSBarry Smith +  max - the maximum values
7261b3d3934dSBarry Smith -  min - the minimum values
7262b3d3934dSBarry Smith 
726395452b02SPatrick Sanan    Notes:
726495452b02SPatrick Sanan     If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
72657db568b7SBarry Smith 
7266b3d3934dSBarry Smith    Level: intermediate
7267b3d3934dSBarry Smith 
7268b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7269b3d3934dSBarry Smith 
7270b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7271b3d3934dSBarry Smith @*/
7272b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7273b3d3934dSBarry Smith {
7274b3d3934dSBarry Smith   PetscInt i;
7275b3d3934dSBarry Smith 
7276b3d3934dSBarry Smith   PetscFunctionBegin;
7277b3d3934dSBarry Smith   if (max) *max = NULL;
7278b3d3934dSBarry Smith   if (min) *min = NULL;
7279b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7280b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
72815537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7282b3d3934dSBarry Smith       if (max) *max = ctx->max;
7283b3d3934dSBarry Smith       if (min) *min = ctx->min;
7284b3d3934dSBarry Smith       break;
7285b3d3934dSBarry Smith     }
7286b3d3934dSBarry Smith   }
7287b3d3934dSBarry Smith   PetscFunctionReturn(0);
7288b3d3934dSBarry Smith }
7289b3d3934dSBarry Smith 
7290b3d3934dSBarry Smith /*@C
7291b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7292b3d3934dSBarry Smith 
7293b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7294b3d3934dSBarry Smith 
7295b3d3934dSBarry Smith    Input Parameter:
7296b3d3934dSBarry Smith .  ctx - the monitor context
7297b3d3934dSBarry Smith 
7298b3d3934dSBarry Smith    Level: intermediate
7299b3d3934dSBarry Smith 
7300b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
7301b3d3934dSBarry Smith 
73027db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7303b3d3934dSBarry Smith @*/
7304b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7305b3d3934dSBarry Smith {
7306b3d3934dSBarry Smith   PetscErrorCode ierr;
7307b3d3934dSBarry Smith 
7308b3d3934dSBarry Smith   PetscFunctionBegin;
7309b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7310b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7311b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7312b3d3934dSBarry Smith   PetscFunctionReturn(0);
7313b3d3934dSBarry Smith }
7314f2dee214SBarry Smith 
731524655328SShri /*@
7316dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
7317dcb233daSLisandro Dalcin 
7318dcb233daSLisandro Dalcin    Collective on TS
7319dcb233daSLisandro Dalcin 
7320dcb233daSLisandro Dalcin    Input Parameter:
7321dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
7322dcb233daSLisandro Dalcin 
7323dcb233daSLisandro Dalcin    Level: advanced
7324dcb233daSLisandro Dalcin 
7325dcb233daSLisandro Dalcin    Notes:
7326dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
7327dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
7328dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
7329dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
7330dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
7331dcb233daSLisandro Dalcin    discontinuous source terms).
7332dcb233daSLisandro Dalcin 
7333dcb233daSLisandro Dalcin .keywords: TS, timestep, restart
7334dcb233daSLisandro Dalcin 
7335dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
7336dcb233daSLisandro Dalcin @*/
7337dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
7338dcb233daSLisandro Dalcin {
7339dcb233daSLisandro Dalcin   PetscFunctionBegin;
7340dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7341dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
7342dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
7343dcb233daSLisandro Dalcin }
7344dcb233daSLisandro Dalcin 
7345dcb233daSLisandro Dalcin /*@
734624655328SShri    TSRollBack - Rolls back one time step
734724655328SShri 
734824655328SShri    Collective on TS
734924655328SShri 
735024655328SShri    Input Parameter:
735124655328SShri .  ts - the TS context obtained from TSCreate()
735224655328SShri 
735324655328SShri    Level: advanced
735424655328SShri 
735524655328SShri .keywords: TS, timestep, rollback
735624655328SShri 
735724655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
735824655328SShri @*/
735924655328SShri PetscErrorCode  TSRollBack(TS ts)
736024655328SShri {
736124655328SShri   PetscErrorCode ierr;
736224655328SShri 
736324655328SShri   PetscFunctionBegin;
736424655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7365b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
736624655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
736724655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
736824655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
736924655328SShri   ts->ptime = ts->ptime_prev;
7370be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
73712808aa04SLisandro Dalcin   ts->steps--;
7372b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
737324655328SShri   PetscFunctionReturn(0);
737424655328SShri }
7375aeb4809dSShri Abhyankar 
7376ff22ae23SHong Zhang /*@
7377ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7378ff22ae23SHong Zhang 
7379ff22ae23SHong Zhang    Input Parameter:
7380ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7381ff22ae23SHong Zhang 
73820429704eSStefano Zampini    Output Parameters:
73830429704eSStefano Zampini +  ns - the number of stages
73840429704eSStefano Zampini -  Y - the current stage vectors
73850429704eSStefano Zampini 
7386ff22ae23SHong Zhang    Level: advanced
7387ff22ae23SHong Zhang 
73880429704eSStefano Zampini    Notes: Both ns and Y can be NULL.
73890429704eSStefano Zampini 
7390ff22ae23SHong Zhang .keywords: TS, getstages
7391ff22ae23SHong Zhang 
7392ff22ae23SHong Zhang .seealso: TSCreate()
7393ff22ae23SHong Zhang @*/
7394ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7395ff22ae23SHong Zhang {
7396ff22ae23SHong Zhang   PetscErrorCode ierr;
7397ff22ae23SHong Zhang 
7398ff22ae23SHong Zhang   PetscFunctionBegin;
7399ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
74000429704eSStefano Zampini   if (ns) PetscValidPointer(ns,2);
74010429704eSStefano Zampini   if (Y) PetscValidPointer(Y,3);
74020429704eSStefano Zampini   if (!ts->ops->getstages) {
74030429704eSStefano Zampini     if (ns) *ns = 0;
74040429704eSStefano Zampini     if (Y) *Y = NULL;
74050429704eSStefano Zampini   } else {
7406ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7407ff22ae23SHong Zhang   }
7408ff22ae23SHong Zhang   PetscFunctionReturn(0);
7409ff22ae23SHong Zhang }
7410ff22ae23SHong Zhang 
7411847ff0e1SMatthew G. Knepley /*@C
7412847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7413847ff0e1SMatthew G. Knepley 
7414847ff0e1SMatthew G. Knepley   Collective on SNES
7415847ff0e1SMatthew G. Knepley 
7416847ff0e1SMatthew G. Knepley   Input Parameters:
7417847ff0e1SMatthew G. Knepley + ts - the TS context
7418847ff0e1SMatthew G. Knepley . t - current timestep
7419847ff0e1SMatthew G. Knepley . U - state vector
7420847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7421847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7422847ff0e1SMatthew G. Knepley - ctx - an optional user context
7423847ff0e1SMatthew G. Knepley 
7424847ff0e1SMatthew G. Knepley   Output Parameters:
7425847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7426847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7427847ff0e1SMatthew G. Knepley 
7428847ff0e1SMatthew G. Knepley   Level: intermediate
7429847ff0e1SMatthew G. Knepley 
7430847ff0e1SMatthew G. Knepley   Notes:
7431847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7432847ff0e1SMatthew G. Knepley 
7433847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7434847ff0e1SMatthew G. Knepley 
7435847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7436847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7437847ff0e1SMatthew G. Knepley 
7438847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7439847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7440847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7441847ff0e1SMatthew G. Knepley 
7442847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
7443847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7444847ff0e1SMatthew G. Knepley @*/
7445847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7446847ff0e1SMatthew G. Knepley {
7447847ff0e1SMatthew G. Knepley   SNES           snes;
7448847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7449847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7450847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7451847ff0e1SMatthew G. Knepley 
7452847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7453c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7454847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7455847ff0e1SMatthew G. Knepley   if (!color) {
7456847ff0e1SMatthew G. Knepley     DM         dm;
7457847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7458847ff0e1SMatthew G. Knepley 
7459847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7460847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7461847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7462847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7463847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7464847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7465847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7466847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7467847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7468847ff0e1SMatthew G. Knepley     } else {
7469847ff0e1SMatthew G. Knepley       MatColoring mc;
7470847ff0e1SMatthew G. Knepley 
7471847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7472847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7473847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7474847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7475847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7476847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7477847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7478847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7479847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7480847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7481847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7482847ff0e1SMatthew G. Knepley     }
7483847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7484847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7485847ff0e1SMatthew G. Knepley   }
7486847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7487847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7488847ff0e1SMatthew G. Knepley   if (J != B) {
7489847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7490847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7491847ff0e1SMatthew G. Knepley   }
7492847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7493847ff0e1SMatthew G. Knepley }
749493b34091SDebojyoti Ghosh 
7495cb9d8021SPierre Barbier de Reuille /*@
7496cb9d8021SPierre Barbier de Reuille     TSSetFunctionDomainError - Set the function testing if the current state vector is valid
7497cb9d8021SPierre Barbier de Reuille 
7498cb9d8021SPierre Barbier de Reuille     Input Parameters:
7499cb9d8021SPierre Barbier de Reuille     ts - the TS context
7500cb9d8021SPierre Barbier de Reuille     func - function called within TSFunctionDomainError
7501cb9d8021SPierre Barbier de Reuille 
7502cb9d8021SPierre Barbier de Reuille     Level: intermediate
7503cb9d8021SPierre Barbier de Reuille 
7504cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain
7505cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError()
7506cb9d8021SPierre Barbier de Reuille @*/
7507cb9d8021SPierre Barbier de Reuille 
7508d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7509cb9d8021SPierre Barbier de Reuille {
7510cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7511cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7512cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7513cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7514cb9d8021SPierre Barbier de Reuille }
7515cb9d8021SPierre Barbier de Reuille 
7516cb9d8021SPierre Barbier de Reuille /*@
7517cb9d8021SPierre Barbier de Reuille     TSFunctionDomainError - Check if the current state is valid
7518cb9d8021SPierre Barbier de Reuille 
7519cb9d8021SPierre Barbier de Reuille     Input Parameters:
7520cb9d8021SPierre Barbier de Reuille     ts - the TS context
7521cb9d8021SPierre Barbier de Reuille     stagetime - time of the simulation
7522d183316bSPierre Barbier de Reuille     Y - state vector to check.
7523cb9d8021SPierre Barbier de Reuille 
7524cb9d8021SPierre Barbier de Reuille     Output Parameter:
7525cb9d8021SPierre Barbier de Reuille     accept - Set to PETSC_FALSE if the current state vector is valid.
7526cb9d8021SPierre Barbier de Reuille 
7527cb9d8021SPierre Barbier de Reuille     Note:
7528cb9d8021SPierre Barbier de Reuille     This function should be used to ensure the state is in a valid part of the space.
7529cb9d8021SPierre Barbier de Reuille     For example, one can ensure here all values are positive.
753096a0c994SBarry Smith 
753196a0c994SBarry Smith     Level: advanced
7532cb9d8021SPierre Barbier de Reuille @*/
7533d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7534cb9d8021SPierre Barbier de Reuille {
7535cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7536cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7537cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7538cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7539d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7540cb9d8021SPierre Barbier de Reuille   }
7541cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7542cb9d8021SPierre Barbier de Reuille }
75431ceb14c0SBarry Smith 
754493b34091SDebojyoti Ghosh /*@C
7545e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
754693b34091SDebojyoti Ghosh 
754793b34091SDebojyoti Ghosh   Collective on MPI_Comm
754893b34091SDebojyoti Ghosh 
754993b34091SDebojyoti Ghosh   Input Parameter:
755093b34091SDebojyoti Ghosh . tsin    - The input TS
755193b34091SDebojyoti Ghosh 
755293b34091SDebojyoti Ghosh   Output Parameter:
7553e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
755493b34091SDebojyoti Ghosh 
75555eca1a21SEmil Constantinescu   Notes:
75565eca1a21SEmil 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.
75575eca1a21SEmil Constantinescu 
7558928bb9adSStefano Zampini   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); TSSetSNES(ts,snes_dup);
75595eca1a21SEmil Constantinescu 
75605eca1a21SEmil Constantinescu   Level: developer
756193b34091SDebojyoti Ghosh 
7562e5168f73SEmil Constantinescu .keywords: TS, clone
7563e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
756493b34091SDebojyoti Ghosh @*/
7565baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
756693b34091SDebojyoti Ghosh {
756793b34091SDebojyoti Ghosh   TS             t;
756893b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7569dc846ba4SSatish Balay   SNES           snes_start;
7570dc846ba4SSatish Balay   DM             dm;
7571dc846ba4SSatish Balay   TSType         type;
757293b34091SDebojyoti Ghosh 
757393b34091SDebojyoti Ghosh   PetscFunctionBegin;
757493b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
757593b34091SDebojyoti Ghosh   *tsout = NULL;
757693b34091SDebojyoti Ghosh 
75777a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
757893b34091SDebojyoti Ghosh 
757993b34091SDebojyoti Ghosh   /* General TS description */
758093b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
758193b34091SDebojyoti Ghosh   t->setupcalled       = 0;
758293b34091SDebojyoti Ghosh   t->ksp_its           = 0;
758393b34091SDebojyoti Ghosh   t->snes_its          = 0;
758493b34091SDebojyoti Ghosh   t->nwork             = 0;
758593b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
758693b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
758793b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
758893b34091SDebojyoti Ghosh 
758934561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
759034561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7591d15a3a53SEmil Constantinescu 
759293b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
759393b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
759493b34091SDebojyoti Ghosh 
759593b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
759651699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
759793b34091SDebojyoti Ghosh 
7598e7069c78SShri   t->trajectory = tsin->trajectory;
7599e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7600e7069c78SShri 
7601e7069c78SShri   t->event = tsin->event;
76026b10a48eSSatish Balay   if (t->event) t->event->refct++;
7603e7069c78SShri 
760493b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
760593b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7606e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
760793b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
760893b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
760993b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
761093b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
761193b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
761293b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
761393b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
761493b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
761593b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
761693b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
761793b34091SDebojyoti Ghosh 
761893b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
761993b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
762093b34091SDebojyoti Ghosh 
762193b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
762293b34091SDebojyoti Ghosh 
762393b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
762493b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
762593b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
762693b34091SDebojyoti Ghosh 
762793b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
762893b34091SDebojyoti Ghosh 
76290d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
76300d4fed19SBarry Smith     PetscInt i;
76310d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
76320d4fed19SBarry Smith     for (i=0; i<10; i++) {
76330d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
76340d4fed19SBarry Smith     }
76350d4fed19SBarry Smith   }
763693b34091SDebojyoti Ghosh   *tsout = t;
763793b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
763893b34091SDebojyoti Ghosh }
7639f3b1f45cSBarry Smith 
7640f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
7641f3b1f45cSBarry Smith {
7642f3b1f45cSBarry Smith   PetscErrorCode ierr;
7643f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
7644f3b1f45cSBarry Smith 
7645f3b1f45cSBarry Smith   PetscFunctionBegin;
7646f3b1f45cSBarry Smith   ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr);
7647f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7648f3b1f45cSBarry Smith }
7649f3b1f45cSBarry Smith 
7650f3b1f45cSBarry Smith /*@
7651f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
7652f3b1f45cSBarry Smith 
7653f3b1f45cSBarry Smith    Logically Collective on TS and Mat
7654f3b1f45cSBarry Smith 
7655f3b1f45cSBarry Smith     Input Parameters:
7656f3b1f45cSBarry Smith     TS - the time stepping routine
7657f3b1f45cSBarry Smith 
7658f3b1f45cSBarry Smith    Output Parameter:
7659f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7660f3b1f45cSBarry Smith 
7661f3b1f45cSBarry Smith    Options Database:
7662f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
7663f3b1f45cSBarry Smith 
7664f3b1f45cSBarry Smith    Level: advanced
7665f3b1f45cSBarry Smith 
766695452b02SPatrick Sanan    Notes:
766795452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7668f3b1f45cSBarry Smith 
7669f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
7670f3b1f45cSBarry Smith @*/
7671f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
7672f3b1f45cSBarry Smith {
7673f3b1f45cSBarry Smith   Mat            J,B;
7674f3b1f45cSBarry Smith   PetscErrorCode ierr;
7675f3b1f45cSBarry Smith   TSRHSJacobian  func;
7676f3b1f45cSBarry Smith   void*          ctx;
7677f3b1f45cSBarry Smith 
7678f3b1f45cSBarry Smith   PetscFunctionBegin;
7679f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7680f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7681f3b1f45cSBarry Smith   ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7682f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7683f3b1f45cSBarry Smith }
7684f3b1f45cSBarry Smith 
7685f3b1f45cSBarry Smith /*@C
7686f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
7687f3b1f45cSBarry Smith 
7688f3b1f45cSBarry Smith    Logically Collective on TS and Mat
7689f3b1f45cSBarry Smith 
7690f3b1f45cSBarry Smith     Input Parameters:
7691f3b1f45cSBarry Smith     TS - the time stepping routine
7692f3b1f45cSBarry Smith 
7693f3b1f45cSBarry Smith    Output Parameter:
7694f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7695f3b1f45cSBarry Smith 
7696f3b1f45cSBarry Smith    Options Database:
7697f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
7698f3b1f45cSBarry Smith 
769995452b02SPatrick Sanan    Notes:
770095452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7701f3b1f45cSBarry Smith 
7702f3b1f45cSBarry Smith    Level: advanced
7703f3b1f45cSBarry Smith 
7704f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
7705f3b1f45cSBarry Smith @*/
7706f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
7707f3b1f45cSBarry Smith {
7708f3b1f45cSBarry Smith   Mat            J,B;
7709f3b1f45cSBarry Smith   PetscErrorCode ierr;
7710f3b1f45cSBarry Smith   void           *ctx;
7711f3b1f45cSBarry Smith   TSRHSJacobian  func;
7712f3b1f45cSBarry Smith 
7713f3b1f45cSBarry Smith   PetscFunctionBegin;
7714f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7715f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7716f3b1f45cSBarry Smith   ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7717f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7718f3b1f45cSBarry Smith }
77190fe4d17eSHong Zhang 
77200fe4d17eSHong Zhang /*@
77210fe4d17eSHong Zhang   TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used.
77220fe4d17eSHong Zhang 
77230fe4d17eSHong Zhang   Logically collective
77240fe4d17eSHong Zhang 
77250fe4d17eSHong Zhang   Input Parameter:
77260fe4d17eSHong Zhang +  ts - timestepping context
77270fe4d17eSHong Zhang -  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
77280fe4d17eSHong Zhang 
77290fe4d17eSHong Zhang   Options Database:
77300fe4d17eSHong Zhang .   -ts_use_splitrhsfunction - <true,false>
77310fe4d17eSHong Zhang 
77320fe4d17eSHong Zhang   Notes:
77330fe4d17eSHong Zhang     This is only useful for multirate methods
77340fe4d17eSHong Zhang 
77350fe4d17eSHong Zhang   Level: intermediate
77360fe4d17eSHong Zhang 
77370fe4d17eSHong Zhang .seealso: TSGetUseSplitRHSFunction()
77380fe4d17eSHong Zhang @*/
77390fe4d17eSHong Zhang PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction)
77400fe4d17eSHong Zhang {
77410fe4d17eSHong Zhang   PetscFunctionBegin;
77420fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
77430fe4d17eSHong Zhang   ts->use_splitrhsfunction = use_splitrhsfunction;
77440fe4d17eSHong Zhang   PetscFunctionReturn(0);
77450fe4d17eSHong Zhang }
77460fe4d17eSHong Zhang 
77470fe4d17eSHong Zhang /*@
77480fe4d17eSHong Zhang   TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used.
77490fe4d17eSHong Zhang 
77500fe4d17eSHong Zhang   Not collective
77510fe4d17eSHong Zhang 
77520fe4d17eSHong Zhang   Input Parameter:
77530fe4d17eSHong Zhang .  ts - timestepping context
77540fe4d17eSHong Zhang 
77550fe4d17eSHong Zhang   Output Parameter:
77560fe4d17eSHong Zhang .  use_splitrhsfunction - PETSC_TRUE indicates that the split RHSFunction will be used
77570fe4d17eSHong Zhang 
77580fe4d17eSHong Zhang   Level: intermediate
77590fe4d17eSHong Zhang 
77600fe4d17eSHong Zhang .seealso: TSSetUseSplitRHSFunction()
77610fe4d17eSHong Zhang @*/
77620fe4d17eSHong Zhang PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction)
77630fe4d17eSHong Zhang {
77640fe4d17eSHong Zhang   PetscFunctionBegin;
77650fe4d17eSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
77660fe4d17eSHong Zhang   *use_splitrhsfunction = ts->use_splitrhsfunction;
77670fe4d17eSHong Zhang   PetscFunctionReturn(0);
77680fe4d17eSHong Zhang }
7769