xref: /petsc/src/ts/interface/ts.c (revision 971015bcbf92bfb5359474763467c3e4753de490)
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);
1391ef27442SStefano Zampini   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
1401ef27442SStefano 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 
4141ef27442SStefano Zampini   /* why do we have to do this here and not during TSSetUp? */
4154f122a70SLisandro Dalcin   ierr = TSGetSNES(ts,&ts->snes);CHKERRQ(ierr);
4161ef27442SStefano Zampini   if (ts->problem_type == TS_LINEAR) {
4171ef27442SStefano Zampini     ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&flg,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
4181ef27442SStefano Zampini     if (!flg) { ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr); }
4191ef27442SStefano 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);
538*971015bcSStefano Zampini 
5396c1e1eecSBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
540*971015bcSStefano Zampini     /* restore back RHS Jacobian matrices if they have been shifted/scaled */
541*971015bcSStefano Zampini     if (A == ts->Arhs) {
542*971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
543*971015bcSStefano Zampini         ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
544*971015bcSStefano Zampini       }
545*971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
546*971015bcSStefano Zampini         ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
547*971015bcSStefano Zampini       }
548*971015bcSStefano Zampini     }
549*971015bcSStefano Zampini     if (B && B == ts->Brhs && A != B) {
550*971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
551*971015bcSStefano Zampini         ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
552*971015bcSStefano Zampini       }
553*971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
554*971015bcSStefano Zampini         ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
555*971015bcSStefano Zampini       }
556*971015bcSStefano Zampini     }
557*971015bcSStefano Zampini     ts->rhsjacobian.shift = 0;
558*971015bcSStefano Zampini     ts->rhsjacobian.scale = 1.;
5590e4ef248SJed Brown     PetscFunctionReturn(0);
560f8ede8e7SJed Brown   }
561d90be118SSean Farley 
562ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
563d90be118SSean Farley 
564e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
565*971015bcSStefano Zampini     if (A == ts->Arhs) {
566*971015bcSStefano Zampini       /* MatScale has a short path for this case.
567*971015bcSStefano Zampini          However, this code path is taken the first time TSComputeRHSJacobian is called
568*971015bcSStefano Zampini          and the matrices have not assembled yet */
569*971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
57094ab13aaSBarry Smith         ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
571*971015bcSStefano Zampini       }
572*971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
57394ab13aaSBarry Smith         ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
574*971015bcSStefano Zampini       }
575*971015bcSStefano Zampini     }
576*971015bcSStefano Zampini     if (B && B == ts->Brhs && A != B) {
577*971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
57894ab13aaSBarry Smith         ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
579*971015bcSStefano Zampini       }
580*971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
58194ab13aaSBarry Smith         ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
582e1244c69SJed Brown       }
583*971015bcSStefano Zampini     }
584e1244c69SJed Brown   }
585e1244c69SJed Brown 
58624989b8cSPeter Brune   if (rhsjacobianfunc) {
5876cd88445SBarry Smith     PetscBool missing;
58894ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
589a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
590d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
591a7a1495cSBarry Smith     PetscStackPop;
59294ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
5936cd88445SBarry Smith     if (A) {
5946cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
5956cd88445SBarry 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");
5966cd88445SBarry Smith     }
5976cd88445SBarry Smith     if (B && B != A) {
5986cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
5996cd88445SBarry 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");
6006cd88445SBarry Smith     }
601ef66eb69SBarry Smith   } else {
60294ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
603*971015bcSStefano Zampini     if (B && A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
604ef66eb69SBarry Smith   }
6050e4ef248SJed Brown   ts->rhsjacobian.time  = t;
606*971015bcSStefano Zampini   ts->rhsjacobian.shift = 0;
607*971015bcSStefano Zampini   ts->rhsjacobian.scale = 1.;
6087f79407eSBarry Smith   ierr                  = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr);
60959e4f3c8SBarry Smith   ierr                  = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
610a7a1495cSBarry Smith   PetscFunctionReturn(0);
611a7a1495cSBarry Smith }
612a7a1495cSBarry Smith 
613316643e7SJed Brown /*@
614d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
615d763cef2SBarry Smith 
616316643e7SJed Brown    Collective on TS and Vec
617316643e7SJed Brown 
618316643e7SJed Brown    Input Parameters:
619316643e7SJed Brown +  ts - the TS context
620316643e7SJed Brown .  t - current time
6210910c330SBarry Smith -  U - state vector
622316643e7SJed Brown 
623316643e7SJed Brown    Output Parameter:
624316643e7SJed Brown .  y - right hand side
625316643e7SJed Brown 
626316643e7SJed Brown    Note:
627316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
628316643e7SJed Brown    is used internally within the nonlinear solvers.
629316643e7SJed Brown 
630316643e7SJed Brown    Level: developer
631316643e7SJed Brown 
632316643e7SJed Brown .keywords: TS, compute
633316643e7SJed Brown 
634316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
635316643e7SJed Brown @*/
6360910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
637d763cef2SBarry Smith {
638dfbe8321SBarry Smith   PetscErrorCode ierr;
63924989b8cSPeter Brune   TSRHSFunction  rhsfunction;
64024989b8cSPeter Brune   TSIFunction    ifunction;
64124989b8cSPeter Brune   void           *ctx;
64224989b8cSPeter Brune   DM             dm;
64324989b8cSPeter Brune 
644d763cef2SBarry Smith   PetscFunctionBegin;
6450700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6460910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6470700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
64824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
64924989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6500298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
651d763cef2SBarry Smith 
652ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
653d763cef2SBarry Smith 
6540910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
65524989b8cSPeter Brune   if (rhsfunction) {
656d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6570910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
658d763cef2SBarry Smith     PetscStackPop;
659214bc6a2SJed Brown   } else {
660214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
661b2cd27e8SJed Brown   }
66244a41b28SSean Farley 
6630910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
664d763cef2SBarry Smith   PetscFunctionReturn(0);
665d763cef2SBarry Smith }
666d763cef2SBarry Smith 
667ef20d060SBarry Smith /*@
668ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
669ef20d060SBarry Smith 
670ef20d060SBarry Smith    Collective on TS and Vec
671ef20d060SBarry Smith 
672ef20d060SBarry Smith    Input Parameters:
673ef20d060SBarry Smith +  ts - the TS context
674ef20d060SBarry Smith -  t - current time
675ef20d060SBarry Smith 
676ef20d060SBarry Smith    Output Parameter:
6770910c330SBarry Smith .  U - the solution
678ef20d060SBarry Smith 
679ef20d060SBarry Smith    Note:
680ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
681ef20d060SBarry Smith    is used internally within the nonlinear solvers.
682ef20d060SBarry Smith 
683ef20d060SBarry Smith    Level: developer
684ef20d060SBarry Smith 
685ef20d060SBarry Smith .keywords: TS, compute
686ef20d060SBarry Smith 
687abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
688ef20d060SBarry Smith @*/
6890910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
690ef20d060SBarry Smith {
691ef20d060SBarry Smith   PetscErrorCode     ierr;
692ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
693ef20d060SBarry Smith   void               *ctx;
694ef20d060SBarry Smith   DM                 dm;
695ef20d060SBarry Smith 
696ef20d060SBarry Smith   PetscFunctionBegin;
697ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6980910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
699ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
700ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
701ef20d060SBarry Smith 
702ef20d060SBarry Smith   if (solutionfunction) {
7039b7cd975SBarry Smith     PetscStackPush("TS user solution function");
7040910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
705ef20d060SBarry Smith     PetscStackPop;
706ef20d060SBarry Smith   }
707ef20d060SBarry Smith   PetscFunctionReturn(0);
708ef20d060SBarry Smith }
7099b7cd975SBarry Smith /*@
7109b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
7119b7cd975SBarry Smith 
7129b7cd975SBarry Smith    Collective on TS and Vec
7139b7cd975SBarry Smith 
7149b7cd975SBarry Smith    Input Parameters:
7159b7cd975SBarry Smith +  ts - the TS context
7169b7cd975SBarry Smith -  t - current time
7179b7cd975SBarry Smith 
7189b7cd975SBarry Smith    Output Parameter:
7199b7cd975SBarry Smith .  U - the function value
7209b7cd975SBarry Smith 
7219b7cd975SBarry Smith    Note:
7229b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
7239b7cd975SBarry Smith    is used internally within the nonlinear solvers.
7249b7cd975SBarry Smith 
7259b7cd975SBarry Smith    Level: developer
7269b7cd975SBarry Smith 
7279b7cd975SBarry Smith .keywords: TS, compute
7289b7cd975SBarry Smith 
7299b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7309b7cd975SBarry Smith @*/
7319b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7329b7cd975SBarry Smith {
7339b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7349b7cd975SBarry Smith   void               *ctx;
7359b7cd975SBarry Smith   DM                 dm;
7369b7cd975SBarry Smith 
7379b7cd975SBarry Smith   PetscFunctionBegin;
7389b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7399b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7409b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7419b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7429b7cd975SBarry Smith 
7439b7cd975SBarry Smith   if (forcing) {
7449b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7459b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7469b7cd975SBarry Smith     PetscStackPop;
7479b7cd975SBarry Smith   }
7489b7cd975SBarry Smith   PetscFunctionReturn(0);
7499b7cd975SBarry Smith }
750ef20d060SBarry Smith 
751214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
752214bc6a2SJed Brown {
7532dd45cf8SJed Brown   Vec            F;
754214bc6a2SJed Brown   PetscErrorCode ierr;
755214bc6a2SJed Brown 
756214bc6a2SJed Brown   PetscFunctionBegin;
7570298fd71SBarry Smith   *Frhs = NULL;
7580298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
759214bc6a2SJed Brown   if (!ts->Frhs) {
7602dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
761214bc6a2SJed Brown   }
762214bc6a2SJed Brown   *Frhs = ts->Frhs;
763214bc6a2SJed Brown   PetscFunctionReturn(0);
764214bc6a2SJed Brown }
765214bc6a2SJed Brown 
766*971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
767214bc6a2SJed Brown {
768214bc6a2SJed Brown   Mat            A,B;
7692dd45cf8SJed Brown   PetscErrorCode ierr;
77041a1d4d2SBarry Smith   TSIJacobian    ijacobian;
771214bc6a2SJed Brown 
772214bc6a2SJed Brown   PetscFunctionBegin;
773c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
774c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
77541a1d4d2SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr);
776214bc6a2SJed Brown   if (Arhs) {
777214bc6a2SJed Brown     if (!ts->Arhs) {
77841a1d4d2SBarry Smith       if (ijacobian) {
779214bc6a2SJed Brown         ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
78041a1d4d2SBarry Smith       } else {
78141a1d4d2SBarry Smith         ts->Arhs = A;
78241a1d4d2SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
78341a1d4d2SBarry Smith       }
7843565c898SBarry Smith     } else {
7853565c898SBarry Smith       PetscBool flg;
7863565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
7873565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
7883565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs){
7893565c898SBarry Smith         ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr);
7903565c898SBarry Smith         ts->Arhs = A;
7913565c898SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
7923565c898SBarry Smith       }
793214bc6a2SJed Brown     }
794214bc6a2SJed Brown     *Arhs = ts->Arhs;
795214bc6a2SJed Brown   }
796214bc6a2SJed Brown   if (Brhs) {
797214bc6a2SJed Brown     if (!ts->Brhs) {
798bdb70873SJed Brown       if (A != B) {
79941a1d4d2SBarry Smith         if (ijacobian) {
800214bc6a2SJed Brown           ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
801bdb70873SJed Brown         } else {
80241a1d4d2SBarry Smith           ts->Brhs = B;
80341a1d4d2SBarry Smith           ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
80441a1d4d2SBarry Smith         }
80541a1d4d2SBarry Smith       } else {
806bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
80751699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
808bdb70873SJed Brown       }
809214bc6a2SJed Brown     }
810214bc6a2SJed Brown     *Brhs = ts->Brhs;
811214bc6a2SJed Brown   }
812214bc6a2SJed Brown   PetscFunctionReturn(0);
813214bc6a2SJed Brown }
814214bc6a2SJed Brown 
815316643e7SJed Brown /*@
8160910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
817316643e7SJed Brown 
818316643e7SJed Brown    Collective on TS and Vec
819316643e7SJed Brown 
820316643e7SJed Brown    Input Parameters:
821316643e7SJed Brown +  ts - the TS context
822316643e7SJed Brown .  t - current time
8230910c330SBarry Smith .  U - state vector
8240910c330SBarry Smith .  Udot - time derivative of state vector
825214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
826316643e7SJed Brown 
827316643e7SJed Brown    Output Parameter:
828316643e7SJed Brown .  Y - right hand side
829316643e7SJed Brown 
830316643e7SJed Brown    Note:
831316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
832316643e7SJed Brown    is used internally within the nonlinear solvers.
833316643e7SJed Brown 
834316643e7SJed Brown    If the user did did not write their equations in implicit form, this
835316643e7SJed Brown    function recasts them in implicit form.
836316643e7SJed Brown 
837316643e7SJed Brown    Level: developer
838316643e7SJed Brown 
839316643e7SJed Brown .keywords: TS, compute
840316643e7SJed Brown 
841316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
842316643e7SJed Brown @*/
8430910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
844316643e7SJed Brown {
845316643e7SJed Brown   PetscErrorCode ierr;
84624989b8cSPeter Brune   TSIFunction    ifunction;
84724989b8cSPeter Brune   TSRHSFunction  rhsfunction;
84824989b8cSPeter Brune   void           *ctx;
84924989b8cSPeter Brune   DM             dm;
850316643e7SJed Brown 
851316643e7SJed Brown   PetscFunctionBegin;
8520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8530910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8540910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8550700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
856316643e7SJed Brown 
85724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
85824989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8590298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
86024989b8cSPeter Brune 
861ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
862d90be118SSean Farley 
8630910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
86424989b8cSPeter Brune   if (ifunction) {
865316643e7SJed Brown     PetscStackPush("TS user implicit function");
8660910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
867316643e7SJed Brown     PetscStackPop;
868214bc6a2SJed Brown   }
869214bc6a2SJed Brown   if (imex) {
87024989b8cSPeter Brune     if (!ifunction) {
8710910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8722dd45cf8SJed Brown     }
87324989b8cSPeter Brune   } else if (rhsfunction) {
87424989b8cSPeter Brune     if (ifunction) {
875214bc6a2SJed Brown       Vec Frhs;
876214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
8770910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
878214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
8792dd45cf8SJed Brown     } else {
8800910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
8810910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
882316643e7SJed Brown     }
8834a6899ffSJed Brown   }
8840910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
885316643e7SJed Brown   PetscFunctionReturn(0);
886316643e7SJed Brown }
887316643e7SJed Brown 
888316643e7SJed Brown /*@
889316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
890316643e7SJed Brown 
891316643e7SJed Brown    Collective on TS and Vec
892316643e7SJed Brown 
893316643e7SJed Brown    Input
894316643e7SJed Brown       Input Parameters:
895316643e7SJed Brown +  ts - the TS context
896316643e7SJed Brown .  t - current timestep
8970910c330SBarry Smith .  U - state vector
8980910c330SBarry Smith .  Udot - time derivative of state vector
899214bc6a2SJed Brown .  shift - shift to apply, see note below
900214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
901316643e7SJed Brown 
902316643e7SJed Brown    Output Parameters:
903316643e7SJed Brown +  A - Jacobian matrix
9043565c898SBarry Smith -  B - matrix from which the preconditioner is constructed; often the same as A
905316643e7SJed Brown 
906316643e7SJed Brown    Notes:
9070910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
908316643e7SJed Brown 
9090910c330SBarry Smith    dF/dU + shift*dF/dUdot
910316643e7SJed Brown 
911316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
912316643e7SJed Brown    is used internally within the nonlinear solvers.
913316643e7SJed Brown 
914316643e7SJed Brown    Level: developer
915316643e7SJed Brown 
916316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
917316643e7SJed Brown 
918316643e7SJed Brown .seealso:  TSSetIJacobian()
91963495f91SJed Brown @*/
920d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
921316643e7SJed Brown {
922316643e7SJed Brown   PetscErrorCode ierr;
92324989b8cSPeter Brune   TSIJacobian    ijacobian;
92424989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
92524989b8cSPeter Brune   DM             dm;
92624989b8cSPeter Brune   void           *ctx;
927316643e7SJed Brown 
928316643e7SJed Brown   PetscFunctionBegin;
9290700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9300910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
9310910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
932316643e7SJed Brown   PetscValidPointer(A,6);
93394ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
934316643e7SJed Brown   PetscValidPointer(B,7);
93594ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
93624989b8cSPeter Brune 
93724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
93824989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9390298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
94024989b8cSPeter Brune 
941ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
942316643e7SJed Brown 
94394ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
94424989b8cSPeter Brune   if (ijacobian) {
9456cd88445SBarry Smith     PetscBool missing;
946316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
947d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
948316643e7SJed Brown     PetscStackPop;
9496cd88445SBarry Smith     ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
9506cd88445SBarry 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");
9513565c898SBarry Smith     if (B != A) {
9526cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
9536cd88445SBarry 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");
9546cd88445SBarry Smith     }
9554a6899ffSJed Brown   }
956214bc6a2SJed Brown   if (imex) {
957b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9584c26be97Sstefano_zampini       PetscBool assembled;
959*971015bcSStefano Zampini       if (rhsjacobian) {
960*971015bcSStefano Zampini         Mat Arhs = NULL;
961*971015bcSStefano Zampini         ierr = TSGetRHSMats_Private(ts,&Arhs,NULL);CHKERRQ(ierr);
962*971015bcSStefano Zampini         if (A == Arhs) {
963*971015bcSStefano Zampini           if (rhsjacobian == TSComputeRHSJacobianConstant) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Unsupported operation! cannot use TSComputeRHSJacobianConstant");
964*971015bcSStefano Zampini           ts->rhsjacobian.time = PETSC_MIN_REAL;
965*971015bcSStefano Zampini         }
966*971015bcSStefano Zampini       }
96794ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
9684c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
9694c26be97Sstefano_zampini       if (!assembled) {
9704c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9714c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9724c26be97Sstefano_zampini       }
97394ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
97494ab13aaSBarry Smith       if (A != B) {
97594ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
9764c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
9774c26be97Sstefano_zampini         if (!assembled) {
9784c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9794c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9804c26be97Sstefano_zampini         }
98194ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
982214bc6a2SJed Brown       }
983214bc6a2SJed Brown     }
984214bc6a2SJed Brown   } else {
985e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
986e1244c69SJed Brown     if (rhsjacobian) {
987214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
988d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
989e1244c69SJed Brown     }
99094ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
9913565c898SBarry Smith       PetscBool flg;
992e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
993e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
9943565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
9953565c898SBarry Smith       /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
9963565c898SBarry Smith       if (!flg) {
99794ab13aaSBarry Smith         ierr = MatScale(A,-1);CHKERRQ(ierr);
99894ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
9993565c898SBarry Smith       }
100094ab13aaSBarry Smith       if (A != B) {
100194ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
100294ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
1003316643e7SJed Brown       }
1004e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
1005e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1006e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
100794ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
100894ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
100994ab13aaSBarry Smith         if (A != B) {
101094ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
101194ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1012214bc6a2SJed Brown         }
1013316643e7SJed Brown       }
101494ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
101594ab13aaSBarry Smith       if (A != B) {
101694ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
1017316643e7SJed Brown       }
1018316643e7SJed Brown     }
1019316643e7SJed Brown   }
102094ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
1021316643e7SJed Brown   PetscFunctionReturn(0);
1022316643e7SJed Brown }
1023316643e7SJed Brown 
1024d763cef2SBarry Smith /*@C
1025d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
1026b5abc632SBarry Smith     where U_t = G(t,u).
1027d763cef2SBarry Smith 
10283f9fe445SBarry Smith     Logically Collective on TS
1029d763cef2SBarry Smith 
1030d763cef2SBarry Smith     Input Parameters:
1031d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
10320298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
1033d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
1034d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
10350298fd71SBarry Smith           function evaluation routine (may be NULL)
1036d763cef2SBarry Smith 
1037d763cef2SBarry Smith     Calling sequence of func:
103887828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
1039d763cef2SBarry Smith 
1040d763cef2SBarry Smith +   t - current timestep
1041d763cef2SBarry Smith .   u - input vector
1042d763cef2SBarry Smith .   F - function vector
1043d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1044d763cef2SBarry Smith 
1045d763cef2SBarry Smith     Level: beginner
1046d763cef2SBarry Smith 
104795452b02SPatrick Sanan     Notes:
104895452b02SPatrick Sanan     You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10492bbac0d3SBarry Smith 
1050d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1051d763cef2SBarry Smith 
1052ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1053d763cef2SBarry Smith @*/
1054089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1055d763cef2SBarry Smith {
1056089b2837SJed Brown   PetscErrorCode ierr;
1057089b2837SJed Brown   SNES           snes;
10580298fd71SBarry Smith   Vec            ralloc = NULL;
105924989b8cSPeter Brune   DM             dm;
1060d763cef2SBarry Smith 
1061089b2837SJed Brown   PetscFunctionBegin;
10620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1063ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
106424989b8cSPeter Brune 
106524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
106624989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1067089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1068e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1069e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1070e856ceecSJed Brown     r = ralloc;
1071e856ceecSJed Brown   }
1072089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1073e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1074d763cef2SBarry Smith   PetscFunctionReturn(0);
1075d763cef2SBarry Smith }
1076d763cef2SBarry Smith 
1077ef20d060SBarry Smith /*@C
1078abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1079ef20d060SBarry Smith 
1080ef20d060SBarry Smith     Logically Collective on TS
1081ef20d060SBarry Smith 
1082ef20d060SBarry Smith     Input Parameters:
1083ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1084ef20d060SBarry Smith .   f - routine for evaluating the solution
1085ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
10860298fd71SBarry Smith           function evaluation routine (may be NULL)
1087ef20d060SBarry Smith 
1088ef20d060SBarry Smith     Calling sequence of func:
1089ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
1090ef20d060SBarry Smith 
1091ef20d060SBarry Smith +   t - current timestep
1092ef20d060SBarry Smith .   u - output vector
1093ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1094ef20d060SBarry Smith 
10950ed3bfb6SBarry Smith     Options Database:
10960ed3bfb6SBarry Smith +  -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction()
10970ed3bfb6SBarry Smith -  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
10980ed3bfb6SBarry Smith 
1099abd5a294SJed Brown     Notes:
1100abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1101abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1102abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1103abd5a294SJed Brown 
11044f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1105abd5a294SJed Brown 
1106ef20d060SBarry Smith     Level: beginner
1107ef20d060SBarry Smith 
1108ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1109ef20d060SBarry Smith 
11100ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError()
1111ef20d060SBarry Smith @*/
1112ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1113ef20d060SBarry Smith {
1114ef20d060SBarry Smith   PetscErrorCode ierr;
1115ef20d060SBarry Smith   DM             dm;
1116ef20d060SBarry Smith 
1117ef20d060SBarry Smith   PetscFunctionBegin;
1118ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1119ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1120ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1121ef20d060SBarry Smith   PetscFunctionReturn(0);
1122ef20d060SBarry Smith }
1123ef20d060SBarry Smith 
11249b7cd975SBarry Smith /*@C
11259b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
11269b7cd975SBarry Smith 
11279b7cd975SBarry Smith     Logically Collective on TS
11289b7cd975SBarry Smith 
11299b7cd975SBarry Smith     Input Parameters:
11309b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1131e162b725SBarry Smith .   func - routine for evaluating the forcing function
11329b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
11330298fd71SBarry Smith           function evaluation routine (may be NULL)
11349b7cd975SBarry Smith 
11359b7cd975SBarry Smith     Calling sequence of func:
1136e162b725SBarry Smith $     func (TS ts,PetscReal t,Vec f,void *ctx);
11379b7cd975SBarry Smith 
11389b7cd975SBarry Smith +   t - current timestep
1139e162b725SBarry Smith .   f - output vector
11409b7cd975SBarry Smith -   ctx - [optional] user-defined function context
11419b7cd975SBarry Smith 
11429b7cd975SBarry Smith     Notes:
11439b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1144e162b725SBarry 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
1145e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1146e162b725SBarry Smith 
1147e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1148e162b725SBarry Smith 
1149e162b725SBarry 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
1150e162b725SBarry Smith     parameters can be passed in the ctx variable.
11519b7cd975SBarry Smith 
11529b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
11539b7cd975SBarry Smith 
11549b7cd975SBarry Smith     Level: beginner
11559b7cd975SBarry Smith 
11569b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
11579b7cd975SBarry Smith 
11589b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
11599b7cd975SBarry Smith @*/
1160e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
11619b7cd975SBarry Smith {
11629b7cd975SBarry Smith   PetscErrorCode ierr;
11639b7cd975SBarry Smith   DM             dm;
11649b7cd975SBarry Smith 
11659b7cd975SBarry Smith   PetscFunctionBegin;
11669b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11679b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1168e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
11699b7cd975SBarry Smith   PetscFunctionReturn(0);
11709b7cd975SBarry Smith }
11719b7cd975SBarry Smith 
1172d763cef2SBarry Smith /*@C
1173f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1174b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1175d763cef2SBarry Smith 
11763f9fe445SBarry Smith    Logically Collective on TS
1177d763cef2SBarry Smith 
1178d763cef2SBarry Smith    Input Parameters:
1179d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1180e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1181e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1182d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1183d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
11840298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1185d763cef2SBarry Smith 
1186f7ab8db6SBarry Smith    Calling sequence of f:
1187ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1188d763cef2SBarry Smith 
1189d763cef2SBarry Smith +  t - current timestep
1190d763cef2SBarry Smith .  u - input vector
1191e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1192e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1193d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1194d763cef2SBarry Smith 
11956cd88445SBarry Smith    Notes:
11966cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
11976cd88445SBarry Smith 
11986cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1199ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1200d763cef2SBarry Smith 
1201d763cef2SBarry Smith    Level: beginner
1202d763cef2SBarry Smith 
1203d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1204d763cef2SBarry Smith 
1205ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1206d763cef2SBarry Smith 
1207d763cef2SBarry Smith @*/
1208e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1209d763cef2SBarry Smith {
1210277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1211089b2837SJed Brown   SNES           snes;
121224989b8cSPeter Brune   DM             dm;
121324989b8cSPeter Brune   TSIJacobian    ijacobian;
1214277b19d0SLisandro Dalcin 
1215d763cef2SBarry Smith   PetscFunctionBegin;
12160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1217e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1218e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1219e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1220e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1221d763cef2SBarry Smith 
122224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
122324989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1224e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1225e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1226e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1227e1244c69SJed Brown   }
12280298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1229089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12305f659677SPeter Brune   if (!ijacobian) {
1231e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
12320e4ef248SJed Brown   }
1233e5d3d808SBarry Smith   if (Amat) {
1234e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
12350e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1236e5d3d808SBarry Smith     ts->Arhs = Amat;
12370e4ef248SJed Brown   }
1238e5d3d808SBarry Smith   if (Pmat) {
1239e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
12400e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1241e5d3d808SBarry Smith     ts->Brhs = Pmat;
12420e4ef248SJed Brown   }
1243d763cef2SBarry Smith   PetscFunctionReturn(0);
1244d763cef2SBarry Smith }
1245d763cef2SBarry Smith 
1246316643e7SJed Brown /*@C
1247b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1248316643e7SJed Brown 
12493f9fe445SBarry Smith    Logically Collective on TS
1250316643e7SJed Brown 
1251316643e7SJed Brown    Input Parameters:
1252316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12530298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1254316643e7SJed Brown .  f   - the function evaluation routine
12550298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1256316643e7SJed Brown 
1257316643e7SJed Brown    Calling sequence of f:
1258316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1259316643e7SJed Brown 
1260316643e7SJed Brown +  t   - time at step/stage being solved
1261316643e7SJed Brown .  u   - state vector
1262316643e7SJed Brown .  u_t - time derivative of state vector
1263316643e7SJed Brown .  F   - function vector
1264316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1265316643e7SJed Brown 
1266316643e7SJed Brown    Important:
12672bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1268316643e7SJed Brown 
1269316643e7SJed Brown    Level: beginner
1270316643e7SJed Brown 
1271316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1272316643e7SJed Brown 
1273d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1274316643e7SJed Brown @*/
127551699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1276316643e7SJed Brown {
1277089b2837SJed Brown   PetscErrorCode ierr;
1278089b2837SJed Brown   SNES           snes;
127951699248SLisandro Dalcin   Vec            ralloc = NULL;
128024989b8cSPeter Brune   DM             dm;
1281316643e7SJed Brown 
1282316643e7SJed Brown   PetscFunctionBegin;
12830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
128451699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
128524989b8cSPeter Brune 
128624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
128724989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
128824989b8cSPeter Brune 
1289089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
129051699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
129151699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
129251699248SLisandro Dalcin     r  = ralloc;
1293e856ceecSJed Brown   }
129451699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
129551699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1296089b2837SJed Brown   PetscFunctionReturn(0);
1297089b2837SJed Brown }
1298089b2837SJed Brown 
1299089b2837SJed Brown /*@C
1300089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1301089b2837SJed Brown 
1302089b2837SJed Brown    Not Collective
1303089b2837SJed Brown 
1304089b2837SJed Brown    Input Parameter:
1305089b2837SJed Brown .  ts - the TS context
1306089b2837SJed Brown 
1307089b2837SJed Brown    Output Parameter:
13080298fd71SBarry Smith +  r - vector to hold residual (or NULL)
13090298fd71SBarry Smith .  func - the function to compute residual (or NULL)
13100298fd71SBarry Smith -  ctx - the function context (or NULL)
1311089b2837SJed Brown 
1312089b2837SJed Brown    Level: advanced
1313089b2837SJed Brown 
1314089b2837SJed Brown .keywords: TS, nonlinear, get, function
1315089b2837SJed Brown 
1316089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1317089b2837SJed Brown @*/
1318089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1319089b2837SJed Brown {
1320089b2837SJed Brown   PetscErrorCode ierr;
1321089b2837SJed Brown   SNES           snes;
132224989b8cSPeter Brune   DM             dm;
1323089b2837SJed Brown 
1324089b2837SJed Brown   PetscFunctionBegin;
1325089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1326089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13270298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
132824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
132924989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1330089b2837SJed Brown   PetscFunctionReturn(0);
1331089b2837SJed Brown }
1332089b2837SJed Brown 
1333089b2837SJed Brown /*@C
1334089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1335089b2837SJed Brown 
1336089b2837SJed Brown    Not Collective
1337089b2837SJed Brown 
1338089b2837SJed Brown    Input Parameter:
1339089b2837SJed Brown .  ts - the TS context
1340089b2837SJed Brown 
1341089b2837SJed Brown    Output Parameter:
13420298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13430298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13440298fd71SBarry Smith -  ctx - the function context (or NULL)
1345089b2837SJed Brown 
1346089b2837SJed Brown    Level: advanced
1347089b2837SJed Brown 
1348089b2837SJed Brown .keywords: TS, nonlinear, get, function
1349089b2837SJed Brown 
13502bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1351089b2837SJed Brown @*/
1352089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1353089b2837SJed Brown {
1354089b2837SJed Brown   PetscErrorCode ierr;
1355089b2837SJed Brown   SNES           snes;
135624989b8cSPeter Brune   DM             dm;
1357089b2837SJed Brown 
1358089b2837SJed Brown   PetscFunctionBegin;
1359089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1360089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13610298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
136224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
136324989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1364316643e7SJed Brown   PetscFunctionReturn(0);
1365316643e7SJed Brown }
1366316643e7SJed Brown 
1367316643e7SJed Brown /*@C
1368a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1369ae8867d6SBarry Smith         provided with TSSetIFunction().
1370316643e7SJed Brown 
13713f9fe445SBarry Smith    Logically Collective on TS
1372316643e7SJed Brown 
1373316643e7SJed Brown    Input Parameters:
1374316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1375e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1376e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1377316643e7SJed Brown .  f   - the Jacobian evaluation routine
13780298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1379316643e7SJed Brown 
1380316643e7SJed Brown    Calling sequence of f:
1381ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1382316643e7SJed Brown 
1383316643e7SJed Brown +  t    - time at step/stage being solved
13841b4a444bSJed Brown .  U    - state vector
13851b4a444bSJed Brown .  U_t  - time derivative of state vector
1386316643e7SJed Brown .  a    - shift
1387e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1388e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1389316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1390316643e7SJed Brown 
1391316643e7SJed Brown    Notes:
1392e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1393316643e7SJed Brown 
1394895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1395895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1396895c21f2SBarry Smith 
1397a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1398b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1399a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1400a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1401a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1402a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1403a4f0a591SBarry Smith 
14046cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
14056cd88445SBarry Smith 
14066cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1407ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1408ca5f011dSBarry Smith 
1409316643e7SJed Brown    Level: beginner
1410316643e7SJed Brown 
1411316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1412316643e7SJed Brown 
1413ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1414316643e7SJed Brown 
1415316643e7SJed Brown @*/
1416e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1417316643e7SJed Brown {
1418316643e7SJed Brown   PetscErrorCode ierr;
1419089b2837SJed Brown   SNES           snes;
142024989b8cSPeter Brune   DM             dm;
1421316643e7SJed Brown 
1422316643e7SJed Brown   PetscFunctionBegin;
14230700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1424e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1425e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1426e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1427e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
142824989b8cSPeter Brune 
142924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
143024989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
143124989b8cSPeter Brune 
1432089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1433e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1434316643e7SJed Brown   PetscFunctionReturn(0);
1435316643e7SJed Brown }
1436316643e7SJed Brown 
1437e1244c69SJed Brown /*@
1438e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1439e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1440e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1441e1244c69SJed Brown    not been changed by the TS.
1442e1244c69SJed Brown 
1443e1244c69SJed Brown    Logically Collective
1444e1244c69SJed Brown 
1445e1244c69SJed Brown    Input Arguments:
1446e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1447e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1448e1244c69SJed Brown 
1449e1244c69SJed Brown    Level: intermediate
1450e1244c69SJed Brown 
1451e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1452e1244c69SJed Brown @*/
1453e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1454e1244c69SJed Brown {
1455e1244c69SJed Brown   PetscFunctionBegin;
1456e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1457e1244c69SJed Brown   PetscFunctionReturn(0);
1458e1244c69SJed Brown }
1459e1244c69SJed Brown 
1460efe9872eSLisandro Dalcin /*@C
1461efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1462efe9872eSLisandro Dalcin 
1463efe9872eSLisandro Dalcin    Logically Collective on TS
1464efe9872eSLisandro Dalcin 
1465efe9872eSLisandro Dalcin    Input Parameters:
1466efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1467efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1468efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1469efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1470efe9872eSLisandro Dalcin 
1471efe9872eSLisandro Dalcin    Calling sequence of fun:
1472efe9872eSLisandro Dalcin $  fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1473efe9872eSLisandro Dalcin 
1474efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1475efe9872eSLisandro Dalcin .  U    - state vector
1476efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1477efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1478efe9872eSLisandro Dalcin .  F    - function vector
1479efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1480efe9872eSLisandro Dalcin 
1481efe9872eSLisandro Dalcin    Level: beginner
1482efe9872eSLisandro Dalcin 
1483efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function
1484efe9872eSLisandro Dalcin 
1485efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian()
1486efe9872eSLisandro Dalcin @*/
1487efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1488efe9872eSLisandro Dalcin {
1489efe9872eSLisandro Dalcin   DM             dm;
1490efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1491efe9872eSLisandro Dalcin 
1492efe9872eSLisandro Dalcin   PetscFunctionBegin;
1493efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1494efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1495efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1496efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1497efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1498efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1499efe9872eSLisandro Dalcin }
1500efe9872eSLisandro Dalcin 
1501efe9872eSLisandro Dalcin /*@C
1502efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1503efe9872eSLisandro Dalcin 
1504efe9872eSLisandro Dalcin   Not Collective
1505efe9872eSLisandro Dalcin 
1506efe9872eSLisandro Dalcin   Input Parameter:
1507efe9872eSLisandro Dalcin . ts - the TS context
1508efe9872eSLisandro Dalcin 
1509efe9872eSLisandro Dalcin   Output Parameter:
1510efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1511efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1512efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1513efe9872eSLisandro Dalcin 
1514efe9872eSLisandro Dalcin   Level: advanced
1515efe9872eSLisandro Dalcin 
1516efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function
1517efe9872eSLisandro Dalcin 
1518efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction()
1519efe9872eSLisandro Dalcin @*/
1520efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1521efe9872eSLisandro Dalcin {
1522efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1523efe9872eSLisandro Dalcin   SNES           snes;
1524efe9872eSLisandro Dalcin   DM             dm;
1525efe9872eSLisandro Dalcin 
1526efe9872eSLisandro Dalcin   PetscFunctionBegin;
1527efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1528efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1529efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1530efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1531efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1532efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1533efe9872eSLisandro Dalcin }
1534efe9872eSLisandro Dalcin 
1535efe9872eSLisandro Dalcin /*@C
1536bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1537efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1538efe9872eSLisandro Dalcin 
1539efe9872eSLisandro Dalcin    Logically Collective on TS
1540efe9872eSLisandro Dalcin 
1541efe9872eSLisandro Dalcin    Input Parameters:
1542efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1543efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1544efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1545efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1546efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1547efe9872eSLisandro Dalcin 
1548efe9872eSLisandro Dalcin    Calling sequence of jac:
1549bc77d74cSLisandro Dalcin $  jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx);
1550efe9872eSLisandro Dalcin 
1551efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1552efe9872eSLisandro Dalcin .  U    - state vector
1553efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1554efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1555efe9872eSLisandro Dalcin .  v    - shift for U_t
1556efe9872eSLisandro Dalcin .  a    - shift for U_tt
1557efe9872eSLisandro 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
1558efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1559efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1560efe9872eSLisandro Dalcin 
1561efe9872eSLisandro Dalcin    Notes:
1562efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1563efe9872eSLisandro Dalcin 
1564efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1565efe9872eSLisandro 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.
1566efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1567bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1568efe9872eSLisandro Dalcin 
1569efe9872eSLisandro Dalcin    Level: beginner
1570efe9872eSLisandro Dalcin 
1571efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian
1572efe9872eSLisandro Dalcin 
1573efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1574efe9872eSLisandro Dalcin @*/
1575efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1576efe9872eSLisandro Dalcin {
1577efe9872eSLisandro Dalcin   DM             dm;
1578efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1579efe9872eSLisandro Dalcin 
1580efe9872eSLisandro Dalcin   PetscFunctionBegin;
1581efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1582efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1583efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1584efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1585efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1586efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1587efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1588efe9872eSLisandro Dalcin }
1589efe9872eSLisandro Dalcin 
1590efe9872eSLisandro Dalcin /*@C
1591efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1592efe9872eSLisandro Dalcin 
1593efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1594efe9872eSLisandro Dalcin 
1595efe9872eSLisandro Dalcin   Input Parameter:
1596efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1597efe9872eSLisandro Dalcin 
1598efe9872eSLisandro Dalcin   Output Parameters:
1599efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1600efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1601efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1602efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1603efe9872eSLisandro Dalcin 
160495452b02SPatrick Sanan   Notes:
160595452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
1606efe9872eSLisandro Dalcin 
1607efe9872eSLisandro Dalcin   Level: advanced
1608efe9872eSLisandro Dalcin 
160980275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
1610efe9872eSLisandro Dalcin 
1611efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian
1612efe9872eSLisandro Dalcin @*/
1613efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1614efe9872eSLisandro Dalcin {
1615efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1616efe9872eSLisandro Dalcin   SNES           snes;
1617efe9872eSLisandro Dalcin   DM             dm;
1618efe9872eSLisandro Dalcin 
1619efe9872eSLisandro Dalcin   PetscFunctionBegin;
1620efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1621efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1622efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1623efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1624efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1625efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1626efe9872eSLisandro Dalcin }
1627efe9872eSLisandro Dalcin 
1628efe9872eSLisandro Dalcin /*@
1629efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1630efe9872eSLisandro Dalcin 
1631efe9872eSLisandro Dalcin   Collective on TS and Vec
1632efe9872eSLisandro Dalcin 
1633efe9872eSLisandro Dalcin   Input Parameters:
1634efe9872eSLisandro Dalcin + ts - the TS context
1635efe9872eSLisandro Dalcin . t - current time
1636efe9872eSLisandro Dalcin . U - state vector
1637efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1638efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1639efe9872eSLisandro Dalcin 
1640efe9872eSLisandro Dalcin   Output Parameter:
1641efe9872eSLisandro Dalcin . F - the residual vector
1642efe9872eSLisandro Dalcin 
1643efe9872eSLisandro Dalcin   Note:
1644efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1645efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1646efe9872eSLisandro Dalcin 
1647efe9872eSLisandro Dalcin   Level: developer
1648efe9872eSLisandro Dalcin 
1649efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector
1650efe9872eSLisandro Dalcin 
1651efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1652efe9872eSLisandro Dalcin @*/
1653efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1654efe9872eSLisandro Dalcin {
1655efe9872eSLisandro Dalcin   DM             dm;
1656efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1657efe9872eSLisandro Dalcin   void           *ctx;
1658efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1659efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1660efe9872eSLisandro Dalcin 
1661efe9872eSLisandro Dalcin   PetscFunctionBegin;
1662efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1663efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1664efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1665efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1666efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1667efe9872eSLisandro Dalcin 
1668efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1669efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1670efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1671efe9872eSLisandro Dalcin 
1672efe9872eSLisandro Dalcin   if (!I2Function) {
1673efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1674efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1675efe9872eSLisandro Dalcin   }
1676efe9872eSLisandro Dalcin 
1677efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1678efe9872eSLisandro Dalcin 
1679efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1680efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1681efe9872eSLisandro Dalcin   PetscStackPop;
1682efe9872eSLisandro Dalcin 
1683efe9872eSLisandro Dalcin   if (rhsfunction) {
1684efe9872eSLisandro Dalcin     Vec Frhs;
1685efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1686efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1687efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1688efe9872eSLisandro Dalcin   }
1689efe9872eSLisandro Dalcin 
1690efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1691efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1692efe9872eSLisandro Dalcin }
1693efe9872eSLisandro Dalcin 
1694efe9872eSLisandro Dalcin /*@
1695efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1696efe9872eSLisandro Dalcin 
1697efe9872eSLisandro Dalcin   Collective on TS and Vec
1698efe9872eSLisandro Dalcin 
1699efe9872eSLisandro Dalcin   Input Parameters:
1700efe9872eSLisandro Dalcin + ts - the TS context
1701efe9872eSLisandro Dalcin . t - current timestep
1702efe9872eSLisandro Dalcin . U - state vector
1703efe9872eSLisandro Dalcin . V - time derivative of state vector
1704efe9872eSLisandro Dalcin . A - second time derivative of state vector
1705efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1706efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1707efe9872eSLisandro Dalcin 
1708efe9872eSLisandro Dalcin   Output Parameters:
1709efe9872eSLisandro Dalcin + J - Jacobian matrix
1710efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1711efe9872eSLisandro Dalcin 
1712efe9872eSLisandro Dalcin   Notes:
1713efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1714efe9872eSLisandro Dalcin 
1715efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1716efe9872eSLisandro Dalcin 
1717efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1718efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1719efe9872eSLisandro Dalcin 
1720efe9872eSLisandro Dalcin   Level: developer
1721efe9872eSLisandro Dalcin 
1722efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix
1723efe9872eSLisandro Dalcin 
1724efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1725efe9872eSLisandro Dalcin @*/
1726efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1727efe9872eSLisandro Dalcin {
1728efe9872eSLisandro Dalcin   DM             dm;
1729efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1730efe9872eSLisandro Dalcin   void           *ctx;
1731efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1732efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1733efe9872eSLisandro Dalcin 
1734efe9872eSLisandro Dalcin   PetscFunctionBegin;
1735efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1736efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1737efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1738efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1739efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1740efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1741efe9872eSLisandro Dalcin 
1742efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1743efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1744efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1745efe9872eSLisandro Dalcin 
1746efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1747efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1748efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1749efe9872eSLisandro Dalcin   }
1750efe9872eSLisandro Dalcin 
1751efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1752efe9872eSLisandro Dalcin 
1753efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1754efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1755efe9872eSLisandro Dalcin   PetscStackPop;
1756efe9872eSLisandro Dalcin 
1757efe9872eSLisandro Dalcin   if (rhsjacobian) {
1758efe9872eSLisandro Dalcin     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1759efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1760efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1761efe9872eSLisandro Dalcin     ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr);
1762efe9872eSLisandro Dalcin     if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);}
1763efe9872eSLisandro Dalcin   }
1764efe9872eSLisandro Dalcin 
1765efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1766efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1767efe9872eSLisandro Dalcin }
1768efe9872eSLisandro Dalcin 
1769efe9872eSLisandro Dalcin /*@
1770efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1771efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1772efe9872eSLisandro Dalcin 
1773efe9872eSLisandro Dalcin    Logically Collective on TS and Vec
1774efe9872eSLisandro Dalcin 
1775efe9872eSLisandro Dalcin    Input Parameters:
1776efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1777efe9872eSLisandro Dalcin .  u - the solution vector
1778efe9872eSLisandro Dalcin -  v - the time derivative vector
1779efe9872eSLisandro Dalcin 
1780efe9872eSLisandro Dalcin    Level: beginner
1781efe9872eSLisandro Dalcin 
1782efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions
1783efe9872eSLisandro Dalcin @*/
1784efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1785efe9872eSLisandro Dalcin {
1786efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1787efe9872eSLisandro Dalcin 
1788efe9872eSLisandro Dalcin   PetscFunctionBegin;
1789efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1790efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1791efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1792efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1793efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1794efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1795efe9872eSLisandro Dalcin   ts->vec_dot = v;
1796efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1797efe9872eSLisandro Dalcin }
1798efe9872eSLisandro Dalcin 
1799efe9872eSLisandro Dalcin /*@
1800efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1801efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1802efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1803efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1804efe9872eSLisandro Dalcin 
1805efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1806efe9872eSLisandro Dalcin 
1807efe9872eSLisandro Dalcin    Input Parameter:
1808efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1809efe9872eSLisandro Dalcin 
1810efe9872eSLisandro Dalcin    Output Parameter:
1811efe9872eSLisandro Dalcin +  u - the vector containing the solution
1812efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1813efe9872eSLisandro Dalcin 
1814efe9872eSLisandro Dalcin    Level: intermediate
1815efe9872eSLisandro Dalcin 
1816efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1817efe9872eSLisandro Dalcin 
1818efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution
1819efe9872eSLisandro Dalcin @*/
1820efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1821efe9872eSLisandro Dalcin {
1822efe9872eSLisandro Dalcin   PetscFunctionBegin;
1823efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1824efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1825efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1826efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1827efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1828efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1829efe9872eSLisandro Dalcin }
1830efe9872eSLisandro Dalcin 
183155849f57SBarry Smith /*@C
183255849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
183355849f57SBarry Smith 
183455849f57SBarry Smith   Collective on PetscViewer
183555849f57SBarry Smith 
183655849f57SBarry Smith   Input Parameters:
183755849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
183855849f57SBarry Smith            some related function before a call to TSLoad().
183955849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
184055849f57SBarry Smith 
184155849f57SBarry Smith    Level: intermediate
184255849f57SBarry Smith 
184355849f57SBarry Smith   Notes:
184455849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
184555849f57SBarry Smith 
184655849f57SBarry Smith   Notes for advanced users:
184755849f57SBarry Smith   Most users should not need to know the details of the binary storage
184855849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
184955849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
185055849f57SBarry Smith   format is
185155849f57SBarry Smith .vb
185255849f57SBarry Smith      has not yet been determined
185355849f57SBarry Smith .ve
185455849f57SBarry Smith 
185555849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
185655849f57SBarry Smith @*/
1857f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
185855849f57SBarry Smith {
185955849f57SBarry Smith   PetscErrorCode ierr;
186055849f57SBarry Smith   PetscBool      isbinary;
186155849f57SBarry Smith   PetscInt       classid;
186255849f57SBarry Smith   char           type[256];
18632d53ad75SBarry Smith   DMTS           sdm;
1864ad6bc421SBarry Smith   DM             dm;
186555849f57SBarry Smith 
186655849f57SBarry Smith   PetscFunctionBegin;
1867f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
186855849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
186955849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
187055849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
187155849f57SBarry Smith 
1872060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1873ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1874060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1875f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1876f2c2a1b9SBarry Smith   if (ts->ops->load) {
1877f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1878f2c2a1b9SBarry Smith   }
1879ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1880ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1881ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1882f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1883f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
18842d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
18852d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
188655849f57SBarry Smith   PetscFunctionReturn(0);
188755849f57SBarry Smith }
188855849f57SBarry Smith 
18899804daf3SBarry Smith #include <petscdraw.h>
1890e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1891e04113cfSBarry Smith #include <petscviewersaws.h>
1892f05ece33SBarry Smith #endif
18937e2c5f70SBarry Smith /*@C
1894d763cef2SBarry Smith     TSView - Prints the TS data structure.
1895d763cef2SBarry Smith 
18964c49b128SBarry Smith     Collective on TS
1897d763cef2SBarry Smith 
1898d763cef2SBarry Smith     Input Parameters:
1899d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1900d763cef2SBarry Smith -   viewer - visualization context
1901d763cef2SBarry Smith 
1902d763cef2SBarry Smith     Options Database Key:
1903d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1904d763cef2SBarry Smith 
1905d763cef2SBarry Smith     Notes:
1906d763cef2SBarry Smith     The available visualization contexts include
1907b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1908b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1909d763cef2SBarry Smith          output where only the first processor opens
1910d763cef2SBarry Smith          the file.  All other processors send their
1911d763cef2SBarry Smith          data to the first processor to print.
1912d763cef2SBarry Smith 
1913d763cef2SBarry Smith     The user can open an alternative visualization context with
1914b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1915d763cef2SBarry Smith 
1916d763cef2SBarry Smith     Level: beginner
1917d763cef2SBarry Smith 
1918d763cef2SBarry Smith .keywords: TS, timestep, view
1919d763cef2SBarry Smith 
1920b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1921d763cef2SBarry Smith @*/
19227087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1923d763cef2SBarry Smith {
1924dfbe8321SBarry Smith   PetscErrorCode ierr;
192519fd82e9SBarry Smith   TSType         type;
19262b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
19272d53ad75SBarry Smith   DMTS           sdm;
1928e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1929536b137fSBarry Smith   PetscBool      issaws;
1930f05ece33SBarry Smith #endif
1931d763cef2SBarry Smith 
1932d763cef2SBarry Smith   PetscFunctionBegin;
19330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19343050cee2SBarry Smith   if (!viewer) {
1935ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
19363050cee2SBarry Smith   }
19370700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1938c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1939fd16b177SBarry Smith 
1940251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1941251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
194255849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
19432b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1944e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1945536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1946f05ece33SBarry Smith #endif
194732077d6dSBarry Smith   if (iascii) {
1948dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
1949efd4aadfSBarry Smith     if (ts->ops->view) {
1950efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1951efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1952efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1953efd4aadfSBarry Smith     }
1954ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
195577431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1956ef85077eSLisandro Dalcin     }
1957ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
19587c8652ddSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1959ef85077eSLisandro Dalcin     }
1960efd4aadfSBarry Smith     if (ts->usessnes) {
1961efd4aadfSBarry Smith       PetscBool lin;
1962d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
19635ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1964d763cef2SBarry Smith       }
19655ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
19661ef27442SStefano Zampini       ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
1967efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
1968efd4aadfSBarry Smith     }
1969193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
1970a0af407cSBarry Smith     if (ts->vrtol) {
1971a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
1972a0af407cSBarry Smith     } else {
1973a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
1974a0af407cSBarry Smith     }
1975a0af407cSBarry Smith     if (ts->vatol) {
1976a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
1977a0af407cSBarry Smith     } else {
1978a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
1979a0af407cSBarry Smith     }
1980825ab935SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1981efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
1982825ab935SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1983825ab935SBarry Smith     if (ts->snes && ts->usessnes)  {
1984825ab935SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1985825ab935SBarry Smith       ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
1986825ab935SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1987825ab935SBarry Smith     }
19882d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19892d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
19900f5bd95cSBarry Smith   } else if (isstring) {
1991a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1992b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
199355849f57SBarry Smith   } else if (isbinary) {
199455849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
199555849f57SBarry Smith     MPI_Comm    comm;
199655849f57SBarry Smith     PetscMPIInt rank;
199755849f57SBarry Smith     char        type[256];
199855849f57SBarry Smith 
199955849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
200055849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
200155849f57SBarry Smith     if (!rank) {
200255849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
200355849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
200455849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
200555849f57SBarry Smith     }
200655849f57SBarry Smith     if (ts->ops->view) {
200755849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
200855849f57SBarry Smith     }
2009efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2010f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
2011f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
20122d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
20132d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
20142b0a91c0SBarry Smith   } else if (isdraw) {
20152b0a91c0SBarry Smith     PetscDraw draw;
20162b0a91c0SBarry Smith     char      str[36];
201789fd9fafSBarry Smith     PetscReal x,y,bottom,h;
20182b0a91c0SBarry Smith 
20192b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
20202b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
20212b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
20222b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
202351fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
202489fd9fafSBarry Smith     bottom = y - h;
20252b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
20262b0a91c0SBarry Smith     if (ts->ops->view) {
20272b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
20282b0a91c0SBarry Smith     }
2029efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2030efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
20312b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2032e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2033536b137fSBarry Smith   } else if (issaws) {
2034d45a07a7SBarry Smith     PetscMPIInt rank;
20352657e9d9SBarry Smith     const char  *name;
20362657e9d9SBarry Smith 
20372657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
2038d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
2039d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
2040d45a07a7SBarry Smith       char       dir[1024];
2041d45a07a7SBarry Smith 
2042e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2043a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
20442657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
20452657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
20462657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2047d763cef2SBarry Smith     }
20480acecf5bSBarry Smith     if (ts->ops->view) {
20490acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
20500acecf5bSBarry Smith     }
2051f05ece33SBarry Smith #endif
2052f05ece33SBarry Smith   }
2053f05ece33SBarry Smith 
2054b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2055251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2056b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2057d763cef2SBarry Smith   PetscFunctionReturn(0);
2058d763cef2SBarry Smith }
2059d763cef2SBarry Smith 
2060b07ff414SBarry Smith /*@
2061d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2062d763cef2SBarry Smith    the timesteppers.
2063d763cef2SBarry Smith 
20643f9fe445SBarry Smith    Logically Collective on TS
2065d763cef2SBarry Smith 
2066d763cef2SBarry Smith    Input Parameters:
2067d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2068d763cef2SBarry Smith -  usrP - optional user context
2069d763cef2SBarry Smith 
207095452b02SPatrick Sanan    Fortran Notes:
207195452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2072daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2073daf670e6SBarry Smith 
2074d763cef2SBarry Smith    Level: intermediate
2075d763cef2SBarry Smith 
2076d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
2077d763cef2SBarry Smith 
2078d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2079d763cef2SBarry Smith @*/
20807087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2081d763cef2SBarry Smith {
2082d763cef2SBarry Smith   PetscFunctionBegin;
20830700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2084d763cef2SBarry Smith   ts->user = usrP;
2085d763cef2SBarry Smith   PetscFunctionReturn(0);
2086d763cef2SBarry Smith }
2087d763cef2SBarry Smith 
2088b07ff414SBarry Smith /*@
2089d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2090d763cef2SBarry Smith     timestepper.
2091d763cef2SBarry Smith 
2092d763cef2SBarry Smith     Not Collective
2093d763cef2SBarry Smith 
2094d763cef2SBarry Smith     Input Parameter:
2095d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2096d763cef2SBarry Smith 
2097d763cef2SBarry Smith     Output Parameter:
2098d763cef2SBarry Smith .   usrP - user context
2099d763cef2SBarry Smith 
210095452b02SPatrick Sanan    Fortran Notes:
210195452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2102daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2103daf670e6SBarry Smith 
2104d763cef2SBarry Smith     Level: intermediate
2105d763cef2SBarry Smith 
2106d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
2107d763cef2SBarry Smith 
2108d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2109d763cef2SBarry Smith @*/
2110e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2111d763cef2SBarry Smith {
2112d763cef2SBarry Smith   PetscFunctionBegin;
21130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2114e71120c6SJed Brown   *(void**)usrP = ts->user;
2115d763cef2SBarry Smith   PetscFunctionReturn(0);
2116d763cef2SBarry Smith }
2117d763cef2SBarry Smith 
2118d763cef2SBarry Smith /*@
211980275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2120d763cef2SBarry Smith 
2121d763cef2SBarry Smith    Not Collective
2122d763cef2SBarry Smith 
2123d763cef2SBarry Smith    Input Parameter:
2124d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2125d763cef2SBarry Smith 
2126d763cef2SBarry Smith    Output Parameter:
212780275a0aSLisandro Dalcin .  steps - number of steps completed so far
2128d763cef2SBarry Smith 
2129d763cef2SBarry Smith    Level: intermediate
2130d763cef2SBarry Smith 
2131d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
21329be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2133d763cef2SBarry Smith @*/
213480275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2135d763cef2SBarry Smith {
2136d763cef2SBarry Smith   PetscFunctionBegin;
21370700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
213880275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
213980275a0aSLisandro Dalcin   *steps = ts->steps;
214080275a0aSLisandro Dalcin   PetscFunctionReturn(0);
214180275a0aSLisandro Dalcin }
214280275a0aSLisandro Dalcin 
214380275a0aSLisandro Dalcin /*@
214480275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
214580275a0aSLisandro Dalcin 
214680275a0aSLisandro Dalcin    Logically Collective on TS
214780275a0aSLisandro Dalcin 
214880275a0aSLisandro Dalcin    Input Parameters:
214980275a0aSLisandro Dalcin +  ts - the TS context
215080275a0aSLisandro Dalcin -  steps - number of steps completed so far
215180275a0aSLisandro Dalcin 
215280275a0aSLisandro Dalcin    Notes:
215380275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
215480275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
215580275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
215680275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
215780275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
215880275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
215980275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
216080275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
216180275a0aSLisandro Dalcin 
216280275a0aSLisandro Dalcin    Level: advanced
216380275a0aSLisandro Dalcin 
216480275a0aSLisandro Dalcin .keywords: TS, timestep, set, iteration, number
216580275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
216680275a0aSLisandro Dalcin @*/
216780275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
216880275a0aSLisandro Dalcin {
216980275a0aSLisandro Dalcin   PetscFunctionBegin;
217080275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
217180275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
217280275a0aSLisandro Dalcin   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
217380275a0aSLisandro Dalcin   ts->steps = steps;
2174d763cef2SBarry Smith   PetscFunctionReturn(0);
2175d763cef2SBarry Smith }
2176d763cef2SBarry Smith 
2177d763cef2SBarry Smith /*@
2178d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2179d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2180d763cef2SBarry Smith 
21813f9fe445SBarry Smith    Logically Collective on TS
2182d763cef2SBarry Smith 
2183d763cef2SBarry Smith    Input Parameters:
2184d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2185d763cef2SBarry Smith -  time_step - the size of the timestep
2186d763cef2SBarry Smith 
2187d763cef2SBarry Smith    Level: intermediate
2188d763cef2SBarry Smith 
2189aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2190d763cef2SBarry Smith 
2191d763cef2SBarry Smith .keywords: TS, set, timestep
2192d763cef2SBarry Smith @*/
21937087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2194d763cef2SBarry Smith {
2195d763cef2SBarry Smith   PetscFunctionBegin;
21960700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2197c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2198d763cef2SBarry Smith   ts->time_step = time_step;
2199d763cef2SBarry Smith   PetscFunctionReturn(0);
2200d763cef2SBarry Smith }
2201d763cef2SBarry Smith 
2202a43b19c4SJed Brown /*@
220349354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
220449354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
220549354f04SShri Abhyankar      or just return at the final time TS computed.
2206a43b19c4SJed Brown 
2207a43b19c4SJed Brown   Logically Collective on TS
2208a43b19c4SJed Brown 
2209a43b19c4SJed Brown    Input Parameter:
2210a43b19c4SJed Brown +   ts - the time-step context
221149354f04SShri Abhyankar -   eftopt - exact final time option
2212a43b19c4SJed Brown 
2213feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2214feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2215feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2216feed9e9dSBarry Smith 
2217feed9e9dSBarry Smith    Options Database:
2218feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2219feed9e9dSBarry Smith 
2220ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2221ee346746SBarry Smith     then the final time you selected.
2222ee346746SBarry Smith 
2223a43b19c4SJed Brown    Level: beginner
2224a43b19c4SJed Brown 
2225f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2226a43b19c4SJed Brown @*/
222749354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2228a43b19c4SJed Brown {
2229a43b19c4SJed Brown   PetscFunctionBegin;
2230a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
223149354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
223249354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2233a43b19c4SJed Brown   PetscFunctionReturn(0);
2234a43b19c4SJed Brown }
2235a43b19c4SJed Brown 
2236d763cef2SBarry Smith /*@
2237f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2238f6953c82SLisandro Dalcin 
2239f6953c82SLisandro Dalcin    Not Collective
2240f6953c82SLisandro Dalcin 
2241f6953c82SLisandro Dalcin    Input Parameter:
2242f6953c82SLisandro Dalcin .  ts - the TS context
2243f6953c82SLisandro Dalcin 
2244f6953c82SLisandro Dalcin    Output Parameter:
2245f6953c82SLisandro Dalcin .  eftopt - exact final time option
2246f6953c82SLisandro Dalcin 
2247f6953c82SLisandro Dalcin    Level: beginner
2248f6953c82SLisandro Dalcin 
2249f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2250f6953c82SLisandro Dalcin @*/
2251f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2252f6953c82SLisandro Dalcin {
2253f6953c82SLisandro Dalcin   PetscFunctionBegin;
2254f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2255f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2256f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2257f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2258f6953c82SLisandro Dalcin }
2259f6953c82SLisandro Dalcin 
2260f6953c82SLisandro Dalcin /*@
2261d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2262d763cef2SBarry Smith 
2263d763cef2SBarry Smith    Not Collective
2264d763cef2SBarry Smith 
2265d763cef2SBarry Smith    Input Parameter:
2266d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2267d763cef2SBarry Smith 
2268d763cef2SBarry Smith    Output Parameter:
2269d763cef2SBarry Smith .  dt - the current timestep size
2270d763cef2SBarry Smith 
2271d763cef2SBarry Smith    Level: intermediate
2272d763cef2SBarry Smith 
2273aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2274d763cef2SBarry Smith 
2275d763cef2SBarry Smith .keywords: TS, get, timestep
2276d763cef2SBarry Smith @*/
22777087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2278d763cef2SBarry Smith {
2279d763cef2SBarry Smith   PetscFunctionBegin;
22800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2281f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2282d763cef2SBarry Smith   *dt = ts->time_step;
2283d763cef2SBarry Smith   PetscFunctionReturn(0);
2284d763cef2SBarry Smith }
2285d763cef2SBarry Smith 
2286d8e5e3e6SSatish Balay /*@
2287d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2288d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2289d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2290d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2291d763cef2SBarry Smith 
2292d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2293d763cef2SBarry Smith 
2294d763cef2SBarry Smith    Input Parameter:
2295d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2296d763cef2SBarry Smith 
2297d763cef2SBarry Smith    Output Parameter:
2298d763cef2SBarry Smith .  v - the vector containing the solution
2299d763cef2SBarry Smith 
230063e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
230163e21af5SBarry Smith    final time. It returns the solution at the next timestep.
230263e21af5SBarry Smith 
2303d763cef2SBarry Smith    Level: intermediate
2304d763cef2SBarry Smith 
23050ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2306d763cef2SBarry Smith 
2307d763cef2SBarry Smith .keywords: TS, timestep, get, solution
2308d763cef2SBarry Smith @*/
23097087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2310d763cef2SBarry Smith {
2311d763cef2SBarry Smith   PetscFunctionBegin;
23120700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23134482741eSBarry Smith   PetscValidPointer(v,2);
23148737fe31SLisandro Dalcin   *v = ts->vec_sol;
2315d763cef2SBarry Smith   PetscFunctionReturn(0);
2316d763cef2SBarry Smith }
2317d763cef2SBarry Smith 
231803fe5f5eSDebojyoti Ghosh /*@
2319b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
232003fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2321b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
232203fe5f5eSDebojyoti Ghosh    structure as the solution vector.
232303fe5f5eSDebojyoti Ghosh 
232403fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
232503fe5f5eSDebojyoti Ghosh 
232603fe5f5eSDebojyoti Ghosh    Parameters :
232703fe5f5eSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2328b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2329b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
233003fe5f5eSDebojyoti Ghosh        returned in v.
2331b2bf4f3aSDebojyoti Ghosh .  v - the vector containing the n-th solution component
233203fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2333b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
233403fe5f5eSDebojyoti Ghosh 
23354cdd57e5SDebojyoti Ghosh    Level: advanced
233603fe5f5eSDebojyoti Ghosh 
233703fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
233803fe5f5eSDebojyoti Ghosh 
233903fe5f5eSDebojyoti Ghosh .keywords: TS, timestep, get, solution
234003fe5f5eSDebojyoti Ghosh @*/
2341b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
234203fe5f5eSDebojyoti Ghosh {
234303fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
234403fe5f5eSDebojyoti Ghosh 
234503fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
234603fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2347b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
234803fe5f5eSDebojyoti Ghosh   else {
2349b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
235003fe5f5eSDebojyoti Ghosh   }
235103fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
235203fe5f5eSDebojyoti Ghosh }
235303fe5f5eSDebojyoti Ghosh 
23544cdd57e5SDebojyoti Ghosh /*@
23554cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
23564cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
23574cdd57e5SDebojyoti Ghosh 
23584cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
23594cdd57e5SDebojyoti Ghosh 
23604cdd57e5SDebojyoti Ghosh    Parameters :
23614cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
23624cdd57e5SDebojyoti Ghosh .  v - the vector containing the auxiliary solution
23634cdd57e5SDebojyoti Ghosh 
23644cdd57e5SDebojyoti Ghosh    Level: intermediate
23654cdd57e5SDebojyoti Ghosh 
23664cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
23674cdd57e5SDebojyoti Ghosh 
23684cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, solution
23694cdd57e5SDebojyoti Ghosh @*/
23704cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
23714cdd57e5SDebojyoti Ghosh {
23724cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
23734cdd57e5SDebojyoti Ghosh 
23744cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
23754cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2376f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
23774cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2378f6356ec7SDebojyoti Ghosh   } else {
2379f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v); CHKERRQ(ierr);
2380f6356ec7SDebojyoti Ghosh   }
23814cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
23824cdd57e5SDebojyoti Ghosh }
23834cdd57e5SDebojyoti Ghosh 
23844cdd57e5SDebojyoti Ghosh /*@
23854cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
23864cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
23874cdd57e5SDebojyoti Ghosh 
23884cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
23894cdd57e5SDebojyoti Ghosh 
23909657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
23919657682dSDebojyoti Ghosh 
23924cdd57e5SDebojyoti Ghosh    Parameters :
23934cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2394657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
23954cdd57e5SDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
23964cdd57e5SDebojyoti Ghosh 
23974cdd57e5SDebojyoti Ghosh    Level: intermediate
23984cdd57e5SDebojyoti Ghosh 
239957df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
24004cdd57e5SDebojyoti Ghosh 
24014cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, error
24024cdd57e5SDebojyoti Ghosh @*/
24030a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
24044cdd57e5SDebojyoti Ghosh {
24054cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
24064cdd57e5SDebojyoti Ghosh 
24074cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
24084cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2409f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
24100a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2411f6356ec7SDebojyoti Ghosh   } else {
2412f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2413f6356ec7SDebojyoti Ghosh   }
24144cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
24154cdd57e5SDebojyoti Ghosh }
24164cdd57e5SDebojyoti Ghosh 
241757df6a1bSDebojyoti Ghosh /*@
241857df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
241957df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
242057df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
242157df6a1bSDebojyoti Ghosh 
242257df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
242357df6a1bSDebojyoti Ghosh 
242457df6a1bSDebojyoti Ghosh    Parameters :
242557df6a1bSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
242657df6a1bSDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
242757df6a1bSDebojyoti Ghosh 
242857df6a1bSDebojyoti Ghosh    Level: intermediate
242957df6a1bSDebojyoti Ghosh 
243057df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
243157df6a1bSDebojyoti Ghosh 
243257df6a1bSDebojyoti Ghosh .keywords: TS, timestep, get, error
243357df6a1bSDebojyoti Ghosh @*/
243457df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
243557df6a1bSDebojyoti Ghosh {
243657df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
243757df6a1bSDebojyoti Ghosh 
243857df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
243957df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24409657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
244157df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
244257df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
244357df6a1bSDebojyoti Ghosh   }
244457df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
244557df6a1bSDebojyoti Ghosh }
244657df6a1bSDebojyoti Ghosh 
2447bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2448d8e5e3e6SSatish Balay /*@
2449bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2450d763cef2SBarry Smith 
2451bdad233fSMatthew Knepley   Not collective
2452d763cef2SBarry Smith 
2453bdad233fSMatthew Knepley   Input Parameters:
2454bdad233fSMatthew Knepley + ts   - The TS
2455bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2456d763cef2SBarry Smith .vb
24570910c330SBarry Smith          U_t - A U = 0      (linear)
24580910c330SBarry Smith          U_t - A(t) U = 0   (linear)
24590910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2460d763cef2SBarry Smith .ve
2461d763cef2SBarry Smith 
2462d763cef2SBarry Smith    Level: beginner
2463d763cef2SBarry Smith 
2464bdad233fSMatthew Knepley .keywords: TS, problem type
2465bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2466d763cef2SBarry Smith @*/
24677087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2468a7cc72afSBarry Smith {
24699e2a6581SJed Brown   PetscErrorCode ierr;
24709e2a6581SJed Brown 
2471d763cef2SBarry Smith   PetscFunctionBegin;
24720700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2473bdad233fSMatthew Knepley   ts->problem_type = type;
24749e2a6581SJed Brown   if (type == TS_LINEAR) {
24759e2a6581SJed Brown     SNES snes;
24769e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
24779e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
24789e2a6581SJed Brown   }
2479d763cef2SBarry Smith   PetscFunctionReturn(0);
2480d763cef2SBarry Smith }
2481d763cef2SBarry Smith 
2482bdad233fSMatthew Knepley /*@C
2483bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2484bdad233fSMatthew Knepley 
2485bdad233fSMatthew Knepley   Not collective
2486bdad233fSMatthew Knepley 
2487bdad233fSMatthew Knepley   Input Parameter:
2488bdad233fSMatthew Knepley . ts   - The TS
2489bdad233fSMatthew Knepley 
2490bdad233fSMatthew Knepley   Output Parameter:
2491bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2492bdad233fSMatthew Knepley .vb
2493089b2837SJed Brown          M U_t = A U
2494089b2837SJed Brown          M(t) U_t = A(t) U
2495b5abc632SBarry Smith          F(t,U,U_t)
2496bdad233fSMatthew Knepley .ve
2497bdad233fSMatthew Knepley 
2498bdad233fSMatthew Knepley    Level: beginner
2499bdad233fSMatthew Knepley 
2500bdad233fSMatthew Knepley .keywords: TS, problem type
2501bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2502bdad233fSMatthew Knepley @*/
25037087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2504a7cc72afSBarry Smith {
2505bdad233fSMatthew Knepley   PetscFunctionBegin;
25060700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
25074482741eSBarry Smith   PetscValidIntPointer(type,2);
2508bdad233fSMatthew Knepley   *type = ts->problem_type;
2509bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2510bdad233fSMatthew Knepley }
2511d763cef2SBarry Smith 
2512d763cef2SBarry Smith /*@
2513d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
2514d763cef2SBarry Smith    of a timestepper.
2515d763cef2SBarry Smith 
2516d763cef2SBarry Smith    Collective on TS
2517d763cef2SBarry Smith 
2518d763cef2SBarry Smith    Input Parameter:
2519d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2520d763cef2SBarry Smith 
2521d763cef2SBarry Smith    Notes:
2522d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2523d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2524141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2525d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2526141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2527d763cef2SBarry Smith 
2528d763cef2SBarry Smith    Level: advanced
2529d763cef2SBarry Smith 
2530d763cef2SBarry Smith .keywords: TS, timestep, setup
2531d763cef2SBarry Smith 
2532141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2533d763cef2SBarry Smith @*/
25347087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2535d763cef2SBarry Smith {
2536dfbe8321SBarry Smith   PetscErrorCode ierr;
25376c6b9e74SPeter Brune   DM             dm;
25386c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2539d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2540cd11d68dSLisandro Dalcin   TSIFunction    ifun;
25416c6b9e74SPeter Brune   TSIJacobian    ijac;
2542efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
25436c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
25442ffb9264SLisandro Dalcin   PetscBool      isnone;
2545d763cef2SBarry Smith 
2546d763cef2SBarry Smith   PetscFunctionBegin;
25470700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2548277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2549277b19d0SLisandro Dalcin 
25507adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2551cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2552cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2553d763cef2SBarry Smith   }
2554277b19d0SLisandro Dalcin 
2555277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
2556277b19d0SLisandro Dalcin 
2557*971015bcSStefano Zampini   ierr = TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL);CHKERRQ(ierr);
2558*971015bcSStefano Zampini   if (ts->rhsjacobian.reuse && rhsjac == TSComputeRHSJacobianConstant) {
2559e1244c69SJed Brown     Mat Amat,Pmat;
2560e1244c69SJed Brown     SNES snes;
2561e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2562e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2563e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2564e1244c69SJed Brown      * have displaced the RHS matrix */
2565*971015bcSStefano Zampini     if (Amat && Amat == ts->Arhs) {
2566abc0d4abSBarry 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 */
2567abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2568e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2569e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2570e1244c69SJed Brown     }
2571*971015bcSStefano Zampini     if (Pmat && Pmat == ts->Brhs) {
2572abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2573e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2574e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2575e1244c69SJed Brown     }
2576e1244c69SJed Brown   }
25772ffb9264SLisandro Dalcin 
25782ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
25792ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
25802ffb9264SLisandro Dalcin 
2581277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2582000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2583277b19d0SLisandro Dalcin   }
2584277b19d0SLisandro Dalcin 
25852ffb9264SLisandro Dalcin   /* Attempt to check/preset a default value for the exact final time option */
25862ffb9264SLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
25872ffb9264SLisandro Dalcin   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED)
25882ffb9264SLisandro Dalcin     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
25892ffb9264SLisandro Dalcin 
2590a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
25916c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
25926c6b9e74SPeter Brune    */
25936c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
25940298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
25956c6b9e74SPeter Brune   if (!func) {
25966c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
25976c6b9e74SPeter Brune   }
2598a6772fa2SLisandro 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.
25996c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
26006c6b9e74SPeter Brune    */
26010298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
26020298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2603efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
26040298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2605efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
26066c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
26076c6b9e74SPeter Brune   }
2608c0517034SDebojyoti Ghosh 
2609c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2610c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2611c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2612c0517034SDebojyoti Ghosh   }
2613c0517034SDebojyoti Ghosh 
2614277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2615277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2616277b19d0SLisandro Dalcin }
2617277b19d0SLisandro Dalcin 
2618f6a906c0SBarry Smith /*@
2619277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2620277b19d0SLisandro Dalcin 
2621277b19d0SLisandro Dalcin    Collective on TS
2622277b19d0SLisandro Dalcin 
2623277b19d0SLisandro Dalcin    Input Parameter:
2624277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2625277b19d0SLisandro Dalcin 
2626277b19d0SLisandro Dalcin    Level: beginner
2627277b19d0SLisandro Dalcin 
2628277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
2629277b19d0SLisandro Dalcin 
2630277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2631277b19d0SLisandro Dalcin @*/
2632277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2633277b19d0SLisandro Dalcin {
26341d06f6b3SHong Zhang   TS_RHSSplitLink ilink = ts->tsrhssplit,next;
2635277b19d0SLisandro Dalcin   PetscErrorCode  ierr;
2636277b19d0SLisandro Dalcin 
2637277b19d0SLisandro Dalcin   PetscFunctionBegin;
2638277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2639b18ea86cSHong Zhang 
2640277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2641277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2642277b19d0SLisandro Dalcin   }
2643277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2644e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2645bbd56ea5SKarl Rupp 
26464e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
26474e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2648214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
26496bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2650efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2651e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2652e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
265338637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2654bbd56ea5SKarl Rupp 
2655d8151eeaSBarry Smith   ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2656d8151eeaSBarry Smith   ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2657715f1b00SHong Zhang 
2658ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
26592c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
266036eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
266113b1b0ffSHong Zhang   ierr = MatDestroy(&ts->mat_sensip);CHKERRQ(ierr);
2662022c081aSHong Zhang 
26631d06f6b3SHong Zhang   while (ilink) {
26641d06f6b3SHong Zhang     next = ilink->next;
26651d06f6b3SHong Zhang     ierr = TSDestroy(&ilink->ts);CHKERRQ(ierr);
26661d06f6b3SHong Zhang     ierr = PetscFree(ilink->splitname);CHKERRQ(ierr);
26671d06f6b3SHong Zhang     ierr = ISDestroy(&ilink->is);CHKERRQ(ierr);
26681d06f6b3SHong Zhang     ierr = PetscFree(ilink);CHKERRQ(ierr);
26691d06f6b3SHong Zhang     ilink = next;
267087f4e208SHong Zhang   }
2671545aaa6fSHong Zhang   ts->num_rhs_splits = 0;
2672277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2673d763cef2SBarry Smith   PetscFunctionReturn(0);
2674d763cef2SBarry Smith }
2675d763cef2SBarry Smith 
2676d8e5e3e6SSatish Balay /*@
2677d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2678d763cef2SBarry Smith    with TSCreate().
2679d763cef2SBarry Smith 
2680d763cef2SBarry Smith    Collective on TS
2681d763cef2SBarry Smith 
2682d763cef2SBarry Smith    Input Parameter:
2683d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2684d763cef2SBarry Smith 
2685d763cef2SBarry Smith    Level: beginner
2686d763cef2SBarry Smith 
2687d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2688d763cef2SBarry Smith 
2689d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2690d763cef2SBarry Smith @*/
26916bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2692d763cef2SBarry Smith {
26936849ba73SBarry Smith   PetscErrorCode ierr;
2694d763cef2SBarry Smith 
2695d763cef2SBarry Smith   PetscFunctionBegin;
26966bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
26976bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
26986bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2699d763cef2SBarry Smith 
27006bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2701277b19d0SLisandro Dalcin 
2702e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2703e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
27046bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
27056d4c513bSLisandro Dalcin 
2706bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2707bc952696SBarry Smith 
270884df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
27096427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
27106427ac75SLisandro Dalcin 
27116bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
27126bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
27136bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
27140dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
27156d4c513bSLisandro Dalcin 
2716a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2717d763cef2SBarry Smith   PetscFunctionReturn(0);
2718d763cef2SBarry Smith }
2719d763cef2SBarry Smith 
2720d8e5e3e6SSatish Balay /*@
2721d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2722d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2723d763cef2SBarry Smith 
2724d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2725d763cef2SBarry Smith 
2726d763cef2SBarry Smith    Input Parameter:
2727d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2728d763cef2SBarry Smith 
2729d763cef2SBarry Smith    Output Parameter:
2730d763cef2SBarry Smith .  snes - the nonlinear solver context
2731d763cef2SBarry Smith 
2732d763cef2SBarry Smith    Notes:
2733d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2734d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
273594b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2736d763cef2SBarry Smith 
2737d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
27380298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2739d763cef2SBarry Smith 
2740d763cef2SBarry Smith    Level: beginner
2741d763cef2SBarry Smith 
2742d763cef2SBarry Smith .keywords: timestep, get, SNES
2743d763cef2SBarry Smith @*/
27447087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2745d763cef2SBarry Smith {
2746d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2747d372ba47SLisandro Dalcin 
2748d763cef2SBarry Smith   PetscFunctionBegin;
27490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27504482741eSBarry Smith   PetscValidPointer(snes,2);
2751d372ba47SLisandro Dalcin   if (!ts->snes) {
2752ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
275316413a6aSBarry Smith     ierr = PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options);CHKERRQ(ierr);
27540298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27553bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2756d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2757496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
27589e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
27599e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
27609e2a6581SJed Brown     }
2761d372ba47SLisandro Dalcin   }
2762d763cef2SBarry Smith   *snes = ts->snes;
2763d763cef2SBarry Smith   PetscFunctionReturn(0);
2764d763cef2SBarry Smith }
2765d763cef2SBarry Smith 
2766deb2cd25SJed Brown /*@
2767deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2768deb2cd25SJed Brown 
2769deb2cd25SJed Brown    Collective
2770deb2cd25SJed Brown 
2771deb2cd25SJed Brown    Input Parameter:
2772deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2773deb2cd25SJed Brown -  snes - the nonlinear solver context
2774deb2cd25SJed Brown 
2775deb2cd25SJed Brown    Notes:
2776deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2777deb2cd25SJed Brown 
2778deb2cd25SJed Brown    Level: developer
2779deb2cd25SJed Brown 
2780deb2cd25SJed Brown .keywords: timestep, set, SNES
2781deb2cd25SJed Brown @*/
2782deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2783deb2cd25SJed Brown {
2784deb2cd25SJed Brown   PetscErrorCode ierr;
2785d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2786deb2cd25SJed Brown 
2787deb2cd25SJed Brown   PetscFunctionBegin;
2788deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2789deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2790deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2791deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2792bbd56ea5SKarl Rupp 
2793deb2cd25SJed Brown   ts->snes = snes;
2794bbd56ea5SKarl Rupp 
27950298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27960298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2797740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
27980298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2799740132f1SEmil Constantinescu   }
2800deb2cd25SJed Brown   PetscFunctionReturn(0);
2801deb2cd25SJed Brown }
2802deb2cd25SJed Brown 
2803d8e5e3e6SSatish Balay /*@
280494b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2805d763cef2SBarry Smith    a TS (timestepper) context.
2806d763cef2SBarry Smith 
280794b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2808d763cef2SBarry Smith 
2809d763cef2SBarry Smith    Input Parameter:
2810d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2811d763cef2SBarry Smith 
2812d763cef2SBarry Smith    Output Parameter:
281394b7f48cSBarry Smith .  ksp - the nonlinear solver context
2814d763cef2SBarry Smith 
2815d763cef2SBarry Smith    Notes:
281694b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2817d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2818d763cef2SBarry Smith    KSP and PC contexts as well.
2819d763cef2SBarry Smith 
282094b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
28210298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2822d763cef2SBarry Smith 
2823d763cef2SBarry Smith    Level: beginner
2824d763cef2SBarry Smith 
282594b7f48cSBarry Smith .keywords: timestep, get, KSP
2826d763cef2SBarry Smith @*/
28277087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2828d763cef2SBarry Smith {
2829d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2830089b2837SJed Brown   SNES           snes;
2831d372ba47SLisandro Dalcin 
2832d763cef2SBarry Smith   PetscFunctionBegin;
28330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28344482741eSBarry Smith   PetscValidPointer(ksp,2);
283517186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2836e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2837089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2838089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2839d763cef2SBarry Smith   PetscFunctionReturn(0);
2840d763cef2SBarry Smith }
2841d763cef2SBarry Smith 
2842d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2843d763cef2SBarry Smith 
2844adb62b0dSMatthew Knepley /*@
2845618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
2846618ce8baSLisandro Dalcin 
2847618ce8baSLisandro Dalcin    Logically Collective on TS
2848618ce8baSLisandro Dalcin 
2849618ce8baSLisandro Dalcin    Input Parameters:
2850618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2851618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
2852618ce8baSLisandro Dalcin 
2853618ce8baSLisandro Dalcin    Options Database Keys:
2854618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
2855618ce8baSLisandro Dalcin 
2856618ce8baSLisandro Dalcin    Notes:
2857618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
2858618ce8baSLisandro Dalcin 
2859618ce8baSLisandro Dalcin    Level: intermediate
2860618ce8baSLisandro Dalcin 
2861618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, steps
2862618ce8baSLisandro Dalcin 
2863618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
2864618ce8baSLisandro Dalcin @*/
2865618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
2866618ce8baSLisandro Dalcin {
2867618ce8baSLisandro Dalcin   PetscFunctionBegin;
2868618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2869618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2870618ce8baSLisandro Dalcin   if (maxsteps < 0 ) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
2871618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
2872618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2873618ce8baSLisandro Dalcin }
2874618ce8baSLisandro Dalcin 
2875618ce8baSLisandro Dalcin /*@
2876618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
2877618ce8baSLisandro Dalcin 
2878618ce8baSLisandro Dalcin    Not Collective
2879618ce8baSLisandro Dalcin 
2880618ce8baSLisandro Dalcin    Input Parameters:
2881618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2882618ce8baSLisandro Dalcin 
2883618ce8baSLisandro Dalcin    Output Parameter:
2884618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
2885618ce8baSLisandro Dalcin 
2886618ce8baSLisandro Dalcin    Level: advanced
2887618ce8baSLisandro Dalcin 
2888618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, steps
2889618ce8baSLisandro Dalcin 
2890618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
2891618ce8baSLisandro Dalcin @*/
2892618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
2893618ce8baSLisandro Dalcin {
2894618ce8baSLisandro Dalcin   PetscFunctionBegin;
2895618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2896618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
2897618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
2898618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2899618ce8baSLisandro Dalcin }
2900618ce8baSLisandro Dalcin 
2901618ce8baSLisandro Dalcin /*@
2902618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
2903618ce8baSLisandro Dalcin 
2904618ce8baSLisandro Dalcin    Logically Collective on TS
2905618ce8baSLisandro Dalcin 
2906618ce8baSLisandro Dalcin    Input Parameters:
2907618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2908618ce8baSLisandro Dalcin -  maxtime - final time to step to
2909618ce8baSLisandro Dalcin 
2910618ce8baSLisandro Dalcin    Options Database Keys:
2911ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
2912618ce8baSLisandro Dalcin 
2913618ce8baSLisandro Dalcin    Notes:
2914618ce8baSLisandro Dalcin    The default maximum time is 5.0
2915618ce8baSLisandro Dalcin 
2916618ce8baSLisandro Dalcin    Level: intermediate
2917618ce8baSLisandro Dalcin 
2918618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, time
2919618ce8baSLisandro Dalcin 
2920618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
2921618ce8baSLisandro Dalcin @*/
2922618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
2923618ce8baSLisandro Dalcin {
2924618ce8baSLisandro Dalcin   PetscFunctionBegin;
2925618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2926618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
2927618ce8baSLisandro Dalcin   ts->max_time = maxtime;
2928618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2929618ce8baSLisandro Dalcin }
2930618ce8baSLisandro Dalcin 
2931618ce8baSLisandro Dalcin /*@
2932618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
2933618ce8baSLisandro Dalcin 
2934618ce8baSLisandro Dalcin    Not Collective
2935618ce8baSLisandro Dalcin 
2936618ce8baSLisandro Dalcin    Input Parameters:
2937618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2938618ce8baSLisandro Dalcin 
2939618ce8baSLisandro Dalcin    Output Parameter:
2940618ce8baSLisandro Dalcin .  maxtime - final time to step to
2941618ce8baSLisandro Dalcin 
2942618ce8baSLisandro Dalcin    Level: advanced
2943618ce8baSLisandro Dalcin 
2944618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, time
2945618ce8baSLisandro Dalcin 
2946618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
2947618ce8baSLisandro Dalcin @*/
2948618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
2949618ce8baSLisandro Dalcin {
2950618ce8baSLisandro Dalcin   PetscFunctionBegin;
2951618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2952618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
2953618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
2954618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2955618ce8baSLisandro Dalcin }
2956618ce8baSLisandro Dalcin 
2957618ce8baSLisandro Dalcin /*@
2958aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
2959edc382c3SSatish Balay 
2960edc382c3SSatish Balay    Level: deprecated
2961edc382c3SSatish Balay 
2962aaa6c58dSLisandro Dalcin @*/
2963aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
2964aaa6c58dSLisandro Dalcin {
2965aaa6c58dSLisandro Dalcin   PetscErrorCode ierr;
2966aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
2967aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2968aaa6c58dSLisandro Dalcin   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
2969aaa6c58dSLisandro Dalcin   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
2970aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
2971aaa6c58dSLisandro Dalcin }
2972aaa6c58dSLisandro Dalcin 
2973aaa6c58dSLisandro Dalcin /*@
297419eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
2975edc382c3SSatish Balay 
2976edc382c3SSatish Balay    Level: deprecated
2977edc382c3SSatish Balay 
2978adb62b0dSMatthew Knepley @*/
29797087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2980adb62b0dSMatthew Knepley {
2981adb62b0dSMatthew Knepley   PetscFunctionBegin;
29820700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2983abc0a331SBarry Smith   if (maxsteps) {
29844482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2985adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2986adb62b0dSMatthew Knepley   }
2987abc0a331SBarry Smith   if (maxtime) {
29884482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2989adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2990adb62b0dSMatthew Knepley   }
2991adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2992adb62b0dSMatthew Knepley }
2993adb62b0dSMatthew Knepley 
2994d763cef2SBarry Smith /*@
299519eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
2996edc382c3SSatish Balay 
2997edc382c3SSatish Balay    Level: deprecated
2998edc382c3SSatish Balay 
2999d763cef2SBarry Smith @*/
30007087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
3001d763cef2SBarry Smith {
3002d763cef2SBarry Smith   PetscFunctionBegin;
30030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3004c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3005c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
300639b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
300739b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
3008d763cef2SBarry Smith   PetscFunctionReturn(0);
3009d763cef2SBarry Smith }
3010d763cef2SBarry Smith 
3011d763cef2SBarry Smith /*@
30125c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
3013edc382c3SSatish Balay 
3014edc382c3SSatish Balay    Level: deprecated
3015edc382c3SSatish Balay 
30165c5f5948SLisandro Dalcin @*/
3017e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
30185c5f5948SLisandro Dalcin 
301919eac22cSLisandro Dalcin /*@
30204f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
3021edc382c3SSatish Balay 
3022edc382c3SSatish Balay    Level: deprecated
3023edc382c3SSatish Balay 
30244f4e0956SLisandro Dalcin @*/
30254f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
30264f4e0956SLisandro Dalcin 
30274f4e0956SLisandro Dalcin /*@
3028d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
3029d763cef2SBarry Smith    for use by the TS routines.
3030d763cef2SBarry Smith 
30313f9fe445SBarry Smith    Logically Collective on TS and Vec
3032d763cef2SBarry Smith 
3033d763cef2SBarry Smith    Input Parameters:
3034d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
30350910c330SBarry Smith -  u - the solution vector
3036d763cef2SBarry Smith 
3037d763cef2SBarry Smith    Level: beginner
3038d763cef2SBarry Smith 
3039715f1b00SHong Zhang .keywords: TS, timestep, set, solution, initial values
30400ed3bfb6SBarry Smith 
30410ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3042d763cef2SBarry Smith @*/
30430910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3044d763cef2SBarry Smith {
30458737fe31SLisandro Dalcin   PetscErrorCode ierr;
3046496e6a7aSJed Brown   DM             dm;
30478737fe31SLisandro Dalcin 
3048d763cef2SBarry Smith   PetscFunctionBegin;
30490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
30500910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
30510910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
30526bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
30530910c330SBarry Smith   ts->vec_sol = u;
3054bbd56ea5SKarl Rupp 
3055496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
30560910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
3057d763cef2SBarry Smith   PetscFunctionReturn(0);
3058d763cef2SBarry Smith }
3059d763cef2SBarry Smith 
3060ac226902SBarry Smith /*@C
3061000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
30623f2090d5SJed Brown   called once at the beginning of each time step.
3063000e7ae3SMatthew Knepley 
30643f9fe445SBarry Smith   Logically Collective on TS
3065000e7ae3SMatthew Knepley 
3066000e7ae3SMatthew Knepley   Input Parameters:
3067000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3068000e7ae3SMatthew Knepley - func - The function
3069000e7ae3SMatthew Knepley 
3070000e7ae3SMatthew Knepley   Calling sequence of func:
3071000e7ae3SMatthew Knepley . func (TS ts);
3072000e7ae3SMatthew Knepley 
3073000e7ae3SMatthew Knepley   Level: intermediate
3074000e7ae3SMatthew Knepley 
3075000e7ae3SMatthew Knepley .keywords: TS, timestep
3076dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3077000e7ae3SMatthew Knepley @*/
30787087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3079000e7ae3SMatthew Knepley {
3080000e7ae3SMatthew Knepley   PetscFunctionBegin;
30810700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3082ae60f76fSBarry Smith   ts->prestep = func;
3083000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3084000e7ae3SMatthew Knepley }
3085000e7ae3SMatthew Knepley 
308609ee8438SJed Brown /*@
30873f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
30883f2090d5SJed Brown 
30893f2090d5SJed Brown   Collective on TS
30903f2090d5SJed Brown 
30913f2090d5SJed Brown   Input Parameters:
30923f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
30933f2090d5SJed Brown 
30943f2090d5SJed Brown   Notes:
30953f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
30963f2090d5SJed Brown   so most users would not generally call this routine themselves.
30973f2090d5SJed Brown 
30983f2090d5SJed Brown   Level: developer
30993f2090d5SJed Brown 
31003f2090d5SJed Brown .keywords: TS, timestep
31019be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
31023f2090d5SJed Brown @*/
31037087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
31043f2090d5SJed Brown {
31053f2090d5SJed Brown   PetscErrorCode ierr;
31063f2090d5SJed Brown 
31073f2090d5SJed Brown   PetscFunctionBegin;
31080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3109ae60f76fSBarry Smith   if (ts->prestep) {
31105efd42a4SStefano Zampini     Vec              U;
31115efd42a4SStefano Zampini     PetscObjectState sprev,spost;
31125efd42a4SStefano Zampini 
31135efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
31145efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3115ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
31165efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3117dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3118312ce896SJed Brown   }
31193f2090d5SJed Brown   PetscFunctionReturn(0);
31203f2090d5SJed Brown }
31213f2090d5SJed Brown 
3122b8123daeSJed Brown /*@C
3123b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3124b8123daeSJed Brown   called once at the beginning of each stage.
3125b8123daeSJed Brown 
3126b8123daeSJed Brown   Logically Collective on TS
3127b8123daeSJed Brown 
3128b8123daeSJed Brown   Input Parameters:
3129b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3130b8123daeSJed Brown - func - The function
3131b8123daeSJed Brown 
3132b8123daeSJed Brown   Calling sequence of func:
3133b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
3134b8123daeSJed Brown 
3135b8123daeSJed Brown   Level: intermediate
3136b8123daeSJed Brown 
3137b8123daeSJed Brown   Note:
3138b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
313980275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3140b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3141b8123daeSJed Brown 
3142b8123daeSJed Brown .keywords: TS, timestep
31439be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3144b8123daeSJed Brown @*/
3145b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3146b8123daeSJed Brown {
3147b8123daeSJed Brown   PetscFunctionBegin;
3148b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3149ae60f76fSBarry Smith   ts->prestage = func;
3150b8123daeSJed Brown   PetscFunctionReturn(0);
3151b8123daeSJed Brown }
3152b8123daeSJed Brown 
31539be3e283SDebojyoti Ghosh /*@C
31549be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
31559be3e283SDebojyoti Ghosh   called once at the end of each stage.
31569be3e283SDebojyoti Ghosh 
31579be3e283SDebojyoti Ghosh   Logically Collective on TS
31589be3e283SDebojyoti Ghosh 
31599be3e283SDebojyoti Ghosh   Input Parameters:
31609be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
31619be3e283SDebojyoti Ghosh - func - The function
31629be3e283SDebojyoti Ghosh 
31639be3e283SDebojyoti Ghosh   Calling sequence of func:
31649be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
31659be3e283SDebojyoti Ghosh 
31669be3e283SDebojyoti Ghosh   Level: intermediate
31679be3e283SDebojyoti Ghosh 
31689be3e283SDebojyoti Ghosh   Note:
31699be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
317080275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
31719be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
31729be3e283SDebojyoti Ghosh 
31739be3e283SDebojyoti Ghosh .keywords: TS, timestep
31749be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
31759be3e283SDebojyoti Ghosh @*/
31769be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
31779be3e283SDebojyoti Ghosh {
31789be3e283SDebojyoti Ghosh   PetscFunctionBegin;
31799be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
31809be3e283SDebojyoti Ghosh   ts->poststage = func;
31819be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
31829be3e283SDebojyoti Ghosh }
31839be3e283SDebojyoti Ghosh 
3184c688d042SShri Abhyankar /*@C
3185c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3186c688d042SShri Abhyankar   called once at the end of each step evaluation.
3187c688d042SShri Abhyankar 
3188c688d042SShri Abhyankar   Logically Collective on TS
3189c688d042SShri Abhyankar 
3190c688d042SShri Abhyankar   Input Parameters:
3191c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3192c688d042SShri Abhyankar - func - The function
3193c688d042SShri Abhyankar 
3194c688d042SShri Abhyankar   Calling sequence of func:
3195c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3196c688d042SShri Abhyankar 
3197c688d042SShri Abhyankar   Level: intermediate
3198c688d042SShri Abhyankar 
3199c688d042SShri Abhyankar   Note:
32001785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
32011785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3202e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3203e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3204e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3205c688d042SShri Abhyankar 
3206c688d042SShri Abhyankar .keywords: TS, timestep
3207c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3208c688d042SShri Abhyankar @*/
3209c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3210c688d042SShri Abhyankar {
3211c688d042SShri Abhyankar   PetscFunctionBegin;
3212c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3213c688d042SShri Abhyankar   ts->postevaluate = func;
3214c688d042SShri Abhyankar   PetscFunctionReturn(0);
3215c688d042SShri Abhyankar }
3216c688d042SShri Abhyankar 
3217b8123daeSJed Brown /*@
3218b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3219b8123daeSJed Brown 
3220b8123daeSJed Brown   Collective on TS
3221b8123daeSJed Brown 
3222b8123daeSJed Brown   Input Parameters:
3223b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
32249be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3225b8123daeSJed Brown 
3226b8123daeSJed Brown   Notes:
3227b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3228b8123daeSJed Brown   most users would not generally call this routine themselves.
3229b8123daeSJed Brown 
3230b8123daeSJed Brown   Level: developer
3231b8123daeSJed Brown 
3232b8123daeSJed Brown .keywords: TS, timestep
32339be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3234b8123daeSJed Brown @*/
3235b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3236b8123daeSJed Brown {
3237b8123daeSJed Brown   PetscErrorCode ierr;
3238b8123daeSJed Brown 
3239b8123daeSJed Brown   PetscFunctionBegin;
3240b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3241ae60f76fSBarry Smith   if (ts->prestage) {
3242ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3243b8123daeSJed Brown   }
3244b8123daeSJed Brown   PetscFunctionReturn(0);
3245b8123daeSJed Brown }
3246b8123daeSJed Brown 
32479be3e283SDebojyoti Ghosh /*@
32489be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
32499be3e283SDebojyoti Ghosh 
32509be3e283SDebojyoti Ghosh   Collective on TS
32519be3e283SDebojyoti Ghosh 
32529be3e283SDebojyoti Ghosh   Input Parameters:
32539be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
32549be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
32559be3e283SDebojyoti Ghosh   stageindex  - Stage number
32569be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
32579be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
32589be3e283SDebojyoti Ghosh 
32599be3e283SDebojyoti Ghosh   Notes:
32609be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
32619be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
32629be3e283SDebojyoti Ghosh 
32639be3e283SDebojyoti Ghosh   Level: developer
32649be3e283SDebojyoti Ghosh 
32659be3e283SDebojyoti Ghosh .keywords: TS, timestep
32669be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
32679be3e283SDebojyoti Ghosh @*/
32689be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
32699be3e283SDebojyoti Ghosh {
32709be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
32719be3e283SDebojyoti Ghosh 
32729be3e283SDebojyoti Ghosh   PetscFunctionBegin;
32739be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
32744beae5d8SLisandro Dalcin   if (ts->poststage) {
32759be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
32769be3e283SDebojyoti Ghosh   }
32779be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
32789be3e283SDebojyoti Ghosh }
32799be3e283SDebojyoti Ghosh 
3280c688d042SShri Abhyankar /*@
3281c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3282c688d042SShri Abhyankar 
3283c688d042SShri Abhyankar   Collective on TS
3284c688d042SShri Abhyankar 
3285c688d042SShri Abhyankar   Input Parameters:
3286c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3287c688d042SShri Abhyankar 
3288c688d042SShri Abhyankar   Notes:
3289c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3290c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3291c688d042SShri Abhyankar 
3292c688d042SShri Abhyankar   Level: developer
3293c688d042SShri Abhyankar 
3294c688d042SShri Abhyankar .keywords: TS, timestep
3295c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3296c688d042SShri Abhyankar @*/
3297c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3298c688d042SShri Abhyankar {
3299c688d042SShri Abhyankar   PetscErrorCode ierr;
3300c688d042SShri Abhyankar 
3301c688d042SShri Abhyankar   PetscFunctionBegin;
3302c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3303c688d042SShri Abhyankar   if (ts->postevaluate) {
3304dcb233daSLisandro Dalcin     Vec              U;
3305dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3306dcb233daSLisandro Dalcin 
3307dcb233daSLisandro Dalcin     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3308dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3309c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3310dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3311dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3312c688d042SShri Abhyankar   }
3313c688d042SShri Abhyankar   PetscFunctionReturn(0);
3314c688d042SShri Abhyankar }
3315c688d042SShri Abhyankar 
3316ac226902SBarry Smith /*@C
3317000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
33183f2090d5SJed Brown   called once at the end of each time step.
3319000e7ae3SMatthew Knepley 
33203f9fe445SBarry Smith   Logically Collective on TS
3321000e7ae3SMatthew Knepley 
3322000e7ae3SMatthew Knepley   Input Parameters:
3323000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3324000e7ae3SMatthew Knepley - func - The function
3325000e7ae3SMatthew Knepley 
3326000e7ae3SMatthew Knepley   Calling sequence of func:
3327b8123daeSJed Brown $ func (TS ts);
3328000e7ae3SMatthew Knepley 
33291785ff2aSShri Abhyankar   Notes:
33301785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
33311785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
33321785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
33331785ff2aSShri Abhyankar 
3334000e7ae3SMatthew Knepley   Level: intermediate
3335000e7ae3SMatthew Knepley 
3336000e7ae3SMatthew Knepley .keywords: TS, timestep
3337dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3338000e7ae3SMatthew Knepley @*/
33397087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3340000e7ae3SMatthew Knepley {
3341000e7ae3SMatthew Knepley   PetscFunctionBegin;
33420700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3343ae60f76fSBarry Smith   ts->poststep = func;
3344000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3345000e7ae3SMatthew Knepley }
3346000e7ae3SMatthew Knepley 
334709ee8438SJed Brown /*@
33483f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
33493f2090d5SJed Brown 
33503f2090d5SJed Brown   Collective on TS
33513f2090d5SJed Brown 
33523f2090d5SJed Brown   Input Parameters:
33533f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
33543f2090d5SJed Brown 
33553f2090d5SJed Brown   Notes:
33563f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
33573f2090d5SJed Brown   so most users would not generally call this routine themselves.
33583f2090d5SJed Brown 
33593f2090d5SJed Brown   Level: developer
33603f2090d5SJed Brown 
33613f2090d5SJed Brown .keywords: TS, timestep
33623f2090d5SJed Brown @*/
33637087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
33643f2090d5SJed Brown {
33653f2090d5SJed Brown   PetscErrorCode ierr;
33663f2090d5SJed Brown 
33673f2090d5SJed Brown   PetscFunctionBegin;
33680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3369ae60f76fSBarry Smith   if (ts->poststep) {
33705efd42a4SStefano Zampini     Vec              U;
33715efd42a4SStefano Zampini     PetscObjectState sprev,spost;
33725efd42a4SStefano Zampini 
33735efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
33745efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3375ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
33765efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3377dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
337872ac3e02SJed Brown   }
33793f2090d5SJed Brown   PetscFunctionReturn(0);
33803f2090d5SJed Brown }
33813f2090d5SJed Brown 
3382d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3383d763cef2SBarry Smith 
3384d763cef2SBarry Smith /*@C
3385a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3386d763cef2SBarry Smith    timestep to display the iteration's  progress.
3387d763cef2SBarry Smith 
33883f9fe445SBarry Smith    Logically Collective on TS
3389d763cef2SBarry Smith 
3390d763cef2SBarry Smith    Input Parameters:
3391d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3392e213d8f1SJed Brown .  monitor - monitoring routine
3393329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
33940298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3395b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
33960298fd71SBarry Smith           (may be NULL)
3397d763cef2SBarry Smith 
3398e213d8f1SJed Brown    Calling sequence of monitor:
3399dcb233daSLisandro Dalcin $    PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3400d763cef2SBarry Smith 
3401d763cef2SBarry Smith +    ts - the TS context
340263e21af5SBarry 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)
34031f06c33eSBarry Smith .    time - current time
34040910c330SBarry Smith .    u - current iterate
3405d763cef2SBarry Smith -    mctx - [optional] monitoring context
3406d763cef2SBarry Smith 
3407d763cef2SBarry Smith    Notes:
3408d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3409d763cef2SBarry Smith    already has been loaded.
3410d763cef2SBarry Smith 
341195452b02SPatrick Sanan    Fortran Notes:
341295452b02SPatrick Sanan     Only a single monitor function can be set for each TS object
3413025f1a04SBarry Smith 
3414d763cef2SBarry Smith    Level: intermediate
3415d763cef2SBarry Smith 
3416d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3417d763cef2SBarry Smith 
3418a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3419d763cef2SBarry Smith @*/
3420c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3421d763cef2SBarry Smith {
342278064530SBarry Smith   PetscErrorCode ierr;
342378064530SBarry Smith   PetscInt       i;
342478064530SBarry Smith   PetscBool      identical;
342578064530SBarry Smith 
3426d763cef2SBarry Smith   PetscFunctionBegin;
34270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
342878064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
342978064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
343078064530SBarry Smith     if (identical) PetscFunctionReturn(0);
343178064530SBarry Smith   }
343217186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3433d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
34348704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3435d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3436d763cef2SBarry Smith   PetscFunctionReturn(0);
3437d763cef2SBarry Smith }
3438d763cef2SBarry Smith 
3439d763cef2SBarry Smith /*@C
3440a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3441d763cef2SBarry Smith 
34423f9fe445SBarry Smith    Logically Collective on TS
3443d763cef2SBarry Smith 
3444d763cef2SBarry Smith    Input Parameters:
3445d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3446d763cef2SBarry Smith 
3447d763cef2SBarry Smith    Notes:
3448d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3449d763cef2SBarry Smith 
3450d763cef2SBarry Smith    Level: intermediate
3451d763cef2SBarry Smith 
3452d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3453d763cef2SBarry Smith 
3454a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3455d763cef2SBarry Smith @*/
34567087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3457d763cef2SBarry Smith {
3458d952e501SBarry Smith   PetscErrorCode ierr;
3459d952e501SBarry Smith   PetscInt       i;
3460d952e501SBarry Smith 
3461d763cef2SBarry Smith   PetscFunctionBegin;
34620700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3463d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
34648704b422SBarry Smith     if (ts->monitordestroy[i]) {
34658704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3466d952e501SBarry Smith     }
3467d952e501SBarry Smith   }
3468d763cef2SBarry Smith   ts->numbermonitors = 0;
3469d763cef2SBarry Smith   PetscFunctionReturn(0);
3470d763cef2SBarry Smith }
3471d763cef2SBarry Smith 
3472721cd6eeSBarry Smith /*@C
3473721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
34745516499fSSatish Balay 
34755516499fSSatish Balay    Level: intermediate
347641251cbbSSatish Balay 
34775516499fSSatish Balay .keywords: TS, set, monitor
34785516499fSSatish Balay 
347963e21af5SBarry Smith .seealso:  TSMonitorSet()
348041251cbbSSatish Balay @*/
3481721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3482d763cef2SBarry Smith {
3483dfbe8321SBarry Smith   PetscErrorCode ierr;
3484721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
348541aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3486d132466eSBarry Smith 
3487d763cef2SBarry Smith   PetscFunctionBegin;
34884d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
348941aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
349041aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3491721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
349241aca3d6SBarry Smith   if (iascii) {
3493649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
349463e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
349563e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
349663e21af5SBarry Smith     } else {
34978392e04aSShri 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);
349863e21af5SBarry Smith     }
3499649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
350041aca3d6SBarry Smith   } else if (ibinary) {
350141aca3d6SBarry Smith     PetscMPIInt rank;
350241aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
350341aca3d6SBarry Smith     if (!rank) {
3504450a797fSBarry Smith       PetscBool skipHeader;
3505450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3506450a797fSBarry Smith 
3507450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3508450a797fSBarry Smith       if (!skipHeader) {
3509450a797fSBarry Smith          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
3510450a797fSBarry Smith        }
351141aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
351241aca3d6SBarry Smith     } else {
351341aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
351441aca3d6SBarry Smith     }
351541aca3d6SBarry Smith   }
3516721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3517d763cef2SBarry Smith   PetscFunctionReturn(0);
3518d763cef2SBarry Smith }
3519d763cef2SBarry Smith 
3520cc9c3a59SBarry Smith /*@C
3521cc9c3a59SBarry Smith    TSMonitorExtreme - Prints the extreme values of the solution at each timestep
3522cc9c3a59SBarry Smith 
3523cc9c3a59SBarry Smith    Level: intermediate
3524cc9c3a59SBarry Smith 
3525cc9c3a59SBarry Smith .keywords: TS, set, monitor
3526cc9c3a59SBarry Smith 
3527cc9c3a59SBarry Smith .seealso:  TSMonitorSet()
3528cc9c3a59SBarry Smith @*/
3529cc9c3a59SBarry Smith PetscErrorCode TSMonitorExtreme(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3530cc9c3a59SBarry Smith {
3531cc9c3a59SBarry Smith   PetscErrorCode ierr;
3532cc9c3a59SBarry Smith   PetscViewer    viewer =  vf->viewer;
3533cc9c3a59SBarry Smith   PetscBool      iascii;
3534cc9c3a59SBarry Smith   PetscReal      max,min;
3535cc9c3a59SBarry Smith 
3536cc9c3a59SBarry Smith 
3537cc9c3a59SBarry Smith   PetscFunctionBegin;
3538cc9c3a59SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3539cc9c3a59SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
3540cc9c3a59SBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3541cc9c3a59SBarry Smith   if (iascii) {
3542cc9c3a59SBarry Smith     ierr = VecMax(v,NULL,&max);CHKERRQ(ierr);
3543cc9c3a59SBarry Smith     ierr = VecMin(v,NULL,&min);CHKERRQ(ierr);
3544cc9c3a59SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
35455132926bSBarry 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);
3546cc9c3a59SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3547cc9c3a59SBarry Smith   }
3548cc9c3a59SBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3549cc9c3a59SBarry Smith   PetscFunctionReturn(0);
3550cc9c3a59SBarry Smith }
3551cc9c3a59SBarry Smith 
3552cd652676SJed Brown /*@
3553cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3554cd652676SJed Brown 
3555cd652676SJed Brown    Collective on TS
3556cd652676SJed Brown 
3557cd652676SJed Brown    Input Argument:
3558cd652676SJed Brown +  ts - time stepping context
3559cd652676SJed Brown -  t - time to interpolate to
3560cd652676SJed Brown 
3561cd652676SJed Brown    Output Argument:
35620910c330SBarry Smith .  U - state at given time
3563cd652676SJed Brown 
3564cd652676SJed Brown    Level: intermediate
3565cd652676SJed Brown 
3566cd652676SJed Brown    Developer Notes:
3567cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3568cd652676SJed Brown 
3569cd652676SJed Brown .keywords: TS, set
3570cd652676SJed Brown 
3571874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3572cd652676SJed Brown @*/
35730910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3574cd652676SJed Brown {
3575cd652676SJed Brown   PetscErrorCode ierr;
3576cd652676SJed Brown 
3577cd652676SJed Brown   PetscFunctionBegin;
3578cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3579b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3580be5899b3SLisandro 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);
3581ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
35820910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3583cd652676SJed Brown   PetscFunctionReturn(0);
3584cd652676SJed Brown }
3585cd652676SJed Brown 
3586d763cef2SBarry Smith /*@
35876d9e5789SSean Farley    TSStep - Steps one time step
3588d763cef2SBarry Smith 
3589d763cef2SBarry Smith    Collective on TS
3590d763cef2SBarry Smith 
3591d763cef2SBarry Smith    Input Parameter:
3592d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3593d763cef2SBarry Smith 
359427829d71SBarry Smith    Level: developer
3595d763cef2SBarry Smith 
3596b8123daeSJed Brown    Notes:
359727829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
359827829d71SBarry Smith 
3599b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3600b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3601b8123daeSJed Brown 
360219eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
360319eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
360425cb2221SBarry Smith 
3605d763cef2SBarry Smith .keywords: TS, timestep, solve
3606d763cef2SBarry Smith 
36079be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3608d763cef2SBarry Smith @*/
3609193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3610d763cef2SBarry Smith {
3611dfbe8321SBarry Smith   PetscErrorCode   ierr;
3612fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3613be5899b3SLisandro Dalcin   PetscReal        ptime;
3614d763cef2SBarry Smith 
3615d763cef2SBarry Smith   PetscFunctionBegin;
36160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3617fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3618fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3619fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3620fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3621fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3622fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3623302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3624fffbeea8SBarry Smith 
3625d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
362668bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3627d405a339SMatthew Knepley 
3628ef85077eSLisandro 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>");
3629a6772fa2SLisandro 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()");
3630be5899b3SLisandro 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");
3631a6772fa2SLisandro Dalcin 
3632be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3633be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
36342808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3635e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3636d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3637193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3638d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3639be5899b3SLisandro Dalcin   ts->ptime_prev = ptime;
36402808aa04SLisandro Dalcin   ts->steps++;
3641be5899b3SLisandro Dalcin   ts->steprollback = PETSC_FALSE;
364228d5b5d6SLisandro Dalcin   ts->steprestart  = PETSC_FALSE;
3643362cd11cSLisandro Dalcin 
3644362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3645cef5090cSJed Brown     if (ts->errorifstepfailed) {
364608c7845fSBarry 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]);
364708c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3648d2daff3dSHong Zhang     }
364908c7845fSBarry Smith   } else if (!ts->reason) {
365008c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
365108c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
365208c7845fSBarry Smith   }
365308c7845fSBarry Smith   PetscFunctionReturn(0);
365408c7845fSBarry Smith }
365508c7845fSBarry Smith 
365608c7845fSBarry Smith /*@
36577cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
36587cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
36597cbde773SLisandro Dalcin 
36607cbde773SLisandro Dalcin    Collective on TS
36617cbde773SLisandro Dalcin 
36627cbde773SLisandro Dalcin    Input Arguments:
36637cbde773SLisandro Dalcin +  ts - time stepping context
36647cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
36657cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
36667cbde773SLisandro Dalcin 
36677cbde773SLisandro Dalcin    Output Arguments:
36687cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
36697cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
36707cbde773SLisandro Dalcin 
36717cbde773SLisandro Dalcin    Level: advanced
36727cbde773SLisandro Dalcin 
36737cbde773SLisandro Dalcin    Notes:
36747cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
36757cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
36767cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
36777cbde773SLisandro Dalcin 
36787cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
36797cbde773SLisandro Dalcin @*/
36807cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
36817cbde773SLisandro Dalcin {
36827cbde773SLisandro Dalcin   PetscErrorCode ierr;
36837cbde773SLisandro Dalcin 
36847cbde773SLisandro Dalcin   PetscFunctionBegin;
36857cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36867cbde773SLisandro Dalcin   PetscValidType(ts,1);
36877cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
36887cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
36897cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
36907cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
36917cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
36927cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
36937cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
36947cbde773SLisandro Dalcin   PetscFunctionReturn(0);
36957cbde773SLisandro Dalcin }
36967cbde773SLisandro Dalcin 
369705175c85SJed Brown /*@
369805175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
369905175c85SJed Brown 
37001c3436cfSJed Brown    Collective on TS
370105175c85SJed Brown 
370205175c85SJed Brown    Input Arguments:
37031c3436cfSJed Brown +  ts - time stepping context
37041c3436cfSJed Brown .  order - desired order of accuracy
37050298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
370605175c85SJed Brown 
370705175c85SJed Brown    Output Arguments:
37080910c330SBarry Smith .  U - state at the end of the current step
370905175c85SJed Brown 
371005175c85SJed Brown    Level: advanced
371105175c85SJed Brown 
3712108c343cSJed Brown    Notes:
3713108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3714108c343cSJed 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.
3715108c343cSJed Brown 
37161c3436cfSJed Brown .seealso: TSStep(), TSAdapt
371705175c85SJed Brown @*/
37180910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
371905175c85SJed Brown {
372005175c85SJed Brown   PetscErrorCode ierr;
372105175c85SJed Brown 
372205175c85SJed Brown   PetscFunctionBegin;
372305175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
372405175c85SJed Brown   PetscValidType(ts,1);
37250910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3726ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
37270910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
372805175c85SJed Brown   PetscFunctionReturn(0);
372905175c85SJed Brown }
373005175c85SJed Brown 
3731b1cb36f3SHong Zhang /*@
37326a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
37336a4d4014SLisandro Dalcin 
37346a4d4014SLisandro Dalcin    Collective on TS
37356a4d4014SLisandro Dalcin 
37366a4d4014SLisandro Dalcin    Input Parameter:
37376a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
373863e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
373963e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
37405a3a76d0SJed Brown 
37416a4d4014SLisandro Dalcin    Level: beginner
37426a4d4014SLisandro Dalcin 
37435a3a76d0SJed Brown    Notes:
37445a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
37455a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
37465a3a76d0SJed Brown    stepped over the final time.
37475a3a76d0SJed Brown 
37486a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
37496a4d4014SLisandro Dalcin 
375063e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
37516a4d4014SLisandro Dalcin @*/
3752cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
37536a4d4014SLisandro Dalcin {
3754b06615a5SLisandro Dalcin   Vec               solution;
37556a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
3756f22f69f0SBarry Smith 
37576a4d4014SLisandro Dalcin   PetscFunctionBegin;
37580700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3759f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
3760feed9e9dSBarry Smith 
3761ee41a567SStefano 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 */
37620910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3763b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3764b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3765b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
37665a3a76d0SJed Brown     }
37670910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3768715f1b00SHong Zhang     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
3769bbd56ea5SKarl Rupp   } else if (u) {
37700910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
37715a3a76d0SJed Brown   }
3772b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
377368bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3774a6772fa2SLisandro Dalcin 
3775ef85077eSLisandro 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>");
3776a6772fa2SLisandro 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()");
3777a6772fa2SLisandro 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");
3778a6772fa2SLisandro Dalcin 
3779715f1b00SHong Zhang   if (ts->forward_solve) {
3780715f1b00SHong Zhang     ierr = TSForwardSetUp(ts);CHKERRQ(ierr);
3781715f1b00SHong Zhang   }
3782715f1b00SHong Zhang 
3783e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
3784715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
3785e7069c78SShri      TSTrajectory to incorrectly save the output files
3786e7069c78SShri   */
3787715f1b00SHong Zhang   /* reset time step and iteration counters */
37882808aa04SLisandro Dalcin   if (!ts->steps) {
37895ef26d82SJed Brown     ts->ksp_its           = 0;
37905ef26d82SJed Brown     ts->snes_its          = 0;
3791c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
3792c610991cSLisandro Dalcin     ts->reject            = 0;
37932808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
37942808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
37952808aa04SLisandro Dalcin   }
379610bc0e4dSStefano 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;
3797193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
3798193ac0bcSJed Brown 
3799ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3800f05ece33SBarry Smith 
3801193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
3802193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
3803a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3804cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3805a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
3806be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
3807db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
3808db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3809e7069c78SShri 
38102808aa04SLisandro Dalcin     if (!ts->steps) {
38112808aa04SLisandro Dalcin       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
38126427ac75SLisandro Dalcin       ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
38132808aa04SLisandro Dalcin     }
38146427ac75SLisandro Dalcin 
3815e1a7a14fSJed Brown     while (!ts->reason) {
38162808aa04SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
38179687d888SLisandro Dalcin       if (!ts->steprollback) {
38189687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
38199687d888SLisandro Dalcin       }
3820193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
3821f3b1f45cSBarry Smith       if (ts->testjacobian) {
3822f3b1f45cSBarry Smith         ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr);
3823f3b1f45cSBarry Smith       }
3824f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
3825f3b1f45cSBarry Smith         ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr);
3826f3b1f45cSBarry Smith       }
3827e783b05fSHong 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. */
3828b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
3829b1cb36f3SHong Zhang       }
383058818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
3831715f1b00SHong Zhang         ierr = TSForwardStep(ts);CHKERRQ(ierr);
3832715f1b00SHong Zhang       }
383310b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
3834e783b05fSHong 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. */
383558818c2dSLisandro Dalcin       if (ts->steprollback) {
383658818c2dSLisandro Dalcin         ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
383758818c2dSLisandro Dalcin       }
3838e783b05fSHong Zhang       if (!ts->steprollback) {
38392808aa04SLisandro Dalcin         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
38401eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
3841aeb4809dSShri Abhyankar       }
3842193ac0bcSJed Brown     }
38432808aa04SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
38446427ac75SLisandro Dalcin 
384549354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
38460910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
3847cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3848b06615a5SLisandro Dalcin       solution = u;
384963e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
38500574a7fbSJed Brown     } else {
3851ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3852cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3853b06615a5SLisandro Dalcin       solution = ts->vec_sol;
38540574a7fbSJed Brown     }
3855193ac0bcSJed Brown   }
3856d2daff3dSHong Zhang 
3857ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
385863e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
385956f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
3860ef222394SBarry Smith   if (ts->adjoint_solve) {
3861ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
38622b0a91c0SBarry Smith   }
38636a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
38646a4d4014SLisandro Dalcin }
38656a4d4014SLisandro Dalcin 
38667db568b7SBarry Smith /*@C
3867228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
3868228d79bcSJed Brown 
3869228d79bcSJed Brown    Collective on TS
3870228d79bcSJed Brown 
3871228d79bcSJed Brown    Input Parameters:
3872228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
3873228d79bcSJed Brown .  step - step number that has just completed
3874228d79bcSJed Brown .  ptime - model time of the state
38750910c330SBarry Smith -  u - state at the current model time
3876228d79bcSJed Brown 
3877228d79bcSJed Brown    Notes:
38787db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
38797db568b7SBarry Smith    Users would almost never call this routine directly.
3880228d79bcSJed Brown 
388163e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
388263e21af5SBarry Smith 
38837db568b7SBarry Smith    Level: developer
3884228d79bcSJed Brown 
3885228d79bcSJed Brown .keywords: TS, timestep
3886228d79bcSJed Brown @*/
38870910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
3888d763cef2SBarry Smith {
3889d6152f81SLisandro Dalcin   DM             dm;
3890a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
3891d6152f81SLisandro Dalcin   PetscErrorCode ierr;
3892d763cef2SBarry Smith 
3893d763cef2SBarry Smith   PetscFunctionBegin;
3894b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3895b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
3896d6152f81SLisandro Dalcin 
3897d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
3898d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
3899d6152f81SLisandro Dalcin 
39005edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
3901d763cef2SBarry Smith   for (i=0; i<n; i++) {
39020910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
3903d763cef2SBarry Smith   }
39045edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
3905d763cef2SBarry Smith   PetscFunctionReturn(0);
3906d763cef2SBarry Smith }
3907d763cef2SBarry Smith 
3908d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
3909d763cef2SBarry Smith /*@C
39107db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
3911a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
3912d763cef2SBarry Smith 
3913d763cef2SBarry Smith    Collective on TS
3914d763cef2SBarry Smith 
3915d763cef2SBarry Smith    Input Parameters:
3916d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
3917d763cef2SBarry Smith .  label - the title to put in the title bar
39187c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
3919a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
3920a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
3921d763cef2SBarry Smith 
3922d763cef2SBarry Smith    Output Parameter:
39230b039ecaSBarry Smith .  ctx - the context
3924d763cef2SBarry Smith 
3925d763cef2SBarry Smith    Options Database Key:
39264f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
39278b668821SLisandro Dalcin +  -ts_monitor_lg_timestep_log - automatically sets line graph monitor
39287db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
39297db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
39307db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
39317db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
3932b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
3933d763cef2SBarry Smith 
3934d763cef2SBarry Smith    Notes:
3935a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
3936d763cef2SBarry Smith 
39377db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
39387db568b7SBarry Smith 
39397db568b7SBarry 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
39407db568b7SBarry 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
39417db568b7SBarry Smith    as the first argument.
39427db568b7SBarry Smith 
39437db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
39447db568b7SBarry Smith 
3945d763cef2SBarry Smith    Level: intermediate
3946d763cef2SBarry Smith 
39477db568b7SBarry Smith .keywords: TS, monitor, line graph, residual
3948d763cef2SBarry Smith 
39497db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
39507db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
39517db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
39527db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
39537db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
39547c922b88SBarry Smith 
3955d763cef2SBarry Smith @*/
3956a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
3957d763cef2SBarry Smith {
39587f52daa2SLisandro Dalcin   PetscDraw      draw;
3959dfbe8321SBarry Smith   PetscErrorCode ierr;
3960d763cef2SBarry Smith 
3961d763cef2SBarry Smith   PetscFunctionBegin;
3962b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
39637f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
39647f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
39657f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
3966287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
39677f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
3968a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
3969d763cef2SBarry Smith   PetscFunctionReturn(0);
3970d763cef2SBarry Smith }
3971d763cef2SBarry Smith 
3972b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
3973d763cef2SBarry Smith {
39740b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
3975c32365f1SBarry Smith   PetscReal      x   = ptime,y;
3976dfbe8321SBarry Smith   PetscErrorCode ierr;
3977d763cef2SBarry Smith 
3978d763cef2SBarry Smith   PetscFunctionBegin;
397963e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
3980b06615a5SLisandro Dalcin   if (!step) {
3981a9f9c1f6SBarry Smith     PetscDrawAxis axis;
39828b668821SLisandro Dalcin     const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step";
3983a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
39848b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr);
3985a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
3986a9f9c1f6SBarry Smith   }
3987c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
39888b668821SLisandro Dalcin   if (ctx->semilogy) y = PetscLog10Real(y);
39890b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
3990b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
39910b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
39926934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
39933923b477SBarry Smith   }
3994d763cef2SBarry Smith   PetscFunctionReturn(0);
3995d763cef2SBarry Smith }
3996d763cef2SBarry Smith 
3997d763cef2SBarry Smith /*@C
3998a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
3999a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4000d763cef2SBarry Smith 
40010b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4002d763cef2SBarry Smith 
4003d763cef2SBarry Smith    Input Parameter:
40040b039ecaSBarry Smith .  ctx - the monitor context
4005d763cef2SBarry Smith 
4006d763cef2SBarry Smith    Level: intermediate
4007d763cef2SBarry Smith 
4008d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
4009d763cef2SBarry Smith 
40104f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4011d763cef2SBarry Smith @*/
4012a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4013d763cef2SBarry Smith {
4014dfbe8321SBarry Smith   PetscErrorCode ierr;
4015d763cef2SBarry Smith 
4016d763cef2SBarry Smith   PetscFunctionBegin;
40177684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
40187684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
40197684fa3eSBarry Smith   }
40200b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
402131152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4022387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4023387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4024387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
40250b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4026d763cef2SBarry Smith   PetscFunctionReturn(0);
4027d763cef2SBarry Smith }
4028d763cef2SBarry Smith 
4029d763cef2SBarry Smith /*@
4030b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4031d763cef2SBarry Smith 
4032d763cef2SBarry Smith    Not Collective
4033d763cef2SBarry Smith 
4034d763cef2SBarry Smith    Input Parameter:
4035d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4036d763cef2SBarry Smith 
4037d763cef2SBarry Smith    Output Parameter:
403819eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
4039d763cef2SBarry Smith 
4040d763cef2SBarry Smith    Level: beginner
4041d763cef2SBarry Smith 
4042b8123daeSJed Brown    Note:
4043b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
40449be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4045b8123daeSJed Brown 
4046aaa6c58dSLisandro Dalcin .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep()
4047d763cef2SBarry Smith 
4048d763cef2SBarry Smith .keywords: TS, get, time
4049d763cef2SBarry Smith @*/
40507087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4051d763cef2SBarry Smith {
4052d763cef2SBarry Smith   PetscFunctionBegin;
40530700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4054f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4055d763cef2SBarry Smith   *t = ts->ptime;
4056d763cef2SBarry Smith   PetscFunctionReturn(0);
4057d763cef2SBarry Smith }
4058d763cef2SBarry Smith 
4059e5e524a1SHong Zhang /*@
4060e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4061e5e524a1SHong Zhang 
4062e5e524a1SHong Zhang    Not Collective
4063e5e524a1SHong Zhang 
4064e5e524a1SHong Zhang    Input Parameter:
4065e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4066e5e524a1SHong Zhang 
4067e5e524a1SHong Zhang    Output Parameter:
4068e5e524a1SHong Zhang .  t  - the previous time
4069e5e524a1SHong Zhang 
4070e5e524a1SHong Zhang    Level: beginner
4071e5e524a1SHong Zhang 
4072aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4073e5e524a1SHong Zhang 
4074e5e524a1SHong Zhang .keywords: TS, get, time
4075e5e524a1SHong Zhang @*/
4076e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4077e5e524a1SHong Zhang {
4078e5e524a1SHong Zhang   PetscFunctionBegin;
4079e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4080e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4081e5e524a1SHong Zhang   *t = ts->ptime_prev;
4082e5e524a1SHong Zhang   PetscFunctionReturn(0);
4083e5e524a1SHong Zhang }
4084e5e524a1SHong Zhang 
40856a4d4014SLisandro Dalcin /*@
40866a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
40876a4d4014SLisandro Dalcin 
40883f9fe445SBarry Smith    Logically Collective on TS
40896a4d4014SLisandro Dalcin 
40906a4d4014SLisandro Dalcin    Input Parameters:
40916a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
40926a4d4014SLisandro Dalcin -  time - the time
40936a4d4014SLisandro Dalcin 
40946a4d4014SLisandro Dalcin    Level: intermediate
40956a4d4014SLisandro Dalcin 
409619eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
40976a4d4014SLisandro Dalcin 
40986a4d4014SLisandro Dalcin .keywords: TS, set, time
40996a4d4014SLisandro Dalcin @*/
41007087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
41016a4d4014SLisandro Dalcin {
41026a4d4014SLisandro Dalcin   PetscFunctionBegin;
41030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4104c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
41056a4d4014SLisandro Dalcin   ts->ptime = t;
41066a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
41076a4d4014SLisandro Dalcin }
41086a4d4014SLisandro Dalcin 
4109d763cef2SBarry Smith /*@C
4110d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4111d763cef2SBarry Smith    TS options in the database.
4112d763cef2SBarry Smith 
41133f9fe445SBarry Smith    Logically Collective on TS
4114d763cef2SBarry Smith 
4115d763cef2SBarry Smith    Input Parameter:
4116d763cef2SBarry Smith +  ts     - The TS context
4117d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4118d763cef2SBarry Smith 
4119d763cef2SBarry Smith    Notes:
4120d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4121d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4122d763cef2SBarry Smith    hyphen.
4123d763cef2SBarry Smith 
4124d763cef2SBarry Smith    Level: advanced
4125d763cef2SBarry Smith 
4126d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
4127d763cef2SBarry Smith 
4128d763cef2SBarry Smith .seealso: TSSetFromOptions()
4129d763cef2SBarry Smith 
4130d763cef2SBarry Smith @*/
41317087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4132d763cef2SBarry Smith {
4133dfbe8321SBarry Smith   PetscErrorCode ierr;
4134089b2837SJed Brown   SNES           snes;
4135d763cef2SBarry Smith 
4136d763cef2SBarry Smith   PetscFunctionBegin;
41370700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4138d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4139089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4140089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4141d763cef2SBarry Smith   PetscFunctionReturn(0);
4142d763cef2SBarry Smith }
4143d763cef2SBarry Smith 
4144d763cef2SBarry Smith /*@C
4145d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4146d763cef2SBarry Smith    TS options in the database.
4147d763cef2SBarry Smith 
41483f9fe445SBarry Smith    Logically Collective on TS
4149d763cef2SBarry Smith 
4150d763cef2SBarry Smith    Input Parameter:
4151d763cef2SBarry Smith +  ts     - The TS context
4152d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4153d763cef2SBarry Smith 
4154d763cef2SBarry Smith    Notes:
4155d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4156d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4157d763cef2SBarry Smith    hyphen.
4158d763cef2SBarry Smith 
4159d763cef2SBarry Smith    Level: advanced
4160d763cef2SBarry Smith 
4161d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
4162d763cef2SBarry Smith 
4163d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4164d763cef2SBarry Smith 
4165d763cef2SBarry Smith @*/
41667087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4167d763cef2SBarry Smith {
4168dfbe8321SBarry Smith   PetscErrorCode ierr;
4169089b2837SJed Brown   SNES           snes;
4170d763cef2SBarry Smith 
4171d763cef2SBarry Smith   PetscFunctionBegin;
41720700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4173d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4174089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4175089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4176d763cef2SBarry Smith   PetscFunctionReturn(0);
4177d763cef2SBarry Smith }
4178d763cef2SBarry Smith 
4179d763cef2SBarry Smith /*@C
4180d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4181d763cef2SBarry Smith    TS options in the database.
4182d763cef2SBarry Smith 
4183d763cef2SBarry Smith    Not Collective
4184d763cef2SBarry Smith 
4185d763cef2SBarry Smith    Input Parameter:
4186d763cef2SBarry Smith .  ts - The TS context
4187d763cef2SBarry Smith 
4188d763cef2SBarry Smith    Output Parameter:
4189d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4190d763cef2SBarry Smith 
419195452b02SPatrick Sanan    Notes:
419295452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prifix' of
4193d763cef2SBarry Smith    sufficient length to hold the prefix.
4194d763cef2SBarry Smith 
4195d763cef2SBarry Smith    Level: intermediate
4196d763cef2SBarry Smith 
4197d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
4198d763cef2SBarry Smith 
4199d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4200d763cef2SBarry Smith @*/
42017087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4202d763cef2SBarry Smith {
4203dfbe8321SBarry Smith   PetscErrorCode ierr;
4204d763cef2SBarry Smith 
4205d763cef2SBarry Smith   PetscFunctionBegin;
42060700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42074482741eSBarry Smith   PetscValidPointer(prefix,2);
4208d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4209d763cef2SBarry Smith   PetscFunctionReturn(0);
4210d763cef2SBarry Smith }
4211d763cef2SBarry Smith 
4212d763cef2SBarry Smith /*@C
4213d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4214d763cef2SBarry Smith 
4215d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4216d763cef2SBarry Smith 
4217d763cef2SBarry Smith    Input Parameter:
4218d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4219d763cef2SBarry Smith 
4220d763cef2SBarry Smith    Output Parameters:
4221e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4222e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4223e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4224e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4225d763cef2SBarry Smith 
422695452b02SPatrick Sanan    Notes:
422795452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
4228d763cef2SBarry Smith 
4229d763cef2SBarry Smith    Level: intermediate
4230d763cef2SBarry Smith 
423180275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4232d763cef2SBarry Smith 
4233d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
4234d763cef2SBarry Smith @*/
4235e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4236d763cef2SBarry Smith {
4237089b2837SJed Brown   PetscErrorCode ierr;
423824989b8cSPeter Brune   DM             dm;
4239089b2837SJed Brown 
4240d763cef2SBarry Smith   PetscFunctionBegin;
424123a57915SBarry Smith   if (Amat || Pmat) {
424223a57915SBarry Smith     SNES snes;
4243089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
424423a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4245e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
424623a57915SBarry Smith   }
424724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
424824989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4249d763cef2SBarry Smith   PetscFunctionReturn(0);
4250d763cef2SBarry Smith }
4251d763cef2SBarry Smith 
42522eca1d9cSJed Brown /*@C
42532eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
42542eca1d9cSJed Brown 
42552eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
42562eca1d9cSJed Brown 
42572eca1d9cSJed Brown    Input Parameter:
42582eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
42592eca1d9cSJed Brown 
42602eca1d9cSJed Brown    Output Parameters:
4261e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4262e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
42632eca1d9cSJed Brown .  f   - The function to compute the matrices
42642eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
42652eca1d9cSJed Brown 
426695452b02SPatrick Sanan    Notes:
426795452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
42682eca1d9cSJed Brown 
42692eca1d9cSJed Brown    Level: advanced
42702eca1d9cSJed Brown 
427180275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
42722eca1d9cSJed Brown 
42732eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
42742eca1d9cSJed Brown @*/
4275e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
42762eca1d9cSJed Brown {
4277089b2837SJed Brown   PetscErrorCode ierr;
427824989b8cSPeter Brune   DM             dm;
42790910c330SBarry Smith 
42802eca1d9cSJed Brown   PetscFunctionBegin;
4281c0aab802Sstefano_zampini   if (Amat || Pmat) {
4282c0aab802Sstefano_zampini     SNES snes;
4283089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4284f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4285e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4286c0aab802Sstefano_zampini   }
428724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
428824989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
42892eca1d9cSJed Brown   PetscFunctionReturn(0);
42902eca1d9cSJed Brown }
42912eca1d9cSJed Brown 
42921713a123SBarry Smith /*@C
429383a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
42941713a123SBarry Smith    VecView() for the solution at each timestep
42951713a123SBarry Smith 
42961713a123SBarry Smith    Collective on TS
42971713a123SBarry Smith 
42981713a123SBarry Smith    Input Parameters:
42991713a123SBarry Smith +  ts - the TS context
43001713a123SBarry Smith .  step - current time-step
4301142b95e3SSatish Balay .  ptime - current time
43020298fd71SBarry Smith -  dummy - either a viewer or NULL
43031713a123SBarry Smith 
430499fdda47SBarry Smith    Options Database:
430599fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
430699fdda47SBarry Smith 
430795452b02SPatrick Sanan    Notes:
430895452b02SPatrick Sanan     the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
430999fdda47SBarry Smith        will look bad
431099fdda47SBarry Smith 
43111713a123SBarry Smith    Level: intermediate
43121713a123SBarry Smith 
43131713a123SBarry Smith .keywords: TS,  vector, monitor, view
43141713a123SBarry Smith 
4315a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
43161713a123SBarry Smith @*/
43170910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
43181713a123SBarry Smith {
4319dfbe8321SBarry Smith   PetscErrorCode   ierr;
432083a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4321473a3ab2SBarry Smith   PetscDraw        draw;
43221713a123SBarry Smith 
43231713a123SBarry Smith   PetscFunctionBegin;
43246083293cSBarry Smith   if (!step && ictx->showinitial) {
43256083293cSBarry Smith     if (!ictx->initialsolution) {
43260910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
43271713a123SBarry Smith     }
43280910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
43296083293cSBarry Smith   }
4330b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
43310dcf80beSBarry Smith 
43326083293cSBarry Smith   if (ictx->showinitial) {
43336083293cSBarry Smith     PetscReal pause;
43346083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
43356083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
43366083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
43376083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
43386083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
43396083293cSBarry Smith   }
43400910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4341473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
434251fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4343473a3ab2SBarry Smith     char      time[32];
4344473a3ab2SBarry Smith 
4345473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
434685b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4347473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4348473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
434951fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4350473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4351473a3ab2SBarry Smith   }
4352473a3ab2SBarry Smith 
43536083293cSBarry Smith   if (ictx->showinitial) {
43546083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
43556083293cSBarry Smith   }
43561713a123SBarry Smith   PetscFunctionReturn(0);
43571713a123SBarry Smith }
43581713a123SBarry Smith 
43599110b2e7SHong Zhang /*@C
43602d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
43612d5ee99bSBarry Smith 
43622d5ee99bSBarry Smith    Collective on TS
43632d5ee99bSBarry Smith 
43642d5ee99bSBarry Smith    Input Parameters:
43652d5ee99bSBarry Smith +  ts - the TS context
43662d5ee99bSBarry Smith .  step - current time-step
43672d5ee99bSBarry Smith .  ptime - current time
43682d5ee99bSBarry Smith -  dummy - either a viewer or NULL
43692d5ee99bSBarry Smith 
43702d5ee99bSBarry Smith    Level: intermediate
43712d5ee99bSBarry Smith 
43722d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
43732d5ee99bSBarry Smith 
43742d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
43752d5ee99bSBarry Smith @*/
43762d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
43772d5ee99bSBarry Smith {
43782d5ee99bSBarry Smith   PetscErrorCode    ierr;
43792d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
43802d5ee99bSBarry Smith   PetscDraw         draw;
43816934998bSLisandro Dalcin   PetscDrawAxis     axis;
43822d5ee99bSBarry Smith   PetscInt          n;
43832d5ee99bSBarry Smith   PetscMPIInt       size;
43846934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
43852d5ee99bSBarry Smith   char              time[32];
43862d5ee99bSBarry Smith   const PetscScalar *U;
43872d5ee99bSBarry Smith 
43882d5ee99bSBarry Smith   PetscFunctionBegin;
43896934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
43906934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
43912d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
43926934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
43932d5ee99bSBarry Smith 
43942d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
43956934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
43966934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
43976934998bSLisandro Dalcin   if (!step) {
43986934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
43996934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
44006934998bSLisandro Dalcin   }
44012d5ee99bSBarry Smith 
44022d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
44036934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
44046934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4405a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
44066934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
44072d5ee99bSBarry Smith 
44086934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
44096934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
44102d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
44114b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
441285b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
44132d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
441451fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
44152d5ee99bSBarry Smith   }
44166934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
44172d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
441856d62c8fSLisandro Dalcin   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
44196934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
44202d5ee99bSBarry Smith   PetscFunctionReturn(0);
44212d5ee99bSBarry Smith }
44222d5ee99bSBarry Smith 
44236083293cSBarry Smith /*@C
442483a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
44256083293cSBarry Smith 
44266083293cSBarry Smith    Collective on TS
44276083293cSBarry Smith 
44286083293cSBarry Smith    Input Parameters:
44296083293cSBarry Smith .    ctx - the monitor context
44306083293cSBarry Smith 
44316083293cSBarry Smith    Level: intermediate
44326083293cSBarry Smith 
44336083293cSBarry Smith .keywords: TS,  vector, monitor, view
44346083293cSBarry Smith 
443583a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
44366083293cSBarry Smith @*/
443783a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
44386083293cSBarry Smith {
44396083293cSBarry Smith   PetscErrorCode ierr;
44406083293cSBarry Smith 
44416083293cSBarry Smith   PetscFunctionBegin;
444283a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
444383a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
444483a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
44456083293cSBarry Smith   PetscFunctionReturn(0);
44466083293cSBarry Smith }
44476083293cSBarry Smith 
44486083293cSBarry Smith /*@C
444983a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
44506083293cSBarry Smith 
44516083293cSBarry Smith    Collective on TS
44526083293cSBarry Smith 
44536083293cSBarry Smith    Input Parameter:
44546083293cSBarry Smith .    ts - time-step context
44556083293cSBarry Smith 
44566083293cSBarry Smith    Output Patameter:
44576083293cSBarry Smith .    ctx - the monitor context
44586083293cSBarry Smith 
445999fdda47SBarry Smith    Options Database:
446099fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
446199fdda47SBarry Smith 
44626083293cSBarry Smith    Level: intermediate
44636083293cSBarry Smith 
44646083293cSBarry Smith .keywords: TS,  vector, monitor, view
44656083293cSBarry Smith 
446683a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
44676083293cSBarry Smith @*/
446883a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
44696083293cSBarry Smith {
44706083293cSBarry Smith   PetscErrorCode   ierr;
44716083293cSBarry Smith 
44726083293cSBarry Smith   PetscFunctionBegin;
4473b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
447483a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4475e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4476bbd56ea5SKarl Rupp 
447783a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4478473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4479c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4480473a3ab2SBarry Smith 
4481473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4482c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
44836083293cSBarry Smith   PetscFunctionReturn(0);
44846083293cSBarry Smith }
44856083293cSBarry Smith 
44863a471f94SBarry Smith /*@C
44870ed3bfb6SBarry Smith    TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling
44880ed3bfb6SBarry Smith    VecView() for the solution provided by TSSetSolutionFunction() at each timestep
44890ed3bfb6SBarry Smith 
44900ed3bfb6SBarry Smith    Collective on TS
44910ed3bfb6SBarry Smith 
44920ed3bfb6SBarry Smith    Input Parameters:
44930ed3bfb6SBarry Smith +  ts - the TS context
44940ed3bfb6SBarry Smith .  step - current time-step
44950ed3bfb6SBarry Smith .  ptime - current time
44960ed3bfb6SBarry Smith -  dummy - either a viewer or NULL
44970ed3bfb6SBarry Smith 
44980ed3bfb6SBarry Smith    Options Database:
44990ed3bfb6SBarry Smith .  -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
45000ed3bfb6SBarry Smith 
45010ed3bfb6SBarry Smith    Level: intermediate
45020ed3bfb6SBarry Smith 
45030ed3bfb6SBarry Smith .keywords: TS,  vector, monitor, view
45040ed3bfb6SBarry Smith 
45050ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
45060ed3bfb6SBarry Smith @*/
45070ed3bfb6SBarry Smith PetscErrorCode  TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
45080ed3bfb6SBarry Smith {
45090ed3bfb6SBarry Smith   PetscErrorCode   ierr;
45100ed3bfb6SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
45110ed3bfb6SBarry Smith   PetscViewer      viewer = ctx->viewer;
45120ed3bfb6SBarry Smith   Vec              work;
45130ed3bfb6SBarry Smith 
45140ed3bfb6SBarry Smith   PetscFunctionBegin;
45150ed3bfb6SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
45160ed3bfb6SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
45170ed3bfb6SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
45180ed3bfb6SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
45190ed3bfb6SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
45200ed3bfb6SBarry Smith   PetscFunctionReturn(0);
45210ed3bfb6SBarry Smith }
45220ed3bfb6SBarry Smith 
45230ed3bfb6SBarry Smith /*@C
452483a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
45253a471f94SBarry Smith    VecView() for the error at each timestep
45263a471f94SBarry Smith 
45273a471f94SBarry Smith    Collective on TS
45283a471f94SBarry Smith 
45293a471f94SBarry Smith    Input Parameters:
45303a471f94SBarry Smith +  ts - the TS context
45313a471f94SBarry Smith .  step - current time-step
45323a471f94SBarry Smith .  ptime - current time
45330298fd71SBarry Smith -  dummy - either a viewer or NULL
45343a471f94SBarry Smith 
45350ed3bfb6SBarry Smith    Options Database:
45360ed3bfb6SBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
45370ed3bfb6SBarry Smith 
45383a471f94SBarry Smith    Level: intermediate
45393a471f94SBarry Smith 
45403a471f94SBarry Smith .keywords: TS,  vector, monitor, view
45413a471f94SBarry Smith 
45420ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
45433a471f94SBarry Smith @*/
45440910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
45453a471f94SBarry Smith {
45463a471f94SBarry Smith   PetscErrorCode   ierr;
454783a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
454883a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
45493a471f94SBarry Smith   Vec              work;
45503a471f94SBarry Smith 
45513a471f94SBarry Smith   PetscFunctionBegin;
4552b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
45530910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
45543a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
45550910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
45563a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
45573a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
45583a471f94SBarry Smith   PetscFunctionReturn(0);
45593a471f94SBarry Smith }
45603a471f94SBarry Smith 
4561af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
45626c699258SBarry Smith /*@
45632a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
45646c699258SBarry Smith 
45653f9fe445SBarry Smith    Logically Collective on TS and DM
45666c699258SBarry Smith 
45676c699258SBarry Smith    Input Parameters:
45682a808120SBarry Smith +  ts - the ODE integrator object
45692a808120SBarry Smith -  dm - the dm, cannot be NULL
45706c699258SBarry Smith 
4571e03a659cSJed Brown    Notes:
4572e03a659cSJed Brown    A DM can only be used for solving one problem at a time because information about the problem is stored on the DM,
4573e03a659cSJed Brown    even when not using interfaces like DMTSSetIFunction().  Use DMClone() to get a distinct DM when solving
4574e03a659cSJed Brown    different problems using the same function space.
4575e03a659cSJed Brown 
45766c699258SBarry Smith    Level: intermediate
45776c699258SBarry Smith 
45786c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
45796c699258SBarry Smith @*/
45807087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
45816c699258SBarry Smith {
45826c699258SBarry Smith   PetscErrorCode ierr;
4583089b2837SJed Brown   SNES           snes;
4584942e3340SBarry Smith   DMTS           tsdm;
45856c699258SBarry Smith 
45866c699258SBarry Smith   PetscFunctionBegin;
45870700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45882a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
458970663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4590942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
45912a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4592942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4593942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
459424989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
459524989b8cSPeter Brune         tsdm->originaldm = dm;
459624989b8cSPeter Brune       }
459724989b8cSPeter Brune     }
45986bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
459924989b8cSPeter Brune   }
46006c699258SBarry Smith   ts->dm = dm;
4601bbd56ea5SKarl Rupp 
4602089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4603089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
46046c699258SBarry Smith   PetscFunctionReturn(0);
46056c699258SBarry Smith }
46066c699258SBarry Smith 
46076c699258SBarry Smith /*@
46086c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
46096c699258SBarry Smith 
46103f9fe445SBarry Smith    Not Collective
46116c699258SBarry Smith 
46126c699258SBarry Smith    Input Parameter:
46136c699258SBarry Smith . ts - the preconditioner context
46146c699258SBarry Smith 
46156c699258SBarry Smith    Output Parameter:
46166c699258SBarry Smith .  dm - the dm
46176c699258SBarry Smith 
46186c699258SBarry Smith    Level: intermediate
46196c699258SBarry Smith 
46206c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
46216c699258SBarry Smith @*/
46227087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
46236c699258SBarry Smith {
4624496e6a7aSJed Brown   PetscErrorCode ierr;
4625496e6a7aSJed Brown 
46266c699258SBarry Smith   PetscFunctionBegin;
46270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4628496e6a7aSJed Brown   if (!ts->dm) {
4629ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4630496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4631496e6a7aSJed Brown   }
46326c699258SBarry Smith   *dm = ts->dm;
46336c699258SBarry Smith   PetscFunctionReturn(0);
46346c699258SBarry Smith }
46351713a123SBarry Smith 
46360f5c6efeSJed Brown /*@
46370f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
46380f5c6efeSJed Brown 
46393f9fe445SBarry Smith    Logically Collective on SNES
46400f5c6efeSJed Brown 
46410f5c6efeSJed Brown    Input Parameter:
4642d42a1c89SJed Brown + snes - nonlinear solver
46430910c330SBarry Smith . U - the current state at which to evaluate the residual
4644d42a1c89SJed Brown - ctx - user context, must be a TS
46450f5c6efeSJed Brown 
46460f5c6efeSJed Brown    Output Parameter:
46470f5c6efeSJed Brown . F - the nonlinear residual
46480f5c6efeSJed Brown 
46490f5c6efeSJed Brown    Notes:
46500f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
46510f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
46520f5c6efeSJed Brown 
46530f5c6efeSJed Brown    Level: advanced
46540f5c6efeSJed Brown 
46550f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
46560f5c6efeSJed Brown @*/
46570910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
46580f5c6efeSJed Brown {
46590f5c6efeSJed Brown   TS             ts = (TS)ctx;
46600f5c6efeSJed Brown   PetscErrorCode ierr;
46610f5c6efeSJed Brown 
46620f5c6efeSJed Brown   PetscFunctionBegin;
46630f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
46640910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
46650f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
46660f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
46670910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
46680f5c6efeSJed Brown   PetscFunctionReturn(0);
46690f5c6efeSJed Brown }
46700f5c6efeSJed Brown 
46710f5c6efeSJed Brown /*@
46720f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
46730f5c6efeSJed Brown 
46740f5c6efeSJed Brown    Collective on SNES
46750f5c6efeSJed Brown 
46760f5c6efeSJed Brown    Input Parameter:
46770f5c6efeSJed Brown + snes - nonlinear solver
46780910c330SBarry Smith . U - the current state at which to evaluate the residual
46790f5c6efeSJed Brown - ctx - user context, must be a TS
46800f5c6efeSJed Brown 
46810f5c6efeSJed Brown    Output Parameter:
46820f5c6efeSJed Brown + A - the Jacobian
46830f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
46840f5c6efeSJed Brown - flag - indicates any structure change in the matrix
46850f5c6efeSJed Brown 
46860f5c6efeSJed Brown    Notes:
46870f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
46880f5c6efeSJed Brown 
46890f5c6efeSJed Brown    Level: developer
46900f5c6efeSJed Brown 
46910f5c6efeSJed Brown .seealso: SNESSetJacobian()
46920f5c6efeSJed Brown @*/
4693d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
46940f5c6efeSJed Brown {
46950f5c6efeSJed Brown   TS             ts = (TS)ctx;
46960f5c6efeSJed Brown   PetscErrorCode ierr;
46970f5c6efeSJed Brown 
46980f5c6efeSJed Brown   PetscFunctionBegin;
46990f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
47000910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
47010f5c6efeSJed Brown   PetscValidPointer(A,3);
470294ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
47030f5c6efeSJed Brown   PetscValidPointer(B,4);
470494ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
47050f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4706d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
47070f5c6efeSJed Brown   PetscFunctionReturn(0);
47080f5c6efeSJed Brown }
4709325fc9f4SBarry Smith 
47100e4ef248SJed Brown /*@C
47119ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
47120e4ef248SJed Brown 
47130e4ef248SJed Brown    Collective on TS
47140e4ef248SJed Brown 
47150e4ef248SJed Brown    Input Arguments:
47160e4ef248SJed Brown +  ts - time stepping context
47170e4ef248SJed Brown .  t - time at which to evaluate
47180910c330SBarry Smith .  U - state at which to evaluate
47190e4ef248SJed Brown -  ctx - context
47200e4ef248SJed Brown 
47210e4ef248SJed Brown    Output Arguments:
47220e4ef248SJed Brown .  F - right hand side
47230e4ef248SJed Brown 
47240e4ef248SJed Brown    Level: intermediate
47250e4ef248SJed Brown 
47260e4ef248SJed Brown    Notes:
47270e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
47280e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
47290e4ef248SJed Brown 
47300e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
47310e4ef248SJed Brown @*/
47320910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
47330e4ef248SJed Brown {
47340e4ef248SJed Brown   PetscErrorCode ierr;
47350e4ef248SJed Brown   Mat            Arhs,Brhs;
47360e4ef248SJed Brown 
47370e4ef248SJed Brown   PetscFunctionBegin;
47380e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
4739d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
47400910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
47410e4ef248SJed Brown   PetscFunctionReturn(0);
47420e4ef248SJed Brown }
47430e4ef248SJed Brown 
47440e4ef248SJed Brown /*@C
47450e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
47460e4ef248SJed Brown 
47470e4ef248SJed Brown    Collective on TS
47480e4ef248SJed Brown 
47490e4ef248SJed Brown    Input Arguments:
47500e4ef248SJed Brown +  ts - time stepping context
47510e4ef248SJed Brown .  t - time at which to evaluate
47520910c330SBarry Smith .  U - state at which to evaluate
47530e4ef248SJed Brown -  ctx - context
47540e4ef248SJed Brown 
47550e4ef248SJed Brown    Output Arguments:
47560e4ef248SJed Brown +  A - pointer to operator
47570e4ef248SJed Brown .  B - pointer to preconditioning matrix
47580e4ef248SJed Brown -  flg - matrix structure flag
47590e4ef248SJed Brown 
47600e4ef248SJed Brown    Level: intermediate
47610e4ef248SJed Brown 
47620e4ef248SJed Brown    Notes:
47630e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
47640e4ef248SJed Brown 
47650e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
47660e4ef248SJed Brown @*/
4767d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
47680e4ef248SJed Brown {
47690e4ef248SJed Brown   PetscFunctionBegin;
47700e4ef248SJed Brown   PetscFunctionReturn(0);
47710e4ef248SJed Brown }
47720e4ef248SJed Brown 
47730026cea9SSean Farley /*@C
47740026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
47750026cea9SSean Farley 
47760026cea9SSean Farley    Collective on TS
47770026cea9SSean Farley 
47780026cea9SSean Farley    Input Arguments:
47790026cea9SSean Farley +  ts - time stepping context
47800026cea9SSean Farley .  t - time at which to evaluate
47810910c330SBarry Smith .  U - state at which to evaluate
47820910c330SBarry Smith .  Udot - time derivative of state vector
47830026cea9SSean Farley -  ctx - context
47840026cea9SSean Farley 
47850026cea9SSean Farley    Output Arguments:
47860026cea9SSean Farley .  F - left hand side
47870026cea9SSean Farley 
47880026cea9SSean Farley    Level: intermediate
47890026cea9SSean Farley 
47900026cea9SSean Farley    Notes:
47910910c330SBarry 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
47920026cea9SSean Farley    user is required to write their own TSComputeIFunction.
47930026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
47940026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
47950026cea9SSean Farley 
47969ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
47979ae8fd06SBarry Smith 
47989ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
47990026cea9SSean Farley @*/
48000910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
48010026cea9SSean Farley {
48020026cea9SSean Farley   PetscErrorCode ierr;
48030026cea9SSean Farley   Mat            A,B;
48040026cea9SSean Farley 
48050026cea9SSean Farley   PetscFunctionBegin;
48060298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
4807d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
48080910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
48090026cea9SSean Farley   PetscFunctionReturn(0);
48100026cea9SSean Farley }
48110026cea9SSean Farley 
48120026cea9SSean Farley /*@C
4813b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
48140026cea9SSean Farley 
48150026cea9SSean Farley    Collective on TS
48160026cea9SSean Farley 
48170026cea9SSean Farley    Input Arguments:
48180026cea9SSean Farley +  ts - time stepping context
48190026cea9SSean Farley .  t - time at which to evaluate
48200910c330SBarry Smith .  U - state at which to evaluate
48210910c330SBarry Smith .  Udot - time derivative of state vector
48220026cea9SSean Farley .  shift - shift to apply
48230026cea9SSean Farley -  ctx - context
48240026cea9SSean Farley 
48250026cea9SSean Farley    Output Arguments:
48260026cea9SSean Farley +  A - pointer to operator
48270026cea9SSean Farley .  B - pointer to preconditioning matrix
48280026cea9SSean Farley -  flg - matrix structure flag
48290026cea9SSean Farley 
4830b41af12eSJed Brown    Level: advanced
48310026cea9SSean Farley 
48320026cea9SSean Farley    Notes:
48330026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
48340026cea9SSean Farley 
4835b41af12eSJed Brown    It is only appropriate for problems of the form
4836b41af12eSJed Brown 
4837b41af12eSJed Brown $     M Udot = F(U,t)
4838b41af12eSJed Brown 
4839b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4840b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4841b41af12eSJed Brown   an implicit operator of the form
4842b41af12eSJed Brown 
4843b41af12eSJed Brown $    shift*M + J
4844b41af12eSJed Brown 
4845b41af12eSJed 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
4846b41af12eSJed Brown   a copy of M or reassemble it when requested.
4847b41af12eSJed Brown 
48480026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
48490026cea9SSean Farley @*/
4850d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
48510026cea9SSean Farley {
4852b41af12eSJed Brown   PetscErrorCode ierr;
4853b41af12eSJed Brown 
48540026cea9SSean Farley   PetscFunctionBegin;
485594ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4856b41af12eSJed Brown   ts->ijacobian.shift = shift;
48570026cea9SSean Farley   PetscFunctionReturn(0);
48580026cea9SSean Farley }
4859b41af12eSJed Brown 
4860e817cc15SEmil Constantinescu /*@
4861e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4862e817cc15SEmil Constantinescu 
4863e817cc15SEmil Constantinescu    Not Collective
4864e817cc15SEmil Constantinescu 
4865e817cc15SEmil Constantinescu    Input Parameter:
4866e817cc15SEmil Constantinescu .  ts - the TS context
4867e817cc15SEmil Constantinescu 
4868e817cc15SEmil Constantinescu    Output Parameter:
48694e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4870e817cc15SEmil Constantinescu 
4871e817cc15SEmil Constantinescu    Level: beginner
4872e817cc15SEmil Constantinescu 
4873e817cc15SEmil Constantinescu .keywords: TS, equation type
4874e817cc15SEmil Constantinescu 
4875e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4876e817cc15SEmil Constantinescu @*/
4877e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4878e817cc15SEmil Constantinescu {
4879e817cc15SEmil Constantinescu   PetscFunctionBegin;
4880e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4881e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4882e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4883e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4884e817cc15SEmil Constantinescu }
4885e817cc15SEmil Constantinescu 
4886e817cc15SEmil Constantinescu /*@
4887e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4888e817cc15SEmil Constantinescu 
4889e817cc15SEmil Constantinescu    Not Collective
4890e817cc15SEmil Constantinescu 
4891e817cc15SEmil Constantinescu    Input Parameter:
4892e817cc15SEmil Constantinescu +  ts - the TS context
48931297b224SEmil Constantinescu -  equation_type - see TSEquationType
4894e817cc15SEmil Constantinescu 
4895e817cc15SEmil Constantinescu    Level: advanced
4896e817cc15SEmil Constantinescu 
4897e817cc15SEmil Constantinescu .keywords: TS, equation type
4898e817cc15SEmil Constantinescu 
4899e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4900e817cc15SEmil Constantinescu @*/
4901e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4902e817cc15SEmil Constantinescu {
4903e817cc15SEmil Constantinescu   PetscFunctionBegin;
4904e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4905e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4906e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4907e817cc15SEmil Constantinescu }
49080026cea9SSean Farley 
49094af1b03aSJed Brown /*@
49104af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
49114af1b03aSJed Brown 
49124af1b03aSJed Brown    Not Collective
49134af1b03aSJed Brown 
49144af1b03aSJed Brown    Input Parameter:
49154af1b03aSJed Brown .  ts - the TS context
49164af1b03aSJed Brown 
49174af1b03aSJed Brown    Output Parameter:
49184af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
49194af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
49204af1b03aSJed Brown 
4921487e0bb9SJed Brown    Level: beginner
49224af1b03aSJed Brown 
4923cd652676SJed Brown    Notes:
4924cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
49254af1b03aSJed Brown 
49264af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
49274af1b03aSJed Brown 
49284af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
49294af1b03aSJed Brown @*/
49304af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
49314af1b03aSJed Brown {
49324af1b03aSJed Brown   PetscFunctionBegin;
49334af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49344af1b03aSJed Brown   PetscValidPointer(reason,2);
49354af1b03aSJed Brown   *reason = ts->reason;
49364af1b03aSJed Brown   PetscFunctionReturn(0);
49374af1b03aSJed Brown }
49384af1b03aSJed Brown 
4939d6ad946cSShri Abhyankar /*@
4940d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
4941d6ad946cSShri Abhyankar 
4942d6ad946cSShri Abhyankar    Not Collective
4943d6ad946cSShri Abhyankar 
4944d6ad946cSShri Abhyankar    Input Parameter:
4945d6ad946cSShri Abhyankar +  ts - the TS context
4946d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
4947d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4948d6ad946cSShri Abhyankar 
4949f5abba47SShri Abhyankar    Level: advanced
4950d6ad946cSShri Abhyankar 
4951d6ad946cSShri Abhyankar    Notes:
4952d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
4953d6ad946cSShri Abhyankar 
4954d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
4955d6ad946cSShri Abhyankar 
4956d6ad946cSShri Abhyankar .seealso: TSConvergedReason
4957d6ad946cSShri Abhyankar @*/
4958d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
4959d6ad946cSShri Abhyankar {
4960d6ad946cSShri Abhyankar   PetscFunctionBegin;
4961d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4962d6ad946cSShri Abhyankar   ts->reason = reason;
4963d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
4964d6ad946cSShri Abhyankar }
4965d6ad946cSShri Abhyankar 
4966cc708dedSBarry Smith /*@
4967cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
4968cc708dedSBarry Smith 
4969cc708dedSBarry Smith    Not Collective
4970cc708dedSBarry Smith 
4971cc708dedSBarry Smith    Input Parameter:
4972cc708dedSBarry Smith .  ts - the TS context
4973cc708dedSBarry Smith 
4974cc708dedSBarry Smith    Output Parameter:
497519eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
4976cc708dedSBarry Smith 
4977487e0bb9SJed Brown    Level: beginner
4978cc708dedSBarry Smith 
4979cc708dedSBarry Smith    Notes:
4980cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
4981cc708dedSBarry Smith 
4982cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
4983cc708dedSBarry Smith 
4984cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
4985cc708dedSBarry Smith @*/
4986cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
4987cc708dedSBarry Smith {
4988cc708dedSBarry Smith   PetscFunctionBegin;
4989cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4990cc708dedSBarry Smith   PetscValidPointer(ftime,2);
4991cc708dedSBarry Smith   *ftime = ts->solvetime;
4992cc708dedSBarry Smith   PetscFunctionReturn(0);
4993cc708dedSBarry Smith }
4994cc708dedSBarry Smith 
49952c18e0fdSBarry Smith /*@
49965ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
49979f67acb7SJed Brown    used by the time integrator.
49989f67acb7SJed Brown 
49999f67acb7SJed Brown    Not Collective
50009f67acb7SJed Brown 
50019f67acb7SJed Brown    Input Parameter:
50029f67acb7SJed Brown .  ts - TS context
50039f67acb7SJed Brown 
50049f67acb7SJed Brown    Output Parameter:
50059f67acb7SJed Brown .  nits - number of nonlinear iterations
50069f67acb7SJed Brown 
50079f67acb7SJed Brown    Notes:
50089f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
50099f67acb7SJed Brown 
50109f67acb7SJed Brown    Level: intermediate
50119f67acb7SJed Brown 
50129f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
50139f67acb7SJed Brown 
50145ef26d82SJed Brown .seealso:  TSGetKSPIterations()
50159f67acb7SJed Brown @*/
50165ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
50179f67acb7SJed Brown {
50189f67acb7SJed Brown   PetscFunctionBegin;
50199f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
50209f67acb7SJed Brown   PetscValidIntPointer(nits,2);
50215ef26d82SJed Brown   *nits = ts->snes_its;
50229f67acb7SJed Brown   PetscFunctionReturn(0);
50239f67acb7SJed Brown }
50249f67acb7SJed Brown 
50259f67acb7SJed Brown /*@
50265ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
50279f67acb7SJed Brown    used by the time integrator.
50289f67acb7SJed Brown 
50299f67acb7SJed Brown    Not Collective
50309f67acb7SJed Brown 
50319f67acb7SJed Brown    Input Parameter:
50329f67acb7SJed Brown .  ts - TS context
50339f67acb7SJed Brown 
50349f67acb7SJed Brown    Output Parameter:
50359f67acb7SJed Brown .  lits - number of linear iterations
50369f67acb7SJed Brown 
50379f67acb7SJed Brown    Notes:
50389f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
50399f67acb7SJed Brown 
50409f67acb7SJed Brown    Level: intermediate
50419f67acb7SJed Brown 
50429f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
50439f67acb7SJed Brown 
50445ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
50459f67acb7SJed Brown @*/
50465ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
50479f67acb7SJed Brown {
50489f67acb7SJed Brown   PetscFunctionBegin;
50499f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
50509f67acb7SJed Brown   PetscValidIntPointer(lits,2);
50515ef26d82SJed Brown   *lits = ts->ksp_its;
50529f67acb7SJed Brown   PetscFunctionReturn(0);
50539f67acb7SJed Brown }
50549f67acb7SJed Brown 
5055cef5090cSJed Brown /*@
5056cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5057cef5090cSJed Brown 
5058cef5090cSJed Brown    Not Collective
5059cef5090cSJed Brown 
5060cef5090cSJed Brown    Input Parameter:
5061cef5090cSJed Brown .  ts - TS context
5062cef5090cSJed Brown 
5063cef5090cSJed Brown    Output Parameter:
5064cef5090cSJed Brown .  rejects - number of steps rejected
5065cef5090cSJed Brown 
5066cef5090cSJed Brown    Notes:
5067cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5068cef5090cSJed Brown 
5069cef5090cSJed Brown    Level: intermediate
5070cef5090cSJed Brown 
5071cef5090cSJed Brown .keywords: TS, get, number
5072cef5090cSJed Brown 
50735ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5074cef5090cSJed Brown @*/
5075cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5076cef5090cSJed Brown {
5077cef5090cSJed Brown   PetscFunctionBegin;
5078cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5079cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5080cef5090cSJed Brown   *rejects = ts->reject;
5081cef5090cSJed Brown   PetscFunctionReturn(0);
5082cef5090cSJed Brown }
5083cef5090cSJed Brown 
5084cef5090cSJed Brown /*@
5085cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5086cef5090cSJed Brown 
5087cef5090cSJed Brown    Not Collective
5088cef5090cSJed Brown 
5089cef5090cSJed Brown    Input Parameter:
5090cef5090cSJed Brown .  ts - TS context
5091cef5090cSJed Brown 
5092cef5090cSJed Brown    Output Parameter:
5093cef5090cSJed Brown .  fails - number of failed nonlinear solves
5094cef5090cSJed Brown 
5095cef5090cSJed Brown    Notes:
5096cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5097cef5090cSJed Brown 
5098cef5090cSJed Brown    Level: intermediate
5099cef5090cSJed Brown 
5100cef5090cSJed Brown .keywords: TS, get, number
5101cef5090cSJed Brown 
51025ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5103cef5090cSJed Brown @*/
5104cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5105cef5090cSJed Brown {
5106cef5090cSJed Brown   PetscFunctionBegin;
5107cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5108cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5109cef5090cSJed Brown   *fails = ts->num_snes_failures;
5110cef5090cSJed Brown   PetscFunctionReturn(0);
5111cef5090cSJed Brown }
5112cef5090cSJed Brown 
5113cef5090cSJed Brown /*@
5114cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5115cef5090cSJed Brown 
5116cef5090cSJed Brown    Not Collective
5117cef5090cSJed Brown 
5118cef5090cSJed Brown    Input Parameter:
5119cef5090cSJed Brown +  ts - TS context
5120cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5121cef5090cSJed Brown 
5122cef5090cSJed Brown    Notes:
5123cef5090cSJed Brown    The counter is reset to zero for each step
5124cef5090cSJed Brown 
5125cef5090cSJed Brown    Options Database Key:
5126cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5127cef5090cSJed Brown 
5128cef5090cSJed Brown    Level: intermediate
5129cef5090cSJed Brown 
5130cef5090cSJed Brown .keywords: TS, set, maximum, number
5131cef5090cSJed Brown 
51325ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5133cef5090cSJed Brown @*/
5134cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5135cef5090cSJed Brown {
5136cef5090cSJed Brown   PetscFunctionBegin;
5137cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5138cef5090cSJed Brown   ts->max_reject = rejects;
5139cef5090cSJed Brown   PetscFunctionReturn(0);
5140cef5090cSJed Brown }
5141cef5090cSJed Brown 
5142cef5090cSJed Brown /*@
5143cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5144cef5090cSJed Brown 
5145cef5090cSJed Brown    Not Collective
5146cef5090cSJed Brown 
5147cef5090cSJed Brown    Input Parameter:
5148cef5090cSJed Brown +  ts - TS context
5149cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5150cef5090cSJed Brown 
5151cef5090cSJed Brown    Notes:
5152cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5153cef5090cSJed Brown 
5154cef5090cSJed Brown    Options Database Key:
5155cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5156cef5090cSJed Brown 
5157cef5090cSJed Brown    Level: intermediate
5158cef5090cSJed Brown 
5159cef5090cSJed Brown .keywords: TS, set, maximum, number
5160cef5090cSJed Brown 
51615ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5162cef5090cSJed Brown @*/
5163cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5164cef5090cSJed Brown {
5165cef5090cSJed Brown   PetscFunctionBegin;
5166cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5167cef5090cSJed Brown   ts->max_snes_failures = fails;
5168cef5090cSJed Brown   PetscFunctionReturn(0);
5169cef5090cSJed Brown }
5170cef5090cSJed Brown 
5171cef5090cSJed Brown /*@
5172cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5173cef5090cSJed Brown 
5174cef5090cSJed Brown    Not Collective
5175cef5090cSJed Brown 
5176cef5090cSJed Brown    Input Parameter:
5177cef5090cSJed Brown +  ts - TS context
5178cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5179cef5090cSJed Brown 
5180cef5090cSJed Brown    Options Database Key:
5181cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5182cef5090cSJed Brown 
5183cef5090cSJed Brown    Level: intermediate
5184cef5090cSJed Brown 
5185cef5090cSJed Brown .keywords: TS, set, error
5186cef5090cSJed Brown 
51875ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5188cef5090cSJed Brown @*/
5189cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5190cef5090cSJed Brown {
5191cef5090cSJed Brown   PetscFunctionBegin;
5192cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5193cef5090cSJed Brown   ts->errorifstepfailed = err;
5194cef5090cSJed Brown   PetscFunctionReturn(0);
5195cef5090cSJed Brown }
5196cef5090cSJed Brown 
5197fb1732b5SBarry Smith /*@C
5198fde5950dSBarry 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
5199fb1732b5SBarry Smith 
5200fb1732b5SBarry Smith    Collective on TS
5201fb1732b5SBarry Smith 
5202fb1732b5SBarry Smith    Input Parameters:
5203fb1732b5SBarry Smith +  ts - the TS context
5204fb1732b5SBarry Smith .  step - current time-step
5205fb1732b5SBarry Smith .  ptime - current time
52060910c330SBarry Smith .  u - current state
5207721cd6eeSBarry Smith -  vf - viewer and its format
5208fb1732b5SBarry Smith 
5209fb1732b5SBarry Smith    Level: intermediate
5210fb1732b5SBarry Smith 
5211fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
5212fb1732b5SBarry Smith 
5213fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5214fb1732b5SBarry Smith @*/
5215721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5216fb1732b5SBarry Smith {
5217fb1732b5SBarry Smith   PetscErrorCode ierr;
5218fb1732b5SBarry Smith 
5219fb1732b5SBarry Smith   PetscFunctionBegin;
5220721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5221721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5222721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5223ed81e22dSJed Brown   PetscFunctionReturn(0);
5224ed81e22dSJed Brown }
5225ed81e22dSJed Brown 
5226ed81e22dSJed Brown /*@C
5227ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5228ed81e22dSJed Brown 
5229ed81e22dSJed Brown    Collective on TS
5230ed81e22dSJed Brown 
5231ed81e22dSJed Brown    Input Parameters:
5232ed81e22dSJed Brown +  ts - the TS context
5233ed81e22dSJed Brown .  step - current time-step
5234ed81e22dSJed Brown .  ptime - current time
52350910c330SBarry Smith .  u - current state
5236ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5237ed81e22dSJed Brown 
5238ed81e22dSJed Brown    Level: intermediate
5239ed81e22dSJed Brown 
5240ed81e22dSJed Brown    Notes:
5241ed81e22dSJed 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.
5242ed81e22dSJed Brown    These are named according to the file name template.
5243ed81e22dSJed Brown 
5244ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5245ed81e22dSJed Brown 
5246ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5247ed81e22dSJed Brown 
5248ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5249ed81e22dSJed Brown @*/
52500910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5251ed81e22dSJed Brown {
5252ed81e22dSJed Brown   PetscErrorCode ierr;
5253ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5254ed81e22dSJed Brown   PetscViewer    viewer;
5255ed81e22dSJed Brown 
5256ed81e22dSJed Brown   PetscFunctionBegin;
525763e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
52588caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5259ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
52600910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5261ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5262ed81e22dSJed Brown   PetscFunctionReturn(0);
5263ed81e22dSJed Brown }
5264ed81e22dSJed Brown 
5265ed81e22dSJed Brown /*@C
5266ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5267ed81e22dSJed Brown 
5268ed81e22dSJed Brown    Collective on TS
5269ed81e22dSJed Brown 
5270ed81e22dSJed Brown    Input Parameters:
5271ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5272ed81e22dSJed Brown 
5273ed81e22dSJed Brown    Level: intermediate
5274ed81e22dSJed Brown 
5275ed81e22dSJed Brown    Note:
5276ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5277ed81e22dSJed Brown 
5278ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5279ed81e22dSJed Brown 
5280ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5281ed81e22dSJed Brown @*/
5282ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5283ed81e22dSJed Brown {
5284ed81e22dSJed Brown   PetscErrorCode ierr;
5285ed81e22dSJed Brown 
5286ed81e22dSJed Brown   PetscFunctionBegin;
5287ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5288fb1732b5SBarry Smith   PetscFunctionReturn(0);
5289fb1732b5SBarry Smith }
5290fb1732b5SBarry Smith 
529184df9cb4SJed Brown /*@
5292552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
529384df9cb4SJed Brown 
5294ed81e22dSJed Brown    Collective on TS if controller has not been created yet
529584df9cb4SJed Brown 
529684df9cb4SJed Brown    Input Arguments:
5297ed81e22dSJed Brown .  ts - time stepping context
529884df9cb4SJed Brown 
529984df9cb4SJed Brown    Output Arguments:
5300ed81e22dSJed Brown .  adapt - adaptive controller
530184df9cb4SJed Brown 
530284df9cb4SJed Brown    Level: intermediate
530384df9cb4SJed Brown 
5304ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
530584df9cb4SJed Brown @*/
5306552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
530784df9cb4SJed Brown {
530884df9cb4SJed Brown   PetscErrorCode ierr;
530984df9cb4SJed Brown 
531084df9cb4SJed Brown   PetscFunctionBegin;
531184df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5312bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
531384df9cb4SJed Brown   if (!ts->adapt) {
5314ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
53153bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
53161c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
531784df9cb4SJed Brown   }
5318bec58848SLisandro Dalcin   *adapt = ts->adapt;
531984df9cb4SJed Brown   PetscFunctionReturn(0);
532084df9cb4SJed Brown }
5321d6ebe24aSShri Abhyankar 
53221c3436cfSJed Brown /*@
53231c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
53241c3436cfSJed Brown 
53251c3436cfSJed Brown    Logically Collective
53261c3436cfSJed Brown 
53271c3436cfSJed Brown    Input Arguments:
53281c3436cfSJed Brown +  ts - time integration context
53291c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
53300298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
53311c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
53320298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
53331c3436cfSJed Brown 
5334a3cdaa26SBarry Smith    Options Database keys:
5335a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5336a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5337a3cdaa26SBarry Smith 
53383ff766beSShri Abhyankar    Notes:
53393ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
53403ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
53413ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
53423ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
53433ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
53443ff766beSShri Abhyankar    differential variables.
53453ff766beSShri Abhyankar 
53461c3436cfSJed Brown    Level: beginner
53471c3436cfSJed Brown 
5348c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
53491c3436cfSJed Brown @*/
53501c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
53511c3436cfSJed Brown {
53521c3436cfSJed Brown   PetscErrorCode ierr;
53531c3436cfSJed Brown 
53541c3436cfSJed Brown   PetscFunctionBegin;
5355c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
53561c3436cfSJed Brown   if (vatol) {
53571c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
53581c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
53591c3436cfSJed Brown     ts->vatol = vatol;
53601c3436cfSJed Brown   }
5361c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
53621c3436cfSJed Brown   if (vrtol) {
53631c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
53641c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
53651c3436cfSJed Brown     ts->vrtol = vrtol;
53661c3436cfSJed Brown   }
53671c3436cfSJed Brown   PetscFunctionReturn(0);
53681c3436cfSJed Brown }
53691c3436cfSJed Brown 
5370c5033834SJed Brown /*@
5371c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5372c5033834SJed Brown 
5373c5033834SJed Brown    Logically Collective
5374c5033834SJed Brown 
5375c5033834SJed Brown    Input Arguments:
5376c5033834SJed Brown .  ts - time integration context
5377c5033834SJed Brown 
5378c5033834SJed Brown    Output Arguments:
53790298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
53800298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
53810298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
53820298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5383c5033834SJed Brown 
5384c5033834SJed Brown    Level: beginner
5385c5033834SJed Brown 
5386c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5387c5033834SJed Brown @*/
5388c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5389c5033834SJed Brown {
5390c5033834SJed Brown   PetscFunctionBegin;
5391c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5392c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5393c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5394c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5395c5033834SJed Brown   PetscFunctionReturn(0);
5396c5033834SJed Brown }
5397c5033834SJed Brown 
53989c6b16b5SShri Abhyankar /*@
5399a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
54009c6b16b5SShri Abhyankar 
54019c6b16b5SShri Abhyankar    Collective on TS
54029c6b16b5SShri Abhyankar 
54039c6b16b5SShri Abhyankar    Input Arguments:
54049c6b16b5SShri Abhyankar +  ts - time stepping context
5405a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5406a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
54079c6b16b5SShri Abhyankar 
54089c6b16b5SShri Abhyankar    Output Arguments:
54097453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
54107453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
54117453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
54129c6b16b5SShri Abhyankar 
54139c6b16b5SShri Abhyankar    Level: developer
54149c6b16b5SShri Abhyankar 
5415deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
54169c6b16b5SShri Abhyankar @*/
54177453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
54189c6b16b5SShri Abhyankar {
54199c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
54209c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
54217453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
54227453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
54239c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
54247453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
54257453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
54267453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
54279c6b16b5SShri Abhyankar 
54289c6b16b5SShri Abhyankar   PetscFunctionBegin;
54299c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5430a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5431a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5432a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5433a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5434a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5435a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
54368a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
54378a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5438a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
54399c6b16b5SShri Abhyankar 
54409c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
54419c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
54429c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
54439c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
54449c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
54457453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
54467453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
54477453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
54489c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
54499c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
54509c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54519c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54529c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54537453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
54547453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
54557453f775SEmil Constantinescu       if(tola>0.){
54567453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
54577453f775SEmil Constantinescu         na_loc++;
54587453f775SEmil Constantinescu       }
54597453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54607453f775SEmil Constantinescu       if(tolr>0.){
54617453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
54627453f775SEmil Constantinescu         nr_loc++;
54637453f775SEmil Constantinescu       }
54647453f775SEmil Constantinescu       tol=tola+tolr;
54657453f775SEmil Constantinescu       if(tol>0.){
54667453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
54677453f775SEmil Constantinescu         n_loc++;
54687453f775SEmil Constantinescu       }
54699c6b16b5SShri Abhyankar     }
54709c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54719c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54729c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
54739c6b16b5SShri Abhyankar     const PetscScalar *atol;
54749c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54759c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54767453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
54777453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
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     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54949c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
54959c6b16b5SShri Abhyankar     const PetscScalar *rtol;
54969c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54979c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54987453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
54997453f775SEmil Constantinescu       tola = ts->atol;
55007453f775SEmil Constantinescu       if(tola>0.){
55017453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
55027453f775SEmil Constantinescu         na_loc++;
55037453f775SEmil Constantinescu       }
55047453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55057453f775SEmil Constantinescu       if(tolr>0.){
55067453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
55077453f775SEmil Constantinescu         nr_loc++;
55087453f775SEmil Constantinescu       }
55097453f775SEmil Constantinescu       tol=tola+tolr;
55107453f775SEmil Constantinescu       if(tol>0.){
55117453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
55127453f775SEmil Constantinescu         n_loc++;
55137453f775SEmil Constantinescu       }
55149c6b16b5SShri Abhyankar     }
55159c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55169c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
55179c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
55187453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55197453f775SEmil Constantinescu      tola = ts->atol;
55207453f775SEmil Constantinescu       if(tola>0.){
55217453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
55227453f775SEmil Constantinescu         na_loc++;
55237453f775SEmil Constantinescu       }
55247453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55257453f775SEmil Constantinescu       if(tolr>0.){
55267453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
55277453f775SEmil Constantinescu         nr_loc++;
55287453f775SEmil Constantinescu       }
55297453f775SEmil Constantinescu       tol=tola+tolr;
55307453f775SEmil Constantinescu       if(tol>0.){
55317453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
55327453f775SEmil Constantinescu         n_loc++;
55337453f775SEmil Constantinescu       }
55349c6b16b5SShri Abhyankar     }
55359c6b16b5SShri Abhyankar   }
55369c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
55379c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
55389c6b16b5SShri Abhyankar 
55397453f775SEmil Constantinescu   err_loc[0] = sum;
55407453f775SEmil Constantinescu   err_loc[1] = suma;
55417453f775SEmil Constantinescu   err_loc[2] = sumr;
55427453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
55437453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
55447453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
55457453f775SEmil Constantinescu 
5546a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
55477453f775SEmil Constantinescu 
55487453f775SEmil Constantinescu   gsum   = err_glb[0];
55497453f775SEmil Constantinescu   gsuma  = err_glb[1];
55507453f775SEmil Constantinescu   gsumr  = err_glb[2];
55517453f775SEmil Constantinescu   n_glb  = err_glb[3];
55527453f775SEmil Constantinescu   na_glb = err_glb[4];
55537453f775SEmil Constantinescu   nr_glb = err_glb[5];
55547453f775SEmil Constantinescu 
5555b1316ef9SEmil Constantinescu   *norm  = 0.;
5556b1316ef9SEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
5557b1316ef9SEmil Constantinescu   *norma = 0.;
5558b1316ef9SEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5559b1316ef9SEmil Constantinescu   *normr = 0.;
5560b1316ef9SEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
55619c6b16b5SShri Abhyankar 
55629c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
55637453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
55647453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
55659c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
55669c6b16b5SShri Abhyankar }
55679c6b16b5SShri Abhyankar 
55689c6b16b5SShri Abhyankar /*@
5569a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
55709c6b16b5SShri Abhyankar 
55719c6b16b5SShri Abhyankar    Collective on TS
55729c6b16b5SShri Abhyankar 
55739c6b16b5SShri Abhyankar    Input Arguments:
55749c6b16b5SShri Abhyankar +  ts - time stepping context
5575a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5576a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
55779c6b16b5SShri Abhyankar 
55789c6b16b5SShri Abhyankar    Output Arguments:
55797453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
55807453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
55817453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
55829c6b16b5SShri Abhyankar 
55839c6b16b5SShri Abhyankar    Level: developer
55849c6b16b5SShri Abhyankar 
5585deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
55869c6b16b5SShri Abhyankar @*/
55877453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
55889c6b16b5SShri Abhyankar {
55899c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
55907453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
55919c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
55927453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
55937453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
55947453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
55959c6b16b5SShri Abhyankar 
55969c6b16b5SShri Abhyankar   PetscFunctionBegin;
55979c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5598a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5599a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5600a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5601a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5602a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5603a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
56048a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
56058a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5606a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
56079c6b16b5SShri Abhyankar 
56089c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
56099c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
56109c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
56119c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
56129c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
56137453f775SEmil Constantinescu 
56147453f775SEmil Constantinescu   max=0.;
56157453f775SEmil Constantinescu   maxa=0.;
56167453f775SEmil Constantinescu   maxr=0.;
56177453f775SEmil Constantinescu 
56187453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
56199c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
56209c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56219c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56227453f775SEmil Constantinescu 
56237453f775SEmil Constantinescu     for (i=0; i<n; i++) {
56247453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
56257453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
56267453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56277453f775SEmil Constantinescu       tol  = tola+tolr;
56287453f775SEmil Constantinescu       if(tola>0.){
56297453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
56307453f775SEmil Constantinescu       }
56317453f775SEmil Constantinescu       if(tolr>0.){
56327453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
56337453f775SEmil Constantinescu       }
56347453f775SEmil Constantinescu       if(tol>0.){
56357453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
56367453f775SEmil Constantinescu       }
56379c6b16b5SShri Abhyankar     }
56389c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56399c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56409c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
56419c6b16b5SShri Abhyankar     const PetscScalar *atol;
56429c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56437453f775SEmil Constantinescu     for (i=0; i<n; i++) {
56447453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
56457453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
56467453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56477453f775SEmil Constantinescu       tol  = tola+tolr;
56487453f775SEmil Constantinescu       if(tola>0.){
56497453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
56507453f775SEmil Constantinescu       }
56517453f775SEmil Constantinescu       if(tolr>0.){
56527453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
56537453f775SEmil Constantinescu       }
56547453f775SEmil Constantinescu       if(tol>0.){
56557453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
56567453f775SEmil Constantinescu       }
56579c6b16b5SShri Abhyankar     }
56589c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56599c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
56609c6b16b5SShri Abhyankar     const PetscScalar *rtol;
56619c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56627453f775SEmil Constantinescu 
56637453f775SEmil Constantinescu     for (i=0; i<n; i++) {
56647453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
56657453f775SEmil Constantinescu       tola = ts->atol;
56667453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56677453f775SEmil Constantinescu       tol  = tola+tolr;
56687453f775SEmil Constantinescu       if(tola>0.){
56697453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
56707453f775SEmil Constantinescu       }
56717453f775SEmil Constantinescu       if(tolr>0.){
56727453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
56737453f775SEmil Constantinescu       }
56747453f775SEmil Constantinescu       if(tol>0.){
56757453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
56767453f775SEmil Constantinescu       }
56779c6b16b5SShri Abhyankar     }
56789c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56799c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
56807453f775SEmil Constantinescu 
56817453f775SEmil Constantinescu     for (i=0; i<n; i++) {
56827453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
56837453f775SEmil Constantinescu       tola = ts->atol;
56847453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56857453f775SEmil Constantinescu       tol  = tola+tolr;
56867453f775SEmil Constantinescu       if(tola>0.){
56877453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
56887453f775SEmil Constantinescu       }
56897453f775SEmil Constantinescu       if(tolr>0.){
56907453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
56917453f775SEmil Constantinescu       }
56927453f775SEmil Constantinescu       if(tol>0.){
56937453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
56947453f775SEmil Constantinescu       }
56959c6b16b5SShri Abhyankar     }
56969c6b16b5SShri Abhyankar   }
56979c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
56989c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
56997453f775SEmil Constantinescu   err_loc[0] = max;
57007453f775SEmil Constantinescu   err_loc[1] = maxa;
57017453f775SEmil Constantinescu   err_loc[2] = maxr;
5702a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
57037453f775SEmil Constantinescu   gmax   = err_glb[0];
57047453f775SEmil Constantinescu   gmaxa  = err_glb[1];
57057453f775SEmil Constantinescu   gmaxr  = err_glb[2];
57069c6b16b5SShri Abhyankar 
57079c6b16b5SShri Abhyankar   *norm = gmax;
57087453f775SEmil Constantinescu   *norma = gmaxa;
57097453f775SEmil Constantinescu   *normr = gmaxr;
57109c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
57117453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
57127453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
57139c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
57149c6b16b5SShri Abhyankar }
57159c6b16b5SShri Abhyankar 
57161c3436cfSJed Brown /*@
57178a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
57181c3436cfSJed Brown 
57191c3436cfSJed Brown    Collective on TS
57201c3436cfSJed Brown 
57211c3436cfSJed Brown    Input Arguments:
57221c3436cfSJed Brown +  ts - time stepping context
5723a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5724a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
5725a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
57267619abb3SShri 
57271c3436cfSJed Brown    Output Arguments:
57288a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
57298a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
57308a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
5731a4868fbcSLisandro Dalcin 
5732a4868fbcSLisandro Dalcin    Options Database Keys:
5733a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
5734a4868fbcSLisandro Dalcin 
57351c3436cfSJed Brown    Level: developer
57361c3436cfSJed Brown 
57378a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
57381c3436cfSJed Brown @*/
57397453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
57401c3436cfSJed Brown {
57418beabaa1SBarry Smith   PetscErrorCode ierr;
57421c3436cfSJed Brown 
57431c3436cfSJed Brown   PetscFunctionBegin;
5744a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
57457453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
5746a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
57477453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
5748a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
57491c3436cfSJed Brown   PetscFunctionReturn(0);
57501c3436cfSJed Brown }
57511c3436cfSJed Brown 
57528a175baeSEmil Constantinescu 
57538a175baeSEmil Constantinescu /*@
57548a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
57558a175baeSEmil Constantinescu 
57568a175baeSEmil Constantinescu    Collective on TS
57578a175baeSEmil Constantinescu 
57588a175baeSEmil Constantinescu    Input Arguments:
57598a175baeSEmil Constantinescu +  ts - time stepping context
57608a175baeSEmil Constantinescu .  E - error vector
57618a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
57628a175baeSEmil Constantinescu -  Y - state vector, previous time step
57638a175baeSEmil Constantinescu 
57648a175baeSEmil Constantinescu    Output Arguments:
57658a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
57668a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
57678a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
57688a175baeSEmil Constantinescu 
57698a175baeSEmil Constantinescu    Level: developer
57708a175baeSEmil Constantinescu 
57718a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
57728a175baeSEmil Constantinescu @*/
57738a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
57748a175baeSEmil Constantinescu {
57758a175baeSEmil Constantinescu   PetscErrorCode    ierr;
57768a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
57778a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
57788a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
57798a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
57808a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
57818a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
57828a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
57838a175baeSEmil Constantinescu 
57848a175baeSEmil Constantinescu   PetscFunctionBegin;
57858a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
57868a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
57878a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
57888a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
57898a175baeSEmil Constantinescu   PetscValidType(E,2);
57908a175baeSEmil Constantinescu   PetscValidType(U,3);
57918a175baeSEmil Constantinescu   PetscValidType(Y,4);
57928a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
57938a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
57948a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
57958a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
57968a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
57978a175baeSEmil Constantinescu 
57988a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
57998a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
58008a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
58018a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
58028a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
58038a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
58048a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
58058a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
58068a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
58078a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
58088a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
58098a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58108a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58118a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
58128a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
58138a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
58148a175baeSEmil Constantinescu       if(tola>0.){
58158a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
58168a175baeSEmil Constantinescu         na_loc++;
58178a175baeSEmil Constantinescu       }
58188a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58198a175baeSEmil Constantinescu       if(tolr>0.){
58208a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
58218a175baeSEmil Constantinescu         nr_loc++;
58228a175baeSEmil Constantinescu       }
58238a175baeSEmil Constantinescu       tol=tola+tolr;
58248a175baeSEmil Constantinescu       if(tol>0.){
58258a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
58268a175baeSEmil Constantinescu         n_loc++;
58278a175baeSEmil Constantinescu       }
58288a175baeSEmil Constantinescu     }
58298a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58308a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58318a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
58328a175baeSEmil Constantinescu     const PetscScalar *atol;
58338a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58348a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
58358a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
58368a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
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     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58538a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
58548a175baeSEmil Constantinescu     const PetscScalar *rtol;
58558a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58568a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
58578a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
58588a175baeSEmil Constantinescu       tola = ts->atol;
58598a175baeSEmil Constantinescu       if(tola>0.){
58608a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
58618a175baeSEmil Constantinescu         na_loc++;
58628a175baeSEmil Constantinescu       }
58638a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58648a175baeSEmil Constantinescu       if(tolr>0.){
58658a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
58668a175baeSEmil Constantinescu         nr_loc++;
58678a175baeSEmil Constantinescu       }
58688a175baeSEmil Constantinescu       tol=tola+tolr;
58698a175baeSEmil Constantinescu       if(tol>0.){
58708a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
58718a175baeSEmil Constantinescu         n_loc++;
58728a175baeSEmil Constantinescu       }
58738a175baeSEmil Constantinescu     }
58748a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58758a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
58768a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
58778a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
58788a175baeSEmil Constantinescu      tola = ts->atol;
58798a175baeSEmil Constantinescu       if(tola>0.){
58808a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
58818a175baeSEmil Constantinescu         na_loc++;
58828a175baeSEmil Constantinescu       }
58838a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58848a175baeSEmil Constantinescu       if(tolr>0.){
58858a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
58868a175baeSEmil Constantinescu         nr_loc++;
58878a175baeSEmil Constantinescu       }
58888a175baeSEmil Constantinescu       tol=tola+tolr;
58898a175baeSEmil Constantinescu       if(tol>0.){
58908a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
58918a175baeSEmil Constantinescu         n_loc++;
58928a175baeSEmil Constantinescu       }
58938a175baeSEmil Constantinescu     }
58948a175baeSEmil Constantinescu   }
58958a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
58968a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
58978a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
58988a175baeSEmil Constantinescu 
58998a175baeSEmil Constantinescu   err_loc[0] = sum;
59008a175baeSEmil Constantinescu   err_loc[1] = suma;
59018a175baeSEmil Constantinescu   err_loc[2] = sumr;
59028a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
59038a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
59048a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
59058a175baeSEmil Constantinescu 
5906a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
59078a175baeSEmil Constantinescu 
59088a175baeSEmil Constantinescu   gsum   = err_glb[0];
59098a175baeSEmil Constantinescu   gsuma  = err_glb[1];
59108a175baeSEmil Constantinescu   gsumr  = err_glb[2];
59118a175baeSEmil Constantinescu   n_glb  = err_glb[3];
59128a175baeSEmil Constantinescu   na_glb = err_glb[4];
59138a175baeSEmil Constantinescu   nr_glb = err_glb[5];
59148a175baeSEmil Constantinescu 
59158a175baeSEmil Constantinescu   *norm  = 0.;
59168a175baeSEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
59178a175baeSEmil Constantinescu   *norma = 0.;
59188a175baeSEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
59198a175baeSEmil Constantinescu   *normr = 0.;
59208a175baeSEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
59218a175baeSEmil Constantinescu 
59228a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
59238a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
59248a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
59258a175baeSEmil Constantinescu   PetscFunctionReturn(0);
59268a175baeSEmil Constantinescu }
59278a175baeSEmil Constantinescu 
59288a175baeSEmil Constantinescu /*@
59298a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
59308a175baeSEmil Constantinescu    Collective on TS
59318a175baeSEmil Constantinescu 
59328a175baeSEmil Constantinescu    Input Arguments:
59338a175baeSEmil Constantinescu +  ts - time stepping context
59348a175baeSEmil Constantinescu .  E - error vector
59358a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
59368a175baeSEmil Constantinescu -  Y - state vector, previous time step
59378a175baeSEmil Constantinescu 
59388a175baeSEmil Constantinescu    Output Arguments:
59398a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
59408a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
59418a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
59428a175baeSEmil Constantinescu 
59438a175baeSEmil Constantinescu    Level: developer
59448a175baeSEmil Constantinescu 
59458a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
59468a175baeSEmil Constantinescu @*/
59478a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
59488a175baeSEmil Constantinescu {
59498a175baeSEmil Constantinescu   PetscErrorCode    ierr;
59508a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
59518a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
59528a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
59538a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
59548a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
59558a175baeSEmil Constantinescu 
59568a175baeSEmil Constantinescu   PetscFunctionBegin;
59578a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
59588a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
59598a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
59608a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
59618a175baeSEmil Constantinescu   PetscValidType(E,2);
59628a175baeSEmil Constantinescu   PetscValidType(U,3);
59638a175baeSEmil Constantinescu   PetscValidType(Y,4);
59648a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
59658a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
59668a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
59678a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
59688a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
59698a175baeSEmil Constantinescu 
59708a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
59718a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
59728a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
59738a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
59748a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
59758a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
59768a175baeSEmil Constantinescu 
59778a175baeSEmil Constantinescu   max=0.;
59788a175baeSEmil Constantinescu   maxa=0.;
59798a175baeSEmil Constantinescu   maxr=0.;
59808a175baeSEmil Constantinescu 
59818a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
59828a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
59838a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
59848a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59858a175baeSEmil Constantinescu 
59868a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
59878a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59888a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
59898a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59908a175baeSEmil Constantinescu       tol  = tola+tolr;
59918a175baeSEmil Constantinescu       if(tola>0.){
59928a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
59938a175baeSEmil Constantinescu       }
59948a175baeSEmil Constantinescu       if(tolr>0.){
59958a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
59968a175baeSEmil Constantinescu       }
59978a175baeSEmil Constantinescu       if(tol>0.){
59988a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
59998a175baeSEmil Constantinescu       }
60008a175baeSEmil Constantinescu     }
60018a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60028a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60038a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
60048a175baeSEmil Constantinescu     const PetscScalar *atol;
60058a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60068a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
60078a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
60088a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
60098a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60108a175baeSEmil Constantinescu       tol  = tola+tolr;
60118a175baeSEmil Constantinescu       if(tola>0.){
60128a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
60138a175baeSEmil Constantinescu       }
60148a175baeSEmil Constantinescu       if(tolr>0.){
60158a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
60168a175baeSEmil Constantinescu       }
60178a175baeSEmil Constantinescu       if(tol>0.){
60188a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
60198a175baeSEmil Constantinescu       }
60208a175baeSEmil Constantinescu     }
60218a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60228a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
60238a175baeSEmil Constantinescu     const PetscScalar *rtol;
60248a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60258a175baeSEmil Constantinescu 
60268a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
60278a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
60288a175baeSEmil Constantinescu       tola = ts->atol;
60298a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60308a175baeSEmil Constantinescu       tol  = tola+tolr;
60318a175baeSEmil Constantinescu       if(tola>0.){
60328a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
60338a175baeSEmil Constantinescu       }
60348a175baeSEmil Constantinescu       if(tolr>0.){
60358a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
60368a175baeSEmil Constantinescu       }
60378a175baeSEmil Constantinescu       if(tol>0.){
60388a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
60398a175baeSEmil Constantinescu       }
60408a175baeSEmil Constantinescu     }
60418a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60428a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
60438a175baeSEmil Constantinescu 
60448a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
60458a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
60468a175baeSEmil Constantinescu       tola = ts->atol;
60478a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60488a175baeSEmil Constantinescu       tol  = tola+tolr;
60498a175baeSEmil Constantinescu       if(tola>0.){
60508a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
60518a175baeSEmil Constantinescu       }
60528a175baeSEmil Constantinescu       if(tolr>0.){
60538a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
60548a175baeSEmil Constantinescu       }
60558a175baeSEmil Constantinescu       if(tol>0.){
60568a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
60578a175baeSEmil Constantinescu       }
60588a175baeSEmil Constantinescu     }
60598a175baeSEmil Constantinescu   }
60608a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
60618a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
60628a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
60638a175baeSEmil Constantinescu   err_loc[0] = max;
60648a175baeSEmil Constantinescu   err_loc[1] = maxa;
60658a175baeSEmil Constantinescu   err_loc[2] = maxr;
6066a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
60678a175baeSEmil Constantinescu   gmax   = err_glb[0];
60688a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
60698a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
60708a175baeSEmil Constantinescu 
60718a175baeSEmil Constantinescu   *norm = gmax;
60728a175baeSEmil Constantinescu   *norma = gmaxa;
60738a175baeSEmil Constantinescu   *normr = gmaxr;
60748a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
60758a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
60768a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
60778a175baeSEmil Constantinescu   PetscFunctionReturn(0);
60788a175baeSEmil Constantinescu }
60798a175baeSEmil Constantinescu 
60808a175baeSEmil Constantinescu /*@
60818a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
60828a175baeSEmil Constantinescu 
60838a175baeSEmil Constantinescu    Collective on TS
60848a175baeSEmil Constantinescu 
60858a175baeSEmil Constantinescu    Input Arguments:
60868a175baeSEmil Constantinescu +  ts - time stepping context
60878a175baeSEmil Constantinescu .  E - error vector
60888a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
60898a175baeSEmil Constantinescu .  Y - state vector, previous time step
60908a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
60918a175baeSEmil Constantinescu 
60928a175baeSEmil Constantinescu    Output Arguments:
60938a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
60948a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
60958a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
60968a175baeSEmil Constantinescu 
60978a175baeSEmil Constantinescu    Options Database Keys:
60988a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
60998a175baeSEmil Constantinescu 
61008a175baeSEmil Constantinescu    Level: developer
61018a175baeSEmil Constantinescu 
61028a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
61038a175baeSEmil Constantinescu @*/
61048a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
61058a175baeSEmil Constantinescu {
61068a175baeSEmil Constantinescu   PetscErrorCode ierr;
61078a175baeSEmil Constantinescu 
61088a175baeSEmil Constantinescu   PetscFunctionBegin;
61098a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
61108a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
61118a175baeSEmil Constantinescu   } else if(wnormtype == NORM_INFINITY) {
61128a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
61138a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
61148a175baeSEmil Constantinescu   PetscFunctionReturn(0);
61158a175baeSEmil Constantinescu }
61168a175baeSEmil Constantinescu 
61178a175baeSEmil Constantinescu 
61188d59e960SJed Brown /*@
61198d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
61208d59e960SJed Brown 
61218d59e960SJed Brown    Logically Collective on TS
61228d59e960SJed Brown 
61238d59e960SJed Brown    Input Arguments:
61248d59e960SJed Brown +  ts - time stepping context
61258d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
61268d59e960SJed Brown 
61278d59e960SJed Brown    Note:
61288d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
61298d59e960SJed Brown 
61308d59e960SJed Brown    Level: intermediate
61318d59e960SJed Brown 
61328d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
61338d59e960SJed Brown @*/
61348d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
61358d59e960SJed Brown {
61368d59e960SJed Brown   PetscFunctionBegin;
61378d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
61388d59e960SJed Brown   ts->cfltime_local = cfltime;
61398d59e960SJed Brown   ts->cfltime       = -1.;
61408d59e960SJed Brown   PetscFunctionReturn(0);
61418d59e960SJed Brown }
61428d59e960SJed Brown 
61438d59e960SJed Brown /*@
61448d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
61458d59e960SJed Brown 
61468d59e960SJed Brown    Collective on TS
61478d59e960SJed Brown 
61488d59e960SJed Brown    Input Arguments:
61498d59e960SJed Brown .  ts - time stepping context
61508d59e960SJed Brown 
61518d59e960SJed Brown    Output Arguments:
61528d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
61538d59e960SJed Brown 
61548d59e960SJed Brown    Level: advanced
61558d59e960SJed Brown 
61568d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
61578d59e960SJed Brown @*/
61588d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
61598d59e960SJed Brown {
61608d59e960SJed Brown   PetscErrorCode ierr;
61618d59e960SJed Brown 
61628d59e960SJed Brown   PetscFunctionBegin;
61638d59e960SJed Brown   if (ts->cfltime < 0) {
6164b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
61658d59e960SJed Brown   }
61668d59e960SJed Brown   *cfltime = ts->cfltime;
61678d59e960SJed Brown   PetscFunctionReturn(0);
61688d59e960SJed Brown }
61698d59e960SJed Brown 
6170d6ebe24aSShri Abhyankar /*@
6171d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6172d6ebe24aSShri Abhyankar 
6173d6ebe24aSShri Abhyankar    Input Parameters:
6174d6ebe24aSShri Abhyankar .  ts   - the TS context.
6175d6ebe24aSShri Abhyankar .  xl   - lower bound.
6176d6ebe24aSShri Abhyankar .  xu   - upper bound.
6177d6ebe24aSShri Abhyankar 
6178d6ebe24aSShri Abhyankar    Notes:
6179d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6180e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6181d6ebe24aSShri Abhyankar 
61822bd2b0e6SSatish Balay    Level: advanced
61832bd2b0e6SSatish Balay 
6184d6ebe24aSShri Abhyankar @*/
6185d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6186d6ebe24aSShri Abhyankar {
6187d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6188d6ebe24aSShri Abhyankar   SNES           snes;
6189d6ebe24aSShri Abhyankar 
6190d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6191d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6192d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6193d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6194d6ebe24aSShri Abhyankar }
6195d6ebe24aSShri Abhyankar 
6196325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
6197c6db04a5SJed Brown #include <mex.h>
6198325fc9f4SBarry Smith 
6199325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
6200325fc9f4SBarry Smith 
6201325fc9f4SBarry Smith /*
6202325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
6203325fc9f4SBarry Smith                          TSSetFunctionMatlab().
6204325fc9f4SBarry Smith 
6205325fc9f4SBarry Smith    Collective on TS
6206325fc9f4SBarry Smith 
6207325fc9f4SBarry Smith    Input Parameters:
6208325fc9f4SBarry Smith +  snes - the TS context
62090910c330SBarry Smith -  u - input vector
6210325fc9f4SBarry Smith 
6211325fc9f4SBarry Smith    Output Parameter:
6212325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
6213325fc9f4SBarry Smith 
6214325fc9f4SBarry Smith    Notes:
6215325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
6216325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
6217325fc9f4SBarry Smith    themselves.
6218325fc9f4SBarry Smith 
6219325fc9f4SBarry Smith    Level: developer
6220325fc9f4SBarry Smith 
6221325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6222325fc9f4SBarry Smith 
6223325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6224325fc9f4SBarry Smith */
62250910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
6226325fc9f4SBarry Smith {
6227325fc9f4SBarry Smith   PetscErrorCode  ierr;
6228325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6229325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
6230325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
6231325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
6232325fc9f4SBarry Smith 
6233325fc9f4SBarry Smith   PetscFunctionBegin;
6234325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
62350910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
62360910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
6237325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
62380910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
6239325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
6240325fc9f4SBarry Smith 
6241325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
62420910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
62430910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
62440910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
6245bbd56ea5SKarl Rupp 
6246325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6247325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
6248325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6249325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6250325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
6251325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
6252325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
6253325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
6254325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6255325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6256325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6257325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6258325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6259325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6260325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6261325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6262325fc9f4SBarry Smith   PetscFunctionReturn(0);
6263325fc9f4SBarry Smith }
6264325fc9f4SBarry Smith 
6265325fc9f4SBarry Smith /*
6266325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
6267325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
6268e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
6269325fc9f4SBarry Smith 
6270325fc9f4SBarry Smith    Logically Collective on TS
6271325fc9f4SBarry Smith 
6272325fc9f4SBarry Smith    Input Parameters:
6273325fc9f4SBarry Smith +  ts - the TS context
6274325fc9f4SBarry Smith -  func - function evaluation routine
6275325fc9f4SBarry Smith 
6276325fc9f4SBarry Smith    Calling sequence of func:
62770910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
6278325fc9f4SBarry Smith 
6279325fc9f4SBarry Smith    Level: beginner
6280325fc9f4SBarry Smith 
6281325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6282325fc9f4SBarry Smith 
6283325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6284325fc9f4SBarry Smith */
6285cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
6286325fc9f4SBarry Smith {
6287325fc9f4SBarry Smith   PetscErrorCode  ierr;
6288325fc9f4SBarry Smith   TSMatlabContext *sctx;
6289325fc9f4SBarry Smith 
6290325fc9f4SBarry Smith   PetscFunctionBegin;
6291325fc9f4SBarry Smith   /* currently sctx is memory bleed */
629295dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6293325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6294325fc9f4SBarry Smith   /*
6295325fc9f4SBarry Smith      This should work, but it doesn't
6296325fc9f4SBarry Smith   sctx->ctx = ctx;
6297325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6298325fc9f4SBarry Smith   */
6299325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6300bbd56ea5SKarl Rupp 
63010298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
6302325fc9f4SBarry Smith   PetscFunctionReturn(0);
6303325fc9f4SBarry Smith }
6304325fc9f4SBarry Smith 
6305325fc9f4SBarry Smith /*
6306325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
6307325fc9f4SBarry Smith                          TSSetJacobianMatlab().
6308325fc9f4SBarry Smith 
6309325fc9f4SBarry Smith    Collective on TS
6310325fc9f4SBarry Smith 
6311325fc9f4SBarry Smith    Input Parameters:
6312cdcf91faSSean Farley +  ts - the TS context
63130910c330SBarry Smith .  u - input vector
6314325fc9f4SBarry Smith .  A, B - the matrices
6315325fc9f4SBarry Smith -  ctx - user context
6316325fc9f4SBarry Smith 
6317325fc9f4SBarry Smith    Level: developer
6318325fc9f4SBarry Smith 
6319325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6320325fc9f4SBarry Smith 
6321325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6322325fc9f4SBarry Smith @*/
6323f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
6324325fc9f4SBarry Smith {
6325325fc9f4SBarry Smith   PetscErrorCode  ierr;
6326325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6327325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
6328325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
6329325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
6330325fc9f4SBarry Smith 
6331325fc9f4SBarry Smith   PetscFunctionBegin;
6332cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
63330910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
6334325fc9f4SBarry Smith 
63350910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
6336325fc9f4SBarry Smith 
6337cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
63380910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
63390910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
63400910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
63410910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
6342bbd56ea5SKarl Rupp 
6343325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6344325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
6345325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6346325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6347325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
6348325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
6349325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
6350325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
6351325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
6352325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
6353325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6354325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6355325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6356325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6357325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6358325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6359325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6360325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
6361325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
6362325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6363325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
6364325fc9f4SBarry Smith   PetscFunctionReturn(0);
6365325fc9f4SBarry Smith }
6366325fc9f4SBarry Smith 
6367325fc9f4SBarry Smith /*
6368325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
6369e3c5b3baSBarry 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
6370325fc9f4SBarry Smith 
6371325fc9f4SBarry Smith    Logically Collective on TS
6372325fc9f4SBarry Smith 
6373325fc9f4SBarry Smith    Input Parameters:
6374cdcf91faSSean Farley +  ts - the TS context
6375325fc9f4SBarry Smith .  A,B - Jacobian matrices
6376325fc9f4SBarry Smith .  func - function evaluation routine
6377325fc9f4SBarry Smith -  ctx - user context
6378325fc9f4SBarry Smith 
6379325fc9f4SBarry Smith    Calling sequence of func:
63800910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
6381325fc9f4SBarry Smith 
6382325fc9f4SBarry Smith    Level: developer
6383325fc9f4SBarry Smith 
6384325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6385325fc9f4SBarry Smith 
6386325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6387325fc9f4SBarry Smith */
6388cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
6389325fc9f4SBarry Smith {
6390325fc9f4SBarry Smith   PetscErrorCode  ierr;
6391325fc9f4SBarry Smith   TSMatlabContext *sctx;
6392325fc9f4SBarry Smith 
6393325fc9f4SBarry Smith   PetscFunctionBegin;
6394325fc9f4SBarry Smith   /* currently sctx is memory bleed */
639595dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6396325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6397325fc9f4SBarry Smith   /*
6398325fc9f4SBarry Smith      This should work, but it doesn't
6399325fc9f4SBarry Smith   sctx->ctx = ctx;
6400325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6401325fc9f4SBarry Smith   */
6402325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6403bbd56ea5SKarl Rupp 
6404cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
6405325fc9f4SBarry Smith   PetscFunctionReturn(0);
6406325fc9f4SBarry Smith }
6407325fc9f4SBarry Smith 
6408b5b1a830SBarry Smith /*
6409b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
6410b5b1a830SBarry Smith 
6411b5b1a830SBarry Smith    Collective on TS
6412b5b1a830SBarry Smith 
6413b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6414b5b1a830SBarry Smith @*/
64150910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
6416b5b1a830SBarry Smith {
6417b5b1a830SBarry Smith   PetscErrorCode  ierr;
6418b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6419a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
6420b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
6421b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
6422b5b1a830SBarry Smith 
6423b5b1a830SBarry Smith   PetscFunctionBegin;
6424cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
64250910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
6426b5b1a830SBarry Smith 
6427cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
64280910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
6429bbd56ea5SKarl Rupp 
6430b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6431b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
6432b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
6433b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
6434b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
6435b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
6436b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
6437b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6438b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
6439b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
6440b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
6441b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
6442b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
6443b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
6444b5b1a830SBarry Smith   PetscFunctionReturn(0);
6445b5b1a830SBarry Smith }
6446b5b1a830SBarry Smith 
6447b5b1a830SBarry Smith /*
6448b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
6449b5b1a830SBarry Smith 
6450b5b1a830SBarry Smith    Level: developer
6451b5b1a830SBarry Smith 
6452b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
6453b5b1a830SBarry Smith 
6454b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6455b5b1a830SBarry Smith */
6456cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
6457b5b1a830SBarry Smith {
6458b5b1a830SBarry Smith   PetscErrorCode  ierr;
6459b5b1a830SBarry Smith   TSMatlabContext *sctx;
6460b5b1a830SBarry Smith 
6461b5b1a830SBarry Smith   PetscFunctionBegin;
6462b5b1a830SBarry Smith   /* currently sctx is memory bleed */
646395dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6464b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6465b5b1a830SBarry Smith   /*
6466b5b1a830SBarry Smith      This should work, but it doesn't
6467b5b1a830SBarry Smith   sctx->ctx = ctx;
6468b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6469b5b1a830SBarry Smith   */
6470b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6471bbd56ea5SKarl Rupp 
64720298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
6473b5b1a830SBarry Smith   PetscFunctionReturn(0);
6474b5b1a830SBarry Smith }
6475325fc9f4SBarry Smith #endif
6476b3603a34SBarry Smith 
6477b3603a34SBarry Smith /*@C
64784f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6479b3603a34SBarry Smith        in a time based line graph
6480b3603a34SBarry Smith 
6481b3603a34SBarry Smith    Collective on TS
6482b3603a34SBarry Smith 
6483b3603a34SBarry Smith    Input Parameters:
6484b3603a34SBarry Smith +  ts - the TS context
6485b3603a34SBarry Smith .  step - current time-step
6486b3603a34SBarry Smith .  ptime - current time
64877db568b7SBarry Smith .  u - current solution
64887db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6489b3603a34SBarry Smith 
6490b3d3934dSBarry Smith    Options Database:
64919ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6492b3d3934dSBarry Smith 
6493b3603a34SBarry Smith    Level: intermediate
6494b3603a34SBarry Smith 
649595452b02SPatrick Sanan    Notes:
649695452b02SPatrick Sanan     Each process in a parallel run displays its component solutions in a separate window
6497b3603a34SBarry Smith 
6498b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
6499b3603a34SBarry Smith 
65007db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
65017db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
65027db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
65037db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6504b3603a34SBarry Smith @*/
65057db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6506b3603a34SBarry Smith {
6507b3603a34SBarry Smith   PetscErrorCode    ierr;
65087db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6509b3603a34SBarry Smith   const PetscScalar *yy;
651080666b62SBarry Smith   Vec               v;
6511b3603a34SBarry Smith 
6512b3603a34SBarry Smith   PetscFunctionBegin;
651363e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
651458ff32f7SBarry Smith   if (!step) {
6515a9f9c1f6SBarry Smith     PetscDrawAxis axis;
65166934998bSLisandro Dalcin     PetscInt      dim;
6517a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6518a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6519bab0a581SBarry Smith     if (!ctx->names) {
6520bab0a581SBarry Smith       PetscBool flg;
6521bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6522bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6523bab0a581SBarry Smith       if (flg) {
6524bab0a581SBarry Smith         PetscInt i,n;
6525bab0a581SBarry Smith         char     **names;
6526bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6527bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6528bab0a581SBarry Smith         for (i=0; i<n; i++) {
6529bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6530bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6531bab0a581SBarry Smith         }
6532bab0a581SBarry Smith         names[n] = NULL;
6533bab0a581SBarry Smith         ctx->names = names;
6534bab0a581SBarry Smith       }
6535bab0a581SBarry Smith     }
6536387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6537387f4636SBarry Smith       char      **displaynames;
6538387f4636SBarry Smith       PetscBool flg;
6539387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
654095dccacaSBarry Smith       ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6541387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
6542c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6543387f4636SBarry Smith       if (flg) {
6544a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6545387f4636SBarry Smith       }
6546387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6547387f4636SBarry Smith     }
6548387f4636SBarry Smith     if (ctx->displaynames) {
6549387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6550387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6551387f4636SBarry Smith     } else if (ctx->names) {
65520910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
65530b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6554387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6555b0bc92c6SBarry Smith     } else {
6556b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6557b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6558387f4636SBarry Smith     }
65590b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
656058ff32f7SBarry Smith   }
65616934998bSLisandro Dalcin 
65626934998bSLisandro Dalcin   if (!ctx->transform) v = u;
65636934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
656480666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
65656934998bSLisandro Dalcin   if (ctx->displaynames) {
65666934998bSLisandro Dalcin     PetscInt i;
65676934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
65686934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
65696934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
65706934998bSLisandro Dalcin   } else {
6571e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6572e3efe391SJed Brown     PetscInt  i,n;
65736934998bSLisandro Dalcin     PetscReal *yreal;
657480666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6575785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6576e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
65770b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6578e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6579e3efe391SJed Brown #else
65800b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6581e3efe391SJed Brown #endif
658280666b62SBarry Smith   }
65836934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
65846934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
65856934998bSLisandro Dalcin 
6586b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
65870b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
65886934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
65893923b477SBarry Smith   }
6590b3603a34SBarry Smith   PetscFunctionReturn(0);
6591b3603a34SBarry Smith }
6592b3603a34SBarry Smith 
6593b037adc7SBarry Smith /*@C
659431152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6595b037adc7SBarry Smith 
6596b037adc7SBarry Smith    Collective on TS
6597b037adc7SBarry Smith 
6598b037adc7SBarry Smith    Input Parameters:
6599b037adc7SBarry Smith +  ts - the TS context
6600b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6601b037adc7SBarry Smith 
6602b037adc7SBarry Smith    Level: intermediate
6603b037adc7SBarry Smith 
660495452b02SPatrick Sanan    Notes:
660595452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66067db568b7SBarry Smith 
6607b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
6608b037adc7SBarry Smith 
6609a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6610b037adc7SBarry Smith @*/
661131152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6612b037adc7SBarry Smith {
6613b037adc7SBarry Smith   PetscErrorCode    ierr;
6614b037adc7SBarry Smith   PetscInt          i;
6615b037adc7SBarry Smith 
6616b037adc7SBarry Smith   PetscFunctionBegin;
6617b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6618b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
66195537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6620b3d3934dSBarry Smith       break;
6621b3d3934dSBarry Smith     }
6622b3d3934dSBarry Smith   }
6623b3d3934dSBarry Smith   PetscFunctionReturn(0);
6624b3d3934dSBarry Smith }
6625b3d3934dSBarry Smith 
6626e673d494SBarry Smith /*@C
6627e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6628e673d494SBarry Smith 
6629e673d494SBarry Smith    Collective on TS
6630e673d494SBarry Smith 
6631e673d494SBarry Smith    Input Parameters:
6632e673d494SBarry Smith +  ts - the TS context
6633e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6634e673d494SBarry Smith 
6635e673d494SBarry Smith    Level: intermediate
6636e673d494SBarry Smith 
6637e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6638e673d494SBarry Smith 
6639a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6640e673d494SBarry Smith @*/
6641e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6642e673d494SBarry Smith {
6643e673d494SBarry Smith   PetscErrorCode    ierr;
6644e673d494SBarry Smith 
6645e673d494SBarry Smith   PetscFunctionBegin;
6646e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6647e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6648e673d494SBarry Smith   PetscFunctionReturn(0);
6649e673d494SBarry Smith }
6650e673d494SBarry Smith 
6651b3d3934dSBarry Smith /*@C
6652b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6653b3d3934dSBarry Smith 
6654b3d3934dSBarry Smith    Collective on TS
6655b3d3934dSBarry Smith 
6656b3d3934dSBarry Smith    Input Parameter:
6657b3d3934dSBarry Smith .  ts - the TS context
6658b3d3934dSBarry Smith 
6659b3d3934dSBarry Smith    Output Parameter:
6660b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6661b3d3934dSBarry Smith 
6662b3d3934dSBarry Smith    Level: intermediate
6663b3d3934dSBarry Smith 
666495452b02SPatrick Sanan    Notes:
666595452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66667db568b7SBarry Smith 
6667b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6668b3d3934dSBarry Smith 
6669b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6670b3d3934dSBarry Smith @*/
6671b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6672b3d3934dSBarry Smith {
6673b3d3934dSBarry Smith   PetscInt       i;
6674b3d3934dSBarry Smith 
6675b3d3934dSBarry Smith   PetscFunctionBegin;
6676b3d3934dSBarry Smith   *names = NULL;
6677b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6678b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
66795537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6680b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6681b3d3934dSBarry Smith       break;
6682387f4636SBarry Smith     }
6683387f4636SBarry Smith   }
6684387f4636SBarry Smith   PetscFunctionReturn(0);
6685387f4636SBarry Smith }
6686387f4636SBarry Smith 
6687a66092f1SBarry Smith /*@C
6688a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6689a66092f1SBarry Smith 
6690a66092f1SBarry Smith    Collective on TS
6691a66092f1SBarry Smith 
6692a66092f1SBarry Smith    Input Parameters:
6693a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6694a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
6695a66092f1SBarry Smith 
6696a66092f1SBarry Smith    Level: intermediate
6697a66092f1SBarry Smith 
6698a66092f1SBarry Smith .keywords: TS,  vector, monitor, view
6699a66092f1SBarry Smith 
6700a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6701a66092f1SBarry Smith @*/
6702a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6703a66092f1SBarry Smith {
6704a66092f1SBarry Smith   PetscInt          j = 0,k;
6705a66092f1SBarry Smith   PetscErrorCode    ierr;
6706a66092f1SBarry Smith 
6707a66092f1SBarry Smith   PetscFunctionBegin;
6708a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6709a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6710a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6711a66092f1SBarry Smith   while (displaynames[j]) j++;
6712a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6713a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6714a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6715a66092f1SBarry Smith   j = 0;
6716a66092f1SBarry Smith   while (displaynames[j]) {
6717a66092f1SBarry Smith     k = 0;
6718a66092f1SBarry Smith     while (ctx->names[k]) {
6719a66092f1SBarry Smith       PetscBool flg;
6720a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6721a66092f1SBarry Smith       if (flg) {
6722a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6723a66092f1SBarry Smith         break;
6724a66092f1SBarry Smith       }
6725a66092f1SBarry Smith       k++;
6726a66092f1SBarry Smith     }
6727a66092f1SBarry Smith     j++;
6728a66092f1SBarry Smith   }
6729a66092f1SBarry Smith   PetscFunctionReturn(0);
6730a66092f1SBarry Smith }
6731a66092f1SBarry Smith 
6732387f4636SBarry Smith /*@C
6733387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6734387f4636SBarry Smith 
6735387f4636SBarry Smith    Collective on TS
6736387f4636SBarry Smith 
6737387f4636SBarry Smith    Input Parameters:
6738387f4636SBarry Smith +  ts - the TS context
6739387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
6740387f4636SBarry Smith 
674195452b02SPatrick Sanan    Notes:
674295452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67437db568b7SBarry Smith 
6744387f4636SBarry Smith    Level: intermediate
6745387f4636SBarry Smith 
6746387f4636SBarry Smith .keywords: TS,  vector, monitor, view
6747387f4636SBarry Smith 
6748387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6749387f4636SBarry Smith @*/
6750387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6751387f4636SBarry Smith {
6752a66092f1SBarry Smith   PetscInt          i;
6753387f4636SBarry Smith   PetscErrorCode    ierr;
6754387f4636SBarry Smith 
6755387f4636SBarry Smith   PetscFunctionBegin;
6756387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6757387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67585537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6759b3d3934dSBarry Smith       break;
6760b037adc7SBarry Smith     }
6761b037adc7SBarry Smith   }
6762b037adc7SBarry Smith   PetscFunctionReturn(0);
6763b037adc7SBarry Smith }
6764b037adc7SBarry Smith 
676580666b62SBarry Smith /*@C
676680666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
676780666b62SBarry Smith 
676880666b62SBarry Smith    Collective on TS
676980666b62SBarry Smith 
677080666b62SBarry Smith    Input Parameters:
677180666b62SBarry Smith +  ts - the TS context
677280666b62SBarry Smith .  transform - the transform function
67737684fa3eSBarry Smith .  destroy - function to destroy the optional context
677480666b62SBarry Smith -  ctx - optional context used by transform function
677580666b62SBarry Smith 
677695452b02SPatrick Sanan    Notes:
677795452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67787db568b7SBarry Smith 
677980666b62SBarry Smith    Level: intermediate
678080666b62SBarry Smith 
678180666b62SBarry Smith .keywords: TS,  vector, monitor, view
678280666b62SBarry Smith 
6783a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
678480666b62SBarry Smith @*/
67857684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
678680666b62SBarry Smith {
678780666b62SBarry Smith   PetscInt          i;
6788a66092f1SBarry Smith   PetscErrorCode    ierr;
678980666b62SBarry Smith 
679080666b62SBarry Smith   PetscFunctionBegin;
679180666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
679280666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67935537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
679480666b62SBarry Smith     }
679580666b62SBarry Smith   }
679680666b62SBarry Smith   PetscFunctionReturn(0);
679780666b62SBarry Smith }
679880666b62SBarry Smith 
6799e673d494SBarry Smith /*@C
6800e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6801e673d494SBarry Smith 
6802e673d494SBarry Smith    Collective on TSLGCtx
6803e673d494SBarry Smith 
6804e673d494SBarry Smith    Input Parameters:
6805e673d494SBarry Smith +  ts - the TS context
6806e673d494SBarry Smith .  transform - the transform function
68077684fa3eSBarry Smith .  destroy - function to destroy the optional context
6808e673d494SBarry Smith -  ctx - optional context used by transform function
6809e673d494SBarry Smith 
6810e673d494SBarry Smith    Level: intermediate
6811e673d494SBarry Smith 
6812e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6813e673d494SBarry Smith 
6814a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6815e673d494SBarry Smith @*/
68167684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6817e673d494SBarry Smith {
6818e673d494SBarry Smith   PetscFunctionBegin;
6819e673d494SBarry Smith   ctx->transform    = transform;
68207684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6821e673d494SBarry Smith   ctx->transformctx = tctx;
6822e673d494SBarry Smith   PetscFunctionReturn(0);
6823e673d494SBarry Smith }
6824e673d494SBarry Smith 
6825ef20d060SBarry Smith /*@C
68268b668821SLisandro Dalcin    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error
6827ef20d060SBarry Smith        in a time based line graph
6828ef20d060SBarry Smith 
6829ef20d060SBarry Smith    Collective on TS
6830ef20d060SBarry Smith 
6831ef20d060SBarry Smith    Input Parameters:
6832ef20d060SBarry Smith +  ts - the TS context
6833ef20d060SBarry Smith .  step - current time-step
6834ef20d060SBarry Smith .  ptime - current time
68357db568b7SBarry Smith .  u - current solution
68367db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6837ef20d060SBarry Smith 
6838ef20d060SBarry Smith    Level: intermediate
6839ef20d060SBarry Smith 
684095452b02SPatrick Sanan    Notes:
684195452b02SPatrick Sanan     Each process in a parallel run displays its component errors in a separate window
6842abd5a294SJed Brown 
6843abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6844abd5a294SJed Brown 
6845abd5a294SJed Brown    Options Database Keys:
68464f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6847ef20d060SBarry Smith 
6848ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
6849ef20d060SBarry Smith 
6850abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6851ef20d060SBarry Smith @*/
68520910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6853ef20d060SBarry Smith {
6854ef20d060SBarry Smith   PetscErrorCode    ierr;
68550b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6856ef20d060SBarry Smith   const PetscScalar *yy;
6857ef20d060SBarry Smith   Vec               y;
6858ef20d060SBarry Smith 
6859ef20d060SBarry Smith   PetscFunctionBegin;
6860a9f9c1f6SBarry Smith   if (!step) {
6861a9f9c1f6SBarry Smith     PetscDrawAxis axis;
68626934998bSLisandro Dalcin     PetscInt      dim;
6863a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
68648b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr);
68650910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6866a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6867a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6868a9f9c1f6SBarry Smith   }
68690910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6870ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
68710910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6872ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6873e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6874e3efe391SJed Brown   {
6875e3efe391SJed Brown     PetscReal *yreal;
6876e3efe391SJed Brown     PetscInt  i,n;
6877e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6878785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6879e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
68800b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6881e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6882e3efe391SJed Brown   }
6883e3efe391SJed Brown #else
68840b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6885e3efe391SJed Brown #endif
6886ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6887ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6888b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
68890b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
68906934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
68913923b477SBarry Smith   }
6892ef20d060SBarry Smith   PetscFunctionReturn(0);
6893ef20d060SBarry Smith }
6894ef20d060SBarry Smith 
68957cf37e64SBarry Smith /*@C
68967cf37e64SBarry Smith    TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep
68977cf37e64SBarry Smith 
68987cf37e64SBarry Smith    Collective on TS
68997cf37e64SBarry Smith 
69007cf37e64SBarry Smith    Input Parameters:
69017cf37e64SBarry Smith +  ts - the TS context
69027cf37e64SBarry Smith .  step - current time-step
69037cf37e64SBarry Smith .  ptime - current time
69047cf37e64SBarry Smith .  u - current solution
69057cf37e64SBarry Smith -  dctx - unused context
69067cf37e64SBarry Smith 
69077cf37e64SBarry Smith    Level: intermediate
69087cf37e64SBarry Smith 
69097cf37e64SBarry Smith    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
69107cf37e64SBarry Smith 
69117cf37e64SBarry Smith    Options Database Keys:
69127cf37e64SBarry Smith .  -ts_monitor_error - create a graphical monitor of error history
69137cf37e64SBarry Smith 
69147cf37e64SBarry Smith .keywords: TS,  vector, monitor, view
69157cf37e64SBarry Smith 
69167cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
69177cf37e64SBarry Smith @*/
6918edbaebb3SBarry Smith PetscErrorCode  TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
69197cf37e64SBarry Smith {
69207cf37e64SBarry Smith   PetscErrorCode    ierr;
69217cf37e64SBarry Smith   Vec               y;
69227cf37e64SBarry Smith   PetscReal         nrm;
6923edbaebb3SBarry Smith   PetscBool         flg;
69247cf37e64SBarry Smith 
69257cf37e64SBarry Smith   PetscFunctionBegin;
69267cf37e64SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
69277cf37e64SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
69287cf37e64SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6929edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr);
6930edbaebb3SBarry Smith   if (flg) {
69317cf37e64SBarry Smith     ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
6932edbaebb3SBarry Smith     ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr);
6933edbaebb3SBarry Smith   }
6934edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr);
6935edbaebb3SBarry Smith   if (flg) {
6936edbaebb3SBarry Smith     ierr = VecView(y,vf->viewer);CHKERRQ(ierr);
6937edbaebb3SBarry Smith   }
6938edbaebb3SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
69397cf37e64SBarry Smith   PetscFunctionReturn(0);
69407cf37e64SBarry Smith }
69417cf37e64SBarry Smith 
6942201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6943201da799SBarry Smith {
6944201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6945201da799SBarry Smith   PetscReal      x   = ptime,y;
6946201da799SBarry Smith   PetscErrorCode ierr;
6947201da799SBarry Smith   PetscInt       its;
6948201da799SBarry Smith 
6949201da799SBarry Smith   PetscFunctionBegin;
695063e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
6951201da799SBarry Smith   if (!n) {
6952201da799SBarry Smith     PetscDrawAxis axis;
6953201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6954201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
6955201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6956201da799SBarry Smith     ctx->snes_its = 0;
6957201da799SBarry Smith   }
6958201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
6959201da799SBarry Smith   y    = its - ctx->snes_its;
6960201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
69613fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6962201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69636934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
6964201da799SBarry Smith   }
6965201da799SBarry Smith   ctx->snes_its = its;
6966201da799SBarry Smith   PetscFunctionReturn(0);
6967201da799SBarry Smith }
6968201da799SBarry Smith 
6969201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6970201da799SBarry Smith {
6971201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6972201da799SBarry Smith   PetscReal      x   = ptime,y;
6973201da799SBarry Smith   PetscErrorCode ierr;
6974201da799SBarry Smith   PetscInt       its;
6975201da799SBarry Smith 
6976201da799SBarry Smith   PetscFunctionBegin;
697763e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
6978201da799SBarry Smith   if (!n) {
6979201da799SBarry Smith     PetscDrawAxis axis;
6980201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6981201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
6982201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6983201da799SBarry Smith     ctx->ksp_its = 0;
6984201da799SBarry Smith   }
6985201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
6986201da799SBarry Smith   y    = its - ctx->ksp_its;
6987201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
698899fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6989201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69906934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
6991201da799SBarry Smith   }
6992201da799SBarry Smith   ctx->ksp_its = its;
6993201da799SBarry Smith   PetscFunctionReturn(0);
6994201da799SBarry Smith }
6995f9c1d6abSBarry Smith 
6996f9c1d6abSBarry Smith /*@
6997f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
6998f9c1d6abSBarry Smith 
6999f9c1d6abSBarry Smith    Collective on TS and Vec
7000f9c1d6abSBarry Smith 
7001f9c1d6abSBarry Smith    Input Parameters:
7002f9c1d6abSBarry Smith +  ts - the TS context
7003f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
7004f9c1d6abSBarry Smith 
7005f9c1d6abSBarry Smith    Output Parameters:
7006f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
7007f9c1d6abSBarry Smith 
7008f9c1d6abSBarry Smith    Level: developer
7009f9c1d6abSBarry Smith 
7010f9c1d6abSBarry Smith .keywords: TS, compute
7011f9c1d6abSBarry Smith 
7012f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
7013f9c1d6abSBarry Smith @*/
7014f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7015f9c1d6abSBarry Smith {
7016f9c1d6abSBarry Smith   PetscErrorCode ierr;
7017f9c1d6abSBarry Smith 
7018f9c1d6abSBarry Smith   PetscFunctionBegin;
7019f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7020ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7021f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
7022f9c1d6abSBarry Smith   PetscFunctionReturn(0);
7023f9c1d6abSBarry Smith }
702424655328SShri 
7025b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7026b3d3934dSBarry Smith /*@C
7027b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7028b3d3934dSBarry Smith 
7029b3d3934dSBarry Smith    Collective on TS
7030b3d3934dSBarry Smith 
7031b3d3934dSBarry Smith    Input Parameters:
7032b3d3934dSBarry Smith .  ts  - the ODE solver object
7033b3d3934dSBarry Smith 
7034b3d3934dSBarry Smith    Output Parameter:
7035b3d3934dSBarry Smith .  ctx - the context
7036b3d3934dSBarry Smith 
7037b3d3934dSBarry Smith    Level: intermediate
7038b3d3934dSBarry Smith 
7039b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
7040b3d3934dSBarry Smith 
7041b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7042b3d3934dSBarry Smith 
7043b3d3934dSBarry Smith @*/
7044b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7045b3d3934dSBarry Smith {
7046b3d3934dSBarry Smith   PetscErrorCode ierr;
7047b3d3934dSBarry Smith 
7048b3d3934dSBarry Smith   PetscFunctionBegin;
7049a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7050b3d3934dSBarry Smith   PetscFunctionReturn(0);
7051b3d3934dSBarry Smith }
7052b3d3934dSBarry Smith 
7053b3d3934dSBarry Smith /*@C
7054b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7055b3d3934dSBarry Smith 
7056b3d3934dSBarry Smith    Collective on TS
7057b3d3934dSBarry Smith 
7058b3d3934dSBarry Smith    Input Parameters:
7059b3d3934dSBarry Smith +  ts - the TS context
7060b3d3934dSBarry Smith .  step - current time-step
7061b3d3934dSBarry Smith .  ptime - current time
70627db568b7SBarry Smith .  u  - current solution
70637db568b7SBarry Smith -  dctx - the envelope context
7064b3d3934dSBarry Smith 
7065b3d3934dSBarry Smith    Options Database:
7066b3d3934dSBarry Smith .  -ts_monitor_envelope
7067b3d3934dSBarry Smith 
7068b3d3934dSBarry Smith    Level: intermediate
7069b3d3934dSBarry Smith 
707095452b02SPatrick Sanan    Notes:
707195452b02SPatrick Sanan     after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7072b3d3934dSBarry Smith 
7073b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7074b3d3934dSBarry Smith 
70757db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7076b3d3934dSBarry Smith @*/
70777db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7078b3d3934dSBarry Smith {
7079b3d3934dSBarry Smith   PetscErrorCode       ierr;
70807db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7081b3d3934dSBarry Smith 
7082b3d3934dSBarry Smith   PetscFunctionBegin;
7083b3d3934dSBarry Smith   if (!ctx->max) {
7084b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7085b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7086b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7087b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7088b3d3934dSBarry Smith   } else {
7089b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7090b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7091b3d3934dSBarry Smith   }
7092b3d3934dSBarry Smith   PetscFunctionReturn(0);
7093b3d3934dSBarry Smith }
7094b3d3934dSBarry Smith 
7095b3d3934dSBarry Smith /*@C
7096b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7097b3d3934dSBarry Smith 
7098b3d3934dSBarry Smith    Collective on TS
7099b3d3934dSBarry Smith 
7100b3d3934dSBarry Smith    Input Parameter:
7101b3d3934dSBarry Smith .  ts - the TS context
7102b3d3934dSBarry Smith 
7103b3d3934dSBarry Smith    Output Parameter:
7104b3d3934dSBarry Smith +  max - the maximum values
7105b3d3934dSBarry Smith -  min - the minimum values
7106b3d3934dSBarry Smith 
710795452b02SPatrick Sanan    Notes:
710895452b02SPatrick Sanan     If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
71097db568b7SBarry Smith 
7110b3d3934dSBarry Smith    Level: intermediate
7111b3d3934dSBarry Smith 
7112b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7113b3d3934dSBarry Smith 
7114b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7115b3d3934dSBarry Smith @*/
7116b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7117b3d3934dSBarry Smith {
7118b3d3934dSBarry Smith   PetscInt i;
7119b3d3934dSBarry Smith 
7120b3d3934dSBarry Smith   PetscFunctionBegin;
7121b3d3934dSBarry Smith   if (max) *max = NULL;
7122b3d3934dSBarry Smith   if (min) *min = NULL;
7123b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7124b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
71255537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7126b3d3934dSBarry Smith       if (max) *max = ctx->max;
7127b3d3934dSBarry Smith       if (min) *min = ctx->min;
7128b3d3934dSBarry Smith       break;
7129b3d3934dSBarry Smith     }
7130b3d3934dSBarry Smith   }
7131b3d3934dSBarry Smith   PetscFunctionReturn(0);
7132b3d3934dSBarry Smith }
7133b3d3934dSBarry Smith 
7134b3d3934dSBarry Smith /*@C
7135b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7136b3d3934dSBarry Smith 
7137b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7138b3d3934dSBarry Smith 
7139b3d3934dSBarry Smith    Input Parameter:
7140b3d3934dSBarry Smith .  ctx - the monitor context
7141b3d3934dSBarry Smith 
7142b3d3934dSBarry Smith    Level: intermediate
7143b3d3934dSBarry Smith 
7144b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
7145b3d3934dSBarry Smith 
71467db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7147b3d3934dSBarry Smith @*/
7148b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7149b3d3934dSBarry Smith {
7150b3d3934dSBarry Smith   PetscErrorCode ierr;
7151b3d3934dSBarry Smith 
7152b3d3934dSBarry Smith   PetscFunctionBegin;
7153b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7154b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7155b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7156b3d3934dSBarry Smith   PetscFunctionReturn(0);
7157b3d3934dSBarry Smith }
7158f2dee214SBarry Smith 
715924655328SShri /*@
7160dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
7161dcb233daSLisandro Dalcin 
7162dcb233daSLisandro Dalcin    Collective on TS
7163dcb233daSLisandro Dalcin 
7164dcb233daSLisandro Dalcin    Input Parameter:
7165dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
7166dcb233daSLisandro Dalcin 
7167dcb233daSLisandro Dalcin    Level: advanced
7168dcb233daSLisandro Dalcin 
7169dcb233daSLisandro Dalcin    Notes:
7170dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
7171dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
7172dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
7173dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
7174dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
7175dcb233daSLisandro Dalcin    discontinuous source terms).
7176dcb233daSLisandro Dalcin 
7177dcb233daSLisandro Dalcin .keywords: TS, timestep, restart
7178dcb233daSLisandro Dalcin 
7179dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
7180dcb233daSLisandro Dalcin @*/
7181dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
7182dcb233daSLisandro Dalcin {
7183dcb233daSLisandro Dalcin   PetscFunctionBegin;
7184dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7185dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
7186dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
7187dcb233daSLisandro Dalcin }
7188dcb233daSLisandro Dalcin 
7189dcb233daSLisandro Dalcin /*@
719024655328SShri    TSRollBack - Rolls back one time step
719124655328SShri 
719224655328SShri    Collective on TS
719324655328SShri 
719424655328SShri    Input Parameter:
719524655328SShri .  ts - the TS context obtained from TSCreate()
719624655328SShri 
719724655328SShri    Level: advanced
719824655328SShri 
719924655328SShri .keywords: TS, timestep, rollback
720024655328SShri 
720124655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
720224655328SShri @*/
720324655328SShri PetscErrorCode  TSRollBack(TS ts)
720424655328SShri {
720524655328SShri   PetscErrorCode ierr;
720624655328SShri 
720724655328SShri   PetscFunctionBegin;
720824655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7209b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
721024655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
721124655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
721224655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
721324655328SShri   ts->ptime = ts->ptime_prev;
7214be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
72152808aa04SLisandro Dalcin   ts->steps--;
7216b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
721724655328SShri   PetscFunctionReturn(0);
721824655328SShri }
7219aeb4809dSShri Abhyankar 
7220ff22ae23SHong Zhang /*@
7221ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7222ff22ae23SHong Zhang 
7223ff22ae23SHong Zhang    Input Parameter:
7224ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7225ff22ae23SHong Zhang 
72260429704eSStefano Zampini    Output Parameters:
72270429704eSStefano Zampini +  ns - the number of stages
72280429704eSStefano Zampini -  Y - the current stage vectors
72290429704eSStefano Zampini 
7230ff22ae23SHong Zhang    Level: advanced
7231ff22ae23SHong Zhang 
72320429704eSStefano Zampini    Notes: Both ns and Y can be NULL.
72330429704eSStefano Zampini 
7234ff22ae23SHong Zhang .keywords: TS, getstages
7235ff22ae23SHong Zhang 
7236ff22ae23SHong Zhang .seealso: TSCreate()
7237ff22ae23SHong Zhang @*/
7238ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7239ff22ae23SHong Zhang {
7240ff22ae23SHong Zhang   PetscErrorCode ierr;
7241ff22ae23SHong Zhang 
7242ff22ae23SHong Zhang   PetscFunctionBegin;
7243ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
72440429704eSStefano Zampini   if (ns) PetscValidPointer(ns,2);
72450429704eSStefano Zampini   if (Y) PetscValidPointer(Y,3);
72460429704eSStefano Zampini   if (!ts->ops->getstages) {
72470429704eSStefano Zampini     if (ns) *ns = 0;
72480429704eSStefano Zampini     if (Y) *Y = NULL;
72490429704eSStefano Zampini   } else {
7250ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7251ff22ae23SHong Zhang   }
7252ff22ae23SHong Zhang   PetscFunctionReturn(0);
7253ff22ae23SHong Zhang }
7254ff22ae23SHong Zhang 
7255847ff0e1SMatthew G. Knepley /*@C
7256847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7257847ff0e1SMatthew G. Knepley 
7258847ff0e1SMatthew G. Knepley   Collective on SNES
7259847ff0e1SMatthew G. Knepley 
7260847ff0e1SMatthew G. Knepley   Input Parameters:
7261847ff0e1SMatthew G. Knepley + ts - the TS context
7262847ff0e1SMatthew G. Knepley . t - current timestep
7263847ff0e1SMatthew G. Knepley . U - state vector
7264847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7265847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7266847ff0e1SMatthew G. Knepley - ctx - an optional user context
7267847ff0e1SMatthew G. Knepley 
7268847ff0e1SMatthew G. Knepley   Output Parameters:
7269847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7270847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7271847ff0e1SMatthew G. Knepley 
7272847ff0e1SMatthew G. Knepley   Level: intermediate
7273847ff0e1SMatthew G. Knepley 
7274847ff0e1SMatthew G. Knepley   Notes:
7275847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7276847ff0e1SMatthew G. Knepley 
7277847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7278847ff0e1SMatthew G. Knepley 
7279847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7280847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7281847ff0e1SMatthew G. Knepley 
7282847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7283847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7284847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7285847ff0e1SMatthew G. Knepley 
7286847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
7287847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7288847ff0e1SMatthew G. Knepley @*/
7289847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7290847ff0e1SMatthew G. Knepley {
7291847ff0e1SMatthew G. Knepley   SNES           snes;
7292847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7293847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7294847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7295847ff0e1SMatthew G. Knepley 
7296847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7297c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7298847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7299847ff0e1SMatthew G. Knepley   if (!color) {
7300847ff0e1SMatthew G. Knepley     DM         dm;
7301847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7302847ff0e1SMatthew G. Knepley 
7303847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7304847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7305847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7306847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7307847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7308847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7309847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7310847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7311847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7312847ff0e1SMatthew G. Knepley     } else {
7313847ff0e1SMatthew G. Knepley       MatColoring mc;
7314847ff0e1SMatthew G. Knepley 
7315847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7316847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7317847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7318847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7319847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7320847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7321847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7322847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7323847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7324847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7325847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7326847ff0e1SMatthew G. Knepley     }
7327847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7328847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7329847ff0e1SMatthew G. Knepley   }
7330847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7331847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7332847ff0e1SMatthew G. Knepley   if (J != B) {
7333847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7334847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7335847ff0e1SMatthew G. Knepley   }
7336847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7337847ff0e1SMatthew G. Knepley }
733893b34091SDebojyoti Ghosh 
7339cb9d8021SPierre Barbier de Reuille /*@
7340cb9d8021SPierre Barbier de Reuille     TSSetFunctionDomainError - Set the function testing if the current state vector is valid
7341cb9d8021SPierre Barbier de Reuille 
7342cb9d8021SPierre Barbier de Reuille     Input Parameters:
7343cb9d8021SPierre Barbier de Reuille     ts - the TS context
7344cb9d8021SPierre Barbier de Reuille     func - function called within TSFunctionDomainError
7345cb9d8021SPierre Barbier de Reuille 
7346cb9d8021SPierre Barbier de Reuille     Level: intermediate
7347cb9d8021SPierre Barbier de Reuille 
7348cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain
7349cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError()
7350cb9d8021SPierre Barbier de Reuille @*/
7351cb9d8021SPierre Barbier de Reuille 
7352d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7353cb9d8021SPierre Barbier de Reuille {
7354cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7355cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7356cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7357cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7358cb9d8021SPierre Barbier de Reuille }
7359cb9d8021SPierre Barbier de Reuille 
7360cb9d8021SPierre Barbier de Reuille /*@
7361cb9d8021SPierre Barbier de Reuille     TSFunctionDomainError - Check if the current state is valid
7362cb9d8021SPierre Barbier de Reuille 
7363cb9d8021SPierre Barbier de Reuille     Input Parameters:
7364cb9d8021SPierre Barbier de Reuille     ts - the TS context
7365cb9d8021SPierre Barbier de Reuille     stagetime - time of the simulation
7366d183316bSPierre Barbier de Reuille     Y - state vector to check.
7367cb9d8021SPierre Barbier de Reuille 
7368cb9d8021SPierre Barbier de Reuille     Output Parameter:
7369cb9d8021SPierre Barbier de Reuille     accept - Set to PETSC_FALSE if the current state vector is valid.
7370cb9d8021SPierre Barbier de Reuille 
7371cb9d8021SPierre Barbier de Reuille     Note:
7372cb9d8021SPierre Barbier de Reuille     This function should be used to ensure the state is in a valid part of the space.
7373cb9d8021SPierre Barbier de Reuille     For example, one can ensure here all values are positive.
737496a0c994SBarry Smith 
737596a0c994SBarry Smith     Level: advanced
7376cb9d8021SPierre Barbier de Reuille @*/
7377d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7378cb9d8021SPierre Barbier de Reuille {
7379cb9d8021SPierre Barbier de Reuille   PetscErrorCode ierr;
7380cb9d8021SPierre Barbier de Reuille 
7381cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7382cb9d8021SPierre Barbier de Reuille 
7383cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7384cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7385cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7386d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7387cb9d8021SPierre Barbier de Reuille   }
7388cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7389cb9d8021SPierre Barbier de Reuille }
73901ceb14c0SBarry Smith 
739193b34091SDebojyoti Ghosh /*@C
7392e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
739393b34091SDebojyoti Ghosh 
739493b34091SDebojyoti Ghosh   Collective on MPI_Comm
739593b34091SDebojyoti Ghosh 
739693b34091SDebojyoti Ghosh   Input Parameter:
739793b34091SDebojyoti Ghosh . tsin    - The input TS
739893b34091SDebojyoti Ghosh 
739993b34091SDebojyoti Ghosh   Output Parameter:
7400e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
740193b34091SDebojyoti Ghosh 
74025eca1a21SEmil Constantinescu   Notes:
74035eca1a21SEmil 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.
74045eca1a21SEmil Constantinescu 
7405928bb9adSStefano 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);
74065eca1a21SEmil Constantinescu 
74075eca1a21SEmil Constantinescu   Level: developer
740893b34091SDebojyoti Ghosh 
7409e5168f73SEmil Constantinescu .keywords: TS, clone
7410e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
741193b34091SDebojyoti Ghosh @*/
7412baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
741393b34091SDebojyoti Ghosh {
741493b34091SDebojyoti Ghosh   TS             t;
741593b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7416dc846ba4SSatish Balay   SNES           snes_start;
7417dc846ba4SSatish Balay   DM             dm;
7418dc846ba4SSatish Balay   TSType         type;
741993b34091SDebojyoti Ghosh 
742093b34091SDebojyoti Ghosh   PetscFunctionBegin;
742193b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
742293b34091SDebojyoti Ghosh   *tsout = NULL;
742393b34091SDebojyoti Ghosh 
74247a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
742593b34091SDebojyoti Ghosh 
742693b34091SDebojyoti Ghosh   /* General TS description */
742793b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
742893b34091SDebojyoti Ghosh   t->setupcalled       = 0;
742993b34091SDebojyoti Ghosh   t->ksp_its           = 0;
743093b34091SDebojyoti Ghosh   t->snes_its          = 0;
743193b34091SDebojyoti Ghosh   t->nwork             = 0;
743293b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
743393b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
743493b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
743593b34091SDebojyoti Ghosh 
743634561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
743734561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7438d15a3a53SEmil Constantinescu 
743993b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
744093b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
744193b34091SDebojyoti Ghosh 
744293b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
744351699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
744493b34091SDebojyoti Ghosh 
7445e7069c78SShri   t->trajectory = tsin->trajectory;
7446e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7447e7069c78SShri 
7448e7069c78SShri   t->event = tsin->event;
74496b10a48eSSatish Balay   if (t->event) t->event->refct++;
7450e7069c78SShri 
745193b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
745293b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7453e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
745493b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
745593b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
745693b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
745793b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
745893b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
745993b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
746093b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
746193b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
746293b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
746393b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
746493b34091SDebojyoti Ghosh 
746593b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
746693b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
746793b34091SDebojyoti Ghosh 
746893b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
746993b34091SDebojyoti Ghosh 
747093b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
747193b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
747293b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
747393b34091SDebojyoti Ghosh 
747493b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
747593b34091SDebojyoti Ghosh 
74760d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
74770d4fed19SBarry Smith     PetscInt i;
74780d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
74790d4fed19SBarry Smith     for (i=0; i<10; i++) {
74800d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
74810d4fed19SBarry Smith     }
74820d4fed19SBarry Smith   }
748393b34091SDebojyoti Ghosh   *tsout = t;
748493b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
748593b34091SDebojyoti Ghosh }
7486f3b1f45cSBarry Smith 
7487f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
7488f3b1f45cSBarry Smith {
7489f3b1f45cSBarry Smith   PetscErrorCode ierr;
7490f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
7491f3b1f45cSBarry Smith 
7492f3b1f45cSBarry Smith   PetscFunctionBegin;
7493f3b1f45cSBarry Smith   ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr);
7494f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7495f3b1f45cSBarry Smith }
7496f3b1f45cSBarry Smith 
7497f3b1f45cSBarry Smith /*@
7498f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
7499f3b1f45cSBarry Smith 
7500f3b1f45cSBarry Smith    Logically Collective on TS and Mat
7501f3b1f45cSBarry Smith 
7502f3b1f45cSBarry Smith     Input Parameters:
7503f3b1f45cSBarry Smith     TS - the time stepping routine
7504f3b1f45cSBarry Smith 
7505f3b1f45cSBarry Smith    Output Parameter:
7506f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7507f3b1f45cSBarry Smith 
7508f3b1f45cSBarry Smith    Options Database:
7509f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
7510f3b1f45cSBarry Smith 
7511f3b1f45cSBarry Smith    Level: advanced
7512f3b1f45cSBarry Smith 
751395452b02SPatrick Sanan    Notes:
751495452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7515f3b1f45cSBarry Smith 
7516f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
7517f3b1f45cSBarry Smith @*/
7518f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
7519f3b1f45cSBarry Smith {
7520f3b1f45cSBarry Smith   Mat            J,B;
7521f3b1f45cSBarry Smith   PetscErrorCode ierr;
7522f3b1f45cSBarry Smith   TSRHSJacobian  func;
7523f3b1f45cSBarry Smith   void*          ctx;
7524f3b1f45cSBarry Smith 
7525f3b1f45cSBarry Smith   PetscFunctionBegin;
7526f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7527f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7528f3b1f45cSBarry Smith   ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7529f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7530f3b1f45cSBarry Smith }
7531f3b1f45cSBarry Smith 
7532f3b1f45cSBarry Smith /*@C
7533f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
7534f3b1f45cSBarry Smith 
7535f3b1f45cSBarry Smith    Logically Collective on TS and Mat
7536f3b1f45cSBarry Smith 
7537f3b1f45cSBarry Smith     Input Parameters:
7538f3b1f45cSBarry Smith     TS - the time stepping routine
7539f3b1f45cSBarry Smith 
7540f3b1f45cSBarry Smith    Output Parameter:
7541f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7542f3b1f45cSBarry Smith 
7543f3b1f45cSBarry Smith    Options Database:
7544f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
7545f3b1f45cSBarry Smith 
754695452b02SPatrick Sanan    Notes:
754795452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7548f3b1f45cSBarry Smith 
7549f3b1f45cSBarry Smith    Level: advanced
7550f3b1f45cSBarry Smith 
7551f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
7552f3b1f45cSBarry Smith @*/
7553f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
7554f3b1f45cSBarry Smith {
7555f3b1f45cSBarry Smith   Mat            J,B;
7556f3b1f45cSBarry Smith   PetscErrorCode ierr;
7557f3b1f45cSBarry Smith   void           *ctx;
7558f3b1f45cSBarry Smith   TSRHSJacobian  func;
7559f3b1f45cSBarry Smith 
7560f3b1f45cSBarry Smith   PetscFunctionBegin;
7561f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7562f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7563f3b1f45cSBarry Smith   ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7564f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7565f3b1f45cSBarry Smith }
7566