xref: /petsc/src/ts/interface/ts.c (revision 1ef274424f215a09ebb8fb75edff2501b0a2cb92)
1af0996ceSBarry Smith #include <petsc/private/tsimpl.h>        /*I "petscts.h"  I*/
2496e6a7aSJed Brown #include <petscdmshell.h>
31e25c274SJed Brown #include <petscdmda.h>
42d5ee99bSBarry Smith #include <petscviewer.h>
52d5ee99bSBarry Smith #include <petscdraw.h>
6d763cef2SBarry Smith 
7d5ba7fb7SMatthew Knepley /* Logging support */
8d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
9a05bf03eSHong Zhang PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
10d405a339SMatthew Knepley 
11feed9e9dSBarry Smith const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED","STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0};
1249354f04SShri Abhyankar 
13fde5950dSBarry Smith /*@C
14fde5950dSBarry Smith    TSMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
15fde5950dSBarry Smith 
16fde5950dSBarry Smith    Collective on TS
17fde5950dSBarry Smith 
18fde5950dSBarry Smith    Input Parameters:
19fde5950dSBarry Smith +  ts - TS object you wish to monitor
20fde5950dSBarry Smith .  name - the monitor type one is seeking
21fde5950dSBarry Smith .  help - message indicating what monitoring is done
22fde5950dSBarry Smith .  manual - manual page for the monitor
23fde5950dSBarry Smith .  monitor - the monitor function
24fde5950dSBarry Smith -  monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the TS or PetscViewer objects
25fde5950dSBarry Smith 
26fde5950dSBarry Smith    Level: developer
27fde5950dSBarry Smith 
28fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
29fde5950dSBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
30fde5950dSBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
31fde5950dSBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
32fde5950dSBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
33fde5950dSBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
34fde5950dSBarry Smith           PetscOptionsFList(), PetscOptionsEList()
35fde5950dSBarry Smith @*/
36721cd6eeSBarry Smith PetscErrorCode  TSMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(TS,PetscViewerAndFormat*))
37fde5950dSBarry Smith {
38fde5950dSBarry Smith   PetscErrorCode    ierr;
39fde5950dSBarry Smith   PetscViewer       viewer;
40fde5950dSBarry Smith   PetscViewerFormat format;
41fde5950dSBarry Smith   PetscBool         flg;
42fde5950dSBarry Smith 
43fde5950dSBarry Smith   PetscFunctionBegin;
4416413a6aSBarry Smith   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject) ts)->options,((PetscObject)ts)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr);
45fde5950dSBarry Smith   if (flg) {
46721cd6eeSBarry Smith     PetscViewerAndFormat *vf;
47721cd6eeSBarry Smith     ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr);
48721cd6eeSBarry Smith     ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr);
49fde5950dSBarry Smith     if (monitorsetup) {
50721cd6eeSBarry Smith       ierr = (*monitorsetup)(ts,vf);CHKERRQ(ierr);
51fde5950dSBarry Smith     }
52721cd6eeSBarry Smith     ierr = TSMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
53fde5950dSBarry Smith   }
54fde5950dSBarry Smith   PetscFunctionReturn(0);
55fde5950dSBarry Smith }
56fde5950dSBarry Smith 
572ffb9264SLisandro Dalcin static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt,TSAdaptType default_type)
582ffb9264SLisandro Dalcin {
592ffb9264SLisandro Dalcin   PetscErrorCode ierr;
602ffb9264SLisandro Dalcin 
612ffb9264SLisandro Dalcin   PetscFunctionBegin;
62b92453a8SLisandro Dalcin   PetscValidHeaderSpecific(adapt,TSADAPT_CLASSID,1);
63b92453a8SLisandro Dalcin   PetscValidCharPointer(default_type,2);
642ffb9264SLisandro Dalcin   if (!((PetscObject)adapt)->type_name) {
652ffb9264SLisandro Dalcin     ierr = TSAdaptSetType(adapt,default_type);CHKERRQ(ierr);
662ffb9264SLisandro Dalcin   }
672ffb9264SLisandro Dalcin   PetscFunctionReturn(0);
682ffb9264SLisandro Dalcin }
692ffb9264SLisandro Dalcin 
70bdad233fSMatthew Knepley /*@
71bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
72bdad233fSMatthew Knepley 
73bdad233fSMatthew Knepley    Collective on TS
74bdad233fSMatthew Knepley 
75bdad233fSMatthew Knepley    Input Parameter:
76bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
77bdad233fSMatthew Knepley 
78bdad233fSMatthew Knepley    Options Database Keys:
79e49d4f37SHong Zhang +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSALPHA, TSGLLE, TSSSP, TSGLEE, TSBSYMP
80ef222394SBarry Smith .  -ts_save_trajectory - checkpoint the solution at each time-step
81ef85077eSLisandro Dalcin .  -ts_max_time <time> - maximum time to compute to
82ef85077eSLisandro Dalcin .  -ts_max_steps <steps> - maximum number of time-steps to take
83ef85077eSLisandro Dalcin .  -ts_init_time <time> - initial time to start computation
84ef85077eSLisandro Dalcin .  -ts_final_time <time> - final time to compute to
853e4cdcaaSBarry Smith .  -ts_dt <dt> - initial time step
86a3cdaa26SBarry Smith .  -ts_exact_final_time <stepover,interpolate,matchstep> whether to stop at the exact given final time and how to compute the solution at that ti,e
87a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
88a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
89a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
90a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
91a3cdaa26SBarry Smith .  -ts_atol <atol> Absolute tolerance for local truncation error
92f3b1f45cSBarry Smith .  -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function
93f3b1f45cSBarry Smith .  -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - test the Jacobian at each iteration against finite difference with RHS function
94ef222394SBarry Smith .  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
95847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
96bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
97de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
98de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
997cf37e64SBarry Smith .  -ts_monitor_error - Monitors norm of error
1006934998bSLisandro Dalcin .  -ts_monitor_lg_timestep - Monitor timestep size graphically
1018b668821SLisandro Dalcin .  -ts_monitor_lg_timestep_log - Monitor log timestep size graphically
102de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
103de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
104de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
105de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
1063e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
1073e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
108fde5950dSBarry Smith .  -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep
109e4160dc7SJulian Andrej .  -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03D.vts (filename-%%03D.vtu)
110110eb670SHong Zhang .  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
11153ea634cSHong Zhang 
11291b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
113bdad233fSMatthew Knepley 
114bdad233fSMatthew Knepley    Level: beginner
115bdad233fSMatthew Knepley 
116bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
117bdad233fSMatthew Knepley 
118a313700dSBarry Smith .seealso: TSGetType()
119bdad233fSMatthew Knepley @*/
1207087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
121bdad233fSMatthew Knepley {
122bc952696SBarry Smith   PetscBool              opt,flg,tflg;
123dfbe8321SBarry Smith   PetscErrorCode         ierr;
124eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
12531748224SBarry Smith   PetscReal              time_step;
12649354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
127d1212d36SBarry Smith   char                   dir[16];
128cd11d68dSLisandro Dalcin   TSIFunction            ifun;
1296991f827SBarry Smith   const char             *defaultType;
1306991f827SBarry Smith   char                   typeName[256];
131bdad233fSMatthew Knepley 
132bdad233fSMatthew Knepley   PetscFunctionBegin;
1330700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1346991f827SBarry Smith 
1356991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
136cd11d68dSLisandro Dalcin   ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
137cd11d68dSLisandro Dalcin 
138cd11d68dSLisandro Dalcin   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
139*1ef27442SStefano Zampini   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
140*1ef27442SStefano Zampini   else defaultType = ifun ? TSBEULER : TSEULER;
1416991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type","TS method","TSSetType",TSList,defaultType,typeName,256,&opt);CHKERRQ(ierr);
1426991f827SBarry Smith   if (opt) {
1436991f827SBarry Smith     ierr = TSSetType(ts,typeName);CHKERRQ(ierr);
1446991f827SBarry Smith   } else {
1456991f827SBarry Smith     ierr = TSSetType(ts,defaultType);CHKERRQ(ierr);
1466991f827SBarry Smith   }
147bdad233fSMatthew Knepley 
148bdad233fSMatthew Knepley   /* Handle generic TS options */
149ef85077eSLisandro Dalcin   ierr = PetscOptionsReal("-ts_max_time","Maximum time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
15019eac22cSLisandro Dalcin   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetMaxSteps",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1510298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
152ef85077eSLisandro Dalcin   ierr = PetscOptionsReal("-ts_final_time","Final time to run to","TSSetMaxTime",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
15331748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
154cd11d68dSLisandro Dalcin   if (flg) {ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);}
15549354f04SShri 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);
15649354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1570298fd71SBarry 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);
1580298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1590298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1600298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1610298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
162bdad233fSMatthew Knepley 
163f3b1f45cSBarry 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);
164f3b1f45cSBarry 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);
16556f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
16656f85f32SBarry Smith   {
16756f85f32SBarry Smith   PetscBool set;
16856f85f32SBarry Smith   flg  = PETSC_FALSE;
16956f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
17056f85f32SBarry Smith   if (set) {
17156f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
17256f85f32SBarry Smith   }
17356f85f32SBarry Smith   }
17456f85f32SBarry Smith #endif
17556f85f32SBarry Smith 
176bdad233fSMatthew Knepley   /* Monitor options */
177cd11d68dSLisandro Dalcin   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor","Monitor time and timestep size","TSMonitorDefault",TSMonitorDefault,NULL);CHKERRQ(ierr);
178cc9c3a59SBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_extreme","Monitor extreme values of the solution","TSMonitorExtreme",TSMonitorExtreme,NULL);CHKERRQ(ierr);
179fde5950dSBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_solution","View the solution at each timestep","TSMonitorSolution",TSMonitorSolution,NULL);CHKERRQ(ierr);
180fde5950dSBarry Smith 
1815180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1825180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1835180491cSLisandro Dalcin 
1844f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
185b3603a34SBarry Smith   if (opt) {
1860b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1873923b477SBarry Smith     PetscInt       howoften = 1;
188b3603a34SBarry Smith 
1890298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
1906ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
1914f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
192bdad233fSMatthew Knepley   }
1936ba87a44SLisandro Dalcin 
1944f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
195ef20d060SBarry Smith   if (opt) {
1960b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1973923b477SBarry Smith     PetscInt       howoften = 1;
198ef20d060SBarry Smith 
1990298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
2006ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2014f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
202ef20d060SBarry Smith   }
203edbaebb3SBarry Smith   ierr = TSMonitorSetFromOptions(ts,"-ts_monitor_error","View the error at each timestep","TSMonitorError",TSMonitorError,NULL);CHKERRQ(ierr);
2047cf37e64SBarry Smith 
2056934998bSLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2066934998bSLisandro Dalcin   if (opt) {
2076934998bSLisandro Dalcin     TSMonitorLGCtx ctx;
2086934998bSLisandro Dalcin     PetscInt       howoften = 1;
2096934998bSLisandro Dalcin 
2106934998bSLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2116ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2126934998bSLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2136934998bSLisandro Dalcin   }
2148b668821SLisandro Dalcin   ierr = PetscOptionsName("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
2158b668821SLisandro Dalcin   if (opt) {
2168b668821SLisandro Dalcin     TSMonitorLGCtx ctx;
2178b668821SLisandro Dalcin     PetscInt       howoften = 1;
2188b668821SLisandro Dalcin 
2198b668821SLisandro Dalcin     ierr = PetscOptionsInt("-ts_monitor_lg_timestep_log","Monitor log timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
2208b668821SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
2218b668821SLisandro Dalcin     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
2228b668821SLisandro Dalcin     ctx->semilogy = PETSC_TRUE;
2238b668821SLisandro Dalcin   }
2248b668821SLisandro Dalcin 
225201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
226201da799SBarry Smith   if (opt) {
227201da799SBarry Smith     TSMonitorLGCtx ctx;
228201da799SBarry Smith     PetscInt       howoften = 1;
229201da799SBarry Smith 
2300298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2316ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
232201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
233201da799SBarry Smith   }
234201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
235201da799SBarry Smith   if (opt) {
236201da799SBarry Smith     TSMonitorLGCtx ctx;
237201da799SBarry Smith     PetscInt       howoften = 1;
238201da799SBarry Smith 
2390298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
2406ba87a44SLisandro Dalcin     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,howoften,&ctx);CHKERRQ(ierr);
241201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
242201da799SBarry Smith   }
2438189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
2448189c53fSBarry Smith   if (opt) {
2458189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
2468189c53fSBarry Smith     PetscInt          howoften = 1;
2478189c53fSBarry Smith 
2480298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
2496934998bSLisandro Dalcin     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2508189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
2518189c53fSBarry Smith   }
252ef20d060SBarry Smith   opt  = PETSC_FALSE;
2530dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
254a7cc72afSBarry Smith   if (opt) {
25583a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
25683a4ac43SBarry Smith     PetscInt         howoften = 1;
257a80ad3e0SBarry Smith 
2580298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
2590ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Computed Solution",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
26083a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
261bdad233fSMatthew Knepley   }
262fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2632d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2642d5ee99bSBarry Smith   if (opt) {
2652d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2662d5ee99bSBarry Smith     PetscReal        bounds[4];
2672d5ee99bSBarry Smith     PetscInt         n = 4;
2682d5ee99bSBarry Smith     PetscDraw        draw;
2696934998bSLisandro Dalcin     PetscDrawAxis    axis;
2702d5ee99bSBarry Smith 
2712d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
2722d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
2736934998bSLisandro Dalcin     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,1,&ctx);CHKERRQ(ierr);
2742d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
2756934998bSLisandro Dalcin     ierr = PetscViewerDrawGetDrawAxis(ctx->viewer,0,&axis);CHKERRQ(ierr);
2766934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLimits(axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
2776934998bSLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
2782d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2792d5ee99bSBarry Smith   }
2802d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2810dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
2823a471f94SBarry Smith   if (opt) {
28383a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
28483a4ac43SBarry Smith     PetscInt         howoften = 1;
2853a471f94SBarry Smith 
2860298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
2870ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Error",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
28883a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2893a471f94SBarry Smith   }
2900ed3bfb6SBarry Smith   opt  = PETSC_FALSE;
2910ed3bfb6SBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",&opt);CHKERRQ(ierr);
2920ed3bfb6SBarry Smith   if (opt) {
2930ed3bfb6SBarry Smith     TSMonitorDrawCtx ctx;
2940ed3bfb6SBarry Smith     PetscInt         howoften = 1;
2950ed3bfb6SBarry Smith 
2960ed3bfb6SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution_function","Monitor solution provided by TSMonitorSetSolutionFunction() graphically","TSMonitorDrawSolutionFunction",howoften,&howoften,NULL);CHKERRQ(ierr);
2970ed3bfb6SBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,"Solution provided by user function",PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
2980ed3bfb6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionFunction,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2990ed3bfb6SBarry Smith   }
300fde5950dSBarry Smith 
301ed81e22dSJed Brown   opt  = PETSC_FALSE;
30291b97e58SBarry 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);
303ed81e22dSJed Brown   if (flg) {
304ed81e22dSJed Brown     const char *ptr,*ptr2;
305ed81e22dSJed Brown     char       *filetemplate;
306ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
307ed81e22dSJed Brown     /* Do some cursory validation of the input. */
308ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
309ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
310ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
311ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
312ce94432eSBarry 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");
313ed81e22dSJed Brown       if (ptr2) break;
314ed81e22dSJed Brown     }
315ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
316ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
317ed81e22dSJed Brown   }
318bdad233fSMatthew Knepley 
319d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
320d1212d36SBarry Smith   if (flg) {
321d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
322d1212d36SBarry Smith     int                  ray = 0;
323d1212d36SBarry Smith     DMDADirection        ddir;
324d1212d36SBarry Smith     DM                   da;
325d1212d36SBarry Smith     PetscMPIInt          rank;
326d1212d36SBarry Smith 
327ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
328d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
329d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
330ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
331d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
332d1212d36SBarry Smith 
333d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
334b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
335d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
336d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
337ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
338d1212d36SBarry Smith     if (!rank) {
339d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
340d1212d36SBarry Smith     }
34151b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
342d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
343d1212d36SBarry Smith   }
34451b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
34551b4a12fSMatthew G. Knepley   if (flg) {
34651b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
34751b4a12fSMatthew G. Knepley     int                 ray = 0;
34851b4a12fSMatthew G. Knepley     DMDADirection       ddir;
34951b4a12fSMatthew G. Knepley     DM                  da;
35051b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
351d1212d36SBarry Smith 
35251b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
35351b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
35451b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
35551b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
35651b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3571c3436cfSJed Brown 
35851b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
359b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
36051b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
36151b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
36251b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
36351b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
36451b4a12fSMatthew G. Knepley   }
365a7a1495cSBarry Smith 
366b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
367b3d3934dSBarry Smith   if (opt) {
368b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
369b3d3934dSBarry Smith 
370b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
371b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
372b3d3934dSBarry Smith   }
373b3d3934dSBarry Smith 
374847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
375847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
376847ff0e1SMatthew G. Knepley   if (flg) {
377847ff0e1SMatthew G. Knepley     DM   dm;
378847ff0e1SMatthew G. Knepley     DMTS tdm;
379847ff0e1SMatthew G. Knepley 
380847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
381847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
382847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
383847ff0e1SMatthew G. Knepley     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr);
384847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
385847ff0e1SMatthew G. Knepley   }
386847ff0e1SMatthew G. Knepley 
387d763cef2SBarry Smith   /* Handle specific TS options */
388d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
3896991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
390d763cef2SBarry Smith   }
391fbc52257SHong Zhang 
392a7bdc993SLisandro Dalcin   /* Handle TSAdapt options */
393a7bdc993SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
394a7bdc993SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
395a7bdc993SLisandro Dalcin   ierr = TSAdaptSetFromOptions(PetscOptionsObject,ts->adapt);CHKERRQ(ierr);
396a7bdc993SLisandro Dalcin 
39768bece0bSHong Zhang   /* TS trajectory must be set after TS, since it may use some TS options above */
3984f122a70SLisandro Dalcin   tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE;
39968bece0bSHong Zhang   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
40068bece0bSHong Zhang   if (tflg) {
40168bece0bSHong Zhang     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
40268bece0bSHong Zhang   }
403a05bf03eSHong Zhang 
404a05bf03eSHong Zhang   ierr = TSAdjointSetFromOptions(PetscOptionsObject,ts);CHKERRQ(ierr);
405d763cef2SBarry Smith 
406d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4070633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)ts);CHKERRQ(ierr);
408fbc52257SHong Zhang   ierr = PetscOptionsEnd();CHKERRQ(ierr);
409d763cef2SBarry Smith 
4104f122a70SLisandro Dalcin   if (ts->trajectory) {
4114f122a70SLisandro Dalcin     ierr = TSTrajectorySetFromOptions(ts->trajectory,ts);CHKERRQ(ierr);
412d763cef2SBarry Smith   }
41368bece0bSHong Zhang 
414*1ef27442SStefano Zampini   /* why do we have to do this here and not during TSSetUp? */
4154f122a70SLisandro Dalcin   ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr);
416*1ef27442SStefano Zampini   if (ts->problem_type == TS_LINEAR) {
417*1ef27442SStefano Zampini     ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&flg,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
418*1ef27442SStefano Zampini     if (!flg) { ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); }
419*1ef27442SStefano Zampini   }
4204f122a70SLisandro Dalcin   ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
421d763cef2SBarry Smith   PetscFunctionReturn(0);
422d763cef2SBarry Smith }
423d763cef2SBarry Smith 
424d2daff3dSHong Zhang /*@
42578fbdcc8SBarry Smith    TSGetTrajectory - Gets the trajectory from a TS if it exists
42678fbdcc8SBarry Smith 
42778fbdcc8SBarry Smith    Collective on TS
42878fbdcc8SBarry Smith 
42978fbdcc8SBarry Smith    Input Parameters:
43078fbdcc8SBarry Smith .  ts - the TS context obtained from TSCreate()
43178fbdcc8SBarry Smith 
43278fbdcc8SBarry Smith    Output Parameters;
43378fbdcc8SBarry Smith .  tr - the TSTrajectory object, if it exists
43478fbdcc8SBarry Smith 
43578fbdcc8SBarry Smith    Note: This routine should be called after all TS options have been set
43678fbdcc8SBarry Smith 
43778fbdcc8SBarry Smith    Level: advanced
43878fbdcc8SBarry Smith 
43978fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectory, TSTrajectoryCreate()
44078fbdcc8SBarry Smith 
44178fbdcc8SBarry Smith .keywords: TS, set, checkpoint,
44278fbdcc8SBarry Smith @*/
44378fbdcc8SBarry Smith PetscErrorCode  TSGetTrajectory(TS ts,TSTrajectory *tr)
44478fbdcc8SBarry Smith {
44578fbdcc8SBarry Smith   PetscFunctionBegin;
44678fbdcc8SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44778fbdcc8SBarry Smith   *tr = ts->trajectory;
44878fbdcc8SBarry Smith   PetscFunctionReturn(0);
44978fbdcc8SBarry Smith }
45078fbdcc8SBarry Smith 
45178fbdcc8SBarry Smith /*@
452bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
453d2daff3dSHong Zhang 
454d2daff3dSHong Zhang    Collective on TS
455d2daff3dSHong Zhang 
456d2daff3dSHong Zhang    Input Parameters:
457bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
458bc952696SBarry Smith 
45978fbdcc8SBarry Smith    Options Database:
46078fbdcc8SBarry Smith +  -ts_save_trajectory - saves the trajectory to a file
46178fbdcc8SBarry Smith -  -ts_trajectory_type type
46278fbdcc8SBarry Smith 
46368bece0bSHong Zhang Note: This routine should be called after all TS options have been set
464d2daff3dSHong Zhang 
465c3a89c15SBarry Smith     The TSTRAJECTORYVISUALIZATION files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and
46678fbdcc8SBarry Smith    MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m
46778fbdcc8SBarry Smith 
468d2daff3dSHong Zhang    Level: intermediate
469d2daff3dSHong Zhang 
47078fbdcc8SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve(), TSTrajectoryType, TSSetTrajectoryType()
471d2daff3dSHong Zhang 
472bc952696SBarry Smith .keywords: TS, set, checkpoint,
473d2daff3dSHong Zhang @*/
474bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
475d2daff3dSHong Zhang {
476bc952696SBarry Smith   PetscErrorCode ierr;
477bc952696SBarry Smith 
478d2daff3dSHong Zhang   PetscFunctionBegin;
479d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
480bc952696SBarry Smith   if (!ts->trajectory) {
481bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
482bc952696SBarry Smith   }
483d2daff3dSHong Zhang   PetscFunctionReturn(0);
484d2daff3dSHong Zhang }
485d2daff3dSHong Zhang 
486a7a1495cSBarry Smith /*@
487a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
488a7a1495cSBarry Smith       set with TSSetRHSJacobian().
489a7a1495cSBarry Smith 
490a7a1495cSBarry Smith    Collective on TS and Vec
491a7a1495cSBarry Smith 
492a7a1495cSBarry Smith    Input Parameters:
493316643e7SJed Brown +  ts - the TS context
494a7a1495cSBarry Smith .  t - current timestep
4950910c330SBarry Smith -  U - input vector
496a7a1495cSBarry Smith 
497a7a1495cSBarry Smith    Output Parameters:
498a7a1495cSBarry Smith +  A - Jacobian matrix
499a7a1495cSBarry Smith .  B - optional preconditioning matrix
500a7a1495cSBarry Smith -  flag - flag indicating matrix structure
501a7a1495cSBarry Smith 
502a7a1495cSBarry Smith    Notes:
503a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
504a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
505a7a1495cSBarry Smith 
50694b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
507a7a1495cSBarry Smith    flag parameter.
508a7a1495cSBarry Smith 
509a7a1495cSBarry Smith    Level: developer
510a7a1495cSBarry Smith 
511a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
512a7a1495cSBarry Smith 
51394b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
514a7a1495cSBarry Smith @*/
515d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
516a7a1495cSBarry Smith {
517dfbe8321SBarry Smith   PetscErrorCode   ierr;
518270bf2e7SJed Brown   PetscObjectState Ustate;
5196c1e1eecSBarry Smith   PetscObjectId    Uid;
52024989b8cSPeter Brune   DM               dm;
521942e3340SBarry Smith   DMTS             tsdm;
52224989b8cSPeter Brune   TSRHSJacobian    rhsjacobianfunc;
52324989b8cSPeter Brune   void             *ctx;
52424989b8cSPeter Brune   TSIJacobian      ijacobianfunc;
525b2df71adSDebojyoti Ghosh   TSRHSFunction    rhsfunction;
526a7a1495cSBarry Smith 
527a7a1495cSBarry Smith   PetscFunctionBegin;
5280700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5290910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5300910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
53124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
532942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
53324989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
5340298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
535b2df71adSDebojyoti Ghosh   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
53659e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
5376c1e1eecSBarry Smith   ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
5386c1e1eecSBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
5390e4ef248SJed Brown     PetscFunctionReturn(0);
540f8ede8e7SJed Brown   }
541d90be118SSean Farley 
542ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
543d90be118SSean Farley 
544e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
54594ab13aaSBarry Smith     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
54694ab13aaSBarry Smith     ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
547303f9b21SStefano Zampini     if (B && A != B) {
54894ab13aaSBarry Smith       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
54994ab13aaSBarry Smith       ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
550e1244c69SJed Brown     }
551e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
552e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
553e1244c69SJed Brown   }
554e1244c69SJed Brown 
55524989b8cSPeter Brune   if (rhsjacobianfunc) {
5566cd88445SBarry Smith     PetscBool missing;
55794ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
558a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
559d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
560a7a1495cSBarry Smith     PetscStackPop;
56194ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
5626cd88445SBarry Smith     if (A) {
5636cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
5646cd88445SBarry 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");
5656cd88445SBarry Smith     }
5666cd88445SBarry Smith     if (B && B != A) {
5676cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
5686cd88445SBarry 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");
5696cd88445SBarry Smith     }
570ef66eb69SBarry Smith   } else {
57194ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
57294ab13aaSBarry Smith     if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
573ef66eb69SBarry Smith   }
5740e4ef248SJed Brown   ts->rhsjacobian.time       = t;
5757f79407eSBarry Smith   ierr                       = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr);
57659e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
577a7a1495cSBarry Smith   PetscFunctionReturn(0);
578a7a1495cSBarry Smith }
579a7a1495cSBarry Smith 
580316643e7SJed Brown /*@
581d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
582d763cef2SBarry Smith 
583316643e7SJed Brown    Collective on TS and Vec
584316643e7SJed Brown 
585316643e7SJed Brown    Input Parameters:
586316643e7SJed Brown +  ts - the TS context
587316643e7SJed Brown .  t - current time
5880910c330SBarry Smith -  U - state vector
589316643e7SJed Brown 
590316643e7SJed Brown    Output Parameter:
591316643e7SJed Brown .  y - right hand side
592316643e7SJed Brown 
593316643e7SJed Brown    Note:
594316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
595316643e7SJed Brown    is used internally within the nonlinear solvers.
596316643e7SJed Brown 
597316643e7SJed Brown    Level: developer
598316643e7SJed Brown 
599316643e7SJed Brown .keywords: TS, compute
600316643e7SJed Brown 
601316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
602316643e7SJed Brown @*/
6030910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
604d763cef2SBarry Smith {
605dfbe8321SBarry Smith   PetscErrorCode ierr;
60624989b8cSPeter Brune   TSRHSFunction  rhsfunction;
60724989b8cSPeter Brune   TSIFunction    ifunction;
60824989b8cSPeter Brune   void           *ctx;
60924989b8cSPeter Brune   DM             dm;
61024989b8cSPeter Brune 
611d763cef2SBarry Smith   PetscFunctionBegin;
6120700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6130910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6140700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
61524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
61624989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6170298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
618d763cef2SBarry Smith 
619ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
620d763cef2SBarry Smith 
6210910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
62224989b8cSPeter Brune   if (rhsfunction) {
623d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6240910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
625d763cef2SBarry Smith     PetscStackPop;
626214bc6a2SJed Brown   } else {
627214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
628b2cd27e8SJed Brown   }
62944a41b28SSean Farley 
6300910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
631d763cef2SBarry Smith   PetscFunctionReturn(0);
632d763cef2SBarry Smith }
633d763cef2SBarry Smith 
634ef20d060SBarry Smith /*@
635ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
636ef20d060SBarry Smith 
637ef20d060SBarry Smith    Collective on TS and Vec
638ef20d060SBarry Smith 
639ef20d060SBarry Smith    Input Parameters:
640ef20d060SBarry Smith +  ts - the TS context
641ef20d060SBarry Smith -  t - current time
642ef20d060SBarry Smith 
643ef20d060SBarry Smith    Output Parameter:
6440910c330SBarry Smith .  U - the solution
645ef20d060SBarry Smith 
646ef20d060SBarry Smith    Note:
647ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
648ef20d060SBarry Smith    is used internally within the nonlinear solvers.
649ef20d060SBarry Smith 
650ef20d060SBarry Smith    Level: developer
651ef20d060SBarry Smith 
652ef20d060SBarry Smith .keywords: TS, compute
653ef20d060SBarry Smith 
654abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
655ef20d060SBarry Smith @*/
6560910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
657ef20d060SBarry Smith {
658ef20d060SBarry Smith   PetscErrorCode     ierr;
659ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
660ef20d060SBarry Smith   void               *ctx;
661ef20d060SBarry Smith   DM                 dm;
662ef20d060SBarry Smith 
663ef20d060SBarry Smith   PetscFunctionBegin;
664ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6650910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
666ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
667ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
668ef20d060SBarry Smith 
669ef20d060SBarry Smith   if (solutionfunction) {
6709b7cd975SBarry Smith     PetscStackPush("TS user solution function");
6710910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
672ef20d060SBarry Smith     PetscStackPop;
673ef20d060SBarry Smith   }
674ef20d060SBarry Smith   PetscFunctionReturn(0);
675ef20d060SBarry Smith }
6769b7cd975SBarry Smith /*@
6779b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
6789b7cd975SBarry Smith 
6799b7cd975SBarry Smith    Collective on TS and Vec
6809b7cd975SBarry Smith 
6819b7cd975SBarry Smith    Input Parameters:
6829b7cd975SBarry Smith +  ts - the TS context
6839b7cd975SBarry Smith -  t - current time
6849b7cd975SBarry Smith 
6859b7cd975SBarry Smith    Output Parameter:
6869b7cd975SBarry Smith .  U - the function value
6879b7cd975SBarry Smith 
6889b7cd975SBarry Smith    Note:
6899b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
6909b7cd975SBarry Smith    is used internally within the nonlinear solvers.
6919b7cd975SBarry Smith 
6929b7cd975SBarry Smith    Level: developer
6939b7cd975SBarry Smith 
6949b7cd975SBarry Smith .keywords: TS, compute
6959b7cd975SBarry Smith 
6969b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
6979b7cd975SBarry Smith @*/
6989b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
6999b7cd975SBarry Smith {
7009b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7019b7cd975SBarry Smith   void               *ctx;
7029b7cd975SBarry Smith   DM                 dm;
7039b7cd975SBarry Smith 
7049b7cd975SBarry Smith   PetscFunctionBegin;
7059b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7069b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7079b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7089b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7099b7cd975SBarry Smith 
7109b7cd975SBarry Smith   if (forcing) {
7119b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7129b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7139b7cd975SBarry Smith     PetscStackPop;
7149b7cd975SBarry Smith   }
7159b7cd975SBarry Smith   PetscFunctionReturn(0);
7169b7cd975SBarry Smith }
717ef20d060SBarry Smith 
718214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
719214bc6a2SJed Brown {
7202dd45cf8SJed Brown   Vec            F;
721214bc6a2SJed Brown   PetscErrorCode ierr;
722214bc6a2SJed Brown 
723214bc6a2SJed Brown   PetscFunctionBegin;
7240298fd71SBarry Smith   *Frhs = NULL;
7250298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
726214bc6a2SJed Brown   if (!ts->Frhs) {
7272dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
728214bc6a2SJed Brown   }
729214bc6a2SJed Brown   *Frhs = ts->Frhs;
730214bc6a2SJed Brown   PetscFunctionReturn(0);
731214bc6a2SJed Brown }
732214bc6a2SJed Brown 
733214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
734214bc6a2SJed Brown {
735214bc6a2SJed Brown   Mat            A,B;
7362dd45cf8SJed Brown   PetscErrorCode ierr;
73741a1d4d2SBarry Smith   TSIJacobian    ijacobian;
738214bc6a2SJed Brown 
739214bc6a2SJed Brown   PetscFunctionBegin;
740c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
741c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
74241a1d4d2SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr);
743214bc6a2SJed Brown   if (Arhs) {
744214bc6a2SJed Brown     if (!ts->Arhs) {
74541a1d4d2SBarry Smith       if (ijacobian) {
746214bc6a2SJed Brown         ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
74741a1d4d2SBarry Smith       } else {
74841a1d4d2SBarry Smith         ts->Arhs = A;
74941a1d4d2SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
75041a1d4d2SBarry Smith       }
7513565c898SBarry Smith     } else {
7523565c898SBarry Smith       PetscBool flg;
7533565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
7543565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
7553565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs){
7563565c898SBarry Smith         ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr);
7573565c898SBarry Smith         ts->Arhs = A;
7583565c898SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
7593565c898SBarry Smith       }
760214bc6a2SJed Brown     }
761214bc6a2SJed Brown     *Arhs = ts->Arhs;
762214bc6a2SJed Brown   }
763214bc6a2SJed Brown   if (Brhs) {
764214bc6a2SJed Brown     if (!ts->Brhs) {
765bdb70873SJed Brown       if (A != B) {
76641a1d4d2SBarry Smith         if (ijacobian) {
767214bc6a2SJed Brown           ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
768bdb70873SJed Brown         } else {
76941a1d4d2SBarry Smith           ts->Brhs = B;
77041a1d4d2SBarry Smith           ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
77141a1d4d2SBarry Smith         }
77241a1d4d2SBarry Smith       } else {
773bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
77451699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
775bdb70873SJed Brown       }
776214bc6a2SJed Brown     }
777214bc6a2SJed Brown     *Brhs = ts->Brhs;
778214bc6a2SJed Brown   }
779214bc6a2SJed Brown   PetscFunctionReturn(0);
780214bc6a2SJed Brown }
781214bc6a2SJed Brown 
782316643e7SJed Brown /*@
7830910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
784316643e7SJed Brown 
785316643e7SJed Brown    Collective on TS and Vec
786316643e7SJed Brown 
787316643e7SJed Brown    Input Parameters:
788316643e7SJed Brown +  ts - the TS context
789316643e7SJed Brown .  t - current time
7900910c330SBarry Smith .  U - state vector
7910910c330SBarry Smith .  Udot - time derivative of state vector
792214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
793316643e7SJed Brown 
794316643e7SJed Brown    Output Parameter:
795316643e7SJed Brown .  Y - right hand side
796316643e7SJed Brown 
797316643e7SJed Brown    Note:
798316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
799316643e7SJed Brown    is used internally within the nonlinear solvers.
800316643e7SJed Brown 
801316643e7SJed Brown    If the user did did not write their equations in implicit form, this
802316643e7SJed Brown    function recasts them in implicit form.
803316643e7SJed Brown 
804316643e7SJed Brown    Level: developer
805316643e7SJed Brown 
806316643e7SJed Brown .keywords: TS, compute
807316643e7SJed Brown 
808316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
809316643e7SJed Brown @*/
8100910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
811316643e7SJed Brown {
812316643e7SJed Brown   PetscErrorCode ierr;
81324989b8cSPeter Brune   TSIFunction    ifunction;
81424989b8cSPeter Brune   TSRHSFunction  rhsfunction;
81524989b8cSPeter Brune   void           *ctx;
81624989b8cSPeter Brune   DM             dm;
817316643e7SJed Brown 
818316643e7SJed Brown   PetscFunctionBegin;
8190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8200910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8210910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8220700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
823316643e7SJed Brown 
82424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
82524989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8260298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
82724989b8cSPeter Brune 
828ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
829d90be118SSean Farley 
8300910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
83124989b8cSPeter Brune   if (ifunction) {
832316643e7SJed Brown     PetscStackPush("TS user implicit function");
8330910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
834316643e7SJed Brown     PetscStackPop;
835214bc6a2SJed Brown   }
836214bc6a2SJed Brown   if (imex) {
83724989b8cSPeter Brune     if (!ifunction) {
8380910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8392dd45cf8SJed Brown     }
84024989b8cSPeter Brune   } else if (rhsfunction) {
84124989b8cSPeter Brune     if (ifunction) {
842214bc6a2SJed Brown       Vec Frhs;
843214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
8440910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
845214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
8462dd45cf8SJed Brown     } else {
8470910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
8480910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
849316643e7SJed Brown     }
8504a6899ffSJed Brown   }
8510910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
852316643e7SJed Brown   PetscFunctionReturn(0);
853316643e7SJed Brown }
854316643e7SJed Brown 
855316643e7SJed Brown /*@
856316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
857316643e7SJed Brown 
858316643e7SJed Brown    Collective on TS and Vec
859316643e7SJed Brown 
860316643e7SJed Brown    Input
861316643e7SJed Brown       Input Parameters:
862316643e7SJed Brown +  ts - the TS context
863316643e7SJed Brown .  t - current timestep
8640910c330SBarry Smith .  U - state vector
8650910c330SBarry Smith .  Udot - time derivative of state vector
866214bc6a2SJed Brown .  shift - shift to apply, see note below
867214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
868316643e7SJed Brown 
869316643e7SJed Brown    Output Parameters:
870316643e7SJed Brown +  A - Jacobian matrix
8713565c898SBarry Smith -  B - matrix from which the preconditioner is constructed; often the same as A
872316643e7SJed Brown 
873316643e7SJed Brown    Notes:
8740910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
875316643e7SJed Brown 
8760910c330SBarry Smith    dF/dU + shift*dF/dUdot
877316643e7SJed Brown 
878316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
879316643e7SJed Brown    is used internally within the nonlinear solvers.
880316643e7SJed Brown 
881316643e7SJed Brown    Level: developer
882316643e7SJed Brown 
883316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
884316643e7SJed Brown 
885316643e7SJed Brown .seealso:  TSSetIJacobian()
88663495f91SJed Brown @*/
887d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
888316643e7SJed Brown {
889316643e7SJed Brown   PetscErrorCode ierr;
89024989b8cSPeter Brune   TSIJacobian    ijacobian;
89124989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
89224989b8cSPeter Brune   DM             dm;
89324989b8cSPeter Brune   void           *ctx;
894316643e7SJed Brown 
895316643e7SJed Brown   PetscFunctionBegin;
8960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8970910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8980910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
899316643e7SJed Brown   PetscValidPointer(A,6);
90094ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
901316643e7SJed Brown   PetscValidPointer(B,7);
90294ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
90324989b8cSPeter Brune 
90424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
90524989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9060298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
90724989b8cSPeter Brune 
908ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
909316643e7SJed Brown 
91094ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
91124989b8cSPeter Brune   if (ijacobian) {
9126cd88445SBarry Smith     PetscBool missing;
913316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
914d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
915316643e7SJed Brown     PetscStackPop;
9166cd88445SBarry Smith     ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
9176cd88445SBarry 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");
9183565c898SBarry Smith     if (B != A) {
9196cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
9206cd88445SBarry 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");
9216cd88445SBarry Smith     }
9224a6899ffSJed Brown   }
923214bc6a2SJed Brown   if (imex) {
924b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9254c26be97Sstefano_zampini       PetscBool assembled;
92694ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
9274c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
9284c26be97Sstefano_zampini       if (!assembled) {
9294c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9304c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9314c26be97Sstefano_zampini       }
93294ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
93394ab13aaSBarry Smith       if (A != B) {
93494ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
9354c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
9364c26be97Sstefano_zampini         if (!assembled) {
9374c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9384c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9394c26be97Sstefano_zampini         }
94094ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
941214bc6a2SJed Brown       }
942214bc6a2SJed Brown     }
943214bc6a2SJed Brown   } else {
944e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
945e1244c69SJed Brown     if (rhsjacobian) {
946214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
947d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
948e1244c69SJed Brown     }
94994ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
9503565c898SBarry Smith       PetscBool flg;
951e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
952e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
9533565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
9543565c898SBarry Smith       /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
9553565c898SBarry Smith       if (!flg) {
95694ab13aaSBarry Smith         ierr = MatScale(A,-1);CHKERRQ(ierr);
95794ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
9583565c898SBarry Smith       }
95994ab13aaSBarry Smith       if (A != B) {
96094ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
96194ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
962316643e7SJed Brown       }
963e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
964e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
965e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
96694ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
96794ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
96894ab13aaSBarry Smith         if (A != B) {
96994ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
97094ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
971214bc6a2SJed Brown         }
972316643e7SJed Brown       }
97394ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
97494ab13aaSBarry Smith       if (A != B) {
97594ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
976316643e7SJed Brown       }
977316643e7SJed Brown     }
978316643e7SJed Brown   }
97994ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
980316643e7SJed Brown   PetscFunctionReturn(0);
981316643e7SJed Brown }
982316643e7SJed Brown 
983d763cef2SBarry Smith /*@C
984d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
985b5abc632SBarry Smith     where U_t = G(t,u).
986d763cef2SBarry Smith 
9873f9fe445SBarry Smith     Logically Collective on TS
988d763cef2SBarry Smith 
989d763cef2SBarry Smith     Input Parameters:
990d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
9910298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
992d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
993d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
9940298fd71SBarry Smith           function evaluation routine (may be NULL)
995d763cef2SBarry Smith 
996d763cef2SBarry Smith     Calling sequence of func:
99787828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
998d763cef2SBarry Smith 
999d763cef2SBarry Smith +   t - current timestep
1000d763cef2SBarry Smith .   u - input vector
1001d763cef2SBarry Smith .   F - function vector
1002d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1003d763cef2SBarry Smith 
1004d763cef2SBarry Smith     Level: beginner
1005d763cef2SBarry Smith 
100695452b02SPatrick Sanan     Notes:
100795452b02SPatrick Sanan     You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10082bbac0d3SBarry Smith 
1009d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1010d763cef2SBarry Smith 
1011ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1012d763cef2SBarry Smith @*/
1013089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1014d763cef2SBarry Smith {
1015089b2837SJed Brown   PetscErrorCode ierr;
1016089b2837SJed Brown   SNES           snes;
10170298fd71SBarry Smith   Vec            ralloc = NULL;
101824989b8cSPeter Brune   DM             dm;
1019d763cef2SBarry Smith 
1020089b2837SJed Brown   PetscFunctionBegin;
10210700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1022ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
102324989b8cSPeter Brune 
102424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
102524989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1026089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1027e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1028e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1029e856ceecSJed Brown     r = ralloc;
1030e856ceecSJed Brown   }
1031089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1032e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1033d763cef2SBarry Smith   PetscFunctionReturn(0);
1034d763cef2SBarry Smith }
1035d763cef2SBarry Smith 
1036ef20d060SBarry Smith /*@C
1037abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1038ef20d060SBarry Smith 
1039ef20d060SBarry Smith     Logically Collective on TS
1040ef20d060SBarry Smith 
1041ef20d060SBarry Smith     Input Parameters:
1042ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1043ef20d060SBarry Smith .   f - routine for evaluating the solution
1044ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
10450298fd71SBarry Smith           function evaluation routine (may be NULL)
1046ef20d060SBarry Smith 
1047ef20d060SBarry Smith     Calling sequence of func:
1048ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
1049ef20d060SBarry Smith 
1050ef20d060SBarry Smith +   t - current timestep
1051ef20d060SBarry Smith .   u - output vector
1052ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1053ef20d060SBarry Smith 
10540ed3bfb6SBarry Smith     Options Database:
10550ed3bfb6SBarry Smith +  -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction()
10560ed3bfb6SBarry Smith -  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
10570ed3bfb6SBarry Smith 
1058abd5a294SJed Brown     Notes:
1059abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1060abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1061abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1062abd5a294SJed Brown 
10634f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1064abd5a294SJed Brown 
1065ef20d060SBarry Smith     Level: beginner
1066ef20d060SBarry Smith 
1067ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1068ef20d060SBarry Smith 
10690ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError()
1070ef20d060SBarry Smith @*/
1071ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1072ef20d060SBarry Smith {
1073ef20d060SBarry Smith   PetscErrorCode ierr;
1074ef20d060SBarry Smith   DM             dm;
1075ef20d060SBarry Smith 
1076ef20d060SBarry Smith   PetscFunctionBegin;
1077ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1078ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1079ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1080ef20d060SBarry Smith   PetscFunctionReturn(0);
1081ef20d060SBarry Smith }
1082ef20d060SBarry Smith 
10839b7cd975SBarry Smith /*@C
10849b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
10859b7cd975SBarry Smith 
10869b7cd975SBarry Smith     Logically Collective on TS
10879b7cd975SBarry Smith 
10889b7cd975SBarry Smith     Input Parameters:
10899b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1090e162b725SBarry Smith .   func - routine for evaluating the forcing function
10919b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
10920298fd71SBarry Smith           function evaluation routine (may be NULL)
10939b7cd975SBarry Smith 
10949b7cd975SBarry Smith     Calling sequence of func:
1095e162b725SBarry Smith $     func (TS ts,PetscReal t,Vec f,void *ctx);
10969b7cd975SBarry Smith 
10979b7cd975SBarry Smith +   t - current timestep
1098e162b725SBarry Smith .   f - output vector
10999b7cd975SBarry Smith -   ctx - [optional] user-defined function context
11009b7cd975SBarry Smith 
11019b7cd975SBarry Smith     Notes:
11029b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1103e162b725SBarry 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
1104e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1105e162b725SBarry Smith 
1106e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1107e162b725SBarry Smith 
1108e162b725SBarry 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
1109e162b725SBarry Smith     parameters can be passed in the ctx variable.
11109b7cd975SBarry Smith 
11119b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
11129b7cd975SBarry Smith 
11139b7cd975SBarry Smith     Level: beginner
11149b7cd975SBarry Smith 
11159b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
11169b7cd975SBarry Smith 
11179b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
11189b7cd975SBarry Smith @*/
1119e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
11209b7cd975SBarry Smith {
11219b7cd975SBarry Smith   PetscErrorCode ierr;
11229b7cd975SBarry Smith   DM             dm;
11239b7cd975SBarry Smith 
11249b7cd975SBarry Smith   PetscFunctionBegin;
11259b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11269b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1127e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
11289b7cd975SBarry Smith   PetscFunctionReturn(0);
11299b7cd975SBarry Smith }
11309b7cd975SBarry Smith 
1131d763cef2SBarry Smith /*@C
1132f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1133b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1134d763cef2SBarry Smith 
11353f9fe445SBarry Smith    Logically Collective on TS
1136d763cef2SBarry Smith 
1137d763cef2SBarry Smith    Input Parameters:
1138d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1139e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1140e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1141d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1142d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
11430298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1144d763cef2SBarry Smith 
1145f7ab8db6SBarry Smith    Calling sequence of f:
1146ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1147d763cef2SBarry Smith 
1148d763cef2SBarry Smith +  t - current timestep
1149d763cef2SBarry Smith .  u - input vector
1150e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1151e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1152d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1153d763cef2SBarry Smith 
11546cd88445SBarry Smith    Notes:
11556cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
11566cd88445SBarry Smith 
11576cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1158ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1159d763cef2SBarry Smith 
1160d763cef2SBarry Smith    Level: beginner
1161d763cef2SBarry Smith 
1162d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1163d763cef2SBarry Smith 
1164ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1165d763cef2SBarry Smith 
1166d763cef2SBarry Smith @*/
1167e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1168d763cef2SBarry Smith {
1169277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1170089b2837SJed Brown   SNES           snes;
117124989b8cSPeter Brune   DM             dm;
117224989b8cSPeter Brune   TSIJacobian    ijacobian;
1173277b19d0SLisandro Dalcin 
1174d763cef2SBarry Smith   PetscFunctionBegin;
11750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1176e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1177e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1178e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1179e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1180d763cef2SBarry Smith 
118124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
118224989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1183e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1184e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1185e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1186e1244c69SJed Brown   }
11870298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1188089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11895f659677SPeter Brune   if (!ijacobian) {
1190e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
11910e4ef248SJed Brown   }
1192e5d3d808SBarry Smith   if (Amat) {
1193e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
11940e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1195e5d3d808SBarry Smith     ts->Arhs = Amat;
11960e4ef248SJed Brown   }
1197e5d3d808SBarry Smith   if (Pmat) {
1198e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
11990e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1200e5d3d808SBarry Smith     ts->Brhs = Pmat;
12010e4ef248SJed Brown   }
1202d763cef2SBarry Smith   PetscFunctionReturn(0);
1203d763cef2SBarry Smith }
1204d763cef2SBarry Smith 
1205316643e7SJed Brown /*@C
1206b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1207316643e7SJed Brown 
12083f9fe445SBarry Smith    Logically Collective on TS
1209316643e7SJed Brown 
1210316643e7SJed Brown    Input Parameters:
1211316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12120298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1213316643e7SJed Brown .  f   - the function evaluation routine
12140298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1215316643e7SJed Brown 
1216316643e7SJed Brown    Calling sequence of f:
1217316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1218316643e7SJed Brown 
1219316643e7SJed Brown +  t   - time at step/stage being solved
1220316643e7SJed Brown .  u   - state vector
1221316643e7SJed Brown .  u_t - time derivative of state vector
1222316643e7SJed Brown .  F   - function vector
1223316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1224316643e7SJed Brown 
1225316643e7SJed Brown    Important:
12262bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1227316643e7SJed Brown 
1228316643e7SJed Brown    Level: beginner
1229316643e7SJed Brown 
1230316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1231316643e7SJed Brown 
1232d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1233316643e7SJed Brown @*/
123451699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1235316643e7SJed Brown {
1236089b2837SJed Brown   PetscErrorCode ierr;
1237089b2837SJed Brown   SNES           snes;
123851699248SLisandro Dalcin   Vec            ralloc = NULL;
123924989b8cSPeter Brune   DM             dm;
1240316643e7SJed Brown 
1241316643e7SJed Brown   PetscFunctionBegin;
12420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
124351699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
124424989b8cSPeter Brune 
124524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
124624989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
124724989b8cSPeter Brune 
1248089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
124951699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
125051699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
125151699248SLisandro Dalcin     r  = ralloc;
1252e856ceecSJed Brown   }
125351699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
125451699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1255089b2837SJed Brown   PetscFunctionReturn(0);
1256089b2837SJed Brown }
1257089b2837SJed Brown 
1258089b2837SJed Brown /*@C
1259089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1260089b2837SJed Brown 
1261089b2837SJed Brown    Not Collective
1262089b2837SJed Brown 
1263089b2837SJed Brown    Input Parameter:
1264089b2837SJed Brown .  ts - the TS context
1265089b2837SJed Brown 
1266089b2837SJed Brown    Output Parameter:
12670298fd71SBarry Smith +  r - vector to hold residual (or NULL)
12680298fd71SBarry Smith .  func - the function to compute residual (or NULL)
12690298fd71SBarry Smith -  ctx - the function context (or NULL)
1270089b2837SJed Brown 
1271089b2837SJed Brown    Level: advanced
1272089b2837SJed Brown 
1273089b2837SJed Brown .keywords: TS, nonlinear, get, function
1274089b2837SJed Brown 
1275089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1276089b2837SJed Brown @*/
1277089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1278089b2837SJed Brown {
1279089b2837SJed Brown   PetscErrorCode ierr;
1280089b2837SJed Brown   SNES           snes;
128124989b8cSPeter Brune   DM             dm;
1282089b2837SJed Brown 
1283089b2837SJed Brown   PetscFunctionBegin;
1284089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1285089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12860298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
128724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
128824989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1289089b2837SJed Brown   PetscFunctionReturn(0);
1290089b2837SJed Brown }
1291089b2837SJed Brown 
1292089b2837SJed Brown /*@C
1293089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1294089b2837SJed Brown 
1295089b2837SJed Brown    Not Collective
1296089b2837SJed Brown 
1297089b2837SJed Brown    Input Parameter:
1298089b2837SJed Brown .  ts - the TS context
1299089b2837SJed Brown 
1300089b2837SJed Brown    Output Parameter:
13010298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13020298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13030298fd71SBarry Smith -  ctx - the function context (or NULL)
1304089b2837SJed Brown 
1305089b2837SJed Brown    Level: advanced
1306089b2837SJed Brown 
1307089b2837SJed Brown .keywords: TS, nonlinear, get, function
1308089b2837SJed Brown 
13092bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1310089b2837SJed Brown @*/
1311089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1312089b2837SJed Brown {
1313089b2837SJed Brown   PetscErrorCode ierr;
1314089b2837SJed Brown   SNES           snes;
131524989b8cSPeter Brune   DM             dm;
1316089b2837SJed Brown 
1317089b2837SJed Brown   PetscFunctionBegin;
1318089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1319089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13200298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
132124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
132224989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1323316643e7SJed Brown   PetscFunctionReturn(0);
1324316643e7SJed Brown }
1325316643e7SJed Brown 
1326316643e7SJed Brown /*@C
1327a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1328ae8867d6SBarry Smith         provided with TSSetIFunction().
1329316643e7SJed Brown 
13303f9fe445SBarry Smith    Logically Collective on TS
1331316643e7SJed Brown 
1332316643e7SJed Brown    Input Parameters:
1333316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1334e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1335e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1336316643e7SJed Brown .  f   - the Jacobian evaluation routine
13370298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1338316643e7SJed Brown 
1339316643e7SJed Brown    Calling sequence of f:
1340ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1341316643e7SJed Brown 
1342316643e7SJed Brown +  t    - time at step/stage being solved
13431b4a444bSJed Brown .  U    - state vector
13441b4a444bSJed Brown .  U_t  - time derivative of state vector
1345316643e7SJed Brown .  a    - shift
1346e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1347e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1348316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1349316643e7SJed Brown 
1350316643e7SJed Brown    Notes:
1351e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1352316643e7SJed Brown 
1353895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1354895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1355895c21f2SBarry Smith 
1356a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1357b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1358a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1359a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1360a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1361a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1362a4f0a591SBarry Smith 
13636cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
13646cd88445SBarry Smith 
13656cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1366ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1367ca5f011dSBarry Smith 
1368316643e7SJed Brown    Level: beginner
1369316643e7SJed Brown 
1370316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1371316643e7SJed Brown 
1372ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1373316643e7SJed Brown 
1374316643e7SJed Brown @*/
1375e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1376316643e7SJed Brown {
1377316643e7SJed Brown   PetscErrorCode ierr;
1378089b2837SJed Brown   SNES           snes;
137924989b8cSPeter Brune   DM             dm;
1380316643e7SJed Brown 
1381316643e7SJed Brown   PetscFunctionBegin;
13820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1383e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1384e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1385e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1386e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
138724989b8cSPeter Brune 
138824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
138924989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
139024989b8cSPeter Brune 
1391089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1392e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1393316643e7SJed Brown   PetscFunctionReturn(0);
1394316643e7SJed Brown }
1395316643e7SJed Brown 
1396e1244c69SJed Brown /*@
1397e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1398e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1399e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1400e1244c69SJed Brown    not been changed by the TS.
1401e1244c69SJed Brown 
1402e1244c69SJed Brown    Logically Collective
1403e1244c69SJed Brown 
1404e1244c69SJed Brown    Input Arguments:
1405e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1406e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1407e1244c69SJed Brown 
1408e1244c69SJed Brown    Level: intermediate
1409e1244c69SJed Brown 
1410e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1411e1244c69SJed Brown @*/
1412e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1413e1244c69SJed Brown {
1414e1244c69SJed Brown   PetscFunctionBegin;
1415e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1416e1244c69SJed Brown   PetscFunctionReturn(0);
1417e1244c69SJed Brown }
1418e1244c69SJed Brown 
1419efe9872eSLisandro Dalcin /*@C
1420efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1421efe9872eSLisandro Dalcin 
1422efe9872eSLisandro Dalcin    Logically Collective on TS
1423efe9872eSLisandro Dalcin 
1424efe9872eSLisandro Dalcin    Input Parameters:
1425efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1426efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1427efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1428efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1429efe9872eSLisandro Dalcin 
1430efe9872eSLisandro Dalcin    Calling sequence of fun:
1431efe9872eSLisandro Dalcin $  fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1432efe9872eSLisandro Dalcin 
1433efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1434efe9872eSLisandro Dalcin .  U    - state vector
1435efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1436efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1437efe9872eSLisandro Dalcin .  F    - function vector
1438efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1439efe9872eSLisandro Dalcin 
1440efe9872eSLisandro Dalcin    Level: beginner
1441efe9872eSLisandro Dalcin 
1442efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function
1443efe9872eSLisandro Dalcin 
1444efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian()
1445efe9872eSLisandro Dalcin @*/
1446efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1447efe9872eSLisandro Dalcin {
1448efe9872eSLisandro Dalcin   DM             dm;
1449efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1450efe9872eSLisandro Dalcin 
1451efe9872eSLisandro Dalcin   PetscFunctionBegin;
1452efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1453efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1454efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1455efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1456efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1457efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1458efe9872eSLisandro Dalcin }
1459efe9872eSLisandro Dalcin 
1460efe9872eSLisandro Dalcin /*@C
1461efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1462efe9872eSLisandro Dalcin 
1463efe9872eSLisandro Dalcin   Not Collective
1464efe9872eSLisandro Dalcin 
1465efe9872eSLisandro Dalcin   Input Parameter:
1466efe9872eSLisandro Dalcin . ts - the TS context
1467efe9872eSLisandro Dalcin 
1468efe9872eSLisandro Dalcin   Output Parameter:
1469efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1470efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1471efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1472efe9872eSLisandro Dalcin 
1473efe9872eSLisandro Dalcin   Level: advanced
1474efe9872eSLisandro Dalcin 
1475efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function
1476efe9872eSLisandro Dalcin 
1477efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction()
1478efe9872eSLisandro Dalcin @*/
1479efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1480efe9872eSLisandro Dalcin {
1481efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1482efe9872eSLisandro Dalcin   SNES           snes;
1483efe9872eSLisandro Dalcin   DM             dm;
1484efe9872eSLisandro Dalcin 
1485efe9872eSLisandro Dalcin   PetscFunctionBegin;
1486efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1487efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1488efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1489efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1490efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1491efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1492efe9872eSLisandro Dalcin }
1493efe9872eSLisandro Dalcin 
1494efe9872eSLisandro Dalcin /*@C
1495bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1496efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1497efe9872eSLisandro Dalcin 
1498efe9872eSLisandro Dalcin    Logically Collective on TS
1499efe9872eSLisandro Dalcin 
1500efe9872eSLisandro Dalcin    Input Parameters:
1501efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1502efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1503efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1504efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1505efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1506efe9872eSLisandro Dalcin 
1507efe9872eSLisandro Dalcin    Calling sequence of jac:
1508bc77d74cSLisandro Dalcin $  jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx);
1509efe9872eSLisandro Dalcin 
1510efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1511efe9872eSLisandro Dalcin .  U    - state vector
1512efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1513efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1514efe9872eSLisandro Dalcin .  v    - shift for U_t
1515efe9872eSLisandro Dalcin .  a    - shift for U_tt
1516efe9872eSLisandro 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
1517efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1518efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1519efe9872eSLisandro Dalcin 
1520efe9872eSLisandro Dalcin    Notes:
1521efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1522efe9872eSLisandro Dalcin 
1523efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1524efe9872eSLisandro 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.
1525efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1526bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1527efe9872eSLisandro Dalcin 
1528efe9872eSLisandro Dalcin    Level: beginner
1529efe9872eSLisandro Dalcin 
1530efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian
1531efe9872eSLisandro Dalcin 
1532efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1533efe9872eSLisandro Dalcin @*/
1534efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1535efe9872eSLisandro Dalcin {
1536efe9872eSLisandro Dalcin   DM             dm;
1537efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1538efe9872eSLisandro Dalcin 
1539efe9872eSLisandro Dalcin   PetscFunctionBegin;
1540efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1541efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1542efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1543efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1544efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1545efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1546efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1547efe9872eSLisandro Dalcin }
1548efe9872eSLisandro Dalcin 
1549efe9872eSLisandro Dalcin /*@C
1550efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1551efe9872eSLisandro Dalcin 
1552efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1553efe9872eSLisandro Dalcin 
1554efe9872eSLisandro Dalcin   Input Parameter:
1555efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1556efe9872eSLisandro Dalcin 
1557efe9872eSLisandro Dalcin   Output Parameters:
1558efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1559efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1560efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1561efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1562efe9872eSLisandro Dalcin 
156395452b02SPatrick Sanan   Notes:
156495452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
1565efe9872eSLisandro Dalcin 
1566efe9872eSLisandro Dalcin   Level: advanced
1567efe9872eSLisandro Dalcin 
156880275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
1569efe9872eSLisandro Dalcin 
1570efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian
1571efe9872eSLisandro Dalcin @*/
1572efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1573efe9872eSLisandro Dalcin {
1574efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1575efe9872eSLisandro Dalcin   SNES           snes;
1576efe9872eSLisandro Dalcin   DM             dm;
1577efe9872eSLisandro Dalcin 
1578efe9872eSLisandro Dalcin   PetscFunctionBegin;
1579efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1580efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1581efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1582efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1583efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1584efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1585efe9872eSLisandro Dalcin }
1586efe9872eSLisandro Dalcin 
1587efe9872eSLisandro Dalcin /*@
1588efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1589efe9872eSLisandro Dalcin 
1590efe9872eSLisandro Dalcin   Collective on TS and Vec
1591efe9872eSLisandro Dalcin 
1592efe9872eSLisandro Dalcin   Input Parameters:
1593efe9872eSLisandro Dalcin + ts - the TS context
1594efe9872eSLisandro Dalcin . t - current time
1595efe9872eSLisandro Dalcin . U - state vector
1596efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1597efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1598efe9872eSLisandro Dalcin 
1599efe9872eSLisandro Dalcin   Output Parameter:
1600efe9872eSLisandro Dalcin . F - the residual vector
1601efe9872eSLisandro Dalcin 
1602efe9872eSLisandro Dalcin   Note:
1603efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1604efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1605efe9872eSLisandro Dalcin 
1606efe9872eSLisandro Dalcin   Level: developer
1607efe9872eSLisandro Dalcin 
1608efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector
1609efe9872eSLisandro Dalcin 
1610efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1611efe9872eSLisandro Dalcin @*/
1612efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1613efe9872eSLisandro Dalcin {
1614efe9872eSLisandro Dalcin   DM             dm;
1615efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1616efe9872eSLisandro Dalcin   void           *ctx;
1617efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1618efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1619efe9872eSLisandro Dalcin 
1620efe9872eSLisandro Dalcin   PetscFunctionBegin;
1621efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1622efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1623efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1624efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1625efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1626efe9872eSLisandro Dalcin 
1627efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1628efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1629efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1630efe9872eSLisandro Dalcin 
1631efe9872eSLisandro Dalcin   if (!I2Function) {
1632efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1633efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1634efe9872eSLisandro Dalcin   }
1635efe9872eSLisandro Dalcin 
1636efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1637efe9872eSLisandro Dalcin 
1638efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1639efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1640efe9872eSLisandro Dalcin   PetscStackPop;
1641efe9872eSLisandro Dalcin 
1642efe9872eSLisandro Dalcin   if (rhsfunction) {
1643efe9872eSLisandro Dalcin     Vec Frhs;
1644efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1645efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1646efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1647efe9872eSLisandro Dalcin   }
1648efe9872eSLisandro Dalcin 
1649efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1650efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1651efe9872eSLisandro Dalcin }
1652efe9872eSLisandro Dalcin 
1653efe9872eSLisandro Dalcin /*@
1654efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1655efe9872eSLisandro Dalcin 
1656efe9872eSLisandro Dalcin   Collective on TS and Vec
1657efe9872eSLisandro Dalcin 
1658efe9872eSLisandro Dalcin   Input Parameters:
1659efe9872eSLisandro Dalcin + ts - the TS context
1660efe9872eSLisandro Dalcin . t - current timestep
1661efe9872eSLisandro Dalcin . U - state vector
1662efe9872eSLisandro Dalcin . V - time derivative of state vector
1663efe9872eSLisandro Dalcin . A - second time derivative of state vector
1664efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1665efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1666efe9872eSLisandro Dalcin 
1667efe9872eSLisandro Dalcin   Output Parameters:
1668efe9872eSLisandro Dalcin + J - Jacobian matrix
1669efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1670efe9872eSLisandro Dalcin 
1671efe9872eSLisandro Dalcin   Notes:
1672efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1673efe9872eSLisandro Dalcin 
1674efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1675efe9872eSLisandro Dalcin 
1676efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1677efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1678efe9872eSLisandro Dalcin 
1679efe9872eSLisandro Dalcin   Level: developer
1680efe9872eSLisandro Dalcin 
1681efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix
1682efe9872eSLisandro Dalcin 
1683efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1684efe9872eSLisandro Dalcin @*/
1685efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1686efe9872eSLisandro Dalcin {
1687efe9872eSLisandro Dalcin   DM             dm;
1688efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1689efe9872eSLisandro Dalcin   void           *ctx;
1690efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1691efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1692efe9872eSLisandro Dalcin 
1693efe9872eSLisandro Dalcin   PetscFunctionBegin;
1694efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1695efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1696efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1697efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1698efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1699efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1700efe9872eSLisandro Dalcin 
1701efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1702efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1703efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1704efe9872eSLisandro Dalcin 
1705efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1706efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1707efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1708efe9872eSLisandro Dalcin   }
1709efe9872eSLisandro Dalcin 
1710efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1711efe9872eSLisandro Dalcin 
1712efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1713efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1714efe9872eSLisandro Dalcin   PetscStackPop;
1715efe9872eSLisandro Dalcin 
1716efe9872eSLisandro Dalcin   if (rhsjacobian) {
1717efe9872eSLisandro Dalcin     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1718efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1719efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1720efe9872eSLisandro Dalcin     ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr);
1721efe9872eSLisandro Dalcin     if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);}
1722efe9872eSLisandro Dalcin   }
1723efe9872eSLisandro Dalcin 
1724efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1725efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1726efe9872eSLisandro Dalcin }
1727efe9872eSLisandro Dalcin 
1728efe9872eSLisandro Dalcin /*@
1729efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1730efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1731efe9872eSLisandro Dalcin 
1732efe9872eSLisandro Dalcin    Logically Collective on TS and Vec
1733efe9872eSLisandro Dalcin 
1734efe9872eSLisandro Dalcin    Input Parameters:
1735efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1736efe9872eSLisandro Dalcin .  u - the solution vector
1737efe9872eSLisandro Dalcin -  v - the time derivative vector
1738efe9872eSLisandro Dalcin 
1739efe9872eSLisandro Dalcin    Level: beginner
1740efe9872eSLisandro Dalcin 
1741efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions
1742efe9872eSLisandro Dalcin @*/
1743efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1744efe9872eSLisandro Dalcin {
1745efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1746efe9872eSLisandro Dalcin 
1747efe9872eSLisandro Dalcin   PetscFunctionBegin;
1748efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1749efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1750efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1751efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1752efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1753efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1754efe9872eSLisandro Dalcin   ts->vec_dot = v;
1755efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1756efe9872eSLisandro Dalcin }
1757efe9872eSLisandro Dalcin 
1758efe9872eSLisandro Dalcin /*@
1759efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1760efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1761efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1762efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1763efe9872eSLisandro Dalcin 
1764efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1765efe9872eSLisandro Dalcin 
1766efe9872eSLisandro Dalcin    Input Parameter:
1767efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1768efe9872eSLisandro Dalcin 
1769efe9872eSLisandro Dalcin    Output Parameter:
1770efe9872eSLisandro Dalcin +  u - the vector containing the solution
1771efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1772efe9872eSLisandro Dalcin 
1773efe9872eSLisandro Dalcin    Level: intermediate
1774efe9872eSLisandro Dalcin 
1775efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1776efe9872eSLisandro Dalcin 
1777efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution
1778efe9872eSLisandro Dalcin @*/
1779efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1780efe9872eSLisandro Dalcin {
1781efe9872eSLisandro Dalcin   PetscFunctionBegin;
1782efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1783efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1784efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1785efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1786efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1787efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1788efe9872eSLisandro Dalcin }
1789efe9872eSLisandro Dalcin 
179055849f57SBarry Smith /*@C
179155849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
179255849f57SBarry Smith 
179355849f57SBarry Smith   Collective on PetscViewer
179455849f57SBarry Smith 
179555849f57SBarry Smith   Input Parameters:
179655849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
179755849f57SBarry Smith            some related function before a call to TSLoad().
179855849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
179955849f57SBarry Smith 
180055849f57SBarry Smith    Level: intermediate
180155849f57SBarry Smith 
180255849f57SBarry Smith   Notes:
180355849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
180455849f57SBarry Smith 
180555849f57SBarry Smith   Notes for advanced users:
180655849f57SBarry Smith   Most users should not need to know the details of the binary storage
180755849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
180855849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
180955849f57SBarry Smith   format is
181055849f57SBarry Smith .vb
181155849f57SBarry Smith      has not yet been determined
181255849f57SBarry Smith .ve
181355849f57SBarry Smith 
181455849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
181555849f57SBarry Smith @*/
1816f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
181755849f57SBarry Smith {
181855849f57SBarry Smith   PetscErrorCode ierr;
181955849f57SBarry Smith   PetscBool      isbinary;
182055849f57SBarry Smith   PetscInt       classid;
182155849f57SBarry Smith   char           type[256];
18222d53ad75SBarry Smith   DMTS           sdm;
1823ad6bc421SBarry Smith   DM             dm;
182455849f57SBarry Smith 
182555849f57SBarry Smith   PetscFunctionBegin;
1826f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
182755849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
182855849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
182955849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
183055849f57SBarry Smith 
1831060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1832ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1833060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1834f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1835f2c2a1b9SBarry Smith   if (ts->ops->load) {
1836f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1837f2c2a1b9SBarry Smith   }
1838ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1839ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1840ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1841f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1842f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
18432d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
18442d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
184555849f57SBarry Smith   PetscFunctionReturn(0);
184655849f57SBarry Smith }
184755849f57SBarry Smith 
18489804daf3SBarry Smith #include <petscdraw.h>
1849e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1850e04113cfSBarry Smith #include <petscviewersaws.h>
1851f05ece33SBarry Smith #endif
18527e2c5f70SBarry Smith /*@C
1853d763cef2SBarry Smith     TSView - Prints the TS data structure.
1854d763cef2SBarry Smith 
18554c49b128SBarry Smith     Collective on TS
1856d763cef2SBarry Smith 
1857d763cef2SBarry Smith     Input Parameters:
1858d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1859d763cef2SBarry Smith -   viewer - visualization context
1860d763cef2SBarry Smith 
1861d763cef2SBarry Smith     Options Database Key:
1862d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1863d763cef2SBarry Smith 
1864d763cef2SBarry Smith     Notes:
1865d763cef2SBarry Smith     The available visualization contexts include
1866b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1867b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1868d763cef2SBarry Smith          output where only the first processor opens
1869d763cef2SBarry Smith          the file.  All other processors send their
1870d763cef2SBarry Smith          data to the first processor to print.
1871d763cef2SBarry Smith 
1872d763cef2SBarry Smith     The user can open an alternative visualization context with
1873b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1874d763cef2SBarry Smith 
1875d763cef2SBarry Smith     Level: beginner
1876d763cef2SBarry Smith 
1877d763cef2SBarry Smith .keywords: TS, timestep, view
1878d763cef2SBarry Smith 
1879b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1880d763cef2SBarry Smith @*/
18817087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1882d763cef2SBarry Smith {
1883dfbe8321SBarry Smith   PetscErrorCode ierr;
188419fd82e9SBarry Smith   TSType         type;
18852b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
18862d53ad75SBarry Smith   DMTS           sdm;
1887e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1888536b137fSBarry Smith   PetscBool      issaws;
1889f05ece33SBarry Smith #endif
1890d763cef2SBarry Smith 
1891d763cef2SBarry Smith   PetscFunctionBegin;
18920700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
18933050cee2SBarry Smith   if (!viewer) {
1894ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
18953050cee2SBarry Smith   }
18960700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1897c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1898fd16b177SBarry Smith 
1899251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1900251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
190155849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
19022b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1903e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1904536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1905f05ece33SBarry Smith #endif
190632077d6dSBarry Smith   if (iascii) {
1907dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
1908efd4aadfSBarry Smith     if (ts->ops->view) {
1909efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1910efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1911efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1912efd4aadfSBarry Smith     }
1913ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
191477431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1915ef85077eSLisandro Dalcin     }
1916ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
19177c8652ddSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1918ef85077eSLisandro Dalcin     }
1919efd4aadfSBarry Smith     if (ts->usessnes) {
1920efd4aadfSBarry Smith       PetscBool lin;
1921d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
19225ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1923d763cef2SBarry Smith       }
19245ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1925*1ef27442SStefano Zampini       ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
1926efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
1927efd4aadfSBarry Smith     }
1928193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
1929a0af407cSBarry Smith     if (ts->vrtol) {
1930a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
1931a0af407cSBarry Smith     } else {
1932a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
1933a0af407cSBarry Smith     }
1934a0af407cSBarry Smith     if (ts->vatol) {
1935a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
1936a0af407cSBarry Smith     } else {
1937a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
1938a0af407cSBarry Smith     }
1939825ab935SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1940efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
1941825ab935SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1942825ab935SBarry Smith     if (ts->snes && ts->usessnes)  {
1943825ab935SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1944825ab935SBarry Smith       ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
1945825ab935SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1946825ab935SBarry Smith     }
19472d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19482d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
19490f5bd95cSBarry Smith   } else if (isstring) {
1950a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1951b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
195255849f57SBarry Smith   } else if (isbinary) {
195355849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
195455849f57SBarry Smith     MPI_Comm    comm;
195555849f57SBarry Smith     PetscMPIInt rank;
195655849f57SBarry Smith     char        type[256];
195755849f57SBarry Smith 
195855849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
195955849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
196055849f57SBarry Smith     if (!rank) {
196155849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
196255849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
196355849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
196455849f57SBarry Smith     }
196555849f57SBarry Smith     if (ts->ops->view) {
196655849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
196755849f57SBarry Smith     }
1968efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
1969f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1970f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
19712d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19722d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
19732b0a91c0SBarry Smith   } else if (isdraw) {
19742b0a91c0SBarry Smith     PetscDraw draw;
19752b0a91c0SBarry Smith     char      str[36];
197689fd9fafSBarry Smith     PetscReal x,y,bottom,h;
19772b0a91c0SBarry Smith 
19782b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
19792b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
19802b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
19812b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
198251fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
198389fd9fafSBarry Smith     bottom = y - h;
19842b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
19852b0a91c0SBarry Smith     if (ts->ops->view) {
19862b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
19872b0a91c0SBarry Smith     }
1988efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
1989efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
19902b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1991e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1992536b137fSBarry Smith   } else if (issaws) {
1993d45a07a7SBarry Smith     PetscMPIInt rank;
19942657e9d9SBarry Smith     const char  *name;
19952657e9d9SBarry Smith 
19962657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
1997d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1998d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
1999d45a07a7SBarry Smith       char       dir[1024];
2000d45a07a7SBarry Smith 
2001e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2002a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
20032657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
20042657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
20052657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2006d763cef2SBarry Smith     }
20070acecf5bSBarry Smith     if (ts->ops->view) {
20080acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
20090acecf5bSBarry Smith     }
2010f05ece33SBarry Smith #endif
2011f05ece33SBarry Smith   }
2012f05ece33SBarry Smith 
2013b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2014251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2015b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2016d763cef2SBarry Smith   PetscFunctionReturn(0);
2017d763cef2SBarry Smith }
2018d763cef2SBarry Smith 
2019b07ff414SBarry Smith /*@
2020d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2021d763cef2SBarry Smith    the timesteppers.
2022d763cef2SBarry Smith 
20233f9fe445SBarry Smith    Logically Collective on TS
2024d763cef2SBarry Smith 
2025d763cef2SBarry Smith    Input Parameters:
2026d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2027d763cef2SBarry Smith -  usrP - optional user context
2028d763cef2SBarry Smith 
202995452b02SPatrick Sanan    Fortran Notes:
203095452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2031daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2032daf670e6SBarry Smith 
2033d763cef2SBarry Smith    Level: intermediate
2034d763cef2SBarry Smith 
2035d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
2036d763cef2SBarry Smith 
2037d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2038d763cef2SBarry Smith @*/
20397087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2040d763cef2SBarry Smith {
2041d763cef2SBarry Smith   PetscFunctionBegin;
20420700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2043d763cef2SBarry Smith   ts->user = usrP;
2044d763cef2SBarry Smith   PetscFunctionReturn(0);
2045d763cef2SBarry Smith }
2046d763cef2SBarry Smith 
2047b07ff414SBarry Smith /*@
2048d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2049d763cef2SBarry Smith     timestepper.
2050d763cef2SBarry Smith 
2051d763cef2SBarry Smith     Not Collective
2052d763cef2SBarry Smith 
2053d763cef2SBarry Smith     Input Parameter:
2054d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2055d763cef2SBarry Smith 
2056d763cef2SBarry Smith     Output Parameter:
2057d763cef2SBarry Smith .   usrP - user context
2058d763cef2SBarry Smith 
205995452b02SPatrick Sanan    Fortran Notes:
206095452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2061daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2062daf670e6SBarry Smith 
2063d763cef2SBarry Smith     Level: intermediate
2064d763cef2SBarry Smith 
2065d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
2066d763cef2SBarry Smith 
2067d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2068d763cef2SBarry Smith @*/
2069e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2070d763cef2SBarry Smith {
2071d763cef2SBarry Smith   PetscFunctionBegin;
20720700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2073e71120c6SJed Brown   *(void**)usrP = ts->user;
2074d763cef2SBarry Smith   PetscFunctionReturn(0);
2075d763cef2SBarry Smith }
2076d763cef2SBarry Smith 
2077d763cef2SBarry Smith /*@
207880275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2079d763cef2SBarry Smith 
2080d763cef2SBarry Smith    Not Collective
2081d763cef2SBarry Smith 
2082d763cef2SBarry Smith    Input Parameter:
2083d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2084d763cef2SBarry Smith 
2085d763cef2SBarry Smith    Output Parameter:
208680275a0aSLisandro Dalcin .  steps - number of steps completed so far
2087d763cef2SBarry Smith 
2088d763cef2SBarry Smith    Level: intermediate
2089d763cef2SBarry Smith 
2090d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
20919be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2092d763cef2SBarry Smith @*/
209380275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2094d763cef2SBarry Smith {
2095d763cef2SBarry Smith   PetscFunctionBegin;
20960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
209780275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
209880275a0aSLisandro Dalcin   *steps = ts->steps;
209980275a0aSLisandro Dalcin   PetscFunctionReturn(0);
210080275a0aSLisandro Dalcin }
210180275a0aSLisandro Dalcin 
210280275a0aSLisandro Dalcin /*@
210380275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
210480275a0aSLisandro Dalcin 
210580275a0aSLisandro Dalcin    Logically Collective on TS
210680275a0aSLisandro Dalcin 
210780275a0aSLisandro Dalcin    Input Parameters:
210880275a0aSLisandro Dalcin +  ts - the TS context
210980275a0aSLisandro Dalcin -  steps - number of steps completed so far
211080275a0aSLisandro Dalcin 
211180275a0aSLisandro Dalcin    Notes:
211280275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
211380275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
211480275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
211580275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
211680275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
211780275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
211880275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
211980275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
212080275a0aSLisandro Dalcin 
212180275a0aSLisandro Dalcin    Level: advanced
212280275a0aSLisandro Dalcin 
212380275a0aSLisandro Dalcin .keywords: TS, timestep, set, iteration, number
212480275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
212580275a0aSLisandro Dalcin @*/
212680275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
212780275a0aSLisandro Dalcin {
212880275a0aSLisandro Dalcin   PetscFunctionBegin;
212980275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
213080275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
213180275a0aSLisandro Dalcin   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
213280275a0aSLisandro Dalcin   ts->steps = steps;
2133d763cef2SBarry Smith   PetscFunctionReturn(0);
2134d763cef2SBarry Smith }
2135d763cef2SBarry Smith 
2136d763cef2SBarry Smith /*@
2137d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2138d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2139d763cef2SBarry Smith 
21403f9fe445SBarry Smith    Logically Collective on TS
2141d763cef2SBarry Smith 
2142d763cef2SBarry Smith    Input Parameters:
2143d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2144d763cef2SBarry Smith -  time_step - the size of the timestep
2145d763cef2SBarry Smith 
2146d763cef2SBarry Smith    Level: intermediate
2147d763cef2SBarry Smith 
2148aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2149d763cef2SBarry Smith 
2150d763cef2SBarry Smith .keywords: TS, set, timestep
2151d763cef2SBarry Smith @*/
21527087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2153d763cef2SBarry Smith {
2154d763cef2SBarry Smith   PetscFunctionBegin;
21550700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2156c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2157d763cef2SBarry Smith   ts->time_step = time_step;
2158d763cef2SBarry Smith   PetscFunctionReturn(0);
2159d763cef2SBarry Smith }
2160d763cef2SBarry Smith 
2161a43b19c4SJed Brown /*@
216249354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
216349354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
216449354f04SShri Abhyankar      or just return at the final time TS computed.
2165a43b19c4SJed Brown 
2166a43b19c4SJed Brown   Logically Collective on TS
2167a43b19c4SJed Brown 
2168a43b19c4SJed Brown    Input Parameter:
2169a43b19c4SJed Brown +   ts - the time-step context
217049354f04SShri Abhyankar -   eftopt - exact final time option
2171a43b19c4SJed Brown 
2172feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2173feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2174feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2175feed9e9dSBarry Smith 
2176feed9e9dSBarry Smith    Options Database:
2177feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2178feed9e9dSBarry Smith 
2179ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2180ee346746SBarry Smith     then the final time you selected.
2181ee346746SBarry Smith 
2182a43b19c4SJed Brown    Level: beginner
2183a43b19c4SJed Brown 
2184f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2185a43b19c4SJed Brown @*/
218649354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2187a43b19c4SJed Brown {
2188a43b19c4SJed Brown   PetscFunctionBegin;
2189a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
219049354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
219149354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2192a43b19c4SJed Brown   PetscFunctionReturn(0);
2193a43b19c4SJed Brown }
2194a43b19c4SJed Brown 
2195d763cef2SBarry Smith /*@
2196f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2197f6953c82SLisandro Dalcin 
2198f6953c82SLisandro Dalcin    Not Collective
2199f6953c82SLisandro Dalcin 
2200f6953c82SLisandro Dalcin    Input Parameter:
2201f6953c82SLisandro Dalcin .  ts - the TS context
2202f6953c82SLisandro Dalcin 
2203f6953c82SLisandro Dalcin    Output Parameter:
2204f6953c82SLisandro Dalcin .  eftopt - exact final time option
2205f6953c82SLisandro Dalcin 
2206f6953c82SLisandro Dalcin    Level: beginner
2207f6953c82SLisandro Dalcin 
2208f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2209f6953c82SLisandro Dalcin @*/
2210f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2211f6953c82SLisandro Dalcin {
2212f6953c82SLisandro Dalcin   PetscFunctionBegin;
2213f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2214f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2215f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2216f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2217f6953c82SLisandro Dalcin }
2218f6953c82SLisandro Dalcin 
2219f6953c82SLisandro Dalcin /*@
2220d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2221d763cef2SBarry Smith 
2222d763cef2SBarry Smith    Not Collective
2223d763cef2SBarry Smith 
2224d763cef2SBarry Smith    Input Parameter:
2225d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2226d763cef2SBarry Smith 
2227d763cef2SBarry Smith    Output Parameter:
2228d763cef2SBarry Smith .  dt - the current timestep size
2229d763cef2SBarry Smith 
2230d763cef2SBarry Smith    Level: intermediate
2231d763cef2SBarry Smith 
2232aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2233d763cef2SBarry Smith 
2234d763cef2SBarry Smith .keywords: TS, get, timestep
2235d763cef2SBarry Smith @*/
22367087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2237d763cef2SBarry Smith {
2238d763cef2SBarry Smith   PetscFunctionBegin;
22390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2240f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2241d763cef2SBarry Smith   *dt = ts->time_step;
2242d763cef2SBarry Smith   PetscFunctionReturn(0);
2243d763cef2SBarry Smith }
2244d763cef2SBarry Smith 
2245d8e5e3e6SSatish Balay /*@
2246d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2247d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2248d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2249d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2250d763cef2SBarry Smith 
2251d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2252d763cef2SBarry Smith 
2253d763cef2SBarry Smith    Input Parameter:
2254d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2255d763cef2SBarry Smith 
2256d763cef2SBarry Smith    Output Parameter:
2257d763cef2SBarry Smith .  v - the vector containing the solution
2258d763cef2SBarry Smith 
225963e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
226063e21af5SBarry Smith    final time. It returns the solution at the next timestep.
226163e21af5SBarry Smith 
2262d763cef2SBarry Smith    Level: intermediate
2263d763cef2SBarry Smith 
22640ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2265d763cef2SBarry Smith 
2266d763cef2SBarry Smith .keywords: TS, timestep, get, solution
2267d763cef2SBarry Smith @*/
22687087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2269d763cef2SBarry Smith {
2270d763cef2SBarry Smith   PetscFunctionBegin;
22710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22724482741eSBarry Smith   PetscValidPointer(v,2);
22738737fe31SLisandro Dalcin   *v = ts->vec_sol;
2274d763cef2SBarry Smith   PetscFunctionReturn(0);
2275d763cef2SBarry Smith }
2276d763cef2SBarry Smith 
227703fe5f5eSDebojyoti Ghosh /*@
2278b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
227903fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2280b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
228103fe5f5eSDebojyoti Ghosh    structure as the solution vector.
228203fe5f5eSDebojyoti Ghosh 
228303fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
228403fe5f5eSDebojyoti Ghosh 
228503fe5f5eSDebojyoti Ghosh    Parameters :
228603fe5f5eSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2287b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2288b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
228903fe5f5eSDebojyoti Ghosh        returned in v.
2290b2bf4f3aSDebojyoti Ghosh .  v - the vector containing the n-th solution component
229103fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2292b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
229303fe5f5eSDebojyoti Ghosh 
22944cdd57e5SDebojyoti Ghosh    Level: advanced
229503fe5f5eSDebojyoti Ghosh 
229603fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
229703fe5f5eSDebojyoti Ghosh 
229803fe5f5eSDebojyoti Ghosh .keywords: TS, timestep, get, solution
229903fe5f5eSDebojyoti Ghosh @*/
2300b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
230103fe5f5eSDebojyoti Ghosh {
230203fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
230303fe5f5eSDebojyoti Ghosh 
230403fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
230503fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2306b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
230703fe5f5eSDebojyoti Ghosh   else {
2308b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
230903fe5f5eSDebojyoti Ghosh   }
231003fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
231103fe5f5eSDebojyoti Ghosh }
231203fe5f5eSDebojyoti Ghosh 
23134cdd57e5SDebojyoti Ghosh /*@
23144cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
23154cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
23164cdd57e5SDebojyoti Ghosh 
23174cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
23184cdd57e5SDebojyoti Ghosh 
23194cdd57e5SDebojyoti Ghosh    Parameters :
23204cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
23214cdd57e5SDebojyoti Ghosh .  v - the vector containing the auxiliary solution
23224cdd57e5SDebojyoti Ghosh 
23234cdd57e5SDebojyoti Ghosh    Level: intermediate
23244cdd57e5SDebojyoti Ghosh 
23254cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
23264cdd57e5SDebojyoti Ghosh 
23274cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, solution
23284cdd57e5SDebojyoti Ghosh @*/
23294cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
23304cdd57e5SDebojyoti Ghosh {
23314cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
23324cdd57e5SDebojyoti Ghosh 
23334cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
23344cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2335f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
23364cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2337f6356ec7SDebojyoti Ghosh   } else {
2338f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v); CHKERRQ(ierr);
2339f6356ec7SDebojyoti Ghosh   }
23404cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
23414cdd57e5SDebojyoti Ghosh }
23424cdd57e5SDebojyoti Ghosh 
23434cdd57e5SDebojyoti Ghosh /*@
23444cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
23454cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
23464cdd57e5SDebojyoti Ghosh 
23474cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
23484cdd57e5SDebojyoti Ghosh 
23499657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
23509657682dSDebojyoti Ghosh 
23514cdd57e5SDebojyoti Ghosh    Parameters :
23524cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2353657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
23544cdd57e5SDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
23554cdd57e5SDebojyoti Ghosh 
23564cdd57e5SDebojyoti Ghosh    Level: intermediate
23574cdd57e5SDebojyoti Ghosh 
235857df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
23594cdd57e5SDebojyoti Ghosh 
23604cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, error
23614cdd57e5SDebojyoti Ghosh @*/
23620a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
23634cdd57e5SDebojyoti Ghosh {
23644cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
23654cdd57e5SDebojyoti Ghosh 
23664cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
23674cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2368f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
23690a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2370f6356ec7SDebojyoti Ghosh   } else {
2371f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2372f6356ec7SDebojyoti Ghosh   }
23734cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
23744cdd57e5SDebojyoti Ghosh }
23754cdd57e5SDebojyoti Ghosh 
237657df6a1bSDebojyoti Ghosh /*@
237757df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
237857df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
237957df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
238057df6a1bSDebojyoti Ghosh 
238157df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
238257df6a1bSDebojyoti Ghosh 
238357df6a1bSDebojyoti Ghosh    Parameters :
238457df6a1bSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
238557df6a1bSDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
238657df6a1bSDebojyoti Ghosh 
238757df6a1bSDebojyoti Ghosh    Level: intermediate
238857df6a1bSDebojyoti Ghosh 
238957df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
239057df6a1bSDebojyoti Ghosh 
239157df6a1bSDebojyoti Ghosh .keywords: TS, timestep, get, error
239257df6a1bSDebojyoti Ghosh @*/
239357df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
239457df6a1bSDebojyoti Ghosh {
239557df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
239657df6a1bSDebojyoti Ghosh 
239757df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
239857df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23999657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
240057df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
240157df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
240257df6a1bSDebojyoti Ghosh   }
240357df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
240457df6a1bSDebojyoti Ghosh }
240557df6a1bSDebojyoti Ghosh 
2406bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2407d8e5e3e6SSatish Balay /*@
2408bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2409d763cef2SBarry Smith 
2410bdad233fSMatthew Knepley   Not collective
2411d763cef2SBarry Smith 
2412bdad233fSMatthew Knepley   Input Parameters:
2413bdad233fSMatthew Knepley + ts   - The TS
2414bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2415d763cef2SBarry Smith .vb
24160910c330SBarry Smith          U_t - A U = 0      (linear)
24170910c330SBarry Smith          U_t - A(t) U = 0   (linear)
24180910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2419d763cef2SBarry Smith .ve
2420d763cef2SBarry Smith 
2421d763cef2SBarry Smith    Level: beginner
2422d763cef2SBarry Smith 
2423bdad233fSMatthew Knepley .keywords: TS, problem type
2424bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2425d763cef2SBarry Smith @*/
24267087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2427a7cc72afSBarry Smith {
24289e2a6581SJed Brown   PetscErrorCode ierr;
24299e2a6581SJed Brown 
2430d763cef2SBarry Smith   PetscFunctionBegin;
24310700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2432bdad233fSMatthew Knepley   ts->problem_type = type;
24339e2a6581SJed Brown   if (type == TS_LINEAR) {
24349e2a6581SJed Brown     SNES snes;
24359e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
24369e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
24379e2a6581SJed Brown   }
2438d763cef2SBarry Smith   PetscFunctionReturn(0);
2439d763cef2SBarry Smith }
2440d763cef2SBarry Smith 
2441bdad233fSMatthew Knepley /*@C
2442bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2443bdad233fSMatthew Knepley 
2444bdad233fSMatthew Knepley   Not collective
2445bdad233fSMatthew Knepley 
2446bdad233fSMatthew Knepley   Input Parameter:
2447bdad233fSMatthew Knepley . ts   - The TS
2448bdad233fSMatthew Knepley 
2449bdad233fSMatthew Knepley   Output Parameter:
2450bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2451bdad233fSMatthew Knepley .vb
2452089b2837SJed Brown          M U_t = A U
2453089b2837SJed Brown          M(t) U_t = A(t) U
2454b5abc632SBarry Smith          F(t,U,U_t)
2455bdad233fSMatthew Knepley .ve
2456bdad233fSMatthew Knepley 
2457bdad233fSMatthew Knepley    Level: beginner
2458bdad233fSMatthew Knepley 
2459bdad233fSMatthew Knepley .keywords: TS, problem type
2460bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2461bdad233fSMatthew Knepley @*/
24627087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2463a7cc72afSBarry Smith {
2464bdad233fSMatthew Knepley   PetscFunctionBegin;
24650700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
24664482741eSBarry Smith   PetscValidIntPointer(type,2);
2467bdad233fSMatthew Knepley   *type = ts->problem_type;
2468bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2469bdad233fSMatthew Knepley }
2470d763cef2SBarry Smith 
2471d763cef2SBarry Smith /*@
2472d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
2473d763cef2SBarry Smith    of a timestepper.
2474d763cef2SBarry Smith 
2475d763cef2SBarry Smith    Collective on TS
2476d763cef2SBarry Smith 
2477d763cef2SBarry Smith    Input Parameter:
2478d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2479d763cef2SBarry Smith 
2480d763cef2SBarry Smith    Notes:
2481d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2482d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2483141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2484d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2485141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2486d763cef2SBarry Smith 
2487d763cef2SBarry Smith    Level: advanced
2488d763cef2SBarry Smith 
2489d763cef2SBarry Smith .keywords: TS, timestep, setup
2490d763cef2SBarry Smith 
2491141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2492d763cef2SBarry Smith @*/
24937087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2494d763cef2SBarry Smith {
2495dfbe8321SBarry Smith   PetscErrorCode ierr;
24966c6b9e74SPeter Brune   DM             dm;
24976c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2498d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2499cd11d68dSLisandro Dalcin   TSIFunction    ifun;
25006c6b9e74SPeter Brune   TSIJacobian    ijac;
2501efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
25026c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
25032ffb9264SLisandro Dalcin   PetscBool      isnone;
2504d763cef2SBarry Smith 
2505d763cef2SBarry Smith   PetscFunctionBegin;
25060700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2507277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2508277b19d0SLisandro Dalcin 
25097adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2510cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2511cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2512d763cef2SBarry Smith   }
2513277b19d0SLisandro Dalcin 
2514277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
2515277b19d0SLisandro Dalcin 
2516e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
2517e1244c69SJed Brown     Mat Amat,Pmat;
2518e1244c69SJed Brown     SNES snes;
2519e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2520e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2521e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2522e1244c69SJed Brown      * have displaced the RHS matrix */
2523e1244c69SJed Brown     if (Amat == ts->Arhs) {
2524abc0d4abSBarry 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 */
2525abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2526e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2527e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2528e1244c69SJed Brown     }
2529e1244c69SJed Brown     if (Pmat == ts->Brhs) {
2530abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2531e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2532e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2533e1244c69SJed Brown     }
2534e1244c69SJed Brown   }
25352ffb9264SLisandro Dalcin 
25362ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
25372ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
25382ffb9264SLisandro Dalcin 
2539277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2540000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2541277b19d0SLisandro Dalcin   }
2542277b19d0SLisandro Dalcin 
25432ffb9264SLisandro Dalcin   /* Attempt to check/preset a default value for the exact final time option */
25442ffb9264SLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
25452ffb9264SLisandro Dalcin   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED)
25462ffb9264SLisandro Dalcin     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
25472ffb9264SLisandro Dalcin 
2548a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
25496c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
25506c6b9e74SPeter Brune    */
25516c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
25520298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
25536c6b9e74SPeter Brune   if (!func) {
25546c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
25556c6b9e74SPeter Brune   }
2556a6772fa2SLisandro 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.
25576c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
25586c6b9e74SPeter Brune    */
25590298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
25600298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2561efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
25620298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2563efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
25646c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
25656c6b9e74SPeter Brune   }
2566c0517034SDebojyoti Ghosh 
2567c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2568c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2569c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2570c0517034SDebojyoti Ghosh   }
2571c0517034SDebojyoti Ghosh 
2572277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2573277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2574277b19d0SLisandro Dalcin }
2575277b19d0SLisandro Dalcin 
2576f6a906c0SBarry Smith /*@
2577277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2578277b19d0SLisandro Dalcin 
2579277b19d0SLisandro Dalcin    Collective on TS
2580277b19d0SLisandro Dalcin 
2581277b19d0SLisandro Dalcin    Input Parameter:
2582277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2583277b19d0SLisandro Dalcin 
2584277b19d0SLisandro Dalcin    Level: beginner
2585277b19d0SLisandro Dalcin 
2586277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
2587277b19d0SLisandro Dalcin 
2588277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2589277b19d0SLisandro Dalcin @*/
2590277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2591277b19d0SLisandro Dalcin {
25921d06f6b3SHong Zhang   TS_RHSSplitLink ilink = ts->tsrhssplit,next;
2593277b19d0SLisandro Dalcin   PetscErrorCode  ierr;
2594277b19d0SLisandro Dalcin 
2595277b19d0SLisandro Dalcin   PetscFunctionBegin;
2596277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2597b18ea86cSHong Zhang 
2598277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2599277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2600277b19d0SLisandro Dalcin   }
2601277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2602e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2603bbd56ea5SKarl Rupp 
26044e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
26054e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2606214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
26076bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2608efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2609e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2610e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
261138637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2612bbd56ea5SKarl Rupp 
2613d8151eeaSBarry Smith   ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2614d8151eeaSBarry Smith   ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2615715f1b00SHong Zhang 
2616ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
26172c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
261836eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
261913b1b0ffSHong Zhang   ierr = MatDestroy(&ts->mat_sensip);CHKERRQ(ierr);
2620022c081aSHong Zhang 
26211d06f6b3SHong Zhang   while (ilink) {
26221d06f6b3SHong Zhang     next = ilink->next;
26231d06f6b3SHong Zhang     ierr = TSDestroy(&ilink->ts);CHKERRQ(ierr);
26241d06f6b3SHong Zhang     ierr = PetscFree(ilink->splitname);CHKERRQ(ierr);
26251d06f6b3SHong Zhang     ierr = ISDestroy(&ilink->is);CHKERRQ(ierr);
26261d06f6b3SHong Zhang     ierr = PetscFree(ilink);CHKERRQ(ierr);
26271d06f6b3SHong Zhang     ilink = next;
262887f4e208SHong Zhang   }
2629545aaa6fSHong Zhang   ts->num_rhs_splits = 0;
2630277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2631d763cef2SBarry Smith   PetscFunctionReturn(0);
2632d763cef2SBarry Smith }
2633d763cef2SBarry Smith 
2634d8e5e3e6SSatish Balay /*@
2635d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2636d763cef2SBarry Smith    with TSCreate().
2637d763cef2SBarry Smith 
2638d763cef2SBarry Smith    Collective on TS
2639d763cef2SBarry Smith 
2640d763cef2SBarry Smith    Input Parameter:
2641d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2642d763cef2SBarry Smith 
2643d763cef2SBarry Smith    Level: beginner
2644d763cef2SBarry Smith 
2645d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2646d763cef2SBarry Smith 
2647d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2648d763cef2SBarry Smith @*/
26496bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2650d763cef2SBarry Smith {
26516849ba73SBarry Smith   PetscErrorCode ierr;
2652d763cef2SBarry Smith 
2653d763cef2SBarry Smith   PetscFunctionBegin;
26546bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
26556bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
26566bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2657d763cef2SBarry Smith 
26586bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2659277b19d0SLisandro Dalcin 
2660e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2661e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
26626bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
26636d4c513bSLisandro Dalcin 
2664bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2665bc952696SBarry Smith 
266684df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
26676427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
26686427ac75SLisandro Dalcin 
26696bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
26706bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
26716bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
26720dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
26736d4c513bSLisandro Dalcin 
2674a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2675d763cef2SBarry Smith   PetscFunctionReturn(0);
2676d763cef2SBarry Smith }
2677d763cef2SBarry Smith 
2678d8e5e3e6SSatish Balay /*@
2679d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2680d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2681d763cef2SBarry Smith 
2682d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2683d763cef2SBarry Smith 
2684d763cef2SBarry Smith    Input Parameter:
2685d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2686d763cef2SBarry Smith 
2687d763cef2SBarry Smith    Output Parameter:
2688d763cef2SBarry Smith .  snes - the nonlinear solver context
2689d763cef2SBarry Smith 
2690d763cef2SBarry Smith    Notes:
2691d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2692d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
269394b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2694d763cef2SBarry Smith 
2695d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
26960298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2697d763cef2SBarry Smith 
2698d763cef2SBarry Smith    Level: beginner
2699d763cef2SBarry Smith 
2700d763cef2SBarry Smith .keywords: timestep, get, SNES
2701d763cef2SBarry Smith @*/
27027087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2703d763cef2SBarry Smith {
2704d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2705d372ba47SLisandro Dalcin 
2706d763cef2SBarry Smith   PetscFunctionBegin;
27070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27084482741eSBarry Smith   PetscValidPointer(snes,2);
2709d372ba47SLisandro Dalcin   if (!ts->snes) {
2710ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
271116413a6aSBarry Smith     ierr = PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options);CHKERRQ(ierr);
27120298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27133bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2714d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2715496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
27169e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
27179e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
27189e2a6581SJed Brown     }
2719d372ba47SLisandro Dalcin   }
2720d763cef2SBarry Smith   *snes = ts->snes;
2721d763cef2SBarry Smith   PetscFunctionReturn(0);
2722d763cef2SBarry Smith }
2723d763cef2SBarry Smith 
2724deb2cd25SJed Brown /*@
2725deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2726deb2cd25SJed Brown 
2727deb2cd25SJed Brown    Collective
2728deb2cd25SJed Brown 
2729deb2cd25SJed Brown    Input Parameter:
2730deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2731deb2cd25SJed Brown -  snes - the nonlinear solver context
2732deb2cd25SJed Brown 
2733deb2cd25SJed Brown    Notes:
2734deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2735deb2cd25SJed Brown 
2736deb2cd25SJed Brown    Level: developer
2737deb2cd25SJed Brown 
2738deb2cd25SJed Brown .keywords: timestep, set, SNES
2739deb2cd25SJed Brown @*/
2740deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2741deb2cd25SJed Brown {
2742deb2cd25SJed Brown   PetscErrorCode ierr;
2743d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2744deb2cd25SJed Brown 
2745deb2cd25SJed Brown   PetscFunctionBegin;
2746deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2747deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2748deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2749deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2750bbd56ea5SKarl Rupp 
2751deb2cd25SJed Brown   ts->snes = snes;
2752bbd56ea5SKarl Rupp 
27530298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27540298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2755740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
27560298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2757740132f1SEmil Constantinescu   }
2758deb2cd25SJed Brown   PetscFunctionReturn(0);
2759deb2cd25SJed Brown }
2760deb2cd25SJed Brown 
2761d8e5e3e6SSatish Balay /*@
276294b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2763d763cef2SBarry Smith    a TS (timestepper) context.
2764d763cef2SBarry Smith 
276594b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2766d763cef2SBarry Smith 
2767d763cef2SBarry Smith    Input Parameter:
2768d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2769d763cef2SBarry Smith 
2770d763cef2SBarry Smith    Output Parameter:
277194b7f48cSBarry Smith .  ksp - the nonlinear solver context
2772d763cef2SBarry Smith 
2773d763cef2SBarry Smith    Notes:
277494b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2775d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2776d763cef2SBarry Smith    KSP and PC contexts as well.
2777d763cef2SBarry Smith 
277894b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
27790298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2780d763cef2SBarry Smith 
2781d763cef2SBarry Smith    Level: beginner
2782d763cef2SBarry Smith 
278394b7f48cSBarry Smith .keywords: timestep, get, KSP
2784d763cef2SBarry Smith @*/
27857087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2786d763cef2SBarry Smith {
2787d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2788089b2837SJed Brown   SNES           snes;
2789d372ba47SLisandro Dalcin 
2790d763cef2SBarry Smith   PetscFunctionBegin;
27910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27924482741eSBarry Smith   PetscValidPointer(ksp,2);
279317186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2794e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2795089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2796089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2797d763cef2SBarry Smith   PetscFunctionReturn(0);
2798d763cef2SBarry Smith }
2799d763cef2SBarry Smith 
2800d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2801d763cef2SBarry Smith 
2802adb62b0dSMatthew Knepley /*@
2803618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
2804618ce8baSLisandro Dalcin 
2805618ce8baSLisandro Dalcin    Logically Collective on TS
2806618ce8baSLisandro Dalcin 
2807618ce8baSLisandro Dalcin    Input Parameters:
2808618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2809618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
2810618ce8baSLisandro Dalcin 
2811618ce8baSLisandro Dalcin    Options Database Keys:
2812618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
2813618ce8baSLisandro Dalcin 
2814618ce8baSLisandro Dalcin    Notes:
2815618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
2816618ce8baSLisandro Dalcin 
2817618ce8baSLisandro Dalcin    Level: intermediate
2818618ce8baSLisandro Dalcin 
2819618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, steps
2820618ce8baSLisandro Dalcin 
2821618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
2822618ce8baSLisandro Dalcin @*/
2823618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
2824618ce8baSLisandro Dalcin {
2825618ce8baSLisandro Dalcin   PetscFunctionBegin;
2826618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2827618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2828618ce8baSLisandro Dalcin   if (maxsteps < 0 ) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
2829618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
2830618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2831618ce8baSLisandro Dalcin }
2832618ce8baSLisandro Dalcin 
2833618ce8baSLisandro Dalcin /*@
2834618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
2835618ce8baSLisandro Dalcin 
2836618ce8baSLisandro Dalcin    Not Collective
2837618ce8baSLisandro Dalcin 
2838618ce8baSLisandro Dalcin    Input Parameters:
2839618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2840618ce8baSLisandro Dalcin 
2841618ce8baSLisandro Dalcin    Output Parameter:
2842618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
2843618ce8baSLisandro Dalcin 
2844618ce8baSLisandro Dalcin    Level: advanced
2845618ce8baSLisandro Dalcin 
2846618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, steps
2847618ce8baSLisandro Dalcin 
2848618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
2849618ce8baSLisandro Dalcin @*/
2850618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
2851618ce8baSLisandro Dalcin {
2852618ce8baSLisandro Dalcin   PetscFunctionBegin;
2853618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2854618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
2855618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
2856618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2857618ce8baSLisandro Dalcin }
2858618ce8baSLisandro Dalcin 
2859618ce8baSLisandro Dalcin /*@
2860618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
2861618ce8baSLisandro Dalcin 
2862618ce8baSLisandro Dalcin    Logically Collective on TS
2863618ce8baSLisandro Dalcin 
2864618ce8baSLisandro Dalcin    Input Parameters:
2865618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2866618ce8baSLisandro Dalcin -  maxtime - final time to step to
2867618ce8baSLisandro Dalcin 
2868618ce8baSLisandro Dalcin    Options Database Keys:
2869ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
2870618ce8baSLisandro Dalcin 
2871618ce8baSLisandro Dalcin    Notes:
2872618ce8baSLisandro Dalcin    The default maximum time is 5.0
2873618ce8baSLisandro Dalcin 
2874618ce8baSLisandro Dalcin    Level: intermediate
2875618ce8baSLisandro Dalcin 
2876618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, time
2877618ce8baSLisandro Dalcin 
2878618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
2879618ce8baSLisandro Dalcin @*/
2880618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
2881618ce8baSLisandro Dalcin {
2882618ce8baSLisandro Dalcin   PetscFunctionBegin;
2883618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2884618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
2885618ce8baSLisandro Dalcin   ts->max_time = maxtime;
2886618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2887618ce8baSLisandro Dalcin }
2888618ce8baSLisandro Dalcin 
2889618ce8baSLisandro Dalcin /*@
2890618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
2891618ce8baSLisandro Dalcin 
2892618ce8baSLisandro Dalcin    Not Collective
2893618ce8baSLisandro Dalcin 
2894618ce8baSLisandro Dalcin    Input Parameters:
2895618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2896618ce8baSLisandro Dalcin 
2897618ce8baSLisandro Dalcin    Output Parameter:
2898618ce8baSLisandro Dalcin .  maxtime - final time to step to
2899618ce8baSLisandro Dalcin 
2900618ce8baSLisandro Dalcin    Level: advanced
2901618ce8baSLisandro Dalcin 
2902618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, time
2903618ce8baSLisandro Dalcin 
2904618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
2905618ce8baSLisandro Dalcin @*/
2906618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
2907618ce8baSLisandro Dalcin {
2908618ce8baSLisandro Dalcin   PetscFunctionBegin;
2909618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2910618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
2911618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
2912618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2913618ce8baSLisandro Dalcin }
2914618ce8baSLisandro Dalcin 
2915618ce8baSLisandro Dalcin /*@
2916aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
2917edc382c3SSatish Balay 
2918edc382c3SSatish Balay    Level: deprecated
2919edc382c3SSatish Balay 
2920aaa6c58dSLisandro Dalcin @*/
2921aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
2922aaa6c58dSLisandro Dalcin {
2923aaa6c58dSLisandro Dalcin   PetscErrorCode ierr;
2924aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
2925aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2926aaa6c58dSLisandro Dalcin   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
2927aaa6c58dSLisandro Dalcin   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
2928aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
2929aaa6c58dSLisandro Dalcin }
2930aaa6c58dSLisandro Dalcin 
2931aaa6c58dSLisandro Dalcin /*@
293219eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
2933edc382c3SSatish Balay 
2934edc382c3SSatish Balay    Level: deprecated
2935edc382c3SSatish Balay 
2936adb62b0dSMatthew Knepley @*/
29377087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2938adb62b0dSMatthew Knepley {
2939adb62b0dSMatthew Knepley   PetscFunctionBegin;
29400700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2941abc0a331SBarry Smith   if (maxsteps) {
29424482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2943adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2944adb62b0dSMatthew Knepley   }
2945abc0a331SBarry Smith   if (maxtime) {
29464482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2947adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2948adb62b0dSMatthew Knepley   }
2949adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2950adb62b0dSMatthew Knepley }
2951adb62b0dSMatthew Knepley 
2952d763cef2SBarry Smith /*@
295319eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
2954edc382c3SSatish Balay 
2955edc382c3SSatish Balay    Level: deprecated
2956edc382c3SSatish Balay 
2957d763cef2SBarry Smith @*/
29587087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2959d763cef2SBarry Smith {
2960d763cef2SBarry Smith   PetscFunctionBegin;
29610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2962c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2963c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
296439b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
296539b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2966d763cef2SBarry Smith   PetscFunctionReturn(0);
2967d763cef2SBarry Smith }
2968d763cef2SBarry Smith 
2969d763cef2SBarry Smith /*@
29705c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
2971edc382c3SSatish Balay 
2972edc382c3SSatish Balay    Level: deprecated
2973edc382c3SSatish Balay 
29745c5f5948SLisandro Dalcin @*/
2975e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
29765c5f5948SLisandro Dalcin 
297719eac22cSLisandro Dalcin /*@
29784f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
2979edc382c3SSatish Balay 
2980edc382c3SSatish Balay    Level: deprecated
2981edc382c3SSatish Balay 
29824f4e0956SLisandro Dalcin @*/
29834f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
29844f4e0956SLisandro Dalcin 
29854f4e0956SLisandro Dalcin /*@
2986d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2987d763cef2SBarry Smith    for use by the TS routines.
2988d763cef2SBarry Smith 
29893f9fe445SBarry Smith    Logically Collective on TS and Vec
2990d763cef2SBarry Smith 
2991d763cef2SBarry Smith    Input Parameters:
2992d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
29930910c330SBarry Smith -  u - the solution vector
2994d763cef2SBarry Smith 
2995d763cef2SBarry Smith    Level: beginner
2996d763cef2SBarry Smith 
2997715f1b00SHong Zhang .keywords: TS, timestep, set, solution, initial values
29980ed3bfb6SBarry Smith 
29990ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3000d763cef2SBarry Smith @*/
30010910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3002d763cef2SBarry Smith {
30038737fe31SLisandro Dalcin   PetscErrorCode ierr;
3004496e6a7aSJed Brown   DM             dm;
30058737fe31SLisandro Dalcin 
3006d763cef2SBarry Smith   PetscFunctionBegin;
30070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
30080910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
30090910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
30106bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
30110910c330SBarry Smith   ts->vec_sol = u;
3012bbd56ea5SKarl Rupp 
3013496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
30140910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
3015d763cef2SBarry Smith   PetscFunctionReturn(0);
3016d763cef2SBarry Smith }
3017d763cef2SBarry Smith 
3018ac226902SBarry Smith /*@C
3019000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
30203f2090d5SJed Brown   called once at the beginning of each time step.
3021000e7ae3SMatthew Knepley 
30223f9fe445SBarry Smith   Logically Collective on TS
3023000e7ae3SMatthew Knepley 
3024000e7ae3SMatthew Knepley   Input Parameters:
3025000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3026000e7ae3SMatthew Knepley - func - The function
3027000e7ae3SMatthew Knepley 
3028000e7ae3SMatthew Knepley   Calling sequence of func:
3029000e7ae3SMatthew Knepley . func (TS ts);
3030000e7ae3SMatthew Knepley 
3031000e7ae3SMatthew Knepley   Level: intermediate
3032000e7ae3SMatthew Knepley 
3033000e7ae3SMatthew Knepley .keywords: TS, timestep
3034dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3035000e7ae3SMatthew Knepley @*/
30367087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3037000e7ae3SMatthew Knepley {
3038000e7ae3SMatthew Knepley   PetscFunctionBegin;
30390700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3040ae60f76fSBarry Smith   ts->prestep = func;
3041000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3042000e7ae3SMatthew Knepley }
3043000e7ae3SMatthew Knepley 
304409ee8438SJed Brown /*@
30453f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
30463f2090d5SJed Brown 
30473f2090d5SJed Brown   Collective on TS
30483f2090d5SJed Brown 
30493f2090d5SJed Brown   Input Parameters:
30503f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
30513f2090d5SJed Brown 
30523f2090d5SJed Brown   Notes:
30533f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
30543f2090d5SJed Brown   so most users would not generally call this routine themselves.
30553f2090d5SJed Brown 
30563f2090d5SJed Brown   Level: developer
30573f2090d5SJed Brown 
30583f2090d5SJed Brown .keywords: TS, timestep
30599be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
30603f2090d5SJed Brown @*/
30617087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
30623f2090d5SJed Brown {
30633f2090d5SJed Brown   PetscErrorCode ierr;
30643f2090d5SJed Brown 
30653f2090d5SJed Brown   PetscFunctionBegin;
30660700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3067ae60f76fSBarry Smith   if (ts->prestep) {
30685efd42a4SStefano Zampini     Vec              U;
30695efd42a4SStefano Zampini     PetscObjectState sprev,spost;
30705efd42a4SStefano Zampini 
30715efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
30725efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3073ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
30745efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3075dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3076312ce896SJed Brown   }
30773f2090d5SJed Brown   PetscFunctionReturn(0);
30783f2090d5SJed Brown }
30793f2090d5SJed Brown 
3080b8123daeSJed Brown /*@C
3081b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3082b8123daeSJed Brown   called once at the beginning of each stage.
3083b8123daeSJed Brown 
3084b8123daeSJed Brown   Logically Collective on TS
3085b8123daeSJed Brown 
3086b8123daeSJed Brown   Input Parameters:
3087b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3088b8123daeSJed Brown - func - The function
3089b8123daeSJed Brown 
3090b8123daeSJed Brown   Calling sequence of func:
3091b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
3092b8123daeSJed Brown 
3093b8123daeSJed Brown   Level: intermediate
3094b8123daeSJed Brown 
3095b8123daeSJed Brown   Note:
3096b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
309780275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3098b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3099b8123daeSJed Brown 
3100b8123daeSJed Brown .keywords: TS, timestep
31019be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3102b8123daeSJed Brown @*/
3103b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3104b8123daeSJed Brown {
3105b8123daeSJed Brown   PetscFunctionBegin;
3106b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3107ae60f76fSBarry Smith   ts->prestage = func;
3108b8123daeSJed Brown   PetscFunctionReturn(0);
3109b8123daeSJed Brown }
3110b8123daeSJed Brown 
31119be3e283SDebojyoti Ghosh /*@C
31129be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
31139be3e283SDebojyoti Ghosh   called once at the end of each stage.
31149be3e283SDebojyoti Ghosh 
31159be3e283SDebojyoti Ghosh   Logically Collective on TS
31169be3e283SDebojyoti Ghosh 
31179be3e283SDebojyoti Ghosh   Input Parameters:
31189be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
31199be3e283SDebojyoti Ghosh - func - The function
31209be3e283SDebojyoti Ghosh 
31219be3e283SDebojyoti Ghosh   Calling sequence of func:
31229be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
31239be3e283SDebojyoti Ghosh 
31249be3e283SDebojyoti Ghosh   Level: intermediate
31259be3e283SDebojyoti Ghosh 
31269be3e283SDebojyoti Ghosh   Note:
31279be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
312880275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
31299be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
31309be3e283SDebojyoti Ghosh 
31319be3e283SDebojyoti Ghosh .keywords: TS, timestep
31329be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
31339be3e283SDebojyoti Ghosh @*/
31349be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
31359be3e283SDebojyoti Ghosh {
31369be3e283SDebojyoti Ghosh   PetscFunctionBegin;
31379be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
31389be3e283SDebojyoti Ghosh   ts->poststage = func;
31399be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
31409be3e283SDebojyoti Ghosh }
31419be3e283SDebojyoti Ghosh 
3142c688d042SShri Abhyankar /*@C
3143c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3144c688d042SShri Abhyankar   called once at the end of each step evaluation.
3145c688d042SShri Abhyankar 
3146c688d042SShri Abhyankar   Logically Collective on TS
3147c688d042SShri Abhyankar 
3148c688d042SShri Abhyankar   Input Parameters:
3149c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3150c688d042SShri Abhyankar - func - The function
3151c688d042SShri Abhyankar 
3152c688d042SShri Abhyankar   Calling sequence of func:
3153c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3154c688d042SShri Abhyankar 
3155c688d042SShri Abhyankar   Level: intermediate
3156c688d042SShri Abhyankar 
3157c688d042SShri Abhyankar   Note:
31581785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
31591785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3160e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3161e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3162e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3163c688d042SShri Abhyankar 
3164c688d042SShri Abhyankar .keywords: TS, timestep
3165c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3166c688d042SShri Abhyankar @*/
3167c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3168c688d042SShri Abhyankar {
3169c688d042SShri Abhyankar   PetscFunctionBegin;
3170c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3171c688d042SShri Abhyankar   ts->postevaluate = func;
3172c688d042SShri Abhyankar   PetscFunctionReturn(0);
3173c688d042SShri Abhyankar }
3174c688d042SShri Abhyankar 
3175b8123daeSJed Brown /*@
3176b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3177b8123daeSJed Brown 
3178b8123daeSJed Brown   Collective on TS
3179b8123daeSJed Brown 
3180b8123daeSJed Brown   Input Parameters:
3181b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
31829be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3183b8123daeSJed Brown 
3184b8123daeSJed Brown   Notes:
3185b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3186b8123daeSJed Brown   most users would not generally call this routine themselves.
3187b8123daeSJed Brown 
3188b8123daeSJed Brown   Level: developer
3189b8123daeSJed Brown 
3190b8123daeSJed Brown .keywords: TS, timestep
31919be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3192b8123daeSJed Brown @*/
3193b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3194b8123daeSJed Brown {
3195b8123daeSJed Brown   PetscErrorCode ierr;
3196b8123daeSJed Brown 
3197b8123daeSJed Brown   PetscFunctionBegin;
3198b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3199ae60f76fSBarry Smith   if (ts->prestage) {
3200ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3201b8123daeSJed Brown   }
3202b8123daeSJed Brown   PetscFunctionReturn(0);
3203b8123daeSJed Brown }
3204b8123daeSJed Brown 
32059be3e283SDebojyoti Ghosh /*@
32069be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
32079be3e283SDebojyoti Ghosh 
32089be3e283SDebojyoti Ghosh   Collective on TS
32099be3e283SDebojyoti Ghosh 
32109be3e283SDebojyoti Ghosh   Input Parameters:
32119be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
32129be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
32139be3e283SDebojyoti Ghosh   stageindex  - Stage number
32149be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
32159be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
32169be3e283SDebojyoti Ghosh 
32179be3e283SDebojyoti Ghosh   Notes:
32189be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
32199be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
32209be3e283SDebojyoti Ghosh 
32219be3e283SDebojyoti Ghosh   Level: developer
32229be3e283SDebojyoti Ghosh 
32239be3e283SDebojyoti Ghosh .keywords: TS, timestep
32249be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
32259be3e283SDebojyoti Ghosh @*/
32269be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
32279be3e283SDebojyoti Ghosh {
32289be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
32299be3e283SDebojyoti Ghosh 
32309be3e283SDebojyoti Ghosh   PetscFunctionBegin;
32319be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32324beae5d8SLisandro Dalcin   if (ts->poststage) {
32339be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
32349be3e283SDebojyoti Ghosh   }
32359be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
32369be3e283SDebojyoti Ghosh }
32379be3e283SDebojyoti Ghosh 
3238c688d042SShri Abhyankar /*@
3239c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3240c688d042SShri Abhyankar 
3241c688d042SShri Abhyankar   Collective on TS
3242c688d042SShri Abhyankar 
3243c688d042SShri Abhyankar   Input Parameters:
3244c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3245c688d042SShri Abhyankar 
3246c688d042SShri Abhyankar   Notes:
3247c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3248c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3249c688d042SShri Abhyankar 
3250c688d042SShri Abhyankar   Level: developer
3251c688d042SShri Abhyankar 
3252c688d042SShri Abhyankar .keywords: TS, timestep
3253c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3254c688d042SShri Abhyankar @*/
3255c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3256c688d042SShri Abhyankar {
3257c688d042SShri Abhyankar   PetscErrorCode ierr;
3258c688d042SShri Abhyankar 
3259c688d042SShri Abhyankar   PetscFunctionBegin;
3260c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3261c688d042SShri Abhyankar   if (ts->postevaluate) {
3262dcb233daSLisandro Dalcin     Vec              U;
3263dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3264dcb233daSLisandro Dalcin 
3265dcb233daSLisandro Dalcin     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3266dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3267c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3268dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3269dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3270c688d042SShri Abhyankar   }
3271c688d042SShri Abhyankar   PetscFunctionReturn(0);
3272c688d042SShri Abhyankar }
3273c688d042SShri Abhyankar 
3274ac226902SBarry Smith /*@C
3275000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
32763f2090d5SJed Brown   called once at the end of each time step.
3277000e7ae3SMatthew Knepley 
32783f9fe445SBarry Smith   Logically Collective on TS
3279000e7ae3SMatthew Knepley 
3280000e7ae3SMatthew Knepley   Input Parameters:
3281000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3282000e7ae3SMatthew Knepley - func - The function
3283000e7ae3SMatthew Knepley 
3284000e7ae3SMatthew Knepley   Calling sequence of func:
3285b8123daeSJed Brown $ func (TS ts);
3286000e7ae3SMatthew Knepley 
32871785ff2aSShri Abhyankar   Notes:
32881785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
32891785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
32901785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
32911785ff2aSShri Abhyankar 
3292000e7ae3SMatthew Knepley   Level: intermediate
3293000e7ae3SMatthew Knepley 
3294000e7ae3SMatthew Knepley .keywords: TS, timestep
3295dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3296000e7ae3SMatthew Knepley @*/
32977087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3298000e7ae3SMatthew Knepley {
3299000e7ae3SMatthew Knepley   PetscFunctionBegin;
33000700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3301ae60f76fSBarry Smith   ts->poststep = func;
3302000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3303000e7ae3SMatthew Knepley }
3304000e7ae3SMatthew Knepley 
330509ee8438SJed Brown /*@
33063f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
33073f2090d5SJed Brown 
33083f2090d5SJed Brown   Collective on TS
33093f2090d5SJed Brown 
33103f2090d5SJed Brown   Input Parameters:
33113f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
33123f2090d5SJed Brown 
33133f2090d5SJed Brown   Notes:
33143f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
33153f2090d5SJed Brown   so most users would not generally call this routine themselves.
33163f2090d5SJed Brown 
33173f2090d5SJed Brown   Level: developer
33183f2090d5SJed Brown 
33193f2090d5SJed Brown .keywords: TS, timestep
33203f2090d5SJed Brown @*/
33217087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
33223f2090d5SJed Brown {
33233f2090d5SJed Brown   PetscErrorCode ierr;
33243f2090d5SJed Brown 
33253f2090d5SJed Brown   PetscFunctionBegin;
33260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3327ae60f76fSBarry Smith   if (ts->poststep) {
33285efd42a4SStefano Zampini     Vec              U;
33295efd42a4SStefano Zampini     PetscObjectState sprev,spost;
33305efd42a4SStefano Zampini 
33315efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
33325efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3333ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
33345efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3335dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
333672ac3e02SJed Brown   }
33373f2090d5SJed Brown   PetscFunctionReturn(0);
33383f2090d5SJed Brown }
33393f2090d5SJed Brown 
3340d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3341d763cef2SBarry Smith 
3342d763cef2SBarry Smith /*@C
3343a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3344d763cef2SBarry Smith    timestep to display the iteration's  progress.
3345d763cef2SBarry Smith 
33463f9fe445SBarry Smith    Logically Collective on TS
3347d763cef2SBarry Smith 
3348d763cef2SBarry Smith    Input Parameters:
3349d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3350e213d8f1SJed Brown .  monitor - monitoring routine
3351329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
33520298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3353b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
33540298fd71SBarry Smith           (may be NULL)
3355d763cef2SBarry Smith 
3356e213d8f1SJed Brown    Calling sequence of monitor:
3357dcb233daSLisandro Dalcin $    PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3358d763cef2SBarry Smith 
3359d763cef2SBarry Smith +    ts - the TS context
336063e21af5SBarry 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)
33611f06c33eSBarry Smith .    time - current time
33620910c330SBarry Smith .    u - current iterate
3363d763cef2SBarry Smith -    mctx - [optional] monitoring context
3364d763cef2SBarry Smith 
3365d763cef2SBarry Smith    Notes:
3366d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3367d763cef2SBarry Smith    already has been loaded.
3368d763cef2SBarry Smith 
336995452b02SPatrick Sanan    Fortran Notes:
337095452b02SPatrick Sanan     Only a single monitor function can be set for each TS object
3371025f1a04SBarry Smith 
3372d763cef2SBarry Smith    Level: intermediate
3373d763cef2SBarry Smith 
3374d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3375d763cef2SBarry Smith 
3376a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3377d763cef2SBarry Smith @*/
3378c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3379d763cef2SBarry Smith {
338078064530SBarry Smith   PetscErrorCode ierr;
338178064530SBarry Smith   PetscInt       i;
338278064530SBarry Smith   PetscBool      identical;
338378064530SBarry Smith 
3384d763cef2SBarry Smith   PetscFunctionBegin;
33850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
338678064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
338778064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
338878064530SBarry Smith     if (identical) PetscFunctionReturn(0);
338978064530SBarry Smith   }
339017186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3391d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
33928704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3393d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3394d763cef2SBarry Smith   PetscFunctionReturn(0);
3395d763cef2SBarry Smith }
3396d763cef2SBarry Smith 
3397d763cef2SBarry Smith /*@C
3398a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3399d763cef2SBarry Smith 
34003f9fe445SBarry Smith    Logically Collective on TS
3401d763cef2SBarry Smith 
3402d763cef2SBarry Smith    Input Parameters:
3403d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3404d763cef2SBarry Smith 
3405d763cef2SBarry Smith    Notes:
3406d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3407d763cef2SBarry Smith 
3408d763cef2SBarry Smith    Level: intermediate
3409d763cef2SBarry Smith 
3410d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3411d763cef2SBarry Smith 
3412a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3413d763cef2SBarry Smith @*/
34147087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3415d763cef2SBarry Smith {
3416d952e501SBarry Smith   PetscErrorCode ierr;
3417d952e501SBarry Smith   PetscInt       i;
3418d952e501SBarry Smith 
3419d763cef2SBarry Smith   PetscFunctionBegin;
34200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3421d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
34228704b422SBarry Smith     if (ts->monitordestroy[i]) {
34238704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3424d952e501SBarry Smith     }
3425d952e501SBarry Smith   }
3426d763cef2SBarry Smith   ts->numbermonitors = 0;
3427d763cef2SBarry Smith   PetscFunctionReturn(0);
3428d763cef2SBarry Smith }
3429d763cef2SBarry Smith 
3430721cd6eeSBarry Smith /*@C
3431721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
34325516499fSSatish Balay 
34335516499fSSatish Balay    Level: intermediate
343441251cbbSSatish Balay 
34355516499fSSatish Balay .keywords: TS, set, monitor
34365516499fSSatish Balay 
343763e21af5SBarry Smith .seealso:  TSMonitorSet()
343841251cbbSSatish Balay @*/
3439721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3440d763cef2SBarry Smith {
3441dfbe8321SBarry Smith   PetscErrorCode ierr;
3442721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
344341aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3444d132466eSBarry Smith 
3445d763cef2SBarry Smith   PetscFunctionBegin;
34464d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
344741aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
344841aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3449721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
345041aca3d6SBarry Smith   if (iascii) {
3451649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
345263e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
345363e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
345463e21af5SBarry Smith     } else {
34558392e04aSShri 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);
345663e21af5SBarry Smith     }
3457649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
345841aca3d6SBarry Smith   } else if (ibinary) {
345941aca3d6SBarry Smith     PetscMPIInt rank;
346041aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
346141aca3d6SBarry Smith     if (!rank) {
3462450a797fSBarry Smith       PetscBool skipHeader;
3463450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3464450a797fSBarry Smith 
3465450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3466450a797fSBarry Smith       if (!skipHeader) {
3467450a797fSBarry Smith          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
3468450a797fSBarry Smith        }
346941aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
347041aca3d6SBarry Smith     } else {
347141aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
347241aca3d6SBarry Smith     }
347341aca3d6SBarry Smith   }
3474721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3475d763cef2SBarry Smith   PetscFunctionReturn(0);
3476d763cef2SBarry Smith }
3477d763cef2SBarry Smith 
3478cc9c3a59SBarry Smith /*@C
3479cc9c3a59SBarry Smith    TSMonitorExtreme - Prints the extreme values of the solution at each timestep
3480cc9c3a59SBarry Smith 
3481cc9c3a59SBarry Smith    Level: intermediate
3482cc9c3a59SBarry Smith 
3483cc9c3a59SBarry Smith .keywords: TS, set, monitor
3484cc9c3a59SBarry Smith 
3485cc9c3a59SBarry Smith .seealso:  TSMonitorSet()
3486cc9c3a59SBarry Smith @*/
3487cc9c3a59SBarry Smith PetscErrorCode TSMonitorExtreme(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3488cc9c3a59SBarry Smith {
3489cc9c3a59SBarry Smith   PetscErrorCode ierr;
3490cc9c3a59SBarry Smith   PetscViewer    viewer =  vf->viewer;
3491cc9c3a59SBarry Smith   PetscBool      iascii;
3492cc9c3a59SBarry Smith   PetscReal      max,min;
3493cc9c3a59SBarry Smith 
3494cc9c3a59SBarry Smith 
3495cc9c3a59SBarry Smith   PetscFunctionBegin;
3496cc9c3a59SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3497cc9c3a59SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
3498cc9c3a59SBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3499cc9c3a59SBarry Smith   if (iascii) {
3500cc9c3a59SBarry Smith     ierr = VecMax(v,NULL,&max);CHKERRQ(ierr);
3501cc9c3a59SBarry Smith     ierr = VecMin(v,NULL,&min);CHKERRQ(ierr);
3502cc9c3a59SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
35035132926bSBarry 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);
3504cc9c3a59SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3505cc9c3a59SBarry Smith   }
3506cc9c3a59SBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3507cc9c3a59SBarry Smith   PetscFunctionReturn(0);
3508cc9c3a59SBarry Smith }
3509cc9c3a59SBarry Smith 
3510cd652676SJed Brown /*@
3511cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3512cd652676SJed Brown 
3513cd652676SJed Brown    Collective on TS
3514cd652676SJed Brown 
3515cd652676SJed Brown    Input Argument:
3516cd652676SJed Brown +  ts - time stepping context
3517cd652676SJed Brown -  t - time to interpolate to
3518cd652676SJed Brown 
3519cd652676SJed Brown    Output Argument:
35200910c330SBarry Smith .  U - state at given time
3521cd652676SJed Brown 
3522cd652676SJed Brown    Level: intermediate
3523cd652676SJed Brown 
3524cd652676SJed Brown    Developer Notes:
3525cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3526cd652676SJed Brown 
3527cd652676SJed Brown .keywords: TS, set
3528cd652676SJed Brown 
3529874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3530cd652676SJed Brown @*/
35310910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3532cd652676SJed Brown {
3533cd652676SJed Brown   PetscErrorCode ierr;
3534cd652676SJed Brown 
3535cd652676SJed Brown   PetscFunctionBegin;
3536cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3537b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3538be5899b3SLisandro 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);
3539ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
35400910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3541cd652676SJed Brown   PetscFunctionReturn(0);
3542cd652676SJed Brown }
3543cd652676SJed Brown 
3544d763cef2SBarry Smith /*@
35456d9e5789SSean Farley    TSStep - Steps one time step
3546d763cef2SBarry Smith 
3547d763cef2SBarry Smith    Collective on TS
3548d763cef2SBarry Smith 
3549d763cef2SBarry Smith    Input Parameter:
3550d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3551d763cef2SBarry Smith 
355227829d71SBarry Smith    Level: developer
3553d763cef2SBarry Smith 
3554b8123daeSJed Brown    Notes:
355527829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
355627829d71SBarry Smith 
3557b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3558b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3559b8123daeSJed Brown 
356019eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
356119eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
356225cb2221SBarry Smith 
3563d763cef2SBarry Smith .keywords: TS, timestep, solve
3564d763cef2SBarry Smith 
35659be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3566d763cef2SBarry Smith @*/
3567193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3568d763cef2SBarry Smith {
3569dfbe8321SBarry Smith   PetscErrorCode   ierr;
3570fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3571be5899b3SLisandro Dalcin   PetscReal        ptime;
3572d763cef2SBarry Smith 
3573d763cef2SBarry Smith   PetscFunctionBegin;
35740700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3575fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3576fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3577fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3578fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3579fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3580fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3581302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3582fffbeea8SBarry Smith 
3583d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
358468bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3585d405a339SMatthew Knepley 
3586ef85077eSLisandro 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>");
3587a6772fa2SLisandro 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()");
3588be5899b3SLisandro 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");
3589a6772fa2SLisandro Dalcin 
3590be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3591be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
35922808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3593e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3594d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3595193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3596d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3597be5899b3SLisandro Dalcin   ts->ptime_prev = ptime;
35982808aa04SLisandro Dalcin   ts->steps++;
3599be5899b3SLisandro Dalcin   ts->steprollback = PETSC_FALSE;
360028d5b5d6SLisandro Dalcin   ts->steprestart  = PETSC_FALSE;
3601362cd11cSLisandro Dalcin 
3602362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3603cef5090cSJed Brown     if (ts->errorifstepfailed) {
360408c7845fSBarry 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]);
360508c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3606d2daff3dSHong Zhang     }
360708c7845fSBarry Smith   } else if (!ts->reason) {
360808c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
360908c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
361008c7845fSBarry Smith   }
361108c7845fSBarry Smith   PetscFunctionReturn(0);
361208c7845fSBarry Smith }
361308c7845fSBarry Smith 
361408c7845fSBarry Smith /*@
36157cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
36167cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
36177cbde773SLisandro Dalcin 
36187cbde773SLisandro Dalcin    Collective on TS
36197cbde773SLisandro Dalcin 
36207cbde773SLisandro Dalcin    Input Arguments:
36217cbde773SLisandro Dalcin +  ts - time stepping context
36227cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
36237cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
36247cbde773SLisandro Dalcin 
36257cbde773SLisandro Dalcin    Output Arguments:
36267cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
36277cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
36287cbde773SLisandro Dalcin 
36297cbde773SLisandro Dalcin    Level: advanced
36307cbde773SLisandro Dalcin 
36317cbde773SLisandro Dalcin    Notes:
36327cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
36337cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
36347cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
36357cbde773SLisandro Dalcin 
36367cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
36377cbde773SLisandro Dalcin @*/
36387cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
36397cbde773SLisandro Dalcin {
36407cbde773SLisandro Dalcin   PetscErrorCode ierr;
36417cbde773SLisandro Dalcin 
36427cbde773SLisandro Dalcin   PetscFunctionBegin;
36437cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36447cbde773SLisandro Dalcin   PetscValidType(ts,1);
36457cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
36467cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
36477cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
36487cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
36497cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
36507cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
36517cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
36527cbde773SLisandro Dalcin   PetscFunctionReturn(0);
36537cbde773SLisandro Dalcin }
36547cbde773SLisandro Dalcin 
365505175c85SJed Brown /*@
365605175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
365705175c85SJed Brown 
36581c3436cfSJed Brown    Collective on TS
365905175c85SJed Brown 
366005175c85SJed Brown    Input Arguments:
36611c3436cfSJed Brown +  ts - time stepping context
36621c3436cfSJed Brown .  order - desired order of accuracy
36630298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
366405175c85SJed Brown 
366505175c85SJed Brown    Output Arguments:
36660910c330SBarry Smith .  U - state at the end of the current step
366705175c85SJed Brown 
366805175c85SJed Brown    Level: advanced
366905175c85SJed Brown 
3670108c343cSJed Brown    Notes:
3671108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3672108c343cSJed 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.
3673108c343cSJed Brown 
36741c3436cfSJed Brown .seealso: TSStep(), TSAdapt
367505175c85SJed Brown @*/
36760910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
367705175c85SJed Brown {
367805175c85SJed Brown   PetscErrorCode ierr;
367905175c85SJed Brown 
368005175c85SJed Brown   PetscFunctionBegin;
368105175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
368205175c85SJed Brown   PetscValidType(ts,1);
36830910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3684ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
36850910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
368605175c85SJed Brown   PetscFunctionReturn(0);
368705175c85SJed Brown }
368805175c85SJed Brown 
3689b1cb36f3SHong Zhang /*@
36906a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
36916a4d4014SLisandro Dalcin 
36926a4d4014SLisandro Dalcin    Collective on TS
36936a4d4014SLisandro Dalcin 
36946a4d4014SLisandro Dalcin    Input Parameter:
36956a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
369663e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
369763e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
36985a3a76d0SJed Brown 
36996a4d4014SLisandro Dalcin    Level: beginner
37006a4d4014SLisandro Dalcin 
37015a3a76d0SJed Brown    Notes:
37025a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
37035a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
37045a3a76d0SJed Brown    stepped over the final time.
37055a3a76d0SJed Brown 
37066a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
37076a4d4014SLisandro Dalcin 
370863e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
37096a4d4014SLisandro Dalcin @*/
3710cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
37116a4d4014SLisandro Dalcin {
3712b06615a5SLisandro Dalcin   Vec               solution;
37136a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
3714f22f69f0SBarry Smith 
37156a4d4014SLisandro Dalcin   PetscFunctionBegin;
37160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3717f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
3718feed9e9dSBarry Smith 
3719ee41a567SStefano 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 */
37200910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3721b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3722b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3723b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
37245a3a76d0SJed Brown     }
37250910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3726715f1b00SHong Zhang     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
3727bbd56ea5SKarl Rupp   } else if (u) {
37280910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
37295a3a76d0SJed Brown   }
3730b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
373168bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3732a6772fa2SLisandro Dalcin 
3733ef85077eSLisandro 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>");
3734a6772fa2SLisandro 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()");
3735a6772fa2SLisandro 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");
3736a6772fa2SLisandro Dalcin 
3737715f1b00SHong Zhang   if (ts->forward_solve) {
3738715f1b00SHong Zhang     ierr = TSForwardSetUp(ts);CHKERRQ(ierr);
3739715f1b00SHong Zhang   }
3740715f1b00SHong Zhang 
3741e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
3742715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
3743e7069c78SShri      TSTrajectory to incorrectly save the output files
3744e7069c78SShri   */
3745715f1b00SHong Zhang   /* reset time step and iteration counters */
37462808aa04SLisandro Dalcin   if (!ts->steps) {
37475ef26d82SJed Brown     ts->ksp_its           = 0;
37485ef26d82SJed Brown     ts->snes_its          = 0;
3749c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
3750c610991cSLisandro Dalcin     ts->reject            = 0;
37512808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
37522808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
37532808aa04SLisandro Dalcin   }
375410bc0e4dSStefano 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;
3755193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
3756193ac0bcSJed Brown 
3757ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3758f05ece33SBarry Smith 
3759193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
3760193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
3761a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3762cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3763a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
3764be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
3765db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
3766db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3767e7069c78SShri 
37682808aa04SLisandro Dalcin     if (!ts->steps) {
37692808aa04SLisandro Dalcin       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
37706427ac75SLisandro Dalcin       ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
37712808aa04SLisandro Dalcin     }
37726427ac75SLisandro Dalcin 
3773e1a7a14fSJed Brown     while (!ts->reason) {
37742808aa04SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
37759687d888SLisandro Dalcin       if (!ts->steprollback) {
37769687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
37779687d888SLisandro Dalcin       }
3778193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
3779f3b1f45cSBarry Smith       if (ts->testjacobian) {
3780f3b1f45cSBarry Smith         ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr);
3781f3b1f45cSBarry Smith       }
3782f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
3783f3b1f45cSBarry Smith         ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr);
3784f3b1f45cSBarry Smith       }
3785e783b05fSHong 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. */
3786b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
3787b1cb36f3SHong Zhang       }
378858818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
3789715f1b00SHong Zhang         ierr = TSForwardStep(ts);CHKERRQ(ierr);
3790715f1b00SHong Zhang       }
379110b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
3792e783b05fSHong 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. */
379358818c2dSLisandro Dalcin       if (ts->steprollback) {
379458818c2dSLisandro Dalcin         ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
379558818c2dSLisandro Dalcin       }
3796e783b05fSHong Zhang       if (!ts->steprollback) {
37972808aa04SLisandro Dalcin         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
37981eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
3799aeb4809dSShri Abhyankar       }
3800193ac0bcSJed Brown     }
38012808aa04SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
38026427ac75SLisandro Dalcin 
380349354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
38040910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
3805cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3806b06615a5SLisandro Dalcin       solution = u;
380763e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
38080574a7fbSJed Brown     } else {
3809ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3810cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3811b06615a5SLisandro Dalcin       solution = ts->vec_sol;
38120574a7fbSJed Brown     }
3813193ac0bcSJed Brown   }
3814d2daff3dSHong Zhang 
3815ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
381663e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
381756f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
3818ef222394SBarry Smith   if (ts->adjoint_solve) {
3819ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
38202b0a91c0SBarry Smith   }
38216a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
38226a4d4014SLisandro Dalcin }
38236a4d4014SLisandro Dalcin 
38247db568b7SBarry Smith /*@C
3825228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
3826228d79bcSJed Brown 
3827228d79bcSJed Brown    Collective on TS
3828228d79bcSJed Brown 
3829228d79bcSJed Brown    Input Parameters:
3830228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
3831228d79bcSJed Brown .  step - step number that has just completed
3832228d79bcSJed Brown .  ptime - model time of the state
38330910c330SBarry Smith -  u - state at the current model time
3834228d79bcSJed Brown 
3835228d79bcSJed Brown    Notes:
38367db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
38377db568b7SBarry Smith    Users would almost never call this routine directly.
3838228d79bcSJed Brown 
383963e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
384063e21af5SBarry Smith 
38417db568b7SBarry Smith    Level: developer
3842228d79bcSJed Brown 
3843228d79bcSJed Brown .keywords: TS, timestep
3844228d79bcSJed Brown @*/
38450910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
3846d763cef2SBarry Smith {
3847d6152f81SLisandro Dalcin   DM             dm;
3848a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
3849d6152f81SLisandro Dalcin   PetscErrorCode ierr;
3850d763cef2SBarry Smith 
3851d763cef2SBarry Smith   PetscFunctionBegin;
3852b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3853b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
3854d6152f81SLisandro Dalcin 
3855d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
3856d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
3857d6152f81SLisandro Dalcin 
38585edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
3859d763cef2SBarry Smith   for (i=0; i<n; i++) {
38600910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
3861d763cef2SBarry Smith   }
38625edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
3863d763cef2SBarry Smith   PetscFunctionReturn(0);
3864d763cef2SBarry Smith }
3865d763cef2SBarry Smith 
3866d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
3867d763cef2SBarry Smith /*@C
38687db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
3869a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
3870d763cef2SBarry Smith 
3871d763cef2SBarry Smith    Collective on TS
3872d763cef2SBarry Smith 
3873d763cef2SBarry Smith    Input Parameters:
3874d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
3875d763cef2SBarry Smith .  label - the title to put in the title bar
38767c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
3877a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
3878a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
3879d763cef2SBarry Smith 
3880d763cef2SBarry Smith    Output Parameter:
38810b039ecaSBarry Smith .  ctx - the context
3882d763cef2SBarry Smith 
3883d763cef2SBarry Smith    Options Database Key:
38844f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
38858b668821SLisandro Dalcin +  -ts_monitor_lg_timestep_log - automatically sets line graph monitor
38867db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
38877db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
38887db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
38897db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
3890b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
3891d763cef2SBarry Smith 
3892d763cef2SBarry Smith    Notes:
3893a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
3894d763cef2SBarry Smith 
38957db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
38967db568b7SBarry Smith 
38977db568b7SBarry 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
38987db568b7SBarry 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
38997db568b7SBarry Smith    as the first argument.
39007db568b7SBarry Smith 
39017db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
39027db568b7SBarry Smith 
3903d763cef2SBarry Smith    Level: intermediate
3904d763cef2SBarry Smith 
39057db568b7SBarry Smith .keywords: TS, monitor, line graph, residual
3906d763cef2SBarry Smith 
39077db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
39087db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
39097db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
39107db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
39117db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
39127c922b88SBarry Smith 
3913d763cef2SBarry Smith @*/
3914a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
3915d763cef2SBarry Smith {
39167f52daa2SLisandro Dalcin   PetscDraw      draw;
3917dfbe8321SBarry Smith   PetscErrorCode ierr;
3918d763cef2SBarry Smith 
3919d763cef2SBarry Smith   PetscFunctionBegin;
3920b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
39217f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
39227f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
39237f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
3924287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
39257f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
3926a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
3927d763cef2SBarry Smith   PetscFunctionReturn(0);
3928d763cef2SBarry Smith }
3929d763cef2SBarry Smith 
3930b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
3931d763cef2SBarry Smith {
39320b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
3933c32365f1SBarry Smith   PetscReal      x   = ptime,y;
3934dfbe8321SBarry Smith   PetscErrorCode ierr;
3935d763cef2SBarry Smith 
3936d763cef2SBarry Smith   PetscFunctionBegin;
393763e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
3938b06615a5SLisandro Dalcin   if (!step) {
3939a9f9c1f6SBarry Smith     PetscDrawAxis axis;
39408b668821SLisandro Dalcin     const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step";
3941a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
39428b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr);
3943a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
3944a9f9c1f6SBarry Smith   }
3945c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
39468b668821SLisandro Dalcin   if (ctx->semilogy) y = PetscLog10Real(y);
39470b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
3948b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
39490b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
39506934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
39513923b477SBarry Smith   }
3952d763cef2SBarry Smith   PetscFunctionReturn(0);
3953d763cef2SBarry Smith }
3954d763cef2SBarry Smith 
3955d763cef2SBarry Smith /*@C
3956a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
3957a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
3958d763cef2SBarry Smith 
39590b039ecaSBarry Smith    Collective on TSMonitorLGCtx
3960d763cef2SBarry Smith 
3961d763cef2SBarry Smith    Input Parameter:
39620b039ecaSBarry Smith .  ctx - the monitor context
3963d763cef2SBarry Smith 
3964d763cef2SBarry Smith    Level: intermediate
3965d763cef2SBarry Smith 
3966d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
3967d763cef2SBarry Smith 
39684f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
3969d763cef2SBarry Smith @*/
3970a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
3971d763cef2SBarry Smith {
3972dfbe8321SBarry Smith   PetscErrorCode ierr;
3973d763cef2SBarry Smith 
3974d763cef2SBarry Smith   PetscFunctionBegin;
39757684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
39767684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
39777684fa3eSBarry Smith   }
39780b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
397931152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
3980387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
3981387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
3982387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
39830b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
3984d763cef2SBarry Smith   PetscFunctionReturn(0);
3985d763cef2SBarry Smith }
3986d763cef2SBarry Smith 
3987d763cef2SBarry Smith /*@
3988b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
3989d763cef2SBarry Smith 
3990d763cef2SBarry Smith    Not Collective
3991d763cef2SBarry Smith 
3992d763cef2SBarry Smith    Input Parameter:
3993d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3994d763cef2SBarry Smith 
3995d763cef2SBarry Smith    Output Parameter:
399619eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
3997d763cef2SBarry Smith 
3998d763cef2SBarry Smith    Level: beginner
3999d763cef2SBarry Smith 
4000b8123daeSJed Brown    Note:
4001b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
40029be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4003b8123daeSJed Brown 
4004aaa6c58dSLisandro Dalcin .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep()
4005d763cef2SBarry Smith 
4006d763cef2SBarry Smith .keywords: TS, get, time
4007d763cef2SBarry Smith @*/
40087087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4009d763cef2SBarry Smith {
4010d763cef2SBarry Smith   PetscFunctionBegin;
40110700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4012f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4013d763cef2SBarry Smith   *t = ts->ptime;
4014d763cef2SBarry Smith   PetscFunctionReturn(0);
4015d763cef2SBarry Smith }
4016d763cef2SBarry Smith 
4017e5e524a1SHong Zhang /*@
4018e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4019e5e524a1SHong Zhang 
4020e5e524a1SHong Zhang    Not Collective
4021e5e524a1SHong Zhang 
4022e5e524a1SHong Zhang    Input Parameter:
4023e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4024e5e524a1SHong Zhang 
4025e5e524a1SHong Zhang    Output Parameter:
4026e5e524a1SHong Zhang .  t  - the previous time
4027e5e524a1SHong Zhang 
4028e5e524a1SHong Zhang    Level: beginner
4029e5e524a1SHong Zhang 
4030aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4031e5e524a1SHong Zhang 
4032e5e524a1SHong Zhang .keywords: TS, get, time
4033e5e524a1SHong Zhang @*/
4034e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4035e5e524a1SHong Zhang {
4036e5e524a1SHong Zhang   PetscFunctionBegin;
4037e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4038e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4039e5e524a1SHong Zhang   *t = ts->ptime_prev;
4040e5e524a1SHong Zhang   PetscFunctionReturn(0);
4041e5e524a1SHong Zhang }
4042e5e524a1SHong Zhang 
40436a4d4014SLisandro Dalcin /*@
40446a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
40456a4d4014SLisandro Dalcin 
40463f9fe445SBarry Smith    Logically Collective on TS
40476a4d4014SLisandro Dalcin 
40486a4d4014SLisandro Dalcin    Input Parameters:
40496a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
40506a4d4014SLisandro Dalcin -  time - the time
40516a4d4014SLisandro Dalcin 
40526a4d4014SLisandro Dalcin    Level: intermediate
40536a4d4014SLisandro Dalcin 
405419eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
40556a4d4014SLisandro Dalcin 
40566a4d4014SLisandro Dalcin .keywords: TS, set, time
40576a4d4014SLisandro Dalcin @*/
40587087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
40596a4d4014SLisandro Dalcin {
40606a4d4014SLisandro Dalcin   PetscFunctionBegin;
40610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4062c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
40636a4d4014SLisandro Dalcin   ts->ptime = t;
40646a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
40656a4d4014SLisandro Dalcin }
40666a4d4014SLisandro Dalcin 
4067d763cef2SBarry Smith /*@C
4068d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4069d763cef2SBarry Smith    TS options in the database.
4070d763cef2SBarry Smith 
40713f9fe445SBarry Smith    Logically Collective on TS
4072d763cef2SBarry Smith 
4073d763cef2SBarry Smith    Input Parameter:
4074d763cef2SBarry Smith +  ts     - The TS context
4075d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4076d763cef2SBarry Smith 
4077d763cef2SBarry Smith    Notes:
4078d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4079d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4080d763cef2SBarry Smith    hyphen.
4081d763cef2SBarry Smith 
4082d763cef2SBarry Smith    Level: advanced
4083d763cef2SBarry Smith 
4084d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
4085d763cef2SBarry Smith 
4086d763cef2SBarry Smith .seealso: TSSetFromOptions()
4087d763cef2SBarry Smith 
4088d763cef2SBarry Smith @*/
40897087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4090d763cef2SBarry Smith {
4091dfbe8321SBarry Smith   PetscErrorCode ierr;
4092089b2837SJed Brown   SNES           snes;
4093d763cef2SBarry Smith 
4094d763cef2SBarry Smith   PetscFunctionBegin;
40950700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4096d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4097089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4098089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4099d763cef2SBarry Smith   PetscFunctionReturn(0);
4100d763cef2SBarry Smith }
4101d763cef2SBarry Smith 
4102d763cef2SBarry Smith /*@C
4103d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4104d763cef2SBarry Smith    TS options in the database.
4105d763cef2SBarry Smith 
41063f9fe445SBarry Smith    Logically Collective on TS
4107d763cef2SBarry Smith 
4108d763cef2SBarry Smith    Input Parameter:
4109d763cef2SBarry Smith +  ts     - The TS context
4110d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4111d763cef2SBarry Smith 
4112d763cef2SBarry Smith    Notes:
4113d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4114d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4115d763cef2SBarry Smith    hyphen.
4116d763cef2SBarry Smith 
4117d763cef2SBarry Smith    Level: advanced
4118d763cef2SBarry Smith 
4119d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
4120d763cef2SBarry Smith 
4121d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4122d763cef2SBarry Smith 
4123d763cef2SBarry Smith @*/
41247087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4125d763cef2SBarry Smith {
4126dfbe8321SBarry Smith   PetscErrorCode ierr;
4127089b2837SJed Brown   SNES           snes;
4128d763cef2SBarry Smith 
4129d763cef2SBarry Smith   PetscFunctionBegin;
41300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4131d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4132089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4133089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4134d763cef2SBarry Smith   PetscFunctionReturn(0);
4135d763cef2SBarry Smith }
4136d763cef2SBarry Smith 
4137d763cef2SBarry Smith /*@C
4138d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4139d763cef2SBarry Smith    TS options in the database.
4140d763cef2SBarry Smith 
4141d763cef2SBarry Smith    Not Collective
4142d763cef2SBarry Smith 
4143d763cef2SBarry Smith    Input Parameter:
4144d763cef2SBarry Smith .  ts - The TS context
4145d763cef2SBarry Smith 
4146d763cef2SBarry Smith    Output Parameter:
4147d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4148d763cef2SBarry Smith 
414995452b02SPatrick Sanan    Notes:
415095452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prifix' of
4151d763cef2SBarry Smith    sufficient length to hold the prefix.
4152d763cef2SBarry Smith 
4153d763cef2SBarry Smith    Level: intermediate
4154d763cef2SBarry Smith 
4155d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
4156d763cef2SBarry Smith 
4157d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4158d763cef2SBarry Smith @*/
41597087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4160d763cef2SBarry Smith {
4161dfbe8321SBarry Smith   PetscErrorCode ierr;
4162d763cef2SBarry Smith 
4163d763cef2SBarry Smith   PetscFunctionBegin;
41640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
41654482741eSBarry Smith   PetscValidPointer(prefix,2);
4166d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4167d763cef2SBarry Smith   PetscFunctionReturn(0);
4168d763cef2SBarry Smith }
4169d763cef2SBarry Smith 
4170d763cef2SBarry Smith /*@C
4171d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4172d763cef2SBarry Smith 
4173d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4174d763cef2SBarry Smith 
4175d763cef2SBarry Smith    Input Parameter:
4176d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4177d763cef2SBarry Smith 
4178d763cef2SBarry Smith    Output Parameters:
4179e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4180e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4181e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4182e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4183d763cef2SBarry Smith 
418495452b02SPatrick Sanan    Notes:
418595452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
4186d763cef2SBarry Smith 
4187d763cef2SBarry Smith    Level: intermediate
4188d763cef2SBarry Smith 
418980275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4190d763cef2SBarry Smith 
4191d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
4192d763cef2SBarry Smith @*/
4193e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4194d763cef2SBarry Smith {
4195089b2837SJed Brown   PetscErrorCode ierr;
419624989b8cSPeter Brune   DM             dm;
4197089b2837SJed Brown 
4198d763cef2SBarry Smith   PetscFunctionBegin;
419923a57915SBarry Smith   if (Amat || Pmat) {
420023a57915SBarry Smith     SNES snes;
4201089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
420223a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4203e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
420423a57915SBarry Smith   }
420524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
420624989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4207d763cef2SBarry Smith   PetscFunctionReturn(0);
4208d763cef2SBarry Smith }
4209d763cef2SBarry Smith 
42102eca1d9cSJed Brown /*@C
42112eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
42122eca1d9cSJed Brown 
42132eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
42142eca1d9cSJed Brown 
42152eca1d9cSJed Brown    Input Parameter:
42162eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
42172eca1d9cSJed Brown 
42182eca1d9cSJed Brown    Output Parameters:
4219e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4220e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
42212eca1d9cSJed Brown .  f   - The function to compute the matrices
42222eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
42232eca1d9cSJed Brown 
422495452b02SPatrick Sanan    Notes:
422595452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
42262eca1d9cSJed Brown 
42272eca1d9cSJed Brown    Level: advanced
42282eca1d9cSJed Brown 
422980275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
42302eca1d9cSJed Brown 
42312eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
42322eca1d9cSJed Brown @*/
4233e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
42342eca1d9cSJed Brown {
4235089b2837SJed Brown   PetscErrorCode ierr;
423624989b8cSPeter Brune   DM             dm;
42370910c330SBarry Smith 
42382eca1d9cSJed Brown   PetscFunctionBegin;
4239c0aab802Sstefano_zampini   if (Amat || Pmat) {
4240c0aab802Sstefano_zampini     SNES snes;
4241089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4242f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4243e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4244c0aab802Sstefano_zampini   }
424524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
424624989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
42472eca1d9cSJed Brown   PetscFunctionReturn(0);
42482eca1d9cSJed Brown }
42492eca1d9cSJed Brown 
42501713a123SBarry Smith /*@C
425183a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
42521713a123SBarry Smith    VecView() for the solution at each timestep
42531713a123SBarry Smith 
42541713a123SBarry Smith    Collective on TS
42551713a123SBarry Smith 
42561713a123SBarry Smith    Input Parameters:
42571713a123SBarry Smith +  ts - the TS context
42581713a123SBarry Smith .  step - current time-step
4259142b95e3SSatish Balay .  ptime - current time
42600298fd71SBarry Smith -  dummy - either a viewer or NULL
42611713a123SBarry Smith 
426299fdda47SBarry Smith    Options Database:
426399fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
426499fdda47SBarry Smith 
426595452b02SPatrick Sanan    Notes:
426695452b02SPatrick Sanan     the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
426799fdda47SBarry Smith        will look bad
426899fdda47SBarry Smith 
42691713a123SBarry Smith    Level: intermediate
42701713a123SBarry Smith 
42711713a123SBarry Smith .keywords: TS,  vector, monitor, view
42721713a123SBarry Smith 
4273a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
42741713a123SBarry Smith @*/
42750910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
42761713a123SBarry Smith {
4277dfbe8321SBarry Smith   PetscErrorCode   ierr;
427883a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4279473a3ab2SBarry Smith   PetscDraw        draw;
42801713a123SBarry Smith 
42811713a123SBarry Smith   PetscFunctionBegin;
42826083293cSBarry Smith   if (!step && ictx->showinitial) {
42836083293cSBarry Smith     if (!ictx->initialsolution) {
42840910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
42851713a123SBarry Smith     }
42860910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
42876083293cSBarry Smith   }
4288b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
42890dcf80beSBarry Smith 
42906083293cSBarry Smith   if (ictx->showinitial) {
42916083293cSBarry Smith     PetscReal pause;
42926083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
42936083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
42946083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
42956083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
42966083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
42976083293cSBarry Smith   }
42980910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4299473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
430051fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4301473a3ab2SBarry Smith     char      time[32];
4302473a3ab2SBarry Smith 
4303473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
430485b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4305473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4306473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
430751fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4308473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4309473a3ab2SBarry Smith   }
4310473a3ab2SBarry Smith 
43116083293cSBarry Smith   if (ictx->showinitial) {
43126083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
43136083293cSBarry Smith   }
43141713a123SBarry Smith   PetscFunctionReturn(0);
43151713a123SBarry Smith }
43161713a123SBarry Smith 
43179110b2e7SHong Zhang /*@C
43182d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
43192d5ee99bSBarry Smith 
43202d5ee99bSBarry Smith    Collective on TS
43212d5ee99bSBarry Smith 
43222d5ee99bSBarry Smith    Input Parameters:
43232d5ee99bSBarry Smith +  ts - the TS context
43242d5ee99bSBarry Smith .  step - current time-step
43252d5ee99bSBarry Smith .  ptime - current time
43262d5ee99bSBarry Smith -  dummy - either a viewer or NULL
43272d5ee99bSBarry Smith 
43282d5ee99bSBarry Smith    Level: intermediate
43292d5ee99bSBarry Smith 
43302d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
43312d5ee99bSBarry Smith 
43322d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
43332d5ee99bSBarry Smith @*/
43342d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
43352d5ee99bSBarry Smith {
43362d5ee99bSBarry Smith   PetscErrorCode    ierr;
43372d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
43382d5ee99bSBarry Smith   PetscDraw         draw;
43396934998bSLisandro Dalcin   PetscDrawAxis     axis;
43402d5ee99bSBarry Smith   PetscInt          n;
43412d5ee99bSBarry Smith   PetscMPIInt       size;
43426934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
43432d5ee99bSBarry Smith   char              time[32];
43442d5ee99bSBarry Smith   const PetscScalar *U;
43452d5ee99bSBarry Smith 
43462d5ee99bSBarry Smith   PetscFunctionBegin;
43476934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
43486934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
43492d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
43506934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
43512d5ee99bSBarry Smith 
43522d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
43536934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
43546934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
43556934998bSLisandro Dalcin   if (!step) {
43566934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
43576934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
43586934998bSLisandro Dalcin   }
43592d5ee99bSBarry Smith 
43602d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
43616934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
43626934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4363a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
43646934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
43652d5ee99bSBarry Smith 
43666934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
43676934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
43682d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
43694b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
437085b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
43712d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
437251fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
43732d5ee99bSBarry Smith   }
43746934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
43752d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
437656d62c8fSLisandro Dalcin   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
43776934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
43782d5ee99bSBarry Smith   PetscFunctionReturn(0);
43792d5ee99bSBarry Smith }
43802d5ee99bSBarry Smith 
43816083293cSBarry Smith /*@C
438283a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
43836083293cSBarry Smith 
43846083293cSBarry Smith    Collective on TS
43856083293cSBarry Smith 
43866083293cSBarry Smith    Input Parameters:
43876083293cSBarry Smith .    ctx - the monitor context
43886083293cSBarry Smith 
43896083293cSBarry Smith    Level: intermediate
43906083293cSBarry Smith 
43916083293cSBarry Smith .keywords: TS,  vector, monitor, view
43926083293cSBarry Smith 
439383a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
43946083293cSBarry Smith @*/
439583a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
43966083293cSBarry Smith {
43976083293cSBarry Smith   PetscErrorCode ierr;
43986083293cSBarry Smith 
43996083293cSBarry Smith   PetscFunctionBegin;
440083a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
440183a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
440283a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
44036083293cSBarry Smith   PetscFunctionReturn(0);
44046083293cSBarry Smith }
44056083293cSBarry Smith 
44066083293cSBarry Smith /*@C
440783a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
44086083293cSBarry Smith 
44096083293cSBarry Smith    Collective on TS
44106083293cSBarry Smith 
44116083293cSBarry Smith    Input Parameter:
44126083293cSBarry Smith .    ts - time-step context
44136083293cSBarry Smith 
44146083293cSBarry Smith    Output Patameter:
44156083293cSBarry Smith .    ctx - the monitor context
44166083293cSBarry Smith 
441799fdda47SBarry Smith    Options Database:
441899fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
441999fdda47SBarry Smith 
44206083293cSBarry Smith    Level: intermediate
44216083293cSBarry Smith 
44226083293cSBarry Smith .keywords: TS,  vector, monitor, view
44236083293cSBarry Smith 
442483a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
44256083293cSBarry Smith @*/
442683a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
44276083293cSBarry Smith {
44286083293cSBarry Smith   PetscErrorCode   ierr;
44296083293cSBarry Smith 
44306083293cSBarry Smith   PetscFunctionBegin;
4431b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
443283a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4433e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4434bbd56ea5SKarl Rupp 
443583a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4436473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4437c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4438473a3ab2SBarry Smith 
4439473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4440c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
44416083293cSBarry Smith   PetscFunctionReturn(0);
44426083293cSBarry Smith }
44436083293cSBarry Smith 
44443a471f94SBarry Smith /*@C
44450ed3bfb6SBarry Smith    TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling
44460ed3bfb6SBarry Smith    VecView() for the solution provided by TSSetSolutionFunction() at each timestep
44470ed3bfb6SBarry Smith 
44480ed3bfb6SBarry Smith    Collective on TS
44490ed3bfb6SBarry Smith 
44500ed3bfb6SBarry Smith    Input Parameters:
44510ed3bfb6SBarry Smith +  ts - the TS context
44520ed3bfb6SBarry Smith .  step - current time-step
44530ed3bfb6SBarry Smith .  ptime - current time
44540ed3bfb6SBarry Smith -  dummy - either a viewer or NULL
44550ed3bfb6SBarry Smith 
44560ed3bfb6SBarry Smith    Options Database:
44570ed3bfb6SBarry Smith .  -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
44580ed3bfb6SBarry Smith 
44590ed3bfb6SBarry Smith    Level: intermediate
44600ed3bfb6SBarry Smith 
44610ed3bfb6SBarry Smith .keywords: TS,  vector, monitor, view
44620ed3bfb6SBarry Smith 
44630ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
44640ed3bfb6SBarry Smith @*/
44650ed3bfb6SBarry Smith PetscErrorCode  TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
44660ed3bfb6SBarry Smith {
44670ed3bfb6SBarry Smith   PetscErrorCode   ierr;
44680ed3bfb6SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
44690ed3bfb6SBarry Smith   PetscViewer      viewer = ctx->viewer;
44700ed3bfb6SBarry Smith   Vec              work;
44710ed3bfb6SBarry Smith 
44720ed3bfb6SBarry Smith   PetscFunctionBegin;
44730ed3bfb6SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
44740ed3bfb6SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
44750ed3bfb6SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
44760ed3bfb6SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
44770ed3bfb6SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
44780ed3bfb6SBarry Smith   PetscFunctionReturn(0);
44790ed3bfb6SBarry Smith }
44800ed3bfb6SBarry Smith 
44810ed3bfb6SBarry Smith /*@C
448283a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
44833a471f94SBarry Smith    VecView() for the error at each timestep
44843a471f94SBarry Smith 
44853a471f94SBarry Smith    Collective on TS
44863a471f94SBarry Smith 
44873a471f94SBarry Smith    Input Parameters:
44883a471f94SBarry Smith +  ts - the TS context
44893a471f94SBarry Smith .  step - current time-step
44903a471f94SBarry Smith .  ptime - current time
44910298fd71SBarry Smith -  dummy - either a viewer or NULL
44923a471f94SBarry Smith 
44930ed3bfb6SBarry Smith    Options Database:
44940ed3bfb6SBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
44950ed3bfb6SBarry Smith 
44963a471f94SBarry Smith    Level: intermediate
44973a471f94SBarry Smith 
44983a471f94SBarry Smith .keywords: TS,  vector, monitor, view
44993a471f94SBarry Smith 
45000ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
45013a471f94SBarry Smith @*/
45020910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
45033a471f94SBarry Smith {
45043a471f94SBarry Smith   PetscErrorCode   ierr;
450583a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
450683a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
45073a471f94SBarry Smith   Vec              work;
45083a471f94SBarry Smith 
45093a471f94SBarry Smith   PetscFunctionBegin;
4510b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
45110910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
45123a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
45130910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
45143a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
45153a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
45163a471f94SBarry Smith   PetscFunctionReturn(0);
45173a471f94SBarry Smith }
45183a471f94SBarry Smith 
4519af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
45206c699258SBarry Smith /*@
45212a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
45226c699258SBarry Smith 
45233f9fe445SBarry Smith    Logically Collective on TS and DM
45246c699258SBarry Smith 
45256c699258SBarry Smith    Input Parameters:
45262a808120SBarry Smith +  ts - the ODE integrator object
45272a808120SBarry Smith -  dm - the dm, cannot be NULL
45286c699258SBarry Smith 
4529e03a659cSJed Brown    Notes:
4530e03a659cSJed Brown    A DM can only be used for solving one problem at a time because information about the problem is stored on the DM,
4531e03a659cSJed Brown    even when not using interfaces like DMTSSetIFunction().  Use DMClone() to get a distinct DM when solving
4532e03a659cSJed Brown    different problems using the same function space.
4533e03a659cSJed Brown 
45346c699258SBarry Smith    Level: intermediate
45356c699258SBarry Smith 
45366c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
45376c699258SBarry Smith @*/
45387087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
45396c699258SBarry Smith {
45406c699258SBarry Smith   PetscErrorCode ierr;
4541089b2837SJed Brown   SNES           snes;
4542942e3340SBarry Smith   DMTS           tsdm;
45436c699258SBarry Smith 
45446c699258SBarry Smith   PetscFunctionBegin;
45450700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45462a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
454770663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4548942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
45492a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4550942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4551942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
455224989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
455324989b8cSPeter Brune         tsdm->originaldm = dm;
455424989b8cSPeter Brune       }
455524989b8cSPeter Brune     }
45566bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
455724989b8cSPeter Brune   }
45586c699258SBarry Smith   ts->dm = dm;
4559bbd56ea5SKarl Rupp 
4560089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4561089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
45626c699258SBarry Smith   PetscFunctionReturn(0);
45636c699258SBarry Smith }
45646c699258SBarry Smith 
45656c699258SBarry Smith /*@
45666c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
45676c699258SBarry Smith 
45683f9fe445SBarry Smith    Not Collective
45696c699258SBarry Smith 
45706c699258SBarry Smith    Input Parameter:
45716c699258SBarry Smith . ts - the preconditioner context
45726c699258SBarry Smith 
45736c699258SBarry Smith    Output Parameter:
45746c699258SBarry Smith .  dm - the dm
45756c699258SBarry Smith 
45766c699258SBarry Smith    Level: intermediate
45776c699258SBarry Smith 
45786c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
45796c699258SBarry Smith @*/
45807087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
45816c699258SBarry Smith {
4582496e6a7aSJed Brown   PetscErrorCode ierr;
4583496e6a7aSJed Brown 
45846c699258SBarry Smith   PetscFunctionBegin;
45850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4586496e6a7aSJed Brown   if (!ts->dm) {
4587ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4588496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4589496e6a7aSJed Brown   }
45906c699258SBarry Smith   *dm = ts->dm;
45916c699258SBarry Smith   PetscFunctionReturn(0);
45926c699258SBarry Smith }
45931713a123SBarry Smith 
45940f5c6efeSJed Brown /*@
45950f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
45960f5c6efeSJed Brown 
45973f9fe445SBarry Smith    Logically Collective on SNES
45980f5c6efeSJed Brown 
45990f5c6efeSJed Brown    Input Parameter:
4600d42a1c89SJed Brown + snes - nonlinear solver
46010910c330SBarry Smith . U - the current state at which to evaluate the residual
4602d42a1c89SJed Brown - ctx - user context, must be a TS
46030f5c6efeSJed Brown 
46040f5c6efeSJed Brown    Output Parameter:
46050f5c6efeSJed Brown . F - the nonlinear residual
46060f5c6efeSJed Brown 
46070f5c6efeSJed Brown    Notes:
46080f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
46090f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
46100f5c6efeSJed Brown 
46110f5c6efeSJed Brown    Level: advanced
46120f5c6efeSJed Brown 
46130f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
46140f5c6efeSJed Brown @*/
46150910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
46160f5c6efeSJed Brown {
46170f5c6efeSJed Brown   TS             ts = (TS)ctx;
46180f5c6efeSJed Brown   PetscErrorCode ierr;
46190f5c6efeSJed Brown 
46200f5c6efeSJed Brown   PetscFunctionBegin;
46210f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
46220910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
46230f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
46240f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
46250910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
46260f5c6efeSJed Brown   PetscFunctionReturn(0);
46270f5c6efeSJed Brown }
46280f5c6efeSJed Brown 
46290f5c6efeSJed Brown /*@
46300f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
46310f5c6efeSJed Brown 
46320f5c6efeSJed Brown    Collective on SNES
46330f5c6efeSJed Brown 
46340f5c6efeSJed Brown    Input Parameter:
46350f5c6efeSJed Brown + snes - nonlinear solver
46360910c330SBarry Smith . U - the current state at which to evaluate the residual
46370f5c6efeSJed Brown - ctx - user context, must be a TS
46380f5c6efeSJed Brown 
46390f5c6efeSJed Brown    Output Parameter:
46400f5c6efeSJed Brown + A - the Jacobian
46410f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
46420f5c6efeSJed Brown - flag - indicates any structure change in the matrix
46430f5c6efeSJed Brown 
46440f5c6efeSJed Brown    Notes:
46450f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
46460f5c6efeSJed Brown 
46470f5c6efeSJed Brown    Level: developer
46480f5c6efeSJed Brown 
46490f5c6efeSJed Brown .seealso: SNESSetJacobian()
46500f5c6efeSJed Brown @*/
4651d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
46520f5c6efeSJed Brown {
46530f5c6efeSJed Brown   TS             ts = (TS)ctx;
46540f5c6efeSJed Brown   PetscErrorCode ierr;
46550f5c6efeSJed Brown 
46560f5c6efeSJed Brown   PetscFunctionBegin;
46570f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
46580910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
46590f5c6efeSJed Brown   PetscValidPointer(A,3);
466094ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
46610f5c6efeSJed Brown   PetscValidPointer(B,4);
466294ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
46630f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4664d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
46650f5c6efeSJed Brown   PetscFunctionReturn(0);
46660f5c6efeSJed Brown }
4667325fc9f4SBarry Smith 
46680e4ef248SJed Brown /*@C
46699ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
46700e4ef248SJed Brown 
46710e4ef248SJed Brown    Collective on TS
46720e4ef248SJed Brown 
46730e4ef248SJed Brown    Input Arguments:
46740e4ef248SJed Brown +  ts - time stepping context
46750e4ef248SJed Brown .  t - time at which to evaluate
46760910c330SBarry Smith .  U - state at which to evaluate
46770e4ef248SJed Brown -  ctx - context
46780e4ef248SJed Brown 
46790e4ef248SJed Brown    Output Arguments:
46800e4ef248SJed Brown .  F - right hand side
46810e4ef248SJed Brown 
46820e4ef248SJed Brown    Level: intermediate
46830e4ef248SJed Brown 
46840e4ef248SJed Brown    Notes:
46850e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
46860e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
46870e4ef248SJed Brown 
46880e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
46890e4ef248SJed Brown @*/
46900910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
46910e4ef248SJed Brown {
46920e4ef248SJed Brown   PetscErrorCode ierr;
46930e4ef248SJed Brown   Mat            Arhs,Brhs;
46940e4ef248SJed Brown 
46950e4ef248SJed Brown   PetscFunctionBegin;
46960e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
4697d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
46980910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
46990e4ef248SJed Brown   PetscFunctionReturn(0);
47000e4ef248SJed Brown }
47010e4ef248SJed Brown 
47020e4ef248SJed Brown /*@C
47030e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
47040e4ef248SJed Brown 
47050e4ef248SJed Brown    Collective on TS
47060e4ef248SJed Brown 
47070e4ef248SJed Brown    Input Arguments:
47080e4ef248SJed Brown +  ts - time stepping context
47090e4ef248SJed Brown .  t - time at which to evaluate
47100910c330SBarry Smith .  U - state at which to evaluate
47110e4ef248SJed Brown -  ctx - context
47120e4ef248SJed Brown 
47130e4ef248SJed Brown    Output Arguments:
47140e4ef248SJed Brown +  A - pointer to operator
47150e4ef248SJed Brown .  B - pointer to preconditioning matrix
47160e4ef248SJed Brown -  flg - matrix structure flag
47170e4ef248SJed Brown 
47180e4ef248SJed Brown    Level: intermediate
47190e4ef248SJed Brown 
47200e4ef248SJed Brown    Notes:
47210e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
47220e4ef248SJed Brown 
47230e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
47240e4ef248SJed Brown @*/
4725d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
47260e4ef248SJed Brown {
47270e4ef248SJed Brown   PetscFunctionBegin;
47280e4ef248SJed Brown   PetscFunctionReturn(0);
47290e4ef248SJed Brown }
47300e4ef248SJed Brown 
47310026cea9SSean Farley /*@C
47320026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
47330026cea9SSean Farley 
47340026cea9SSean Farley    Collective on TS
47350026cea9SSean Farley 
47360026cea9SSean Farley    Input Arguments:
47370026cea9SSean Farley +  ts - time stepping context
47380026cea9SSean Farley .  t - time at which to evaluate
47390910c330SBarry Smith .  U - state at which to evaluate
47400910c330SBarry Smith .  Udot - time derivative of state vector
47410026cea9SSean Farley -  ctx - context
47420026cea9SSean Farley 
47430026cea9SSean Farley    Output Arguments:
47440026cea9SSean Farley .  F - left hand side
47450026cea9SSean Farley 
47460026cea9SSean Farley    Level: intermediate
47470026cea9SSean Farley 
47480026cea9SSean Farley    Notes:
47490910c330SBarry 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
47500026cea9SSean Farley    user is required to write their own TSComputeIFunction.
47510026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
47520026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
47530026cea9SSean Farley 
47549ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
47559ae8fd06SBarry Smith 
47569ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
47570026cea9SSean Farley @*/
47580910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
47590026cea9SSean Farley {
47600026cea9SSean Farley   PetscErrorCode ierr;
47610026cea9SSean Farley   Mat            A,B;
47620026cea9SSean Farley 
47630026cea9SSean Farley   PetscFunctionBegin;
47640298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
4765d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
47660910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
47670026cea9SSean Farley   PetscFunctionReturn(0);
47680026cea9SSean Farley }
47690026cea9SSean Farley 
47700026cea9SSean Farley /*@C
4771b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
47720026cea9SSean Farley 
47730026cea9SSean Farley    Collective on TS
47740026cea9SSean Farley 
47750026cea9SSean Farley    Input Arguments:
47760026cea9SSean Farley +  ts - time stepping context
47770026cea9SSean Farley .  t - time at which to evaluate
47780910c330SBarry Smith .  U - state at which to evaluate
47790910c330SBarry Smith .  Udot - time derivative of state vector
47800026cea9SSean Farley .  shift - shift to apply
47810026cea9SSean Farley -  ctx - context
47820026cea9SSean Farley 
47830026cea9SSean Farley    Output Arguments:
47840026cea9SSean Farley +  A - pointer to operator
47850026cea9SSean Farley .  B - pointer to preconditioning matrix
47860026cea9SSean Farley -  flg - matrix structure flag
47870026cea9SSean Farley 
4788b41af12eSJed Brown    Level: advanced
47890026cea9SSean Farley 
47900026cea9SSean Farley    Notes:
47910026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
47920026cea9SSean Farley 
4793b41af12eSJed Brown    It is only appropriate for problems of the form
4794b41af12eSJed Brown 
4795b41af12eSJed Brown $     M Udot = F(U,t)
4796b41af12eSJed Brown 
4797b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4798b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4799b41af12eSJed Brown   an implicit operator of the form
4800b41af12eSJed Brown 
4801b41af12eSJed Brown $    shift*M + J
4802b41af12eSJed Brown 
4803b41af12eSJed 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
4804b41af12eSJed Brown   a copy of M or reassemble it when requested.
4805b41af12eSJed Brown 
48060026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
48070026cea9SSean Farley @*/
4808d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
48090026cea9SSean Farley {
4810b41af12eSJed Brown   PetscErrorCode ierr;
4811b41af12eSJed Brown 
48120026cea9SSean Farley   PetscFunctionBegin;
481394ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4814b41af12eSJed Brown   ts->ijacobian.shift = shift;
48150026cea9SSean Farley   PetscFunctionReturn(0);
48160026cea9SSean Farley }
4817b41af12eSJed Brown 
4818e817cc15SEmil Constantinescu /*@
4819e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4820e817cc15SEmil Constantinescu 
4821e817cc15SEmil Constantinescu    Not Collective
4822e817cc15SEmil Constantinescu 
4823e817cc15SEmil Constantinescu    Input Parameter:
4824e817cc15SEmil Constantinescu .  ts - the TS context
4825e817cc15SEmil Constantinescu 
4826e817cc15SEmil Constantinescu    Output Parameter:
48274e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4828e817cc15SEmil Constantinescu 
4829e817cc15SEmil Constantinescu    Level: beginner
4830e817cc15SEmil Constantinescu 
4831e817cc15SEmil Constantinescu .keywords: TS, equation type
4832e817cc15SEmil Constantinescu 
4833e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4834e817cc15SEmil Constantinescu @*/
4835e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4836e817cc15SEmil Constantinescu {
4837e817cc15SEmil Constantinescu   PetscFunctionBegin;
4838e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4839e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4840e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4841e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4842e817cc15SEmil Constantinescu }
4843e817cc15SEmil Constantinescu 
4844e817cc15SEmil Constantinescu /*@
4845e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4846e817cc15SEmil Constantinescu 
4847e817cc15SEmil Constantinescu    Not Collective
4848e817cc15SEmil Constantinescu 
4849e817cc15SEmil Constantinescu    Input Parameter:
4850e817cc15SEmil Constantinescu +  ts - the TS context
48511297b224SEmil Constantinescu -  equation_type - see TSEquationType
4852e817cc15SEmil Constantinescu 
4853e817cc15SEmil Constantinescu    Level: advanced
4854e817cc15SEmil Constantinescu 
4855e817cc15SEmil Constantinescu .keywords: TS, equation type
4856e817cc15SEmil Constantinescu 
4857e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4858e817cc15SEmil Constantinescu @*/
4859e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4860e817cc15SEmil Constantinescu {
4861e817cc15SEmil Constantinescu   PetscFunctionBegin;
4862e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4863e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4864e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4865e817cc15SEmil Constantinescu }
48660026cea9SSean Farley 
48674af1b03aSJed Brown /*@
48684af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
48694af1b03aSJed Brown 
48704af1b03aSJed Brown    Not Collective
48714af1b03aSJed Brown 
48724af1b03aSJed Brown    Input Parameter:
48734af1b03aSJed Brown .  ts - the TS context
48744af1b03aSJed Brown 
48754af1b03aSJed Brown    Output Parameter:
48764af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
48774af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
48784af1b03aSJed Brown 
4879487e0bb9SJed Brown    Level: beginner
48804af1b03aSJed Brown 
4881cd652676SJed Brown    Notes:
4882cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
48834af1b03aSJed Brown 
48844af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
48854af1b03aSJed Brown 
48864af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
48874af1b03aSJed Brown @*/
48884af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
48894af1b03aSJed Brown {
48904af1b03aSJed Brown   PetscFunctionBegin;
48914af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
48924af1b03aSJed Brown   PetscValidPointer(reason,2);
48934af1b03aSJed Brown   *reason = ts->reason;
48944af1b03aSJed Brown   PetscFunctionReturn(0);
48954af1b03aSJed Brown }
48964af1b03aSJed Brown 
4897d6ad946cSShri Abhyankar /*@
4898d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
4899d6ad946cSShri Abhyankar 
4900d6ad946cSShri Abhyankar    Not Collective
4901d6ad946cSShri Abhyankar 
4902d6ad946cSShri Abhyankar    Input Parameter:
4903d6ad946cSShri Abhyankar +  ts - the TS context
4904d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
4905d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4906d6ad946cSShri Abhyankar 
4907f5abba47SShri Abhyankar    Level: advanced
4908d6ad946cSShri Abhyankar 
4909d6ad946cSShri Abhyankar    Notes:
4910d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
4911d6ad946cSShri Abhyankar 
4912d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
4913d6ad946cSShri Abhyankar 
4914d6ad946cSShri Abhyankar .seealso: TSConvergedReason
4915d6ad946cSShri Abhyankar @*/
4916d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
4917d6ad946cSShri Abhyankar {
4918d6ad946cSShri Abhyankar   PetscFunctionBegin;
4919d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4920d6ad946cSShri Abhyankar   ts->reason = reason;
4921d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
4922d6ad946cSShri Abhyankar }
4923d6ad946cSShri Abhyankar 
4924cc708dedSBarry Smith /*@
4925cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
4926cc708dedSBarry Smith 
4927cc708dedSBarry Smith    Not Collective
4928cc708dedSBarry Smith 
4929cc708dedSBarry Smith    Input Parameter:
4930cc708dedSBarry Smith .  ts - the TS context
4931cc708dedSBarry Smith 
4932cc708dedSBarry Smith    Output Parameter:
493319eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
4934cc708dedSBarry Smith 
4935487e0bb9SJed Brown    Level: beginner
4936cc708dedSBarry Smith 
4937cc708dedSBarry Smith    Notes:
4938cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
4939cc708dedSBarry Smith 
4940cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
4941cc708dedSBarry Smith 
4942cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
4943cc708dedSBarry Smith @*/
4944cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
4945cc708dedSBarry Smith {
4946cc708dedSBarry Smith   PetscFunctionBegin;
4947cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4948cc708dedSBarry Smith   PetscValidPointer(ftime,2);
4949cc708dedSBarry Smith   *ftime = ts->solvetime;
4950cc708dedSBarry Smith   PetscFunctionReturn(0);
4951cc708dedSBarry Smith }
4952cc708dedSBarry Smith 
49532c18e0fdSBarry Smith /*@
49545ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
49559f67acb7SJed Brown    used by the time integrator.
49569f67acb7SJed Brown 
49579f67acb7SJed Brown    Not Collective
49589f67acb7SJed Brown 
49599f67acb7SJed Brown    Input Parameter:
49609f67acb7SJed Brown .  ts - TS context
49619f67acb7SJed Brown 
49629f67acb7SJed Brown    Output Parameter:
49639f67acb7SJed Brown .  nits - number of nonlinear iterations
49649f67acb7SJed Brown 
49659f67acb7SJed Brown    Notes:
49669f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
49679f67acb7SJed Brown 
49689f67acb7SJed Brown    Level: intermediate
49699f67acb7SJed Brown 
49709f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
49719f67acb7SJed Brown 
49725ef26d82SJed Brown .seealso:  TSGetKSPIterations()
49739f67acb7SJed Brown @*/
49745ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
49759f67acb7SJed Brown {
49769f67acb7SJed Brown   PetscFunctionBegin;
49779f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49789f67acb7SJed Brown   PetscValidIntPointer(nits,2);
49795ef26d82SJed Brown   *nits = ts->snes_its;
49809f67acb7SJed Brown   PetscFunctionReturn(0);
49819f67acb7SJed Brown }
49829f67acb7SJed Brown 
49839f67acb7SJed Brown /*@
49845ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
49859f67acb7SJed Brown    used by the time integrator.
49869f67acb7SJed Brown 
49879f67acb7SJed Brown    Not Collective
49889f67acb7SJed Brown 
49899f67acb7SJed Brown    Input Parameter:
49909f67acb7SJed Brown .  ts - TS context
49919f67acb7SJed Brown 
49929f67acb7SJed Brown    Output Parameter:
49939f67acb7SJed Brown .  lits - number of linear iterations
49949f67acb7SJed Brown 
49959f67acb7SJed Brown    Notes:
49969f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
49979f67acb7SJed Brown 
49989f67acb7SJed Brown    Level: intermediate
49999f67acb7SJed Brown 
50009f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
50019f67acb7SJed Brown 
50025ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
50039f67acb7SJed Brown @*/
50045ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
50059f67acb7SJed Brown {
50069f67acb7SJed Brown   PetscFunctionBegin;
50079f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
50089f67acb7SJed Brown   PetscValidIntPointer(lits,2);
50095ef26d82SJed Brown   *lits = ts->ksp_its;
50109f67acb7SJed Brown   PetscFunctionReturn(0);
50119f67acb7SJed Brown }
50129f67acb7SJed Brown 
5013cef5090cSJed Brown /*@
5014cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5015cef5090cSJed Brown 
5016cef5090cSJed Brown    Not Collective
5017cef5090cSJed Brown 
5018cef5090cSJed Brown    Input Parameter:
5019cef5090cSJed Brown .  ts - TS context
5020cef5090cSJed Brown 
5021cef5090cSJed Brown    Output Parameter:
5022cef5090cSJed Brown .  rejects - number of steps rejected
5023cef5090cSJed Brown 
5024cef5090cSJed Brown    Notes:
5025cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5026cef5090cSJed Brown 
5027cef5090cSJed Brown    Level: intermediate
5028cef5090cSJed Brown 
5029cef5090cSJed Brown .keywords: TS, get, number
5030cef5090cSJed Brown 
50315ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5032cef5090cSJed Brown @*/
5033cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5034cef5090cSJed Brown {
5035cef5090cSJed Brown   PetscFunctionBegin;
5036cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5037cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5038cef5090cSJed Brown   *rejects = ts->reject;
5039cef5090cSJed Brown   PetscFunctionReturn(0);
5040cef5090cSJed Brown }
5041cef5090cSJed Brown 
5042cef5090cSJed Brown /*@
5043cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5044cef5090cSJed Brown 
5045cef5090cSJed Brown    Not Collective
5046cef5090cSJed Brown 
5047cef5090cSJed Brown    Input Parameter:
5048cef5090cSJed Brown .  ts - TS context
5049cef5090cSJed Brown 
5050cef5090cSJed Brown    Output Parameter:
5051cef5090cSJed Brown .  fails - number of failed nonlinear solves
5052cef5090cSJed Brown 
5053cef5090cSJed Brown    Notes:
5054cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5055cef5090cSJed Brown 
5056cef5090cSJed Brown    Level: intermediate
5057cef5090cSJed Brown 
5058cef5090cSJed Brown .keywords: TS, get, number
5059cef5090cSJed Brown 
50605ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5061cef5090cSJed Brown @*/
5062cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5063cef5090cSJed Brown {
5064cef5090cSJed Brown   PetscFunctionBegin;
5065cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5066cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5067cef5090cSJed Brown   *fails = ts->num_snes_failures;
5068cef5090cSJed Brown   PetscFunctionReturn(0);
5069cef5090cSJed Brown }
5070cef5090cSJed Brown 
5071cef5090cSJed Brown /*@
5072cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5073cef5090cSJed Brown 
5074cef5090cSJed Brown    Not Collective
5075cef5090cSJed Brown 
5076cef5090cSJed Brown    Input Parameter:
5077cef5090cSJed Brown +  ts - TS context
5078cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5079cef5090cSJed Brown 
5080cef5090cSJed Brown    Notes:
5081cef5090cSJed Brown    The counter is reset to zero for each step
5082cef5090cSJed Brown 
5083cef5090cSJed Brown    Options Database Key:
5084cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5085cef5090cSJed Brown 
5086cef5090cSJed Brown    Level: intermediate
5087cef5090cSJed Brown 
5088cef5090cSJed Brown .keywords: TS, set, maximum, number
5089cef5090cSJed Brown 
50905ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5091cef5090cSJed Brown @*/
5092cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5093cef5090cSJed Brown {
5094cef5090cSJed Brown   PetscFunctionBegin;
5095cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5096cef5090cSJed Brown   ts->max_reject = rejects;
5097cef5090cSJed Brown   PetscFunctionReturn(0);
5098cef5090cSJed Brown }
5099cef5090cSJed Brown 
5100cef5090cSJed Brown /*@
5101cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5102cef5090cSJed Brown 
5103cef5090cSJed Brown    Not Collective
5104cef5090cSJed Brown 
5105cef5090cSJed Brown    Input Parameter:
5106cef5090cSJed Brown +  ts - TS context
5107cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5108cef5090cSJed Brown 
5109cef5090cSJed Brown    Notes:
5110cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5111cef5090cSJed Brown 
5112cef5090cSJed Brown    Options Database Key:
5113cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5114cef5090cSJed Brown 
5115cef5090cSJed Brown    Level: intermediate
5116cef5090cSJed Brown 
5117cef5090cSJed Brown .keywords: TS, set, maximum, number
5118cef5090cSJed Brown 
51195ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5120cef5090cSJed Brown @*/
5121cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5122cef5090cSJed Brown {
5123cef5090cSJed Brown   PetscFunctionBegin;
5124cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5125cef5090cSJed Brown   ts->max_snes_failures = fails;
5126cef5090cSJed Brown   PetscFunctionReturn(0);
5127cef5090cSJed Brown }
5128cef5090cSJed Brown 
5129cef5090cSJed Brown /*@
5130cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5131cef5090cSJed Brown 
5132cef5090cSJed Brown    Not Collective
5133cef5090cSJed Brown 
5134cef5090cSJed Brown    Input Parameter:
5135cef5090cSJed Brown +  ts - TS context
5136cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5137cef5090cSJed Brown 
5138cef5090cSJed Brown    Options Database Key:
5139cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5140cef5090cSJed Brown 
5141cef5090cSJed Brown    Level: intermediate
5142cef5090cSJed Brown 
5143cef5090cSJed Brown .keywords: TS, set, error
5144cef5090cSJed Brown 
51455ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5146cef5090cSJed Brown @*/
5147cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5148cef5090cSJed Brown {
5149cef5090cSJed Brown   PetscFunctionBegin;
5150cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5151cef5090cSJed Brown   ts->errorifstepfailed = err;
5152cef5090cSJed Brown   PetscFunctionReturn(0);
5153cef5090cSJed Brown }
5154cef5090cSJed Brown 
5155fb1732b5SBarry Smith /*@C
5156fde5950dSBarry 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
5157fb1732b5SBarry Smith 
5158fb1732b5SBarry Smith    Collective on TS
5159fb1732b5SBarry Smith 
5160fb1732b5SBarry Smith    Input Parameters:
5161fb1732b5SBarry Smith +  ts - the TS context
5162fb1732b5SBarry Smith .  step - current time-step
5163fb1732b5SBarry Smith .  ptime - current time
51640910c330SBarry Smith .  u - current state
5165721cd6eeSBarry Smith -  vf - viewer and its format
5166fb1732b5SBarry Smith 
5167fb1732b5SBarry Smith    Level: intermediate
5168fb1732b5SBarry Smith 
5169fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
5170fb1732b5SBarry Smith 
5171fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5172fb1732b5SBarry Smith @*/
5173721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5174fb1732b5SBarry Smith {
5175fb1732b5SBarry Smith   PetscErrorCode ierr;
5176fb1732b5SBarry Smith 
5177fb1732b5SBarry Smith   PetscFunctionBegin;
5178721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5179721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5180721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5181ed81e22dSJed Brown   PetscFunctionReturn(0);
5182ed81e22dSJed Brown }
5183ed81e22dSJed Brown 
5184ed81e22dSJed Brown /*@C
5185ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5186ed81e22dSJed Brown 
5187ed81e22dSJed Brown    Collective on TS
5188ed81e22dSJed Brown 
5189ed81e22dSJed Brown    Input Parameters:
5190ed81e22dSJed Brown +  ts - the TS context
5191ed81e22dSJed Brown .  step - current time-step
5192ed81e22dSJed Brown .  ptime - current time
51930910c330SBarry Smith .  u - current state
5194ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5195ed81e22dSJed Brown 
5196ed81e22dSJed Brown    Level: intermediate
5197ed81e22dSJed Brown 
5198ed81e22dSJed Brown    Notes:
5199ed81e22dSJed 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.
5200ed81e22dSJed Brown    These are named according to the file name template.
5201ed81e22dSJed Brown 
5202ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5203ed81e22dSJed Brown 
5204ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5205ed81e22dSJed Brown 
5206ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5207ed81e22dSJed Brown @*/
52080910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5209ed81e22dSJed Brown {
5210ed81e22dSJed Brown   PetscErrorCode ierr;
5211ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5212ed81e22dSJed Brown   PetscViewer    viewer;
5213ed81e22dSJed Brown 
5214ed81e22dSJed Brown   PetscFunctionBegin;
521563e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
52168caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5217ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
52180910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5219ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5220ed81e22dSJed Brown   PetscFunctionReturn(0);
5221ed81e22dSJed Brown }
5222ed81e22dSJed Brown 
5223ed81e22dSJed Brown /*@C
5224ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5225ed81e22dSJed Brown 
5226ed81e22dSJed Brown    Collective on TS
5227ed81e22dSJed Brown 
5228ed81e22dSJed Brown    Input Parameters:
5229ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5230ed81e22dSJed Brown 
5231ed81e22dSJed Brown    Level: intermediate
5232ed81e22dSJed Brown 
5233ed81e22dSJed Brown    Note:
5234ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5235ed81e22dSJed Brown 
5236ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5237ed81e22dSJed Brown 
5238ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5239ed81e22dSJed Brown @*/
5240ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5241ed81e22dSJed Brown {
5242ed81e22dSJed Brown   PetscErrorCode ierr;
5243ed81e22dSJed Brown 
5244ed81e22dSJed Brown   PetscFunctionBegin;
5245ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5246fb1732b5SBarry Smith   PetscFunctionReturn(0);
5247fb1732b5SBarry Smith }
5248fb1732b5SBarry Smith 
524984df9cb4SJed Brown /*@
5250552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
525184df9cb4SJed Brown 
5252ed81e22dSJed Brown    Collective on TS if controller has not been created yet
525384df9cb4SJed Brown 
525484df9cb4SJed Brown    Input Arguments:
5255ed81e22dSJed Brown .  ts - time stepping context
525684df9cb4SJed Brown 
525784df9cb4SJed Brown    Output Arguments:
5258ed81e22dSJed Brown .  adapt - adaptive controller
525984df9cb4SJed Brown 
526084df9cb4SJed Brown    Level: intermediate
526184df9cb4SJed Brown 
5262ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
526384df9cb4SJed Brown @*/
5264552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
526584df9cb4SJed Brown {
526684df9cb4SJed Brown   PetscErrorCode ierr;
526784df9cb4SJed Brown 
526884df9cb4SJed Brown   PetscFunctionBegin;
526984df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5270bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
527184df9cb4SJed Brown   if (!ts->adapt) {
5272ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
52733bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
52741c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
527584df9cb4SJed Brown   }
5276bec58848SLisandro Dalcin   *adapt = ts->adapt;
527784df9cb4SJed Brown   PetscFunctionReturn(0);
527884df9cb4SJed Brown }
5279d6ebe24aSShri Abhyankar 
52801c3436cfSJed Brown /*@
52811c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
52821c3436cfSJed Brown 
52831c3436cfSJed Brown    Logically Collective
52841c3436cfSJed Brown 
52851c3436cfSJed Brown    Input Arguments:
52861c3436cfSJed Brown +  ts - time integration context
52871c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
52880298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
52891c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
52900298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
52911c3436cfSJed Brown 
5292a3cdaa26SBarry Smith    Options Database keys:
5293a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5294a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5295a3cdaa26SBarry Smith 
52963ff766beSShri Abhyankar    Notes:
52973ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
52983ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
52993ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
53003ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
53013ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
53023ff766beSShri Abhyankar    differential variables.
53033ff766beSShri Abhyankar 
53041c3436cfSJed Brown    Level: beginner
53051c3436cfSJed Brown 
5306c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
53071c3436cfSJed Brown @*/
53081c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
53091c3436cfSJed Brown {
53101c3436cfSJed Brown   PetscErrorCode ierr;
53111c3436cfSJed Brown 
53121c3436cfSJed Brown   PetscFunctionBegin;
5313c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
53141c3436cfSJed Brown   if (vatol) {
53151c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
53161c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
53171c3436cfSJed Brown     ts->vatol = vatol;
53181c3436cfSJed Brown   }
5319c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
53201c3436cfSJed Brown   if (vrtol) {
53211c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
53221c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
53231c3436cfSJed Brown     ts->vrtol = vrtol;
53241c3436cfSJed Brown   }
53251c3436cfSJed Brown   PetscFunctionReturn(0);
53261c3436cfSJed Brown }
53271c3436cfSJed Brown 
5328c5033834SJed Brown /*@
5329c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5330c5033834SJed Brown 
5331c5033834SJed Brown    Logically Collective
5332c5033834SJed Brown 
5333c5033834SJed Brown    Input Arguments:
5334c5033834SJed Brown .  ts - time integration context
5335c5033834SJed Brown 
5336c5033834SJed Brown    Output Arguments:
53370298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
53380298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
53390298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
53400298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5341c5033834SJed Brown 
5342c5033834SJed Brown    Level: beginner
5343c5033834SJed Brown 
5344c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5345c5033834SJed Brown @*/
5346c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5347c5033834SJed Brown {
5348c5033834SJed Brown   PetscFunctionBegin;
5349c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5350c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5351c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5352c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5353c5033834SJed Brown   PetscFunctionReturn(0);
5354c5033834SJed Brown }
5355c5033834SJed Brown 
53569c6b16b5SShri Abhyankar /*@
5357a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
53589c6b16b5SShri Abhyankar 
53599c6b16b5SShri Abhyankar    Collective on TS
53609c6b16b5SShri Abhyankar 
53619c6b16b5SShri Abhyankar    Input Arguments:
53629c6b16b5SShri Abhyankar +  ts - time stepping context
5363a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5364a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
53659c6b16b5SShri Abhyankar 
53669c6b16b5SShri Abhyankar    Output Arguments:
53677453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
53687453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
53697453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
53709c6b16b5SShri Abhyankar 
53719c6b16b5SShri Abhyankar    Level: developer
53729c6b16b5SShri Abhyankar 
5373deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
53749c6b16b5SShri Abhyankar @*/
53757453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
53769c6b16b5SShri Abhyankar {
53779c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
53789c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
53797453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
53807453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
53819c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
53827453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
53837453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
53847453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
53859c6b16b5SShri Abhyankar 
53869c6b16b5SShri Abhyankar   PetscFunctionBegin;
53879c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5388a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5389a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5390a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5391a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5392a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5393a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
53948a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
53958a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5396a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
53979c6b16b5SShri Abhyankar 
53989c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
53999c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
54009c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
54019c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
54029c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
54037453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
54047453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
54057453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
54069c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
54079c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
54089c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54099c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54109c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54117453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
54127453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
54137453f775SEmil Constantinescu       if(tola>0.){
54147453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
54157453f775SEmil Constantinescu         na_loc++;
54167453f775SEmil Constantinescu       }
54177453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54187453f775SEmil Constantinescu       if(tolr>0.){
54197453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
54207453f775SEmil Constantinescu         nr_loc++;
54217453f775SEmil Constantinescu       }
54227453f775SEmil Constantinescu       tol=tola+tolr;
54237453f775SEmil Constantinescu       if(tol>0.){
54247453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
54257453f775SEmil Constantinescu         n_loc++;
54267453f775SEmil Constantinescu       }
54279c6b16b5SShri Abhyankar     }
54289c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54299c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54309c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
54319c6b16b5SShri Abhyankar     const PetscScalar *atol;
54329c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54339c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54347453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
54357453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
54367453f775SEmil Constantinescu       if(tola>0.){
54377453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
54387453f775SEmil Constantinescu         na_loc++;
54397453f775SEmil Constantinescu       }
54407453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54417453f775SEmil Constantinescu       if(tolr>0.){
54427453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
54437453f775SEmil Constantinescu         nr_loc++;
54447453f775SEmil Constantinescu       }
54457453f775SEmil Constantinescu       tol=tola+tolr;
54467453f775SEmil Constantinescu       if(tol>0.){
54477453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
54487453f775SEmil Constantinescu         n_loc++;
54497453f775SEmil Constantinescu       }
54509c6b16b5SShri Abhyankar     }
54519c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54529c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
54539c6b16b5SShri Abhyankar     const PetscScalar *rtol;
54549c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54559c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54567453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
54577453f775SEmil Constantinescu       tola = ts->atol;
54587453f775SEmil Constantinescu       if(tola>0.){
54597453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
54607453f775SEmil Constantinescu         na_loc++;
54617453f775SEmil Constantinescu       }
54627453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54637453f775SEmil Constantinescu       if(tolr>0.){
54647453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
54657453f775SEmil Constantinescu         nr_loc++;
54667453f775SEmil Constantinescu       }
54677453f775SEmil Constantinescu       tol=tola+tolr;
54687453f775SEmil Constantinescu       if(tol>0.){
54697453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
54707453f775SEmil Constantinescu         n_loc++;
54717453f775SEmil Constantinescu       }
54729c6b16b5SShri Abhyankar     }
54739c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54749c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
54759c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54767453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
54777453f775SEmil Constantinescu      tola = ts->atol;
54787453f775SEmil Constantinescu       if(tola>0.){
54797453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
54807453f775SEmil Constantinescu         na_loc++;
54817453f775SEmil Constantinescu       }
54827453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54837453f775SEmil Constantinescu       if(tolr>0.){
54847453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
54857453f775SEmil Constantinescu         nr_loc++;
54867453f775SEmil Constantinescu       }
54877453f775SEmil Constantinescu       tol=tola+tolr;
54887453f775SEmil Constantinescu       if(tol>0.){
54897453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
54907453f775SEmil Constantinescu         n_loc++;
54917453f775SEmil Constantinescu       }
54929c6b16b5SShri Abhyankar     }
54939c6b16b5SShri Abhyankar   }
54949c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
54959c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
54969c6b16b5SShri Abhyankar 
54977453f775SEmil Constantinescu   err_loc[0] = sum;
54987453f775SEmil Constantinescu   err_loc[1] = suma;
54997453f775SEmil Constantinescu   err_loc[2] = sumr;
55007453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
55017453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
55027453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
55037453f775SEmil Constantinescu 
5504a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
55057453f775SEmil Constantinescu 
55067453f775SEmil Constantinescu   gsum   = err_glb[0];
55077453f775SEmil Constantinescu   gsuma  = err_glb[1];
55087453f775SEmil Constantinescu   gsumr  = err_glb[2];
55097453f775SEmil Constantinescu   n_glb  = err_glb[3];
55107453f775SEmil Constantinescu   na_glb = err_glb[4];
55117453f775SEmil Constantinescu   nr_glb = err_glb[5];
55127453f775SEmil Constantinescu 
5513b1316ef9SEmil Constantinescu   *norm  = 0.;
5514b1316ef9SEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
5515b1316ef9SEmil Constantinescu   *norma = 0.;
5516b1316ef9SEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5517b1316ef9SEmil Constantinescu   *normr = 0.;
5518b1316ef9SEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
55199c6b16b5SShri Abhyankar 
55209c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
55217453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
55227453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
55239c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
55249c6b16b5SShri Abhyankar }
55259c6b16b5SShri Abhyankar 
55269c6b16b5SShri Abhyankar /*@
5527a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
55289c6b16b5SShri Abhyankar 
55299c6b16b5SShri Abhyankar    Collective on TS
55309c6b16b5SShri Abhyankar 
55319c6b16b5SShri Abhyankar    Input Arguments:
55329c6b16b5SShri Abhyankar +  ts - time stepping context
5533a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5534a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
55359c6b16b5SShri Abhyankar 
55369c6b16b5SShri Abhyankar    Output Arguments:
55377453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
55387453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
55397453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
55409c6b16b5SShri Abhyankar 
55419c6b16b5SShri Abhyankar    Level: developer
55429c6b16b5SShri Abhyankar 
5543deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
55449c6b16b5SShri Abhyankar @*/
55457453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
55469c6b16b5SShri Abhyankar {
55479c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
55487453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
55499c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
55507453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
55517453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
55527453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
55539c6b16b5SShri Abhyankar 
55549c6b16b5SShri Abhyankar   PetscFunctionBegin;
55559c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5556a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5557a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5558a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5559a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5560a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5561a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
55628a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
55638a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5564a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
55659c6b16b5SShri Abhyankar 
55669c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
55679c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
55689c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
55699c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
55709c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
55717453f775SEmil Constantinescu 
55727453f775SEmil Constantinescu   max=0.;
55737453f775SEmil Constantinescu   maxa=0.;
55747453f775SEmil Constantinescu   maxr=0.;
55757453f775SEmil Constantinescu 
55767453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
55779c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
55789c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55799c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55807453f775SEmil Constantinescu 
55817453f775SEmil Constantinescu     for (i=0; i<n; i++) {
55827453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55837453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
55847453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55857453f775SEmil Constantinescu       tol  = tola+tolr;
55867453f775SEmil Constantinescu       if(tola>0.){
55877453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
55887453f775SEmil Constantinescu       }
55897453f775SEmil Constantinescu       if(tolr>0.){
55907453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
55917453f775SEmil Constantinescu       }
55927453f775SEmil Constantinescu       if(tol>0.){
55937453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
55947453f775SEmil Constantinescu       }
55959c6b16b5SShri Abhyankar     }
55969c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55979c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55989c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
55999c6b16b5SShri Abhyankar     const PetscScalar *atol;
56009c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56017453f775SEmil Constantinescu     for (i=0; i<n; i++) {
56027453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
56037453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
56047453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56057453f775SEmil Constantinescu       tol  = tola+tolr;
56067453f775SEmil Constantinescu       if(tola>0.){
56077453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
56087453f775SEmil Constantinescu       }
56097453f775SEmil Constantinescu       if(tolr>0.){
56107453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
56117453f775SEmil Constantinescu       }
56127453f775SEmil Constantinescu       if(tol>0.){
56137453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
56147453f775SEmil Constantinescu       }
56159c6b16b5SShri Abhyankar     }
56169c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56179c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
56189c6b16b5SShri Abhyankar     const PetscScalar *rtol;
56199c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56207453f775SEmil Constantinescu 
56217453f775SEmil Constantinescu     for (i=0; i<n; i++) {
56227453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
56237453f775SEmil Constantinescu       tola = ts->atol;
56247453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56257453f775SEmil Constantinescu       tol  = tola+tolr;
56267453f775SEmil Constantinescu       if(tola>0.){
56277453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
56287453f775SEmil Constantinescu       }
56297453f775SEmil Constantinescu       if(tolr>0.){
56307453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
56317453f775SEmil Constantinescu       }
56327453f775SEmil Constantinescu       if(tol>0.){
56337453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
56347453f775SEmil Constantinescu       }
56359c6b16b5SShri Abhyankar     }
56369c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56379c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
56387453f775SEmil Constantinescu 
56397453f775SEmil Constantinescu     for (i=0; i<n; i++) {
56407453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
56417453f775SEmil Constantinescu       tola = ts->atol;
56427453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56437453f775SEmil Constantinescu       tol  = tola+tolr;
56447453f775SEmil Constantinescu       if(tola>0.){
56457453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
56467453f775SEmil Constantinescu       }
56477453f775SEmil Constantinescu       if(tolr>0.){
56487453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
56497453f775SEmil Constantinescu       }
56507453f775SEmil Constantinescu       if(tol>0.){
56517453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
56527453f775SEmil Constantinescu       }
56539c6b16b5SShri Abhyankar     }
56549c6b16b5SShri Abhyankar   }
56559c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
56569c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
56577453f775SEmil Constantinescu   err_loc[0] = max;
56587453f775SEmil Constantinescu   err_loc[1] = maxa;
56597453f775SEmil Constantinescu   err_loc[2] = maxr;
5660a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
56617453f775SEmil Constantinescu   gmax   = err_glb[0];
56627453f775SEmil Constantinescu   gmaxa  = err_glb[1];
56637453f775SEmil Constantinescu   gmaxr  = err_glb[2];
56649c6b16b5SShri Abhyankar 
56659c6b16b5SShri Abhyankar   *norm = gmax;
56667453f775SEmil Constantinescu   *norma = gmaxa;
56677453f775SEmil Constantinescu   *normr = gmaxr;
56689c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
56697453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
56707453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
56719c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
56729c6b16b5SShri Abhyankar }
56739c6b16b5SShri Abhyankar 
56741c3436cfSJed Brown /*@
56758a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
56761c3436cfSJed Brown 
56771c3436cfSJed Brown    Collective on TS
56781c3436cfSJed Brown 
56791c3436cfSJed Brown    Input Arguments:
56801c3436cfSJed Brown +  ts - time stepping context
5681a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5682a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
5683a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
56847619abb3SShri 
56851c3436cfSJed Brown    Output Arguments:
56868a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
56878a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
56888a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
5689a4868fbcSLisandro Dalcin 
5690a4868fbcSLisandro Dalcin    Options Database Keys:
5691a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
5692a4868fbcSLisandro Dalcin 
56931c3436cfSJed Brown    Level: developer
56941c3436cfSJed Brown 
56958a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
56961c3436cfSJed Brown @*/
56977453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
56981c3436cfSJed Brown {
56998beabaa1SBarry Smith   PetscErrorCode ierr;
57001c3436cfSJed Brown 
57011c3436cfSJed Brown   PetscFunctionBegin;
5702a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
57037453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
5704a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
57057453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
5706a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
57071c3436cfSJed Brown   PetscFunctionReturn(0);
57081c3436cfSJed Brown }
57091c3436cfSJed Brown 
57108a175baeSEmil Constantinescu 
57118a175baeSEmil Constantinescu /*@
57128a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
57138a175baeSEmil Constantinescu 
57148a175baeSEmil Constantinescu    Collective on TS
57158a175baeSEmil Constantinescu 
57168a175baeSEmil Constantinescu    Input Arguments:
57178a175baeSEmil Constantinescu +  ts - time stepping context
57188a175baeSEmil Constantinescu .  E - error vector
57198a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
57208a175baeSEmil Constantinescu -  Y - state vector, previous time step
57218a175baeSEmil Constantinescu 
57228a175baeSEmil Constantinescu    Output Arguments:
57238a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
57248a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
57258a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
57268a175baeSEmil Constantinescu 
57278a175baeSEmil Constantinescu    Level: developer
57288a175baeSEmil Constantinescu 
57298a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
57308a175baeSEmil Constantinescu @*/
57318a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
57328a175baeSEmil Constantinescu {
57338a175baeSEmil Constantinescu   PetscErrorCode    ierr;
57348a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
57358a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
57368a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
57378a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
57388a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
57398a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
57408a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
57418a175baeSEmil Constantinescu 
57428a175baeSEmil Constantinescu   PetscFunctionBegin;
57438a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
57448a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
57458a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
57468a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
57478a175baeSEmil Constantinescu   PetscValidType(E,2);
57488a175baeSEmil Constantinescu   PetscValidType(U,3);
57498a175baeSEmil Constantinescu   PetscValidType(Y,4);
57508a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
57518a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
57528a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
57538a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
57548a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
57558a175baeSEmil Constantinescu 
57568a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
57578a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
57588a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
57598a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
57608a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
57618a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
57628a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
57638a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
57648a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
57658a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
57668a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
57678a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57688a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57698a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
57708a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
57718a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
57728a175baeSEmil Constantinescu       if(tola>0.){
57738a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
57748a175baeSEmil Constantinescu         na_loc++;
57758a175baeSEmil Constantinescu       }
57768a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57778a175baeSEmil Constantinescu       if(tolr>0.){
57788a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
57798a175baeSEmil Constantinescu         nr_loc++;
57808a175baeSEmil Constantinescu       }
57818a175baeSEmil Constantinescu       tol=tola+tolr;
57828a175baeSEmil Constantinescu       if(tol>0.){
57838a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
57848a175baeSEmil Constantinescu         n_loc++;
57858a175baeSEmil Constantinescu       }
57868a175baeSEmil Constantinescu     }
57878a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57888a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57898a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
57908a175baeSEmil Constantinescu     const PetscScalar *atol;
57918a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
57928a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
57938a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
57948a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
57958a175baeSEmil Constantinescu       if(tola>0.){
57968a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
57978a175baeSEmil Constantinescu         na_loc++;
57988a175baeSEmil Constantinescu       }
57998a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58008a175baeSEmil Constantinescu       if(tolr>0.){
58018a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
58028a175baeSEmil Constantinescu         nr_loc++;
58038a175baeSEmil Constantinescu       }
58048a175baeSEmil Constantinescu       tol=tola+tolr;
58058a175baeSEmil Constantinescu       if(tol>0.){
58068a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
58078a175baeSEmil Constantinescu         n_loc++;
58088a175baeSEmil Constantinescu       }
58098a175baeSEmil Constantinescu     }
58108a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58118a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
58128a175baeSEmil Constantinescu     const PetscScalar *rtol;
58138a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58148a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
58158a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
58168a175baeSEmil Constantinescu       tola = ts->atol;
58178a175baeSEmil Constantinescu       if(tola>0.){
58188a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
58198a175baeSEmil Constantinescu         na_loc++;
58208a175baeSEmil Constantinescu       }
58218a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58228a175baeSEmil Constantinescu       if(tolr>0.){
58238a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
58248a175baeSEmil Constantinescu         nr_loc++;
58258a175baeSEmil Constantinescu       }
58268a175baeSEmil Constantinescu       tol=tola+tolr;
58278a175baeSEmil Constantinescu       if(tol>0.){
58288a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
58298a175baeSEmil Constantinescu         n_loc++;
58308a175baeSEmil Constantinescu       }
58318a175baeSEmil Constantinescu     }
58328a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58338a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
58348a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
58358a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
58368a175baeSEmil Constantinescu      tola = ts->atol;
58378a175baeSEmil Constantinescu       if(tola>0.){
58388a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
58398a175baeSEmil Constantinescu         na_loc++;
58408a175baeSEmil Constantinescu       }
58418a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58428a175baeSEmil Constantinescu       if(tolr>0.){
58438a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
58448a175baeSEmil Constantinescu         nr_loc++;
58458a175baeSEmil Constantinescu       }
58468a175baeSEmil Constantinescu       tol=tola+tolr;
58478a175baeSEmil Constantinescu       if(tol>0.){
58488a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
58498a175baeSEmil Constantinescu         n_loc++;
58508a175baeSEmil Constantinescu       }
58518a175baeSEmil Constantinescu     }
58528a175baeSEmil Constantinescu   }
58538a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
58548a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
58558a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
58568a175baeSEmil Constantinescu 
58578a175baeSEmil Constantinescu   err_loc[0] = sum;
58588a175baeSEmil Constantinescu   err_loc[1] = suma;
58598a175baeSEmil Constantinescu   err_loc[2] = sumr;
58608a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
58618a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
58628a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
58638a175baeSEmil Constantinescu 
5864a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
58658a175baeSEmil Constantinescu 
58668a175baeSEmil Constantinescu   gsum   = err_glb[0];
58678a175baeSEmil Constantinescu   gsuma  = err_glb[1];
58688a175baeSEmil Constantinescu   gsumr  = err_glb[2];
58698a175baeSEmil Constantinescu   n_glb  = err_glb[3];
58708a175baeSEmil Constantinescu   na_glb = err_glb[4];
58718a175baeSEmil Constantinescu   nr_glb = err_glb[5];
58728a175baeSEmil Constantinescu 
58738a175baeSEmil Constantinescu   *norm  = 0.;
58748a175baeSEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
58758a175baeSEmil Constantinescu   *norma = 0.;
58768a175baeSEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
58778a175baeSEmil Constantinescu   *normr = 0.;
58788a175baeSEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
58798a175baeSEmil Constantinescu 
58808a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
58818a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
58828a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
58838a175baeSEmil Constantinescu   PetscFunctionReturn(0);
58848a175baeSEmil Constantinescu }
58858a175baeSEmil Constantinescu 
58868a175baeSEmil Constantinescu /*@
58878a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
58888a175baeSEmil Constantinescu    Collective on TS
58898a175baeSEmil Constantinescu 
58908a175baeSEmil Constantinescu    Input Arguments:
58918a175baeSEmil Constantinescu +  ts - time stepping context
58928a175baeSEmil Constantinescu .  E - error vector
58938a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
58948a175baeSEmil Constantinescu -  Y - state vector, previous time step
58958a175baeSEmil Constantinescu 
58968a175baeSEmil Constantinescu    Output Arguments:
58978a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
58988a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
58998a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
59008a175baeSEmil Constantinescu 
59018a175baeSEmil Constantinescu    Level: developer
59028a175baeSEmil Constantinescu 
59038a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
59048a175baeSEmil Constantinescu @*/
59058a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
59068a175baeSEmil Constantinescu {
59078a175baeSEmil Constantinescu   PetscErrorCode    ierr;
59088a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
59098a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
59108a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
59118a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
59128a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
59138a175baeSEmil Constantinescu 
59148a175baeSEmil Constantinescu   PetscFunctionBegin;
59158a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
59168a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
59178a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
59188a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
59198a175baeSEmil Constantinescu   PetscValidType(E,2);
59208a175baeSEmil Constantinescu   PetscValidType(U,3);
59218a175baeSEmil Constantinescu   PetscValidType(Y,4);
59228a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
59238a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
59248a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
59258a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
59268a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
59278a175baeSEmil Constantinescu 
59288a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
59298a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
59308a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
59318a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
59328a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
59338a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
59348a175baeSEmil Constantinescu 
59358a175baeSEmil Constantinescu   max=0.;
59368a175baeSEmil Constantinescu   maxa=0.;
59378a175baeSEmil Constantinescu   maxr=0.;
59388a175baeSEmil Constantinescu 
59398a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
59408a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
59418a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59428a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59438a175baeSEmil Constantinescu 
59448a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
59458a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59468a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
59478a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59488a175baeSEmil Constantinescu       tol  = tola+tolr;
59498a175baeSEmil Constantinescu       if(tola>0.){
59508a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
59518a175baeSEmil Constantinescu       }
59528a175baeSEmil Constantinescu       if(tolr>0.){
59538a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
59548a175baeSEmil Constantinescu       }
59558a175baeSEmil Constantinescu       if(tol>0.){
59568a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
59578a175baeSEmil Constantinescu       }
59588a175baeSEmil Constantinescu     }
59598a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59608a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59618a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
59628a175baeSEmil Constantinescu     const PetscScalar *atol;
59638a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59648a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
59658a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59668a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
59678a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59688a175baeSEmil Constantinescu       tol  = tola+tolr;
59698a175baeSEmil Constantinescu       if(tola>0.){
59708a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
59718a175baeSEmil Constantinescu       }
59728a175baeSEmil Constantinescu       if(tolr>0.){
59738a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
59748a175baeSEmil Constantinescu       }
59758a175baeSEmil Constantinescu       if(tol>0.){
59768a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
59778a175baeSEmil Constantinescu       }
59788a175baeSEmil Constantinescu     }
59798a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59808a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
59818a175baeSEmil Constantinescu     const PetscScalar *rtol;
59828a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59838a175baeSEmil Constantinescu 
59848a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
59858a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59868a175baeSEmil Constantinescu       tola = ts->atol;
59878a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59888a175baeSEmil Constantinescu       tol  = tola+tolr;
59898a175baeSEmil Constantinescu       if(tola>0.){
59908a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
59918a175baeSEmil Constantinescu       }
59928a175baeSEmil Constantinescu       if(tolr>0.){
59938a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
59948a175baeSEmil Constantinescu       }
59958a175baeSEmil Constantinescu       if(tol>0.){
59968a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
59978a175baeSEmil Constantinescu       }
59988a175baeSEmil Constantinescu     }
59998a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60008a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
60018a175baeSEmil Constantinescu 
60028a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
60038a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
60048a175baeSEmil Constantinescu       tola = ts->atol;
60058a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60068a175baeSEmil Constantinescu       tol  = tola+tolr;
60078a175baeSEmil Constantinescu       if(tola>0.){
60088a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
60098a175baeSEmil Constantinescu       }
60108a175baeSEmil Constantinescu       if(tolr>0.){
60118a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
60128a175baeSEmil Constantinescu       }
60138a175baeSEmil Constantinescu       if(tol>0.){
60148a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
60158a175baeSEmil Constantinescu       }
60168a175baeSEmil Constantinescu     }
60178a175baeSEmil Constantinescu   }
60188a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
60198a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
60208a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
60218a175baeSEmil Constantinescu   err_loc[0] = max;
60228a175baeSEmil Constantinescu   err_loc[1] = maxa;
60238a175baeSEmil Constantinescu   err_loc[2] = maxr;
6024a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
60258a175baeSEmil Constantinescu   gmax   = err_glb[0];
60268a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
60278a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
60288a175baeSEmil Constantinescu 
60298a175baeSEmil Constantinescu   *norm = gmax;
60308a175baeSEmil Constantinescu   *norma = gmaxa;
60318a175baeSEmil Constantinescu   *normr = gmaxr;
60328a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
60338a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
60348a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
60358a175baeSEmil Constantinescu   PetscFunctionReturn(0);
60368a175baeSEmil Constantinescu }
60378a175baeSEmil Constantinescu 
60388a175baeSEmil Constantinescu /*@
60398a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
60408a175baeSEmil Constantinescu 
60418a175baeSEmil Constantinescu    Collective on TS
60428a175baeSEmil Constantinescu 
60438a175baeSEmil Constantinescu    Input Arguments:
60448a175baeSEmil Constantinescu +  ts - time stepping context
60458a175baeSEmil Constantinescu .  E - error vector
60468a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
60478a175baeSEmil Constantinescu .  Y - state vector, previous time step
60488a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
60498a175baeSEmil Constantinescu 
60508a175baeSEmil Constantinescu    Output Arguments:
60518a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
60528a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
60538a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
60548a175baeSEmil Constantinescu 
60558a175baeSEmil Constantinescu    Options Database Keys:
60568a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
60578a175baeSEmil Constantinescu 
60588a175baeSEmil Constantinescu    Level: developer
60598a175baeSEmil Constantinescu 
60608a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
60618a175baeSEmil Constantinescu @*/
60628a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
60638a175baeSEmil Constantinescu {
60648a175baeSEmil Constantinescu   PetscErrorCode ierr;
60658a175baeSEmil Constantinescu 
60668a175baeSEmil Constantinescu   PetscFunctionBegin;
60678a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
60688a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
60698a175baeSEmil Constantinescu   } else if(wnormtype == NORM_INFINITY) {
60708a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
60718a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
60728a175baeSEmil Constantinescu   PetscFunctionReturn(0);
60738a175baeSEmil Constantinescu }
60748a175baeSEmil Constantinescu 
60758a175baeSEmil Constantinescu 
60768d59e960SJed Brown /*@
60778d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
60788d59e960SJed Brown 
60798d59e960SJed Brown    Logically Collective on TS
60808d59e960SJed Brown 
60818d59e960SJed Brown    Input Arguments:
60828d59e960SJed Brown +  ts - time stepping context
60838d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
60848d59e960SJed Brown 
60858d59e960SJed Brown    Note:
60868d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
60878d59e960SJed Brown 
60888d59e960SJed Brown    Level: intermediate
60898d59e960SJed Brown 
60908d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
60918d59e960SJed Brown @*/
60928d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
60938d59e960SJed Brown {
60948d59e960SJed Brown   PetscFunctionBegin;
60958d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
60968d59e960SJed Brown   ts->cfltime_local = cfltime;
60978d59e960SJed Brown   ts->cfltime       = -1.;
60988d59e960SJed Brown   PetscFunctionReturn(0);
60998d59e960SJed Brown }
61008d59e960SJed Brown 
61018d59e960SJed Brown /*@
61028d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
61038d59e960SJed Brown 
61048d59e960SJed Brown    Collective on TS
61058d59e960SJed Brown 
61068d59e960SJed Brown    Input Arguments:
61078d59e960SJed Brown .  ts - time stepping context
61088d59e960SJed Brown 
61098d59e960SJed Brown    Output Arguments:
61108d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
61118d59e960SJed Brown 
61128d59e960SJed Brown    Level: advanced
61138d59e960SJed Brown 
61148d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
61158d59e960SJed Brown @*/
61168d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
61178d59e960SJed Brown {
61188d59e960SJed Brown   PetscErrorCode ierr;
61198d59e960SJed Brown 
61208d59e960SJed Brown   PetscFunctionBegin;
61218d59e960SJed Brown   if (ts->cfltime < 0) {
6122b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
61238d59e960SJed Brown   }
61248d59e960SJed Brown   *cfltime = ts->cfltime;
61258d59e960SJed Brown   PetscFunctionReturn(0);
61268d59e960SJed Brown }
61278d59e960SJed Brown 
6128d6ebe24aSShri Abhyankar /*@
6129d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6130d6ebe24aSShri Abhyankar 
6131d6ebe24aSShri Abhyankar    Input Parameters:
6132d6ebe24aSShri Abhyankar .  ts   - the TS context.
6133d6ebe24aSShri Abhyankar .  xl   - lower bound.
6134d6ebe24aSShri Abhyankar .  xu   - upper bound.
6135d6ebe24aSShri Abhyankar 
6136d6ebe24aSShri Abhyankar    Notes:
6137d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6138e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6139d6ebe24aSShri Abhyankar 
61402bd2b0e6SSatish Balay    Level: advanced
61412bd2b0e6SSatish Balay 
6142d6ebe24aSShri Abhyankar @*/
6143d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6144d6ebe24aSShri Abhyankar {
6145d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6146d6ebe24aSShri Abhyankar   SNES           snes;
6147d6ebe24aSShri Abhyankar 
6148d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6149d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6150d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6151d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6152d6ebe24aSShri Abhyankar }
6153d6ebe24aSShri Abhyankar 
6154325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
6155c6db04a5SJed Brown #include <mex.h>
6156325fc9f4SBarry Smith 
6157325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
6158325fc9f4SBarry Smith 
6159325fc9f4SBarry Smith /*
6160325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
6161325fc9f4SBarry Smith                          TSSetFunctionMatlab().
6162325fc9f4SBarry Smith 
6163325fc9f4SBarry Smith    Collective on TS
6164325fc9f4SBarry Smith 
6165325fc9f4SBarry Smith    Input Parameters:
6166325fc9f4SBarry Smith +  snes - the TS context
61670910c330SBarry Smith -  u - input vector
6168325fc9f4SBarry Smith 
6169325fc9f4SBarry Smith    Output Parameter:
6170325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
6171325fc9f4SBarry Smith 
6172325fc9f4SBarry Smith    Notes:
6173325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
6174325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
6175325fc9f4SBarry Smith    themselves.
6176325fc9f4SBarry Smith 
6177325fc9f4SBarry Smith    Level: developer
6178325fc9f4SBarry Smith 
6179325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6180325fc9f4SBarry Smith 
6181325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6182325fc9f4SBarry Smith */
61830910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
6184325fc9f4SBarry Smith {
6185325fc9f4SBarry Smith   PetscErrorCode  ierr;
6186325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6187325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
6188325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
6189325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
6190325fc9f4SBarry Smith 
6191325fc9f4SBarry Smith   PetscFunctionBegin;
6192325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
61930910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
61940910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
6195325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
61960910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
6197325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
6198325fc9f4SBarry Smith 
6199325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
62000910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
62010910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
62020910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
6203bbd56ea5SKarl Rupp 
6204325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6205325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
6206325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6207325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6208325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
6209325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
6210325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
6211325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
6212325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6213325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6214325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6215325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6216325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6217325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6218325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6219325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6220325fc9f4SBarry Smith   PetscFunctionReturn(0);
6221325fc9f4SBarry Smith }
6222325fc9f4SBarry Smith 
6223325fc9f4SBarry Smith /*
6224325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
6225325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
6226e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
6227325fc9f4SBarry Smith 
6228325fc9f4SBarry Smith    Logically Collective on TS
6229325fc9f4SBarry Smith 
6230325fc9f4SBarry Smith    Input Parameters:
6231325fc9f4SBarry Smith +  ts - the TS context
6232325fc9f4SBarry Smith -  func - function evaluation routine
6233325fc9f4SBarry Smith 
6234325fc9f4SBarry Smith    Calling sequence of func:
62350910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
6236325fc9f4SBarry Smith 
6237325fc9f4SBarry Smith    Level: beginner
6238325fc9f4SBarry Smith 
6239325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6240325fc9f4SBarry Smith 
6241325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6242325fc9f4SBarry Smith */
6243cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
6244325fc9f4SBarry Smith {
6245325fc9f4SBarry Smith   PetscErrorCode  ierr;
6246325fc9f4SBarry Smith   TSMatlabContext *sctx;
6247325fc9f4SBarry Smith 
6248325fc9f4SBarry Smith   PetscFunctionBegin;
6249325fc9f4SBarry Smith   /* currently sctx is memory bleed */
625095dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6251325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6252325fc9f4SBarry Smith   /*
6253325fc9f4SBarry Smith      This should work, but it doesn't
6254325fc9f4SBarry Smith   sctx->ctx = ctx;
6255325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6256325fc9f4SBarry Smith   */
6257325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6258bbd56ea5SKarl Rupp 
62590298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
6260325fc9f4SBarry Smith   PetscFunctionReturn(0);
6261325fc9f4SBarry Smith }
6262325fc9f4SBarry Smith 
6263325fc9f4SBarry Smith /*
6264325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
6265325fc9f4SBarry Smith                          TSSetJacobianMatlab().
6266325fc9f4SBarry Smith 
6267325fc9f4SBarry Smith    Collective on TS
6268325fc9f4SBarry Smith 
6269325fc9f4SBarry Smith    Input Parameters:
6270cdcf91faSSean Farley +  ts - the TS context
62710910c330SBarry Smith .  u - input vector
6272325fc9f4SBarry Smith .  A, B - the matrices
6273325fc9f4SBarry Smith -  ctx - user context
6274325fc9f4SBarry Smith 
6275325fc9f4SBarry Smith    Level: developer
6276325fc9f4SBarry Smith 
6277325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6278325fc9f4SBarry Smith 
6279325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6280325fc9f4SBarry Smith @*/
6281f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
6282325fc9f4SBarry Smith {
6283325fc9f4SBarry Smith   PetscErrorCode  ierr;
6284325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6285325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
6286325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
6287325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
6288325fc9f4SBarry Smith 
6289325fc9f4SBarry Smith   PetscFunctionBegin;
6290cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
62910910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
6292325fc9f4SBarry Smith 
62930910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
6294325fc9f4SBarry Smith 
6295cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
62960910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
62970910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
62980910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
62990910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
6300bbd56ea5SKarl Rupp 
6301325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6302325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
6303325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6304325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6305325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
6306325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
6307325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
6308325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
6309325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
6310325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
6311325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6312325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6313325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6314325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6315325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6316325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6317325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6318325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
6319325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
6320325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6321325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
6322325fc9f4SBarry Smith   PetscFunctionReturn(0);
6323325fc9f4SBarry Smith }
6324325fc9f4SBarry Smith 
6325325fc9f4SBarry Smith /*
6326325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
6327e3c5b3baSBarry 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
6328325fc9f4SBarry Smith 
6329325fc9f4SBarry Smith    Logically Collective on TS
6330325fc9f4SBarry Smith 
6331325fc9f4SBarry Smith    Input Parameters:
6332cdcf91faSSean Farley +  ts - the TS context
6333325fc9f4SBarry Smith .  A,B - Jacobian matrices
6334325fc9f4SBarry Smith .  func - function evaluation routine
6335325fc9f4SBarry Smith -  ctx - user context
6336325fc9f4SBarry Smith 
6337325fc9f4SBarry Smith    Calling sequence of func:
63380910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
6339325fc9f4SBarry Smith 
6340325fc9f4SBarry Smith    Level: developer
6341325fc9f4SBarry Smith 
6342325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6343325fc9f4SBarry Smith 
6344325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6345325fc9f4SBarry Smith */
6346cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
6347325fc9f4SBarry Smith {
6348325fc9f4SBarry Smith   PetscErrorCode  ierr;
6349325fc9f4SBarry Smith   TSMatlabContext *sctx;
6350325fc9f4SBarry Smith 
6351325fc9f4SBarry Smith   PetscFunctionBegin;
6352325fc9f4SBarry Smith   /* currently sctx is memory bleed */
635395dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6354325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6355325fc9f4SBarry Smith   /*
6356325fc9f4SBarry Smith      This should work, but it doesn't
6357325fc9f4SBarry Smith   sctx->ctx = ctx;
6358325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6359325fc9f4SBarry Smith   */
6360325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6361bbd56ea5SKarl Rupp 
6362cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
6363325fc9f4SBarry Smith   PetscFunctionReturn(0);
6364325fc9f4SBarry Smith }
6365325fc9f4SBarry Smith 
6366b5b1a830SBarry Smith /*
6367b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
6368b5b1a830SBarry Smith 
6369b5b1a830SBarry Smith    Collective on TS
6370b5b1a830SBarry Smith 
6371b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6372b5b1a830SBarry Smith @*/
63730910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
6374b5b1a830SBarry Smith {
6375b5b1a830SBarry Smith   PetscErrorCode  ierr;
6376b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6377a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
6378b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
6379b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
6380b5b1a830SBarry Smith 
6381b5b1a830SBarry Smith   PetscFunctionBegin;
6382cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
63830910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
6384b5b1a830SBarry Smith 
6385cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
63860910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
6387bbd56ea5SKarl Rupp 
6388b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6389b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
6390b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
6391b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
6392b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
6393b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
6394b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
6395b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6396b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
6397b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
6398b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
6399b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
6400b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
6401b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
6402b5b1a830SBarry Smith   PetscFunctionReturn(0);
6403b5b1a830SBarry Smith }
6404b5b1a830SBarry Smith 
6405b5b1a830SBarry Smith /*
6406b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
6407b5b1a830SBarry Smith 
6408b5b1a830SBarry Smith    Level: developer
6409b5b1a830SBarry Smith 
6410b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
6411b5b1a830SBarry Smith 
6412b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6413b5b1a830SBarry Smith */
6414cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
6415b5b1a830SBarry Smith {
6416b5b1a830SBarry Smith   PetscErrorCode  ierr;
6417b5b1a830SBarry Smith   TSMatlabContext *sctx;
6418b5b1a830SBarry Smith 
6419b5b1a830SBarry Smith   PetscFunctionBegin;
6420b5b1a830SBarry Smith   /* currently sctx is memory bleed */
642195dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6422b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6423b5b1a830SBarry Smith   /*
6424b5b1a830SBarry Smith      This should work, but it doesn't
6425b5b1a830SBarry Smith   sctx->ctx = ctx;
6426b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6427b5b1a830SBarry Smith   */
6428b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6429bbd56ea5SKarl Rupp 
64300298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
6431b5b1a830SBarry Smith   PetscFunctionReturn(0);
6432b5b1a830SBarry Smith }
6433325fc9f4SBarry Smith #endif
6434b3603a34SBarry Smith 
6435b3603a34SBarry Smith /*@C
64364f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6437b3603a34SBarry Smith        in a time based line graph
6438b3603a34SBarry Smith 
6439b3603a34SBarry Smith    Collective on TS
6440b3603a34SBarry Smith 
6441b3603a34SBarry Smith    Input Parameters:
6442b3603a34SBarry Smith +  ts - the TS context
6443b3603a34SBarry Smith .  step - current time-step
6444b3603a34SBarry Smith .  ptime - current time
64457db568b7SBarry Smith .  u - current solution
64467db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6447b3603a34SBarry Smith 
6448b3d3934dSBarry Smith    Options Database:
64499ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6450b3d3934dSBarry Smith 
6451b3603a34SBarry Smith    Level: intermediate
6452b3603a34SBarry Smith 
645395452b02SPatrick Sanan    Notes:
645495452b02SPatrick Sanan     Each process in a parallel run displays its component solutions in a separate window
6455b3603a34SBarry Smith 
6456b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
6457b3603a34SBarry Smith 
64587db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
64597db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
64607db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
64617db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6462b3603a34SBarry Smith @*/
64637db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6464b3603a34SBarry Smith {
6465b3603a34SBarry Smith   PetscErrorCode    ierr;
64667db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6467b3603a34SBarry Smith   const PetscScalar *yy;
646880666b62SBarry Smith   Vec               v;
6469b3603a34SBarry Smith 
6470b3603a34SBarry Smith   PetscFunctionBegin;
647163e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
647258ff32f7SBarry Smith   if (!step) {
6473a9f9c1f6SBarry Smith     PetscDrawAxis axis;
64746934998bSLisandro Dalcin     PetscInt      dim;
6475a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6476a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6477bab0a581SBarry Smith     if (!ctx->names) {
6478bab0a581SBarry Smith       PetscBool flg;
6479bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6480bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6481bab0a581SBarry Smith       if (flg) {
6482bab0a581SBarry Smith         PetscInt i,n;
6483bab0a581SBarry Smith         char     **names;
6484bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6485bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6486bab0a581SBarry Smith         for (i=0; i<n; i++) {
6487bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6488bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6489bab0a581SBarry Smith         }
6490bab0a581SBarry Smith         names[n] = NULL;
6491bab0a581SBarry Smith         ctx->names = names;
6492bab0a581SBarry Smith       }
6493bab0a581SBarry Smith     }
6494387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6495387f4636SBarry Smith       char      **displaynames;
6496387f4636SBarry Smith       PetscBool flg;
6497387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
649895dccacaSBarry Smith       ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6499387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
6500c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6501387f4636SBarry Smith       if (flg) {
6502a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6503387f4636SBarry Smith       }
6504387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6505387f4636SBarry Smith     }
6506387f4636SBarry Smith     if (ctx->displaynames) {
6507387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6508387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6509387f4636SBarry Smith     } else if (ctx->names) {
65100910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
65110b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6512387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6513b0bc92c6SBarry Smith     } else {
6514b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6515b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6516387f4636SBarry Smith     }
65170b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
651858ff32f7SBarry Smith   }
65196934998bSLisandro Dalcin 
65206934998bSLisandro Dalcin   if (!ctx->transform) v = u;
65216934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
652280666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
65236934998bSLisandro Dalcin   if (ctx->displaynames) {
65246934998bSLisandro Dalcin     PetscInt i;
65256934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
65266934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
65276934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
65286934998bSLisandro Dalcin   } else {
6529e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6530e3efe391SJed Brown     PetscInt  i,n;
65316934998bSLisandro Dalcin     PetscReal *yreal;
653280666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6533785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6534e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
65350b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6536e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6537e3efe391SJed Brown #else
65380b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6539e3efe391SJed Brown #endif
654080666b62SBarry Smith   }
65416934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
65426934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
65436934998bSLisandro Dalcin 
6544b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
65450b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
65466934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
65473923b477SBarry Smith   }
6548b3603a34SBarry Smith   PetscFunctionReturn(0);
6549b3603a34SBarry Smith }
6550b3603a34SBarry Smith 
6551b037adc7SBarry Smith /*@C
655231152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6553b037adc7SBarry Smith 
6554b037adc7SBarry Smith    Collective on TS
6555b037adc7SBarry Smith 
6556b037adc7SBarry Smith    Input Parameters:
6557b037adc7SBarry Smith +  ts - the TS context
6558b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6559b037adc7SBarry Smith 
6560b037adc7SBarry Smith    Level: intermediate
6561b037adc7SBarry Smith 
656295452b02SPatrick Sanan    Notes:
656395452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
65647db568b7SBarry Smith 
6565b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
6566b037adc7SBarry Smith 
6567a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6568b037adc7SBarry Smith @*/
656931152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6570b037adc7SBarry Smith {
6571b037adc7SBarry Smith   PetscErrorCode    ierr;
6572b037adc7SBarry Smith   PetscInt          i;
6573b037adc7SBarry Smith 
6574b037adc7SBarry Smith   PetscFunctionBegin;
6575b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6576b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
65775537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6578b3d3934dSBarry Smith       break;
6579b3d3934dSBarry Smith     }
6580b3d3934dSBarry Smith   }
6581b3d3934dSBarry Smith   PetscFunctionReturn(0);
6582b3d3934dSBarry Smith }
6583b3d3934dSBarry Smith 
6584e673d494SBarry Smith /*@C
6585e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6586e673d494SBarry Smith 
6587e673d494SBarry Smith    Collective on TS
6588e673d494SBarry Smith 
6589e673d494SBarry Smith    Input Parameters:
6590e673d494SBarry Smith +  ts - the TS context
6591e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6592e673d494SBarry Smith 
6593e673d494SBarry Smith    Level: intermediate
6594e673d494SBarry Smith 
6595e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6596e673d494SBarry Smith 
6597a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6598e673d494SBarry Smith @*/
6599e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6600e673d494SBarry Smith {
6601e673d494SBarry Smith   PetscErrorCode    ierr;
6602e673d494SBarry Smith 
6603e673d494SBarry Smith   PetscFunctionBegin;
6604e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6605e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6606e673d494SBarry Smith   PetscFunctionReturn(0);
6607e673d494SBarry Smith }
6608e673d494SBarry Smith 
6609b3d3934dSBarry Smith /*@C
6610b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6611b3d3934dSBarry Smith 
6612b3d3934dSBarry Smith    Collective on TS
6613b3d3934dSBarry Smith 
6614b3d3934dSBarry Smith    Input Parameter:
6615b3d3934dSBarry Smith .  ts - the TS context
6616b3d3934dSBarry Smith 
6617b3d3934dSBarry Smith    Output Parameter:
6618b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6619b3d3934dSBarry Smith 
6620b3d3934dSBarry Smith    Level: intermediate
6621b3d3934dSBarry Smith 
662295452b02SPatrick Sanan    Notes:
662395452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66247db568b7SBarry Smith 
6625b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6626b3d3934dSBarry Smith 
6627b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6628b3d3934dSBarry Smith @*/
6629b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6630b3d3934dSBarry Smith {
6631b3d3934dSBarry Smith   PetscInt       i;
6632b3d3934dSBarry Smith 
6633b3d3934dSBarry Smith   PetscFunctionBegin;
6634b3d3934dSBarry Smith   *names = NULL;
6635b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6636b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
66375537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6638b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6639b3d3934dSBarry Smith       break;
6640387f4636SBarry Smith     }
6641387f4636SBarry Smith   }
6642387f4636SBarry Smith   PetscFunctionReturn(0);
6643387f4636SBarry Smith }
6644387f4636SBarry Smith 
6645a66092f1SBarry Smith /*@C
6646a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6647a66092f1SBarry Smith 
6648a66092f1SBarry Smith    Collective on TS
6649a66092f1SBarry Smith 
6650a66092f1SBarry Smith    Input Parameters:
6651a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6652a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
6653a66092f1SBarry Smith 
6654a66092f1SBarry Smith    Level: intermediate
6655a66092f1SBarry Smith 
6656a66092f1SBarry Smith .keywords: TS,  vector, monitor, view
6657a66092f1SBarry Smith 
6658a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6659a66092f1SBarry Smith @*/
6660a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6661a66092f1SBarry Smith {
6662a66092f1SBarry Smith   PetscInt          j = 0,k;
6663a66092f1SBarry Smith   PetscErrorCode    ierr;
6664a66092f1SBarry Smith 
6665a66092f1SBarry Smith   PetscFunctionBegin;
6666a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6667a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6668a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6669a66092f1SBarry Smith   while (displaynames[j]) j++;
6670a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6671a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6672a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6673a66092f1SBarry Smith   j = 0;
6674a66092f1SBarry Smith   while (displaynames[j]) {
6675a66092f1SBarry Smith     k = 0;
6676a66092f1SBarry Smith     while (ctx->names[k]) {
6677a66092f1SBarry Smith       PetscBool flg;
6678a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6679a66092f1SBarry Smith       if (flg) {
6680a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6681a66092f1SBarry Smith         break;
6682a66092f1SBarry Smith       }
6683a66092f1SBarry Smith       k++;
6684a66092f1SBarry Smith     }
6685a66092f1SBarry Smith     j++;
6686a66092f1SBarry Smith   }
6687a66092f1SBarry Smith   PetscFunctionReturn(0);
6688a66092f1SBarry Smith }
6689a66092f1SBarry Smith 
6690387f4636SBarry Smith /*@C
6691387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6692387f4636SBarry Smith 
6693387f4636SBarry Smith    Collective on TS
6694387f4636SBarry Smith 
6695387f4636SBarry Smith    Input Parameters:
6696387f4636SBarry Smith +  ts - the TS context
6697387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
6698387f4636SBarry Smith 
669995452b02SPatrick Sanan    Notes:
670095452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67017db568b7SBarry Smith 
6702387f4636SBarry Smith    Level: intermediate
6703387f4636SBarry Smith 
6704387f4636SBarry Smith .keywords: TS,  vector, monitor, view
6705387f4636SBarry Smith 
6706387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6707387f4636SBarry Smith @*/
6708387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6709387f4636SBarry Smith {
6710a66092f1SBarry Smith   PetscInt          i;
6711387f4636SBarry Smith   PetscErrorCode    ierr;
6712387f4636SBarry Smith 
6713387f4636SBarry Smith   PetscFunctionBegin;
6714387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6715387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67165537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6717b3d3934dSBarry Smith       break;
6718b037adc7SBarry Smith     }
6719b037adc7SBarry Smith   }
6720b037adc7SBarry Smith   PetscFunctionReturn(0);
6721b037adc7SBarry Smith }
6722b037adc7SBarry Smith 
672380666b62SBarry Smith /*@C
672480666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
672580666b62SBarry Smith 
672680666b62SBarry Smith    Collective on TS
672780666b62SBarry Smith 
672880666b62SBarry Smith    Input Parameters:
672980666b62SBarry Smith +  ts - the TS context
673080666b62SBarry Smith .  transform - the transform function
67317684fa3eSBarry Smith .  destroy - function to destroy the optional context
673280666b62SBarry Smith -  ctx - optional context used by transform function
673380666b62SBarry Smith 
673495452b02SPatrick Sanan    Notes:
673595452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67367db568b7SBarry Smith 
673780666b62SBarry Smith    Level: intermediate
673880666b62SBarry Smith 
673980666b62SBarry Smith .keywords: TS,  vector, monitor, view
674080666b62SBarry Smith 
6741a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
674280666b62SBarry Smith @*/
67437684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
674480666b62SBarry Smith {
674580666b62SBarry Smith   PetscInt          i;
6746a66092f1SBarry Smith   PetscErrorCode    ierr;
674780666b62SBarry Smith 
674880666b62SBarry Smith   PetscFunctionBegin;
674980666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
675080666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67515537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
675280666b62SBarry Smith     }
675380666b62SBarry Smith   }
675480666b62SBarry Smith   PetscFunctionReturn(0);
675580666b62SBarry Smith }
675680666b62SBarry Smith 
6757e673d494SBarry Smith /*@C
6758e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6759e673d494SBarry Smith 
6760e673d494SBarry Smith    Collective on TSLGCtx
6761e673d494SBarry Smith 
6762e673d494SBarry Smith    Input Parameters:
6763e673d494SBarry Smith +  ts - the TS context
6764e673d494SBarry Smith .  transform - the transform function
67657684fa3eSBarry Smith .  destroy - function to destroy the optional context
6766e673d494SBarry Smith -  ctx - optional context used by transform function
6767e673d494SBarry Smith 
6768e673d494SBarry Smith    Level: intermediate
6769e673d494SBarry Smith 
6770e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6771e673d494SBarry Smith 
6772a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6773e673d494SBarry Smith @*/
67747684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6775e673d494SBarry Smith {
6776e673d494SBarry Smith   PetscFunctionBegin;
6777e673d494SBarry Smith   ctx->transform    = transform;
67787684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6779e673d494SBarry Smith   ctx->transformctx = tctx;
6780e673d494SBarry Smith   PetscFunctionReturn(0);
6781e673d494SBarry Smith }
6782e673d494SBarry Smith 
6783ef20d060SBarry Smith /*@C
67848b668821SLisandro Dalcin    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error
6785ef20d060SBarry Smith        in a time based line graph
6786ef20d060SBarry Smith 
6787ef20d060SBarry Smith    Collective on TS
6788ef20d060SBarry Smith 
6789ef20d060SBarry Smith    Input Parameters:
6790ef20d060SBarry Smith +  ts - the TS context
6791ef20d060SBarry Smith .  step - current time-step
6792ef20d060SBarry Smith .  ptime - current time
67937db568b7SBarry Smith .  u - current solution
67947db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6795ef20d060SBarry Smith 
6796ef20d060SBarry Smith    Level: intermediate
6797ef20d060SBarry Smith 
679895452b02SPatrick Sanan    Notes:
679995452b02SPatrick Sanan     Each process in a parallel run displays its component errors in a separate window
6800abd5a294SJed Brown 
6801abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6802abd5a294SJed Brown 
6803abd5a294SJed Brown    Options Database Keys:
68044f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6805ef20d060SBarry Smith 
6806ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
6807ef20d060SBarry Smith 
6808abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6809ef20d060SBarry Smith @*/
68100910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6811ef20d060SBarry Smith {
6812ef20d060SBarry Smith   PetscErrorCode    ierr;
68130b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6814ef20d060SBarry Smith   const PetscScalar *yy;
6815ef20d060SBarry Smith   Vec               y;
6816ef20d060SBarry Smith 
6817ef20d060SBarry Smith   PetscFunctionBegin;
6818a9f9c1f6SBarry Smith   if (!step) {
6819a9f9c1f6SBarry Smith     PetscDrawAxis axis;
68206934998bSLisandro Dalcin     PetscInt      dim;
6821a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
68228b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr);
68230910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6824a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6825a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6826a9f9c1f6SBarry Smith   }
68270910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6828ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
68290910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6830ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6831e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6832e3efe391SJed Brown   {
6833e3efe391SJed Brown     PetscReal *yreal;
6834e3efe391SJed Brown     PetscInt  i,n;
6835e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6836785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6837e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
68380b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6839e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6840e3efe391SJed Brown   }
6841e3efe391SJed Brown #else
68420b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6843e3efe391SJed Brown #endif
6844ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6845ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6846b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
68470b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
68486934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
68493923b477SBarry Smith   }
6850ef20d060SBarry Smith   PetscFunctionReturn(0);
6851ef20d060SBarry Smith }
6852ef20d060SBarry Smith 
68537cf37e64SBarry Smith /*@C
68547cf37e64SBarry Smith    TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep
68557cf37e64SBarry Smith 
68567cf37e64SBarry Smith    Collective on TS
68577cf37e64SBarry Smith 
68587cf37e64SBarry Smith    Input Parameters:
68597cf37e64SBarry Smith +  ts - the TS context
68607cf37e64SBarry Smith .  step - current time-step
68617cf37e64SBarry Smith .  ptime - current time
68627cf37e64SBarry Smith .  u - current solution
68637cf37e64SBarry Smith -  dctx - unused context
68647cf37e64SBarry Smith 
68657cf37e64SBarry Smith    Level: intermediate
68667cf37e64SBarry Smith 
68677cf37e64SBarry Smith    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
68687cf37e64SBarry Smith 
68697cf37e64SBarry Smith    Options Database Keys:
68707cf37e64SBarry Smith .  -ts_monitor_error - create a graphical monitor of error history
68717cf37e64SBarry Smith 
68727cf37e64SBarry Smith .keywords: TS,  vector, monitor, view
68737cf37e64SBarry Smith 
68747cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
68757cf37e64SBarry Smith @*/
6876edbaebb3SBarry Smith PetscErrorCode  TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
68777cf37e64SBarry Smith {
68787cf37e64SBarry Smith   PetscErrorCode    ierr;
68797cf37e64SBarry Smith   Vec               y;
68807cf37e64SBarry Smith   PetscReal         nrm;
6881edbaebb3SBarry Smith   PetscBool         flg;
68827cf37e64SBarry Smith 
68837cf37e64SBarry Smith   PetscFunctionBegin;
68847cf37e64SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
68857cf37e64SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
68867cf37e64SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6887edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr);
6888edbaebb3SBarry Smith   if (flg) {
68897cf37e64SBarry Smith     ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
6890edbaebb3SBarry Smith     ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr);
6891edbaebb3SBarry Smith   }
6892edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr);
6893edbaebb3SBarry Smith   if (flg) {
6894edbaebb3SBarry Smith     ierr = VecView(y,vf->viewer);CHKERRQ(ierr);
6895edbaebb3SBarry Smith   }
6896edbaebb3SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
68977cf37e64SBarry Smith   PetscFunctionReturn(0);
68987cf37e64SBarry Smith }
68997cf37e64SBarry Smith 
6900201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6901201da799SBarry Smith {
6902201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6903201da799SBarry Smith   PetscReal      x   = ptime,y;
6904201da799SBarry Smith   PetscErrorCode ierr;
6905201da799SBarry Smith   PetscInt       its;
6906201da799SBarry Smith 
6907201da799SBarry Smith   PetscFunctionBegin;
690863e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
6909201da799SBarry Smith   if (!n) {
6910201da799SBarry Smith     PetscDrawAxis axis;
6911201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6912201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
6913201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6914201da799SBarry Smith     ctx->snes_its = 0;
6915201da799SBarry Smith   }
6916201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
6917201da799SBarry Smith   y    = its - ctx->snes_its;
6918201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
69193fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6920201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69216934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
6922201da799SBarry Smith   }
6923201da799SBarry Smith   ctx->snes_its = its;
6924201da799SBarry Smith   PetscFunctionReturn(0);
6925201da799SBarry Smith }
6926201da799SBarry Smith 
6927201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6928201da799SBarry Smith {
6929201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6930201da799SBarry Smith   PetscReal      x   = ptime,y;
6931201da799SBarry Smith   PetscErrorCode ierr;
6932201da799SBarry Smith   PetscInt       its;
6933201da799SBarry Smith 
6934201da799SBarry Smith   PetscFunctionBegin;
693563e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
6936201da799SBarry Smith   if (!n) {
6937201da799SBarry Smith     PetscDrawAxis axis;
6938201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6939201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
6940201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6941201da799SBarry Smith     ctx->ksp_its = 0;
6942201da799SBarry Smith   }
6943201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
6944201da799SBarry Smith   y    = its - ctx->ksp_its;
6945201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
694699fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6947201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69486934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
6949201da799SBarry Smith   }
6950201da799SBarry Smith   ctx->ksp_its = its;
6951201da799SBarry Smith   PetscFunctionReturn(0);
6952201da799SBarry Smith }
6953f9c1d6abSBarry Smith 
6954f9c1d6abSBarry Smith /*@
6955f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
6956f9c1d6abSBarry Smith 
6957f9c1d6abSBarry Smith    Collective on TS and Vec
6958f9c1d6abSBarry Smith 
6959f9c1d6abSBarry Smith    Input Parameters:
6960f9c1d6abSBarry Smith +  ts - the TS context
6961f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
6962f9c1d6abSBarry Smith 
6963f9c1d6abSBarry Smith    Output Parameters:
6964f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
6965f9c1d6abSBarry Smith 
6966f9c1d6abSBarry Smith    Level: developer
6967f9c1d6abSBarry Smith 
6968f9c1d6abSBarry Smith .keywords: TS, compute
6969f9c1d6abSBarry Smith 
6970f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
6971f9c1d6abSBarry Smith @*/
6972f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
6973f9c1d6abSBarry Smith {
6974f9c1d6abSBarry Smith   PetscErrorCode ierr;
6975f9c1d6abSBarry Smith 
6976f9c1d6abSBarry Smith   PetscFunctionBegin;
6977f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6978ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
6979f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
6980f9c1d6abSBarry Smith   PetscFunctionReturn(0);
6981f9c1d6abSBarry Smith }
698224655328SShri 
6983b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
6984b3d3934dSBarry Smith /*@C
6985b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
6986b3d3934dSBarry Smith 
6987b3d3934dSBarry Smith    Collective on TS
6988b3d3934dSBarry Smith 
6989b3d3934dSBarry Smith    Input Parameters:
6990b3d3934dSBarry Smith .  ts  - the ODE solver object
6991b3d3934dSBarry Smith 
6992b3d3934dSBarry Smith    Output Parameter:
6993b3d3934dSBarry Smith .  ctx - the context
6994b3d3934dSBarry Smith 
6995b3d3934dSBarry Smith    Level: intermediate
6996b3d3934dSBarry Smith 
6997b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
6998b3d3934dSBarry Smith 
6999b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7000b3d3934dSBarry Smith 
7001b3d3934dSBarry Smith @*/
7002b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7003b3d3934dSBarry Smith {
7004b3d3934dSBarry Smith   PetscErrorCode ierr;
7005b3d3934dSBarry Smith 
7006b3d3934dSBarry Smith   PetscFunctionBegin;
7007a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7008b3d3934dSBarry Smith   PetscFunctionReturn(0);
7009b3d3934dSBarry Smith }
7010b3d3934dSBarry Smith 
7011b3d3934dSBarry Smith /*@C
7012b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7013b3d3934dSBarry Smith 
7014b3d3934dSBarry Smith    Collective on TS
7015b3d3934dSBarry Smith 
7016b3d3934dSBarry Smith    Input Parameters:
7017b3d3934dSBarry Smith +  ts - the TS context
7018b3d3934dSBarry Smith .  step - current time-step
7019b3d3934dSBarry Smith .  ptime - current time
70207db568b7SBarry Smith .  u  - current solution
70217db568b7SBarry Smith -  dctx - the envelope context
7022b3d3934dSBarry Smith 
7023b3d3934dSBarry Smith    Options Database:
7024b3d3934dSBarry Smith .  -ts_monitor_envelope
7025b3d3934dSBarry Smith 
7026b3d3934dSBarry Smith    Level: intermediate
7027b3d3934dSBarry Smith 
702895452b02SPatrick Sanan    Notes:
702995452b02SPatrick Sanan     after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7030b3d3934dSBarry Smith 
7031b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7032b3d3934dSBarry Smith 
70337db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7034b3d3934dSBarry Smith @*/
70357db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7036b3d3934dSBarry Smith {
7037b3d3934dSBarry Smith   PetscErrorCode       ierr;
70387db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7039b3d3934dSBarry Smith 
7040b3d3934dSBarry Smith   PetscFunctionBegin;
7041b3d3934dSBarry Smith   if (!ctx->max) {
7042b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7043b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7044b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7045b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7046b3d3934dSBarry Smith   } else {
7047b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7048b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7049b3d3934dSBarry Smith   }
7050b3d3934dSBarry Smith   PetscFunctionReturn(0);
7051b3d3934dSBarry Smith }
7052b3d3934dSBarry Smith 
7053b3d3934dSBarry Smith /*@C
7054b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7055b3d3934dSBarry Smith 
7056b3d3934dSBarry Smith    Collective on TS
7057b3d3934dSBarry Smith 
7058b3d3934dSBarry Smith    Input Parameter:
7059b3d3934dSBarry Smith .  ts - the TS context
7060b3d3934dSBarry Smith 
7061b3d3934dSBarry Smith    Output Parameter:
7062b3d3934dSBarry Smith +  max - the maximum values
7063b3d3934dSBarry Smith -  min - the minimum values
7064b3d3934dSBarry Smith 
706595452b02SPatrick Sanan    Notes:
706695452b02SPatrick Sanan     If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
70677db568b7SBarry Smith 
7068b3d3934dSBarry Smith    Level: intermediate
7069b3d3934dSBarry Smith 
7070b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7071b3d3934dSBarry Smith 
7072b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7073b3d3934dSBarry Smith @*/
7074b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7075b3d3934dSBarry Smith {
7076b3d3934dSBarry Smith   PetscInt i;
7077b3d3934dSBarry Smith 
7078b3d3934dSBarry Smith   PetscFunctionBegin;
7079b3d3934dSBarry Smith   if (max) *max = NULL;
7080b3d3934dSBarry Smith   if (min) *min = NULL;
7081b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7082b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
70835537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7084b3d3934dSBarry Smith       if (max) *max = ctx->max;
7085b3d3934dSBarry Smith       if (min) *min = ctx->min;
7086b3d3934dSBarry Smith       break;
7087b3d3934dSBarry Smith     }
7088b3d3934dSBarry Smith   }
7089b3d3934dSBarry Smith   PetscFunctionReturn(0);
7090b3d3934dSBarry Smith }
7091b3d3934dSBarry Smith 
7092b3d3934dSBarry Smith /*@C
7093b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7094b3d3934dSBarry Smith 
7095b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7096b3d3934dSBarry Smith 
7097b3d3934dSBarry Smith    Input Parameter:
7098b3d3934dSBarry Smith .  ctx - the monitor context
7099b3d3934dSBarry Smith 
7100b3d3934dSBarry Smith    Level: intermediate
7101b3d3934dSBarry Smith 
7102b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
7103b3d3934dSBarry Smith 
71047db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7105b3d3934dSBarry Smith @*/
7106b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7107b3d3934dSBarry Smith {
7108b3d3934dSBarry Smith   PetscErrorCode ierr;
7109b3d3934dSBarry Smith 
7110b3d3934dSBarry Smith   PetscFunctionBegin;
7111b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7112b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7113b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7114b3d3934dSBarry Smith   PetscFunctionReturn(0);
7115b3d3934dSBarry Smith }
7116f2dee214SBarry Smith 
711724655328SShri /*@
7118dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
7119dcb233daSLisandro Dalcin 
7120dcb233daSLisandro Dalcin    Collective on TS
7121dcb233daSLisandro Dalcin 
7122dcb233daSLisandro Dalcin    Input Parameter:
7123dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
7124dcb233daSLisandro Dalcin 
7125dcb233daSLisandro Dalcin    Level: advanced
7126dcb233daSLisandro Dalcin 
7127dcb233daSLisandro Dalcin    Notes:
7128dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
7129dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
7130dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
7131dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
7132dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
7133dcb233daSLisandro Dalcin    discontinuous source terms).
7134dcb233daSLisandro Dalcin 
7135dcb233daSLisandro Dalcin .keywords: TS, timestep, restart
7136dcb233daSLisandro Dalcin 
7137dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
7138dcb233daSLisandro Dalcin @*/
7139dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
7140dcb233daSLisandro Dalcin {
7141dcb233daSLisandro Dalcin   PetscFunctionBegin;
7142dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7143dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
7144dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
7145dcb233daSLisandro Dalcin }
7146dcb233daSLisandro Dalcin 
7147dcb233daSLisandro Dalcin /*@
714824655328SShri    TSRollBack - Rolls back one time step
714924655328SShri 
715024655328SShri    Collective on TS
715124655328SShri 
715224655328SShri    Input Parameter:
715324655328SShri .  ts - the TS context obtained from TSCreate()
715424655328SShri 
715524655328SShri    Level: advanced
715624655328SShri 
715724655328SShri .keywords: TS, timestep, rollback
715824655328SShri 
715924655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
716024655328SShri @*/
716124655328SShri PetscErrorCode  TSRollBack(TS ts)
716224655328SShri {
716324655328SShri   PetscErrorCode ierr;
716424655328SShri 
716524655328SShri   PetscFunctionBegin;
716624655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7167b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
716824655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
716924655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
717024655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
717124655328SShri   ts->ptime = ts->ptime_prev;
7172be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
71732808aa04SLisandro Dalcin   ts->steps--;
7174b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
717524655328SShri   PetscFunctionReturn(0);
717624655328SShri }
7177aeb4809dSShri Abhyankar 
7178ff22ae23SHong Zhang /*@
7179ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7180ff22ae23SHong Zhang 
7181ff22ae23SHong Zhang    Input Parameter:
7182ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7183ff22ae23SHong Zhang 
7184ff22ae23SHong Zhang    Level: advanced
7185ff22ae23SHong Zhang 
7186ff22ae23SHong Zhang .keywords: TS, getstages
7187ff22ae23SHong Zhang 
7188ff22ae23SHong Zhang .seealso: TSCreate()
7189ff22ae23SHong Zhang @*/
7190ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7191ff22ae23SHong Zhang {
7192ff22ae23SHong Zhang   PetscErrorCode ierr;
7193ff22ae23SHong Zhang 
7194ff22ae23SHong Zhang   PetscFunctionBegin;
7195ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7196ff22ae23SHong Zhang   PetscValidPointer(ns,2);
7197ff22ae23SHong Zhang 
7198ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
7199ff22ae23SHong Zhang   else {
7200ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7201ff22ae23SHong Zhang   }
7202ff22ae23SHong Zhang   PetscFunctionReturn(0);
7203ff22ae23SHong Zhang }
7204ff22ae23SHong Zhang 
7205847ff0e1SMatthew G. Knepley /*@C
7206847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7207847ff0e1SMatthew G. Knepley 
7208847ff0e1SMatthew G. Knepley   Collective on SNES
7209847ff0e1SMatthew G. Knepley 
7210847ff0e1SMatthew G. Knepley   Input Parameters:
7211847ff0e1SMatthew G. Knepley + ts - the TS context
7212847ff0e1SMatthew G. Knepley . t - current timestep
7213847ff0e1SMatthew G. Knepley . U - state vector
7214847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7215847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7216847ff0e1SMatthew G. Knepley - ctx - an optional user context
7217847ff0e1SMatthew G. Knepley 
7218847ff0e1SMatthew G. Knepley   Output Parameters:
7219847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7220847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7221847ff0e1SMatthew G. Knepley 
7222847ff0e1SMatthew G. Knepley   Level: intermediate
7223847ff0e1SMatthew G. Knepley 
7224847ff0e1SMatthew G. Knepley   Notes:
7225847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7226847ff0e1SMatthew G. Knepley 
7227847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7228847ff0e1SMatthew G. Knepley 
7229847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7230847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7231847ff0e1SMatthew G. Knepley 
7232847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7233847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7234847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7235847ff0e1SMatthew G. Knepley 
7236847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
7237847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7238847ff0e1SMatthew G. Knepley @*/
7239847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7240847ff0e1SMatthew G. Knepley {
7241847ff0e1SMatthew G. Knepley   SNES           snes;
7242847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7243847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7244847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7245847ff0e1SMatthew G. Knepley 
7246847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7247c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7248847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7249847ff0e1SMatthew G. Knepley   if (!color) {
7250847ff0e1SMatthew G. Knepley     DM         dm;
7251847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7252847ff0e1SMatthew G. Knepley 
7253847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7254847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7255847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7256847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7257847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7258847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7259847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7260847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7261847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7262847ff0e1SMatthew G. Knepley     } else {
7263847ff0e1SMatthew G. Knepley       MatColoring mc;
7264847ff0e1SMatthew G. Knepley 
7265847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7266847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7267847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7268847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7269847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7270847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7271847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7272847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7273847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7274847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7275847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7276847ff0e1SMatthew G. Knepley     }
7277847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7278847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7279847ff0e1SMatthew G. Knepley   }
7280847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7281847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7282847ff0e1SMatthew G. Knepley   if (J != B) {
7283847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7284847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7285847ff0e1SMatthew G. Knepley   }
7286847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7287847ff0e1SMatthew G. Knepley }
728893b34091SDebojyoti Ghosh 
7289cb9d8021SPierre Barbier de Reuille /*@
7290cb9d8021SPierre Barbier de Reuille     TSSetFunctionDomainError - Set the function testing if the current state vector is valid
7291cb9d8021SPierre Barbier de Reuille 
7292cb9d8021SPierre Barbier de Reuille     Input Parameters:
7293cb9d8021SPierre Barbier de Reuille     ts - the TS context
7294cb9d8021SPierre Barbier de Reuille     func - function called within TSFunctionDomainError
7295cb9d8021SPierre Barbier de Reuille 
7296cb9d8021SPierre Barbier de Reuille     Level: intermediate
7297cb9d8021SPierre Barbier de Reuille 
7298cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain
7299cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError()
7300cb9d8021SPierre Barbier de Reuille @*/
7301cb9d8021SPierre Barbier de Reuille 
7302d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7303cb9d8021SPierre Barbier de Reuille {
7304cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7305cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7306cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7307cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7308cb9d8021SPierre Barbier de Reuille }
7309cb9d8021SPierre Barbier de Reuille 
7310cb9d8021SPierre Barbier de Reuille /*@
7311cb9d8021SPierre Barbier de Reuille     TSFunctionDomainError - Check if the current state is valid
7312cb9d8021SPierre Barbier de Reuille 
7313cb9d8021SPierre Barbier de Reuille     Input Parameters:
7314cb9d8021SPierre Barbier de Reuille     ts - the TS context
7315cb9d8021SPierre Barbier de Reuille     stagetime - time of the simulation
7316d183316bSPierre Barbier de Reuille     Y - state vector to check.
7317cb9d8021SPierre Barbier de Reuille 
7318cb9d8021SPierre Barbier de Reuille     Output Parameter:
7319cb9d8021SPierre Barbier de Reuille     accept - Set to PETSC_FALSE if the current state vector is valid.
7320cb9d8021SPierre Barbier de Reuille 
7321cb9d8021SPierre Barbier de Reuille     Note:
7322cb9d8021SPierre Barbier de Reuille     This function should be used to ensure the state is in a valid part of the space.
7323cb9d8021SPierre Barbier de Reuille     For example, one can ensure here all values are positive.
732496a0c994SBarry Smith 
732596a0c994SBarry Smith     Level: advanced
7326cb9d8021SPierre Barbier de Reuille @*/
7327d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7328cb9d8021SPierre Barbier de Reuille {
7329cb9d8021SPierre Barbier de Reuille   PetscErrorCode ierr;
7330cb9d8021SPierre Barbier de Reuille 
7331cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7332cb9d8021SPierre Barbier de Reuille 
7333cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7334cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7335cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7336d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7337cb9d8021SPierre Barbier de Reuille   }
7338cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7339cb9d8021SPierre Barbier de Reuille }
73401ceb14c0SBarry Smith 
734193b34091SDebojyoti Ghosh /*@C
7342e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
734393b34091SDebojyoti Ghosh 
734493b34091SDebojyoti Ghosh   Collective on MPI_Comm
734593b34091SDebojyoti Ghosh 
734693b34091SDebojyoti Ghosh   Input Parameter:
734793b34091SDebojyoti Ghosh . tsin    - The input TS
734893b34091SDebojyoti Ghosh 
734993b34091SDebojyoti Ghosh   Output Parameter:
7350e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
735193b34091SDebojyoti Ghosh 
73525eca1a21SEmil Constantinescu   Notes:
73535eca1a21SEmil 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.
73545eca1a21SEmil Constantinescu 
7355928bb9adSStefano 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);
73565eca1a21SEmil Constantinescu 
73575eca1a21SEmil Constantinescu   Level: developer
735893b34091SDebojyoti Ghosh 
7359e5168f73SEmil Constantinescu .keywords: TS, clone
7360e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
736193b34091SDebojyoti Ghosh @*/
7362baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
736393b34091SDebojyoti Ghosh {
736493b34091SDebojyoti Ghosh   TS             t;
736593b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7366dc846ba4SSatish Balay   SNES           snes_start;
7367dc846ba4SSatish Balay   DM             dm;
7368dc846ba4SSatish Balay   TSType         type;
736993b34091SDebojyoti Ghosh 
737093b34091SDebojyoti Ghosh   PetscFunctionBegin;
737193b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
737293b34091SDebojyoti Ghosh   *tsout = NULL;
737393b34091SDebojyoti Ghosh 
73747a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
737593b34091SDebojyoti Ghosh 
737693b34091SDebojyoti Ghosh   /* General TS description */
737793b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
737893b34091SDebojyoti Ghosh   t->setupcalled       = 0;
737993b34091SDebojyoti Ghosh   t->ksp_its           = 0;
738093b34091SDebojyoti Ghosh   t->snes_its          = 0;
738193b34091SDebojyoti Ghosh   t->nwork             = 0;
738293b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
738393b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
738493b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
738593b34091SDebojyoti Ghosh 
738634561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
738734561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7388d15a3a53SEmil Constantinescu 
738993b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
739093b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
739193b34091SDebojyoti Ghosh 
739293b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
739351699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
739493b34091SDebojyoti Ghosh 
7395e7069c78SShri   t->trajectory = tsin->trajectory;
7396e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7397e7069c78SShri 
7398e7069c78SShri   t->event = tsin->event;
73996b10a48eSSatish Balay   if (t->event) t->event->refct++;
7400e7069c78SShri 
740193b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
740293b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7403e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
740493b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
740593b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
740693b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
740793b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
740893b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
740993b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
741093b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
741193b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
741293b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
741393b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
741493b34091SDebojyoti Ghosh 
741593b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
741693b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
741793b34091SDebojyoti Ghosh 
741893b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
741993b34091SDebojyoti Ghosh 
742093b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
742193b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
742293b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
742393b34091SDebojyoti Ghosh 
742493b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
742593b34091SDebojyoti Ghosh 
74260d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
74270d4fed19SBarry Smith     PetscInt i;
74280d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
74290d4fed19SBarry Smith     for (i=0; i<10; i++) {
74300d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
74310d4fed19SBarry Smith     }
74320d4fed19SBarry Smith   }
743393b34091SDebojyoti Ghosh   *tsout = t;
743493b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
743593b34091SDebojyoti Ghosh }
7436f3b1f45cSBarry Smith 
7437f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
7438f3b1f45cSBarry Smith {
7439f3b1f45cSBarry Smith   PetscErrorCode ierr;
7440f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
7441f3b1f45cSBarry Smith 
7442f3b1f45cSBarry Smith   PetscFunctionBegin;
7443f3b1f45cSBarry Smith   ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr);
7444f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7445f3b1f45cSBarry Smith }
7446f3b1f45cSBarry Smith 
7447f3b1f45cSBarry Smith /*@
7448f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
7449f3b1f45cSBarry Smith 
7450f3b1f45cSBarry Smith    Logically Collective on TS and Mat
7451f3b1f45cSBarry Smith 
7452f3b1f45cSBarry Smith     Input Parameters:
7453f3b1f45cSBarry Smith     TS - the time stepping routine
7454f3b1f45cSBarry Smith 
7455f3b1f45cSBarry Smith    Output Parameter:
7456f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7457f3b1f45cSBarry Smith 
7458f3b1f45cSBarry Smith    Options Database:
7459f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
7460f3b1f45cSBarry Smith 
7461f3b1f45cSBarry Smith    Level: advanced
7462f3b1f45cSBarry Smith 
746395452b02SPatrick Sanan    Notes:
746495452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7465f3b1f45cSBarry Smith 
7466f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
7467f3b1f45cSBarry Smith @*/
7468f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
7469f3b1f45cSBarry Smith {
7470f3b1f45cSBarry Smith   Mat            J,B;
7471f3b1f45cSBarry Smith   PetscErrorCode ierr;
7472f3b1f45cSBarry Smith   TSRHSJacobian  func;
7473f3b1f45cSBarry Smith   void*          ctx;
7474f3b1f45cSBarry Smith 
7475f3b1f45cSBarry Smith   PetscFunctionBegin;
7476f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7477f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7478f3b1f45cSBarry Smith   ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7479f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7480f3b1f45cSBarry Smith }
7481f3b1f45cSBarry Smith 
7482f3b1f45cSBarry Smith /*@C
7483f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
7484f3b1f45cSBarry Smith 
7485f3b1f45cSBarry Smith    Logically Collective on TS and Mat
7486f3b1f45cSBarry Smith 
7487f3b1f45cSBarry Smith     Input Parameters:
7488f3b1f45cSBarry Smith     TS - the time stepping routine
7489f3b1f45cSBarry Smith 
7490f3b1f45cSBarry Smith    Output Parameter:
7491f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7492f3b1f45cSBarry Smith 
7493f3b1f45cSBarry Smith    Options Database:
7494f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
7495f3b1f45cSBarry Smith 
749695452b02SPatrick Sanan    Notes:
749795452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7498f3b1f45cSBarry Smith 
7499f3b1f45cSBarry Smith    Level: advanced
7500f3b1f45cSBarry Smith 
7501f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
7502f3b1f45cSBarry Smith @*/
7503f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
7504f3b1f45cSBarry Smith {
7505f3b1f45cSBarry Smith   Mat            J,B;
7506f3b1f45cSBarry Smith   PetscErrorCode ierr;
7507f3b1f45cSBarry Smith   void           *ctx;
7508f3b1f45cSBarry Smith   TSRHSJacobian  func;
7509f3b1f45cSBarry Smith 
7510f3b1f45cSBarry Smith   PetscFunctionBegin;
7511f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7512f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7513f3b1f45cSBarry Smith   ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7514f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7515f3b1f45cSBarry Smith }
7516