xref: /petsc/src/ts/interface/ts.c (revision 2d29f1f2de9afb4cc7d2cd7d474594898662f93c)
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 
470*2d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve()
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 /*@
487*2d29f1f2SStefano Zampini    TSResetTrajectory - Destroys and recreates the internal TSTrajectory object
488*2d29f1f2SStefano Zampini 
489*2d29f1f2SStefano Zampini    Collective on TS
490*2d29f1f2SStefano Zampini 
491*2d29f1f2SStefano Zampini    Input Parameters:
492*2d29f1f2SStefano Zampini .  ts - the TS context obtained from TSCreate()
493*2d29f1f2SStefano Zampini 
494*2d29f1f2SStefano Zampini    Level: intermediate
495*2d29f1f2SStefano Zampini 
496*2d29f1f2SStefano Zampini .seealso: TSGetTrajectory(), TSAdjointSolve()
497*2d29f1f2SStefano Zampini 
498*2d29f1f2SStefano Zampini .keywords: TS, set, checkpoint,
499*2d29f1f2SStefano Zampini @*/
500*2d29f1f2SStefano Zampini PetscErrorCode  TSResetTrajectory(TS ts)
501*2d29f1f2SStefano Zampini {
502*2d29f1f2SStefano Zampini   PetscErrorCode ierr;
503*2d29f1f2SStefano Zampini 
504*2d29f1f2SStefano Zampini   PetscFunctionBegin;
505*2d29f1f2SStefano Zampini   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
506*2d29f1f2SStefano Zampini   if (ts->trajectory) {
507*2d29f1f2SStefano Zampini     ierr = TSTrajectoryDestroy(&ts->trajectory);CHKERRQ(ierr);
508*2d29f1f2SStefano Zampini     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
509*2d29f1f2SStefano Zampini   }
510*2d29f1f2SStefano Zampini   PetscFunctionReturn(0);
511*2d29f1f2SStefano Zampini }
512*2d29f1f2SStefano Zampini 
513*2d29f1f2SStefano Zampini /*@
514a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
515a7a1495cSBarry Smith       set with TSSetRHSJacobian().
516a7a1495cSBarry Smith 
517a7a1495cSBarry Smith    Collective on TS and Vec
518a7a1495cSBarry Smith 
519a7a1495cSBarry Smith    Input Parameters:
520316643e7SJed Brown +  ts - the TS context
521a7a1495cSBarry Smith .  t - current timestep
5220910c330SBarry Smith -  U - input vector
523a7a1495cSBarry Smith 
524a7a1495cSBarry Smith    Output Parameters:
525a7a1495cSBarry Smith +  A - Jacobian matrix
526a7a1495cSBarry Smith .  B - optional preconditioning matrix
527a7a1495cSBarry Smith -  flag - flag indicating matrix structure
528a7a1495cSBarry Smith 
529a7a1495cSBarry Smith    Notes:
530a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
531a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
532a7a1495cSBarry Smith 
53394b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
534a7a1495cSBarry Smith    flag parameter.
535a7a1495cSBarry Smith 
536a7a1495cSBarry Smith    Level: developer
537a7a1495cSBarry Smith 
538a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
539a7a1495cSBarry Smith 
54094b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
541a7a1495cSBarry Smith @*/
542d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
543a7a1495cSBarry Smith {
544dfbe8321SBarry Smith   PetscErrorCode   ierr;
545270bf2e7SJed Brown   PetscObjectState Ustate;
5466c1e1eecSBarry Smith   PetscObjectId    Uid;
54724989b8cSPeter Brune   DM               dm;
548942e3340SBarry Smith   DMTS             tsdm;
54924989b8cSPeter Brune   TSRHSJacobian    rhsjacobianfunc;
55024989b8cSPeter Brune   void             *ctx;
55124989b8cSPeter Brune   TSIJacobian      ijacobianfunc;
552b2df71adSDebojyoti Ghosh   TSRHSFunction    rhsfunction;
553a7a1495cSBarry Smith 
554a7a1495cSBarry Smith   PetscFunctionBegin;
5550700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5560910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5570910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
55824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
559942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
56024989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
5610298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
562b2df71adSDebojyoti Ghosh   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
56359e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
5646c1e1eecSBarry Smith   ierr = PetscObjectGetId((PetscObject)U,&Uid);CHKERRQ(ierr);
565971015bcSStefano Zampini 
5666c1e1eecSBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
567971015bcSStefano Zampini     /* restore back RHS Jacobian matrices if they have been shifted/scaled */
568971015bcSStefano Zampini     if (A == ts->Arhs) {
569971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
570971015bcSStefano Zampini         ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
571971015bcSStefano Zampini       }
572971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
573971015bcSStefano Zampini         ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
574971015bcSStefano Zampini       }
575971015bcSStefano Zampini     }
576971015bcSStefano Zampini     if (B && B == ts->Brhs && A != B) {
577971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
578971015bcSStefano Zampini         ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
579971015bcSStefano Zampini       }
580971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
581971015bcSStefano Zampini         ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
582971015bcSStefano Zampini       }
583971015bcSStefano Zampini     }
584971015bcSStefano Zampini     ts->rhsjacobian.shift = 0;
585971015bcSStefano Zampini     ts->rhsjacobian.scale = 1.;
5860e4ef248SJed Brown     PetscFunctionReturn(0);
587f8ede8e7SJed Brown   }
588d90be118SSean Farley 
589ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
590d90be118SSean Farley 
591e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
592971015bcSStefano Zampini     if (A == ts->Arhs) {
593971015bcSStefano Zampini       /* MatScale has a short path for this case.
594971015bcSStefano Zampini          However, this code path is taken the first time TSComputeRHSJacobian is called
595971015bcSStefano Zampini          and the matrices have not assembled yet */
596971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
59794ab13aaSBarry Smith         ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
598971015bcSStefano Zampini       }
599971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
60094ab13aaSBarry Smith         ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
601971015bcSStefano Zampini       }
602971015bcSStefano Zampini     }
603971015bcSStefano Zampini     if (B && B == ts->Brhs && A != B) {
604971015bcSStefano Zampini       if (ts->rhsjacobian.shift != 0) {
60594ab13aaSBarry Smith         ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
606971015bcSStefano Zampini       }
607971015bcSStefano Zampini       if (ts->rhsjacobian.scale != 1.) {
60894ab13aaSBarry Smith         ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
609e1244c69SJed Brown       }
610971015bcSStefano Zampini     }
611e1244c69SJed Brown   }
612e1244c69SJed Brown 
61324989b8cSPeter Brune   if (rhsjacobianfunc) {
6146cd88445SBarry Smith     PetscBool missing;
61594ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
616a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
617d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
618a7a1495cSBarry Smith     PetscStackPop;
61994ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
6206cd88445SBarry Smith     if (A) {
6216cd88445SBarry Smith       ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
6226cd88445SBarry 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");
6236cd88445SBarry Smith     }
6246cd88445SBarry Smith     if (B && B != A) {
6256cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
6266cd88445SBarry 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");
6276cd88445SBarry Smith     }
628ef66eb69SBarry Smith   } else {
62994ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
630971015bcSStefano Zampini     if (B && A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
631ef66eb69SBarry Smith   }
6320e4ef248SJed Brown   ts->rhsjacobian.time  = t;
633971015bcSStefano Zampini   ts->rhsjacobian.shift = 0;
634971015bcSStefano Zampini   ts->rhsjacobian.scale = 1.;
6357f79407eSBarry Smith   ierr                  = PetscObjectGetId((PetscObject)U,&ts->rhsjacobian.Xid);CHKERRQ(ierr);
63659e4f3c8SBarry Smith   ierr                  = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
637a7a1495cSBarry Smith   PetscFunctionReturn(0);
638a7a1495cSBarry Smith }
639a7a1495cSBarry Smith 
640316643e7SJed Brown /*@
641d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
642d763cef2SBarry Smith 
643316643e7SJed Brown    Collective on TS and Vec
644316643e7SJed Brown 
645316643e7SJed Brown    Input Parameters:
646316643e7SJed Brown +  ts - the TS context
647316643e7SJed Brown .  t - current time
6480910c330SBarry Smith -  U - state vector
649316643e7SJed Brown 
650316643e7SJed Brown    Output Parameter:
651316643e7SJed Brown .  y - right hand side
652316643e7SJed Brown 
653316643e7SJed Brown    Note:
654316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
655316643e7SJed Brown    is used internally within the nonlinear solvers.
656316643e7SJed Brown 
657316643e7SJed Brown    Level: developer
658316643e7SJed Brown 
659316643e7SJed Brown .keywords: TS, compute
660316643e7SJed Brown 
661316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
662316643e7SJed Brown @*/
6630910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
664d763cef2SBarry Smith {
665dfbe8321SBarry Smith   PetscErrorCode ierr;
66624989b8cSPeter Brune   TSRHSFunction  rhsfunction;
66724989b8cSPeter Brune   TSIFunction    ifunction;
66824989b8cSPeter Brune   void           *ctx;
66924989b8cSPeter Brune   DM             dm;
67024989b8cSPeter Brune 
671d763cef2SBarry Smith   PetscFunctionBegin;
6720700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6730910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6740700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
67524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
67624989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
6770298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
678d763cef2SBarry Smith 
679ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
680d763cef2SBarry Smith 
6810910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
68224989b8cSPeter Brune   if (rhsfunction) {
683d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
6840910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
685d763cef2SBarry Smith     PetscStackPop;
686214bc6a2SJed Brown   } else {
687214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
688b2cd27e8SJed Brown   }
68944a41b28SSean Farley 
6900910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
691d763cef2SBarry Smith   PetscFunctionReturn(0);
692d763cef2SBarry Smith }
693d763cef2SBarry Smith 
694ef20d060SBarry Smith /*@
695ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
696ef20d060SBarry Smith 
697ef20d060SBarry Smith    Collective on TS and Vec
698ef20d060SBarry Smith 
699ef20d060SBarry Smith    Input Parameters:
700ef20d060SBarry Smith +  ts - the TS context
701ef20d060SBarry Smith -  t - current time
702ef20d060SBarry Smith 
703ef20d060SBarry Smith    Output Parameter:
7040910c330SBarry Smith .  U - the solution
705ef20d060SBarry Smith 
706ef20d060SBarry Smith    Note:
707ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
708ef20d060SBarry Smith    is used internally within the nonlinear solvers.
709ef20d060SBarry Smith 
710ef20d060SBarry Smith    Level: developer
711ef20d060SBarry Smith 
712ef20d060SBarry Smith .keywords: TS, compute
713ef20d060SBarry Smith 
714abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
715ef20d060SBarry Smith @*/
7160910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
717ef20d060SBarry Smith {
718ef20d060SBarry Smith   PetscErrorCode     ierr;
719ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
720ef20d060SBarry Smith   void               *ctx;
721ef20d060SBarry Smith   DM                 dm;
722ef20d060SBarry Smith 
723ef20d060SBarry Smith   PetscFunctionBegin;
724ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7250910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
726ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
727ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
728ef20d060SBarry Smith 
729ef20d060SBarry Smith   if (solutionfunction) {
7309b7cd975SBarry Smith     PetscStackPush("TS user solution function");
7310910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
732ef20d060SBarry Smith     PetscStackPop;
733ef20d060SBarry Smith   }
734ef20d060SBarry Smith   PetscFunctionReturn(0);
735ef20d060SBarry Smith }
7369b7cd975SBarry Smith /*@
7379b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
7389b7cd975SBarry Smith 
7399b7cd975SBarry Smith    Collective on TS and Vec
7409b7cd975SBarry Smith 
7419b7cd975SBarry Smith    Input Parameters:
7429b7cd975SBarry Smith +  ts - the TS context
7439b7cd975SBarry Smith -  t - current time
7449b7cd975SBarry Smith 
7459b7cd975SBarry Smith    Output Parameter:
7469b7cd975SBarry Smith .  U - the function value
7479b7cd975SBarry Smith 
7489b7cd975SBarry Smith    Note:
7499b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
7509b7cd975SBarry Smith    is used internally within the nonlinear solvers.
7519b7cd975SBarry Smith 
7529b7cd975SBarry Smith    Level: developer
7539b7cd975SBarry Smith 
7549b7cd975SBarry Smith .keywords: TS, compute
7559b7cd975SBarry Smith 
7569b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
7579b7cd975SBarry Smith @*/
7589b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
7599b7cd975SBarry Smith {
7609b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
7619b7cd975SBarry Smith   void               *ctx;
7629b7cd975SBarry Smith   DM                 dm;
7639b7cd975SBarry Smith 
7649b7cd975SBarry Smith   PetscFunctionBegin;
7659b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7669b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7679b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
7689b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
7699b7cd975SBarry Smith 
7709b7cd975SBarry Smith   if (forcing) {
7719b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
7729b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
7739b7cd975SBarry Smith     PetscStackPop;
7749b7cd975SBarry Smith   }
7759b7cd975SBarry Smith   PetscFunctionReturn(0);
7769b7cd975SBarry Smith }
777ef20d060SBarry Smith 
778214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
779214bc6a2SJed Brown {
7802dd45cf8SJed Brown   Vec            F;
781214bc6a2SJed Brown   PetscErrorCode ierr;
782214bc6a2SJed Brown 
783214bc6a2SJed Brown   PetscFunctionBegin;
7840298fd71SBarry Smith   *Frhs = NULL;
7850298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
786214bc6a2SJed Brown   if (!ts->Frhs) {
7872dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
788214bc6a2SJed Brown   }
789214bc6a2SJed Brown   *Frhs = ts->Frhs;
790214bc6a2SJed Brown   PetscFunctionReturn(0);
791214bc6a2SJed Brown }
792214bc6a2SJed Brown 
793971015bcSStefano Zampini PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
794214bc6a2SJed Brown {
795214bc6a2SJed Brown   Mat            A,B;
7962dd45cf8SJed Brown   PetscErrorCode ierr;
79741a1d4d2SBarry Smith   TSIJacobian    ijacobian;
798214bc6a2SJed Brown 
799214bc6a2SJed Brown   PetscFunctionBegin;
800c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
801c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
80241a1d4d2SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,&ijacobian,NULL);CHKERRQ(ierr);
803214bc6a2SJed Brown   if (Arhs) {
804214bc6a2SJed Brown     if (!ts->Arhs) {
80541a1d4d2SBarry Smith       if (ijacobian) {
806214bc6a2SJed Brown         ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
80741a1d4d2SBarry Smith       } else {
80841a1d4d2SBarry Smith         ts->Arhs = A;
80941a1d4d2SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
81041a1d4d2SBarry Smith       }
8113565c898SBarry Smith     } else {
8123565c898SBarry Smith       PetscBool flg;
8133565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
8143565c898SBarry Smith       /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */
8153565c898SBarry Smith       if (flg && !ijacobian && ts->Arhs == ts->Brhs){
8163565c898SBarry Smith         ierr = PetscObjectDereference((PetscObject)ts->Arhs);CHKERRQ(ierr);
8173565c898SBarry Smith         ts->Arhs = A;
8183565c898SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
8193565c898SBarry Smith       }
820214bc6a2SJed Brown     }
821214bc6a2SJed Brown     *Arhs = ts->Arhs;
822214bc6a2SJed Brown   }
823214bc6a2SJed Brown   if (Brhs) {
824214bc6a2SJed Brown     if (!ts->Brhs) {
825bdb70873SJed Brown       if (A != B) {
82641a1d4d2SBarry Smith         if (ijacobian) {
827214bc6a2SJed Brown           ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
828bdb70873SJed Brown         } else {
82941a1d4d2SBarry Smith           ts->Brhs = B;
83041a1d4d2SBarry Smith           ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
83141a1d4d2SBarry Smith         }
83241a1d4d2SBarry Smith       } else {
833bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
83451699248SLisandro Dalcin         ts->Brhs = ts->Arhs;
835bdb70873SJed Brown       }
836214bc6a2SJed Brown     }
837214bc6a2SJed Brown     *Brhs = ts->Brhs;
838214bc6a2SJed Brown   }
839214bc6a2SJed Brown   PetscFunctionReturn(0);
840214bc6a2SJed Brown }
841214bc6a2SJed Brown 
842316643e7SJed Brown /*@
8430910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
844316643e7SJed Brown 
845316643e7SJed Brown    Collective on TS and Vec
846316643e7SJed Brown 
847316643e7SJed Brown    Input Parameters:
848316643e7SJed Brown +  ts - the TS context
849316643e7SJed Brown .  t - current time
8500910c330SBarry Smith .  U - state vector
8510910c330SBarry Smith .  Udot - time derivative of state vector
852214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
853316643e7SJed Brown 
854316643e7SJed Brown    Output Parameter:
855316643e7SJed Brown .  Y - right hand side
856316643e7SJed Brown 
857316643e7SJed Brown    Note:
858316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
859316643e7SJed Brown    is used internally within the nonlinear solvers.
860316643e7SJed Brown 
861316643e7SJed Brown    If the user did did not write their equations in implicit form, this
862316643e7SJed Brown    function recasts them in implicit form.
863316643e7SJed Brown 
864316643e7SJed Brown    Level: developer
865316643e7SJed Brown 
866316643e7SJed Brown .keywords: TS, compute
867316643e7SJed Brown 
868316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
869316643e7SJed Brown @*/
8700910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
871316643e7SJed Brown {
872316643e7SJed Brown   PetscErrorCode ierr;
87324989b8cSPeter Brune   TSIFunction    ifunction;
87424989b8cSPeter Brune   TSRHSFunction  rhsfunction;
87524989b8cSPeter Brune   void           *ctx;
87624989b8cSPeter Brune   DM             dm;
877316643e7SJed Brown 
878316643e7SJed Brown   PetscFunctionBegin;
8790700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
8800910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
8810910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
8820700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
883316643e7SJed Brown 
88424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
88524989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
8860298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
88724989b8cSPeter Brune 
888ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
889d90be118SSean Farley 
8900910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
89124989b8cSPeter Brune   if (ifunction) {
892316643e7SJed Brown     PetscStackPush("TS user implicit function");
8930910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
894316643e7SJed Brown     PetscStackPop;
895214bc6a2SJed Brown   }
896214bc6a2SJed Brown   if (imex) {
89724989b8cSPeter Brune     if (!ifunction) {
8980910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
8992dd45cf8SJed Brown     }
90024989b8cSPeter Brune   } else if (rhsfunction) {
90124989b8cSPeter Brune     if (ifunction) {
902214bc6a2SJed Brown       Vec Frhs;
903214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
9040910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
905214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
9062dd45cf8SJed Brown     } else {
9070910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
9080910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
909316643e7SJed Brown     }
9104a6899ffSJed Brown   }
9110910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
912316643e7SJed Brown   PetscFunctionReturn(0);
913316643e7SJed Brown }
914316643e7SJed Brown 
915316643e7SJed Brown /*@
916316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
917316643e7SJed Brown 
918316643e7SJed Brown    Collective on TS and Vec
919316643e7SJed Brown 
920316643e7SJed Brown    Input
921316643e7SJed Brown       Input Parameters:
922316643e7SJed Brown +  ts - the TS context
923316643e7SJed Brown .  t - current timestep
9240910c330SBarry Smith .  U - state vector
9250910c330SBarry Smith .  Udot - time derivative of state vector
926214bc6a2SJed Brown .  shift - shift to apply, see note below
927214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
928316643e7SJed Brown 
929316643e7SJed Brown    Output Parameters:
930316643e7SJed Brown +  A - Jacobian matrix
9313565c898SBarry Smith -  B - matrix from which the preconditioner is constructed; often the same as A
932316643e7SJed Brown 
933316643e7SJed Brown    Notes:
9340910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
935316643e7SJed Brown 
9360910c330SBarry Smith    dF/dU + shift*dF/dUdot
937316643e7SJed Brown 
938316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
939316643e7SJed Brown    is used internally within the nonlinear solvers.
940316643e7SJed Brown 
941316643e7SJed Brown    Level: developer
942316643e7SJed Brown 
943316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
944316643e7SJed Brown 
945316643e7SJed Brown .seealso:  TSSetIJacobian()
94663495f91SJed Brown @*/
947d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
948316643e7SJed Brown {
949316643e7SJed Brown   PetscErrorCode ierr;
95024989b8cSPeter Brune   TSIJacobian    ijacobian;
95124989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
95224989b8cSPeter Brune   DM             dm;
95324989b8cSPeter Brune   void           *ctx;
954316643e7SJed Brown 
955316643e7SJed Brown   PetscFunctionBegin;
9560700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9570910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
9580910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
959316643e7SJed Brown   PetscValidPointer(A,6);
96094ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
961316643e7SJed Brown   PetscValidPointer(B,7);
96294ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
96324989b8cSPeter Brune 
96424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
96524989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
9660298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
96724989b8cSPeter Brune 
968ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
969316643e7SJed Brown 
97094ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
97124989b8cSPeter Brune   if (ijacobian) {
9726cd88445SBarry Smith     PetscBool missing;
973316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
974d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
975316643e7SJed Brown     PetscStackPop;
9766cd88445SBarry Smith     ierr = MatMissingDiagonal(A,&missing,NULL);CHKERRQ(ierr);
9776cd88445SBarry 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");
9783565c898SBarry Smith     if (B != A) {
9796cd88445SBarry Smith       ierr = MatMissingDiagonal(B,&missing,NULL);CHKERRQ(ierr);
9806cd88445SBarry 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");
9816cd88445SBarry Smith     }
9824a6899ffSJed Brown   }
983214bc6a2SJed Brown   if (imex) {
984b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
9854c26be97Sstefano_zampini       PetscBool assembled;
986971015bcSStefano Zampini       if (rhsjacobian) {
987971015bcSStefano Zampini         Mat Arhs = NULL;
988971015bcSStefano Zampini         ierr = TSGetRHSMats_Private(ts,&Arhs,NULL);CHKERRQ(ierr);
989971015bcSStefano Zampini         if (A == Arhs) {
990971015bcSStefano Zampini           if (rhsjacobian == TSComputeRHSJacobianConstant) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Unsupported operation! cannot use TSComputeRHSJacobianConstant");
991971015bcSStefano Zampini           ts->rhsjacobian.time = PETSC_MIN_REAL;
992971015bcSStefano Zampini         }
993971015bcSStefano Zampini       }
99494ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
9954c26be97Sstefano_zampini       ierr = MatAssembled(A,&assembled);CHKERRQ(ierr);
9964c26be97Sstefano_zampini       if (!assembled) {
9974c26be97Sstefano_zampini         ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9984c26be97Sstefano_zampini         ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9994c26be97Sstefano_zampini       }
100094ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
100194ab13aaSBarry Smith       if (A != B) {
100294ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
10034c26be97Sstefano_zampini         ierr = MatAssembled(B,&assembled);CHKERRQ(ierr);
10044c26be97Sstefano_zampini         if (!assembled) {
10054c26be97Sstefano_zampini           ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10064c26be97Sstefano_zampini           ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10074c26be97Sstefano_zampini         }
100894ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
1009214bc6a2SJed Brown       }
1010214bc6a2SJed Brown     }
1011214bc6a2SJed Brown   } else {
1012e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
1013e1244c69SJed Brown     if (rhsjacobian) {
1014214bc6a2SJed Brown       ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
1015d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
1016e1244c69SJed Brown     }
101794ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
10183565c898SBarry Smith       PetscBool flg;
1019e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
1020e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
10213565c898SBarry Smith       ierr = SNESGetUseMatrixFree(ts->snes,NULL,&flg);CHKERRQ(ierr);
10223565c898SBarry Smith       /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */
10233565c898SBarry Smith       if (!flg) {
102494ab13aaSBarry Smith         ierr = MatScale(A,-1);CHKERRQ(ierr);
102594ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
10263565c898SBarry Smith       }
102794ab13aaSBarry Smith       if (A != B) {
102894ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
102994ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
1030316643e7SJed Brown       }
1031e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
1032e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1033e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
103494ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
103594ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
103694ab13aaSBarry Smith         if (A != B) {
103794ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
103894ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
1039214bc6a2SJed Brown         }
1040316643e7SJed Brown       }
104194ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
104294ab13aaSBarry Smith       if (A != B) {
104394ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
1044316643e7SJed Brown       }
1045316643e7SJed Brown     }
1046316643e7SJed Brown   }
104794ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
1048316643e7SJed Brown   PetscFunctionReturn(0);
1049316643e7SJed Brown }
1050316643e7SJed Brown 
1051d763cef2SBarry Smith /*@C
1052d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
1053b5abc632SBarry Smith     where U_t = G(t,u).
1054d763cef2SBarry Smith 
10553f9fe445SBarry Smith     Logically Collective on TS
1056d763cef2SBarry Smith 
1057d763cef2SBarry Smith     Input Parameters:
1058d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
10590298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
1060d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
1061d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
10620298fd71SBarry Smith           function evaluation routine (may be NULL)
1063d763cef2SBarry Smith 
1064d763cef2SBarry Smith     Calling sequence of func:
106587828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
1066d763cef2SBarry Smith 
1067d763cef2SBarry Smith +   t - current timestep
1068d763cef2SBarry Smith .   u - input vector
1069d763cef2SBarry Smith .   F - function vector
1070d763cef2SBarry Smith -   ctx - [optional] user-defined function context
1071d763cef2SBarry Smith 
1072d763cef2SBarry Smith     Level: beginner
1073d763cef2SBarry Smith 
107495452b02SPatrick Sanan     Notes:
107595452b02SPatrick Sanan     You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
10762bbac0d3SBarry Smith 
1077d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1078d763cef2SBarry Smith 
1079ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
1080d763cef2SBarry Smith @*/
1081089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
1082d763cef2SBarry Smith {
1083089b2837SJed Brown   PetscErrorCode ierr;
1084089b2837SJed Brown   SNES           snes;
10850298fd71SBarry Smith   Vec            ralloc = NULL;
108624989b8cSPeter Brune   DM             dm;
1087d763cef2SBarry Smith 
1088089b2837SJed Brown   PetscFunctionBegin;
10890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1090ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
109124989b8cSPeter Brune 
109224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
109324989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
1094089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1095e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
1096e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
1097e856ceecSJed Brown     r = ralloc;
1098e856ceecSJed Brown   }
1099089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
1100e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1101d763cef2SBarry Smith   PetscFunctionReturn(0);
1102d763cef2SBarry Smith }
1103d763cef2SBarry Smith 
1104ef20d060SBarry Smith /*@C
1105abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
1106ef20d060SBarry Smith 
1107ef20d060SBarry Smith     Logically Collective on TS
1108ef20d060SBarry Smith 
1109ef20d060SBarry Smith     Input Parameters:
1110ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
1111ef20d060SBarry Smith .   f - routine for evaluating the solution
1112ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
11130298fd71SBarry Smith           function evaluation routine (may be NULL)
1114ef20d060SBarry Smith 
1115ef20d060SBarry Smith     Calling sequence of func:
1116ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
1117ef20d060SBarry Smith 
1118ef20d060SBarry Smith +   t - current timestep
1119ef20d060SBarry Smith .   u - output vector
1120ef20d060SBarry Smith -   ctx - [optional] user-defined function context
1121ef20d060SBarry Smith 
11220ed3bfb6SBarry Smith     Options Database:
11230ed3bfb6SBarry Smith +  -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided TSSetSolutionFunction()
11240ed3bfb6SBarry Smith -  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
11250ed3bfb6SBarry Smith 
1126abd5a294SJed Brown     Notes:
1127abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
1128abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
1129abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
1130abd5a294SJed Brown 
11314f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
1132abd5a294SJed Brown 
1133ef20d060SBarry Smith     Level: beginner
1134ef20d060SBarry Smith 
1135ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
1136ef20d060SBarry Smith 
11370ed3bfb6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction(), TSSetSolution(), TSGetSolution(), TSMonitorLGError(), TSMonitorDrawError()
1138ef20d060SBarry Smith @*/
1139ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
1140ef20d060SBarry Smith {
1141ef20d060SBarry Smith   PetscErrorCode ierr;
1142ef20d060SBarry Smith   DM             dm;
1143ef20d060SBarry Smith 
1144ef20d060SBarry Smith   PetscFunctionBegin;
1145ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1146ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1147ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
1148ef20d060SBarry Smith   PetscFunctionReturn(0);
1149ef20d060SBarry Smith }
1150ef20d060SBarry Smith 
11519b7cd975SBarry Smith /*@C
11529b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
11539b7cd975SBarry Smith 
11549b7cd975SBarry Smith     Logically Collective on TS
11559b7cd975SBarry Smith 
11569b7cd975SBarry Smith     Input Parameters:
11579b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
1158e162b725SBarry Smith .   func - routine for evaluating the forcing function
11599b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
11600298fd71SBarry Smith           function evaluation routine (may be NULL)
11619b7cd975SBarry Smith 
11629b7cd975SBarry Smith     Calling sequence of func:
1163e162b725SBarry Smith $     func (TS ts,PetscReal t,Vec f,void *ctx);
11649b7cd975SBarry Smith 
11659b7cd975SBarry Smith +   t - current timestep
1166e162b725SBarry Smith .   f - output vector
11679b7cd975SBarry Smith -   ctx - [optional] user-defined function context
11689b7cd975SBarry Smith 
11699b7cd975SBarry Smith     Notes:
11709b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
1171e162b725SBarry 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
1172e162b725SBarry Smith     definition of the problem you are solving and hence possibly introducing bugs.
1173e162b725SBarry Smith 
1174e162b725SBarry Smith     This replaces the ODE F(u,u_t,t) = 0 the TS is solving with F(u,u_t,t) - func(t) = 0
1175e162b725SBarry Smith 
1176e162b725SBarry 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
1177e162b725SBarry Smith     parameters can be passed in the ctx variable.
11789b7cd975SBarry Smith 
11799b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
11809b7cd975SBarry Smith 
11819b7cd975SBarry Smith     Level: beginner
11829b7cd975SBarry Smith 
11839b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
11849b7cd975SBarry Smith 
11859b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
11869b7cd975SBarry Smith @*/
1187e162b725SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,TSForcingFunction func,void *ctx)
11889b7cd975SBarry Smith {
11899b7cd975SBarry Smith   PetscErrorCode ierr;
11909b7cd975SBarry Smith   DM             dm;
11919b7cd975SBarry Smith 
11929b7cd975SBarry Smith   PetscFunctionBegin;
11939b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
11949b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1195e162b725SBarry Smith   ierr = DMTSSetForcingFunction(dm,func,ctx);CHKERRQ(ierr);
11969b7cd975SBarry Smith   PetscFunctionReturn(0);
11979b7cd975SBarry Smith }
11989b7cd975SBarry Smith 
1199d763cef2SBarry Smith /*@C
1200f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1201b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1202d763cef2SBarry Smith 
12033f9fe445SBarry Smith    Logically Collective on TS
1204d763cef2SBarry Smith 
1205d763cef2SBarry Smith    Input Parameters:
1206d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1207e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1208e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1209d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1210d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
12110298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1212d763cef2SBarry Smith 
1213f7ab8db6SBarry Smith    Calling sequence of f:
1214ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1215d763cef2SBarry Smith 
1216d763cef2SBarry Smith +  t - current timestep
1217d763cef2SBarry Smith .  u - input vector
1218e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1219e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1220d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1221d763cef2SBarry Smith 
12226cd88445SBarry Smith    Notes:
12236cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
12246cd88445SBarry Smith 
12256cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1226ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1227d763cef2SBarry Smith 
1228d763cef2SBarry Smith    Level: beginner
1229d763cef2SBarry Smith 
1230d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1231d763cef2SBarry Smith 
1232ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1233d763cef2SBarry Smith 
1234d763cef2SBarry Smith @*/
1235e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1236d763cef2SBarry Smith {
1237277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1238089b2837SJed Brown   SNES           snes;
123924989b8cSPeter Brune   DM             dm;
124024989b8cSPeter Brune   TSIJacobian    ijacobian;
1241277b19d0SLisandro Dalcin 
1242d763cef2SBarry Smith   PetscFunctionBegin;
12430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1244e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1245e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1246e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1247e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1248d763cef2SBarry Smith 
124924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
125024989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1251e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1252e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1253e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1254e1244c69SJed Brown   }
12550298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1256089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12575f659677SPeter Brune   if (!ijacobian) {
1258e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
12590e4ef248SJed Brown   }
1260e5d3d808SBarry Smith   if (Amat) {
1261e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
12620e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1263e5d3d808SBarry Smith     ts->Arhs = Amat;
12640e4ef248SJed Brown   }
1265e5d3d808SBarry Smith   if (Pmat) {
1266e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
12670e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1268e5d3d808SBarry Smith     ts->Brhs = Pmat;
12690e4ef248SJed Brown   }
1270d763cef2SBarry Smith   PetscFunctionReturn(0);
1271d763cef2SBarry Smith }
1272d763cef2SBarry Smith 
1273316643e7SJed Brown /*@C
1274b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1275316643e7SJed Brown 
12763f9fe445SBarry Smith    Logically Collective on TS
1277316643e7SJed Brown 
1278316643e7SJed Brown    Input Parameters:
1279316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
12800298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1281316643e7SJed Brown .  f   - the function evaluation routine
12820298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1283316643e7SJed Brown 
1284316643e7SJed Brown    Calling sequence of f:
1285316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1286316643e7SJed Brown 
1287316643e7SJed Brown +  t   - time at step/stage being solved
1288316643e7SJed Brown .  u   - state vector
1289316643e7SJed Brown .  u_t - time derivative of state vector
1290316643e7SJed Brown .  F   - function vector
1291316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1292316643e7SJed Brown 
1293316643e7SJed Brown    Important:
12942bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1295316643e7SJed Brown 
1296316643e7SJed Brown    Level: beginner
1297316643e7SJed Brown 
1298316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1299316643e7SJed Brown 
1300d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1301316643e7SJed Brown @*/
130251699248SLisandro Dalcin PetscErrorCode  TSSetIFunction(TS ts,Vec r,TSIFunction f,void *ctx)
1303316643e7SJed Brown {
1304089b2837SJed Brown   PetscErrorCode ierr;
1305089b2837SJed Brown   SNES           snes;
130651699248SLisandro Dalcin   Vec            ralloc = NULL;
130724989b8cSPeter Brune   DM             dm;
1308316643e7SJed Brown 
1309316643e7SJed Brown   PetscFunctionBegin;
13100700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
131151699248SLisandro Dalcin   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
131224989b8cSPeter Brune 
131324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
131424989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
131524989b8cSPeter Brune 
1316089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
131751699248SLisandro Dalcin   if (!r && !ts->dm && ts->vec_sol) {
131851699248SLisandro Dalcin     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
131951699248SLisandro Dalcin     r  = ralloc;
1320e856ceecSJed Brown   }
132151699248SLisandro Dalcin   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
132251699248SLisandro Dalcin   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
1323089b2837SJed Brown   PetscFunctionReturn(0);
1324089b2837SJed Brown }
1325089b2837SJed Brown 
1326089b2837SJed Brown /*@C
1327089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1328089b2837SJed Brown 
1329089b2837SJed Brown    Not Collective
1330089b2837SJed Brown 
1331089b2837SJed Brown    Input Parameter:
1332089b2837SJed Brown .  ts - the TS context
1333089b2837SJed Brown 
1334089b2837SJed Brown    Output Parameter:
13350298fd71SBarry Smith +  r - vector to hold residual (or NULL)
13360298fd71SBarry Smith .  func - the function to compute residual (or NULL)
13370298fd71SBarry Smith -  ctx - the function context (or NULL)
1338089b2837SJed Brown 
1339089b2837SJed Brown    Level: advanced
1340089b2837SJed Brown 
1341089b2837SJed Brown .keywords: TS, nonlinear, get, function
1342089b2837SJed Brown 
1343089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1344089b2837SJed Brown @*/
1345089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1346089b2837SJed Brown {
1347089b2837SJed Brown   PetscErrorCode ierr;
1348089b2837SJed Brown   SNES           snes;
134924989b8cSPeter Brune   DM             dm;
1350089b2837SJed Brown 
1351089b2837SJed Brown   PetscFunctionBegin;
1352089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1353089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13540298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
135524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
135624989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1357089b2837SJed Brown   PetscFunctionReturn(0);
1358089b2837SJed Brown }
1359089b2837SJed Brown 
1360089b2837SJed Brown /*@C
1361089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1362089b2837SJed Brown 
1363089b2837SJed Brown    Not Collective
1364089b2837SJed Brown 
1365089b2837SJed Brown    Input Parameter:
1366089b2837SJed Brown .  ts - the TS context
1367089b2837SJed Brown 
1368089b2837SJed Brown    Output Parameter:
13690298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
13700298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
13710298fd71SBarry Smith -  ctx - the function context (or NULL)
1372089b2837SJed Brown 
1373089b2837SJed Brown    Level: advanced
1374089b2837SJed Brown 
1375089b2837SJed Brown .keywords: TS, nonlinear, get, function
1376089b2837SJed Brown 
13772bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1378089b2837SJed Brown @*/
1379089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1380089b2837SJed Brown {
1381089b2837SJed Brown   PetscErrorCode ierr;
1382089b2837SJed Brown   SNES           snes;
138324989b8cSPeter Brune   DM             dm;
1384089b2837SJed Brown 
1385089b2837SJed Brown   PetscFunctionBegin;
1386089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1387089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
13880298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
138924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
139024989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1391316643e7SJed Brown   PetscFunctionReturn(0);
1392316643e7SJed Brown }
1393316643e7SJed Brown 
1394316643e7SJed Brown /*@C
1395a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1396ae8867d6SBarry Smith         provided with TSSetIFunction().
1397316643e7SJed Brown 
13983f9fe445SBarry Smith    Logically Collective on TS
1399316643e7SJed Brown 
1400316643e7SJed Brown    Input Parameters:
1401316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1402e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1403e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1404316643e7SJed Brown .  f   - the Jacobian evaluation routine
14050298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1406316643e7SJed Brown 
1407316643e7SJed Brown    Calling sequence of f:
1408ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1409316643e7SJed Brown 
1410316643e7SJed Brown +  t    - time at step/stage being solved
14111b4a444bSJed Brown .  U    - state vector
14121b4a444bSJed Brown .  U_t  - time derivative of state vector
1413316643e7SJed Brown .  a    - shift
1414e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1415e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1416316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1417316643e7SJed Brown 
1418316643e7SJed Brown    Notes:
1419e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1420316643e7SJed Brown 
1421895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1422895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1423895c21f2SBarry Smith 
1424a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1425b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1426a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1427a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1428a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1429a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1430a4f0a591SBarry Smith 
14316cd88445SBarry Smith    You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value
14326cd88445SBarry Smith 
14336cd88445SBarry Smith    The TS solver may modify the nonzero structure and the entries of the matrices Amat and Pmat between the calls to f()
1434ca5f011dSBarry Smith    You should not assume the values are the same in the next call to f() as you set them in the previous call.
1435ca5f011dSBarry Smith 
1436316643e7SJed Brown    Level: beginner
1437316643e7SJed Brown 
1438316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1439316643e7SJed Brown 
1440ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1441316643e7SJed Brown 
1442316643e7SJed Brown @*/
1443e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1444316643e7SJed Brown {
1445316643e7SJed Brown   PetscErrorCode ierr;
1446089b2837SJed Brown   SNES           snes;
144724989b8cSPeter Brune   DM             dm;
1448316643e7SJed Brown 
1449316643e7SJed Brown   PetscFunctionBegin;
14500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1451e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1452e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1453e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1454e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
145524989b8cSPeter Brune 
145624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
145724989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
145824989b8cSPeter Brune 
1459089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1460e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1461316643e7SJed Brown   PetscFunctionReturn(0);
1462316643e7SJed Brown }
1463316643e7SJed Brown 
1464e1244c69SJed Brown /*@
1465e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1466e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1467e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1468e1244c69SJed Brown    not been changed by the TS.
1469e1244c69SJed Brown 
1470e1244c69SJed Brown    Logically Collective
1471e1244c69SJed Brown 
1472e1244c69SJed Brown    Input Arguments:
1473e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1474e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1475e1244c69SJed Brown 
1476e1244c69SJed Brown    Level: intermediate
1477e1244c69SJed Brown 
1478e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1479e1244c69SJed Brown @*/
1480e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1481e1244c69SJed Brown {
1482e1244c69SJed Brown   PetscFunctionBegin;
1483e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1484e1244c69SJed Brown   PetscFunctionReturn(0);
1485e1244c69SJed Brown }
1486e1244c69SJed Brown 
1487efe9872eSLisandro Dalcin /*@C
1488efe9872eSLisandro Dalcin    TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved.
1489efe9872eSLisandro Dalcin 
1490efe9872eSLisandro Dalcin    Logically Collective on TS
1491efe9872eSLisandro Dalcin 
1492efe9872eSLisandro Dalcin    Input Parameters:
1493efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1494efe9872eSLisandro Dalcin .  F   - vector to hold the residual (or NULL to have it created internally)
1495efe9872eSLisandro Dalcin .  fun - the function evaluation routine
1496efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1497efe9872eSLisandro Dalcin 
1498efe9872eSLisandro Dalcin    Calling sequence of fun:
1499efe9872eSLisandro Dalcin $  fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
1500efe9872eSLisandro Dalcin 
1501efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1502efe9872eSLisandro Dalcin .  U    - state vector
1503efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1504efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1505efe9872eSLisandro Dalcin .  F    - function vector
1506efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
1507efe9872eSLisandro Dalcin 
1508efe9872eSLisandro Dalcin    Level: beginner
1509efe9872eSLisandro Dalcin 
1510efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Function
1511efe9872eSLisandro Dalcin 
1512efe9872eSLisandro Dalcin .seealso: TSSetI2Jacobian()
1513efe9872eSLisandro Dalcin @*/
1514efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Function(TS ts,Vec F,TSI2Function fun,void *ctx)
1515efe9872eSLisandro Dalcin {
1516efe9872eSLisandro Dalcin   DM             dm;
1517efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1518efe9872eSLisandro Dalcin 
1519efe9872eSLisandro Dalcin   PetscFunctionBegin;
1520efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1521efe9872eSLisandro Dalcin   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,2);
1522efe9872eSLisandro Dalcin   ierr = TSSetIFunction(ts,F,NULL,NULL);CHKERRQ(ierr);
1523efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1524efe9872eSLisandro Dalcin   ierr = DMTSSetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1525efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1526efe9872eSLisandro Dalcin }
1527efe9872eSLisandro Dalcin 
1528efe9872eSLisandro Dalcin /*@C
1529efe9872eSLisandro Dalcin   TSGetI2Function - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1530efe9872eSLisandro Dalcin 
1531efe9872eSLisandro Dalcin   Not Collective
1532efe9872eSLisandro Dalcin 
1533efe9872eSLisandro Dalcin   Input Parameter:
1534efe9872eSLisandro Dalcin . ts - the TS context
1535efe9872eSLisandro Dalcin 
1536efe9872eSLisandro Dalcin   Output Parameter:
1537efe9872eSLisandro Dalcin + r - vector to hold residual (or NULL)
1538efe9872eSLisandro Dalcin . fun - the function to compute residual (or NULL)
1539efe9872eSLisandro Dalcin - ctx - the function context (or NULL)
1540efe9872eSLisandro Dalcin 
1541efe9872eSLisandro Dalcin   Level: advanced
1542efe9872eSLisandro Dalcin 
1543efe9872eSLisandro Dalcin .keywords: TS, nonlinear, get, function
1544efe9872eSLisandro Dalcin 
1545efe9872eSLisandro Dalcin .seealso: TSSetI2Function(), SNESGetFunction()
1546efe9872eSLisandro Dalcin @*/
1547efe9872eSLisandro Dalcin PetscErrorCode TSGetI2Function(TS ts,Vec *r,TSI2Function *fun,void **ctx)
1548efe9872eSLisandro Dalcin {
1549efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1550efe9872eSLisandro Dalcin   SNES           snes;
1551efe9872eSLisandro Dalcin   DM             dm;
1552efe9872eSLisandro Dalcin 
1553efe9872eSLisandro Dalcin   PetscFunctionBegin;
1554efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1555efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1556efe9872eSLisandro Dalcin   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
1557efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1558efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,fun,ctx);CHKERRQ(ierr);
1559efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1560efe9872eSLisandro Dalcin }
1561efe9872eSLisandro Dalcin 
1562efe9872eSLisandro Dalcin /*@C
1563bc77d74cSLisandro Dalcin    TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t  + a*dF/dU_tt
1564efe9872eSLisandro Dalcin         where F(t,U,U_t,U_tt) is the function you provided with TSSetI2Function().
1565efe9872eSLisandro Dalcin 
1566efe9872eSLisandro Dalcin    Logically Collective on TS
1567efe9872eSLisandro Dalcin 
1568efe9872eSLisandro Dalcin    Input Parameters:
1569efe9872eSLisandro Dalcin +  ts  - the TS context obtained from TSCreate()
1570efe9872eSLisandro Dalcin .  J   - Jacobian matrix
1571efe9872eSLisandro Dalcin .  P   - preconditioning matrix for J (may be same as J)
1572efe9872eSLisandro Dalcin .  jac - the Jacobian evaluation routine
1573efe9872eSLisandro Dalcin -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1574efe9872eSLisandro Dalcin 
1575efe9872eSLisandro Dalcin    Calling sequence of jac:
1576bc77d74cSLisandro Dalcin $  jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx);
1577efe9872eSLisandro Dalcin 
1578efe9872eSLisandro Dalcin +  t    - time at step/stage being solved
1579efe9872eSLisandro Dalcin .  U    - state vector
1580efe9872eSLisandro Dalcin .  U_t  - time derivative of state vector
1581efe9872eSLisandro Dalcin .  U_tt - second time derivative of state vector
1582efe9872eSLisandro Dalcin .  v    - shift for U_t
1583efe9872eSLisandro Dalcin .  a    - shift for U_tt
1584efe9872eSLisandro 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
1585efe9872eSLisandro Dalcin .  P    - preconditioning matrix for J, may be same as J
1586efe9872eSLisandro Dalcin -  ctx  - [optional] user-defined context for matrix evaluation routine
1587efe9872eSLisandro Dalcin 
1588efe9872eSLisandro Dalcin    Notes:
1589efe9872eSLisandro Dalcin    The matrices J and P are exactly the matrices that are used by SNES for the nonlinear solve.
1590efe9872eSLisandro Dalcin 
1591efe9872eSLisandro Dalcin    The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be
1592efe9872eSLisandro 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.
1593efe9872eSLisandro Dalcin    The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U  where the positive "shift"
1594bc77d74cSLisandro Dalcin    parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states.
1595efe9872eSLisandro Dalcin 
1596efe9872eSLisandro Dalcin    Level: beginner
1597efe9872eSLisandro Dalcin 
1598efe9872eSLisandro Dalcin .keywords: TS, timestep, set, ODE, DAE, Jacobian
1599efe9872eSLisandro Dalcin 
1600efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1601efe9872eSLisandro Dalcin @*/
1602efe9872eSLisandro Dalcin PetscErrorCode TSSetI2Jacobian(TS ts,Mat J,Mat P,TSI2Jacobian jac,void *ctx)
1603efe9872eSLisandro Dalcin {
1604efe9872eSLisandro Dalcin   DM             dm;
1605efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1606efe9872eSLisandro Dalcin 
1607efe9872eSLisandro Dalcin   PetscFunctionBegin;
1608efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1609efe9872eSLisandro Dalcin   if (J) PetscValidHeaderSpecific(J,MAT_CLASSID,2);
1610efe9872eSLisandro Dalcin   if (P) PetscValidHeaderSpecific(P,MAT_CLASSID,3);
1611efe9872eSLisandro Dalcin   ierr = TSSetIJacobian(ts,J,P,NULL,NULL);CHKERRQ(ierr);
1612efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1613efe9872eSLisandro Dalcin   ierr = DMTSSetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1614efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1615efe9872eSLisandro Dalcin }
1616efe9872eSLisandro Dalcin 
1617efe9872eSLisandro Dalcin /*@C
1618efe9872eSLisandro Dalcin   TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep.
1619efe9872eSLisandro Dalcin 
1620efe9872eSLisandro Dalcin   Not Collective, but parallel objects are returned if TS is parallel
1621efe9872eSLisandro Dalcin 
1622efe9872eSLisandro Dalcin   Input Parameter:
1623efe9872eSLisandro Dalcin . ts  - The TS context obtained from TSCreate()
1624efe9872eSLisandro Dalcin 
1625efe9872eSLisandro Dalcin   Output Parameters:
1626efe9872eSLisandro Dalcin + J  - The (approximate) Jacobian of F(t,U,U_t,U_tt)
1627efe9872eSLisandro Dalcin . P - The matrix from which the preconditioner is constructed, often the same as J
1628efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices
1629efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine
1630efe9872eSLisandro Dalcin 
163195452b02SPatrick Sanan   Notes:
163295452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
1633efe9872eSLisandro Dalcin 
1634efe9872eSLisandro Dalcin   Level: advanced
1635efe9872eSLisandro Dalcin 
163680275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
1637efe9872eSLisandro Dalcin 
1638efe9872eSLisandro Dalcin .keywords: TS, timestep, get, matrix, Jacobian
1639efe9872eSLisandro Dalcin @*/
1640efe9872eSLisandro Dalcin PetscErrorCode  TSGetI2Jacobian(TS ts,Mat *J,Mat *P,TSI2Jacobian *jac,void **ctx)
1641efe9872eSLisandro Dalcin {
1642efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1643efe9872eSLisandro Dalcin   SNES           snes;
1644efe9872eSLisandro Dalcin   DM             dm;
1645efe9872eSLisandro Dalcin 
1646efe9872eSLisandro Dalcin   PetscFunctionBegin;
1647efe9872eSLisandro Dalcin   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1648efe9872eSLisandro Dalcin   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
1649efe9872eSLisandro Dalcin   ierr = SNESGetJacobian(snes,J,P,NULL,NULL);CHKERRQ(ierr);
1650efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1651efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,jac,ctx);CHKERRQ(ierr);
1652efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1653efe9872eSLisandro Dalcin }
1654efe9872eSLisandro Dalcin 
1655efe9872eSLisandro Dalcin /*@
1656efe9872eSLisandro Dalcin   TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0
1657efe9872eSLisandro Dalcin 
1658efe9872eSLisandro Dalcin   Collective on TS and Vec
1659efe9872eSLisandro Dalcin 
1660efe9872eSLisandro Dalcin   Input Parameters:
1661efe9872eSLisandro Dalcin + ts - the TS context
1662efe9872eSLisandro Dalcin . t - current time
1663efe9872eSLisandro Dalcin . U - state vector
1664efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t)
1665efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt)
1666efe9872eSLisandro Dalcin 
1667efe9872eSLisandro Dalcin   Output Parameter:
1668efe9872eSLisandro Dalcin . F - the residual vector
1669efe9872eSLisandro Dalcin 
1670efe9872eSLisandro Dalcin   Note:
1671efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1672efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1673efe9872eSLisandro Dalcin 
1674efe9872eSLisandro Dalcin   Level: developer
1675efe9872eSLisandro Dalcin 
1676efe9872eSLisandro Dalcin .keywords: TS, compute, function, vector
1677efe9872eSLisandro Dalcin 
1678efe9872eSLisandro Dalcin .seealso: TSSetI2Function()
1679efe9872eSLisandro Dalcin @*/
1680efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Function(TS ts,PetscReal t,Vec U,Vec V,Vec A,Vec F)
1681efe9872eSLisandro Dalcin {
1682efe9872eSLisandro Dalcin   DM             dm;
1683efe9872eSLisandro Dalcin   TSI2Function   I2Function;
1684efe9872eSLisandro Dalcin   void           *ctx;
1685efe9872eSLisandro Dalcin   TSRHSFunction  rhsfunction;
1686efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1687efe9872eSLisandro Dalcin 
1688efe9872eSLisandro Dalcin   PetscFunctionBegin;
1689efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1690efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1691efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1692efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1693efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(F,VEC_CLASSID,6);
1694efe9872eSLisandro Dalcin 
1695efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1696efe9872eSLisandro Dalcin   ierr = DMTSGetI2Function(dm,&I2Function,&ctx);CHKERRQ(ierr);
1697efe9872eSLisandro Dalcin   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
1698efe9872eSLisandro Dalcin 
1699efe9872eSLisandro Dalcin   if (!I2Function) {
1700efe9872eSLisandro Dalcin     ierr = TSComputeIFunction(ts,t,U,A,F,PETSC_FALSE);CHKERRQ(ierr);
1701efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1702efe9872eSLisandro Dalcin   }
1703efe9872eSLisandro Dalcin 
1704efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1705efe9872eSLisandro Dalcin 
1706efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit function");
1707efe9872eSLisandro Dalcin   ierr = I2Function(ts,t,U,V,A,F,ctx);CHKERRQ(ierr);
1708efe9872eSLisandro Dalcin   PetscStackPop;
1709efe9872eSLisandro Dalcin 
1710efe9872eSLisandro Dalcin   if (rhsfunction) {
1711efe9872eSLisandro Dalcin     Vec Frhs;
1712efe9872eSLisandro Dalcin     ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
1713efe9872eSLisandro Dalcin     ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
1714efe9872eSLisandro Dalcin     ierr = VecAXPY(F,-1,Frhs);CHKERRQ(ierr);
1715efe9872eSLisandro Dalcin   }
1716efe9872eSLisandro Dalcin 
1717efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,V,F);CHKERRQ(ierr);
1718efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1719efe9872eSLisandro Dalcin }
1720efe9872eSLisandro Dalcin 
1721efe9872eSLisandro Dalcin /*@
1722efe9872eSLisandro Dalcin   TSComputeI2Jacobian - Evaluates the Jacobian of the DAE
1723efe9872eSLisandro Dalcin 
1724efe9872eSLisandro Dalcin   Collective on TS and Vec
1725efe9872eSLisandro Dalcin 
1726efe9872eSLisandro Dalcin   Input Parameters:
1727efe9872eSLisandro Dalcin + ts - the TS context
1728efe9872eSLisandro Dalcin . t - current timestep
1729efe9872eSLisandro Dalcin . U - state vector
1730efe9872eSLisandro Dalcin . V - time derivative of state vector
1731efe9872eSLisandro Dalcin . A - second time derivative of state vector
1732efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below
1733efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below
1734efe9872eSLisandro Dalcin 
1735efe9872eSLisandro Dalcin   Output Parameters:
1736efe9872eSLisandro Dalcin + J - Jacobian matrix
1737efe9872eSLisandro Dalcin - P - optional preconditioning matrix
1738efe9872eSLisandro Dalcin 
1739efe9872eSLisandro Dalcin   Notes:
1740efe9872eSLisandro Dalcin   If F(t,U,V,A)=0 is the DAE, the required Jacobian is
1741efe9872eSLisandro Dalcin 
1742efe9872eSLisandro Dalcin   dF/dU + shiftV*dF/dV + shiftA*dF/dA
1743efe9872eSLisandro Dalcin 
1744efe9872eSLisandro Dalcin   Most users should not need to explicitly call this routine, as it
1745efe9872eSLisandro Dalcin   is used internally within the nonlinear solvers.
1746efe9872eSLisandro Dalcin 
1747efe9872eSLisandro Dalcin   Level: developer
1748efe9872eSLisandro Dalcin 
1749efe9872eSLisandro Dalcin .keywords: TS, compute, Jacobian, matrix
1750efe9872eSLisandro Dalcin 
1751efe9872eSLisandro Dalcin .seealso:  TSSetI2Jacobian()
1752efe9872eSLisandro Dalcin @*/
1753efe9872eSLisandro Dalcin PetscErrorCode TSComputeI2Jacobian(TS ts,PetscReal t,Vec U,Vec V,Vec A,PetscReal shiftV,PetscReal shiftA,Mat J,Mat P)
1754efe9872eSLisandro Dalcin {
1755efe9872eSLisandro Dalcin   DM             dm;
1756efe9872eSLisandro Dalcin   TSI2Jacobian   I2Jacobian;
1757efe9872eSLisandro Dalcin   void           *ctx;
1758efe9872eSLisandro Dalcin   TSRHSJacobian  rhsjacobian;
1759efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1760efe9872eSLisandro Dalcin 
1761efe9872eSLisandro Dalcin   PetscFunctionBegin;
1762efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1763efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
1764efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(V,VEC_CLASSID,4);
1765efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(A,VEC_CLASSID,5);
1766efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(J,MAT_CLASSID,8);
1767efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(P,MAT_CLASSID,9);
1768efe9872eSLisandro Dalcin 
1769efe9872eSLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
1770efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&I2Jacobian,&ctx);CHKERRQ(ierr);
1771efe9872eSLisandro Dalcin   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
1772efe9872eSLisandro Dalcin 
1773efe9872eSLisandro Dalcin   if (!I2Jacobian) {
1774efe9872eSLisandro Dalcin     ierr = TSComputeIJacobian(ts,t,U,A,shiftA,J,P,PETSC_FALSE);CHKERRQ(ierr);
1775efe9872eSLisandro Dalcin     PetscFunctionReturn(0);
1776efe9872eSLisandro Dalcin   }
1777efe9872eSLisandro Dalcin 
1778efe9872eSLisandro Dalcin   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1779efe9872eSLisandro Dalcin 
1780efe9872eSLisandro Dalcin   PetscStackPush("TS user implicit Jacobian");
1781efe9872eSLisandro Dalcin   ierr = I2Jacobian(ts,t,U,V,A,shiftV,shiftA,J,P,ctx);CHKERRQ(ierr);
1782efe9872eSLisandro Dalcin   PetscStackPop;
1783efe9872eSLisandro Dalcin 
1784efe9872eSLisandro Dalcin   if (rhsjacobian) {
1785efe9872eSLisandro Dalcin     Mat Jrhs,Prhs; MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
1786efe9872eSLisandro Dalcin     ierr = TSGetRHSMats_Private(ts,&Jrhs,&Prhs);CHKERRQ(ierr);
1787efe9872eSLisandro Dalcin     ierr = TSComputeRHSJacobian(ts,t,U,Jrhs,Prhs);CHKERRQ(ierr);
1788efe9872eSLisandro Dalcin     ierr = MatAXPY(J,-1,Jrhs,axpy);CHKERRQ(ierr);
1789efe9872eSLisandro Dalcin     if (P != J) {ierr = MatAXPY(P,-1,Prhs,axpy);CHKERRQ(ierr);}
1790efe9872eSLisandro Dalcin   }
1791efe9872eSLisandro Dalcin 
1792efe9872eSLisandro Dalcin   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,J,P);CHKERRQ(ierr);
1793efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1794efe9872eSLisandro Dalcin }
1795efe9872eSLisandro Dalcin 
1796efe9872eSLisandro Dalcin /*@
1797efe9872eSLisandro Dalcin    TS2SetSolution - Sets the initial solution and time derivative vectors
1798efe9872eSLisandro Dalcin    for use by the TS routines handling second order equations.
1799efe9872eSLisandro Dalcin 
1800efe9872eSLisandro Dalcin    Logically Collective on TS and Vec
1801efe9872eSLisandro Dalcin 
1802efe9872eSLisandro Dalcin    Input Parameters:
1803efe9872eSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
1804efe9872eSLisandro Dalcin .  u - the solution vector
1805efe9872eSLisandro Dalcin -  v - the time derivative vector
1806efe9872eSLisandro Dalcin 
1807efe9872eSLisandro Dalcin    Level: beginner
1808efe9872eSLisandro Dalcin 
1809efe9872eSLisandro Dalcin .keywords: TS, timestep, set, solution, initial conditions
1810efe9872eSLisandro Dalcin @*/
1811efe9872eSLisandro Dalcin PetscErrorCode  TS2SetSolution(TS ts,Vec u,Vec v)
1812efe9872eSLisandro Dalcin {
1813efe9872eSLisandro Dalcin   PetscErrorCode ierr;
1814efe9872eSLisandro Dalcin 
1815efe9872eSLisandro Dalcin   PetscFunctionBegin;
1816efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1817efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
1818efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(v,VEC_CLASSID,3);
1819efe9872eSLisandro Dalcin   ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
1820efe9872eSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)v);CHKERRQ(ierr);
1821efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
1822efe9872eSLisandro Dalcin   ts->vec_dot = v;
1823efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1824efe9872eSLisandro Dalcin }
1825efe9872eSLisandro Dalcin 
1826efe9872eSLisandro Dalcin /*@
1827efe9872eSLisandro Dalcin    TS2GetSolution - Returns the solution and time derivative at the present timestep
1828efe9872eSLisandro Dalcin    for second order equations. It is valid to call this routine inside the function
1829efe9872eSLisandro Dalcin    that you are evaluating in order to move to the new timestep. This vector not
1830efe9872eSLisandro Dalcin    changed until the solution at the next timestep has been calculated.
1831efe9872eSLisandro Dalcin 
1832efe9872eSLisandro Dalcin    Not Collective, but Vec returned is parallel if TS is parallel
1833efe9872eSLisandro Dalcin 
1834efe9872eSLisandro Dalcin    Input Parameter:
1835efe9872eSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1836efe9872eSLisandro Dalcin 
1837efe9872eSLisandro Dalcin    Output Parameter:
1838efe9872eSLisandro Dalcin +  u - the vector containing the solution
1839efe9872eSLisandro Dalcin -  v - the vector containing the time derivative
1840efe9872eSLisandro Dalcin 
1841efe9872eSLisandro Dalcin    Level: intermediate
1842efe9872eSLisandro Dalcin 
1843efe9872eSLisandro Dalcin .seealso: TS2SetSolution(), TSGetTimeStep(), TSGetTime()
1844efe9872eSLisandro Dalcin 
1845efe9872eSLisandro Dalcin .keywords: TS, timestep, get, solution
1846efe9872eSLisandro Dalcin @*/
1847efe9872eSLisandro Dalcin PetscErrorCode  TS2GetSolution(TS ts,Vec *u,Vec *v)
1848efe9872eSLisandro Dalcin {
1849efe9872eSLisandro Dalcin   PetscFunctionBegin;
1850efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1851efe9872eSLisandro Dalcin   if (u) PetscValidPointer(u,2);
1852efe9872eSLisandro Dalcin   if (v) PetscValidPointer(v,3);
1853efe9872eSLisandro Dalcin   if (u) *u = ts->vec_sol;
1854efe9872eSLisandro Dalcin   if (v) *v = ts->vec_dot;
1855efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
1856efe9872eSLisandro Dalcin }
1857efe9872eSLisandro Dalcin 
185855849f57SBarry Smith /*@C
185955849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
186055849f57SBarry Smith 
186155849f57SBarry Smith   Collective on PetscViewer
186255849f57SBarry Smith 
186355849f57SBarry Smith   Input Parameters:
186455849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
186555849f57SBarry Smith            some related function before a call to TSLoad().
186655849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
186755849f57SBarry Smith 
186855849f57SBarry Smith    Level: intermediate
186955849f57SBarry Smith 
187055849f57SBarry Smith   Notes:
187155849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
187255849f57SBarry Smith 
187355849f57SBarry Smith   Notes for advanced users:
187455849f57SBarry Smith   Most users should not need to know the details of the binary storage
187555849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
187655849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
187755849f57SBarry Smith   format is
187855849f57SBarry Smith .vb
187955849f57SBarry Smith      has not yet been determined
188055849f57SBarry Smith .ve
188155849f57SBarry Smith 
188255849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
188355849f57SBarry Smith @*/
1884f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
188555849f57SBarry Smith {
188655849f57SBarry Smith   PetscErrorCode ierr;
188755849f57SBarry Smith   PetscBool      isbinary;
188855849f57SBarry Smith   PetscInt       classid;
188955849f57SBarry Smith   char           type[256];
18902d53ad75SBarry Smith   DMTS           sdm;
1891ad6bc421SBarry Smith   DM             dm;
189255849f57SBarry Smith 
189355849f57SBarry Smith   PetscFunctionBegin;
1894f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
189555849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
189655849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
189755849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
189855849f57SBarry Smith 
1899060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1900ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1901060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1902f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1903f2c2a1b9SBarry Smith   if (ts->ops->load) {
1904f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1905f2c2a1b9SBarry Smith   }
1906ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1907ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1908ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1909f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1910f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
19112d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
19122d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
191355849f57SBarry Smith   PetscFunctionReturn(0);
191455849f57SBarry Smith }
191555849f57SBarry Smith 
19169804daf3SBarry Smith #include <petscdraw.h>
1917e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1918e04113cfSBarry Smith #include <petscviewersaws.h>
1919f05ece33SBarry Smith #endif
19207e2c5f70SBarry Smith /*@C
1921d763cef2SBarry Smith     TSView - Prints the TS data structure.
1922d763cef2SBarry Smith 
19234c49b128SBarry Smith     Collective on TS
1924d763cef2SBarry Smith 
1925d763cef2SBarry Smith     Input Parameters:
1926d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1927d763cef2SBarry Smith -   viewer - visualization context
1928d763cef2SBarry Smith 
1929d763cef2SBarry Smith     Options Database Key:
1930d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1931d763cef2SBarry Smith 
1932d763cef2SBarry Smith     Notes:
1933d763cef2SBarry Smith     The available visualization contexts include
1934b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1935b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1936d763cef2SBarry Smith          output where only the first processor opens
1937d763cef2SBarry Smith          the file.  All other processors send their
1938d763cef2SBarry Smith          data to the first processor to print.
1939d763cef2SBarry Smith 
1940d763cef2SBarry Smith     The user can open an alternative visualization context with
1941b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1942d763cef2SBarry Smith 
1943d763cef2SBarry Smith     Level: beginner
1944d763cef2SBarry Smith 
1945d763cef2SBarry Smith .keywords: TS, timestep, view
1946d763cef2SBarry Smith 
1947b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1948d763cef2SBarry Smith @*/
19497087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1950d763cef2SBarry Smith {
1951dfbe8321SBarry Smith   PetscErrorCode ierr;
195219fd82e9SBarry Smith   TSType         type;
19532b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
19542d53ad75SBarry Smith   DMTS           sdm;
1955e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1956536b137fSBarry Smith   PetscBool      issaws;
1957f05ece33SBarry Smith #endif
1958d763cef2SBarry Smith 
1959d763cef2SBarry Smith   PetscFunctionBegin;
19600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
19613050cee2SBarry Smith   if (!viewer) {
1962ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
19633050cee2SBarry Smith   }
19640700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1965c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1966fd16b177SBarry Smith 
1967251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1968251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
196955849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
19702b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1971e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1972536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1973f05ece33SBarry Smith #endif
197432077d6dSBarry Smith   if (iascii) {
1975dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
1976efd4aadfSBarry Smith     if (ts->ops->view) {
1977efd4aadfSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1978efd4aadfSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1979efd4aadfSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1980efd4aadfSBarry Smith     }
1981ef85077eSLisandro Dalcin     if (ts->max_steps < PETSC_MAX_INT) {
198277431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
1983ef85077eSLisandro Dalcin     }
1984ef85077eSLisandro Dalcin     if (ts->max_time < PETSC_MAX_REAL) {
19857c8652ddSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1986ef85077eSLisandro Dalcin     }
1987efd4aadfSBarry Smith     if (ts->usessnes) {
1988efd4aadfSBarry Smith       PetscBool lin;
1989d763cef2SBarry Smith       if (ts->problem_type == TS_NONLINEAR) {
19905ef26d82SJed Brown         ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1991d763cef2SBarry Smith       }
19925ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
19931ef27442SStefano Zampini       ierr = PetscObjectTypeCompareAny((PetscObject)ts->snes,&lin,SNESKSPONLY,SNESKSPTRANSPOSEONLY,"");CHKERRQ(ierr);
1994efd4aadfSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  total number of %slinear solve failures=%D\n",lin ? "" : "non",ts->num_snes_failures);CHKERRQ(ierr);
1995efd4aadfSBarry Smith     }
1996193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
1997a0af407cSBarry Smith     if (ts->vrtol) {
1998a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of relative error tolerances, ");CHKERRQ(ierr);
1999a0af407cSBarry Smith     } else {
2000a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using relative error tolerance of %g, ",(double)ts->rtol);CHKERRQ(ierr);
2001a0af407cSBarry Smith     }
2002a0af407cSBarry Smith     if (ts->vatol) {
2003a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using vector of absolute error tolerances\n");CHKERRQ(ierr);
2004a0af407cSBarry Smith     } else {
2005a0af407cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  using absolute error tolerance of %g\n",(double)ts->atol);CHKERRQ(ierr);
2006a0af407cSBarry Smith     }
2007825ab935SBarry Smith     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2008efd4aadfSBarry Smith     ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);
2009825ab935SBarry Smith     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2010825ab935SBarry Smith     if (ts->snes && ts->usessnes)  {
2011825ab935SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2012825ab935SBarry Smith       ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
2013825ab935SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2014825ab935SBarry Smith     }
20152d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
20162d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
20170f5bd95cSBarry Smith   } else if (isstring) {
2018a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
2019b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
202055849f57SBarry Smith   } else if (isbinary) {
202155849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
202255849f57SBarry Smith     MPI_Comm    comm;
202355849f57SBarry Smith     PetscMPIInt rank;
202455849f57SBarry Smith     char        type[256];
202555849f57SBarry Smith 
202655849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
202755849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
202855849f57SBarry Smith     if (!rank) {
202955849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
203055849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
203155849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
203255849f57SBarry Smith     }
203355849f57SBarry Smith     if (ts->ops->view) {
203455849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
203555849f57SBarry Smith     }
2036efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2037f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
2038f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
20392d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
20402d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
20412b0a91c0SBarry Smith   } else if (isdraw) {
20422b0a91c0SBarry Smith     PetscDraw draw;
20432b0a91c0SBarry Smith     char      str[36];
204489fd9fafSBarry Smith     PetscReal x,y,bottom,h;
20452b0a91c0SBarry Smith 
20462b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
20472b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
20482b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
20492b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
205051fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
205189fd9fafSBarry Smith     bottom = y - h;
20522b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
20532b0a91c0SBarry Smith     if (ts->ops->view) {
20542b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
20552b0a91c0SBarry Smith     }
2056efd4aadfSBarry Smith     if (ts->adapt) {ierr = TSAdaptView(ts->adapt,viewer);CHKERRQ(ierr);}
2057efd4aadfSBarry Smith     if (ts->snes)  {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
20582b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
2059e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
2060536b137fSBarry Smith   } else if (issaws) {
2061d45a07a7SBarry Smith     PetscMPIInt rank;
20622657e9d9SBarry Smith     const char  *name;
20632657e9d9SBarry Smith 
20642657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
2065d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
2066d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
2067d45a07a7SBarry Smith       char       dir[1024];
2068d45a07a7SBarry Smith 
2069e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
2070a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
20712657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
20722657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
20732657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
2074d763cef2SBarry Smith     }
20750acecf5bSBarry Smith     if (ts->ops->view) {
20760acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
20770acecf5bSBarry Smith     }
2078f05ece33SBarry Smith #endif
2079f05ece33SBarry Smith   }
2080f05ece33SBarry Smith 
2081b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
2082251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
2083b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2084d763cef2SBarry Smith   PetscFunctionReturn(0);
2085d763cef2SBarry Smith }
2086d763cef2SBarry Smith 
2087b07ff414SBarry Smith /*@
2088d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
2089d763cef2SBarry Smith    the timesteppers.
2090d763cef2SBarry Smith 
20913f9fe445SBarry Smith    Logically Collective on TS
2092d763cef2SBarry Smith 
2093d763cef2SBarry Smith    Input Parameters:
2094d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2095d763cef2SBarry Smith -  usrP - optional user context
2096d763cef2SBarry Smith 
209795452b02SPatrick Sanan    Fortran Notes:
209895452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2099daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2100daf670e6SBarry Smith 
2101d763cef2SBarry Smith    Level: intermediate
2102d763cef2SBarry Smith 
2103d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
2104d763cef2SBarry Smith 
2105d763cef2SBarry Smith .seealso: TSGetApplicationContext()
2106d763cef2SBarry Smith @*/
21077087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
2108d763cef2SBarry Smith {
2109d763cef2SBarry Smith   PetscFunctionBegin;
21100700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2111d763cef2SBarry Smith   ts->user = usrP;
2112d763cef2SBarry Smith   PetscFunctionReturn(0);
2113d763cef2SBarry Smith }
2114d763cef2SBarry Smith 
2115b07ff414SBarry Smith /*@
2116d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
2117d763cef2SBarry Smith     timestepper.
2118d763cef2SBarry Smith 
2119d763cef2SBarry Smith     Not Collective
2120d763cef2SBarry Smith 
2121d763cef2SBarry Smith     Input Parameter:
2122d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
2123d763cef2SBarry Smith 
2124d763cef2SBarry Smith     Output Parameter:
2125d763cef2SBarry Smith .   usrP - user context
2126d763cef2SBarry Smith 
212795452b02SPatrick Sanan    Fortran Notes:
212895452b02SPatrick Sanan     To use this from Fortran you must write a Fortran interface definition for this
2129daf670e6SBarry Smith     function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument.
2130daf670e6SBarry Smith 
2131d763cef2SBarry Smith     Level: intermediate
2132d763cef2SBarry Smith 
2133d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
2134d763cef2SBarry Smith 
2135d763cef2SBarry Smith .seealso: TSSetApplicationContext()
2136d763cef2SBarry Smith @*/
2137e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
2138d763cef2SBarry Smith {
2139d763cef2SBarry Smith   PetscFunctionBegin;
21400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2141e71120c6SJed Brown   *(void**)usrP = ts->user;
2142d763cef2SBarry Smith   PetscFunctionReturn(0);
2143d763cef2SBarry Smith }
2144d763cef2SBarry Smith 
2145d763cef2SBarry Smith /*@
214680275a0aSLisandro Dalcin    TSGetStepNumber - Gets the number of steps completed.
2147d763cef2SBarry Smith 
2148d763cef2SBarry Smith    Not Collective
2149d763cef2SBarry Smith 
2150d763cef2SBarry Smith    Input Parameter:
2151d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2152d763cef2SBarry Smith 
2153d763cef2SBarry Smith    Output Parameter:
215480275a0aSLisandro Dalcin .  steps - number of steps completed so far
2155d763cef2SBarry Smith 
2156d763cef2SBarry Smith    Level: intermediate
2157d763cef2SBarry Smith 
2158d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
21599be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
2160d763cef2SBarry Smith @*/
216180275a0aSLisandro Dalcin PetscErrorCode TSGetStepNumber(TS ts,PetscInt *steps)
2162d763cef2SBarry Smith {
2163d763cef2SBarry Smith   PetscFunctionBegin;
21640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
216580275a0aSLisandro Dalcin   PetscValidIntPointer(steps,2);
216680275a0aSLisandro Dalcin   *steps = ts->steps;
216780275a0aSLisandro Dalcin   PetscFunctionReturn(0);
216880275a0aSLisandro Dalcin }
216980275a0aSLisandro Dalcin 
217080275a0aSLisandro Dalcin /*@
217180275a0aSLisandro Dalcin    TSSetStepNumber - Sets the number of steps completed.
217280275a0aSLisandro Dalcin 
217380275a0aSLisandro Dalcin    Logically Collective on TS
217480275a0aSLisandro Dalcin 
217580275a0aSLisandro Dalcin    Input Parameters:
217680275a0aSLisandro Dalcin +  ts - the TS context
217780275a0aSLisandro Dalcin -  steps - number of steps completed so far
217880275a0aSLisandro Dalcin 
217980275a0aSLisandro Dalcin    Notes:
218080275a0aSLisandro Dalcin    For most uses of the TS solvers the user need not explicitly call
218180275a0aSLisandro Dalcin    TSSetStepNumber(), as the step counter is appropriately updated in
218280275a0aSLisandro Dalcin    TSSolve()/TSStep()/TSRollBack(). Power users may call this routine to
218380275a0aSLisandro Dalcin    reinitialize timestepping by setting the step counter to zero (and time
218480275a0aSLisandro Dalcin    to the initial time) to solve a similar problem with different initial
218580275a0aSLisandro Dalcin    conditions or parameters. Other possible use case is to continue
218680275a0aSLisandro Dalcin    timestepping from a previously interrupted run in such a way that TS
218780275a0aSLisandro Dalcin    monitors will be called with a initial nonzero step counter.
218880275a0aSLisandro Dalcin 
218980275a0aSLisandro Dalcin    Level: advanced
219080275a0aSLisandro Dalcin 
219180275a0aSLisandro Dalcin .keywords: TS, timestep, set, iteration, number
219280275a0aSLisandro Dalcin .seealso: TSGetStepNumber(), TSSetTime(), TSSetTimeStep(), TSSetSolution()
219380275a0aSLisandro Dalcin @*/
219480275a0aSLisandro Dalcin PetscErrorCode TSSetStepNumber(TS ts,PetscInt steps)
219580275a0aSLisandro Dalcin {
219680275a0aSLisandro Dalcin   PetscFunctionBegin;
219780275a0aSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
219880275a0aSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,steps,2);
219980275a0aSLisandro Dalcin   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Step number must be non-negative");
220080275a0aSLisandro Dalcin   ts->steps = steps;
2201d763cef2SBarry Smith   PetscFunctionReturn(0);
2202d763cef2SBarry Smith }
2203d763cef2SBarry Smith 
2204d763cef2SBarry Smith /*@
2205d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
2206d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
2207d763cef2SBarry Smith 
22083f9fe445SBarry Smith    Logically Collective on TS
2209d763cef2SBarry Smith 
2210d763cef2SBarry Smith    Input Parameters:
2211d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2212d763cef2SBarry Smith -  time_step - the size of the timestep
2213d763cef2SBarry Smith 
2214d763cef2SBarry Smith    Level: intermediate
2215d763cef2SBarry Smith 
2216aaa6c58dSLisandro Dalcin .seealso: TSGetTimeStep(), TSSetTime()
2217d763cef2SBarry Smith 
2218d763cef2SBarry Smith .keywords: TS, set, timestep
2219d763cef2SBarry Smith @*/
22207087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
2221d763cef2SBarry Smith {
2222d763cef2SBarry Smith   PetscFunctionBegin;
22230700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2224c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
2225d763cef2SBarry Smith   ts->time_step = time_step;
2226d763cef2SBarry Smith   PetscFunctionReturn(0);
2227d763cef2SBarry Smith }
2228d763cef2SBarry Smith 
2229a43b19c4SJed Brown /*@
223049354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
223149354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
223249354f04SShri Abhyankar      or just return at the final time TS computed.
2233a43b19c4SJed Brown 
2234a43b19c4SJed Brown   Logically Collective on TS
2235a43b19c4SJed Brown 
2236a43b19c4SJed Brown    Input Parameter:
2237a43b19c4SJed Brown +   ts - the time-step context
223849354f04SShri Abhyankar -   eftopt - exact final time option
2239a43b19c4SJed Brown 
2240feed9e9dSBarry Smith $  TS_EXACTFINALTIME_STEPOVER    - Don't do anything if final time is exceeded
2241feed9e9dSBarry Smith $  TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time
2242feed9e9dSBarry Smith $  TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to match the final time
2243feed9e9dSBarry Smith 
2244feed9e9dSBarry Smith    Options Database:
2245feed9e9dSBarry Smith .   -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step at runtime
2246feed9e9dSBarry Smith 
2247ee346746SBarry Smith    Warning: If you use the option TS_EXACTFINALTIME_STEPOVER the solution may be at a very different time
2248ee346746SBarry Smith     then the final time you selected.
2249ee346746SBarry Smith 
2250a43b19c4SJed Brown    Level: beginner
2251a43b19c4SJed Brown 
2252f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSGetExactFinalTime()
2253a43b19c4SJed Brown @*/
225449354f04SShri Abhyankar PetscErrorCode TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
2255a43b19c4SJed Brown {
2256a43b19c4SJed Brown   PetscFunctionBegin;
2257a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
225849354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
225949354f04SShri Abhyankar   ts->exact_final_time = eftopt;
2260a43b19c4SJed Brown   PetscFunctionReturn(0);
2261a43b19c4SJed Brown }
2262a43b19c4SJed Brown 
2263d763cef2SBarry Smith /*@
2264f6953c82SLisandro Dalcin    TSGetExactFinalTime - Gets the exact final time option.
2265f6953c82SLisandro Dalcin 
2266f6953c82SLisandro Dalcin    Not Collective
2267f6953c82SLisandro Dalcin 
2268f6953c82SLisandro Dalcin    Input Parameter:
2269f6953c82SLisandro Dalcin .  ts - the TS context
2270f6953c82SLisandro Dalcin 
2271f6953c82SLisandro Dalcin    Output Parameter:
2272f6953c82SLisandro Dalcin .  eftopt - exact final time option
2273f6953c82SLisandro Dalcin 
2274f6953c82SLisandro Dalcin    Level: beginner
2275f6953c82SLisandro Dalcin 
2276f6953c82SLisandro Dalcin .seealso: TSExactFinalTimeOption, TSSetExactFinalTime()
2277f6953c82SLisandro Dalcin @*/
2278f6953c82SLisandro Dalcin PetscErrorCode TSGetExactFinalTime(TS ts,TSExactFinalTimeOption *eftopt)
2279f6953c82SLisandro Dalcin {
2280f6953c82SLisandro Dalcin   PetscFunctionBegin;
2281f6953c82SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2282f6953c82SLisandro Dalcin   PetscValidPointer(eftopt,2);
2283f6953c82SLisandro Dalcin   *eftopt = ts->exact_final_time;
2284f6953c82SLisandro Dalcin   PetscFunctionReturn(0);
2285f6953c82SLisandro Dalcin }
2286f6953c82SLisandro Dalcin 
2287f6953c82SLisandro Dalcin /*@
2288d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
2289d763cef2SBarry Smith 
2290d763cef2SBarry Smith    Not Collective
2291d763cef2SBarry Smith 
2292d763cef2SBarry Smith    Input Parameter:
2293d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2294d763cef2SBarry Smith 
2295d763cef2SBarry Smith    Output Parameter:
2296d763cef2SBarry Smith .  dt - the current timestep size
2297d763cef2SBarry Smith 
2298d763cef2SBarry Smith    Level: intermediate
2299d763cef2SBarry Smith 
2300aaa6c58dSLisandro Dalcin .seealso: TSSetTimeStep(), TSGetTime()
2301d763cef2SBarry Smith 
2302d763cef2SBarry Smith .keywords: TS, get, timestep
2303d763cef2SBarry Smith @*/
23047087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
2305d763cef2SBarry Smith {
2306d763cef2SBarry Smith   PetscFunctionBegin;
23070700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2308f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
2309d763cef2SBarry Smith   *dt = ts->time_step;
2310d763cef2SBarry Smith   PetscFunctionReturn(0);
2311d763cef2SBarry Smith }
2312d763cef2SBarry Smith 
2313d8e5e3e6SSatish Balay /*@
2314d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
2315d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
2316d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
2317d763cef2SBarry Smith    the solution at the next timestep has been calculated.
2318d763cef2SBarry Smith 
2319d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
2320d763cef2SBarry Smith 
2321d763cef2SBarry Smith    Input Parameter:
2322d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2323d763cef2SBarry Smith 
2324d763cef2SBarry Smith    Output Parameter:
2325d763cef2SBarry Smith .  v - the vector containing the solution
2326d763cef2SBarry Smith 
232763e21af5SBarry Smith    Note: If you used TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP); this does not return the solution at the requested
232863e21af5SBarry Smith    final time. It returns the solution at the next timestep.
232963e21af5SBarry Smith 
2330d763cef2SBarry Smith    Level: intermediate
2331d763cef2SBarry Smith 
23320ed3bfb6SBarry Smith .seealso: TSGetTimeStep(), TSGetTime(), TSGetSolveTime(), TSGetSolutionComponents(), TSSetSolutionFunction()
2333d763cef2SBarry Smith 
2334d763cef2SBarry Smith .keywords: TS, timestep, get, solution
2335d763cef2SBarry Smith @*/
23367087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
2337d763cef2SBarry Smith {
2338d763cef2SBarry Smith   PetscFunctionBegin;
23390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
23404482741eSBarry Smith   PetscValidPointer(v,2);
23418737fe31SLisandro Dalcin   *v = ts->vec_sol;
2342d763cef2SBarry Smith   PetscFunctionReturn(0);
2343d763cef2SBarry Smith }
2344d763cef2SBarry Smith 
234503fe5f5eSDebojyoti Ghosh /*@
2346b2bf4f3aSDebojyoti Ghosh    TSGetSolutionComponents - Returns any solution components at the present
234703fe5f5eSDebojyoti Ghosh    timestep, if available for the time integration method being used.
2348b2bf4f3aSDebojyoti Ghosh    Solution components are quantities that share the same size and
234903fe5f5eSDebojyoti Ghosh    structure as the solution vector.
235003fe5f5eSDebojyoti Ghosh 
235103fe5f5eSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
235203fe5f5eSDebojyoti Ghosh 
235303fe5f5eSDebojyoti Ghosh    Parameters :
235403fe5f5eSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2355b2bf4f3aSDebojyoti Ghosh .  n - If v is PETSC_NULL, then the number of solution components is
2356b2bf4f3aSDebojyoti Ghosh        returned through n, else the n-th solution component is
235703fe5f5eSDebojyoti Ghosh        returned in v.
2358b2bf4f3aSDebojyoti Ghosh .  v - the vector containing the n-th solution component
235903fe5f5eSDebojyoti Ghosh        (may be PETSC_NULL to use this function to find out
2360b2bf4f3aSDebojyoti Ghosh         the number of solutions components).
236103fe5f5eSDebojyoti Ghosh 
23624cdd57e5SDebojyoti Ghosh    Level: advanced
236303fe5f5eSDebojyoti Ghosh 
236403fe5f5eSDebojyoti Ghosh .seealso: TSGetSolution()
236503fe5f5eSDebojyoti Ghosh 
236603fe5f5eSDebojyoti Ghosh .keywords: TS, timestep, get, solution
236703fe5f5eSDebojyoti Ghosh @*/
2368b2bf4f3aSDebojyoti Ghosh PetscErrorCode  TSGetSolutionComponents(TS ts,PetscInt *n,Vec *v)
236903fe5f5eSDebojyoti Ghosh {
237003fe5f5eSDebojyoti Ghosh   PetscErrorCode ierr;
237103fe5f5eSDebojyoti Ghosh 
237203fe5f5eSDebojyoti Ghosh   PetscFunctionBegin;
237303fe5f5eSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2374b2bf4f3aSDebojyoti Ghosh   if (!ts->ops->getsolutioncomponents) *n = 0;
237503fe5f5eSDebojyoti Ghosh   else {
2376b2bf4f3aSDebojyoti Ghosh     ierr = (*ts->ops->getsolutioncomponents)(ts,n,v);CHKERRQ(ierr);
237703fe5f5eSDebojyoti Ghosh   }
237803fe5f5eSDebojyoti Ghosh   PetscFunctionReturn(0);
237903fe5f5eSDebojyoti Ghosh }
238003fe5f5eSDebojyoti Ghosh 
23814cdd57e5SDebojyoti Ghosh /*@
23824cdd57e5SDebojyoti Ghosh    TSGetAuxSolution - Returns an auxiliary solution at the present
23834cdd57e5SDebojyoti Ghosh    timestep, if available for the time integration method being used.
23844cdd57e5SDebojyoti Ghosh 
23854cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
23864cdd57e5SDebojyoti Ghosh 
23874cdd57e5SDebojyoti Ghosh    Parameters :
23884cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
23894cdd57e5SDebojyoti Ghosh .  v - the vector containing the auxiliary solution
23904cdd57e5SDebojyoti Ghosh 
23914cdd57e5SDebojyoti Ghosh    Level: intermediate
23924cdd57e5SDebojyoti Ghosh 
23934cdd57e5SDebojyoti Ghosh .seealso: TSGetSolution()
23944cdd57e5SDebojyoti Ghosh 
23954cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, solution
23964cdd57e5SDebojyoti Ghosh @*/
23974cdd57e5SDebojyoti Ghosh PetscErrorCode  TSGetAuxSolution(TS ts,Vec *v)
23984cdd57e5SDebojyoti Ghosh {
23994cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
24004cdd57e5SDebojyoti Ghosh 
24014cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
24024cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2403f6356ec7SDebojyoti Ghosh   if (ts->ops->getauxsolution) {
24044cdd57e5SDebojyoti Ghosh     ierr = (*ts->ops->getauxsolution)(ts,v);CHKERRQ(ierr);
2405f6356ec7SDebojyoti Ghosh   } else {
2406f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v); CHKERRQ(ierr);
2407f6356ec7SDebojyoti Ghosh   }
24084cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
24094cdd57e5SDebojyoti Ghosh }
24104cdd57e5SDebojyoti Ghosh 
24114cdd57e5SDebojyoti Ghosh /*@
24124cdd57e5SDebojyoti Ghosh    TSGetTimeError - Returns the estimated error vector, if the chosen
24134cdd57e5SDebojyoti Ghosh    TSType has an error estimation functionality.
24144cdd57e5SDebojyoti Ghosh 
24154cdd57e5SDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
24164cdd57e5SDebojyoti Ghosh 
24179657682dSDebojyoti Ghosh    Note: MUST call after TSSetUp()
24189657682dSDebojyoti Ghosh 
24194cdd57e5SDebojyoti Ghosh    Parameters :
24204cdd57e5SDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
2421657c1e31SEmil Constantinescu .  n - current estimate (n=0) or previous one (n=-1)
24224cdd57e5SDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
24234cdd57e5SDebojyoti Ghosh 
24244cdd57e5SDebojyoti Ghosh    Level: intermediate
24254cdd57e5SDebojyoti Ghosh 
242657df6a1bSDebojyoti Ghosh .seealso: TSGetSolution(), TSSetTimeError()
24274cdd57e5SDebojyoti Ghosh 
24284cdd57e5SDebojyoti Ghosh .keywords: TS, timestep, get, error
24294cdd57e5SDebojyoti Ghosh @*/
24300a01e1b2SEmil Constantinescu PetscErrorCode  TSGetTimeError(TS ts,PetscInt n,Vec *v)
24314cdd57e5SDebojyoti Ghosh {
24324cdd57e5SDebojyoti Ghosh   PetscErrorCode ierr;
24334cdd57e5SDebojyoti Ghosh 
24344cdd57e5SDebojyoti Ghosh   PetscFunctionBegin;
24354cdd57e5SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2436f6356ec7SDebojyoti Ghosh   if (ts->ops->gettimeerror) {
24370a01e1b2SEmil Constantinescu     ierr = (*ts->ops->gettimeerror)(ts,n,v);CHKERRQ(ierr);
2438f6356ec7SDebojyoti Ghosh   } else {
2439f6356ec7SDebojyoti Ghosh     ierr = VecZeroEntries(*v);CHKERRQ(ierr);
2440f6356ec7SDebojyoti Ghosh   }
24414cdd57e5SDebojyoti Ghosh   PetscFunctionReturn(0);
24424cdd57e5SDebojyoti Ghosh }
24434cdd57e5SDebojyoti Ghosh 
244457df6a1bSDebojyoti Ghosh /*@
244557df6a1bSDebojyoti Ghosh    TSSetTimeError - Sets the estimated error vector, if the chosen
244657df6a1bSDebojyoti Ghosh    TSType has an error estimation functionality. This can be used
244757df6a1bSDebojyoti Ghosh    to restart such a time integrator with a given error vector.
244857df6a1bSDebojyoti Ghosh 
244957df6a1bSDebojyoti Ghosh    Not Collective, but Vec returned is parallel if TS is parallel
245057df6a1bSDebojyoti Ghosh 
245157df6a1bSDebojyoti Ghosh    Parameters :
245257df6a1bSDebojyoti Ghosh .  ts - the TS context obtained from TSCreate() (input parameter).
245357df6a1bSDebojyoti Ghosh .  v - the vector containing the error (same size as the solution).
245457df6a1bSDebojyoti Ghosh 
245557df6a1bSDebojyoti Ghosh    Level: intermediate
245657df6a1bSDebojyoti Ghosh 
245757df6a1bSDebojyoti Ghosh .seealso: TSSetSolution(), TSGetTimeError)
245857df6a1bSDebojyoti Ghosh 
245957df6a1bSDebojyoti Ghosh .keywords: TS, timestep, get, error
246057df6a1bSDebojyoti Ghosh @*/
246157df6a1bSDebojyoti Ghosh PetscErrorCode  TSSetTimeError(TS ts,Vec v)
246257df6a1bSDebojyoti Ghosh {
246357df6a1bSDebojyoti Ghosh   PetscErrorCode ierr;
246457df6a1bSDebojyoti Ghosh 
246557df6a1bSDebojyoti Ghosh   PetscFunctionBegin;
246657df6a1bSDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24679657682dSDebojyoti Ghosh   if (!ts->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetUp() first");
246857df6a1bSDebojyoti Ghosh   if (ts->ops->settimeerror) {
246957df6a1bSDebojyoti Ghosh     ierr = (*ts->ops->settimeerror)(ts,v);CHKERRQ(ierr);
247057df6a1bSDebojyoti Ghosh   }
247157df6a1bSDebojyoti Ghosh   PetscFunctionReturn(0);
247257df6a1bSDebojyoti Ghosh }
247357df6a1bSDebojyoti Ghosh 
2474bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
2475d8e5e3e6SSatish Balay /*@
2476bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
2477d763cef2SBarry Smith 
2478bdad233fSMatthew Knepley   Not collective
2479d763cef2SBarry Smith 
2480bdad233fSMatthew Knepley   Input Parameters:
2481bdad233fSMatthew Knepley + ts   - The TS
2482bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2483d763cef2SBarry Smith .vb
24840910c330SBarry Smith          U_t - A U = 0      (linear)
24850910c330SBarry Smith          U_t - A(t) U = 0   (linear)
24860910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
2487d763cef2SBarry Smith .ve
2488d763cef2SBarry Smith 
2489d763cef2SBarry Smith    Level: beginner
2490d763cef2SBarry Smith 
2491bdad233fSMatthew Knepley .keywords: TS, problem type
2492bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2493d763cef2SBarry Smith @*/
24947087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
2495a7cc72afSBarry Smith {
24969e2a6581SJed Brown   PetscErrorCode ierr;
24979e2a6581SJed Brown 
2498d763cef2SBarry Smith   PetscFunctionBegin;
24990700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2500bdad233fSMatthew Knepley   ts->problem_type = type;
25019e2a6581SJed Brown   if (type == TS_LINEAR) {
25029e2a6581SJed Brown     SNES snes;
25039e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
25049e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
25059e2a6581SJed Brown   }
2506d763cef2SBarry Smith   PetscFunctionReturn(0);
2507d763cef2SBarry Smith }
2508d763cef2SBarry Smith 
2509bdad233fSMatthew Knepley /*@C
2510bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
2511bdad233fSMatthew Knepley 
2512bdad233fSMatthew Knepley   Not collective
2513bdad233fSMatthew Knepley 
2514bdad233fSMatthew Knepley   Input Parameter:
2515bdad233fSMatthew Knepley . ts   - The TS
2516bdad233fSMatthew Knepley 
2517bdad233fSMatthew Knepley   Output Parameter:
2518bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
2519bdad233fSMatthew Knepley .vb
2520089b2837SJed Brown          M U_t = A U
2521089b2837SJed Brown          M(t) U_t = A(t) U
2522b5abc632SBarry Smith          F(t,U,U_t)
2523bdad233fSMatthew Knepley .ve
2524bdad233fSMatthew Knepley 
2525bdad233fSMatthew Knepley    Level: beginner
2526bdad233fSMatthew Knepley 
2527bdad233fSMatthew Knepley .keywords: TS, problem type
2528bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
2529bdad233fSMatthew Knepley @*/
25307087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
2531a7cc72afSBarry Smith {
2532bdad233fSMatthew Knepley   PetscFunctionBegin;
25330700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
25344482741eSBarry Smith   PetscValidIntPointer(type,2);
2535bdad233fSMatthew Knepley   *type = ts->problem_type;
2536bdad233fSMatthew Knepley   PetscFunctionReturn(0);
2537bdad233fSMatthew Knepley }
2538d763cef2SBarry Smith 
2539d763cef2SBarry Smith /*@
2540d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
2541d763cef2SBarry Smith    of a timestepper.
2542d763cef2SBarry Smith 
2543d763cef2SBarry Smith    Collective on TS
2544d763cef2SBarry Smith 
2545d763cef2SBarry Smith    Input Parameter:
2546d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2547d763cef2SBarry Smith 
2548d763cef2SBarry Smith    Notes:
2549d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
2550d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
2551141bd67dSStefano Zampini    the call to TSStep() or TSSolve().  However, if one wishes to control this
2552d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
2553141bd67dSStefano Zampini    and optional routines of the form TSSetXXX(), but before TSStep() and TSSolve().
2554d763cef2SBarry Smith 
2555d763cef2SBarry Smith    Level: advanced
2556d763cef2SBarry Smith 
2557d763cef2SBarry Smith .keywords: TS, timestep, setup
2558d763cef2SBarry Smith 
2559141bd67dSStefano Zampini .seealso: TSCreate(), TSStep(), TSDestroy(), TSSolve()
2560d763cef2SBarry Smith @*/
25617087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
2562d763cef2SBarry Smith {
2563dfbe8321SBarry Smith   PetscErrorCode ierr;
25646c6b9e74SPeter Brune   DM             dm;
25656c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
2566d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
2567cd11d68dSLisandro Dalcin   TSIFunction    ifun;
25686c6b9e74SPeter Brune   TSIJacobian    ijac;
2569efe9872eSLisandro Dalcin   TSI2Jacobian   i2jac;
25706c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
25712ffb9264SLisandro Dalcin   PetscBool      isnone;
2572d763cef2SBarry Smith 
2573d763cef2SBarry Smith   PetscFunctionBegin;
25740700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2575277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
2576277b19d0SLisandro Dalcin 
25777adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
2578cd11d68dSLisandro Dalcin     ierr = TSGetIFunction(ts,NULL,&ifun,NULL);CHKERRQ(ierr);
2579cd11d68dSLisandro Dalcin     ierr = TSSetType(ts,ifun ? TSBEULER : TSEULER);CHKERRQ(ierr);
2580d763cef2SBarry Smith   }
2581277b19d0SLisandro Dalcin 
2582277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
2583277b19d0SLisandro Dalcin 
2584971015bcSStefano Zampini   ierr = TSGetRHSJacobian(ts,NULL,NULL,&rhsjac,NULL);CHKERRQ(ierr);
2585971015bcSStefano Zampini   if (ts->rhsjacobian.reuse && rhsjac == TSComputeRHSJacobianConstant) {
2586e1244c69SJed Brown     Mat Amat,Pmat;
2587e1244c69SJed Brown     SNES snes;
2588e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2589e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
2590e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
2591e1244c69SJed Brown      * have displaced the RHS matrix */
2592971015bcSStefano Zampini     if (Amat && Amat == ts->Arhs) {
2593abc0d4abSBarry 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 */
2594abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Arhs,MAT_COPY_VALUES,&Amat);CHKERRQ(ierr);
2595e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
2596e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
2597e1244c69SJed Brown     }
2598971015bcSStefano Zampini     if (Pmat && Pmat == ts->Brhs) {
2599abc0d4abSBarry Smith       ierr = MatDuplicate(ts->Brhs,MAT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
2600e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
2601e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
2602e1244c69SJed Brown     }
2603e1244c69SJed Brown   }
26042ffb9264SLisandro Dalcin 
26052ffb9264SLisandro Dalcin   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
26062ffb9264SLisandro Dalcin   ierr = TSAdaptSetDefaultType(ts->adapt,ts->default_adapt_type);CHKERRQ(ierr);
26072ffb9264SLisandro Dalcin 
2608277b19d0SLisandro Dalcin   if (ts->ops->setup) {
2609000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
2610277b19d0SLisandro Dalcin   }
2611277b19d0SLisandro Dalcin 
26122ffb9264SLisandro Dalcin   /* Attempt to check/preset a default value for the exact final time option */
26132ffb9264SLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject)ts->adapt,TSADAPTNONE,&isnone);CHKERRQ(ierr);
26142ffb9264SLisandro Dalcin   if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED)
26152ffb9264SLisandro Dalcin     ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP;
26162ffb9264SLisandro Dalcin 
2617a6772fa2SLisandro Dalcin   /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
26186c6b9e74SPeter Brune      to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
26196c6b9e74SPeter Brune    */
26206c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
26210298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
26226c6b9e74SPeter Brune   if (!func) {
26236c6b9e74SPeter Brune     ierr = DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
26246c6b9e74SPeter Brune   }
2625a6772fa2SLisandro 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.
26266c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
26276c6b9e74SPeter Brune    */
26280298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
26290298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
2630efe9872eSLisandro Dalcin   ierr = DMTSGetI2Jacobian(dm,&i2jac,NULL);CHKERRQ(ierr);
26310298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
2632efe9872eSLisandro Dalcin   if (!jac && (ijac || i2jac || rhsjac)) {
26336c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
26346c6b9e74SPeter Brune   }
2635c0517034SDebojyoti Ghosh 
2636c0517034SDebojyoti Ghosh   /* if time integration scheme has a starting method, call it */
2637c0517034SDebojyoti Ghosh   if (ts->ops->startingmethod) {
2638c0517034SDebojyoti Ghosh     ierr = (*ts->ops->startingmethod)(ts);CHKERRQ(ierr);
2639c0517034SDebojyoti Ghosh   }
2640c0517034SDebojyoti Ghosh 
2641277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
2642277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
2643277b19d0SLisandro Dalcin }
2644277b19d0SLisandro Dalcin 
2645f6a906c0SBarry Smith /*@
2646277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
2647277b19d0SLisandro Dalcin 
2648277b19d0SLisandro Dalcin    Collective on TS
2649277b19d0SLisandro Dalcin 
2650277b19d0SLisandro Dalcin    Input Parameter:
2651277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2652277b19d0SLisandro Dalcin 
2653277b19d0SLisandro Dalcin    Level: beginner
2654277b19d0SLisandro Dalcin 
2655277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
2656277b19d0SLisandro Dalcin 
2657277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
2658277b19d0SLisandro Dalcin @*/
2659277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
2660277b19d0SLisandro Dalcin {
26611d06f6b3SHong Zhang   TS_RHSSplitLink ilink = ts->tsrhssplit,next;
2662277b19d0SLisandro Dalcin   PetscErrorCode  ierr;
2663277b19d0SLisandro Dalcin 
2664277b19d0SLisandro Dalcin   PetscFunctionBegin;
2665277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2666b18ea86cSHong Zhang 
2667277b19d0SLisandro Dalcin   if (ts->ops->reset) {
2668277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
2669277b19d0SLisandro Dalcin   }
2670277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
2671e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
2672bbd56ea5SKarl Rupp 
26734e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
26744e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
2675214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
26766bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2677efe9872eSLisandro Dalcin   ierr = VecDestroy(&ts->vec_dot);CHKERRQ(ierr);
2678e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
2679e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
268038637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
2681bbd56ea5SKarl Rupp 
2682d8151eeaSBarry Smith   ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
2683d8151eeaSBarry Smith   ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2684715f1b00SHong Zhang 
2685ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
26862c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
268736eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
268813b1b0ffSHong Zhang   ierr = MatDestroy(&ts->mat_sensip);CHKERRQ(ierr);
2689022c081aSHong Zhang 
26901d06f6b3SHong Zhang   while (ilink) {
26911d06f6b3SHong Zhang     next = ilink->next;
26921d06f6b3SHong Zhang     ierr = TSDestroy(&ilink->ts);CHKERRQ(ierr);
26931d06f6b3SHong Zhang     ierr = PetscFree(ilink->splitname);CHKERRQ(ierr);
26941d06f6b3SHong Zhang     ierr = ISDestroy(&ilink->is);CHKERRQ(ierr);
26951d06f6b3SHong Zhang     ierr = PetscFree(ilink);CHKERRQ(ierr);
26961d06f6b3SHong Zhang     ilink = next;
269787f4e208SHong Zhang   }
2698545aaa6fSHong Zhang   ts->num_rhs_splits = 0;
2699277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2700d763cef2SBarry Smith   PetscFunctionReturn(0);
2701d763cef2SBarry Smith }
2702d763cef2SBarry Smith 
2703d8e5e3e6SSatish Balay /*@
2704d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2705d763cef2SBarry Smith    with TSCreate().
2706d763cef2SBarry Smith 
2707d763cef2SBarry Smith    Collective on TS
2708d763cef2SBarry Smith 
2709d763cef2SBarry Smith    Input Parameter:
2710d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2711d763cef2SBarry Smith 
2712d763cef2SBarry Smith    Level: beginner
2713d763cef2SBarry Smith 
2714d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2715d763cef2SBarry Smith 
2716d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2717d763cef2SBarry Smith @*/
27186bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2719d763cef2SBarry Smith {
27206849ba73SBarry Smith   PetscErrorCode ierr;
2721d763cef2SBarry Smith 
2722d763cef2SBarry Smith   PetscFunctionBegin;
27236bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
27246bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
27256bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2726d763cef2SBarry Smith 
27276bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2728277b19d0SLisandro Dalcin 
2729e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2730e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
27316bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
27326d4c513bSLisandro Dalcin 
2733bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2734bc952696SBarry Smith 
273584df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
27366427ac75SLisandro Dalcin   ierr = TSEventDestroy(&(*ts)->event);CHKERRQ(ierr);
27376427ac75SLisandro Dalcin 
27386bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
27396bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
27406bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
27410dd9f2efSHong Zhang   ierr = TSAdjointMonitorCancel((*ts));CHKERRQ(ierr);
27426d4c513bSLisandro Dalcin 
2743a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2744d763cef2SBarry Smith   PetscFunctionReturn(0);
2745d763cef2SBarry Smith }
2746d763cef2SBarry Smith 
2747d8e5e3e6SSatish Balay /*@
2748d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2749d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2750d763cef2SBarry Smith 
2751d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2752d763cef2SBarry Smith 
2753d763cef2SBarry Smith    Input Parameter:
2754d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2755d763cef2SBarry Smith 
2756d763cef2SBarry Smith    Output Parameter:
2757d763cef2SBarry Smith .  snes - the nonlinear solver context
2758d763cef2SBarry Smith 
2759d763cef2SBarry Smith    Notes:
2760d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2761d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
276294b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2763d763cef2SBarry Smith 
2764d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
27650298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2766d763cef2SBarry Smith 
2767d763cef2SBarry Smith    Level: beginner
2768d763cef2SBarry Smith 
2769d763cef2SBarry Smith .keywords: timestep, get, SNES
2770d763cef2SBarry Smith @*/
27717087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2772d763cef2SBarry Smith {
2773d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2774d372ba47SLisandro Dalcin 
2775d763cef2SBarry Smith   PetscFunctionBegin;
27760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27774482741eSBarry Smith   PetscValidPointer(snes,2);
2778d372ba47SLisandro Dalcin   if (!ts->snes) {
2779ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
278016413a6aSBarry Smith     ierr = PetscObjectSetOptions((PetscObject)ts->snes,((PetscObject)ts)->options);CHKERRQ(ierr);
27810298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
27823bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2783d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2784496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
27859e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
27869e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
27879e2a6581SJed Brown     }
2788d372ba47SLisandro Dalcin   }
2789d763cef2SBarry Smith   *snes = ts->snes;
2790d763cef2SBarry Smith   PetscFunctionReturn(0);
2791d763cef2SBarry Smith }
2792d763cef2SBarry Smith 
2793deb2cd25SJed Brown /*@
2794deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2795deb2cd25SJed Brown 
2796deb2cd25SJed Brown    Collective
2797deb2cd25SJed Brown 
2798deb2cd25SJed Brown    Input Parameter:
2799deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2800deb2cd25SJed Brown -  snes - the nonlinear solver context
2801deb2cd25SJed Brown 
2802deb2cd25SJed Brown    Notes:
2803deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2804deb2cd25SJed Brown 
2805deb2cd25SJed Brown    Level: developer
2806deb2cd25SJed Brown 
2807deb2cd25SJed Brown .keywords: timestep, set, SNES
2808deb2cd25SJed Brown @*/
2809deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2810deb2cd25SJed Brown {
2811deb2cd25SJed Brown   PetscErrorCode ierr;
2812d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2813deb2cd25SJed Brown 
2814deb2cd25SJed Brown   PetscFunctionBegin;
2815deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2816deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2817deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2818deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2819bbd56ea5SKarl Rupp 
2820deb2cd25SJed Brown   ts->snes = snes;
2821bbd56ea5SKarl Rupp 
28220298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
28230298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2824740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
28250298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2826740132f1SEmil Constantinescu   }
2827deb2cd25SJed Brown   PetscFunctionReturn(0);
2828deb2cd25SJed Brown }
2829deb2cd25SJed Brown 
2830d8e5e3e6SSatish Balay /*@
283194b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2832d763cef2SBarry Smith    a TS (timestepper) context.
2833d763cef2SBarry Smith 
283494b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2835d763cef2SBarry Smith 
2836d763cef2SBarry Smith    Input Parameter:
2837d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2838d763cef2SBarry Smith 
2839d763cef2SBarry Smith    Output Parameter:
284094b7f48cSBarry Smith .  ksp - the nonlinear solver context
2841d763cef2SBarry Smith 
2842d763cef2SBarry Smith    Notes:
284394b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2844d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2845d763cef2SBarry Smith    KSP and PC contexts as well.
2846d763cef2SBarry Smith 
284794b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
28480298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2849d763cef2SBarry Smith 
2850d763cef2SBarry Smith    Level: beginner
2851d763cef2SBarry Smith 
285294b7f48cSBarry Smith .keywords: timestep, get, KSP
2853d763cef2SBarry Smith @*/
28547087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2855d763cef2SBarry Smith {
2856d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2857089b2837SJed Brown   SNES           snes;
2858d372ba47SLisandro Dalcin 
2859d763cef2SBarry Smith   PetscFunctionBegin;
28600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28614482741eSBarry Smith   PetscValidPointer(ksp,2);
286217186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2863e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2864089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2865089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2866d763cef2SBarry Smith   PetscFunctionReturn(0);
2867d763cef2SBarry Smith }
2868d763cef2SBarry Smith 
2869d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2870d763cef2SBarry Smith 
2871adb62b0dSMatthew Knepley /*@
2872618ce8baSLisandro Dalcin    TSSetMaxSteps - Sets the maximum number of steps to use.
2873618ce8baSLisandro Dalcin 
2874618ce8baSLisandro Dalcin    Logically Collective on TS
2875618ce8baSLisandro Dalcin 
2876618ce8baSLisandro Dalcin    Input Parameters:
2877618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2878618ce8baSLisandro Dalcin -  maxsteps - maximum number of steps to use
2879618ce8baSLisandro Dalcin 
2880618ce8baSLisandro Dalcin    Options Database Keys:
2881618ce8baSLisandro Dalcin .  -ts_max_steps <maxsteps> - Sets maxsteps
2882618ce8baSLisandro Dalcin 
2883618ce8baSLisandro Dalcin    Notes:
2884618ce8baSLisandro Dalcin    The default maximum number of steps is 5000
2885618ce8baSLisandro Dalcin 
2886618ce8baSLisandro Dalcin    Level: intermediate
2887618ce8baSLisandro Dalcin 
2888618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, steps
2889618ce8baSLisandro Dalcin 
2890618ce8baSLisandro Dalcin .seealso: TSGetMaxSteps(), TSSetMaxTime(), TSSetExactFinalTime()
2891618ce8baSLisandro Dalcin @*/
2892618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxSteps(TS ts,PetscInt maxsteps)
2893618ce8baSLisandro Dalcin {
2894618ce8baSLisandro Dalcin   PetscFunctionBegin;
2895618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2896618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2897618ce8baSLisandro Dalcin   if (maxsteps < 0 ) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of steps must be non-negative");
2898618ce8baSLisandro Dalcin   ts->max_steps = maxsteps;
2899618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2900618ce8baSLisandro Dalcin }
2901618ce8baSLisandro Dalcin 
2902618ce8baSLisandro Dalcin /*@
2903618ce8baSLisandro Dalcin    TSGetMaxSteps - Gets the maximum number of steps to use.
2904618ce8baSLisandro Dalcin 
2905618ce8baSLisandro Dalcin    Not Collective
2906618ce8baSLisandro Dalcin 
2907618ce8baSLisandro Dalcin    Input Parameters:
2908618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2909618ce8baSLisandro Dalcin 
2910618ce8baSLisandro Dalcin    Output Parameter:
2911618ce8baSLisandro Dalcin .  maxsteps - maximum number of steps to use
2912618ce8baSLisandro Dalcin 
2913618ce8baSLisandro Dalcin    Level: advanced
2914618ce8baSLisandro Dalcin 
2915618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, steps
2916618ce8baSLisandro Dalcin 
2917618ce8baSLisandro Dalcin .seealso: TSSetMaxSteps(), TSGetMaxTime(), TSSetMaxTime()
2918618ce8baSLisandro Dalcin @*/
2919618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxSteps(TS ts,PetscInt *maxsteps)
2920618ce8baSLisandro Dalcin {
2921618ce8baSLisandro Dalcin   PetscFunctionBegin;
2922618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2923618ce8baSLisandro Dalcin   PetscValidIntPointer(maxsteps,2);
2924618ce8baSLisandro Dalcin   *maxsteps = ts->max_steps;
2925618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2926618ce8baSLisandro Dalcin }
2927618ce8baSLisandro Dalcin 
2928618ce8baSLisandro Dalcin /*@
2929618ce8baSLisandro Dalcin    TSSetMaxTime - Sets the maximum (or final) time for timestepping.
2930618ce8baSLisandro Dalcin 
2931618ce8baSLisandro Dalcin    Logically Collective on TS
2932618ce8baSLisandro Dalcin 
2933618ce8baSLisandro Dalcin    Input Parameters:
2934618ce8baSLisandro Dalcin +  ts - the TS context obtained from TSCreate()
2935618ce8baSLisandro Dalcin -  maxtime - final time to step to
2936618ce8baSLisandro Dalcin 
2937618ce8baSLisandro Dalcin    Options Database Keys:
2938ef85077eSLisandro Dalcin .  -ts_max_time <maxtime> - Sets maxtime
2939618ce8baSLisandro Dalcin 
2940618ce8baSLisandro Dalcin    Notes:
2941618ce8baSLisandro Dalcin    The default maximum time is 5.0
2942618ce8baSLisandro Dalcin 
2943618ce8baSLisandro Dalcin    Level: intermediate
2944618ce8baSLisandro Dalcin 
2945618ce8baSLisandro Dalcin .keywords: TS, timestep, set, maximum, time
2946618ce8baSLisandro Dalcin 
2947618ce8baSLisandro Dalcin .seealso: TSGetMaxTime(), TSSetMaxSteps(), TSSetExactFinalTime()
2948618ce8baSLisandro Dalcin @*/
2949618ce8baSLisandro Dalcin PetscErrorCode TSSetMaxTime(TS ts,PetscReal maxtime)
2950618ce8baSLisandro Dalcin {
2951618ce8baSLisandro Dalcin   PetscFunctionBegin;
2952618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2953618ce8baSLisandro Dalcin   PetscValidLogicalCollectiveReal(ts,maxtime,2);
2954618ce8baSLisandro Dalcin   ts->max_time = maxtime;
2955618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2956618ce8baSLisandro Dalcin }
2957618ce8baSLisandro Dalcin 
2958618ce8baSLisandro Dalcin /*@
2959618ce8baSLisandro Dalcin    TSGetMaxTime - Gets the maximum (or final) time for timestepping.
2960618ce8baSLisandro Dalcin 
2961618ce8baSLisandro Dalcin    Not Collective
2962618ce8baSLisandro Dalcin 
2963618ce8baSLisandro Dalcin    Input Parameters:
2964618ce8baSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
2965618ce8baSLisandro Dalcin 
2966618ce8baSLisandro Dalcin    Output Parameter:
2967618ce8baSLisandro Dalcin .  maxtime - final time to step to
2968618ce8baSLisandro Dalcin 
2969618ce8baSLisandro Dalcin    Level: advanced
2970618ce8baSLisandro Dalcin 
2971618ce8baSLisandro Dalcin .keywords: TS, timestep, get, maximum, time
2972618ce8baSLisandro Dalcin 
2973618ce8baSLisandro Dalcin .seealso: TSSetMaxTime(), TSGetMaxSteps(), TSSetMaxSteps()
2974618ce8baSLisandro Dalcin @*/
2975618ce8baSLisandro Dalcin PetscErrorCode TSGetMaxTime(TS ts,PetscReal *maxtime)
2976618ce8baSLisandro Dalcin {
2977618ce8baSLisandro Dalcin   PetscFunctionBegin;
2978618ce8baSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2979618ce8baSLisandro Dalcin   PetscValidRealPointer(maxtime,2);
2980618ce8baSLisandro Dalcin   *maxtime = ts->max_time;
2981618ce8baSLisandro Dalcin   PetscFunctionReturn(0);
2982618ce8baSLisandro Dalcin }
2983618ce8baSLisandro Dalcin 
2984618ce8baSLisandro Dalcin /*@
2985aaa6c58dSLisandro Dalcin    TSSetInitialTimeStep - Deprecated, use TSSetTime() and TSSetTimeStep().
2986edc382c3SSatish Balay 
2987edc382c3SSatish Balay    Level: deprecated
2988edc382c3SSatish Balay 
2989aaa6c58dSLisandro Dalcin @*/
2990aaa6c58dSLisandro Dalcin PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
2991aaa6c58dSLisandro Dalcin {
2992aaa6c58dSLisandro Dalcin   PetscErrorCode ierr;
2993aaa6c58dSLisandro Dalcin   PetscFunctionBegin;
2994aaa6c58dSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2995aaa6c58dSLisandro Dalcin   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
2996aaa6c58dSLisandro Dalcin   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
2997aaa6c58dSLisandro Dalcin   PetscFunctionReturn(0);
2998aaa6c58dSLisandro Dalcin }
2999aaa6c58dSLisandro Dalcin 
3000aaa6c58dSLisandro Dalcin /*@
300119eac22cSLisandro Dalcin    TSGetDuration - Deprecated, use TSGetMaxSteps() and TSGetMaxTime().
3002edc382c3SSatish Balay 
3003edc382c3SSatish Balay    Level: deprecated
3004edc382c3SSatish Balay 
3005adb62b0dSMatthew Knepley @*/
30067087cfbeSBarry Smith PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
3007adb62b0dSMatthew Knepley {
3008adb62b0dSMatthew Knepley   PetscFunctionBegin;
30090700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3010abc0a331SBarry Smith   if (maxsteps) {
30114482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
3012adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
3013adb62b0dSMatthew Knepley   }
3014abc0a331SBarry Smith   if (maxtime) {
30154482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
3016adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
3017adb62b0dSMatthew Knepley   }
3018adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
3019adb62b0dSMatthew Knepley }
3020adb62b0dSMatthew Knepley 
3021d763cef2SBarry Smith /*@
302219eac22cSLisandro Dalcin    TSSetDuration - Deprecated, use TSSetMaxSteps() and TSSetMaxTime().
3023edc382c3SSatish Balay 
3024edc382c3SSatish Balay    Level: deprecated
3025edc382c3SSatish Balay 
3026d763cef2SBarry Smith @*/
30277087cfbeSBarry Smith PetscErrorCode TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
3028d763cef2SBarry Smith {
3029d763cef2SBarry Smith   PetscFunctionBegin;
30300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3031c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
3032c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
303339b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
303439b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
3035d763cef2SBarry Smith   PetscFunctionReturn(0);
3036d763cef2SBarry Smith }
3037d763cef2SBarry Smith 
3038d763cef2SBarry Smith /*@
30395c5f5948SLisandro Dalcin    TSGetTimeStepNumber - Deprecated, use TSGetStepNumber().
3040edc382c3SSatish Balay 
3041edc382c3SSatish Balay    Level: deprecated
3042edc382c3SSatish Balay 
30435c5f5948SLisandro Dalcin @*/
3044e193eaafSLisandro Dalcin PetscErrorCode TSGetTimeStepNumber(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
30455c5f5948SLisandro Dalcin 
304619eac22cSLisandro Dalcin /*@
30474f4e0956SLisandro Dalcin    TSGetTotalSteps - Deprecated, use TSGetStepNumber().
3048edc382c3SSatish Balay 
3049edc382c3SSatish Balay    Level: deprecated
3050edc382c3SSatish Balay 
30514f4e0956SLisandro Dalcin @*/
30524f4e0956SLisandro Dalcin PetscErrorCode TSGetTotalSteps(TS ts,PetscInt *steps) { return TSGetStepNumber(ts,steps); }
30534f4e0956SLisandro Dalcin 
30544f4e0956SLisandro Dalcin /*@
3055d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
3056d763cef2SBarry Smith    for use by the TS routines.
3057d763cef2SBarry Smith 
30583f9fe445SBarry Smith    Logically Collective on TS and Vec
3059d763cef2SBarry Smith 
3060d763cef2SBarry Smith    Input Parameters:
3061d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
30620910c330SBarry Smith -  u - the solution vector
3063d763cef2SBarry Smith 
3064d763cef2SBarry Smith    Level: beginner
3065d763cef2SBarry Smith 
3066715f1b00SHong Zhang .keywords: TS, timestep, set, solution, initial values
30670ed3bfb6SBarry Smith 
30680ed3bfb6SBarry Smith .seealso: TSSetSolutionFunction(), TSGetSolution(), TSCreate()
3069d763cef2SBarry Smith @*/
30700910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
3071d763cef2SBarry Smith {
30728737fe31SLisandro Dalcin   PetscErrorCode ierr;
3073496e6a7aSJed Brown   DM             dm;
30748737fe31SLisandro Dalcin 
3075d763cef2SBarry Smith   PetscFunctionBegin;
30760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
30770910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
30780910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
30796bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
30800910c330SBarry Smith   ts->vec_sol = u;
3081bbd56ea5SKarl Rupp 
3082496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
30830910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
3084d763cef2SBarry Smith   PetscFunctionReturn(0);
3085d763cef2SBarry Smith }
3086d763cef2SBarry Smith 
3087ac226902SBarry Smith /*@C
3088000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
30893f2090d5SJed Brown   called once at the beginning of each time step.
3090000e7ae3SMatthew Knepley 
30913f9fe445SBarry Smith   Logically Collective on TS
3092000e7ae3SMatthew Knepley 
3093000e7ae3SMatthew Knepley   Input Parameters:
3094000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3095000e7ae3SMatthew Knepley - func - The function
3096000e7ae3SMatthew Knepley 
3097000e7ae3SMatthew Knepley   Calling sequence of func:
3098000e7ae3SMatthew Knepley . func (TS ts);
3099000e7ae3SMatthew Knepley 
3100000e7ae3SMatthew Knepley   Level: intermediate
3101000e7ae3SMatthew Knepley 
3102000e7ae3SMatthew Knepley .keywords: TS, timestep
3103dcb233daSLisandro Dalcin .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep(), TSRestartStep()
3104000e7ae3SMatthew Knepley @*/
31057087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
3106000e7ae3SMatthew Knepley {
3107000e7ae3SMatthew Knepley   PetscFunctionBegin;
31080700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3109ae60f76fSBarry Smith   ts->prestep = func;
3110000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3111000e7ae3SMatthew Knepley }
3112000e7ae3SMatthew Knepley 
311309ee8438SJed Brown /*@
31143f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
31153f2090d5SJed Brown 
31163f2090d5SJed Brown   Collective on TS
31173f2090d5SJed Brown 
31183f2090d5SJed Brown   Input Parameters:
31193f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
31203f2090d5SJed Brown 
31213f2090d5SJed Brown   Notes:
31223f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
31233f2090d5SJed Brown   so most users would not generally call this routine themselves.
31243f2090d5SJed Brown 
31253f2090d5SJed Brown   Level: developer
31263f2090d5SJed Brown 
31273f2090d5SJed Brown .keywords: TS, timestep
31289be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
31293f2090d5SJed Brown @*/
31307087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
31313f2090d5SJed Brown {
31323f2090d5SJed Brown   PetscErrorCode ierr;
31333f2090d5SJed Brown 
31343f2090d5SJed Brown   PetscFunctionBegin;
31350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3136ae60f76fSBarry Smith   if (ts->prestep) {
31375efd42a4SStefano Zampini     Vec              U;
31385efd42a4SStefano Zampini     PetscObjectState sprev,spost;
31395efd42a4SStefano Zampini 
31405efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
31415efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3142ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
31435efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3144dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3145312ce896SJed Brown   }
31463f2090d5SJed Brown   PetscFunctionReturn(0);
31473f2090d5SJed Brown }
31483f2090d5SJed Brown 
3149b8123daeSJed Brown /*@C
3150b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
3151b8123daeSJed Brown   called once at the beginning of each stage.
3152b8123daeSJed Brown 
3153b8123daeSJed Brown   Logically Collective on TS
3154b8123daeSJed Brown 
3155b8123daeSJed Brown   Input Parameters:
3156b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
3157b8123daeSJed Brown - func - The function
3158b8123daeSJed Brown 
3159b8123daeSJed Brown   Calling sequence of func:
3160b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
3161b8123daeSJed Brown 
3162b8123daeSJed Brown   Level: intermediate
3163b8123daeSJed Brown 
3164b8123daeSJed Brown   Note:
3165b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
316680275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
3167b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
3168b8123daeSJed Brown 
3169b8123daeSJed Brown .keywords: TS, timestep
31709be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3171b8123daeSJed Brown @*/
3172b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
3173b8123daeSJed Brown {
3174b8123daeSJed Brown   PetscFunctionBegin;
3175b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3176ae60f76fSBarry Smith   ts->prestage = func;
3177b8123daeSJed Brown   PetscFunctionReturn(0);
3178b8123daeSJed Brown }
3179b8123daeSJed Brown 
31809be3e283SDebojyoti Ghosh /*@C
31819be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
31829be3e283SDebojyoti Ghosh   called once at the end of each stage.
31839be3e283SDebojyoti Ghosh 
31849be3e283SDebojyoti Ghosh   Logically Collective on TS
31859be3e283SDebojyoti Ghosh 
31869be3e283SDebojyoti Ghosh   Input Parameters:
31879be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
31889be3e283SDebojyoti Ghosh - func - The function
31899be3e283SDebojyoti Ghosh 
31909be3e283SDebojyoti Ghosh   Calling sequence of func:
31919be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
31929be3e283SDebojyoti Ghosh 
31939be3e283SDebojyoti Ghosh   Level: intermediate
31949be3e283SDebojyoti Ghosh 
31959be3e283SDebojyoti Ghosh   Note:
31969be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
319780275a0aSLisandro Dalcin   The time step number being computed can be queried using TSGetStepNumber() and the total size of the step being
31989be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
31999be3e283SDebojyoti Ghosh 
32009be3e283SDebojyoti Ghosh .keywords: TS, timestep
32019be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
32029be3e283SDebojyoti Ghosh @*/
32039be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
32049be3e283SDebojyoti Ghosh {
32059be3e283SDebojyoti Ghosh   PetscFunctionBegin;
32069be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
32079be3e283SDebojyoti Ghosh   ts->poststage = func;
32089be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
32099be3e283SDebojyoti Ghosh }
32109be3e283SDebojyoti Ghosh 
3211c688d042SShri Abhyankar /*@C
3212c688d042SShri Abhyankar   TSSetPostEvaluate - Sets the general-purpose function
3213c688d042SShri Abhyankar   called once at the end of each step evaluation.
3214c688d042SShri Abhyankar 
3215c688d042SShri Abhyankar   Logically Collective on TS
3216c688d042SShri Abhyankar 
3217c688d042SShri Abhyankar   Input Parameters:
3218c688d042SShri Abhyankar + ts   - The TS context obtained from TSCreate()
3219c688d042SShri Abhyankar - func - The function
3220c688d042SShri Abhyankar 
3221c688d042SShri Abhyankar   Calling sequence of func:
3222c688d042SShri Abhyankar . PetscErrorCode func(TS ts);
3223c688d042SShri Abhyankar 
3224c688d042SShri Abhyankar   Level: intermediate
3225c688d042SShri Abhyankar 
3226c688d042SShri Abhyankar   Note:
32271785ff2aSShri Abhyankar   Semantically, TSSetPostEvaluate() differs from TSSetPostStep() since the function it sets is called before event-handling
32281785ff2aSShri Abhyankar   thus guaranteeing the same solution (computed by the time-stepper) will be passed to it. On the other hand, TSPostStep()
3229e7e94ed4SShri Abhyankar   may be passed a different solution, possibly changed by the event handler. TSPostEvaluate() is called after the next step
3230e7e94ed4SShri Abhyankar   solution is evaluated allowing to modify it, if need be. The solution can be obtained with TSGetSolution(), the time step
3231e7e94ed4SShri Abhyankar   with TSGetTimeStep(), and the time at the start of the step is available via TSGetTime()
3232c688d042SShri Abhyankar 
3233c688d042SShri Abhyankar .keywords: TS, timestep
3234c688d042SShri Abhyankar .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
3235c688d042SShri Abhyankar @*/
3236c688d042SShri Abhyankar PetscErrorCode  TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS))
3237c688d042SShri Abhyankar {
3238c688d042SShri Abhyankar   PetscFunctionBegin;
3239c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3240c688d042SShri Abhyankar   ts->postevaluate = func;
3241c688d042SShri Abhyankar   PetscFunctionReturn(0);
3242c688d042SShri Abhyankar }
3243c688d042SShri Abhyankar 
3244b8123daeSJed Brown /*@
3245b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
3246b8123daeSJed Brown 
3247b8123daeSJed Brown   Collective on TS
3248b8123daeSJed Brown 
3249b8123daeSJed Brown   Input Parameters:
3250b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
32519be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
3252b8123daeSJed Brown 
3253b8123daeSJed Brown   Notes:
3254b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
3255b8123daeSJed Brown   most users would not generally call this routine themselves.
3256b8123daeSJed Brown 
3257b8123daeSJed Brown   Level: developer
3258b8123daeSJed Brown 
3259b8123daeSJed Brown .keywords: TS, timestep
32609be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
3261b8123daeSJed Brown @*/
3262b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
3263b8123daeSJed Brown {
3264b8123daeSJed Brown   PetscErrorCode ierr;
3265b8123daeSJed Brown 
3266b8123daeSJed Brown   PetscFunctionBegin;
3267b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3268ae60f76fSBarry Smith   if (ts->prestage) {
3269ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
3270b8123daeSJed Brown   }
3271b8123daeSJed Brown   PetscFunctionReturn(0);
3272b8123daeSJed Brown }
3273b8123daeSJed Brown 
32749be3e283SDebojyoti Ghosh /*@
32759be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
32769be3e283SDebojyoti Ghosh 
32779be3e283SDebojyoti Ghosh   Collective on TS
32789be3e283SDebojyoti Ghosh 
32799be3e283SDebojyoti Ghosh   Input Parameters:
32809be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
32819be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
32829be3e283SDebojyoti Ghosh   stageindex  - Stage number
32839be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
32849be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
32859be3e283SDebojyoti Ghosh 
32869be3e283SDebojyoti Ghosh   Notes:
32879be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
32889be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
32899be3e283SDebojyoti Ghosh 
32909be3e283SDebojyoti Ghosh   Level: developer
32919be3e283SDebojyoti Ghosh 
32929be3e283SDebojyoti Ghosh .keywords: TS, timestep
32939be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
32949be3e283SDebojyoti Ghosh @*/
32959be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
32969be3e283SDebojyoti Ghosh {
32979be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
32989be3e283SDebojyoti Ghosh 
32999be3e283SDebojyoti Ghosh   PetscFunctionBegin;
33009be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
33014beae5d8SLisandro Dalcin   if (ts->poststage) {
33029be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
33039be3e283SDebojyoti Ghosh   }
33049be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
33059be3e283SDebojyoti Ghosh }
33069be3e283SDebojyoti Ghosh 
3307c688d042SShri Abhyankar /*@
3308c688d042SShri Abhyankar   TSPostEvaluate - Runs the user-defined post-evaluate function set using TSSetPostEvaluate()
3309c688d042SShri Abhyankar 
3310c688d042SShri Abhyankar   Collective on TS
3311c688d042SShri Abhyankar 
3312c688d042SShri Abhyankar   Input Parameters:
3313c688d042SShri Abhyankar . ts          - The TS context obtained from TSCreate()
3314c688d042SShri Abhyankar 
3315c688d042SShri Abhyankar   Notes:
3316c688d042SShri Abhyankar   TSPostEvaluate() is typically used within time stepping implementations,
3317c688d042SShri Abhyankar   most users would not generally call this routine themselves.
3318c688d042SShri Abhyankar 
3319c688d042SShri Abhyankar   Level: developer
3320c688d042SShri Abhyankar 
3321c688d042SShri Abhyankar .keywords: TS, timestep
3322c688d042SShri Abhyankar .seealso: TSSetPostEvaluate(), TSSetPreStep(), TSPreStep(), TSPostStep()
3323c688d042SShri Abhyankar @*/
3324c688d042SShri Abhyankar PetscErrorCode  TSPostEvaluate(TS ts)
3325c688d042SShri Abhyankar {
3326c688d042SShri Abhyankar   PetscErrorCode ierr;
3327c688d042SShri Abhyankar 
3328c688d042SShri Abhyankar   PetscFunctionBegin;
3329c688d042SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3330c688d042SShri Abhyankar   if (ts->postevaluate) {
3331dcb233daSLisandro Dalcin     Vec              U;
3332dcb233daSLisandro Dalcin     PetscObjectState sprev,spost;
3333dcb233daSLisandro Dalcin 
3334dcb233daSLisandro Dalcin     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
3335dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3336c688d042SShri Abhyankar     PetscStackCallStandard((*ts->postevaluate),(ts));
3337dcb233daSLisandro Dalcin     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3338dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
3339c688d042SShri Abhyankar   }
3340c688d042SShri Abhyankar   PetscFunctionReturn(0);
3341c688d042SShri Abhyankar }
3342c688d042SShri Abhyankar 
3343ac226902SBarry Smith /*@C
3344000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
33453f2090d5SJed Brown   called once at the end of each time step.
3346000e7ae3SMatthew Knepley 
33473f9fe445SBarry Smith   Logically Collective on TS
3348000e7ae3SMatthew Knepley 
3349000e7ae3SMatthew Knepley   Input Parameters:
3350000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
3351000e7ae3SMatthew Knepley - func - The function
3352000e7ae3SMatthew Knepley 
3353000e7ae3SMatthew Knepley   Calling sequence of func:
3354b8123daeSJed Brown $ func (TS ts);
3355000e7ae3SMatthew Knepley 
33561785ff2aSShri Abhyankar   Notes:
33571785ff2aSShri Abhyankar   The function set by TSSetPostStep() is called after each successful step. The solution vector X
33581785ff2aSShri Abhyankar   obtained by TSGetSolution() may be different than that computed at the step end if the event handler
33591785ff2aSShri Abhyankar   locates an event and TSPostEvent() modifies it. Use TSSetPostEvaluate() if an unmodified solution is needed instead.
33601785ff2aSShri Abhyankar 
3361000e7ae3SMatthew Knepley   Level: intermediate
3362000e7ae3SMatthew Knepley 
3363000e7ae3SMatthew Knepley .keywords: TS, timestep
3364dcb233daSLisandro Dalcin .seealso: TSSetPreStep(), TSSetPreStage(), TSSetPostEvaluate(), TSGetTimeStep(), TSGetStepNumber(), TSGetTime(), TSRestartStep()
3365000e7ae3SMatthew Knepley @*/
33667087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
3367000e7ae3SMatthew Knepley {
3368000e7ae3SMatthew Knepley   PetscFunctionBegin;
33690700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3370ae60f76fSBarry Smith   ts->poststep = func;
3371000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
3372000e7ae3SMatthew Knepley }
3373000e7ae3SMatthew Knepley 
337409ee8438SJed Brown /*@
33753f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
33763f2090d5SJed Brown 
33773f2090d5SJed Brown   Collective on TS
33783f2090d5SJed Brown 
33793f2090d5SJed Brown   Input Parameters:
33803f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
33813f2090d5SJed Brown 
33823f2090d5SJed Brown   Notes:
33833f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
33843f2090d5SJed Brown   so most users would not generally call this routine themselves.
33853f2090d5SJed Brown 
33863f2090d5SJed Brown   Level: developer
33873f2090d5SJed Brown 
33883f2090d5SJed Brown .keywords: TS, timestep
33893f2090d5SJed Brown @*/
33907087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
33913f2090d5SJed Brown {
33923f2090d5SJed Brown   PetscErrorCode ierr;
33933f2090d5SJed Brown 
33943f2090d5SJed Brown   PetscFunctionBegin;
33950700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3396ae60f76fSBarry Smith   if (ts->poststep) {
33975efd42a4SStefano Zampini     Vec              U;
33985efd42a4SStefano Zampini     PetscObjectState sprev,spost;
33995efd42a4SStefano Zampini 
34005efd42a4SStefano Zampini     ierr = TSGetSolution(ts,&U);CHKERRQ(ierr);
34015efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&sprev);CHKERRQ(ierr);
3402ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
34035efd42a4SStefano Zampini     ierr = PetscObjectStateGet((PetscObject)U,&spost);CHKERRQ(ierr);
3404dcb233daSLisandro Dalcin     if (sprev != spost) {ierr = TSRestartStep(ts);CHKERRQ(ierr);}
340572ac3e02SJed Brown   }
34063f2090d5SJed Brown   PetscFunctionReturn(0);
34073f2090d5SJed Brown }
34083f2090d5SJed Brown 
3409d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
3410d763cef2SBarry Smith 
3411d763cef2SBarry Smith /*@C
3412a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
3413d763cef2SBarry Smith    timestep to display the iteration's  progress.
3414d763cef2SBarry Smith 
34153f9fe445SBarry Smith    Logically Collective on TS
3416d763cef2SBarry Smith 
3417d763cef2SBarry Smith    Input Parameters:
3418d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3419e213d8f1SJed Brown .  monitor - monitoring routine
3420329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
34210298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
3422b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
34230298fd71SBarry Smith           (may be NULL)
3424d763cef2SBarry Smith 
3425e213d8f1SJed Brown    Calling sequence of monitor:
3426dcb233daSLisandro Dalcin $    PetscErrorCode monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
3427d763cef2SBarry Smith 
3428d763cef2SBarry Smith +    ts - the TS context
342963e21af5SBarry 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)
34301f06c33eSBarry Smith .    time - current time
34310910c330SBarry Smith .    u - current iterate
3432d763cef2SBarry Smith -    mctx - [optional] monitoring context
3433d763cef2SBarry Smith 
3434d763cef2SBarry Smith    Notes:
3435d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
3436d763cef2SBarry Smith    already has been loaded.
3437d763cef2SBarry Smith 
343895452b02SPatrick Sanan    Fortran Notes:
343995452b02SPatrick Sanan     Only a single monitor function can be set for each TS object
3440025f1a04SBarry Smith 
3441d763cef2SBarry Smith    Level: intermediate
3442d763cef2SBarry Smith 
3443d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3444d763cef2SBarry Smith 
3445a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
3446d763cef2SBarry Smith @*/
3447c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
3448d763cef2SBarry Smith {
344978064530SBarry Smith   PetscErrorCode ierr;
345078064530SBarry Smith   PetscInt       i;
345178064530SBarry Smith   PetscBool      identical;
345278064530SBarry Smith 
3453d763cef2SBarry Smith   PetscFunctionBegin;
34540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
345578064530SBarry Smith   for (i=0; i<ts->numbermonitors;i++) {
345678064530SBarry Smith     ierr = PetscMonitorCompare((PetscErrorCode (*)(void))monitor,mctx,mdestroy,(PetscErrorCode (*)(void))ts->monitor[i],ts->monitorcontext[i],ts->monitordestroy[i],&identical);CHKERRQ(ierr);
345778064530SBarry Smith     if (identical) PetscFunctionReturn(0);
345878064530SBarry Smith   }
345917186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
3460d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
34618704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
3462d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
3463d763cef2SBarry Smith   PetscFunctionReturn(0);
3464d763cef2SBarry Smith }
3465d763cef2SBarry Smith 
3466d763cef2SBarry Smith /*@C
3467a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
3468d763cef2SBarry Smith 
34693f9fe445SBarry Smith    Logically Collective on TS
3470d763cef2SBarry Smith 
3471d763cef2SBarry Smith    Input Parameters:
3472d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3473d763cef2SBarry Smith 
3474d763cef2SBarry Smith    Notes:
3475d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
3476d763cef2SBarry Smith 
3477d763cef2SBarry Smith    Level: intermediate
3478d763cef2SBarry Smith 
3479d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
3480d763cef2SBarry Smith 
3481a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
3482d763cef2SBarry Smith @*/
34837087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
3484d763cef2SBarry Smith {
3485d952e501SBarry Smith   PetscErrorCode ierr;
3486d952e501SBarry Smith   PetscInt       i;
3487d952e501SBarry Smith 
3488d763cef2SBarry Smith   PetscFunctionBegin;
34890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3490d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
34918704b422SBarry Smith     if (ts->monitordestroy[i]) {
34928704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
3493d952e501SBarry Smith     }
3494d952e501SBarry Smith   }
3495d763cef2SBarry Smith   ts->numbermonitors = 0;
3496d763cef2SBarry Smith   PetscFunctionReturn(0);
3497d763cef2SBarry Smith }
3498d763cef2SBarry Smith 
3499721cd6eeSBarry Smith /*@C
3500721cd6eeSBarry Smith    TSMonitorDefault - The Default monitor, prints the timestep and time for each step
35015516499fSSatish Balay 
35025516499fSSatish Balay    Level: intermediate
350341251cbbSSatish Balay 
35045516499fSSatish Balay .keywords: TS, set, monitor
35055516499fSSatish Balay 
350663e21af5SBarry Smith .seealso:  TSMonitorSet()
350741251cbbSSatish Balay @*/
3508721cd6eeSBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3509d763cef2SBarry Smith {
3510dfbe8321SBarry Smith   PetscErrorCode ierr;
3511721cd6eeSBarry Smith   PetscViewer    viewer =  vf->viewer;
351241aca3d6SBarry Smith   PetscBool      iascii,ibinary;
3513d132466eSBarry Smith 
3514d763cef2SBarry Smith   PetscFunctionBegin;
35154d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
351641aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
351741aca3d6SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&ibinary);CHKERRQ(ierr);
3518721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
351941aca3d6SBarry Smith   if (iascii) {
3520649052a6SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
352163e21af5SBarry Smith     if (step == -1){ /* this indicates it is an interpolated solution */
352263e21af5SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"Interpolated solution at time %g between steps %D and %D\n",(double)ptime,ts->steps-1,ts->steps);CHKERRQ(ierr);
352363e21af5SBarry Smith     } else {
35248392e04aSShri 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);
352563e21af5SBarry Smith     }
3526649052a6SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
352741aca3d6SBarry Smith   } else if (ibinary) {
352841aca3d6SBarry Smith     PetscMPIInt rank;
352941aca3d6SBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
353041aca3d6SBarry Smith     if (!rank) {
3531450a797fSBarry Smith       PetscBool skipHeader;
3532450a797fSBarry Smith       PetscInt  classid = REAL_FILE_CLASSID;
3533450a797fSBarry Smith 
3534450a797fSBarry Smith       ierr = PetscViewerBinaryGetSkipHeader(viewer,&skipHeader);CHKERRQ(ierr);
3535450a797fSBarry Smith       if (!skipHeader) {
3536450a797fSBarry Smith          ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
3537450a797fSBarry Smith        }
353841aca3d6SBarry Smith       ierr = PetscRealView(1,&ptime,viewer);CHKERRQ(ierr);
353941aca3d6SBarry Smith     } else {
354041aca3d6SBarry Smith       ierr = PetscRealView(0,&ptime,viewer);CHKERRQ(ierr);
354141aca3d6SBarry Smith     }
354241aca3d6SBarry Smith   }
3543721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3544d763cef2SBarry Smith   PetscFunctionReturn(0);
3545d763cef2SBarry Smith }
3546d763cef2SBarry Smith 
3547cc9c3a59SBarry Smith /*@C
3548cc9c3a59SBarry Smith    TSMonitorExtreme - Prints the extreme values of the solution at each timestep
3549cc9c3a59SBarry Smith 
3550cc9c3a59SBarry Smith    Level: intermediate
3551cc9c3a59SBarry Smith 
3552cc9c3a59SBarry Smith .keywords: TS, set, monitor
3553cc9c3a59SBarry Smith 
3554cc9c3a59SBarry Smith .seealso:  TSMonitorSet()
3555cc9c3a59SBarry Smith @*/
3556cc9c3a59SBarry Smith PetscErrorCode TSMonitorExtreme(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscViewerAndFormat *vf)
3557cc9c3a59SBarry Smith {
3558cc9c3a59SBarry Smith   PetscErrorCode ierr;
3559cc9c3a59SBarry Smith   PetscViewer    viewer =  vf->viewer;
3560cc9c3a59SBarry Smith   PetscBool      iascii;
3561cc9c3a59SBarry Smith   PetscReal      max,min;
3562cc9c3a59SBarry Smith 
3563cc9c3a59SBarry Smith 
3564cc9c3a59SBarry Smith   PetscFunctionBegin;
3565cc9c3a59SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
3566cc9c3a59SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
3567cc9c3a59SBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
3568cc9c3a59SBarry Smith   if (iascii) {
3569cc9c3a59SBarry Smith     ierr = VecMax(v,NULL,&max);CHKERRQ(ierr);
3570cc9c3a59SBarry Smith     ierr = VecMin(v,NULL,&min);CHKERRQ(ierr);
3571cc9c3a59SBarry Smith     ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
35725132926bSBarry 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);
3573cc9c3a59SBarry Smith     ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
3574cc9c3a59SBarry Smith   }
3575cc9c3a59SBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
3576cc9c3a59SBarry Smith   PetscFunctionReturn(0);
3577cc9c3a59SBarry Smith }
3578cc9c3a59SBarry Smith 
3579cd652676SJed Brown /*@
3580cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3581cd652676SJed Brown 
3582cd652676SJed Brown    Collective on TS
3583cd652676SJed Brown 
3584cd652676SJed Brown    Input Argument:
3585cd652676SJed Brown +  ts - time stepping context
3586cd652676SJed Brown -  t - time to interpolate to
3587cd652676SJed Brown 
3588cd652676SJed Brown    Output Argument:
35890910c330SBarry Smith .  U - state at given time
3590cd652676SJed Brown 
3591cd652676SJed Brown    Level: intermediate
3592cd652676SJed Brown 
3593cd652676SJed Brown    Developer Notes:
3594cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3595cd652676SJed Brown 
3596cd652676SJed Brown .keywords: TS, set
3597cd652676SJed Brown 
3598874c02e6SLisandro Dalcin .seealso: TSSetExactFinalTime(), TSSolve()
3599cd652676SJed Brown @*/
36000910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3601cd652676SJed Brown {
3602cd652676SJed Brown   PetscErrorCode ierr;
3603cd652676SJed Brown 
3604cd652676SJed Brown   PetscFunctionBegin;
3605cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3606b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3607be5899b3SLisandro 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);
3608ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
36090910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3610cd652676SJed Brown   PetscFunctionReturn(0);
3611cd652676SJed Brown }
3612cd652676SJed Brown 
3613d763cef2SBarry Smith /*@
36146d9e5789SSean Farley    TSStep - Steps one time step
3615d763cef2SBarry Smith 
3616d763cef2SBarry Smith    Collective on TS
3617d763cef2SBarry Smith 
3618d763cef2SBarry Smith    Input Parameter:
3619d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3620d763cef2SBarry Smith 
362127829d71SBarry Smith    Level: developer
3622d763cef2SBarry Smith 
3623b8123daeSJed Brown    Notes:
362427829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
362527829d71SBarry Smith 
3626b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3627b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3628b8123daeSJed Brown 
362919eac22cSLisandro Dalcin    This may over-step the final time provided in TSSetMaxTime() depending on the time-step used. TSSolve() interpolates to exactly the
363019eac22cSLisandro Dalcin    time provided in TSSetMaxTime(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
363125cb2221SBarry Smith 
3632d763cef2SBarry Smith .keywords: TS, timestep, solve
3633d763cef2SBarry Smith 
36349be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3635d763cef2SBarry Smith @*/
3636193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3637d763cef2SBarry Smith {
3638dfbe8321SBarry Smith   PetscErrorCode   ierr;
3639fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3640be5899b3SLisandro Dalcin   PetscReal        ptime;
3641d763cef2SBarry Smith 
3642d763cef2SBarry Smith   PetscFunctionBegin;
36430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3644fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3645fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3646fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3647fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3648fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3649fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3650302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3651fffbeea8SBarry Smith 
3652d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
365368bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3654d405a339SMatthew Knepley 
3655ef85077eSLisandro 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>");
3656a6772fa2SLisandro 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()");
3657be5899b3SLisandro 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");
3658a6772fa2SLisandro Dalcin 
3659be5899b3SLisandro Dalcin   if (!ts->steps) ts->ptime_prev = ts->ptime;
3660be5899b3SLisandro Dalcin   ptime = ts->ptime; ts->ptime_prev_rollback = ts->ptime_prev;
36612808aa04SLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
3662e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3663d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3664193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3665d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3666be5899b3SLisandro Dalcin   ts->ptime_prev = ptime;
36672808aa04SLisandro Dalcin   ts->steps++;
3668be5899b3SLisandro Dalcin   ts->steprollback = PETSC_FALSE;
366928d5b5d6SLisandro Dalcin   ts->steprestart  = PETSC_FALSE;
3670362cd11cSLisandro Dalcin 
3671362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3672cef5090cSJed Brown     if (ts->errorifstepfailed) {
367308c7845fSBarry 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]);
367408c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3675d2daff3dSHong Zhang     }
367608c7845fSBarry Smith   } else if (!ts->reason) {
367708c7845fSBarry Smith     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
367808c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
367908c7845fSBarry Smith   }
368008c7845fSBarry Smith   PetscFunctionReturn(0);
368108c7845fSBarry Smith }
368208c7845fSBarry Smith 
368308c7845fSBarry Smith /*@
36847cbde773SLisandro Dalcin    TSEvaluateWLTE - Evaluate the weighted local truncation error norm
36857cbde773SLisandro Dalcin    at the end of a time step with a given order of accuracy.
36867cbde773SLisandro Dalcin 
36877cbde773SLisandro Dalcin    Collective on TS
36887cbde773SLisandro Dalcin 
36897cbde773SLisandro Dalcin    Input Arguments:
36907cbde773SLisandro Dalcin +  ts - time stepping context
36917cbde773SLisandro Dalcin .  wnormtype - norm type, either NORM_2 or NORM_INFINITY
36927cbde773SLisandro Dalcin -  order - optional, desired order for the error evaluation or PETSC_DECIDE
36937cbde773SLisandro Dalcin 
36947cbde773SLisandro Dalcin    Output Arguments:
36957cbde773SLisandro Dalcin +  order - optional, the actual order of the error evaluation
36967cbde773SLisandro Dalcin -  wlte - the weighted local truncation error norm
36977cbde773SLisandro Dalcin 
36987cbde773SLisandro Dalcin    Level: advanced
36997cbde773SLisandro Dalcin 
37007cbde773SLisandro Dalcin    Notes:
37017cbde773SLisandro Dalcin    If the timestepper cannot evaluate the error in a particular step
37027cbde773SLisandro Dalcin    (eg. in the first step or restart steps after event handling),
37037cbde773SLisandro Dalcin    this routine returns wlte=-1.0 .
37047cbde773SLisandro Dalcin 
37057cbde773SLisandro Dalcin .seealso: TSStep(), TSAdapt, TSErrorWeightedNorm()
37067cbde773SLisandro Dalcin @*/
37077cbde773SLisandro Dalcin PetscErrorCode TSEvaluateWLTE(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
37087cbde773SLisandro Dalcin {
37097cbde773SLisandro Dalcin   PetscErrorCode ierr;
37107cbde773SLisandro Dalcin 
37117cbde773SLisandro Dalcin   PetscFunctionBegin;
37127cbde773SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37137cbde773SLisandro Dalcin   PetscValidType(ts,1);
37147cbde773SLisandro Dalcin   PetscValidLogicalCollectiveEnum(ts,wnormtype,4);
37157cbde773SLisandro Dalcin   if (order) PetscValidIntPointer(order,3);
37167cbde773SLisandro Dalcin   if (order) PetscValidLogicalCollectiveInt(ts,*order,3);
37177cbde773SLisandro Dalcin   PetscValidRealPointer(wlte,4);
37187cbde773SLisandro Dalcin   if (wnormtype != NORM_2 && wnormtype != NORM_INFINITY) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
37197cbde773SLisandro Dalcin   if (!ts->ops->evaluatewlte) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateWLTE not implemented for type '%s'",((PetscObject)ts)->type_name);
37207cbde773SLisandro Dalcin   ierr = (*ts->ops->evaluatewlte)(ts,wnormtype,order,wlte);CHKERRQ(ierr);
37217cbde773SLisandro Dalcin   PetscFunctionReturn(0);
37227cbde773SLisandro Dalcin }
37237cbde773SLisandro Dalcin 
372405175c85SJed Brown /*@
372505175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
372605175c85SJed Brown 
37271c3436cfSJed Brown    Collective on TS
372805175c85SJed Brown 
372905175c85SJed Brown    Input Arguments:
37301c3436cfSJed Brown +  ts - time stepping context
37311c3436cfSJed Brown .  order - desired order of accuracy
37320298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
373305175c85SJed Brown 
373405175c85SJed Brown    Output Arguments:
37350910c330SBarry Smith .  U - state at the end of the current step
373605175c85SJed Brown 
373705175c85SJed Brown    Level: advanced
373805175c85SJed Brown 
3739108c343cSJed Brown    Notes:
3740108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3741108c343cSJed 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.
3742108c343cSJed Brown 
37431c3436cfSJed Brown .seealso: TSStep(), TSAdapt
374405175c85SJed Brown @*/
37450910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
374605175c85SJed Brown {
374705175c85SJed Brown   PetscErrorCode ierr;
374805175c85SJed Brown 
374905175c85SJed Brown   PetscFunctionBegin;
375005175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
375105175c85SJed Brown   PetscValidType(ts,1);
37520910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3753ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
37540910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
375505175c85SJed Brown   PetscFunctionReturn(0);
375605175c85SJed Brown }
375705175c85SJed Brown 
3758b1cb36f3SHong Zhang /*@
37596a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
37606a4d4014SLisandro Dalcin 
37616a4d4014SLisandro Dalcin    Collective on TS
37626a4d4014SLisandro Dalcin 
37636a4d4014SLisandro Dalcin    Input Parameter:
37646a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
376563e21af5SBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used and TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP) was not used,
376663e21af5SBarry Smith                              otherwise must contain the initial conditions and will contain the solution at the final requested time
37675a3a76d0SJed Brown 
37686a4d4014SLisandro Dalcin    Level: beginner
37696a4d4014SLisandro Dalcin 
37705a3a76d0SJed Brown    Notes:
37715a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
37725a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
37735a3a76d0SJed Brown    stepped over the final time.
37745a3a76d0SJed Brown 
37756a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
37766a4d4014SLisandro Dalcin 
377763e21af5SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep(), TSGetTime(), TSGetSolveTime()
37786a4d4014SLisandro Dalcin @*/
3779cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
37806a4d4014SLisandro Dalcin {
3781b06615a5SLisandro Dalcin   Vec               solution;
37826a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
3783f22f69f0SBarry Smith 
37846a4d4014SLisandro Dalcin   PetscFunctionBegin;
37850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3786f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
3787feed9e9dSBarry Smith 
3788ee41a567SStefano 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 */
37890910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3790b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3791b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3792b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
37935a3a76d0SJed Brown     }
37940910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3795715f1b00SHong Zhang     if (ts->forward_solve) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE");
3796bbd56ea5SKarl Rupp   } else if (u) {
37970910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
37985a3a76d0SJed Brown   }
3799b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
380068bece0bSHong Zhang   ierr = TSTrajectorySetUp(ts->trajectory,ts);CHKERRQ(ierr);
3801a6772fa2SLisandro Dalcin 
3802ef85077eSLisandro 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>");
3803a6772fa2SLisandro 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()");
3804a6772fa2SLisandro 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");
3805a6772fa2SLisandro Dalcin 
3806715f1b00SHong Zhang   if (ts->forward_solve) {
3807715f1b00SHong Zhang     ierr = TSForwardSetUp(ts);CHKERRQ(ierr);
3808715f1b00SHong Zhang   }
3809715f1b00SHong Zhang 
3810e7069c78SShri   /* reset number of steps only when the step is not restarted. ARKIMEX
3811715f1b00SHong Zhang      restarts the step after an event. Resetting these counters in such case causes
3812e7069c78SShri      TSTrajectory to incorrectly save the output files
3813e7069c78SShri   */
3814715f1b00SHong Zhang   /* reset time step and iteration counters */
38152808aa04SLisandro Dalcin   if (!ts->steps) {
38165ef26d82SJed Brown     ts->ksp_its           = 0;
38175ef26d82SJed Brown     ts->snes_its          = 0;
3818c610991cSLisandro Dalcin     ts->num_snes_failures = 0;
3819c610991cSLisandro Dalcin     ts->reject            = 0;
38202808aa04SLisandro Dalcin     ts->steprestart       = PETSC_TRUE;
38212808aa04SLisandro Dalcin     ts->steprollback      = PETSC_FALSE;
38222808aa04SLisandro Dalcin   }
382310bc0e4dSStefano 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;
3824193ac0bcSJed Brown   ts->reason = TS_CONVERGED_ITERATING;
3825193ac0bcSJed Brown 
3826ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3827f05ece33SBarry Smith 
3828193ac0bcSJed Brown   if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */
3829193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
3830a6772fa2SLisandro Dalcin     if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3831cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3832a6772fa2SLisandro Dalcin     solution = ts->vec_sol;
3833be5899b3SLisandro Dalcin   } else { /* Step the requested number of timesteps. */
3834db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS;
3835db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3836e7069c78SShri 
38372808aa04SLisandro Dalcin     if (!ts->steps) {
38382808aa04SLisandro Dalcin       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
38396427ac75SLisandro Dalcin       ierr = TSEventInitialize(ts->event,ts,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
38402808aa04SLisandro Dalcin     }
38416427ac75SLisandro Dalcin 
3842e1a7a14fSJed Brown     while (!ts->reason) {
38432808aa04SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
38449687d888SLisandro Dalcin       if (!ts->steprollback) {
38459687d888SLisandro Dalcin         ierr = TSPreStep(ts);CHKERRQ(ierr);
38469687d888SLisandro Dalcin       }
3847193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
3848f3b1f45cSBarry Smith       if (ts->testjacobian) {
3849f3b1f45cSBarry Smith         ierr = TSRHSJacobianTest(ts,NULL);CHKERRQ(ierr);
3850f3b1f45cSBarry Smith       }
3851f3b1f45cSBarry Smith       if (ts->testjacobiantranspose) {
3852f3b1f45cSBarry Smith         ierr = TSRHSJacobianTestTranspose(ts,NULL);CHKERRQ(ierr);
3853f3b1f45cSBarry Smith       }
3854e783b05fSHong 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. */
3855b1cb36f3SHong Zhang         ierr = TSForwardCostIntegral(ts);CHKERRQ(ierr);
3856b1cb36f3SHong Zhang       }
385758818c2dSLisandro Dalcin       if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */
3858715f1b00SHong Zhang         ierr = TSForwardStep(ts);CHKERRQ(ierr);
3859715f1b00SHong Zhang       }
386010b82f12SShri Abhyankar       ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
3861e783b05fSHong 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. */
386258818c2dSLisandro Dalcin       if (ts->steprollback) {
386358818c2dSLisandro Dalcin         ierr = TSPostEvaluate(ts);CHKERRQ(ierr);
386458818c2dSLisandro Dalcin       }
3865e783b05fSHong Zhang       if (!ts->steprollback) {
38662808aa04SLisandro Dalcin         ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
38671eda64f1SShri Abhyankar         ierr = TSPostStep(ts);CHKERRQ(ierr);
3868aeb4809dSShri Abhyankar       }
3869193ac0bcSJed Brown     }
38702808aa04SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
38716427ac75SLisandro Dalcin 
387249354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
38730910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
3874cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3875b06615a5SLisandro Dalcin       solution = u;
387663e21af5SBarry Smith       ierr = TSMonitor(ts,-1,ts->solvetime,solution);CHKERRQ(ierr);
38770574a7fbSJed Brown     } else {
3878ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3879cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3880b06615a5SLisandro Dalcin       solution = ts->vec_sol;
38810574a7fbSJed Brown     }
3882193ac0bcSJed Brown   }
3883d2daff3dSHong Zhang 
3884ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
388563e21af5SBarry Smith   ierr = VecViewFromOptions(solution,NULL,"-ts_view_solution");CHKERRQ(ierr);
388656f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
3887ef222394SBarry Smith   if (ts->adjoint_solve) {
3888ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
38892b0a91c0SBarry Smith   }
38906a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
38916a4d4014SLisandro Dalcin }
38926a4d4014SLisandro Dalcin 
38937db568b7SBarry Smith /*@C
3894228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
3895228d79bcSJed Brown 
3896228d79bcSJed Brown    Collective on TS
3897228d79bcSJed Brown 
3898228d79bcSJed Brown    Input Parameters:
3899228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
3900228d79bcSJed Brown .  step - step number that has just completed
3901228d79bcSJed Brown .  ptime - model time of the state
39020910c330SBarry Smith -  u - state at the current model time
3903228d79bcSJed Brown 
3904228d79bcSJed Brown    Notes:
39057db568b7SBarry Smith    TSMonitor() is typically used automatically within the time stepping implementations.
39067db568b7SBarry Smith    Users would almost never call this routine directly.
3907228d79bcSJed Brown 
390863e21af5SBarry Smith    A step of -1 indicates that the monitor is being called on a solution obtained by interpolating from computed solutions
390963e21af5SBarry Smith 
39107db568b7SBarry Smith    Level: developer
3911228d79bcSJed Brown 
3912228d79bcSJed Brown .keywords: TS, timestep
3913228d79bcSJed Brown @*/
39140910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
3915d763cef2SBarry Smith {
3916d6152f81SLisandro Dalcin   DM             dm;
3917a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
3918d6152f81SLisandro Dalcin   PetscErrorCode ierr;
3919d763cef2SBarry Smith 
3920d763cef2SBarry Smith   PetscFunctionBegin;
3921b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3922b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
3923d6152f81SLisandro Dalcin 
3924d6152f81SLisandro Dalcin   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
3925d6152f81SLisandro Dalcin   ierr = DMSetOutputSequenceNumber(dm,step,ptime);CHKERRQ(ierr);
3926d6152f81SLisandro Dalcin 
39275edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
3928d763cef2SBarry Smith   for (i=0; i<n; i++) {
39290910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
3930d763cef2SBarry Smith   }
39315edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
3932d763cef2SBarry Smith   PetscFunctionReturn(0);
3933d763cef2SBarry Smith }
3934d763cef2SBarry Smith 
3935d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
3936d763cef2SBarry Smith /*@C
39377db568b7SBarry Smith    TSMonitorLGCtxCreate - Creates a TSMonitorLGCtx context for use with
3938a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
3939d763cef2SBarry Smith 
3940d763cef2SBarry Smith    Collective on TS
3941d763cef2SBarry Smith 
3942d763cef2SBarry Smith    Input Parameters:
3943d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
3944d763cef2SBarry Smith .  label - the title to put in the title bar
39457c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
3946a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
3947a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
3948d763cef2SBarry Smith 
3949d763cef2SBarry Smith    Output Parameter:
39500b039ecaSBarry Smith .  ctx - the context
3951d763cef2SBarry Smith 
3952d763cef2SBarry Smith    Options Database Key:
39534f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
39548b668821SLisandro Dalcin +  -ts_monitor_lg_timestep_log - automatically sets line graph monitor
39557db568b7SBarry Smith .  -ts_monitor_lg_solution - monitor the solution (or certain values of the solution by calling TSMonitorLGSetDisplayVariables() or TSMonitorLGCtxSetDisplayVariables())
39567db568b7SBarry Smith .  -ts_monitor_lg_error -  monitor the error
39577db568b7SBarry Smith .  -ts_monitor_lg_ksp_iterations - monitor the number of KSP iterations needed for each timestep
39587db568b7SBarry Smith .  -ts_monitor_lg_snes_iterations - monitor the number of SNES iterations needed for each timestep
3959b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
3960d763cef2SBarry Smith 
3961d763cef2SBarry Smith    Notes:
3962a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
3963d763cef2SBarry Smith 
39647db568b7SBarry Smith    One can provide a function that transforms the solution before plotting it with TSMonitorLGCtxSetTransform() or TSMonitorLGSetTransform()
39657db568b7SBarry Smith 
39667db568b7SBarry 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
39677db568b7SBarry 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
39687db568b7SBarry Smith    as the first argument.
39697db568b7SBarry Smith 
39707db568b7SBarry Smith    One can control the names displayed for each solution or error variable with TSMonitorLGCtxSetVariableNames() or TSMonitorLGSetVariableNames()
39717db568b7SBarry Smith 
3972d763cef2SBarry Smith    Level: intermediate
3973d763cef2SBarry Smith 
39747db568b7SBarry Smith .keywords: TS, monitor, line graph, residual
3975d763cef2SBarry Smith 
39767db568b7SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError(), TSMonitorDefault(), VecView(),
39777db568b7SBarry Smith            TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
39787db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
39797db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
39807db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
39817c922b88SBarry Smith 
3982d763cef2SBarry Smith @*/
3983a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
3984d763cef2SBarry Smith {
39857f52daa2SLisandro Dalcin   PetscDraw      draw;
3986dfbe8321SBarry Smith   PetscErrorCode ierr;
3987d763cef2SBarry Smith 
3988d763cef2SBarry Smith   PetscFunctionBegin;
3989b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
39907f52daa2SLisandro Dalcin   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&draw);CHKERRQ(ierr);
39917f52daa2SLisandro Dalcin   ierr = PetscDrawSetFromOptions(draw);CHKERRQ(ierr);
39927f52daa2SLisandro Dalcin   ierr = PetscDrawLGCreate(draw,1,&(*ctx)->lg);CHKERRQ(ierr);
3993287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
39947f52daa2SLisandro Dalcin   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
3995a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
3996d763cef2SBarry Smith   PetscFunctionReturn(0);
3997d763cef2SBarry Smith }
3998d763cef2SBarry Smith 
3999b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
4000d763cef2SBarry Smith {
40010b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
4002c32365f1SBarry Smith   PetscReal      x   = ptime,y;
4003dfbe8321SBarry Smith   PetscErrorCode ierr;
4004d763cef2SBarry Smith 
4005d763cef2SBarry Smith   PetscFunctionBegin;
400663e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates an interpolated solution */
4007b06615a5SLisandro Dalcin   if (!step) {
4008a9f9c1f6SBarry Smith     PetscDrawAxis axis;
40098b668821SLisandro Dalcin     const char *ylabel = ctx->semilogy ? "Log Time Step" : "Time Step";
4010a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
40118b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time",ylabel);CHKERRQ(ierr);
4012a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
4013a9f9c1f6SBarry Smith   }
4014c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
40158b668821SLisandro Dalcin   if (ctx->semilogy) y = PetscLog10Real(y);
40160b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
4017b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
40180b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
40196934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
40203923b477SBarry Smith   }
4021d763cef2SBarry Smith   PetscFunctionReturn(0);
4022d763cef2SBarry Smith }
4023d763cef2SBarry Smith 
4024d763cef2SBarry Smith /*@C
4025a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
4026a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
4027d763cef2SBarry Smith 
40280b039ecaSBarry Smith    Collective on TSMonitorLGCtx
4029d763cef2SBarry Smith 
4030d763cef2SBarry Smith    Input Parameter:
40310b039ecaSBarry Smith .  ctx - the monitor context
4032d763cef2SBarry Smith 
4033d763cef2SBarry Smith    Level: intermediate
4034d763cef2SBarry Smith 
4035d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
4036d763cef2SBarry Smith 
40374f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
4038d763cef2SBarry Smith @*/
4039a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
4040d763cef2SBarry Smith {
4041dfbe8321SBarry Smith   PetscErrorCode ierr;
4042d763cef2SBarry Smith 
4043d763cef2SBarry Smith   PetscFunctionBegin;
40447684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
40457684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
40467684fa3eSBarry Smith   }
40470b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
404831152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
4049387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
4050387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
4051387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
40520b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
4053d763cef2SBarry Smith   PetscFunctionReturn(0);
4054d763cef2SBarry Smith }
4055d763cef2SBarry Smith 
4056d763cef2SBarry Smith /*@
4057b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
4058d763cef2SBarry Smith 
4059d763cef2SBarry Smith    Not Collective
4060d763cef2SBarry Smith 
4061d763cef2SBarry Smith    Input Parameter:
4062d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
4063d763cef2SBarry Smith 
4064d763cef2SBarry Smith    Output Parameter:
406519eac22cSLisandro Dalcin .  t  - the current time. This time may not corresponds to the final time set with TSSetMaxTime(), use TSGetSolveTime().
4066d763cef2SBarry Smith 
4067d763cef2SBarry Smith    Level: beginner
4068d763cef2SBarry Smith 
4069b8123daeSJed Brown    Note:
4070b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
40719be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
4072b8123daeSJed Brown 
4073aaa6c58dSLisandro Dalcin .seealso:  TSGetSolveTime(), TSSetTime(), TSGetTimeStep()
4074d763cef2SBarry Smith 
4075d763cef2SBarry Smith .keywords: TS, get, time
4076d763cef2SBarry Smith @*/
40777087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
4078d763cef2SBarry Smith {
4079d763cef2SBarry Smith   PetscFunctionBegin;
40800700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4081f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
4082d763cef2SBarry Smith   *t = ts->ptime;
4083d763cef2SBarry Smith   PetscFunctionReturn(0);
4084d763cef2SBarry Smith }
4085d763cef2SBarry Smith 
4086e5e524a1SHong Zhang /*@
4087e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
4088e5e524a1SHong Zhang 
4089e5e524a1SHong Zhang    Not Collective
4090e5e524a1SHong Zhang 
4091e5e524a1SHong Zhang    Input Parameter:
4092e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
4093e5e524a1SHong Zhang 
4094e5e524a1SHong Zhang    Output Parameter:
4095e5e524a1SHong Zhang .  t  - the previous time
4096e5e524a1SHong Zhang 
4097e5e524a1SHong Zhang    Level: beginner
4098e5e524a1SHong Zhang 
4099aaa6c58dSLisandro Dalcin .seealso: TSGetTime(), TSGetSolveTime(), TSGetTimeStep()
4100e5e524a1SHong Zhang 
4101e5e524a1SHong Zhang .keywords: TS, get, time
4102e5e524a1SHong Zhang @*/
4103e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
4104e5e524a1SHong Zhang {
4105e5e524a1SHong Zhang   PetscFunctionBegin;
4106e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4107e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
4108e5e524a1SHong Zhang   *t = ts->ptime_prev;
4109e5e524a1SHong Zhang   PetscFunctionReturn(0);
4110e5e524a1SHong Zhang }
4111e5e524a1SHong Zhang 
41126a4d4014SLisandro Dalcin /*@
41136a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
41146a4d4014SLisandro Dalcin 
41153f9fe445SBarry Smith    Logically Collective on TS
41166a4d4014SLisandro Dalcin 
41176a4d4014SLisandro Dalcin    Input Parameters:
41186a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
41196a4d4014SLisandro Dalcin -  time - the time
41206a4d4014SLisandro Dalcin 
41216a4d4014SLisandro Dalcin    Level: intermediate
41226a4d4014SLisandro Dalcin 
412319eac22cSLisandro Dalcin .seealso: TSGetTime(), TSSetMaxSteps()
41246a4d4014SLisandro Dalcin 
41256a4d4014SLisandro Dalcin .keywords: TS, set, time
41266a4d4014SLisandro Dalcin @*/
41277087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
41286a4d4014SLisandro Dalcin {
41296a4d4014SLisandro Dalcin   PetscFunctionBegin;
41300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4131c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
41326a4d4014SLisandro Dalcin   ts->ptime = t;
41336a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
41346a4d4014SLisandro Dalcin }
41356a4d4014SLisandro Dalcin 
4136d763cef2SBarry Smith /*@C
4137d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
4138d763cef2SBarry Smith    TS options in the database.
4139d763cef2SBarry Smith 
41403f9fe445SBarry Smith    Logically Collective on TS
4141d763cef2SBarry Smith 
4142d763cef2SBarry Smith    Input Parameter:
4143d763cef2SBarry Smith +  ts     - The TS context
4144d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4145d763cef2SBarry Smith 
4146d763cef2SBarry Smith    Notes:
4147d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4148d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4149d763cef2SBarry Smith    hyphen.
4150d763cef2SBarry Smith 
4151d763cef2SBarry Smith    Level: advanced
4152d763cef2SBarry Smith 
4153d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
4154d763cef2SBarry Smith 
4155d763cef2SBarry Smith .seealso: TSSetFromOptions()
4156d763cef2SBarry Smith 
4157d763cef2SBarry Smith @*/
41587087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
4159d763cef2SBarry Smith {
4160dfbe8321SBarry Smith   PetscErrorCode ierr;
4161089b2837SJed Brown   SNES           snes;
4162d763cef2SBarry Smith 
4163d763cef2SBarry Smith   PetscFunctionBegin;
41640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4165d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4166089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4167089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4168d763cef2SBarry Smith   PetscFunctionReturn(0);
4169d763cef2SBarry Smith }
4170d763cef2SBarry Smith 
4171d763cef2SBarry Smith /*@C
4172d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
4173d763cef2SBarry Smith    TS options in the database.
4174d763cef2SBarry Smith 
41753f9fe445SBarry Smith    Logically Collective on TS
4176d763cef2SBarry Smith 
4177d763cef2SBarry Smith    Input Parameter:
4178d763cef2SBarry Smith +  ts     - The TS context
4179d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
4180d763cef2SBarry Smith 
4181d763cef2SBarry Smith    Notes:
4182d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4183d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
4184d763cef2SBarry Smith    hyphen.
4185d763cef2SBarry Smith 
4186d763cef2SBarry Smith    Level: advanced
4187d763cef2SBarry Smith 
4188d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
4189d763cef2SBarry Smith 
4190d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
4191d763cef2SBarry Smith 
4192d763cef2SBarry Smith @*/
41937087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
4194d763cef2SBarry Smith {
4195dfbe8321SBarry Smith   PetscErrorCode ierr;
4196089b2837SJed Brown   SNES           snes;
4197d763cef2SBarry Smith 
4198d763cef2SBarry Smith   PetscFunctionBegin;
41990700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4200d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4201089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4202089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
4203d763cef2SBarry Smith   PetscFunctionReturn(0);
4204d763cef2SBarry Smith }
4205d763cef2SBarry Smith 
4206d763cef2SBarry Smith /*@C
4207d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
4208d763cef2SBarry Smith    TS options in the database.
4209d763cef2SBarry Smith 
4210d763cef2SBarry Smith    Not Collective
4211d763cef2SBarry Smith 
4212d763cef2SBarry Smith    Input Parameter:
4213d763cef2SBarry Smith .  ts - The TS context
4214d763cef2SBarry Smith 
4215d763cef2SBarry Smith    Output Parameter:
4216d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
4217d763cef2SBarry Smith 
421895452b02SPatrick Sanan    Notes:
421995452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prifix' of
4220d763cef2SBarry Smith    sufficient length to hold the prefix.
4221d763cef2SBarry Smith 
4222d763cef2SBarry Smith    Level: intermediate
4223d763cef2SBarry Smith 
4224d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
4225d763cef2SBarry Smith 
4226d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
4227d763cef2SBarry Smith @*/
42287087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
4229d763cef2SBarry Smith {
4230dfbe8321SBarry Smith   PetscErrorCode ierr;
4231d763cef2SBarry Smith 
4232d763cef2SBarry Smith   PetscFunctionBegin;
42330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
42344482741eSBarry Smith   PetscValidPointer(prefix,2);
4235d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
4236d763cef2SBarry Smith   PetscFunctionReturn(0);
4237d763cef2SBarry Smith }
4238d763cef2SBarry Smith 
4239d763cef2SBarry Smith /*@C
4240d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
4241d763cef2SBarry Smith 
4242d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
4243d763cef2SBarry Smith 
4244d763cef2SBarry Smith    Input Parameter:
4245d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
4246d763cef2SBarry Smith 
4247d763cef2SBarry Smith    Output Parameters:
4248e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
4249e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
4250e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
4251e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
4252d763cef2SBarry Smith 
425395452b02SPatrick Sanan    Notes:
425495452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
4255d763cef2SBarry Smith 
4256d763cef2SBarry Smith    Level: intermediate
4257d763cef2SBarry Smith 
425880275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
4259d763cef2SBarry Smith 
4260d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
4261d763cef2SBarry Smith @*/
4262e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
4263d763cef2SBarry Smith {
4264089b2837SJed Brown   PetscErrorCode ierr;
426524989b8cSPeter Brune   DM             dm;
4266089b2837SJed Brown 
4267d763cef2SBarry Smith   PetscFunctionBegin;
426823a57915SBarry Smith   if (Amat || Pmat) {
426923a57915SBarry Smith     SNES snes;
4270089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
427123a57915SBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4272e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
427323a57915SBarry Smith   }
427424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
427524989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
4276d763cef2SBarry Smith   PetscFunctionReturn(0);
4277d763cef2SBarry Smith }
4278d763cef2SBarry Smith 
42792eca1d9cSJed Brown /*@C
42802eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
42812eca1d9cSJed Brown 
42822eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
42832eca1d9cSJed Brown 
42842eca1d9cSJed Brown    Input Parameter:
42852eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
42862eca1d9cSJed Brown 
42872eca1d9cSJed Brown    Output Parameters:
4288e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
4289e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
42902eca1d9cSJed Brown .  f   - The function to compute the matrices
42912eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
42922eca1d9cSJed Brown 
429395452b02SPatrick Sanan    Notes:
429495452b02SPatrick Sanan     You can pass in NULL for any return argument you do not need.
42952eca1d9cSJed Brown 
42962eca1d9cSJed Brown    Level: advanced
42972eca1d9cSJed Brown 
429880275a0aSLisandro Dalcin .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetStepNumber()
42992eca1d9cSJed Brown 
43002eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
43012eca1d9cSJed Brown @*/
4302e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
43032eca1d9cSJed Brown {
4304089b2837SJed Brown   PetscErrorCode ierr;
430524989b8cSPeter Brune   DM             dm;
43060910c330SBarry Smith 
43072eca1d9cSJed Brown   PetscFunctionBegin;
4308c0aab802Sstefano_zampini   if (Amat || Pmat) {
4309c0aab802Sstefano_zampini     SNES snes;
4310089b2837SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4311f7d39f7aSBarry Smith     ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
4312e4357dc4SBarry Smith     ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
4313c0aab802Sstefano_zampini   }
431424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
431524989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
43162eca1d9cSJed Brown   PetscFunctionReturn(0);
43172eca1d9cSJed Brown }
43182eca1d9cSJed Brown 
43191713a123SBarry Smith /*@C
432083a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
43211713a123SBarry Smith    VecView() for the solution at each timestep
43221713a123SBarry Smith 
43231713a123SBarry Smith    Collective on TS
43241713a123SBarry Smith 
43251713a123SBarry Smith    Input Parameters:
43261713a123SBarry Smith +  ts - the TS context
43271713a123SBarry Smith .  step - current time-step
4328142b95e3SSatish Balay .  ptime - current time
43290298fd71SBarry Smith -  dummy - either a viewer or NULL
43301713a123SBarry Smith 
433199fdda47SBarry Smith    Options Database:
433299fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
433399fdda47SBarry Smith 
433495452b02SPatrick Sanan    Notes:
433595452b02SPatrick Sanan     the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
433699fdda47SBarry Smith        will look bad
433799fdda47SBarry Smith 
43381713a123SBarry Smith    Level: intermediate
43391713a123SBarry Smith 
43401713a123SBarry Smith .keywords: TS,  vector, monitor, view
43411713a123SBarry Smith 
4342a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
43431713a123SBarry Smith @*/
43440910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
43451713a123SBarry Smith {
4346dfbe8321SBarry Smith   PetscErrorCode   ierr;
434783a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
4348473a3ab2SBarry Smith   PetscDraw        draw;
43491713a123SBarry Smith 
43501713a123SBarry Smith   PetscFunctionBegin;
43516083293cSBarry Smith   if (!step && ictx->showinitial) {
43526083293cSBarry Smith     if (!ictx->initialsolution) {
43530910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
43541713a123SBarry Smith     }
43550910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
43566083293cSBarry Smith   }
4357b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
43580dcf80beSBarry Smith 
43596083293cSBarry Smith   if (ictx->showinitial) {
43606083293cSBarry Smith     PetscReal pause;
43616083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
43626083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
43636083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
43646083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
43656083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
43666083293cSBarry Smith   }
43670910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
4368473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
436951fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
4370473a3ab2SBarry Smith     char      time[32];
4371473a3ab2SBarry Smith 
4372473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
437385b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
4374473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
4375473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
437651fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
4377473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
4378473a3ab2SBarry Smith   }
4379473a3ab2SBarry Smith 
43806083293cSBarry Smith   if (ictx->showinitial) {
43816083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
43826083293cSBarry Smith   }
43831713a123SBarry Smith   PetscFunctionReturn(0);
43841713a123SBarry Smith }
43851713a123SBarry Smith 
43869110b2e7SHong Zhang /*@C
43872d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
43882d5ee99bSBarry Smith 
43892d5ee99bSBarry Smith    Collective on TS
43902d5ee99bSBarry Smith 
43912d5ee99bSBarry Smith    Input Parameters:
43922d5ee99bSBarry Smith +  ts - the TS context
43932d5ee99bSBarry Smith .  step - current time-step
43942d5ee99bSBarry Smith .  ptime - current time
43952d5ee99bSBarry Smith -  dummy - either a viewer or NULL
43962d5ee99bSBarry Smith 
43972d5ee99bSBarry Smith    Level: intermediate
43982d5ee99bSBarry Smith 
43992d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
44002d5ee99bSBarry Smith 
44012d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
44022d5ee99bSBarry Smith @*/
44032d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
44042d5ee99bSBarry Smith {
44052d5ee99bSBarry Smith   PetscErrorCode    ierr;
44062d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
44072d5ee99bSBarry Smith   PetscDraw         draw;
44086934998bSLisandro Dalcin   PetscDrawAxis     axis;
44092d5ee99bSBarry Smith   PetscInt          n;
44102d5ee99bSBarry Smith   PetscMPIInt       size;
44116934998bSLisandro Dalcin   PetscReal         U0,U1,xl,yl,xr,yr,h;
44122d5ee99bSBarry Smith   char              time[32];
44132d5ee99bSBarry Smith   const PetscScalar *U;
44142d5ee99bSBarry Smith 
44152d5ee99bSBarry Smith   PetscFunctionBegin;
44166934998bSLisandro Dalcin   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)ts),&size);CHKERRQ(ierr);
44176934998bSLisandro Dalcin   if (size != 1) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only allowed for sequential runs");
44182d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
44196934998bSLisandro Dalcin   if (n != 2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Only for ODEs with two unknowns");
44202d5ee99bSBarry Smith 
44212d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
44226934998bSLisandro Dalcin   ierr = PetscViewerDrawGetDrawAxis(ictx->viewer,0,&axis);CHKERRQ(ierr);
44236934998bSLisandro Dalcin   ierr = PetscDrawAxisGetLimits(axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
44246934998bSLisandro Dalcin   if (!step) {
44256934998bSLisandro Dalcin     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
44266934998bSLisandro Dalcin     ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
44276934998bSLisandro Dalcin   }
44282d5ee99bSBarry Smith 
44292d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
44306934998bSLisandro Dalcin   U0 = PetscRealPart(U[0]);
44316934998bSLisandro Dalcin   U1 = PetscRealPart(U[1]);
4432a4494fc1SBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
44336934998bSLisandro Dalcin   if ((U0 < xl) || (U1 < yl) || (U0 > xr) || (U1 > yr)) PetscFunctionReturn(0);
44342d5ee99bSBarry Smith 
44356934998bSLisandro Dalcin   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
44366934998bSLisandro Dalcin   ierr = PetscDrawPoint(draw,U0,U1,PETSC_DRAW_BLACK);CHKERRQ(ierr);
44372d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
44384b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
443985b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
44402d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
444151fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
44422d5ee99bSBarry Smith   }
44436934998bSLisandro Dalcin   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
44442d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
444556d62c8fSLisandro Dalcin   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
44466934998bSLisandro Dalcin   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
44472d5ee99bSBarry Smith   PetscFunctionReturn(0);
44482d5ee99bSBarry Smith }
44492d5ee99bSBarry Smith 
44506083293cSBarry Smith /*@C
445183a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
44526083293cSBarry Smith 
44536083293cSBarry Smith    Collective on TS
44546083293cSBarry Smith 
44556083293cSBarry Smith    Input Parameters:
44566083293cSBarry Smith .    ctx - the monitor context
44576083293cSBarry Smith 
44586083293cSBarry Smith    Level: intermediate
44596083293cSBarry Smith 
44606083293cSBarry Smith .keywords: TS,  vector, monitor, view
44616083293cSBarry Smith 
446283a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
44636083293cSBarry Smith @*/
446483a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
44656083293cSBarry Smith {
44666083293cSBarry Smith   PetscErrorCode ierr;
44676083293cSBarry Smith 
44686083293cSBarry Smith   PetscFunctionBegin;
446983a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
447083a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
447183a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
44726083293cSBarry Smith   PetscFunctionReturn(0);
44736083293cSBarry Smith }
44746083293cSBarry Smith 
44756083293cSBarry Smith /*@C
447683a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
44776083293cSBarry Smith 
44786083293cSBarry Smith    Collective on TS
44796083293cSBarry Smith 
44806083293cSBarry Smith    Input Parameter:
44816083293cSBarry Smith .    ts - time-step context
44826083293cSBarry Smith 
44836083293cSBarry Smith    Output Patameter:
44846083293cSBarry Smith .    ctx - the monitor context
44856083293cSBarry Smith 
448699fdda47SBarry Smith    Options Database:
448799fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
448899fdda47SBarry Smith 
44896083293cSBarry Smith    Level: intermediate
44906083293cSBarry Smith 
44916083293cSBarry Smith .keywords: TS,  vector, monitor, view
44926083293cSBarry Smith 
449383a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
44946083293cSBarry Smith @*/
449583a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
44966083293cSBarry Smith {
44976083293cSBarry Smith   PetscErrorCode   ierr;
44986083293cSBarry Smith 
44996083293cSBarry Smith   PetscFunctionBegin;
4500b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
450183a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4502e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4503bbd56ea5SKarl Rupp 
450483a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4505473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
4506c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4507473a3ab2SBarry Smith 
4508473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
4509c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
45106083293cSBarry Smith   PetscFunctionReturn(0);
45116083293cSBarry Smith }
45126083293cSBarry Smith 
45133a471f94SBarry Smith /*@C
45140ed3bfb6SBarry Smith    TSMonitorDrawSolutionFunction - Monitors progress of the TS solvers by calling
45150ed3bfb6SBarry Smith    VecView() for the solution provided by TSSetSolutionFunction() at each timestep
45160ed3bfb6SBarry Smith 
45170ed3bfb6SBarry Smith    Collective on TS
45180ed3bfb6SBarry Smith 
45190ed3bfb6SBarry Smith    Input Parameters:
45200ed3bfb6SBarry Smith +  ts - the TS context
45210ed3bfb6SBarry Smith .  step - current time-step
45220ed3bfb6SBarry Smith .  ptime - current time
45230ed3bfb6SBarry Smith -  dummy - either a viewer or NULL
45240ed3bfb6SBarry Smith 
45250ed3bfb6SBarry Smith    Options Database:
45260ed3bfb6SBarry Smith .  -ts_monitor_draw_solution_function - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
45270ed3bfb6SBarry Smith 
45280ed3bfb6SBarry Smith    Level: intermediate
45290ed3bfb6SBarry Smith 
45300ed3bfb6SBarry Smith .keywords: TS,  vector, monitor, view
45310ed3bfb6SBarry Smith 
45320ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
45330ed3bfb6SBarry Smith @*/
45340ed3bfb6SBarry Smith PetscErrorCode  TSMonitorDrawSolutionFunction(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
45350ed3bfb6SBarry Smith {
45360ed3bfb6SBarry Smith   PetscErrorCode   ierr;
45370ed3bfb6SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
45380ed3bfb6SBarry Smith   PetscViewer      viewer = ctx->viewer;
45390ed3bfb6SBarry Smith   Vec              work;
45400ed3bfb6SBarry Smith 
45410ed3bfb6SBarry Smith   PetscFunctionBegin;
45420ed3bfb6SBarry Smith   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
45430ed3bfb6SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
45440ed3bfb6SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
45450ed3bfb6SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
45460ed3bfb6SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
45470ed3bfb6SBarry Smith   PetscFunctionReturn(0);
45480ed3bfb6SBarry Smith }
45490ed3bfb6SBarry Smith 
45500ed3bfb6SBarry Smith /*@C
455183a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
45523a471f94SBarry Smith    VecView() for the error at each timestep
45533a471f94SBarry Smith 
45543a471f94SBarry Smith    Collective on TS
45553a471f94SBarry Smith 
45563a471f94SBarry Smith    Input Parameters:
45573a471f94SBarry Smith +  ts - the TS context
45583a471f94SBarry Smith .  step - current time-step
45593a471f94SBarry Smith .  ptime - current time
45600298fd71SBarry Smith -  dummy - either a viewer or NULL
45613a471f94SBarry Smith 
45620ed3bfb6SBarry Smith    Options Database:
45630ed3bfb6SBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires user to have provided TSSetSolutionFunction()
45640ed3bfb6SBarry Smith 
45653a471f94SBarry Smith    Level: intermediate
45663a471f94SBarry Smith 
45673a471f94SBarry Smith .keywords: TS,  vector, monitor, view
45683a471f94SBarry Smith 
45690ed3bfb6SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
45703a471f94SBarry Smith @*/
45710910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
45723a471f94SBarry Smith {
45733a471f94SBarry Smith   PetscErrorCode   ierr;
457483a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
457583a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
45763a471f94SBarry Smith   Vec              work;
45773a471f94SBarry Smith 
45783a471f94SBarry Smith   PetscFunctionBegin;
4579b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
45800910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
45813a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
45820910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
45833a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
45843a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
45853a471f94SBarry Smith   PetscFunctionReturn(0);
45863a471f94SBarry Smith }
45873a471f94SBarry Smith 
4588af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
45896c699258SBarry Smith /*@
45902a808120SBarry Smith    TSSetDM - Sets the DM that may be used by some nonlinear solvers or preconditioners under the TS
45916c699258SBarry Smith 
45923f9fe445SBarry Smith    Logically Collective on TS and DM
45936c699258SBarry Smith 
45946c699258SBarry Smith    Input Parameters:
45952a808120SBarry Smith +  ts - the ODE integrator object
45962a808120SBarry Smith -  dm - the dm, cannot be NULL
45976c699258SBarry Smith 
4598e03a659cSJed Brown    Notes:
4599e03a659cSJed Brown    A DM can only be used for solving one problem at a time because information about the problem is stored on the DM,
4600e03a659cSJed Brown    even when not using interfaces like DMTSSetIFunction().  Use DMClone() to get a distinct DM when solving
4601e03a659cSJed Brown    different problems using the same function space.
4602e03a659cSJed Brown 
46036c699258SBarry Smith    Level: intermediate
46046c699258SBarry Smith 
46056c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
46066c699258SBarry Smith @*/
46077087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
46086c699258SBarry Smith {
46096c699258SBarry Smith   PetscErrorCode ierr;
4610089b2837SJed Brown   SNES           snes;
4611942e3340SBarry Smith   DMTS           tsdm;
46126c699258SBarry Smith 
46136c699258SBarry Smith   PetscFunctionBegin;
46140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
46152a808120SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,2);
461670663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4617942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
46182a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4619942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4620942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
462124989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
462224989b8cSPeter Brune         tsdm->originaldm = dm;
462324989b8cSPeter Brune       }
462424989b8cSPeter Brune     }
46256bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
462624989b8cSPeter Brune   }
46276c699258SBarry Smith   ts->dm = dm;
4628bbd56ea5SKarl Rupp 
4629089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4630089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
46316c699258SBarry Smith   PetscFunctionReturn(0);
46326c699258SBarry Smith }
46336c699258SBarry Smith 
46346c699258SBarry Smith /*@
46356c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
46366c699258SBarry Smith 
46373f9fe445SBarry Smith    Not Collective
46386c699258SBarry Smith 
46396c699258SBarry Smith    Input Parameter:
46406c699258SBarry Smith . ts - the preconditioner context
46416c699258SBarry Smith 
46426c699258SBarry Smith    Output Parameter:
46436c699258SBarry Smith .  dm - the dm
46446c699258SBarry Smith 
46456c699258SBarry Smith    Level: intermediate
46466c699258SBarry Smith 
46476c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
46486c699258SBarry Smith @*/
46497087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
46506c699258SBarry Smith {
4651496e6a7aSJed Brown   PetscErrorCode ierr;
4652496e6a7aSJed Brown 
46536c699258SBarry Smith   PetscFunctionBegin;
46540700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4655496e6a7aSJed Brown   if (!ts->dm) {
4656ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4657496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4658496e6a7aSJed Brown   }
46596c699258SBarry Smith   *dm = ts->dm;
46606c699258SBarry Smith   PetscFunctionReturn(0);
46616c699258SBarry Smith }
46621713a123SBarry Smith 
46630f5c6efeSJed Brown /*@
46640f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
46650f5c6efeSJed Brown 
46663f9fe445SBarry Smith    Logically Collective on SNES
46670f5c6efeSJed Brown 
46680f5c6efeSJed Brown    Input Parameter:
4669d42a1c89SJed Brown + snes - nonlinear solver
46700910c330SBarry Smith . U - the current state at which to evaluate the residual
4671d42a1c89SJed Brown - ctx - user context, must be a TS
46720f5c6efeSJed Brown 
46730f5c6efeSJed Brown    Output Parameter:
46740f5c6efeSJed Brown . F - the nonlinear residual
46750f5c6efeSJed Brown 
46760f5c6efeSJed Brown    Notes:
46770f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
46780f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
46790f5c6efeSJed Brown 
46800f5c6efeSJed Brown    Level: advanced
46810f5c6efeSJed Brown 
46820f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
46830f5c6efeSJed Brown @*/
46840910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
46850f5c6efeSJed Brown {
46860f5c6efeSJed Brown   TS             ts = (TS)ctx;
46870f5c6efeSJed Brown   PetscErrorCode ierr;
46880f5c6efeSJed Brown 
46890f5c6efeSJed Brown   PetscFunctionBegin;
46900f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
46910910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
46920f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
46930f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
46940910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
46950f5c6efeSJed Brown   PetscFunctionReturn(0);
46960f5c6efeSJed Brown }
46970f5c6efeSJed Brown 
46980f5c6efeSJed Brown /*@
46990f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
47000f5c6efeSJed Brown 
47010f5c6efeSJed Brown    Collective on SNES
47020f5c6efeSJed Brown 
47030f5c6efeSJed Brown    Input Parameter:
47040f5c6efeSJed Brown + snes - nonlinear solver
47050910c330SBarry Smith . U - the current state at which to evaluate the residual
47060f5c6efeSJed Brown - ctx - user context, must be a TS
47070f5c6efeSJed Brown 
47080f5c6efeSJed Brown    Output Parameter:
47090f5c6efeSJed Brown + A - the Jacobian
47100f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
47110f5c6efeSJed Brown - flag - indicates any structure change in the matrix
47120f5c6efeSJed Brown 
47130f5c6efeSJed Brown    Notes:
47140f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
47150f5c6efeSJed Brown 
47160f5c6efeSJed Brown    Level: developer
47170f5c6efeSJed Brown 
47180f5c6efeSJed Brown .seealso: SNESSetJacobian()
47190f5c6efeSJed Brown @*/
4720d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
47210f5c6efeSJed Brown {
47220f5c6efeSJed Brown   TS             ts = (TS)ctx;
47230f5c6efeSJed Brown   PetscErrorCode ierr;
47240f5c6efeSJed Brown 
47250f5c6efeSJed Brown   PetscFunctionBegin;
47260f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
47270910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
47280f5c6efeSJed Brown   PetscValidPointer(A,3);
472994ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
47300f5c6efeSJed Brown   PetscValidPointer(B,4);
473194ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
47320f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4733d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
47340f5c6efeSJed Brown   PetscFunctionReturn(0);
47350f5c6efeSJed Brown }
4736325fc9f4SBarry Smith 
47370e4ef248SJed Brown /*@C
47389ae8fd06SBarry Smith    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems Udot = A U only
47390e4ef248SJed Brown 
47400e4ef248SJed Brown    Collective on TS
47410e4ef248SJed Brown 
47420e4ef248SJed Brown    Input Arguments:
47430e4ef248SJed Brown +  ts - time stepping context
47440e4ef248SJed Brown .  t - time at which to evaluate
47450910c330SBarry Smith .  U - state at which to evaluate
47460e4ef248SJed Brown -  ctx - context
47470e4ef248SJed Brown 
47480e4ef248SJed Brown    Output Arguments:
47490e4ef248SJed Brown .  F - right hand side
47500e4ef248SJed Brown 
47510e4ef248SJed Brown    Level: intermediate
47520e4ef248SJed Brown 
47530e4ef248SJed Brown    Notes:
47540e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
47550e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
47560e4ef248SJed Brown 
47570e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
47580e4ef248SJed Brown @*/
47590910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
47600e4ef248SJed Brown {
47610e4ef248SJed Brown   PetscErrorCode ierr;
47620e4ef248SJed Brown   Mat            Arhs,Brhs;
47630e4ef248SJed Brown 
47640e4ef248SJed Brown   PetscFunctionBegin;
47650e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
4766d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
47670910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
47680e4ef248SJed Brown   PetscFunctionReturn(0);
47690e4ef248SJed Brown }
47700e4ef248SJed Brown 
47710e4ef248SJed Brown /*@C
47720e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
47730e4ef248SJed Brown 
47740e4ef248SJed Brown    Collective on TS
47750e4ef248SJed Brown 
47760e4ef248SJed Brown    Input Arguments:
47770e4ef248SJed Brown +  ts - time stepping context
47780e4ef248SJed Brown .  t - time at which to evaluate
47790910c330SBarry Smith .  U - state at which to evaluate
47800e4ef248SJed Brown -  ctx - context
47810e4ef248SJed Brown 
47820e4ef248SJed Brown    Output Arguments:
47830e4ef248SJed Brown +  A - pointer to operator
47840e4ef248SJed Brown .  B - pointer to preconditioning matrix
47850e4ef248SJed Brown -  flg - matrix structure flag
47860e4ef248SJed Brown 
47870e4ef248SJed Brown    Level: intermediate
47880e4ef248SJed Brown 
47890e4ef248SJed Brown    Notes:
47900e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
47910e4ef248SJed Brown 
47920e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
47930e4ef248SJed Brown @*/
4794d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
47950e4ef248SJed Brown {
47960e4ef248SJed Brown   PetscFunctionBegin;
47970e4ef248SJed Brown   PetscFunctionReturn(0);
47980e4ef248SJed Brown }
47990e4ef248SJed Brown 
48000026cea9SSean Farley /*@C
48010026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
48020026cea9SSean Farley 
48030026cea9SSean Farley    Collective on TS
48040026cea9SSean Farley 
48050026cea9SSean Farley    Input Arguments:
48060026cea9SSean Farley +  ts - time stepping context
48070026cea9SSean Farley .  t - time at which to evaluate
48080910c330SBarry Smith .  U - state at which to evaluate
48090910c330SBarry Smith .  Udot - time derivative of state vector
48100026cea9SSean Farley -  ctx - context
48110026cea9SSean Farley 
48120026cea9SSean Farley    Output Arguments:
48130026cea9SSean Farley .  F - left hand side
48140026cea9SSean Farley 
48150026cea9SSean Farley    Level: intermediate
48160026cea9SSean Farley 
48170026cea9SSean Farley    Notes:
48180910c330SBarry 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
48190026cea9SSean Farley    user is required to write their own TSComputeIFunction.
48200026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
48210026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
48220026cea9SSean Farley 
48239ae8fd06SBarry Smith    Note that using this function is NOT equivalent to using TSComputeRHSFunctionLinear() since that solves Udot = A U
48249ae8fd06SBarry Smith 
48259ae8fd06SBarry Smith .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant(), TSComputeRHSFunctionLinear()
48260026cea9SSean Farley @*/
48270910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
48280026cea9SSean Farley {
48290026cea9SSean Farley   PetscErrorCode ierr;
48300026cea9SSean Farley   Mat            A,B;
48310026cea9SSean Farley 
48320026cea9SSean Farley   PetscFunctionBegin;
48330298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
4834d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
48350910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
48360026cea9SSean Farley   PetscFunctionReturn(0);
48370026cea9SSean Farley }
48380026cea9SSean Farley 
48390026cea9SSean Farley /*@C
4840b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
48410026cea9SSean Farley 
48420026cea9SSean Farley    Collective on TS
48430026cea9SSean Farley 
48440026cea9SSean Farley    Input Arguments:
48450026cea9SSean Farley +  ts - time stepping context
48460026cea9SSean Farley .  t - time at which to evaluate
48470910c330SBarry Smith .  U - state at which to evaluate
48480910c330SBarry Smith .  Udot - time derivative of state vector
48490026cea9SSean Farley .  shift - shift to apply
48500026cea9SSean Farley -  ctx - context
48510026cea9SSean Farley 
48520026cea9SSean Farley    Output Arguments:
48530026cea9SSean Farley +  A - pointer to operator
48540026cea9SSean Farley .  B - pointer to preconditioning matrix
48550026cea9SSean Farley -  flg - matrix structure flag
48560026cea9SSean Farley 
4857b41af12eSJed Brown    Level: advanced
48580026cea9SSean Farley 
48590026cea9SSean Farley    Notes:
48600026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
48610026cea9SSean Farley 
4862b41af12eSJed Brown    It is only appropriate for problems of the form
4863b41af12eSJed Brown 
4864b41af12eSJed Brown $     M Udot = F(U,t)
4865b41af12eSJed Brown 
4866b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4867b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4868b41af12eSJed Brown   an implicit operator of the form
4869b41af12eSJed Brown 
4870b41af12eSJed Brown $    shift*M + J
4871b41af12eSJed Brown 
4872b41af12eSJed 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
4873b41af12eSJed Brown   a copy of M or reassemble it when requested.
4874b41af12eSJed Brown 
48750026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
48760026cea9SSean Farley @*/
4877d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
48780026cea9SSean Farley {
4879b41af12eSJed Brown   PetscErrorCode ierr;
4880b41af12eSJed Brown 
48810026cea9SSean Farley   PetscFunctionBegin;
488294ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4883b41af12eSJed Brown   ts->ijacobian.shift = shift;
48840026cea9SSean Farley   PetscFunctionReturn(0);
48850026cea9SSean Farley }
4886b41af12eSJed Brown 
4887e817cc15SEmil Constantinescu /*@
4888e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4889e817cc15SEmil Constantinescu 
4890e817cc15SEmil Constantinescu    Not Collective
4891e817cc15SEmil Constantinescu 
4892e817cc15SEmil Constantinescu    Input Parameter:
4893e817cc15SEmil Constantinescu .  ts - the TS context
4894e817cc15SEmil Constantinescu 
4895e817cc15SEmil Constantinescu    Output Parameter:
48964e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4897e817cc15SEmil Constantinescu 
4898e817cc15SEmil Constantinescu    Level: beginner
4899e817cc15SEmil Constantinescu 
4900e817cc15SEmil Constantinescu .keywords: TS, equation type
4901e817cc15SEmil Constantinescu 
4902e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4903e817cc15SEmil Constantinescu @*/
4904e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4905e817cc15SEmil Constantinescu {
4906e817cc15SEmil Constantinescu   PetscFunctionBegin;
4907e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4908e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4909e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4910e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4911e817cc15SEmil Constantinescu }
4912e817cc15SEmil Constantinescu 
4913e817cc15SEmil Constantinescu /*@
4914e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4915e817cc15SEmil Constantinescu 
4916e817cc15SEmil Constantinescu    Not Collective
4917e817cc15SEmil Constantinescu 
4918e817cc15SEmil Constantinescu    Input Parameter:
4919e817cc15SEmil Constantinescu +  ts - the TS context
49201297b224SEmil Constantinescu -  equation_type - see TSEquationType
4921e817cc15SEmil Constantinescu 
4922e817cc15SEmil Constantinescu    Level: advanced
4923e817cc15SEmil Constantinescu 
4924e817cc15SEmil Constantinescu .keywords: TS, equation type
4925e817cc15SEmil Constantinescu 
4926e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4927e817cc15SEmil Constantinescu @*/
4928e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4929e817cc15SEmil Constantinescu {
4930e817cc15SEmil Constantinescu   PetscFunctionBegin;
4931e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4932e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4933e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4934e817cc15SEmil Constantinescu }
49350026cea9SSean Farley 
49364af1b03aSJed Brown /*@
49374af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
49384af1b03aSJed Brown 
49394af1b03aSJed Brown    Not Collective
49404af1b03aSJed Brown 
49414af1b03aSJed Brown    Input Parameter:
49424af1b03aSJed Brown .  ts - the TS context
49434af1b03aSJed Brown 
49444af1b03aSJed Brown    Output Parameter:
49454af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
49464af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
49474af1b03aSJed Brown 
4948487e0bb9SJed Brown    Level: beginner
49494af1b03aSJed Brown 
4950cd652676SJed Brown    Notes:
4951cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
49524af1b03aSJed Brown 
49534af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
49544af1b03aSJed Brown 
49554af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
49564af1b03aSJed Brown @*/
49574af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
49584af1b03aSJed Brown {
49594af1b03aSJed Brown   PetscFunctionBegin;
49604af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49614af1b03aSJed Brown   PetscValidPointer(reason,2);
49624af1b03aSJed Brown   *reason = ts->reason;
49634af1b03aSJed Brown   PetscFunctionReturn(0);
49644af1b03aSJed Brown }
49654af1b03aSJed Brown 
4966d6ad946cSShri Abhyankar /*@
4967d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
4968d6ad946cSShri Abhyankar 
4969d6ad946cSShri Abhyankar    Not Collective
4970d6ad946cSShri Abhyankar 
4971d6ad946cSShri Abhyankar    Input Parameter:
4972d6ad946cSShri Abhyankar +  ts - the TS context
4973d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
4974d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4975d6ad946cSShri Abhyankar 
4976f5abba47SShri Abhyankar    Level: advanced
4977d6ad946cSShri Abhyankar 
4978d6ad946cSShri Abhyankar    Notes:
4979d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
4980d6ad946cSShri Abhyankar 
4981d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
4982d6ad946cSShri Abhyankar 
4983d6ad946cSShri Abhyankar .seealso: TSConvergedReason
4984d6ad946cSShri Abhyankar @*/
4985d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
4986d6ad946cSShri Abhyankar {
4987d6ad946cSShri Abhyankar   PetscFunctionBegin;
4988d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4989d6ad946cSShri Abhyankar   ts->reason = reason;
4990d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
4991d6ad946cSShri Abhyankar }
4992d6ad946cSShri Abhyankar 
4993cc708dedSBarry Smith /*@
4994cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
4995cc708dedSBarry Smith 
4996cc708dedSBarry Smith    Not Collective
4997cc708dedSBarry Smith 
4998cc708dedSBarry Smith    Input Parameter:
4999cc708dedSBarry Smith .  ts - the TS context
5000cc708dedSBarry Smith 
5001cc708dedSBarry Smith    Output Parameter:
500219eac22cSLisandro Dalcin .  ftime - the final time. This time corresponds to the final time set with TSSetMaxTime()
5003cc708dedSBarry Smith 
5004487e0bb9SJed Brown    Level: beginner
5005cc708dedSBarry Smith 
5006cc708dedSBarry Smith    Notes:
5007cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
5008cc708dedSBarry Smith 
5009cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
5010cc708dedSBarry Smith 
5011cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
5012cc708dedSBarry Smith @*/
5013cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
5014cc708dedSBarry Smith {
5015cc708dedSBarry Smith   PetscFunctionBegin;
5016cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5017cc708dedSBarry Smith   PetscValidPointer(ftime,2);
5018cc708dedSBarry Smith   *ftime = ts->solvetime;
5019cc708dedSBarry Smith   PetscFunctionReturn(0);
5020cc708dedSBarry Smith }
5021cc708dedSBarry Smith 
50222c18e0fdSBarry Smith /*@
50235ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
50249f67acb7SJed Brown    used by the time integrator.
50259f67acb7SJed Brown 
50269f67acb7SJed Brown    Not Collective
50279f67acb7SJed Brown 
50289f67acb7SJed Brown    Input Parameter:
50299f67acb7SJed Brown .  ts - TS context
50309f67acb7SJed Brown 
50319f67acb7SJed Brown    Output Parameter:
50329f67acb7SJed Brown .  nits - number of nonlinear iterations
50339f67acb7SJed Brown 
50349f67acb7SJed Brown    Notes:
50359f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
50369f67acb7SJed Brown 
50379f67acb7SJed Brown    Level: intermediate
50389f67acb7SJed Brown 
50399f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
50409f67acb7SJed Brown 
50415ef26d82SJed Brown .seealso:  TSGetKSPIterations()
50429f67acb7SJed Brown @*/
50435ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
50449f67acb7SJed Brown {
50459f67acb7SJed Brown   PetscFunctionBegin;
50469f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
50479f67acb7SJed Brown   PetscValidIntPointer(nits,2);
50485ef26d82SJed Brown   *nits = ts->snes_its;
50499f67acb7SJed Brown   PetscFunctionReturn(0);
50509f67acb7SJed Brown }
50519f67acb7SJed Brown 
50529f67acb7SJed Brown /*@
50535ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
50549f67acb7SJed Brown    used by the time integrator.
50559f67acb7SJed Brown 
50569f67acb7SJed Brown    Not Collective
50579f67acb7SJed Brown 
50589f67acb7SJed Brown    Input Parameter:
50599f67acb7SJed Brown .  ts - TS context
50609f67acb7SJed Brown 
50619f67acb7SJed Brown    Output Parameter:
50629f67acb7SJed Brown .  lits - number of linear iterations
50639f67acb7SJed Brown 
50649f67acb7SJed Brown    Notes:
50659f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
50669f67acb7SJed Brown 
50679f67acb7SJed Brown    Level: intermediate
50689f67acb7SJed Brown 
50699f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
50709f67acb7SJed Brown 
50715ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
50729f67acb7SJed Brown @*/
50735ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
50749f67acb7SJed Brown {
50759f67acb7SJed Brown   PetscFunctionBegin;
50769f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
50779f67acb7SJed Brown   PetscValidIntPointer(lits,2);
50785ef26d82SJed Brown   *lits = ts->ksp_its;
50799f67acb7SJed Brown   PetscFunctionReturn(0);
50809f67acb7SJed Brown }
50819f67acb7SJed Brown 
5082cef5090cSJed Brown /*@
5083cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
5084cef5090cSJed Brown 
5085cef5090cSJed Brown    Not Collective
5086cef5090cSJed Brown 
5087cef5090cSJed Brown    Input Parameter:
5088cef5090cSJed Brown .  ts - TS context
5089cef5090cSJed Brown 
5090cef5090cSJed Brown    Output Parameter:
5091cef5090cSJed Brown .  rejects - number of steps rejected
5092cef5090cSJed Brown 
5093cef5090cSJed Brown    Notes:
5094cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5095cef5090cSJed Brown 
5096cef5090cSJed Brown    Level: intermediate
5097cef5090cSJed Brown 
5098cef5090cSJed Brown .keywords: TS, get, number
5099cef5090cSJed Brown 
51005ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
5101cef5090cSJed Brown @*/
5102cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
5103cef5090cSJed Brown {
5104cef5090cSJed Brown   PetscFunctionBegin;
5105cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5106cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
5107cef5090cSJed Brown   *rejects = ts->reject;
5108cef5090cSJed Brown   PetscFunctionReturn(0);
5109cef5090cSJed Brown }
5110cef5090cSJed Brown 
5111cef5090cSJed Brown /*@
5112cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
5113cef5090cSJed Brown 
5114cef5090cSJed Brown    Not Collective
5115cef5090cSJed Brown 
5116cef5090cSJed Brown    Input Parameter:
5117cef5090cSJed Brown .  ts - TS context
5118cef5090cSJed Brown 
5119cef5090cSJed Brown    Output Parameter:
5120cef5090cSJed Brown .  fails - number of failed nonlinear solves
5121cef5090cSJed Brown 
5122cef5090cSJed Brown    Notes:
5123cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
5124cef5090cSJed Brown 
5125cef5090cSJed Brown    Level: intermediate
5126cef5090cSJed Brown 
5127cef5090cSJed Brown .keywords: TS, get, number
5128cef5090cSJed Brown 
51295ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
5130cef5090cSJed Brown @*/
5131cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
5132cef5090cSJed Brown {
5133cef5090cSJed Brown   PetscFunctionBegin;
5134cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5135cef5090cSJed Brown   PetscValidIntPointer(fails,2);
5136cef5090cSJed Brown   *fails = ts->num_snes_failures;
5137cef5090cSJed Brown   PetscFunctionReturn(0);
5138cef5090cSJed Brown }
5139cef5090cSJed Brown 
5140cef5090cSJed Brown /*@
5141cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
5142cef5090cSJed Brown 
5143cef5090cSJed Brown    Not Collective
5144cef5090cSJed Brown 
5145cef5090cSJed Brown    Input Parameter:
5146cef5090cSJed Brown +  ts - TS context
5147cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
5148cef5090cSJed Brown 
5149cef5090cSJed Brown    Notes:
5150cef5090cSJed Brown    The counter is reset to zero for each step
5151cef5090cSJed Brown 
5152cef5090cSJed Brown    Options Database Key:
5153cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
5154cef5090cSJed Brown 
5155cef5090cSJed Brown    Level: intermediate
5156cef5090cSJed Brown 
5157cef5090cSJed Brown .keywords: TS, set, maximum, number
5158cef5090cSJed Brown 
51595ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5160cef5090cSJed Brown @*/
5161cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
5162cef5090cSJed Brown {
5163cef5090cSJed Brown   PetscFunctionBegin;
5164cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5165cef5090cSJed Brown   ts->max_reject = rejects;
5166cef5090cSJed Brown   PetscFunctionReturn(0);
5167cef5090cSJed Brown }
5168cef5090cSJed Brown 
5169cef5090cSJed Brown /*@
5170cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
5171cef5090cSJed Brown 
5172cef5090cSJed Brown    Not Collective
5173cef5090cSJed Brown 
5174cef5090cSJed Brown    Input Parameter:
5175cef5090cSJed Brown +  ts - TS context
5176cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
5177cef5090cSJed Brown 
5178cef5090cSJed Brown    Notes:
5179cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
5180cef5090cSJed Brown 
5181cef5090cSJed Brown    Options Database Key:
5182cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
5183cef5090cSJed Brown 
5184cef5090cSJed Brown    Level: intermediate
5185cef5090cSJed Brown 
5186cef5090cSJed Brown .keywords: TS, set, maximum, number
5187cef5090cSJed Brown 
51885ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
5189cef5090cSJed Brown @*/
5190cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
5191cef5090cSJed Brown {
5192cef5090cSJed Brown   PetscFunctionBegin;
5193cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5194cef5090cSJed Brown   ts->max_snes_failures = fails;
5195cef5090cSJed Brown   PetscFunctionReturn(0);
5196cef5090cSJed Brown }
5197cef5090cSJed Brown 
5198cef5090cSJed Brown /*@
5199cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
5200cef5090cSJed Brown 
5201cef5090cSJed Brown    Not Collective
5202cef5090cSJed Brown 
5203cef5090cSJed Brown    Input Parameter:
5204cef5090cSJed Brown +  ts - TS context
5205cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
5206cef5090cSJed Brown 
5207cef5090cSJed Brown    Options Database Key:
5208cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
5209cef5090cSJed Brown 
5210cef5090cSJed Brown    Level: intermediate
5211cef5090cSJed Brown 
5212cef5090cSJed Brown .keywords: TS, set, error
5213cef5090cSJed Brown 
52145ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
5215cef5090cSJed Brown @*/
5216cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
5217cef5090cSJed Brown {
5218cef5090cSJed Brown   PetscFunctionBegin;
5219cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5220cef5090cSJed Brown   ts->errorifstepfailed = err;
5221cef5090cSJed Brown   PetscFunctionReturn(0);
5222cef5090cSJed Brown }
5223cef5090cSJed Brown 
5224fb1732b5SBarry Smith /*@C
5225fde5950dSBarry 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
5226fb1732b5SBarry Smith 
5227fb1732b5SBarry Smith    Collective on TS
5228fb1732b5SBarry Smith 
5229fb1732b5SBarry Smith    Input Parameters:
5230fb1732b5SBarry Smith +  ts - the TS context
5231fb1732b5SBarry Smith .  step - current time-step
5232fb1732b5SBarry Smith .  ptime - current time
52330910c330SBarry Smith .  u - current state
5234721cd6eeSBarry Smith -  vf - viewer and its format
5235fb1732b5SBarry Smith 
5236fb1732b5SBarry Smith    Level: intermediate
5237fb1732b5SBarry Smith 
5238fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
5239fb1732b5SBarry Smith 
5240fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5241fb1732b5SBarry Smith @*/
5242721cd6eeSBarry Smith PetscErrorCode  TSMonitorSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
5243fb1732b5SBarry Smith {
5244fb1732b5SBarry Smith   PetscErrorCode ierr;
5245fb1732b5SBarry Smith 
5246fb1732b5SBarry Smith   PetscFunctionBegin;
5247721cd6eeSBarry Smith   ierr = PetscViewerPushFormat(vf->viewer,vf->format);CHKERRQ(ierr);
5248721cd6eeSBarry Smith   ierr = VecView(u,vf->viewer);CHKERRQ(ierr);
5249721cd6eeSBarry Smith   ierr = PetscViewerPopFormat(vf->viewer);CHKERRQ(ierr);
5250ed81e22dSJed Brown   PetscFunctionReturn(0);
5251ed81e22dSJed Brown }
5252ed81e22dSJed Brown 
5253ed81e22dSJed Brown /*@C
5254ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
5255ed81e22dSJed Brown 
5256ed81e22dSJed Brown    Collective on TS
5257ed81e22dSJed Brown 
5258ed81e22dSJed Brown    Input Parameters:
5259ed81e22dSJed Brown +  ts - the TS context
5260ed81e22dSJed Brown .  step - current time-step
5261ed81e22dSJed Brown .  ptime - current time
52620910c330SBarry Smith .  u - current state
5263ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5264ed81e22dSJed Brown 
5265ed81e22dSJed Brown    Level: intermediate
5266ed81e22dSJed Brown 
5267ed81e22dSJed Brown    Notes:
5268ed81e22dSJed 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.
5269ed81e22dSJed Brown    These are named according to the file name template.
5270ed81e22dSJed Brown 
5271ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
5272ed81e22dSJed Brown 
5273ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5274ed81e22dSJed Brown 
5275ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5276ed81e22dSJed Brown @*/
52770910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
5278ed81e22dSJed Brown {
5279ed81e22dSJed Brown   PetscErrorCode ierr;
5280ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
5281ed81e22dSJed Brown   PetscViewer    viewer;
5282ed81e22dSJed Brown 
5283ed81e22dSJed Brown   PetscFunctionBegin;
528463e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
52858caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
5286ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
52870910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
5288ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
5289ed81e22dSJed Brown   PetscFunctionReturn(0);
5290ed81e22dSJed Brown }
5291ed81e22dSJed Brown 
5292ed81e22dSJed Brown /*@C
5293ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
5294ed81e22dSJed Brown 
5295ed81e22dSJed Brown    Collective on TS
5296ed81e22dSJed Brown 
5297ed81e22dSJed Brown    Input Parameters:
5298ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
5299ed81e22dSJed Brown 
5300ed81e22dSJed Brown    Level: intermediate
5301ed81e22dSJed Brown 
5302ed81e22dSJed Brown    Note:
5303ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
5304ed81e22dSJed Brown 
5305ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
5306ed81e22dSJed Brown 
5307ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
5308ed81e22dSJed Brown @*/
5309ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
5310ed81e22dSJed Brown {
5311ed81e22dSJed Brown   PetscErrorCode ierr;
5312ed81e22dSJed Brown 
5313ed81e22dSJed Brown   PetscFunctionBegin;
5314ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
5315fb1732b5SBarry Smith   PetscFunctionReturn(0);
5316fb1732b5SBarry Smith }
5317fb1732b5SBarry Smith 
531884df9cb4SJed Brown /*@
5319552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
532084df9cb4SJed Brown 
5321ed81e22dSJed Brown    Collective on TS if controller has not been created yet
532284df9cb4SJed Brown 
532384df9cb4SJed Brown    Input Arguments:
5324ed81e22dSJed Brown .  ts - time stepping context
532584df9cb4SJed Brown 
532684df9cb4SJed Brown    Output Arguments:
5327ed81e22dSJed Brown .  adapt - adaptive controller
532884df9cb4SJed Brown 
532984df9cb4SJed Brown    Level: intermediate
533084df9cb4SJed Brown 
5331ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
533284df9cb4SJed Brown @*/
5333552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
533484df9cb4SJed Brown {
533584df9cb4SJed Brown   PetscErrorCode ierr;
533684df9cb4SJed Brown 
533784df9cb4SJed Brown   PetscFunctionBegin;
533884df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5339bec58848SLisandro Dalcin   PetscValidPointer(adapt,2);
534084df9cb4SJed Brown   if (!ts->adapt) {
5341ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
53423bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
53431c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
534484df9cb4SJed Brown   }
5345bec58848SLisandro Dalcin   *adapt = ts->adapt;
534684df9cb4SJed Brown   PetscFunctionReturn(0);
534784df9cb4SJed Brown }
5348d6ebe24aSShri Abhyankar 
53491c3436cfSJed Brown /*@
53501c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
53511c3436cfSJed Brown 
53521c3436cfSJed Brown    Logically Collective
53531c3436cfSJed Brown 
53541c3436cfSJed Brown    Input Arguments:
53551c3436cfSJed Brown +  ts - time integration context
53561c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
53570298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
53581c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
53590298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
53601c3436cfSJed Brown 
5361a3cdaa26SBarry Smith    Options Database keys:
5362a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
5363a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
5364a3cdaa26SBarry Smith 
53653ff766beSShri Abhyankar    Notes:
53663ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
53673ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
53683ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
53693ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
53703ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
53713ff766beSShri Abhyankar    differential variables.
53723ff766beSShri Abhyankar 
53731c3436cfSJed Brown    Level: beginner
53741c3436cfSJed Brown 
5375c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
53761c3436cfSJed Brown @*/
53771c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
53781c3436cfSJed Brown {
53791c3436cfSJed Brown   PetscErrorCode ierr;
53801c3436cfSJed Brown 
53811c3436cfSJed Brown   PetscFunctionBegin;
5382c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
53831c3436cfSJed Brown   if (vatol) {
53841c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
53851c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
53861c3436cfSJed Brown     ts->vatol = vatol;
53871c3436cfSJed Brown   }
5388c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
53891c3436cfSJed Brown   if (vrtol) {
53901c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
53911c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
53921c3436cfSJed Brown     ts->vrtol = vrtol;
53931c3436cfSJed Brown   }
53941c3436cfSJed Brown   PetscFunctionReturn(0);
53951c3436cfSJed Brown }
53961c3436cfSJed Brown 
5397c5033834SJed Brown /*@
5398c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
5399c5033834SJed Brown 
5400c5033834SJed Brown    Logically Collective
5401c5033834SJed Brown 
5402c5033834SJed Brown    Input Arguments:
5403c5033834SJed Brown .  ts - time integration context
5404c5033834SJed Brown 
5405c5033834SJed Brown    Output Arguments:
54060298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
54070298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
54080298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
54090298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
5410c5033834SJed Brown 
5411c5033834SJed Brown    Level: beginner
5412c5033834SJed Brown 
5413c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
5414c5033834SJed Brown @*/
5415c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
5416c5033834SJed Brown {
5417c5033834SJed Brown   PetscFunctionBegin;
5418c5033834SJed Brown   if (atol)  *atol  = ts->atol;
5419c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
5420c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
5421c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
5422c5033834SJed Brown   PetscFunctionReturn(0);
5423c5033834SJed Brown }
5424c5033834SJed Brown 
54259c6b16b5SShri Abhyankar /*@
5426a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
54279c6b16b5SShri Abhyankar 
54289c6b16b5SShri Abhyankar    Collective on TS
54299c6b16b5SShri Abhyankar 
54309c6b16b5SShri Abhyankar    Input Arguments:
54319c6b16b5SShri Abhyankar +  ts - time stepping context
5432a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5433a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
54349c6b16b5SShri Abhyankar 
54359c6b16b5SShri Abhyankar    Output Arguments:
54367453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
54377453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
54387453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
54399c6b16b5SShri Abhyankar 
54409c6b16b5SShri Abhyankar    Level: developer
54419c6b16b5SShri Abhyankar 
5442deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
54439c6b16b5SShri Abhyankar @*/
54447453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
54459c6b16b5SShri Abhyankar {
54469c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
54479c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
54487453f775SEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
54497453f775SEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
54509c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
54517453f775SEmil Constantinescu   PetscReal         sum,suma,sumr,gsum,gsuma,gsumr,diff;
54527453f775SEmil Constantinescu   PetscReal         tol,tola,tolr;
54537453f775SEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
54549c6b16b5SShri Abhyankar 
54559c6b16b5SShri Abhyankar   PetscFunctionBegin;
54569c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5457a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5458a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5459a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5460a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5461a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5462a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
54638a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
54648a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5465a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
54669c6b16b5SShri Abhyankar 
54679c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
54689c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
54699c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
54709c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
54719c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
54727453f775SEmil Constantinescu   sum  = 0.; n_loc  = 0;
54737453f775SEmil Constantinescu   suma = 0.; na_loc = 0;
54747453f775SEmil Constantinescu   sumr = 0.; nr_loc = 0;
54759c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
54769c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
54779c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54789c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54799c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
54807453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
54817453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
54827453f775SEmil Constantinescu       if(tola>0.){
54837453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
54847453f775SEmil Constantinescu         na_loc++;
54857453f775SEmil Constantinescu       }
54867453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
54877453f775SEmil Constantinescu       if(tolr>0.){
54887453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
54897453f775SEmil Constantinescu         nr_loc++;
54907453f775SEmil Constantinescu       }
54917453f775SEmil Constantinescu       tol=tola+tolr;
54927453f775SEmil Constantinescu       if(tol>0.){
54937453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
54947453f775SEmil Constantinescu         n_loc++;
54957453f775SEmil Constantinescu       }
54969c6b16b5SShri Abhyankar     }
54979c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
54989c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
54999c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
55009c6b16b5SShri Abhyankar     const PetscScalar *atol;
55019c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55029c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
55037453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55047453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
55057453f775SEmil Constantinescu       if(tola>0.){
55067453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
55077453f775SEmil Constantinescu         na_loc++;
55087453f775SEmil Constantinescu       }
55097453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55107453f775SEmil Constantinescu       if(tolr>0.){
55117453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
55127453f775SEmil Constantinescu         nr_loc++;
55137453f775SEmil Constantinescu       }
55147453f775SEmil Constantinescu       tol=tola+tolr;
55157453f775SEmil Constantinescu       if(tol>0.){
55167453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
55177453f775SEmil Constantinescu         n_loc++;
55187453f775SEmil Constantinescu       }
55199c6b16b5SShri Abhyankar     }
55209c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
55219c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
55229c6b16b5SShri Abhyankar     const PetscScalar *rtol;
55239c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55249c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
55257453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55267453f775SEmil Constantinescu       tola = ts->atol;
55277453f775SEmil Constantinescu       if(tola>0.){
55287453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
55297453f775SEmil Constantinescu         na_loc++;
55307453f775SEmil Constantinescu       }
55317453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55327453f775SEmil Constantinescu       if(tolr>0.){
55337453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
55347453f775SEmil Constantinescu         nr_loc++;
55357453f775SEmil Constantinescu       }
55367453f775SEmil Constantinescu       tol=tola+tolr;
55377453f775SEmil Constantinescu       if(tol>0.){
55387453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
55397453f775SEmil Constantinescu         n_loc++;
55407453f775SEmil Constantinescu       }
55419c6b16b5SShri Abhyankar     }
55429c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
55439c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
55449c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
55457453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
55467453f775SEmil Constantinescu      tola = ts->atol;
55477453f775SEmil Constantinescu       if(tola>0.){
55487453f775SEmil Constantinescu         suma  += PetscSqr(diff/tola);
55497453f775SEmil Constantinescu         na_loc++;
55507453f775SEmil Constantinescu       }
55517453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
55527453f775SEmil Constantinescu       if(tolr>0.){
55537453f775SEmil Constantinescu         sumr  += PetscSqr(diff/tolr);
55547453f775SEmil Constantinescu         nr_loc++;
55557453f775SEmil Constantinescu       }
55567453f775SEmil Constantinescu       tol=tola+tolr;
55577453f775SEmil Constantinescu       if(tol>0.){
55587453f775SEmil Constantinescu         sum  += PetscSqr(diff/tol);
55597453f775SEmil Constantinescu         n_loc++;
55607453f775SEmil Constantinescu       }
55619c6b16b5SShri Abhyankar     }
55629c6b16b5SShri Abhyankar   }
55639c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
55649c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
55659c6b16b5SShri Abhyankar 
55667453f775SEmil Constantinescu   err_loc[0] = sum;
55677453f775SEmil Constantinescu   err_loc[1] = suma;
55687453f775SEmil Constantinescu   err_loc[2] = sumr;
55697453f775SEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
55707453f775SEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
55717453f775SEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
55727453f775SEmil Constantinescu 
5573a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
55747453f775SEmil Constantinescu 
55757453f775SEmil Constantinescu   gsum   = err_glb[0];
55767453f775SEmil Constantinescu   gsuma  = err_glb[1];
55777453f775SEmil Constantinescu   gsumr  = err_glb[2];
55787453f775SEmil Constantinescu   n_glb  = err_glb[3];
55797453f775SEmil Constantinescu   na_glb = err_glb[4];
55807453f775SEmil Constantinescu   nr_glb = err_glb[5];
55817453f775SEmil Constantinescu 
5582b1316ef9SEmil Constantinescu   *norm  = 0.;
5583b1316ef9SEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
5584b1316ef9SEmil Constantinescu   *norma = 0.;
5585b1316ef9SEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
5586b1316ef9SEmil Constantinescu   *normr = 0.;
5587b1316ef9SEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
55889c6b16b5SShri Abhyankar 
55899c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
55907453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
55917453f775SEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
55929c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
55939c6b16b5SShri Abhyankar }
55949c6b16b5SShri Abhyankar 
55959c6b16b5SShri Abhyankar /*@
5596a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
55979c6b16b5SShri Abhyankar 
55989c6b16b5SShri Abhyankar    Collective on TS
55999c6b16b5SShri Abhyankar 
56009c6b16b5SShri Abhyankar    Input Arguments:
56019c6b16b5SShri Abhyankar +  ts - time stepping context
5602a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5603a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
56049c6b16b5SShri Abhyankar 
56059c6b16b5SShri Abhyankar    Output Arguments:
56067453f775SEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
56077453f775SEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
56087453f775SEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
56099c6b16b5SShri Abhyankar 
56109c6b16b5SShri Abhyankar    Level: developer
56119c6b16b5SShri Abhyankar 
5612deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
56139c6b16b5SShri Abhyankar @*/
56147453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
56159c6b16b5SShri Abhyankar {
56169c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
56177453f775SEmil Constantinescu   PetscInt          i,n,N,rstart;
56189c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
56197453f775SEmil Constantinescu   PetscReal         max,gmax,maxa,gmaxa,maxr,gmaxr;
56207453f775SEmil Constantinescu   PetscReal         tol,tola,tolr,diff;
56217453f775SEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
56229c6b16b5SShri Abhyankar 
56239c6b16b5SShri Abhyankar   PetscFunctionBegin;
56249c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5625a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5626a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5627a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5628a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5629a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5630a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
56318a175baeSEmil Constantinescu   PetscValidPointer(norma,5);
56328a175baeSEmil Constantinescu   PetscValidPointer(normr,6);
5633a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
56349c6b16b5SShri Abhyankar 
56359c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
56369c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
56379c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
56389c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
56399c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
56407453f775SEmil Constantinescu 
56417453f775SEmil Constantinescu   max=0.;
56427453f775SEmil Constantinescu   maxa=0.;
56437453f775SEmil Constantinescu   maxr=0.;
56447453f775SEmil Constantinescu 
56457453f775SEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
56469c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
56479c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56489c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56497453f775SEmil Constantinescu 
56507453f775SEmil Constantinescu     for (i=0; i<n; i++) {
56517453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
56527453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
56537453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56547453f775SEmil Constantinescu       tol  = tola+tolr;
56557453f775SEmil Constantinescu       if(tola>0.){
56567453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
56577453f775SEmil Constantinescu       }
56587453f775SEmil Constantinescu       if(tolr>0.){
56597453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
56607453f775SEmil Constantinescu       }
56617453f775SEmil Constantinescu       if(tol>0.){
56627453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
56637453f775SEmil Constantinescu       }
56649c6b16b5SShri Abhyankar     }
56659c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56669c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56679c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
56689c6b16b5SShri Abhyankar     const PetscScalar *atol;
56699c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56707453f775SEmil Constantinescu     for (i=0; i<n; i++) {
56717453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
56727453f775SEmil Constantinescu       tola = PetscRealPart(atol[i]);
56737453f775SEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56747453f775SEmil Constantinescu       tol  = tola+tolr;
56757453f775SEmil Constantinescu       if(tola>0.){
56767453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
56777453f775SEmil Constantinescu       }
56787453f775SEmil Constantinescu       if(tolr>0.){
56797453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
56807453f775SEmil Constantinescu       }
56817453f775SEmil Constantinescu       if(tol>0.){
56827453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
56837453f775SEmil Constantinescu       }
56849c6b16b5SShri Abhyankar     }
56859c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
56869c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
56879c6b16b5SShri Abhyankar     const PetscScalar *rtol;
56889c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
56897453f775SEmil Constantinescu 
56907453f775SEmil Constantinescu     for (i=0; i<n; i++) {
56917453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
56927453f775SEmil Constantinescu       tola = ts->atol;
56937453f775SEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
56947453f775SEmil Constantinescu       tol  = tola+tolr;
56957453f775SEmil Constantinescu       if(tola>0.){
56967453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
56977453f775SEmil Constantinescu       }
56987453f775SEmil Constantinescu       if(tolr>0.){
56997453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
57007453f775SEmil Constantinescu       }
57017453f775SEmil Constantinescu       if(tol>0.){
57027453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
57037453f775SEmil Constantinescu       }
57049c6b16b5SShri Abhyankar     }
57059c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
57069c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
57077453f775SEmil Constantinescu 
57087453f775SEmil Constantinescu     for (i=0; i<n; i++) {
57097453f775SEmil Constantinescu       diff = PetscAbsScalar(y[i] - u[i]);
57107453f775SEmil Constantinescu       tola = ts->atol;
57117453f775SEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
57127453f775SEmil Constantinescu       tol  = tola+tolr;
57137453f775SEmil Constantinescu       if(tola>0.){
57147453f775SEmil Constantinescu         maxa = PetscMax(maxa,diff / tola);
57157453f775SEmil Constantinescu       }
57167453f775SEmil Constantinescu       if(tolr>0.){
57177453f775SEmil Constantinescu         maxr = PetscMax(maxr,diff / tolr);
57187453f775SEmil Constantinescu       }
57197453f775SEmil Constantinescu       if(tol>0.){
57207453f775SEmil Constantinescu         max = PetscMax(max,diff / tol);
57217453f775SEmil Constantinescu       }
57229c6b16b5SShri Abhyankar     }
57239c6b16b5SShri Abhyankar   }
57249c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
57259c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
57267453f775SEmil Constantinescu   err_loc[0] = max;
57277453f775SEmil Constantinescu   err_loc[1] = maxa;
57287453f775SEmil Constantinescu   err_loc[2] = maxr;
5729a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
57307453f775SEmil Constantinescu   gmax   = err_glb[0];
57317453f775SEmil Constantinescu   gmaxa  = err_glb[1];
57327453f775SEmil Constantinescu   gmaxr  = err_glb[2];
57339c6b16b5SShri Abhyankar 
57349c6b16b5SShri Abhyankar   *norm = gmax;
57357453f775SEmil Constantinescu   *norma = gmaxa;
57367453f775SEmil Constantinescu   *normr = gmaxr;
57379c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
57387453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
57397453f775SEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
57409c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
57419c6b16b5SShri Abhyankar }
57429c6b16b5SShri Abhyankar 
57431c3436cfSJed Brown /*@
57448a175baeSEmil Constantinescu    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances
57451c3436cfSJed Brown 
57461c3436cfSJed Brown    Collective on TS
57471c3436cfSJed Brown 
57481c3436cfSJed Brown    Input Arguments:
57491c3436cfSJed Brown +  ts - time stepping context
5750a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5751a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
5752a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
57537619abb3SShri 
57541c3436cfSJed Brown    Output Arguments:
57558a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
57568a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
57578a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
5758a4868fbcSLisandro Dalcin 
5759a4868fbcSLisandro Dalcin    Options Database Keys:
5760a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
5761a4868fbcSLisandro Dalcin 
57621c3436cfSJed Brown    Level: developer
57631c3436cfSJed Brown 
57648a175baeSEmil Constantinescu .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2(), TSErrorWeightedENorm
57651c3436cfSJed Brown @*/
57667453f775SEmil Constantinescu PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
57671c3436cfSJed Brown {
57688beabaa1SBarry Smith   PetscErrorCode ierr;
57691c3436cfSJed Brown 
57701c3436cfSJed Brown   PetscFunctionBegin;
5771a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
57727453f775SEmil Constantinescu     ierr = TSErrorWeightedNorm2(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
5773a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
57747453f775SEmil Constantinescu     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm,norma,normr);CHKERRQ(ierr);
5775a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
57761c3436cfSJed Brown   PetscFunctionReturn(0);
57771c3436cfSJed Brown }
57781c3436cfSJed Brown 
57798a175baeSEmil Constantinescu 
57808a175baeSEmil Constantinescu /*@
57818a175baeSEmil Constantinescu    TSErrorWeightedENorm2 - compute a weighted 2 error norm based on supplied absolute and relative tolerances
57828a175baeSEmil Constantinescu 
57838a175baeSEmil Constantinescu    Collective on TS
57848a175baeSEmil Constantinescu 
57858a175baeSEmil Constantinescu    Input Arguments:
57868a175baeSEmil Constantinescu +  ts - time stepping context
57878a175baeSEmil Constantinescu .  E - error vector
57888a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
57898a175baeSEmil Constantinescu -  Y - state vector, previous time step
57908a175baeSEmil Constantinescu 
57918a175baeSEmil Constantinescu    Output Arguments:
57928a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
57938a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
57948a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
57958a175baeSEmil Constantinescu 
57968a175baeSEmil Constantinescu    Level: developer
57978a175baeSEmil Constantinescu 
57988a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENormInfinity()
57998a175baeSEmil Constantinescu @*/
58008a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm2(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
58018a175baeSEmil Constantinescu {
58028a175baeSEmil Constantinescu   PetscErrorCode    ierr;
58038a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
58048a175baeSEmil Constantinescu   PetscInt          n_loc,na_loc,nr_loc;
58058a175baeSEmil Constantinescu   PetscReal         n_glb,na_glb,nr_glb;
58068a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
58078a175baeSEmil Constantinescu   PetscReal         err,sum,suma,sumr,gsum,gsuma,gsumr;
58088a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
58098a175baeSEmil Constantinescu   PetscReal         err_loc[6],err_glb[6];
58108a175baeSEmil Constantinescu 
58118a175baeSEmil Constantinescu   PetscFunctionBegin;
58128a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
58138a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
58148a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
58158a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
58168a175baeSEmil Constantinescu   PetscValidType(E,2);
58178a175baeSEmil Constantinescu   PetscValidType(U,3);
58188a175baeSEmil Constantinescu   PetscValidType(Y,4);
58198a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
58208a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
58218a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
58228a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
58238a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
58248a175baeSEmil Constantinescu 
58258a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
58268a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
58278a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
58288a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
58298a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
58308a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
58318a175baeSEmil Constantinescu   sum  = 0.; n_loc  = 0;
58328a175baeSEmil Constantinescu   suma = 0.; na_loc = 0;
58338a175baeSEmil Constantinescu   sumr = 0.; nr_loc = 0;
58348a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {
58358a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
58368a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58378a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58388a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
58398a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
58408a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
58418a175baeSEmil Constantinescu       if(tola>0.){
58428a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
58438a175baeSEmil Constantinescu         na_loc++;
58448a175baeSEmil Constantinescu       }
58458a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58468a175baeSEmil Constantinescu       if(tolr>0.){
58478a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
58488a175baeSEmil Constantinescu         nr_loc++;
58498a175baeSEmil Constantinescu       }
58508a175baeSEmil Constantinescu       tol=tola+tolr;
58518a175baeSEmil Constantinescu       if(tol>0.){
58528a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
58538a175baeSEmil Constantinescu         n_loc++;
58548a175baeSEmil Constantinescu       }
58558a175baeSEmil Constantinescu     }
58568a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58578a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58588a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
58598a175baeSEmil Constantinescu     const PetscScalar *atol;
58608a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58618a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
58628a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
58638a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
58648a175baeSEmil Constantinescu       if(tola>0.){
58658a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
58668a175baeSEmil Constantinescu         na_loc++;
58678a175baeSEmil Constantinescu       }
58688a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58698a175baeSEmil Constantinescu       if(tolr>0.){
58708a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
58718a175baeSEmil Constantinescu         nr_loc++;
58728a175baeSEmil Constantinescu       }
58738a175baeSEmil Constantinescu       tol=tola+tolr;
58748a175baeSEmil Constantinescu       if(tol>0.){
58758a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
58768a175baeSEmil Constantinescu         n_loc++;
58778a175baeSEmil Constantinescu       }
58788a175baeSEmil Constantinescu     }
58798a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
58808a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
58818a175baeSEmil Constantinescu     const PetscScalar *rtol;
58828a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
58838a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
58848a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
58858a175baeSEmil Constantinescu       tola = ts->atol;
58868a175baeSEmil Constantinescu       if(tola>0.){
58878a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
58888a175baeSEmil Constantinescu         na_loc++;
58898a175baeSEmil Constantinescu       }
58908a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
58918a175baeSEmil Constantinescu       if(tolr>0.){
58928a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
58938a175baeSEmil Constantinescu         nr_loc++;
58948a175baeSEmil Constantinescu       }
58958a175baeSEmil Constantinescu       tol=tola+tolr;
58968a175baeSEmil Constantinescu       if(tol>0.){
58978a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
58988a175baeSEmil Constantinescu         n_loc++;
58998a175baeSEmil Constantinescu       }
59008a175baeSEmil Constantinescu     }
59018a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
59028a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
59038a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
59048a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
59058a175baeSEmil Constantinescu      tola = ts->atol;
59068a175baeSEmil Constantinescu       if(tola>0.){
59078a175baeSEmil Constantinescu         suma  += PetscSqr(err/tola);
59088a175baeSEmil Constantinescu         na_loc++;
59098a175baeSEmil Constantinescu       }
59108a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
59118a175baeSEmil Constantinescu       if(tolr>0.){
59128a175baeSEmil Constantinescu         sumr  += PetscSqr(err/tolr);
59138a175baeSEmil Constantinescu         nr_loc++;
59148a175baeSEmil Constantinescu       }
59158a175baeSEmil Constantinescu       tol=tola+tolr;
59168a175baeSEmil Constantinescu       if(tol>0.){
59178a175baeSEmil Constantinescu         sum  += PetscSqr(err/tol);
59188a175baeSEmil Constantinescu         n_loc++;
59198a175baeSEmil Constantinescu       }
59208a175baeSEmil Constantinescu     }
59218a175baeSEmil Constantinescu   }
59228a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
59238a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
59248a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
59258a175baeSEmil Constantinescu 
59268a175baeSEmil Constantinescu   err_loc[0] = sum;
59278a175baeSEmil Constantinescu   err_loc[1] = suma;
59288a175baeSEmil Constantinescu   err_loc[2] = sumr;
59298a175baeSEmil Constantinescu   err_loc[3] = (PetscReal)n_loc;
59308a175baeSEmil Constantinescu   err_loc[4] = (PetscReal)na_loc;
59318a175baeSEmil Constantinescu   err_loc[5] = (PetscReal)nr_loc;
59328a175baeSEmil Constantinescu 
5933a88a9d1dSSatish Balay   ierr = MPIU_Allreduce(err_loc,err_glb,6,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
59348a175baeSEmil Constantinescu 
59358a175baeSEmil Constantinescu   gsum   = err_glb[0];
59368a175baeSEmil Constantinescu   gsuma  = err_glb[1];
59378a175baeSEmil Constantinescu   gsumr  = err_glb[2];
59388a175baeSEmil Constantinescu   n_glb  = err_glb[3];
59398a175baeSEmil Constantinescu   na_glb = err_glb[4];
59408a175baeSEmil Constantinescu   nr_glb = err_glb[5];
59418a175baeSEmil Constantinescu 
59428a175baeSEmil Constantinescu   *norm  = 0.;
59438a175baeSEmil Constantinescu   if(n_glb>0. ){*norm  = PetscSqrtReal(gsum  / n_glb );}
59448a175baeSEmil Constantinescu   *norma = 0.;
59458a175baeSEmil Constantinescu   if(na_glb>0.){*norma = PetscSqrtReal(gsuma / na_glb);}
59468a175baeSEmil Constantinescu   *normr = 0.;
59478a175baeSEmil Constantinescu   if(nr_glb>0.){*normr = PetscSqrtReal(gsumr / nr_glb);}
59488a175baeSEmil Constantinescu 
59498a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
59508a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
59518a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
59528a175baeSEmil Constantinescu   PetscFunctionReturn(0);
59538a175baeSEmil Constantinescu }
59548a175baeSEmil Constantinescu 
59558a175baeSEmil Constantinescu /*@
59568a175baeSEmil Constantinescu    TSErrorWeightedENormInfinity - compute a weighted infinity error norm based on supplied absolute and relative tolerances
59578a175baeSEmil Constantinescu    Collective on TS
59588a175baeSEmil Constantinescu 
59598a175baeSEmil Constantinescu    Input Arguments:
59608a175baeSEmil Constantinescu +  ts - time stepping context
59618a175baeSEmil Constantinescu .  E - error vector
59628a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
59638a175baeSEmil Constantinescu -  Y - state vector, previous time step
59648a175baeSEmil Constantinescu 
59658a175baeSEmil Constantinescu    Output Arguments:
59668a175baeSEmil Constantinescu .  norm - weighted norm, a value of 1.0 means that the error matches the tolerances
59678a175baeSEmil Constantinescu .  norma - weighted norm based on the absolute tolerance, a value of 1.0 means that the error matches the tolerances
59688a175baeSEmil Constantinescu .  normr - weighted norm based on the relative tolerance, a value of 1.0 means that the error matches the tolerances
59698a175baeSEmil Constantinescu 
59708a175baeSEmil Constantinescu    Level: developer
59718a175baeSEmil Constantinescu 
59728a175baeSEmil Constantinescu .seealso: TSErrorWeightedENorm(), TSErrorWeightedENorm2()
59738a175baeSEmil Constantinescu @*/
59748a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENormInfinity(TS ts,Vec E,Vec U,Vec Y,PetscReal *norm,PetscReal *norma,PetscReal *normr)
59758a175baeSEmil Constantinescu {
59768a175baeSEmil Constantinescu   PetscErrorCode    ierr;
59778a175baeSEmil Constantinescu   PetscInt          i,n,N,rstart;
59788a175baeSEmil Constantinescu   const PetscScalar *e,*u,*y;
59798a175baeSEmil Constantinescu   PetscReal         err,max,gmax,maxa,gmaxa,maxr,gmaxr;
59808a175baeSEmil Constantinescu   PetscReal         tol,tola,tolr;
59818a175baeSEmil Constantinescu   PetscReal         err_loc[3],err_glb[3];
59828a175baeSEmil Constantinescu 
59838a175baeSEmil Constantinescu   PetscFunctionBegin;
59848a175baeSEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
59858a175baeSEmil Constantinescu   PetscValidHeaderSpecific(E,VEC_CLASSID,2);
59868a175baeSEmil Constantinescu   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
59878a175baeSEmil Constantinescu   PetscValidHeaderSpecific(Y,VEC_CLASSID,4);
59888a175baeSEmil Constantinescu   PetscValidType(E,2);
59898a175baeSEmil Constantinescu   PetscValidType(U,3);
59908a175baeSEmil Constantinescu   PetscValidType(Y,4);
59918a175baeSEmil Constantinescu   PetscCheckSameComm(E,2,U,3);
59928a175baeSEmil Constantinescu   PetscCheckSameComm(U,2,Y,3);
59938a175baeSEmil Constantinescu   PetscValidPointer(norm,5);
59948a175baeSEmil Constantinescu   PetscValidPointer(norma,6);
59958a175baeSEmil Constantinescu   PetscValidPointer(normr,7);
59968a175baeSEmil Constantinescu 
59978a175baeSEmil Constantinescu   ierr = VecGetSize(E,&N);CHKERRQ(ierr);
59988a175baeSEmil Constantinescu   ierr = VecGetLocalSize(E,&n);CHKERRQ(ierr);
59998a175baeSEmil Constantinescu   ierr = VecGetOwnershipRange(E,&rstart,NULL);CHKERRQ(ierr);
60008a175baeSEmil Constantinescu   ierr = VecGetArrayRead(E,&e);CHKERRQ(ierr);
60018a175baeSEmil Constantinescu   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
60028a175baeSEmil Constantinescu   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
60038a175baeSEmil Constantinescu 
60048a175baeSEmil Constantinescu   max=0.;
60058a175baeSEmil Constantinescu   maxa=0.;
60068a175baeSEmil Constantinescu   maxr=0.;
60078a175baeSEmil Constantinescu 
60088a175baeSEmil Constantinescu   if (ts->vatol && ts->vrtol) {     /* vector atol, vector rtol */
60098a175baeSEmil Constantinescu     const PetscScalar *atol,*rtol;
60108a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60118a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60128a175baeSEmil Constantinescu 
60138a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
60148a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
60158a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
60168a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60178a175baeSEmil Constantinescu       tol  = tola+tolr;
60188a175baeSEmil Constantinescu       if(tola>0.){
60198a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
60208a175baeSEmil Constantinescu       }
60218a175baeSEmil Constantinescu       if(tolr>0.){
60228a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
60238a175baeSEmil Constantinescu       }
60248a175baeSEmil Constantinescu       if(tol>0.){
60258a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
60268a175baeSEmil Constantinescu       }
60278a175baeSEmil Constantinescu     }
60288a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60298a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60308a175baeSEmil Constantinescu   } else if (ts->vatol) {       /* vector atol, scalar rtol */
60318a175baeSEmil Constantinescu     const PetscScalar *atol;
60328a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60338a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
60348a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
60358a175baeSEmil Constantinescu       tola = PetscRealPart(atol[i]);
60368a175baeSEmil Constantinescu       tolr = ts->rtol  * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60378a175baeSEmil Constantinescu       tol  = tola+tolr;
60388a175baeSEmil Constantinescu       if(tola>0.){
60398a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
60408a175baeSEmil Constantinescu       }
60418a175baeSEmil Constantinescu       if(tolr>0.){
60428a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
60438a175baeSEmil Constantinescu       }
60448a175baeSEmil Constantinescu       if(tol>0.){
60458a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
60468a175baeSEmil Constantinescu       }
60478a175baeSEmil Constantinescu     }
60488a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
60498a175baeSEmil Constantinescu   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
60508a175baeSEmil Constantinescu     const PetscScalar *rtol;
60518a175baeSEmil Constantinescu     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60528a175baeSEmil Constantinescu 
60538a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
60548a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
60558a175baeSEmil Constantinescu       tola = ts->atol;
60568a175baeSEmil Constantinescu       tolr = PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60578a175baeSEmil Constantinescu       tol  = tola+tolr;
60588a175baeSEmil Constantinescu       if(tola>0.){
60598a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
60608a175baeSEmil Constantinescu       }
60618a175baeSEmil Constantinescu       if(tolr>0.){
60628a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
60638a175baeSEmil Constantinescu       }
60648a175baeSEmil Constantinescu       if(tol>0.){
60658a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
60668a175baeSEmil Constantinescu       }
60678a175baeSEmil Constantinescu     }
60688a175baeSEmil Constantinescu     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
60698a175baeSEmil Constantinescu   } else {                      /* scalar atol, scalar rtol */
60708a175baeSEmil Constantinescu 
60718a175baeSEmil Constantinescu     for (i=0; i<n; i++) {
60728a175baeSEmil Constantinescu       err = PetscAbsScalar(e[i]);
60738a175baeSEmil Constantinescu       tola = ts->atol;
60748a175baeSEmil Constantinescu       tolr = ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
60758a175baeSEmil Constantinescu       tol  = tola+tolr;
60768a175baeSEmil Constantinescu       if(tola>0.){
60778a175baeSEmil Constantinescu         maxa = PetscMax(maxa,err / tola);
60788a175baeSEmil Constantinescu       }
60798a175baeSEmil Constantinescu       if(tolr>0.){
60808a175baeSEmil Constantinescu         maxr = PetscMax(maxr,err / tolr);
60818a175baeSEmil Constantinescu       }
60828a175baeSEmil Constantinescu       if(tol>0.){
60838a175baeSEmil Constantinescu         max = PetscMax(max,err / tol);
60848a175baeSEmil Constantinescu       }
60858a175baeSEmil Constantinescu     }
60868a175baeSEmil Constantinescu   }
60878a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(E,&e);CHKERRQ(ierr);
60888a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
60898a175baeSEmil Constantinescu   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
60908a175baeSEmil Constantinescu   err_loc[0] = max;
60918a175baeSEmil Constantinescu   err_loc[1] = maxa;
60928a175baeSEmil Constantinescu   err_loc[2] = maxr;
6093a88a9d1dSSatish Balay   ierr  = MPIU_Allreduce(err_loc,err_glb,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
60948a175baeSEmil Constantinescu   gmax   = err_glb[0];
60958a175baeSEmil Constantinescu   gmaxa  = err_glb[1];
60968a175baeSEmil Constantinescu   gmaxr  = err_glb[2];
60978a175baeSEmil Constantinescu 
60988a175baeSEmil Constantinescu   *norm = gmax;
60998a175baeSEmil Constantinescu   *norma = gmaxa;
61008a175baeSEmil Constantinescu   *normr = gmaxr;
61018a175baeSEmil Constantinescu   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
61028a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*norma)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norma");
61038a175baeSEmil Constantinescu     if (PetscIsInfOrNanScalar(*normr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in normr");
61048a175baeSEmil Constantinescu   PetscFunctionReturn(0);
61058a175baeSEmil Constantinescu }
61068a175baeSEmil Constantinescu 
61078a175baeSEmil Constantinescu /*@
61088a175baeSEmil Constantinescu    TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances
61098a175baeSEmil Constantinescu 
61108a175baeSEmil Constantinescu    Collective on TS
61118a175baeSEmil Constantinescu 
61128a175baeSEmil Constantinescu    Input Arguments:
61138a175baeSEmil Constantinescu +  ts - time stepping context
61148a175baeSEmil Constantinescu .  E - error vector
61158a175baeSEmil Constantinescu .  U - state vector, usually ts->vec_sol
61168a175baeSEmil Constantinescu .  Y - state vector, previous time step
61178a175baeSEmil Constantinescu -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
61188a175baeSEmil Constantinescu 
61198a175baeSEmil Constantinescu    Output Arguments:
61208a175baeSEmil Constantinescu .  norm  - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances
61218a175baeSEmil Constantinescu .  norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user
61228a175baeSEmil Constantinescu .  normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user
61238a175baeSEmil Constantinescu 
61248a175baeSEmil Constantinescu    Options Database Keys:
61258a175baeSEmil Constantinescu .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
61268a175baeSEmil Constantinescu 
61278a175baeSEmil Constantinescu    Level: developer
61288a175baeSEmil Constantinescu 
61298a175baeSEmil Constantinescu .seealso: TSErrorWeightedENormInfinity(), TSErrorWeightedENorm2(), TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
61308a175baeSEmil Constantinescu @*/
61318a175baeSEmil Constantinescu PetscErrorCode TSErrorWeightedENorm(TS ts,Vec E,Vec U,Vec Y,NormType wnormtype,PetscReal *norm,PetscReal *norma,PetscReal *normr)
61328a175baeSEmil Constantinescu {
61338a175baeSEmil Constantinescu   PetscErrorCode ierr;
61348a175baeSEmil Constantinescu 
61358a175baeSEmil Constantinescu   PetscFunctionBegin;
61368a175baeSEmil Constantinescu   if (wnormtype == NORM_2) {
61378a175baeSEmil Constantinescu     ierr = TSErrorWeightedENorm2(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
61388a175baeSEmil Constantinescu   } else if(wnormtype == NORM_INFINITY) {
61398a175baeSEmil Constantinescu     ierr = TSErrorWeightedENormInfinity(ts,E,U,Y,norm,norma,normr);CHKERRQ(ierr);
61408a175baeSEmil Constantinescu   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
61418a175baeSEmil Constantinescu   PetscFunctionReturn(0);
61428a175baeSEmil Constantinescu }
61438a175baeSEmil Constantinescu 
61448a175baeSEmil Constantinescu 
61458d59e960SJed Brown /*@
61468d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
61478d59e960SJed Brown 
61488d59e960SJed Brown    Logically Collective on TS
61498d59e960SJed Brown 
61508d59e960SJed Brown    Input Arguments:
61518d59e960SJed Brown +  ts - time stepping context
61528d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
61538d59e960SJed Brown 
61548d59e960SJed Brown    Note:
61558d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
61568d59e960SJed Brown 
61578d59e960SJed Brown    Level: intermediate
61588d59e960SJed Brown 
61598d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
61608d59e960SJed Brown @*/
61618d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
61628d59e960SJed Brown {
61638d59e960SJed Brown   PetscFunctionBegin;
61648d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
61658d59e960SJed Brown   ts->cfltime_local = cfltime;
61668d59e960SJed Brown   ts->cfltime       = -1.;
61678d59e960SJed Brown   PetscFunctionReturn(0);
61688d59e960SJed Brown }
61698d59e960SJed Brown 
61708d59e960SJed Brown /*@
61718d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
61728d59e960SJed Brown 
61738d59e960SJed Brown    Collective on TS
61748d59e960SJed Brown 
61758d59e960SJed Brown    Input Arguments:
61768d59e960SJed Brown .  ts - time stepping context
61778d59e960SJed Brown 
61788d59e960SJed Brown    Output Arguments:
61798d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
61808d59e960SJed Brown 
61818d59e960SJed Brown    Level: advanced
61828d59e960SJed Brown 
61838d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
61848d59e960SJed Brown @*/
61858d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
61868d59e960SJed Brown {
61878d59e960SJed Brown   PetscErrorCode ierr;
61888d59e960SJed Brown 
61898d59e960SJed Brown   PetscFunctionBegin;
61908d59e960SJed Brown   if (ts->cfltime < 0) {
6191b2566f29SBarry Smith     ierr = MPIU_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
61928d59e960SJed Brown   }
61938d59e960SJed Brown   *cfltime = ts->cfltime;
61948d59e960SJed Brown   PetscFunctionReturn(0);
61958d59e960SJed Brown }
61968d59e960SJed Brown 
6197d6ebe24aSShri Abhyankar /*@
6198d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
6199d6ebe24aSShri Abhyankar 
6200d6ebe24aSShri Abhyankar    Input Parameters:
6201d6ebe24aSShri Abhyankar .  ts   - the TS context.
6202d6ebe24aSShri Abhyankar .  xl   - lower bound.
6203d6ebe24aSShri Abhyankar .  xu   - upper bound.
6204d6ebe24aSShri Abhyankar 
6205d6ebe24aSShri Abhyankar    Notes:
6206d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
6207e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
6208d6ebe24aSShri Abhyankar 
62092bd2b0e6SSatish Balay    Level: advanced
62102bd2b0e6SSatish Balay 
6211d6ebe24aSShri Abhyankar @*/
6212d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
6213d6ebe24aSShri Abhyankar {
6214d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
6215d6ebe24aSShri Abhyankar   SNES           snes;
6216d6ebe24aSShri Abhyankar 
6217d6ebe24aSShri Abhyankar   PetscFunctionBegin;
6218d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
6219d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
6220d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
6221d6ebe24aSShri Abhyankar }
6222d6ebe24aSShri Abhyankar 
6223325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
6224c6db04a5SJed Brown #include <mex.h>
6225325fc9f4SBarry Smith 
6226325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
6227325fc9f4SBarry Smith 
6228325fc9f4SBarry Smith /*
6229325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
6230325fc9f4SBarry Smith                          TSSetFunctionMatlab().
6231325fc9f4SBarry Smith 
6232325fc9f4SBarry Smith    Collective on TS
6233325fc9f4SBarry Smith 
6234325fc9f4SBarry Smith    Input Parameters:
6235325fc9f4SBarry Smith +  snes - the TS context
62360910c330SBarry Smith -  u - input vector
6237325fc9f4SBarry Smith 
6238325fc9f4SBarry Smith    Output Parameter:
6239325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
6240325fc9f4SBarry Smith 
6241325fc9f4SBarry Smith    Notes:
6242325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
6243325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
6244325fc9f4SBarry Smith    themselves.
6245325fc9f4SBarry Smith 
6246325fc9f4SBarry Smith    Level: developer
6247325fc9f4SBarry Smith 
6248325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6249325fc9f4SBarry Smith 
6250325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6251325fc9f4SBarry Smith */
62520910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
6253325fc9f4SBarry Smith {
6254325fc9f4SBarry Smith   PetscErrorCode  ierr;
6255325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6256325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
6257325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
6258325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
6259325fc9f4SBarry Smith 
6260325fc9f4SBarry Smith   PetscFunctionBegin;
6261325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
62620910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
62630910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
6264325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
62650910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
6266325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
6267325fc9f4SBarry Smith 
6268325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
62690910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
62700910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
62710910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
6272bbd56ea5SKarl Rupp 
6273325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6274325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
6275325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6276325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6277325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
6278325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
6279325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
6280325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
6281325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6282325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6283325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6284325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6285325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6286325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6287325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6288325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6289325fc9f4SBarry Smith   PetscFunctionReturn(0);
6290325fc9f4SBarry Smith }
6291325fc9f4SBarry Smith 
6292325fc9f4SBarry Smith /*
6293325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
6294325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
6295e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
6296325fc9f4SBarry Smith 
6297325fc9f4SBarry Smith    Logically Collective on TS
6298325fc9f4SBarry Smith 
6299325fc9f4SBarry Smith    Input Parameters:
6300325fc9f4SBarry Smith +  ts - the TS context
6301325fc9f4SBarry Smith -  func - function evaluation routine
6302325fc9f4SBarry Smith 
6303325fc9f4SBarry Smith    Calling sequence of func:
63040910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
6305325fc9f4SBarry Smith 
6306325fc9f4SBarry Smith    Level: beginner
6307325fc9f4SBarry Smith 
6308325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6309325fc9f4SBarry Smith 
6310325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6311325fc9f4SBarry Smith */
6312cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
6313325fc9f4SBarry Smith {
6314325fc9f4SBarry Smith   PetscErrorCode  ierr;
6315325fc9f4SBarry Smith   TSMatlabContext *sctx;
6316325fc9f4SBarry Smith 
6317325fc9f4SBarry Smith   PetscFunctionBegin;
6318325fc9f4SBarry Smith   /* currently sctx is memory bleed */
631995dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6320325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6321325fc9f4SBarry Smith   /*
6322325fc9f4SBarry Smith      This should work, but it doesn't
6323325fc9f4SBarry Smith   sctx->ctx = ctx;
6324325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6325325fc9f4SBarry Smith   */
6326325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6327bbd56ea5SKarl Rupp 
63280298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
6329325fc9f4SBarry Smith   PetscFunctionReturn(0);
6330325fc9f4SBarry Smith }
6331325fc9f4SBarry Smith 
6332325fc9f4SBarry Smith /*
6333325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
6334325fc9f4SBarry Smith                          TSSetJacobianMatlab().
6335325fc9f4SBarry Smith 
6336325fc9f4SBarry Smith    Collective on TS
6337325fc9f4SBarry Smith 
6338325fc9f4SBarry Smith    Input Parameters:
6339cdcf91faSSean Farley +  ts - the TS context
63400910c330SBarry Smith .  u - input vector
6341325fc9f4SBarry Smith .  A, B - the matrices
6342325fc9f4SBarry Smith -  ctx - user context
6343325fc9f4SBarry Smith 
6344325fc9f4SBarry Smith    Level: developer
6345325fc9f4SBarry Smith 
6346325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
6347325fc9f4SBarry Smith 
6348325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6349325fc9f4SBarry Smith @*/
6350f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
6351325fc9f4SBarry Smith {
6352325fc9f4SBarry Smith   PetscErrorCode  ierr;
6353325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6354325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
6355325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
6356325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
6357325fc9f4SBarry Smith 
6358325fc9f4SBarry Smith   PetscFunctionBegin;
6359cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
63600910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
6361325fc9f4SBarry Smith 
63620910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
6363325fc9f4SBarry Smith 
6364cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
63650910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
63660910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
63670910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
63680910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
6369bbd56ea5SKarl Rupp 
6370325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6371325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
6372325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
6373325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
6374325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
6375325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
6376325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
6377325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
6378325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
6379325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
6380325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6381325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
6382325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
6383325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
6384325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
6385325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
6386325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
6387325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
6388325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
6389325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
6390325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
6391325fc9f4SBarry Smith   PetscFunctionReturn(0);
6392325fc9f4SBarry Smith }
6393325fc9f4SBarry Smith 
6394325fc9f4SBarry Smith /*
6395325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
6396e3c5b3baSBarry 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
6397325fc9f4SBarry Smith 
6398325fc9f4SBarry Smith    Logically Collective on TS
6399325fc9f4SBarry Smith 
6400325fc9f4SBarry Smith    Input Parameters:
6401cdcf91faSSean Farley +  ts - the TS context
6402325fc9f4SBarry Smith .  A,B - Jacobian matrices
6403325fc9f4SBarry Smith .  func - function evaluation routine
6404325fc9f4SBarry Smith -  ctx - user context
6405325fc9f4SBarry Smith 
6406325fc9f4SBarry Smith    Calling sequence of func:
64070910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
6408325fc9f4SBarry Smith 
6409325fc9f4SBarry Smith    Level: developer
6410325fc9f4SBarry Smith 
6411325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
6412325fc9f4SBarry Smith 
6413325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6414325fc9f4SBarry Smith */
6415cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
6416325fc9f4SBarry Smith {
6417325fc9f4SBarry Smith   PetscErrorCode  ierr;
6418325fc9f4SBarry Smith   TSMatlabContext *sctx;
6419325fc9f4SBarry Smith 
6420325fc9f4SBarry Smith   PetscFunctionBegin;
6421325fc9f4SBarry Smith   /* currently sctx is memory bleed */
642295dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6423325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6424325fc9f4SBarry Smith   /*
6425325fc9f4SBarry Smith      This should work, but it doesn't
6426325fc9f4SBarry Smith   sctx->ctx = ctx;
6427325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6428325fc9f4SBarry Smith   */
6429325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6430bbd56ea5SKarl Rupp 
6431cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
6432325fc9f4SBarry Smith   PetscFunctionReturn(0);
6433325fc9f4SBarry Smith }
6434325fc9f4SBarry Smith 
6435b5b1a830SBarry Smith /*
6436b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
6437b5b1a830SBarry Smith 
6438b5b1a830SBarry Smith    Collective on TS
6439b5b1a830SBarry Smith 
6440b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
6441b5b1a830SBarry Smith @*/
64420910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
6443b5b1a830SBarry Smith {
6444b5b1a830SBarry Smith   PetscErrorCode  ierr;
6445b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
6446a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
6447b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
6448b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
6449b5b1a830SBarry Smith 
6450b5b1a830SBarry Smith   PetscFunctionBegin;
6451cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
64520910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
6453b5b1a830SBarry Smith 
6454cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
64550910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
6456bbd56ea5SKarl Rupp 
6457b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
6458b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
6459b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
6460b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
6461b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
6462b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
6463b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
6464b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
6465b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
6466b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
6467b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
6468b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
6469b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
6470b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
6471b5b1a830SBarry Smith   PetscFunctionReturn(0);
6472b5b1a830SBarry Smith }
6473b5b1a830SBarry Smith 
6474b5b1a830SBarry Smith /*
6475b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
6476b5b1a830SBarry Smith 
6477b5b1a830SBarry Smith    Level: developer
6478b5b1a830SBarry Smith 
6479b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
6480b5b1a830SBarry Smith 
6481b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
6482b5b1a830SBarry Smith */
6483cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
6484b5b1a830SBarry Smith {
6485b5b1a830SBarry Smith   PetscErrorCode  ierr;
6486b5b1a830SBarry Smith   TSMatlabContext *sctx;
6487b5b1a830SBarry Smith 
6488b5b1a830SBarry Smith   PetscFunctionBegin;
6489b5b1a830SBarry Smith   /* currently sctx is memory bleed */
649095dccacaSBarry Smith   ierr = PetscNew(&sctx);CHKERRQ(ierr);
6491b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
6492b5b1a830SBarry Smith   /*
6493b5b1a830SBarry Smith      This should work, but it doesn't
6494b5b1a830SBarry Smith   sctx->ctx = ctx;
6495b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
6496b5b1a830SBarry Smith   */
6497b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
6498bbd56ea5SKarl Rupp 
64990298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
6500b5b1a830SBarry Smith   PetscFunctionReturn(0);
6501b5b1a830SBarry Smith }
6502325fc9f4SBarry Smith #endif
6503b3603a34SBarry Smith 
6504b3603a34SBarry Smith /*@C
65054f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
6506b3603a34SBarry Smith        in a time based line graph
6507b3603a34SBarry Smith 
6508b3603a34SBarry Smith    Collective on TS
6509b3603a34SBarry Smith 
6510b3603a34SBarry Smith    Input Parameters:
6511b3603a34SBarry Smith +  ts - the TS context
6512b3603a34SBarry Smith .  step - current time-step
6513b3603a34SBarry Smith .  ptime - current time
65147db568b7SBarry Smith .  u - current solution
65157db568b7SBarry Smith -  dctx - the TSMonitorLGCtx object that contains all the options for the monitoring, this is created with TSMonitorLGCtxCreate()
6516b3603a34SBarry Smith 
6517b3d3934dSBarry Smith    Options Database:
65189ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
6519b3d3934dSBarry Smith 
6520b3603a34SBarry Smith    Level: intermediate
6521b3603a34SBarry Smith 
652295452b02SPatrick Sanan    Notes:
652395452b02SPatrick Sanan     Each process in a parallel run displays its component solutions in a separate window
6524b3603a34SBarry Smith 
6525b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
6526b3603a34SBarry Smith 
65277db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGCtxCreate(), TSMonitorLGCtxSetVariableNames(), TSMonitorLGCtxGetVariableNames(),
65287db568b7SBarry Smith            TSMonitorLGSetVariableNames(), TSMonitorLGGetVariableNames(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetDisplayVariables(),
65297db568b7SBarry Smith            TSMonitorLGCtxSetTransform(), TSMonitorLGSetTransform(), TSMonitorLGError(), TSMonitorLGSNESIterations(), TSMonitorLGKSPIterations(),
65307db568b7SBarry Smith            TSMonitorEnvelopeCtxCreate(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxDestroy(), TSMonitorEnvelop()
6531b3603a34SBarry Smith @*/
65327db568b7SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
6533b3603a34SBarry Smith {
6534b3603a34SBarry Smith   PetscErrorCode    ierr;
65357db568b7SBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dctx;
6536b3603a34SBarry Smith   const PetscScalar *yy;
653780666b62SBarry Smith   Vec               v;
6538b3603a34SBarry Smith 
6539b3603a34SBarry Smith   PetscFunctionBegin;
654063e21af5SBarry Smith   if (step < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
654158ff32f7SBarry Smith   if (!step) {
6542a9f9c1f6SBarry Smith     PetscDrawAxis axis;
65436934998bSLisandro Dalcin     PetscInt      dim;
6544a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6545a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
6546bab0a581SBarry Smith     if (!ctx->names) {
6547bab0a581SBarry Smith       PetscBool flg;
6548bab0a581SBarry Smith       /* user provides names of variables to plot but no names has been set so assume names are integer values */
6549bab0a581SBarry Smith       ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",&flg);CHKERRQ(ierr);
6550bab0a581SBarry Smith       if (flg) {
6551bab0a581SBarry Smith         PetscInt i,n;
6552bab0a581SBarry Smith         char     **names;
6553bab0a581SBarry Smith         ierr = VecGetSize(u,&n);CHKERRQ(ierr);
6554bab0a581SBarry Smith         ierr = PetscMalloc1(n+1,&names);CHKERRQ(ierr);
6555bab0a581SBarry Smith         for (i=0; i<n; i++) {
6556bab0a581SBarry Smith           ierr = PetscMalloc1(5,&names[i]);CHKERRQ(ierr);
6557bab0a581SBarry Smith           ierr = PetscSNPrintf(names[i],5,"%D",i);CHKERRQ(ierr);
6558bab0a581SBarry Smith         }
6559bab0a581SBarry Smith         names[n] = NULL;
6560bab0a581SBarry Smith         ctx->names = names;
6561bab0a581SBarry Smith       }
6562bab0a581SBarry Smith     }
6563387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
6564387f4636SBarry Smith       char      **displaynames;
6565387f4636SBarry Smith       PetscBool flg;
6566387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
656795dccacaSBarry Smith       ierr = PetscMalloc1(dim+1,&displaynames);CHKERRQ(ierr);
6568387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
6569c5929fdfSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
6570387f4636SBarry Smith       if (flg) {
6571a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
6572387f4636SBarry Smith       }
6573387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
6574387f4636SBarry Smith     }
6575387f4636SBarry Smith     if (ctx->displaynames) {
6576387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
6577387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
6578387f4636SBarry Smith     } else if (ctx->names) {
65790910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
65800b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6581387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
6582b0bc92c6SBarry Smith     } else {
6583b0bc92c6SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6584b0bc92c6SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6585387f4636SBarry Smith     }
65860b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
658758ff32f7SBarry Smith   }
65886934998bSLisandro Dalcin 
65896934998bSLisandro Dalcin   if (!ctx->transform) v = u;
65906934998bSLisandro Dalcin   else {ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);}
659180666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
65926934998bSLisandro Dalcin   if (ctx->displaynames) {
65936934998bSLisandro Dalcin     PetscInt i;
65946934998bSLisandro Dalcin     for (i=0; i<ctx->ndisplayvariables; i++)
65956934998bSLisandro Dalcin       ctx->displayvalues[i] = PetscRealPart(yy[ctx->displayvariables[i]]);
65966934998bSLisandro Dalcin     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
65976934998bSLisandro Dalcin   } else {
6598e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6599e3efe391SJed Brown     PetscInt  i,n;
66006934998bSLisandro Dalcin     PetscReal *yreal;
660180666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
6602785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6603e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
66040b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6605e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6606e3efe391SJed Brown #else
66070b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6608e3efe391SJed Brown #endif
660980666b62SBarry Smith   }
66106934998bSLisandro Dalcin   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
66116934998bSLisandro Dalcin   if (ctx->transform) {ierr = VecDestroy(&v);CHKERRQ(ierr);}
66126934998bSLisandro Dalcin 
6613b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
66140b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
66156934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
66163923b477SBarry Smith   }
6617b3603a34SBarry Smith   PetscFunctionReturn(0);
6618b3603a34SBarry Smith }
6619b3603a34SBarry Smith 
6620b037adc7SBarry Smith /*@C
662131152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6622b037adc7SBarry Smith 
6623b037adc7SBarry Smith    Collective on TS
6624b037adc7SBarry Smith 
6625b037adc7SBarry Smith    Input Parameters:
6626b037adc7SBarry Smith +  ts - the TS context
6627b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
6628b037adc7SBarry Smith 
6629b037adc7SBarry Smith    Level: intermediate
6630b037adc7SBarry Smith 
663195452b02SPatrick Sanan    Notes:
663295452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66337db568b7SBarry Smith 
6634b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
6635b037adc7SBarry Smith 
6636a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
6637b037adc7SBarry Smith @*/
663831152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
6639b037adc7SBarry Smith {
6640b037adc7SBarry Smith   PetscErrorCode    ierr;
6641b037adc7SBarry Smith   PetscInt          i;
6642b037adc7SBarry Smith 
6643b037adc7SBarry Smith   PetscFunctionBegin;
6644b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6645b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
66465537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
6647b3d3934dSBarry Smith       break;
6648b3d3934dSBarry Smith     }
6649b3d3934dSBarry Smith   }
6650b3d3934dSBarry Smith   PetscFunctionReturn(0);
6651b3d3934dSBarry Smith }
6652b3d3934dSBarry Smith 
6653e673d494SBarry Smith /*@C
6654e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
6655e673d494SBarry Smith 
6656e673d494SBarry Smith    Collective on TS
6657e673d494SBarry Smith 
6658e673d494SBarry Smith    Input Parameters:
6659e673d494SBarry Smith +  ts - the TS context
6660e673d494SBarry Smith -  names - the names of the components, final string must be NULL
6661e673d494SBarry Smith 
6662e673d494SBarry Smith    Level: intermediate
6663e673d494SBarry Smith 
6664e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6665e673d494SBarry Smith 
6666a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
6667e673d494SBarry Smith @*/
6668e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
6669e673d494SBarry Smith {
6670e673d494SBarry Smith   PetscErrorCode    ierr;
6671e673d494SBarry Smith 
6672e673d494SBarry Smith   PetscFunctionBegin;
6673e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
6674e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
6675e673d494SBarry Smith   PetscFunctionReturn(0);
6676e673d494SBarry Smith }
6677e673d494SBarry Smith 
6678b3d3934dSBarry Smith /*@C
6679b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
6680b3d3934dSBarry Smith 
6681b3d3934dSBarry Smith    Collective on TS
6682b3d3934dSBarry Smith 
6683b3d3934dSBarry Smith    Input Parameter:
6684b3d3934dSBarry Smith .  ts - the TS context
6685b3d3934dSBarry Smith 
6686b3d3934dSBarry Smith    Output Parameter:
6687b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
6688b3d3934dSBarry Smith 
6689b3d3934dSBarry Smith    Level: intermediate
6690b3d3934dSBarry Smith 
669195452b02SPatrick Sanan    Notes:
669295452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
66937db568b7SBarry Smith 
6694b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6695b3d3934dSBarry Smith 
6696b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6697b3d3934dSBarry Smith @*/
6698b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
6699b3d3934dSBarry Smith {
6700b3d3934dSBarry Smith   PetscInt       i;
6701b3d3934dSBarry Smith 
6702b3d3934dSBarry Smith   PetscFunctionBegin;
6703b3d3934dSBarry Smith   *names = NULL;
6704b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6705b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67065537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
6707b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
6708b3d3934dSBarry Smith       break;
6709387f4636SBarry Smith     }
6710387f4636SBarry Smith   }
6711387f4636SBarry Smith   PetscFunctionReturn(0);
6712387f4636SBarry Smith }
6713387f4636SBarry Smith 
6714a66092f1SBarry Smith /*@C
6715a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
6716a66092f1SBarry Smith 
6717a66092f1SBarry Smith    Collective on TS
6718a66092f1SBarry Smith 
6719a66092f1SBarry Smith    Input Parameters:
6720a66092f1SBarry Smith +  ctx - the TSMonitorLG context
6721a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
6722a66092f1SBarry Smith 
6723a66092f1SBarry Smith    Level: intermediate
6724a66092f1SBarry Smith 
6725a66092f1SBarry Smith .keywords: TS,  vector, monitor, view
6726a66092f1SBarry Smith 
6727a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6728a66092f1SBarry Smith @*/
6729a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
6730a66092f1SBarry Smith {
6731a66092f1SBarry Smith   PetscInt          j = 0,k;
6732a66092f1SBarry Smith   PetscErrorCode    ierr;
6733a66092f1SBarry Smith 
6734a66092f1SBarry Smith   PetscFunctionBegin;
6735a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
6736a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
6737a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
6738a66092f1SBarry Smith   while (displaynames[j]) j++;
6739a66092f1SBarry Smith   ctx->ndisplayvariables = j;
6740a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
6741a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
6742a66092f1SBarry Smith   j = 0;
6743a66092f1SBarry Smith   while (displaynames[j]) {
6744a66092f1SBarry Smith     k = 0;
6745a66092f1SBarry Smith     while (ctx->names[k]) {
6746a66092f1SBarry Smith       PetscBool flg;
6747a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
6748a66092f1SBarry Smith       if (flg) {
6749a66092f1SBarry Smith         ctx->displayvariables[j] = k;
6750a66092f1SBarry Smith         break;
6751a66092f1SBarry Smith       }
6752a66092f1SBarry Smith       k++;
6753a66092f1SBarry Smith     }
6754a66092f1SBarry Smith     j++;
6755a66092f1SBarry Smith   }
6756a66092f1SBarry Smith   PetscFunctionReturn(0);
6757a66092f1SBarry Smith }
6758a66092f1SBarry Smith 
6759387f4636SBarry Smith /*@C
6760387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
6761387f4636SBarry Smith 
6762387f4636SBarry Smith    Collective on TS
6763387f4636SBarry Smith 
6764387f4636SBarry Smith    Input Parameters:
6765387f4636SBarry Smith +  ts - the TS context
6766387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
6767387f4636SBarry Smith 
676895452b02SPatrick Sanan    Notes:
676995452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
67707db568b7SBarry Smith 
6771387f4636SBarry Smith    Level: intermediate
6772387f4636SBarry Smith 
6773387f4636SBarry Smith .keywords: TS,  vector, monitor, view
6774387f4636SBarry Smith 
6775387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
6776387f4636SBarry Smith @*/
6777387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
6778387f4636SBarry Smith {
6779a66092f1SBarry Smith   PetscInt          i;
6780387f4636SBarry Smith   PetscErrorCode    ierr;
6781387f4636SBarry Smith 
6782387f4636SBarry Smith   PetscFunctionBegin;
6783387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6784387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
67855537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
6786b3d3934dSBarry Smith       break;
6787b037adc7SBarry Smith     }
6788b037adc7SBarry Smith   }
6789b037adc7SBarry Smith   PetscFunctionReturn(0);
6790b037adc7SBarry Smith }
6791b037adc7SBarry Smith 
679280666b62SBarry Smith /*@C
679380666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
679480666b62SBarry Smith 
679580666b62SBarry Smith    Collective on TS
679680666b62SBarry Smith 
679780666b62SBarry Smith    Input Parameters:
679880666b62SBarry Smith +  ts - the TS context
679980666b62SBarry Smith .  transform - the transform function
68007684fa3eSBarry Smith .  destroy - function to destroy the optional context
680180666b62SBarry Smith -  ctx - optional context used by transform function
680280666b62SBarry Smith 
680395452b02SPatrick Sanan    Notes:
680495452b02SPatrick Sanan     If the TS object does not have a TSMonitorLGCtx associated with it then this function is ignored
68057db568b7SBarry Smith 
680680666b62SBarry Smith    Level: intermediate
680780666b62SBarry Smith 
680880666b62SBarry Smith .keywords: TS,  vector, monitor, view
680980666b62SBarry Smith 
6810a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
681180666b62SBarry Smith @*/
68127684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
681380666b62SBarry Smith {
681480666b62SBarry Smith   PetscInt          i;
6815a66092f1SBarry Smith   PetscErrorCode    ierr;
681680666b62SBarry Smith 
681780666b62SBarry Smith   PetscFunctionBegin;
681880666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
681980666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
68205537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
682180666b62SBarry Smith     }
682280666b62SBarry Smith   }
682380666b62SBarry Smith   PetscFunctionReturn(0);
682480666b62SBarry Smith }
682580666b62SBarry Smith 
6826e673d494SBarry Smith /*@C
6827e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6828e673d494SBarry Smith 
6829e673d494SBarry Smith    Collective on TSLGCtx
6830e673d494SBarry Smith 
6831e673d494SBarry Smith    Input Parameters:
6832e673d494SBarry Smith +  ts - the TS context
6833e673d494SBarry Smith .  transform - the transform function
68347684fa3eSBarry Smith .  destroy - function to destroy the optional context
6835e673d494SBarry Smith -  ctx - optional context used by transform function
6836e673d494SBarry Smith 
6837e673d494SBarry Smith    Level: intermediate
6838e673d494SBarry Smith 
6839e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6840e673d494SBarry Smith 
6841a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6842e673d494SBarry Smith @*/
68437684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6844e673d494SBarry Smith {
6845e673d494SBarry Smith   PetscFunctionBegin;
6846e673d494SBarry Smith   ctx->transform    = transform;
68477684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6848e673d494SBarry Smith   ctx->transformctx = tctx;
6849e673d494SBarry Smith   PetscFunctionReturn(0);
6850e673d494SBarry Smith }
6851e673d494SBarry Smith 
6852ef20d060SBarry Smith /*@C
68538b668821SLisandro Dalcin    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the error
6854ef20d060SBarry Smith        in a time based line graph
6855ef20d060SBarry Smith 
6856ef20d060SBarry Smith    Collective on TS
6857ef20d060SBarry Smith 
6858ef20d060SBarry Smith    Input Parameters:
6859ef20d060SBarry Smith +  ts - the TS context
6860ef20d060SBarry Smith .  step - current time-step
6861ef20d060SBarry Smith .  ptime - current time
68627db568b7SBarry Smith .  u - current solution
68637db568b7SBarry Smith -  dctx - TSMonitorLGCtx object created with TSMonitorLGCtxCreate()
6864ef20d060SBarry Smith 
6865ef20d060SBarry Smith    Level: intermediate
6866ef20d060SBarry Smith 
686795452b02SPatrick Sanan    Notes:
686895452b02SPatrick Sanan     Each process in a parallel run displays its component errors in a separate window
6869abd5a294SJed Brown 
6870abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6871abd5a294SJed Brown 
6872abd5a294SJed Brown    Options Database Keys:
68734f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6874ef20d060SBarry Smith 
6875ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
6876ef20d060SBarry Smith 
6877abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6878ef20d060SBarry Smith @*/
68790910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6880ef20d060SBarry Smith {
6881ef20d060SBarry Smith   PetscErrorCode    ierr;
68820b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6883ef20d060SBarry Smith   const PetscScalar *yy;
6884ef20d060SBarry Smith   Vec               y;
6885ef20d060SBarry Smith 
6886ef20d060SBarry Smith   PetscFunctionBegin;
6887a9f9c1f6SBarry Smith   if (!step) {
6888a9f9c1f6SBarry Smith     PetscDrawAxis axis;
68896934998bSLisandro Dalcin     PetscInt      dim;
6890a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
68918b668821SLisandro Dalcin     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Error");CHKERRQ(ierr);
68920910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6893a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6894a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6895a9f9c1f6SBarry Smith   }
68960910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6897ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
68980910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6899ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6900e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6901e3efe391SJed Brown   {
6902e3efe391SJed Brown     PetscReal *yreal;
6903e3efe391SJed Brown     PetscInt  i,n;
6904e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6905785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6906e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
69070b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6908e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6909e3efe391SJed Brown   }
6910e3efe391SJed Brown #else
69110b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6912e3efe391SJed Brown #endif
6913ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6914ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6915b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
69160b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
69176934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
69183923b477SBarry Smith   }
6919ef20d060SBarry Smith   PetscFunctionReturn(0);
6920ef20d060SBarry Smith }
6921ef20d060SBarry Smith 
69227cf37e64SBarry Smith /*@C
69237cf37e64SBarry Smith    TSMonitorError - Monitors progress of the TS solvers by printing the 2 norm of the error at each timestep
69247cf37e64SBarry Smith 
69257cf37e64SBarry Smith    Collective on TS
69267cf37e64SBarry Smith 
69277cf37e64SBarry Smith    Input Parameters:
69287cf37e64SBarry Smith +  ts - the TS context
69297cf37e64SBarry Smith .  step - current time-step
69307cf37e64SBarry Smith .  ptime - current time
69317cf37e64SBarry Smith .  u - current solution
69327cf37e64SBarry Smith -  dctx - unused context
69337cf37e64SBarry Smith 
69347cf37e64SBarry Smith    Level: intermediate
69357cf37e64SBarry Smith 
69367cf37e64SBarry Smith    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
69377cf37e64SBarry Smith 
69387cf37e64SBarry Smith    Options Database Keys:
69397cf37e64SBarry Smith .  -ts_monitor_error - create a graphical monitor of error history
69407cf37e64SBarry Smith 
69417cf37e64SBarry Smith .keywords: TS,  vector, monitor, view
69427cf37e64SBarry Smith 
69437cf37e64SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
69447cf37e64SBarry Smith @*/
6945edbaebb3SBarry Smith PetscErrorCode  TSMonitorError(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscViewerAndFormat *vf)
69467cf37e64SBarry Smith {
69477cf37e64SBarry Smith   PetscErrorCode    ierr;
69487cf37e64SBarry Smith   Vec               y;
69497cf37e64SBarry Smith   PetscReal         nrm;
6950edbaebb3SBarry Smith   PetscBool         flg;
69517cf37e64SBarry Smith 
69527cf37e64SBarry Smith   PetscFunctionBegin;
69537cf37e64SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
69547cf37e64SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
69557cf37e64SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6956edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERASCII,&flg);CHKERRQ(ierr);
6957edbaebb3SBarry Smith   if (flg) {
69587cf37e64SBarry Smith     ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
6959edbaebb3SBarry Smith     ierr = PetscViewerASCIIPrintf(vf->viewer,"2-norm of error %g\n",(double)nrm);CHKERRQ(ierr);
6960edbaebb3SBarry Smith   }
6961edbaebb3SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)vf->viewer,PETSCVIEWERDRAW,&flg);CHKERRQ(ierr);
6962edbaebb3SBarry Smith   if (flg) {
6963edbaebb3SBarry Smith     ierr = VecView(y,vf->viewer);CHKERRQ(ierr);
6964edbaebb3SBarry Smith   }
6965edbaebb3SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
69667cf37e64SBarry Smith   PetscFunctionReturn(0);
69677cf37e64SBarry Smith }
69687cf37e64SBarry Smith 
6969201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(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,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
6982201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6983201da799SBarry Smith     ctx->snes_its = 0;
6984201da799SBarry Smith   }
6985201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
6986201da799SBarry Smith   y    = its - ctx->snes_its;
6987201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
69883fbbecb0SBarry 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->snes_its = its;
6993201da799SBarry Smith   PetscFunctionReturn(0);
6994201da799SBarry Smith }
6995201da799SBarry Smith 
6996201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6997201da799SBarry Smith {
6998201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6999201da799SBarry Smith   PetscReal      x   = ptime,y;
7000201da799SBarry Smith   PetscErrorCode ierr;
7001201da799SBarry Smith   PetscInt       its;
7002201da799SBarry Smith 
7003201da799SBarry Smith   PetscFunctionBegin;
700463e21af5SBarry Smith   if (n < 0) PetscFunctionReturn(0); /* -1 indicates interpolated solution */
7005201da799SBarry Smith   if (!n) {
7006201da799SBarry Smith     PetscDrawAxis axis;
7007201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
7008201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
7009201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
7010201da799SBarry Smith     ctx->ksp_its = 0;
7011201da799SBarry Smith   }
7012201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
7013201da799SBarry Smith   y    = its - ctx->ksp_its;
7014201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
701599fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
7016201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
70176934998bSLisandro Dalcin     ierr = PetscDrawLGSave(ctx->lg);CHKERRQ(ierr);
7018201da799SBarry Smith   }
7019201da799SBarry Smith   ctx->ksp_its = its;
7020201da799SBarry Smith   PetscFunctionReturn(0);
7021201da799SBarry Smith }
7022f9c1d6abSBarry Smith 
7023f9c1d6abSBarry Smith /*@
7024f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
7025f9c1d6abSBarry Smith 
7026f9c1d6abSBarry Smith    Collective on TS and Vec
7027f9c1d6abSBarry Smith 
7028f9c1d6abSBarry Smith    Input Parameters:
7029f9c1d6abSBarry Smith +  ts - the TS context
7030f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
7031f9c1d6abSBarry Smith 
7032f9c1d6abSBarry Smith    Output Parameters:
7033f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
7034f9c1d6abSBarry Smith 
7035f9c1d6abSBarry Smith    Level: developer
7036f9c1d6abSBarry Smith 
7037f9c1d6abSBarry Smith .keywords: TS, compute
7038f9c1d6abSBarry Smith 
7039f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
7040f9c1d6abSBarry Smith @*/
7041f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
7042f9c1d6abSBarry Smith {
7043f9c1d6abSBarry Smith   PetscErrorCode ierr;
7044f9c1d6abSBarry Smith 
7045f9c1d6abSBarry Smith   PetscFunctionBegin;
7046f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7047ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
7048f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
7049f9c1d6abSBarry Smith   PetscFunctionReturn(0);
7050f9c1d6abSBarry Smith }
705124655328SShri 
7052b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
7053b3d3934dSBarry Smith /*@C
7054b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
7055b3d3934dSBarry Smith 
7056b3d3934dSBarry Smith    Collective on TS
7057b3d3934dSBarry Smith 
7058b3d3934dSBarry Smith    Input Parameters:
7059b3d3934dSBarry Smith .  ts  - the ODE solver object
7060b3d3934dSBarry Smith 
7061b3d3934dSBarry Smith    Output Parameter:
7062b3d3934dSBarry Smith .  ctx - the context
7063b3d3934dSBarry Smith 
7064b3d3934dSBarry Smith    Level: intermediate
7065b3d3934dSBarry Smith 
7066b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
7067b3d3934dSBarry Smith 
7068b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
7069b3d3934dSBarry Smith 
7070b3d3934dSBarry Smith @*/
7071b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
7072b3d3934dSBarry Smith {
7073b3d3934dSBarry Smith   PetscErrorCode ierr;
7074b3d3934dSBarry Smith 
7075b3d3934dSBarry Smith   PetscFunctionBegin;
7076a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
7077b3d3934dSBarry Smith   PetscFunctionReturn(0);
7078b3d3934dSBarry Smith }
7079b3d3934dSBarry Smith 
7080b3d3934dSBarry Smith /*@C
7081b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
7082b3d3934dSBarry Smith 
7083b3d3934dSBarry Smith    Collective on TS
7084b3d3934dSBarry Smith 
7085b3d3934dSBarry Smith    Input Parameters:
7086b3d3934dSBarry Smith +  ts - the TS context
7087b3d3934dSBarry Smith .  step - current time-step
7088b3d3934dSBarry Smith .  ptime - current time
70897db568b7SBarry Smith .  u  - current solution
70907db568b7SBarry Smith -  dctx - the envelope context
7091b3d3934dSBarry Smith 
7092b3d3934dSBarry Smith    Options Database:
7093b3d3934dSBarry Smith .  -ts_monitor_envelope
7094b3d3934dSBarry Smith 
7095b3d3934dSBarry Smith    Level: intermediate
7096b3d3934dSBarry Smith 
709795452b02SPatrick Sanan    Notes:
709895452b02SPatrick Sanan     after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
7099b3d3934dSBarry Smith 
7100b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7101b3d3934dSBarry Smith 
71027db568b7SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds(), TSMonitorEnvelopeCtxCreate()
7103b3d3934dSBarry Smith @*/
71047db568b7SBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dctx)
7105b3d3934dSBarry Smith {
7106b3d3934dSBarry Smith   PetscErrorCode       ierr;
71077db568b7SBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dctx;
7108b3d3934dSBarry Smith 
7109b3d3934dSBarry Smith   PetscFunctionBegin;
7110b3d3934dSBarry Smith   if (!ctx->max) {
7111b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
7112b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
7113b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
7114b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
7115b3d3934dSBarry Smith   } else {
7116b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
7117b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
7118b3d3934dSBarry Smith   }
7119b3d3934dSBarry Smith   PetscFunctionReturn(0);
7120b3d3934dSBarry Smith }
7121b3d3934dSBarry Smith 
7122b3d3934dSBarry Smith /*@C
7123b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
7124b3d3934dSBarry Smith 
7125b3d3934dSBarry Smith    Collective on TS
7126b3d3934dSBarry Smith 
7127b3d3934dSBarry Smith    Input Parameter:
7128b3d3934dSBarry Smith .  ts - the TS context
7129b3d3934dSBarry Smith 
7130b3d3934dSBarry Smith    Output Parameter:
7131b3d3934dSBarry Smith +  max - the maximum values
7132b3d3934dSBarry Smith -  min - the minimum values
7133b3d3934dSBarry Smith 
713495452b02SPatrick Sanan    Notes:
713595452b02SPatrick Sanan     If the TS does not have a TSMonitorEnvelopeCtx associated with it then this function is ignored
71367db568b7SBarry Smith 
7137b3d3934dSBarry Smith    Level: intermediate
7138b3d3934dSBarry Smith 
7139b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
7140b3d3934dSBarry Smith 
7141b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
7142b3d3934dSBarry Smith @*/
7143b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
7144b3d3934dSBarry Smith {
7145b3d3934dSBarry Smith   PetscInt i;
7146b3d3934dSBarry Smith 
7147b3d3934dSBarry Smith   PetscFunctionBegin;
7148b3d3934dSBarry Smith   if (max) *max = NULL;
7149b3d3934dSBarry Smith   if (min) *min = NULL;
7150b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
7151b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
71525537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
7153b3d3934dSBarry Smith       if (max) *max = ctx->max;
7154b3d3934dSBarry Smith       if (min) *min = ctx->min;
7155b3d3934dSBarry Smith       break;
7156b3d3934dSBarry Smith     }
7157b3d3934dSBarry Smith   }
7158b3d3934dSBarry Smith   PetscFunctionReturn(0);
7159b3d3934dSBarry Smith }
7160b3d3934dSBarry Smith 
7161b3d3934dSBarry Smith /*@C
7162b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
7163b3d3934dSBarry Smith 
7164b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
7165b3d3934dSBarry Smith 
7166b3d3934dSBarry Smith    Input Parameter:
7167b3d3934dSBarry Smith .  ctx - the monitor context
7168b3d3934dSBarry Smith 
7169b3d3934dSBarry Smith    Level: intermediate
7170b3d3934dSBarry Smith 
7171b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
7172b3d3934dSBarry Smith 
71737db568b7SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep()
7174b3d3934dSBarry Smith @*/
7175b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
7176b3d3934dSBarry Smith {
7177b3d3934dSBarry Smith   PetscErrorCode ierr;
7178b3d3934dSBarry Smith 
7179b3d3934dSBarry Smith   PetscFunctionBegin;
7180b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
7181b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
7182b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7183b3d3934dSBarry Smith   PetscFunctionReturn(0);
7184b3d3934dSBarry Smith }
7185f2dee214SBarry Smith 
718624655328SShri /*@
7187dcb233daSLisandro Dalcin    TSRestartStep - Flags the solver to restart the next step
7188dcb233daSLisandro Dalcin 
7189dcb233daSLisandro Dalcin    Collective on TS
7190dcb233daSLisandro Dalcin 
7191dcb233daSLisandro Dalcin    Input Parameter:
7192dcb233daSLisandro Dalcin .  ts - the TS context obtained from TSCreate()
7193dcb233daSLisandro Dalcin 
7194dcb233daSLisandro Dalcin    Level: advanced
7195dcb233daSLisandro Dalcin 
7196dcb233daSLisandro Dalcin    Notes:
7197dcb233daSLisandro Dalcin    Multistep methods like BDF or Runge-Kutta methods with FSAL property require restarting the solver in the event of
7198dcb233daSLisandro Dalcin    discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution
7199dcb233daSLisandro Dalcin    vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For
7200dcb233daSLisandro Dalcin    the sake of correctness and maximum safety, users are expected to call TSRestart() whenever they introduce
7201dcb233daSLisandro Dalcin    discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with
7202dcb233daSLisandro Dalcin    discontinuous source terms).
7203dcb233daSLisandro Dalcin 
7204dcb233daSLisandro Dalcin .keywords: TS, timestep, restart
7205dcb233daSLisandro Dalcin 
7206dcb233daSLisandro Dalcin .seealso: TSSolve(), TSSetPreStep(), TSSetPostStep()
7207dcb233daSLisandro Dalcin @*/
7208dcb233daSLisandro Dalcin PetscErrorCode TSRestartStep(TS ts)
7209dcb233daSLisandro Dalcin {
7210dcb233daSLisandro Dalcin   PetscFunctionBegin;
7211dcb233daSLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7212dcb233daSLisandro Dalcin   ts->steprestart = PETSC_TRUE;
7213dcb233daSLisandro Dalcin   PetscFunctionReturn(0);
7214dcb233daSLisandro Dalcin }
7215dcb233daSLisandro Dalcin 
7216dcb233daSLisandro Dalcin /*@
721724655328SShri    TSRollBack - Rolls back one time step
721824655328SShri 
721924655328SShri    Collective on TS
722024655328SShri 
722124655328SShri    Input Parameter:
722224655328SShri .  ts - the TS context obtained from TSCreate()
722324655328SShri 
722424655328SShri    Level: advanced
722524655328SShri 
722624655328SShri .keywords: TS, timestep, rollback
722724655328SShri 
722824655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
722924655328SShri @*/
723024655328SShri PetscErrorCode  TSRollBack(TS ts)
723124655328SShri {
723224655328SShri   PetscErrorCode ierr;
723324655328SShri 
723424655328SShri   PetscFunctionBegin;
723524655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7236b3de5cdeSLisandro Dalcin   if (ts->steprollback) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"TSRollBack already called");
723724655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
723824655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
723924655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
724024655328SShri   ts->ptime = ts->ptime_prev;
7241be5899b3SLisandro Dalcin   ts->ptime_prev = ts->ptime_prev_rollback;
72422808aa04SLisandro Dalcin   ts->steps--;
7243b3de5cdeSLisandro Dalcin   ts->steprollback = PETSC_TRUE;
724424655328SShri   PetscFunctionReturn(0);
724524655328SShri }
7246aeb4809dSShri Abhyankar 
7247ff22ae23SHong Zhang /*@
7248ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
7249ff22ae23SHong Zhang 
7250ff22ae23SHong Zhang    Input Parameter:
7251ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
7252ff22ae23SHong Zhang 
72530429704eSStefano Zampini    Output Parameters:
72540429704eSStefano Zampini +  ns - the number of stages
72550429704eSStefano Zampini -  Y - the current stage vectors
72560429704eSStefano Zampini 
7257ff22ae23SHong Zhang    Level: advanced
7258ff22ae23SHong Zhang 
72590429704eSStefano Zampini    Notes: Both ns and Y can be NULL.
72600429704eSStefano Zampini 
7261ff22ae23SHong Zhang .keywords: TS, getstages
7262ff22ae23SHong Zhang 
7263ff22ae23SHong Zhang .seealso: TSCreate()
7264ff22ae23SHong Zhang @*/
7265ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns,Vec **Y)
7266ff22ae23SHong Zhang {
7267ff22ae23SHong Zhang   PetscErrorCode ierr;
7268ff22ae23SHong Zhang 
7269ff22ae23SHong Zhang   PetscFunctionBegin;
7270ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
72710429704eSStefano Zampini   if (ns) PetscValidPointer(ns,2);
72720429704eSStefano Zampini   if (Y) PetscValidPointer(Y,3);
72730429704eSStefano Zampini   if (!ts->ops->getstages) {
72740429704eSStefano Zampini     if (ns) *ns = 0;
72750429704eSStefano Zampini     if (Y) *Y = NULL;
72760429704eSStefano Zampini   } else {
7277ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
7278ff22ae23SHong Zhang   }
7279ff22ae23SHong Zhang   PetscFunctionReturn(0);
7280ff22ae23SHong Zhang }
7281ff22ae23SHong Zhang 
7282847ff0e1SMatthew G. Knepley /*@C
7283847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
7284847ff0e1SMatthew G. Knepley 
7285847ff0e1SMatthew G. Knepley   Collective on SNES
7286847ff0e1SMatthew G. Knepley 
7287847ff0e1SMatthew G. Knepley   Input Parameters:
7288847ff0e1SMatthew G. Knepley + ts - the TS context
7289847ff0e1SMatthew G. Knepley . t - current timestep
7290847ff0e1SMatthew G. Knepley . U - state vector
7291847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
7292847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
7293847ff0e1SMatthew G. Knepley - ctx - an optional user context
7294847ff0e1SMatthew G. Knepley 
7295847ff0e1SMatthew G. Knepley   Output Parameters:
7296847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
7297847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
7298847ff0e1SMatthew G. Knepley 
7299847ff0e1SMatthew G. Knepley   Level: intermediate
7300847ff0e1SMatthew G. Knepley 
7301847ff0e1SMatthew G. Knepley   Notes:
7302847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
7303847ff0e1SMatthew G. Knepley 
7304847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
7305847ff0e1SMatthew G. Knepley 
7306847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
7307847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
7308847ff0e1SMatthew G. Knepley 
7309847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
7310847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
7311847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
7312847ff0e1SMatthew G. Knepley 
7313847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
7314847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
7315847ff0e1SMatthew G. Knepley @*/
7316847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
7317847ff0e1SMatthew G. Knepley {
7318847ff0e1SMatthew G. Knepley   SNES           snes;
7319847ff0e1SMatthew G. Knepley   MatFDColoring  color;
7320847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
7321847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
7322847ff0e1SMatthew G. Knepley 
7323847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
7324c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
7325847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
7326847ff0e1SMatthew G. Knepley   if (!color) {
7327847ff0e1SMatthew G. Knepley     DM         dm;
7328847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
7329847ff0e1SMatthew G. Knepley 
7330847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
7331847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
7332847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
7333847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
7334847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7335847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7336847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7337847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7338847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7339847ff0e1SMatthew G. Knepley     } else {
7340847ff0e1SMatthew G. Knepley       MatColoring mc;
7341847ff0e1SMatthew G. Knepley 
7342847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
7343847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
7344847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
7345847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
7346847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
7347847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
7348847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
7349847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
7350847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
7351847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
7352847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
7353847ff0e1SMatthew G. Knepley     }
7354847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
7355847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
7356847ff0e1SMatthew G. Knepley   }
7357847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
7358847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
7359847ff0e1SMatthew G. Knepley   if (J != B) {
7360847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7361847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
7362847ff0e1SMatthew G. Knepley   }
7363847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
7364847ff0e1SMatthew G. Knepley }
736593b34091SDebojyoti Ghosh 
7366cb9d8021SPierre Barbier de Reuille /*@
7367cb9d8021SPierre Barbier de Reuille     TSSetFunctionDomainError - Set the function testing if the current state vector is valid
7368cb9d8021SPierre Barbier de Reuille 
7369cb9d8021SPierre Barbier de Reuille     Input Parameters:
7370cb9d8021SPierre Barbier de Reuille     ts - the TS context
7371cb9d8021SPierre Barbier de Reuille     func - function called within TSFunctionDomainError
7372cb9d8021SPierre Barbier de Reuille 
7373cb9d8021SPierre Barbier de Reuille     Level: intermediate
7374cb9d8021SPierre Barbier de Reuille 
7375cb9d8021SPierre Barbier de Reuille .keywords: TS, state, domain
7376cb9d8021SPierre Barbier de Reuille .seealso: TSAdaptCheckStage(), TSFunctionDomainError()
7377cb9d8021SPierre Barbier de Reuille @*/
7378cb9d8021SPierre Barbier de Reuille 
7379d183316bSPierre Barbier de Reuille PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS,PetscReal,Vec,PetscBool*))
7380cb9d8021SPierre Barbier de Reuille {
7381cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7382cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
7383cb9d8021SPierre Barbier de Reuille   ts->functiondomainerror = func;
7384cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7385cb9d8021SPierre Barbier de Reuille }
7386cb9d8021SPierre Barbier de Reuille 
7387cb9d8021SPierre Barbier de Reuille /*@
7388cb9d8021SPierre Barbier de Reuille     TSFunctionDomainError - Check if the current state is valid
7389cb9d8021SPierre Barbier de Reuille 
7390cb9d8021SPierre Barbier de Reuille     Input Parameters:
7391cb9d8021SPierre Barbier de Reuille     ts - the TS context
7392cb9d8021SPierre Barbier de Reuille     stagetime - time of the simulation
7393d183316bSPierre Barbier de Reuille     Y - state vector to check.
7394cb9d8021SPierre Barbier de Reuille 
7395cb9d8021SPierre Barbier de Reuille     Output Parameter:
7396cb9d8021SPierre Barbier de Reuille     accept - Set to PETSC_FALSE if the current state vector is valid.
7397cb9d8021SPierre Barbier de Reuille 
7398cb9d8021SPierre Barbier de Reuille     Note:
7399cb9d8021SPierre Barbier de Reuille     This function should be used to ensure the state is in a valid part of the space.
7400cb9d8021SPierre Barbier de Reuille     For example, one can ensure here all values are positive.
740196a0c994SBarry Smith 
740296a0c994SBarry Smith     Level: advanced
7403cb9d8021SPierre Barbier de Reuille @*/
7404d183316bSPierre Barbier de Reuille PetscErrorCode TSFunctionDomainError(TS ts,PetscReal stagetime,Vec Y,PetscBool* accept)
7405cb9d8021SPierre Barbier de Reuille {
7406cb9d8021SPierre Barbier de Reuille   PetscErrorCode ierr;
7407cb9d8021SPierre Barbier de Reuille 
7408cb9d8021SPierre Barbier de Reuille   PetscFunctionBegin;
7409cb9d8021SPierre Barbier de Reuille 
7410cb9d8021SPierre Barbier de Reuille   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7411cb9d8021SPierre Barbier de Reuille   *accept = PETSC_TRUE;
7412cb9d8021SPierre Barbier de Reuille   if (ts->functiondomainerror) {
7413d183316bSPierre Barbier de Reuille     PetscStackCallStandard((*ts->functiondomainerror),(ts,stagetime,Y,accept));
7414cb9d8021SPierre Barbier de Reuille   }
7415cb9d8021SPierre Barbier de Reuille   PetscFunctionReturn(0);
7416cb9d8021SPierre Barbier de Reuille }
74171ceb14c0SBarry Smith 
741893b34091SDebojyoti Ghosh /*@C
7419e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
742093b34091SDebojyoti Ghosh 
742193b34091SDebojyoti Ghosh   Collective on MPI_Comm
742293b34091SDebojyoti Ghosh 
742393b34091SDebojyoti Ghosh   Input Parameter:
742493b34091SDebojyoti Ghosh . tsin    - The input TS
742593b34091SDebojyoti Ghosh 
742693b34091SDebojyoti Ghosh   Output Parameter:
7427e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
742893b34091SDebojyoti Ghosh 
74295eca1a21SEmil Constantinescu   Notes:
74305eca1a21SEmil 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.
74315eca1a21SEmil Constantinescu 
7432928bb9adSStefano 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);
74335eca1a21SEmil Constantinescu 
74345eca1a21SEmil Constantinescu   Level: developer
743593b34091SDebojyoti Ghosh 
7436e5168f73SEmil Constantinescu .keywords: TS, clone
7437e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
743893b34091SDebojyoti Ghosh @*/
7439baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
744093b34091SDebojyoti Ghosh {
744193b34091SDebojyoti Ghosh   TS             t;
744293b34091SDebojyoti Ghosh   PetscErrorCode ierr;
7443dc846ba4SSatish Balay   SNES           snes_start;
7444dc846ba4SSatish Balay   DM             dm;
7445dc846ba4SSatish Balay   TSType         type;
744693b34091SDebojyoti Ghosh 
744793b34091SDebojyoti Ghosh   PetscFunctionBegin;
744893b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
744993b34091SDebojyoti Ghosh   *tsout = NULL;
745093b34091SDebojyoti Ghosh 
74517a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
745293b34091SDebojyoti Ghosh 
745393b34091SDebojyoti Ghosh   /* General TS description */
745493b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
745593b34091SDebojyoti Ghosh   t->setupcalled       = 0;
745693b34091SDebojyoti Ghosh   t->ksp_its           = 0;
745793b34091SDebojyoti Ghosh   t->snes_its          = 0;
745893b34091SDebojyoti Ghosh   t->nwork             = 0;
745993b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
746093b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
746193b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
746293b34091SDebojyoti Ghosh 
746334561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);CHKERRQ(ierr);
746434561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);CHKERRQ(ierr);
7465d15a3a53SEmil Constantinescu 
746693b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);CHKERRQ(ierr);
746793b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);CHKERRQ(ierr);
746893b34091SDebojyoti Ghosh 
746993b34091SDebojyoti Ghosh   t->adapt = tsin->adapt;
747051699248SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)t->adapt);CHKERRQ(ierr);
747193b34091SDebojyoti Ghosh 
7472e7069c78SShri   t->trajectory = tsin->trajectory;
7473e7069c78SShri   ierr = PetscObjectReference((PetscObject)t->trajectory);CHKERRQ(ierr);
7474e7069c78SShri 
7475e7069c78SShri   t->event = tsin->event;
74766b10a48eSSatish Balay   if (t->event) t->event->refct++;
7477e7069c78SShri 
747893b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
747993b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
7480e7069c78SShri   t->ptime_prev        = tsin->ptime_prev;
748193b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
748293b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
748393b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
748493b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
748593b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
748693b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
748793b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
748893b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
748993b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
749093b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
749193b34091SDebojyoti Ghosh 
749293b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type);CHKERRQ(ierr);
749393b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);CHKERRQ(ierr);
749493b34091SDebojyoti Ghosh 
749593b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
749693b34091SDebojyoti Ghosh 
749793b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
749893b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
749993b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
750093b34091SDebojyoti Ghosh 
750193b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
750293b34091SDebojyoti Ghosh 
75030d4fed19SBarry Smith   if (((PetscObject)tsin)->fortran_func_pointers) {
75040d4fed19SBarry Smith     PetscInt i;
75050d4fed19SBarry Smith     ierr = PetscMalloc((10)*sizeof(void(*)(void)),&((PetscObject)t)->fortran_func_pointers);CHKERRQ(ierr);
75060d4fed19SBarry Smith     for (i=0; i<10; i++) {
75070d4fed19SBarry Smith       ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i];
75080d4fed19SBarry Smith     }
75090d4fed19SBarry Smith   }
751093b34091SDebojyoti Ghosh   *tsout = t;
751193b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
751293b34091SDebojyoti Ghosh }
7513f3b1f45cSBarry Smith 
7514f3b1f45cSBarry Smith static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void* ctx,Vec x,Vec y)
7515f3b1f45cSBarry Smith {
7516f3b1f45cSBarry Smith   PetscErrorCode ierr;
7517f3b1f45cSBarry Smith   TS             ts = (TS) ctx;
7518f3b1f45cSBarry Smith 
7519f3b1f45cSBarry Smith   PetscFunctionBegin;
7520f3b1f45cSBarry Smith   ierr = TSComputeRHSFunction(ts,0,x,y);CHKERRQ(ierr);
7521f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7522f3b1f45cSBarry Smith }
7523f3b1f45cSBarry Smith 
7524f3b1f45cSBarry Smith /*@
7525f3b1f45cSBarry Smith     TSRHSJacobianTest - Compares the multiply routine provided to the MATSHELL with differencing on the TS given RHS function.
7526f3b1f45cSBarry Smith 
7527f3b1f45cSBarry Smith    Logically Collective on TS and Mat
7528f3b1f45cSBarry Smith 
7529f3b1f45cSBarry Smith     Input Parameters:
7530f3b1f45cSBarry Smith     TS - the time stepping routine
7531f3b1f45cSBarry Smith 
7532f3b1f45cSBarry Smith    Output Parameter:
7533f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7534f3b1f45cSBarry Smith 
7535f3b1f45cSBarry Smith    Options Database:
7536f3b1f45cSBarry Smith  .   -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator
7537f3b1f45cSBarry Smith 
7538f3b1f45cSBarry Smith    Level: advanced
7539f3b1f45cSBarry Smith 
754095452b02SPatrick Sanan    Notes:
754195452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7542f3b1f45cSBarry Smith 
7543f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTestTranspose()
7544f3b1f45cSBarry Smith @*/
7545f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTest(TS ts,PetscBool *flg)
7546f3b1f45cSBarry Smith {
7547f3b1f45cSBarry Smith   Mat            J,B;
7548f3b1f45cSBarry Smith   PetscErrorCode ierr;
7549f3b1f45cSBarry Smith   TSRHSJacobian  func;
7550f3b1f45cSBarry Smith   void*          ctx;
7551f3b1f45cSBarry Smith 
7552f3b1f45cSBarry Smith   PetscFunctionBegin;
7553f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7554f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7555f3b1f45cSBarry Smith   ierr = MatShellTestMult(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7556f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7557f3b1f45cSBarry Smith }
7558f3b1f45cSBarry Smith 
7559f3b1f45cSBarry Smith /*@C
7560f3b1f45cSBarry Smith     TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the MATSHELL with differencing on the TS given RHS function.
7561f3b1f45cSBarry Smith 
7562f3b1f45cSBarry Smith    Logically Collective on TS and Mat
7563f3b1f45cSBarry Smith 
7564f3b1f45cSBarry Smith     Input Parameters:
7565f3b1f45cSBarry Smith     TS - the time stepping routine
7566f3b1f45cSBarry Smith 
7567f3b1f45cSBarry Smith    Output Parameter:
7568f3b1f45cSBarry Smith .   flg - PETSC_TRUE if the multiply is likely correct
7569f3b1f45cSBarry Smith 
7570f3b1f45cSBarry Smith    Options Database:
7571f3b1f45cSBarry Smith .   -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator
7572f3b1f45cSBarry Smith 
757395452b02SPatrick Sanan    Notes:
757495452b02SPatrick Sanan     This only works for problems defined only the RHS function and Jacobian NOT IFunction and IJacobian
7575f3b1f45cSBarry Smith 
7576f3b1f45cSBarry Smith    Level: advanced
7577f3b1f45cSBarry Smith 
7578f3b1f45cSBarry Smith .seealso: MatCreateShell(), MatShellGetContext(), MatShellGetOperation(), MatShellTestMultTranspose(), TSRHSJacobianTest()
7579f3b1f45cSBarry Smith @*/
7580f3b1f45cSBarry Smith PetscErrorCode  TSRHSJacobianTestTranspose(TS ts,PetscBool *flg)
7581f3b1f45cSBarry Smith {
7582f3b1f45cSBarry Smith   Mat            J,B;
7583f3b1f45cSBarry Smith   PetscErrorCode ierr;
7584f3b1f45cSBarry Smith   void           *ctx;
7585f3b1f45cSBarry Smith   TSRHSJacobian  func;
7586f3b1f45cSBarry Smith 
7587f3b1f45cSBarry Smith   PetscFunctionBegin;
7588f3b1f45cSBarry Smith   ierr = TSGetRHSJacobian(ts,&J,&B,&func,&ctx);CHKERRQ(ierr);
7589f3b1f45cSBarry Smith   ierr = (*func)(ts,0.0,ts->vec_sol,J,B,ctx);CHKERRQ(ierr);
7590f3b1f45cSBarry Smith   ierr = MatShellTestMultTranspose(J,RHSWrapperFunction_TSRHSJacobianTest,ts->vec_sol,ts,flg);CHKERRQ(ierr);
7591f3b1f45cSBarry Smith   PetscFunctionReturn(0);
7592f3b1f45cSBarry Smith }
7593